summaryrefslogtreecommitdiff
path: root/src/client/inputhandler.h
diff options
context:
space:
mode:
authorShadowNinja <shadowninja@minetest.net>2017-06-03 14:55:10 -0400
committerShadowNinja <shadowninja@minetest.net>2017-06-03 14:55:10 -0400
commitcaecdb681c428c1aab9c0f7eec2570c0460f995c (patch)
treee5115982ea59bbf2343ba9b35bc4a0cfbb56f407 /src/client/inputhandler.h
parent81d56b94919dceb7b2e51d70b21a7ca22f852bd5 (diff)
parent80dc961d24e1964e25d57039ddb2ba639f9f4d22 (diff)
downloadminetest-caecdb681c428c1aab9c0f7eec2570c0460f995c.tar.gz
minetest-caecdb681c428c1aab9c0f7eec2570c0460f995c.tar.bz2
minetest-caecdb681c428c1aab9c0f7eec2570c0460f995c.zip
Merge 0.4.16 into stable-0.4
Diffstat (limited to 'src/client/inputhandler.h')
-rw-r--r--src/client/inputhandler.h360
1 files changed, 143 insertions, 217 deletions
diff --git a/src/client/inputhandler.h b/src/client/inputhandler.h
index 824b0da2e..7c422d189 100644
--- a/src/client/inputhandler.h
+++ b/src/client/inputhandler.h
@@ -22,104 +22,87 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "irrlichttypes_extrabloated.h"
#include "joystick_controller.h"
+#include <list>
+#include "keycode.h"
-class MyEventReceiver : public IEventReceiver
-{
-public:
- // This is the one method that we have to implement
- virtual bool OnEvent(const SEvent& event)
- {
- /*
- React to nothing here if a menu is active
- */
- if (noMenuActive() == false) {
#ifdef HAVE_TOUCHSCREENGUI
- if (m_touchscreengui != 0) {
- m_touchscreengui->Toggle(false);
- }
+#include "touchscreengui.h"
#endif
- return g_menumgr.preprocessEvent(event);
- }
- // Remember whether each key is down or up
- if (event.EventType == irr::EET_KEY_INPUT_EVENT) {
- const KeyPress &keyCode = event.KeyInput;
- if (keysListenedFor[keyCode]) {
- if (event.KeyInput.PressedDown) {
- keyIsDown.set(keyCode);
- keyWasDown.set(keyCode);
- } else {
- keyIsDown.unset(keyCode);
- }
- return true;
- }
- }
+class KeyList : private std::list<KeyPress>
+{
+ typedef std::list<KeyPress> super;
+ typedef super::iterator iterator;
+ typedef super::const_iterator const_iterator;
-#ifdef HAVE_TOUCHSCREENGUI
- // case of touchscreengui we have to handle different events
- if ((m_touchscreengui != 0) &&
- (event.EventType == irr::EET_TOUCH_INPUT_EVENT)) {
- m_touchscreengui->translateEvent(event);
- return true;
- }
-#endif
+ virtual const_iterator find(const KeyPress &key) const
+ {
+ const_iterator f(begin());
+ const_iterator e(end());
+
+ while (f != e) {
+ if (*f == key)
+ return f;
- if (event.EventType == irr::EET_JOYSTICK_INPUT_EVENT) {
- /* TODO add a check like:
- if (event.JoystickEvent != joystick_we_listen_for)
- return false;
- */
- return joystick->handleEvent(event.JoystickEvent);
+ ++f;
}
- // handle mouse events
- if (event.EventType == irr::EET_MOUSE_INPUT_EVENT) {
- if (noMenuActive() == false) {
- left_active = false;
- middle_active = false;
- right_active = false;
- } else {
- left_active = event.MouseInput.isLeftPressed();
- middle_active = event.MouseInput.isMiddlePressed();
- right_active = event.MouseInput.isRightPressed();
-
- if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
- leftclicked = true;
- }
- if (event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN) {
- rightclicked = true;
- }
- if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
- leftreleased = true;
- }
- if (event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP) {
- rightreleased = true;
- }
- if (event.MouseInput.Event == EMIE_MOUSE_WHEEL) {
- mouse_wheel += event.MouseInput.Wheel;
- }
- }
- } else if (event.EventType == irr::EET_LOG_TEXT_EVENT) {
- static const LogLevel irr_loglev_conv[] = {
- LL_VERBOSE, // ELL_DEBUG
- LL_INFO, // ELL_INFORMATION
- LL_WARNING, // ELL_WARNING
- LL_ERROR, // ELL_ERROR
- LL_NONE, // ELL_NONE
- };
- assert(event.LogEvent.Level < ARRLEN(irr_loglev_conv));
- g_logger.log(irr_loglev_conv[event.LogEvent.Level],
- std::string("Irrlicht: ") + (const char*) event.LogEvent.Text);
- return true;
+
+ return e;
+ }
+
+ virtual iterator find(const KeyPress &key)
+ {
+ iterator f(begin());
+ iterator e(end());
+
+ while (f != e) {
+ if (*f == key)
+ return f;
+
+ ++f;
}
- /* always return false in order to continue processing events */
- return false;
+
+ return e;
+ }
+
+public:
+ void clear() { super::clear(); }
+
+ void set(const KeyPress &key)
+ {
+ if (find(key) == end())
+ push_back(key);
}
- bool IsKeyDown(const KeyPress &keyCode) const
+ void unset(const KeyPress &key)
{
- return keyIsDown[keyCode];
+ iterator p(find(key));
+
+ if (p != end())
+ erase(p);
}
+ void toggle(const KeyPress &key)
+ {
+ iterator p(this->find(key));
+
+ if (p != end())
+ erase(p);
+ else
+ push_back(key);
+ }
+
+ bool operator[](const KeyPress &key) const { return find(key) != end(); }
+};
+
+class MyEventReceiver : public IEventReceiver
+{
+public:
+ // This is the one method that we have to implement
+ virtual bool OnEvent(const SEvent &event);
+
+ bool IsKeyDown(const KeyPress &keyCode) const { return keyIsDown[keyCode]; }
+
// Checks whether a key was down and resets the state
bool WasKeyDown(const KeyPress &keyCode)
{
@@ -129,14 +112,8 @@ public:
return b;
}
- void listenForKey(const KeyPress &keyCode)
- {
- keysListenedFor.set(keyCode);
- }
- void dontListenForKeys()
- {
- keysListenedFor.clear();
- }
+ void listenForKey(const KeyPress &keyCode) { keysListenedFor.set(keyCode); }
+ void dontListenForKeys() { keysListenedFor.clear(); }
s32 getMouseWheel()
{
@@ -184,7 +161,7 @@ public:
JoystickController *joystick;
#ifdef HAVE_TOUCHSCREENGUI
- TouchScreenGUI* m_touchscreengui;
+ TouchScreenGUI *m_touchscreengui;
#endif
private:
@@ -200,7 +177,42 @@ private:
KeyList keysListenedFor;
};
+class InputHandler
+{
+public:
+ InputHandler() {}
+ virtual ~InputHandler() {}
+
+ virtual bool isKeyDown(const KeyPress &keyCode) = 0;
+ virtual bool wasKeyDown(const KeyPress &keyCode) = 0;
+
+ virtual void listenForKey(const KeyPress &keyCode) {}
+ virtual void dontListenForKeys() {}
+
+ virtual v2s32 getMousePos() = 0;
+ virtual void setMousePos(s32 x, s32 y) = 0;
+
+ virtual bool getLeftState() = 0;
+ virtual bool getRightState() = 0;
+
+ virtual bool getLeftClicked() = 0;
+ virtual bool getRightClicked() = 0;
+ virtual void resetLeftClicked() = 0;
+ virtual void resetRightClicked() = 0;
+
+ virtual bool getLeftReleased() = 0;
+ virtual bool getRightReleased() = 0;
+ virtual void resetLeftReleased() = 0;
+ virtual void resetRightReleased() = 0;
+ virtual s32 getMouseWheel() = 0;
+
+ virtual void step(float dtime) {}
+
+ virtual void clear() {}
+
+ JoystickController joystick;
+};
/*
Separated input handler
*/
@@ -208,10 +220,8 @@ private:
class RealInputHandler : public InputHandler
{
public:
- RealInputHandler(IrrlichtDevice *device, MyEventReceiver *receiver):
- m_device(device),
- m_receiver(receiver),
- m_mousepos(0,0)
+ RealInputHandler(IrrlichtDevice *device, MyEventReceiver *receiver)
+ : m_device(device), m_receiver(receiver), m_mousepos(0, 0)
{
m_receiver->joystick = &joystick;
}
@@ -227,16 +237,12 @@ public:
{
m_receiver->listenForKey(keyCode);
}
- virtual void dontListenForKeys()
- {
- m_receiver->dontListenForKeys();
- }
+ virtual void dontListenForKeys() { m_receiver->dontListenForKeys(); }
virtual v2s32 getMousePos()
{
if (m_device->getCursorControl()) {
return m_device->getCursorControl()->getPosition();
- }
- else {
+ } else {
return m_mousepos;
}
}
@@ -244,69 +250,36 @@ public:
{
if (m_device->getCursorControl()) {
m_device->getCursorControl()->setPosition(x, y);
- }
- else {
- m_mousepos = v2s32(x,y);
+ } else {
+ m_mousepos = v2s32(x, y);
}
}
- virtual bool getLeftState()
- {
- return m_receiver->left_active;
- }
- virtual bool getRightState()
- {
- return m_receiver->right_active;
- }
+ virtual bool getLeftState() { return m_receiver->left_active; }
+ virtual bool getRightState() { return m_receiver->right_active; }
- virtual bool getLeftClicked()
- {
- return m_receiver->leftclicked;
- }
- virtual bool getRightClicked()
- {
- return m_receiver->rightclicked;
- }
- virtual void resetLeftClicked()
- {
- m_receiver->leftclicked = false;
- }
- virtual void resetRightClicked()
- {
- m_receiver->rightclicked = false;
- }
+ virtual bool getLeftClicked() { return m_receiver->leftclicked; }
+ virtual bool getRightClicked() { return m_receiver->rightclicked; }
+ virtual void resetLeftClicked() { m_receiver->leftclicked = false; }
+ virtual void resetRightClicked() { m_receiver->rightclicked = false; }
- virtual bool getLeftReleased()
- {
- return m_receiver->leftreleased;
- }
- virtual bool getRightReleased()
- {
- return m_receiver->rightreleased;
- }
- virtual void resetLeftReleased()
- {
- m_receiver->leftreleased = false;
- }
- virtual void resetRightReleased()
- {
- m_receiver->rightreleased = false;
- }
+ virtual bool getLeftReleased() { return m_receiver->leftreleased; }
+ virtual bool getRightReleased() { return m_receiver->rightreleased; }
+ virtual void resetLeftReleased() { m_receiver->leftreleased = false; }
+ virtual void resetRightReleased() { m_receiver->rightreleased = false; }
- virtual s32 getMouseWheel()
- {
- return m_receiver->getMouseWheel();
- }
+ virtual s32 getMouseWheel() { return m_receiver->getMouseWheel(); }
void clear()
{
joystick.clear();
m_receiver->clearInput();
}
+
private:
- IrrlichtDevice *m_device;
+ IrrlichtDevice *m_device;
MyEventReceiver *m_receiver;
- v2s32 m_mousepos;
+ v2s32 m_mousepos;
};
class RandomInputHandler : public InputHandler
@@ -322,70 +295,25 @@ public:
rightreleased = false;
keydown.clear();
}
- virtual bool isKeyDown(const KeyPress &keyCode)
- {
- return keydown[keyCode];
- }
- virtual bool wasKeyDown(const KeyPress &keyCode)
- {
- return false;
- }
- virtual v2s32 getMousePos()
- {
- return mousepos;
- }
- virtual void setMousePos(s32 x, s32 y)
- {
- mousepos = v2s32(x, y);
- }
+ virtual bool isKeyDown(const KeyPress &keyCode) { return keydown[keyCode]; }
+ virtual bool wasKeyDown(const KeyPress &keyCode) { return false; }
+ virtual v2s32 getMousePos() { return mousepos; }
+ virtual void setMousePos(s32 x, s32 y) { mousepos = v2s32(x, y); }
- virtual bool getLeftState()
- {
- return leftdown;
- }
- virtual bool getRightState()
- {
- return rightdown;
- }
+ virtual bool getLeftState() { return leftdown; }
+ virtual bool getRightState() { return rightdown; }
- virtual bool getLeftClicked()
- {
- return leftclicked;
- }
- virtual bool getRightClicked()
- {
- return rightclicked;
- }
- virtual void resetLeftClicked()
- {
- leftclicked = false;
- }
- virtual void resetRightClicked()
- {
- rightclicked = false;
- }
+ virtual bool getLeftClicked() { return leftclicked; }
+ virtual bool getRightClicked() { return rightclicked; }
+ virtual void resetLeftClicked() { leftclicked = false; }
+ virtual void resetRightClicked() { rightclicked = false; }
- virtual bool getLeftReleased()
- {
- return leftreleased;
- }
- virtual bool getRightReleased()
- {
- return rightreleased;
- }
- virtual void resetLeftReleased()
- {
- leftreleased = false;
- }
- virtual void resetRightReleased()
- {
- rightreleased = false;
- }
+ virtual bool getLeftReleased() { return leftreleased; }
+ virtual bool getRightReleased() { return rightreleased; }
+ virtual void resetLeftReleased() { leftreleased = false; }
+ virtual void resetRightReleased() { rightreleased = false; }
- virtual s32 getMouseWheel()
- {
- return 0;
- }
+ virtual s32 getMouseWheel() { return 0; }
virtual void step(float dtime)
{
@@ -456,10 +384,8 @@ public:
mousepos += mousespeed;
}
- s32 Rand(s32 min, s32 max)
- {
- return (myrand()%(max-min+1))+min;
- }
+ s32 Rand(s32 min, s32 max);
+
private:
KeyList keydown;
v2s32 mousepos;