Skip to content

Commit a11e4ef

Browse files
committed
start adding fucntionality to register evented input actions
1 parent 1499c09 commit a11e4ef

3 files changed

Lines changed: 103 additions & 39 deletions

File tree

src/engine/Engine.cpp

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,33 @@ void Engine::start(void)
5959

6060
m_window->makeCurrentContext();
6161

62+
m_window->getInput()->registerKeyToAction(SDLK_F1, "propertyEditor");
63+
m_window->getInput()->registerKeyToAction(SDLK_F2, "fullscreenToggle");
64+
65+
m_window->getInput()->registerButtonToAction(SDL_BUTTON_LEFT, "fireRay");
66+
67+
m_window->getInput()->bindAction("propertyEditor", IE_PRESSED, [this]() {
68+
m_window->getGuiManager()->togglePropertyEditor();
69+
});
70+
71+
m_window->getInput()->bindAction("fullscreenToggle", IE_PRESSED, [this]() {
72+
m_window->toggleFullscreen();
73+
m_glManager->setDrawSize(m_window->getDrawableSize());
74+
});
75+
76+
m_window->getInput()->bindAction("fireRay", IE_REPEAT, [this]() {
77+
log_info("BANG!");
78+
Ray ray = Ray::getPickRay(m_window->getInput()->getMousePosition(), m_window->getViewport(), m_glManager->getViewMatrix(), m_glManager->getProjectionMatrix());
79+
80+
Entity *pickedEntity = m_physicsManager->pick(&ray);
81+
82+
if (pickedEntity != nullptr) {
83+
m_glManager->drawEntity(pickedEntity);
84+
}
85+
86+
m_glManager->drawLine(ray.getLine(100.0f));
87+
});
88+
6289
m_time = std::chrono::high_resolution_clock::now();
6390
// TODO: DO WE NEED FIXED UPDATES?
6491
//m_physicsTimeSimulated = std::chrono::high_resolution_clock::now();
@@ -105,42 +132,6 @@ void Engine::tick(void)
105132

106133
game->render(m_glManager.get());
107134

108-
if (m_window->getInput()->mouseIsPressed(SDL_BUTTON_LEFT))
109-
{
110-
Ray ray = Ray::getPickRay(m_window->getInput()->getMousePosition(), m_window->getViewport(), m_glManager->getViewMatrix(), m_glManager->getProjectionMatrix());
111-
112-
Entity *pickedEntity = m_physicsManager->pick(&ray);
113-
114-
if (pickedEntity != nullptr)
115-
m_glManager->drawEntity(pickedEntity);
116-
117-
m_glManager->drawLine(ray.getLine(100.0f));
118-
}
119-
120-
static bool f1Pressed = false;
121-
static bool f2Pressed = false;
122-
123-
if (!f1Pressed && m_window->getInput()->isPressed(SDLK_F1))
124-
{
125-
f1Pressed = true;
126-
m_window->getGuiManager()->togglePropertyEditor();
127-
}
128-
else if (f1Pressed && m_window->getInput()->isReleased(SDLK_F1))
129-
{
130-
f1Pressed = false;
131-
}
132-
133-
if (!f2Pressed && m_window->getInput()->isPressed(SDLK_F2))
134-
{
135-
f2Pressed = true;
136-
m_window->toggleFullscreen();
137-
m_glManager->setDrawSize(m_window->getDrawableSize());
138-
}
139-
else if (f2Pressed && m_window->getInput()->isReleased(SDLK_F2))
140-
{
141-
f2Pressed = false;
142-
}
143-
144135
m_window->getGuiManager()->render(game->getRootScene().get());
145136

146137
m_window->swapBuffer();

src/engine/Input.cpp

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,55 @@ Input::~Input(void)
1616

