From 9477f1f7226d32f68e1709fbb136b41b054de58c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20=E2=80=98Touky=E2=80=99=20Huet?= Date: Mon, 23 Mar 2015 05:43:27 +0000 Subject: [PATCH] Moved GetKey/Axis status tests to protected, and made them accessible through controller directly to implement layer system in the future. --- doc/samples/btphystest.cpp | 16 ++++++++-------- doc/samples/meshviewer.cpp | 14 +++++++------- doc/tutorial/07_input.cpp | 16 ++++++++-------- doc/tutorial/12_voronoi.cpp | 12 ++++++------ src/imgui/lolimgui.cpp | 12 ++++++------ src/input/controller.cpp | 38 ++++++++++++++++++++++++++++++++++++- src/input/controller.h | 33 ++++++++++++++++++++++++++------ 7 files changed, 99 insertions(+), 42 deletions(-) diff --git a/doc/samples/btphystest.cpp b/doc/samples/btphystest.cpp index ff08765a..bda5be9a 100644 --- a/doc/samples/btphystest.cpp +++ b/doc/samples/btphystest.cpp @@ -317,7 +317,7 @@ void BtPhysTest::TickGame(float seconds) } - if (m_controller->GetKey(KEY_QUIT).JustReleased()) + if (m_controller->WasKeyReleased(KEY_QUIT)) Ticker::Shutdown(); m_loop_value += seconds; @@ -500,15 +500,15 @@ void BtPhysTest::TickGame(float seconds) mat4 CtlrMx = Character->GetTransform(); vec3 movement(0.f); - movement.z = (m_controller->GetKey(KEY_MOVE_RIGHT).IsPressed() ? 1.f : 0.f) - - (m_controller->GetKey(KEY_MOVE_LEFT).IsPressed() ? 1.f : 0.f); - movement.x = (m_controller->GetKey(KEY_MOVE_FORWARD).IsPressed() ? 1.f : 0.f) - - (m_controller->GetKey(KEY_MOVE_BACK).IsPressed() ? 1.f : 0.f); - movement.y = (m_controller->GetKey(KEY_MOVE_UP).IsPressed() ? 1.f : 0.f) - - (m_controller->GetKey(KEY_MOVE_DOWN).IsPressed() ? 1.f : 0.f); + movement.z = (m_controller->IsKeyPressed(KEY_MOVE_RIGHT) ? 1.f : 0.f) + - (m_controller->IsKeyPressed(KEY_MOVE_LEFT) ? 1.f : 0.f); + movement.x = (m_controller->IsKeyPressed(KEY_MOVE_FORWARD) ? 1.f : 0.f) + - (m_controller->IsKeyPressed(KEY_MOVE_BACK) ? 1.f : 0.f); + movement.y = (m_controller->IsKeyPressed(KEY_MOVE_UP) ? 1.f : 0.f) + - (m_controller->IsKeyPressed(KEY_MOVE_DOWN) ? 1.f : 0.f); vec3 CharMove = movement * seconds * vec3(4.f, 10.f, 4.f); - if (m_controller->GetKey(KEY_MOVE_JUMP).JustReleased()) + if (m_controller->WasKeyReleased(KEY_MOVE_JUMP)) Character->Jump(); Character->SetMovementForFrame(CharMove); diff --git a/doc/samples/meshviewer.cpp b/doc/samples/meshviewer.cpp index 49b3e960..1fb53f9e 100644 --- a/doc/samples/meshviewer.cpp +++ b/doc/samples/meshviewer.cpp @@ -248,13 +248,13 @@ public: } #if NO_NACL_EM_INPUT - bool KeyReleased(MVKeyboardList index) { return (HAS_KBOARD && m_controller->GetKey(index).JustReleased()); } - bool KeyPressed(MVKeyboardList index) { return (HAS_KBOARD && m_controller->GetKey(index).JustPressed()); } - bool KeyDown(MVKeyboardList index) { return (HAS_KBOARD && m_controller->GetKey(index).IsPressed()); } - bool KeyReleased(MVMouseKeyList index) { return (HAS_MOUSE && m_controller->GetKey(index).JustReleased()); } - bool KeyPressed(MVMouseKeyList index) { return (HAS_MOUSE && m_controller->GetKey(index).JustPressed()); } - bool KeyDown(MVMouseKeyList index) { return (HAS_MOUSE && m_controller->GetKey(index).IsPressed()); } - float AxisValue(MVMouseAxisList index) { return (HAS_MOUSE)?(m_controller->GetAxis(index).GetValue()):(0.f); } + bool KeyReleased(MVKeyboardList index) { return (HAS_KBOARD && m_controller->WasKeyReleased(index)); } + bool KeyPressed(MVKeyboardList index) { return (HAS_KBOARD && m_controller->WasKeyPressed(index)); } + bool KeyDown(MVKeyboardList index) { return (HAS_KBOARD && m_controller->IsKeyPressed(index)); } + bool KeyReleased(MVMouseKeyList index) { return (HAS_MOUSE && m_controller->WasKeyReleased(index)); } + bool KeyPressed(MVMouseKeyList index) { return (HAS_MOUSE && m_controller->WasKeyPressed(index)); } + bool KeyDown(MVMouseKeyList index) { return (HAS_MOUSE && m_controller->IsKeyPressed(index)); } + float AxisValue(MVMouseAxisList index) { return (HAS_MOUSE) ? (m_controller->GetAxisValue(index)) : (0.f); } #endif //NO_NACL_EM_INPUT void Init() diff --git a/doc/tutorial/07_input.cpp b/doc/tutorial/07_input.cpp index af4884be..81de3951 100644 --- a/doc/tutorial/07_input.cpp +++ b/doc/tutorial/07_input.cpp @@ -107,27 +107,27 @@ public: /* Handle keyboard */ if (m_keyboard) { - if (m_controller->GetKey(KEY_MANUAL_ROTATION).JustPressed()) + if (m_controller->WasKeyPressed(KEY_MANUAL_ROTATION)) m_autorot = !m_autorot; } /* Handle joystick */ if (m_joystick) { - if (lol::abs(m_controller->GetAxis(AXIS_PITCH).GetValue()) > 0.2f) - m_pitch_angle += m_controller->GetAxis(AXIS_PITCH).GetValue() * seconds * 100; - if (lol::abs(m_controller->GetAxis(AXIS_YAW).GetValue()) > 0.2f) - m_yaw_angle += m_controller->GetAxis(AXIS_YAW).GetValue() * seconds * 100; + if (lol::abs(m_controller->GetAxisValue(AXIS_PITCH)) > 0.2f) + m_pitch_angle += m_controller->GetAxisValue(AXIS_PITCH) * seconds * 100; + if (lol::abs(m_controller->GetAxisValue(AXIS_YAW)) > 0.2f) + m_yaw_angle += m_controller->GetAxisValue(AXIS_YAW) * seconds * 100; } /* Handle mouse */ if (m_mouse) { - if (m_controller->GetKey(KEY_DRAG_MESH).IsPressed()) + if (m_controller->IsKeyPressed(KEY_DRAG_MESH)) { InputDevice::CaptureMouse(true); - m_pitch_angle -= m_controller->GetAxis(AXIS_DRAG_PITCH).GetValue() * seconds * 100; - m_yaw_angle += m_controller->GetAxis(AXIS_DRAG_YAW).GetValue() * seconds * 100; + m_pitch_angle -= m_controller->GetAxisValue(AXIS_DRAG_PITCH) * seconds * 100; + m_yaw_angle += m_controller->GetAxisValue(AXIS_DRAG_YAW) * seconds * 100; } else { diff --git a/doc/tutorial/12_voronoi.cpp b/doc/tutorial/12_voronoi.cpp index 8ebb309f..d218b26d 100644 --- a/doc/tutorial/12_voronoi.cpp +++ b/doc/tutorial/12_voronoi.cpp @@ -78,7 +78,7 @@ public: { //Shutdown logic - if (m_controller->GetKey(KEY_ESC).JustReleased()) + if (m_controller->WasKeyReleased(KEY_ESC)) Ticker::Shutdown(); } @@ -169,16 +169,16 @@ public: { //Shutdown logic - if (m_controller->GetKey(KEY_POP).JustReleased()) + if (m_controller->WasKeyReleased(KEY_POP)) voronoi_points.Pop(); - else if (m_controller->GetKey(KEY_PUSH).JustReleased()) + else if (m_controller->WasKeyReleased(KEY_PUSH)) voronoi_points.Push(vec3(rand(512.f), rand(512.f), .0f), vec2(64.f + rand(64.f), 64.f + rand(64.f))); - else if (m_controller->GetKey(KEY_F1).JustReleased()) + else if (m_controller->WasKeyReleased(KEY_F1)) m_cur_fbo = SrcVoronoiFbo; - else if (m_controller->GetKey(KEY_F2).JustReleased()) + else if (m_controller->WasKeyReleased(KEY_F2)) m_cur_fbo = VoronoiFbo; - else if (m_controller->GetKey(KEY_F3).JustReleased()) + else if (m_controller->WasKeyReleased(KEY_F3)) { voronoi_points.Empty(); if (mode == 0) diff --git a/src/imgui/lolimgui.cpp b/src/imgui/lolimgui.cpp index 0f7515bd..aa6ac8f4 100644 --- a/src/imgui/lolimgui.cpp +++ b/src/imgui/lolimgui.cpp @@ -197,15 +197,15 @@ void LolImGui::TickGame(float seconds) switch (i) { default: - io.KeysDown[i] = m_controller->GetKey(i).IsPressed(); + io.KeysDown[i] = m_controller->IsKeyPressed(i); break; case LolImGuiKey::LShift: case LolImGuiKey::RShift: - io.KeyShift = (io.KeyShift || m_controller->GetKey(i).IsPressed()); + io.KeyShift = (io.KeyShift || m_controller->IsKeyPressed(i)); break; case LolImGuiKey::LCtrl: case LolImGuiKey::RCtrl: - io.KeyCtrl = (io.KeyCtrl || m_controller->GetKey(i).IsPressed()); + io.KeyCtrl = (io.KeyCtrl || m_controller->IsKeyPressed(i)); break; } } @@ -223,17 +223,17 @@ void LolImGui::TickGame(float seconds) cursor.y = 1.f - cursor.y; cursor *= video_size; io.MousePos = ImVec2(cursor.x, cursor.y); - io.MouseWheel = m_controller->GetAxis(LolImGuiAxis::Scroll).GetValue(); + io.MouseWheel = m_controller->GetAxisValue(LolImGuiAxis::Scroll); for (int i = LolImGuiKey::MOUSE_KEY_START; i < LolImGuiKey::MOUSE_KEY_END; ++i) { switch (i) { default: - io.MouseDown[i - LolImGuiKey::MOUSE_KEY_START] = m_controller->GetKey(i).IsPressed(); + io.MouseDown[i - LolImGuiKey::MOUSE_KEY_START] = m_controller->IsKeyPressed(i); break; case LolImGuiKey::Focus: - if (m_controller->GetKey(i).IsPressed()) + if (m_controller->IsKeyPressed(i)) io.MousePos = ImVec2(-1.f, -1.f); break; } diff --git a/src/input/controller.cpp b/src/input/controller.cpp index 3706a0ae..771f959c 100644 --- a/src/input/controller.cpp +++ b/src/input/controller.cpp @@ -295,13 +295,49 @@ bool Controller::IsLayerActive() //GetKeys/Axis stuff ---------------------------------------------------------- KeyBinding& Controller::GetKey(int index) { -//#error do something better IsLayerActive() + return m_keys[index]; +} +KeyBinding const& Controller::GetKey(int index) const +{ return m_keys[index]; } AxisBinding& Controller::GetAxis(int index) { return m_axis[index]; } +AxisBinding const& Controller::GetAxis(int index) const +{ + return m_axis[index]; +} + +//Key methods: should not go directly to binding ------------------------------ +bool Controller::IsKeyPressed(int index) const +{ +//#error do something better IsLayerActive() + return GetKey(index).IsPressed(); +} +bool Controller::IsKeyReleased(int index) const +{ + return GetKey(index).IsReleased(); +} +bool Controller::WasKeyPressed(int index) const +{ + return GetKey(index).WasKeyPressed(); +} +bool Controller::WasKeyReleased(int index) const +{ + return GetKey(index).WasKeyReleased(); +} + +//Axis methods: should not go directly to binding ----------------------------- +float Controller::GetAxisValue(int index) const +{ + return GetAxis(index).GetValue(); +} +float Controller::GetAxisDelta(int index) const +{ + return GetAxis(index).GetDelta(); +} //----------------------------------------------------------------------------- Controller* Controller::Get(String const &name) diff --git a/src/input/controller.h b/src/input/controller.h index decc36dc..b1fed79a 100644 --- a/src/input/controller.h +++ b/src/input/controller.h @@ -16,22 +16,25 @@ namespace lol //----------------------------------------------------------------------------- class KeyBinding { + friend class Controller; public: KeyBinding() : m_current(false), m_previous(false) {} +protected: //Status methods ---------------------------------------------------------- /** Indicates wheither the key is currently down */ bool IsPressed() const { return m_current; } /** Indicates wheither the key is currently up */ bool IsReleased() const { return !m_current; } /** Indicates wheither the key has just been pressed */ - bool JustPressed() const { return m_current && !m_previous; } + bool WasKeyPressed() const { return m_current && !m_previous; } /** Indicates wheither the key has just been released */ - bool JustReleased() const { return !m_current && m_previous; } + bool WasKeyReleased() const { return !m_current && m_previous; } +public: //Binding methods --------------------------------------------------------- /** Bind a physical device and key */ void Bind(const String& device_name, const String& key_name); @@ -67,25 +70,26 @@ protected: bool m_current; /** Value at the current frame */ bool m_previous; - - friend class Controller; }; //----------------------------------------------------------------------------- class AxisBinding { + friend class Controller; public: AxisBinding() : m_current(0.0f), m_previous(0.0f) {} +protected: //Status methods ---------------------------------------------------------- /** Gets the current absolute value of this axis */ float GetValue() const { return m_current; } /** Gets the current delta value of this axis */ float GetDelta() const { return m_current - m_previous; } +public: //Binding methods --------------------------------------------------------- /** Bind a physical device and axis */ void Bind(const String& device_name, const String& axis_name); @@ -132,8 +136,6 @@ protected: array m_keybindings; float m_current; float m_previous; - - friend class Controller; }; //------------------------------------------------------------------------- @@ -348,8 +350,27 @@ public: /** GetKeys/Axis stuff */ KeyBinding& GetKey(int index); + KeyBinding const& GetKey(int index) const; AxisBinding& GetAxis(int index); + AxisBinding const& GetAxis(int index) const; + + /** Key methods: should not go directly to binding */ + /** Indicates wheither the key is currently down */ + bool IsKeyPressed(int index) const; + /** Indicates wheither the key is currently up */ + bool IsKeyReleased(int index) const; + /** Indicates wheither the key has just been pressed */ + bool WasKeyPressed(int index) const; + /** Indicates wheither the key has just been released */ + bool WasKeyReleased(int index) const; + + /** Axis methods: should not go directly to binding */ + /** Gets the current absolute value of this axis */ + float GetAxisValue(int index) const; + /** Gets the current delta value of this axis */ + float GetAxisDelta(int index) const; + /** Get named controller */ static Controller* Get(String const &name); protected: