From 46f377c94c4743d987fc7c2f1e02421726259cec Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Mon, 2 Jun 2014 22:56:39 +0200 Subject: [PATCH] Interface input revised --- src/GUI/Frame.h | 16 ++++++--- src/GUI/GameFrame.h | 4 +-- src/GUI/VehicleSelection.h | 73 ++++++++++++++++++++++++++------------ src/GameWorld.cpp | 16 +++++++++ 4 files changed, 79 insertions(+), 30 deletions(-) diff --git a/src/GUI/Frame.h b/src/GUI/Frame.h index 47d5146..f82095f 100644 --- a/src/GUI/Frame.h +++ b/src/GUI/Frame.h @@ -6,6 +6,9 @@ #include "Util/Rectangle.h" #include "EventBroker.h" +#include "Events/KeyDown.h" +#include "Events/KeyUp.h" +#include "Events/InputCommand.h" #include "ResourceManager.h" #include "Renderer.h" #include "RenderQueue.h" @@ -129,11 +132,6 @@ public: return Rectangle(left, top, width, height); } - virtual bool OnKeyDown(const Events::KeyDown &event) { return false; } - virtual bool OnKeyUp(const Events::KeyUp &event) { return false; } - //virtual bool OnMouseDown(const Events::KeyDown &event) { } - //virtual bool OnMouseUp(const Events::KeyDown &event) { } - void UpdateLayered(double dt) { if (this->Hidden()) @@ -196,14 +194,22 @@ protected: typedef std::multimap> Children_t; // name -> frame std::map m_Children; // layer -> Children_t + virtual bool OnKeyDown(const Events::KeyDown &event) { return false; } + virtual bool OnKeyUp(const Events::KeyUp &event) { return false; } + //virtual bool OnMouseDown(const Events::KeyDown &event) { } + //virtual bool OnMouseUp(const Events::KeyDown &event) { } + virtual bool OnCommand(const Events::InputCommand &event) { return false; } + private: EventRelay m_EKeyDown; EventRelay m_EKeyUp; + EventRelay m_EInputCommand; void Initialize() { EVENT_SUBSCRIBE_MEMBER(m_EKeyDown, &Frame::OnKeyDown); EVENT_SUBSCRIBE_MEMBER(m_EKeyUp, &Frame::OnKeyUp); + EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &Frame::OnCommand); } }; diff --git a/src/GUI/GameFrame.h b/src/GUI/GameFrame.h index f180835..1100dc9 100644 --- a/src/GUI/GameFrame.h +++ b/src/GUI/GameFrame.h @@ -25,8 +25,8 @@ public: vp1 = new Viewport(worldFrame, "Viewport1", m_World); vp1->X = 0; vp1->Width = this->Width / 2.f; - new PlayerHUD(vp1, "PlayerHUD", m_World, 1); - //new VehicleSelection(vp1, "VehicleSelection", m_World, 1); + //new PlayerHUD(vp1, "PlayerHUD", m_World, 1); + new VehicleSelection(vp1, "VehicleSelection", m_World, 1); vp2 = new Viewport(worldFrame, "Viewport2", m_World); vp2->X = vp1->Right(); diff --git a/src/GUI/VehicleSelection.h b/src/GUI/VehicleSelection.h index fa11ceb..3c8cb39 100644 --- a/src/GUI/VehicleSelection.h +++ b/src/GUI/VehicleSelection.h @@ -37,30 +37,9 @@ namespace GUI m_Background->SetTexture("Textures/GUI/VehicleSelection/1.png"); } - bool OnKeyUp(const Events::KeyUp &event) override - { - if (event.KeyCode == GLFW_KEY_LEFT) - { - m_CurrentSelection--; - } - else if (event.KeyCode == GLFW_KEY_RIGHT) - { - m_CurrentSelection++; - } - m_CurrentSelection = (m_CurrentSelection < 0) ? 0 : ((m_CurrentSelection > 3) ? 3 : m_CurrentSelection); + - std::stringstream ss; - ss << "Textures/GUI/VehicleSelection/" << m_CurrentSelection + 1 << ".png"; - m_Background->FadeToTexture(ss.str(), 1.f); - - glm::vec2 scale = Scale(); - glm::vec2 coord = -m_SelectionCoordinates[m_CurrentSelection]; - m_TargetCoordinate = coord * scale + glm::vec2(Width / 2.f, Height / 2.f); - glm::vec2 size = m_SelectionSizes[m_CurrentSelection]; - m_TargetSize = size * scale; - - return true; - } + void Update(double dt) override { @@ -94,6 +73,54 @@ namespace GUI float m_CurrentAlpha; TextureFrame* m_Background; + + bool OnCommand(const Events::InputCommand &event) override + { + // Menu movement + if (event.Command == "interface_horizontal" || event.Command == "interface_vertical") + { + // Horizontal scrolling + if (event.Command == "interface_horizontal") + { + if (event.Value > 0) + m_CurrentSelection++; + else if (event.Value < 0) + m_CurrentSelection--; + } + // Vertical scrolling to switch between "rows" in an intuitive way + else if (event.Command == "interface_vertical") + { + if (event.Value > 0) + { + if (m_CurrentSelection == 0) + m_CurrentSelection++; + else if (m_CurrentSelection == 3) + m_CurrentSelection--; + } + else if (event.Value < 0) + { + if (m_CurrentSelection == 1) + m_CurrentSelection--; + else if (m_CurrentSelection == 2) + m_CurrentSelection++; + } + } + + m_CurrentSelection = (m_CurrentSelection < 0) ? 0 : ((m_CurrentSelection > 3) ? 3 : m_CurrentSelection); + + std::stringstream ss; + ss << "Textures/GUI/VehicleSelection/" << m_CurrentSelection + 1 << ".png"; + m_Background->FadeToTexture(ss.str(), 1.f); + + glm::vec2 scale = Scale(); + glm::vec2 coord = -m_SelectionCoordinates[m_CurrentSelection]; + m_TargetCoordinate = coord * scale + glm::vec2(Width / 2.f, Height / 2.f); + glm::vec2 size = m_SelectionSizes[m_CurrentSelection]; + m_TargetSize = size * scale; + } + + return true; + } }; glm::vec2 VehicleSelection::m_SelectionCoordinates[4] = { diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index aaa25bc..7d6d38f 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -8,6 +8,22 @@ void GameWorld::Initialize() ResourceManager->Preload("Model", "Models/Placeholders/PhysicsTest/Plane.obj"); ResourceManager->Preload("Model", "Models/Placeholders/PhysicsTest/ArrowCube.obj"); + // Interface + BindKey(GLFW_KEY_A, "interface_horizontal", -1.f); + BindKey(GLFW_KEY_D, "interface_horizontal", 1.f); + BindGamepadAxis(Gamepad::Axis::LeftX, "interface_horizontal", 1.f); + BindGamepadButton(Gamepad::Button::Left, "interface_horizontal", -1.f); + BindGamepadButton(Gamepad::Button::Right, "interface_horizontal", 1.f); + BindKey(GLFW_KEY_W, "interface_vertical", 1.f); + BindKey(GLFW_KEY_S, "interface_vertical", -1.f); + BindGamepadAxis(Gamepad::Axis::LeftY, "interface_vertical", 1.f); + BindGamepadButton(Gamepad::Button::Up, "interface_vertical", 1.f); + BindGamepadButton(Gamepad::Button::Down, "interface_vertical", -1.f); + BindKey(GLFW_KEY_SPACE, "interface_accept", 1.f); + BindGamepadButton(Gamepad::Button::A, "interface_accept", 1.f); + + + BindKey(GLFW_KEY_W, "vertical", 1.f); BindKey(GLFW_KEY_S, "vertical", -1.f); BindKey(GLFW_KEY_A, "horizontal", -1.f);