1717
void Input::handleKeyboardEvent(SDL_KeyboardEvent keyEvent)
1818
{
19+
auto keyToActionIt = m_keyToAction.find(keyEvent.keysym.sym);
20+
21+
if (keyToActionIt != m_keyToAction.end()) {
22+
auto actionInputEventIt = m_actionInputEventHandler.find(keyToActionIt->second);
23+
24+
if (actionInputEventIt != m_actionInputEventHandler.end()) {
25+
auto inputEventHandler = actionInputEventIt->second;
26+
27+
auto inputEventHandlerIt = inputEventHandler.find(
28+
keyEvent.state == SDL_PRESSED
29+
? (m_keyState[keyEvent.keysym.sym] == SDL_PRESSED
30+
? IE_REPEAT
31+
: IE_PRESSED)
32+
: IE_RELEASED);
33+
34+
if (inputEventHandlerIt != inputEventHandler.end()) {
35+
inputEventHandlerIt->second();
36+
}
37+
}
38+
}
39+
1940
m_keyState[keyEvent.keysym.sym] = keyEvent.state;
2041
m_keyModState = SDL_GetModState();
2142
}
22-
43+
#include "Logger.h"
2344
void Input::handleMouseEvent(SDL_MouseButtonEvent buttonEvent)
2445
{
46+
auto buttonToActionIt = m_buttonToAction.find(buttonEvent.button);
47+
48+
if (buttonToActionIt != m_buttonToAction.end()) {
49+
auto actionInputEventIt = m_actionInputEventHandler.find(buttonToActionIt->second);
50+
log_info("HERE");
51+
52+
if (actionInputEventIt != m_actionInputEventHandler.end()) {
53+
auto inputEventHandler = actionInputEventIt->second;
54+
55+
auto inputEventHandlerIt = inputEventHandler.find(
56+
buttonEvent.state == SDL_PRESSED
57+
? (m_buttonState[buttonEvent.button] == SDL_PRESSED
58+
? IE_REPEAT
59+
: IE_PRESSED)
60+
: IE_RELEASED);
61+
62+
if (inputEventHandlerIt != inputEventHandler.end()) {
63+
inputEventHandlerIt->second();
64+
}
65+
}
66+
}
67+
2568
m_buttonState[buttonEvent.button] = buttonEvent.state;
2669
}
2770

@@ -96,3 +139,18 @@ void Input::releaseMouse(void)
96139
{
97140
SDL_SetRelativeMouseMode(SDL_FALSE);
98141
}
142+
143+
void Input::bindAction(const std::string &action, InputEvent state, std::function<void(void)> handler)
144+
{
145+
m_actionInputEventHandler[action][state] = handler;
146+
}
147+
148+
void Input::registerKeyToAction(SDL_Keycode key, const std::string &action)
149+
{
150+
m_keyToAction[key] = action;
151+
}
152+
153+
void Input::registerButtonToAction(Uint8 button, const std::string &action)
154+
{
155+
m_buttonToAction[button] = action;
156+
}

src/engine/Input.h

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,18 @@
55
#pragma once
66

77
#include <map>
8+
#include <functional>
89

910
#include <SDL.h>
1011

1112
#include <glm/glm.hpp>
1213

14+
enum InputEvent {
15+
IE_PRESSED,
16+
IE_RELEASED,
17+
IE_REPEAT
18+
};
19+
1320
class Input
1421
{
1522
public:
@@ -37,12 +44,20 @@ class Input
3744
void grabMouse(void);
3845
void releaseMouse(void);
3946

47+
void bindAction(const std::string &action, InputEvent state, std::function<void(void)> handler);
48+
void registerKeyToAction(SDL_Keycode key, const std::string &action);
49+
void registerButtonToAction(Uint8 button, const std::string &action);
50+
4051
private:
41-
std::map <SDL_Keycode, Uint8> m_keyState;
42-
std::map <Uint8, Uint8> m_buttonState;
52+
std::map<SDL_Keycode, Uint8> m_keyState;
53+
std::map<Uint8, Uint8> m_buttonState;
4354
SDL_Keymod m_keyModState;
4455

4556
glm::vec2 m_mouseDelta;
4657
glm::vec2 m_mousePosition;
4758
glm::vec2 m_mouseWheel;
59+
60+
std::map<Uint8, std::string> m_buttonToAction;
61+
std::map<SDL_Keycode, std::string> m_keyToAction;
62+
std::map<std::string, std::map<InputEvent, std::function<void(void)>>> m_actionInputEventHandler;
4863
};

0 commit comments

Comments
 (0)