瀏覽代碼

Controller menu navigation & limit camera rotation

Thomas Buck 9 年之前
父節點
當前提交
187855894f
共有 6 個文件被更改,包括 62 次插入43 次删除
  1. 4
    0
      ChangeLog.md
  2. 0
    1
      include/Camera.h
  3. 0
    1
      include/Game.h
  4. 20
    32
      src/Camera.cpp
  5. 2
    8
      src/Game.cpp
  6. 36
    1
      src/UI.cpp

+ 4
- 0
ChangeLog.md 查看文件

@@ -2,6 +2,10 @@
2 2
 
3 3
 ## OpenRaider (0.1.3) xythobuz <xythobuz@xythobuz.de>
4 4
 
5
+    [ 20140213 ]
6
+    * The menu can be navigated using a controller
7
+    * Vertical camera rotation is now also clamped when using a controller
8
+
5 9
     [ 20140211 ]
6 10
     * Updated imgui to version 1.32
7 11
     * Included some SDL game controller configurations

+ 0
- 1
include/Camera.h 查看文件

@@ -21,7 +21,6 @@ class Camera {
21 21
     static void handleAction(ActionEvents action, bool isFinished);
22 22
     static void handleMouseMotion(int x, int y);
23 23
     static void handleControllerAxis(float value, KeyboardButton axis);
24
-    static void handleControllerButton(KeyboardButton button, bool released);
25 24
 
26 25
     //! \fixme The Y axis seems to be the source of all evil?
27 26
     static void setPosition(glm::vec3 p) { pos = glm::vec3(p.x, -p.y, p.z); }

+ 0
- 1
include/Game.h 查看文件

@@ -23,7 +23,6 @@ class Game {
23 23
     static void handleAction(ActionEvents action, bool isFinished);
24 24
     static void handleMouseMotion(int xrel, int yrel, int xabs, int yabs);
25 25
     static void handleControllerAxis(float value, KeyboardButton axis);
26
-    static void handleControllerButton(KeyboardButton button, bool released);
27 26
 
28 27
     static Entity& getLara();
29 28
     static void setLara(long lara);

+ 20
- 32
src/Camera.cpp 查看文件

@@ -37,8 +37,10 @@ const static float fov = 45.0f;
37 37
 const static float nearDist = 0.1f;
38 38
 const static float farDist = 75000.0f;
39 39
 const static float maxSpeed = 3072.0f;
40
+const static float controllerDeadZone = 0.33f;
40 41
 const static float controllerViewFactor = glm::pi<float>();
41
-const static float controllerDeadZone = 0.2f;
42
+const static float rotationAngleClamp = glm::pi<float>() * 2.0f;
43
+const static float rotationAngleVertMax = glm::pi<float>() / 2.0f;
42 44
 
43 45
 const static glm::vec3 rightUnit(1.0f, 0.0f, 0.0f);
44 46
 const static glm::vec3 upUnit(0.0f, 1.0f, 0.0f);
@@ -111,32 +113,24 @@ void Camera::handleMouseMotion(int x, int y) {
111 113
     }
112 114
 
113 115
     while (y > 0) {
114
-        if (rot.y > -(glm::pi<float>() / 2.0f)) {
116
+        if (rot.y > -rotationAngleVertMax) {
115 117
             rot.y -= rotationDeltaY;
116 118
         }
117 119
         y--;
118 120
     }
119 121
 
120 122
     while (y < 0) {
121
-        if (rot.y < (glm::pi<float>() / 2.0f)) {
123
+        if (rot.y < rotationAngleVertMax) {
122 124
             rot.y += rotationDeltaY;
123 125
         }
124 126
         y++;
125 127
     }
126
-
127
-    while (rot.x > (glm::pi<float>() * 2.0f))
128
-        rot.x -= glm::pi<float>() * 2.0f;
129
-
130
-    while (rot.x < -(glm::pi<float>() * 2.0f))
131
-        rot.x += glm::pi<float>() * 2.0f;
132 128
 }
133 129
 
134 130
 void Camera::handleControllerAxis(float value, KeyboardButton axis) {
135 131
     if (glm::epsilonEqual(value, 0.0f, controllerDeadZone))
136 132
         value = 0.0f;
137 133
 
138
-    // TODO clamp Y rotation axis somehow...?
139
-
140 134
     if (axis == leftXAxis) {
141 135
         posSpeed.x = -maxSpeed * value;
142 136
     } else if (axis == leftYAxis) {
@@ -152,32 +146,26 @@ void Camera::handleControllerAxis(float value, KeyboardButton axis) {
152 146
     dirty = true;
153 147
 }
154 148
 
155
-void Camera::handleControllerButton(KeyboardButton button, bool released) {
156
-    if (button == aButton) {
157
-        handleAction(jumpAction, released);
158
-    } else if (button == bButton) {
159
-        handleAction(crouchAction, released);
160
-    } else if (button == padUp) {
161
-        handleAction(forwardAction, released);
162
-    } else if (button == padDown) {
163
-        handleAction(backwardAction, released);
164
-    } else if (button == padLeft) {
165
-        handleAction(leftAction, released);
166
-    } else if (button == padRight) {
167
-        handleAction(rightAction, released);
168
-    } else {
169
-        return;
170
-    }
171
-
172
-    dirty = true;
173
-}
174
-
175 149
 bool Camera::update() {
176 150
     if ((!dirty) && equal(posSpeed, 0.0f) && equal(rotSpeed, 0.0f))
177 151
         return false;
178 152
 
153
+    while (rot.x > rotationAngleClamp)
154
+        rot.x -= rotationAngleClamp;
155
+    while (rot.x < -rotationAngleClamp)
156
+        rot.x += rotationAngleClamp;
157
+    while (rot.y > rotationAngleClamp)
158
+        rot.y -= rotationAngleClamp;
159
+    while (rot.y < -rotationAngleClamp)
160
+        rot.y += rotationAngleClamp;
161
+
179 162
     float dT = RunTime::getLastFrameTime();
180
-    rot += rotSpeed * dT;
163
+    glm::vec2 newRot = rot + rotSpeed * dT;
164
+
165
+    if ((newRot.y > -rotationAngleVertMax) && (newRot.y < rotationAngleVertMax))
166
+        rot = newRot;
167
+    else
168
+        rotSpeed = glm::vec2(0.0f, 0.0f);
181 169
 
182 170
     glm::quat quatY = glm::angleAxis(rot.x, glm::vec3(0.0f, 1.0f, 0.0f));
183 171
     glm::quat quatX = glm::angleAxis(rot.y, glm::vec3(1.0f, 0.0f, 0.0f));

+ 2
- 8
src/Game.cpp 查看文件

@@ -87,8 +87,9 @@ void Game::handleAction(ActionEvents action, bool isFinished) {
87 87
         return;
88 88
 
89 89
     if (isFinished) {
90
+        if (activeEvents[action])
91
+            Camera::handleAction(action, isFinished);
90 92
         activeEvents[action] = false;
91
-        Camera::handleAction(action, isFinished);
92 93
     } else {
93 94
         if (!activeEvents[action])
94 95
             Camera::handleAction(action, isFinished);
@@ -110,13 +111,6 @@ void Game::handleControllerAxis(float value, KeyboardButton axis) {
110 111
     Camera::handleControllerAxis(value, axis);
111 112
 }
112 113
 
113
-void Game::handleControllerButton(KeyboardButton button, bool released) {
114
-    if (!mLoaded)
115
-        return;
116
-
117
-    Camera::handleControllerButton(button, released);
118
-}
119
-
120 114
 Entity& Game::getLara() {
121 115
     assert(mLara >= 0);
122 116
     assert(mLara < (int)getWorld().sizeEntity());

+ 36
- 1
src/UI.cpp 查看文件

@@ -360,7 +360,42 @@ void UI::handleControllerAxis(float value, KeyboardButton axis) {
360 360
 }
361 361
 
362 362
 void UI::handleControllerButton(KeyboardButton button, bool released) {
363
-    Game::handleControllerButton(button, released);
363
+    if (visible || Console::isVisible())
364
+        return;
365
+
366
+    if (getMenu().isVisible()) {
367
+        if (button == aButton) {
368
+            handleKeyboard(enterKey, !released);
369
+        } else if (button == padUp) {
370
+            handleKeyboard(upKey, !released);
371
+        } else if (button == padDown) {
372
+            handleKeyboard(downKey, !released);
373
+        } else if (button == padLeft) {
374
+            handleKeyboard(leftKey, !released);
375
+        } else if (button == padRight) {
376
+            handleKeyboard(rightKey, !released);
377
+        } else if (button == startButton) {
378
+            if (!released)
379
+                getMenu().setVisible(false);
380
+        }
381
+    } else {
382
+        if (button == aButton) {
383
+            Game::handleAction(jumpAction, released);
384
+        } else if (button == bButton) {
385
+            Game::handleAction(crouchAction, released);
386
+        } else if (button == padUp) {
387
+            Game::handleAction(forwardAction, released);
388
+        } else if (button == padDown) {
389
+            Game::handleAction(backwardAction, released);
390
+        } else if (button == padLeft) {
391
+            Game::handleAction(leftAction, released);
392
+        } else if (button == padRight) {
393
+            Game::handleAction(rightAction, released);
394
+        } else if (button == startButton) {
395
+            if (!released)
396
+                getMenu().setVisible(true);
397
+        }
398
+    }
364 399
 }
365 400
 
366 401
 void UI::renderImGui(ImDrawList** const cmd_lists, int cmd_lists_count) {

Loading…
取消
儲存