]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/client/inputhandler.h
Merge branch 'master' of https://github.com/minetest/minetest
[dragonfireclient.git] / src / client / inputhandler.h
index 91c1111345e6964481fbdc20e48cd61cee83af91..47a61d4b85ba86f6b5f454295170ab2ccfcdae6b 100644 (file)
@@ -30,6 +30,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #endif
 
 class InputHandler;
+class TouchScreenGUI;
 
 /****************************************************************************
  Fast key cache for main game loop
@@ -42,7 +43,8 @@ class InputHandler;
  * (up to 10x faster) key lookup is an asset. Other parts of the codebase
  * (e.g. formspecs) should continue using getKeySetting().
  */
-struct KeyCache {
+struct KeyCache
+{
 
        KeyCache()
        {
@@ -143,8 +145,22 @@ class MyEventReceiver : public IEventReceiver
                return b;
        }
 
-       void listenForKey(const KeyPress &keyCode) { keysListenedFor.set(keyCode); }
-       void dontListenForKeys() { keysListenedFor.clear(); }
+       // Checks whether a key was just pressed. State will be cleared
+       // in the subsequent iteration of Game::processPlayerInteraction
+       bool WasKeyPressed(const KeyPress &keycode) const { return keyWasPressed[keycode]; }
+
+       // Checks whether a key was just released. State will be cleared
+       // in the subsequent iteration of Game::processPlayerInteraction
+       bool WasKeyReleased(const KeyPress &keycode) const { return keyWasReleased[keycode]; }
+
+       void listenForKey(const KeyPress &keyCode)
+       {
+               keysListenedFor.set(keyCode);
+       }
+       void dontListenForKeys()
+       {
+               keysListenedFor.clear();
+       }
 
        s32 getMouseWheel()
        {
@@ -157,17 +173,20 @@ class MyEventReceiver : public IEventReceiver
        {
                keyIsDown.clear();
                keyWasDown.clear();
+               keyWasPressed.clear();
+               keyWasReleased.clear();
 
-               leftclicked = false;
-               rightclicked = false;
-               leftreleased = false;
-               rightreleased = false;
+               mouse_wheel = 0;
+       }
 
-               left_active = false;
-               middle_active = false;
-               right_active = false;
+       void clearWasKeyPressed()
+       {
+               keyWasPressed.clear();
+       }
 
-               mouse_wheel = 0;
+       void clearWasKeyReleased()
+       {
+               keyWasReleased.clear();
        }
 
        MyEventReceiver()
@@ -177,28 +196,26 @@ class MyEventReceiver : public IEventReceiver
 #endif
        }
 
-       bool leftclicked = false;
-       bool rightclicked = false;
-       bool leftreleased = false;
-       bool rightreleased = false;
-
-       bool left_active = false;
-       bool middle_active = false;
-       bool right_active = false;
-
-       s32 mouse_wheel = 0;
-
        JoystickController *joystick = nullptr;
 
 #ifdef HAVE_TOUCHSCREENGUI
        TouchScreenGUI *m_touchscreengui;
 #endif
 
-private:
+       s32 mouse_wheel = 0;
+
        // The current state of keys
        KeyList keyIsDown;
-       // Whether a key has been pressed or not
+
+       // Like keyIsDown but only reset when that key is read
        KeyList keyWasDown;
+
+       // Whether a key has just been pressed
+       KeyList keyWasPressed;
+
+       // Whether a key has just been released
+       KeyList keyWasReleased;
+
        // List of keys we listen for
        // TODO perhaps the type of this is not really
        // performant as KeyList is designed for few but
@@ -210,12 +227,32 @@ class MyEventReceiver : public IEventReceiver
 class InputHandler
 {
 public:
-       InputHandler() = default;
+       InputHandler()
+       {
+               keycache.handler = this;
+               keycache.populate();
+       }
 
        virtual ~InputHandler() = default;
 
-       virtual bool isKeyDown(const KeyPress &keyCode) = 0;
-       virtual bool wasKeyDown(const KeyPress &keyCode) = 0;
+       virtual bool isRandom() const
+       {
+               return false;
+       }
+
+       virtual bool isKeyDown(GameKeyType k) = 0;
+       virtual void setKeypress(const KeyPress &keyCode) = 0;
+       virtual void unsetKeypress(const KeyPress &keyCode) = 0;
+       virtual bool wasKeyDown(GameKeyType k) = 0;
+       virtual bool wasKeyPressed(GameKeyType k) = 0;
+       virtual bool wasKeyReleased(GameKeyType k) = 0;
+       virtual bool cancelPressed() = 0;
+
+       virtual float getMovementSpeed() = 0;
+       virtual float getMovementDirection() = 0;
+
+       virtual void clearWasKeyPressed() {}
+       virtual void clearWasKeyReleased() {}
 
        virtual void listenForKey(const KeyPress &keyCode) {}
        virtual void dontListenForKeys() {}
@@ -223,19 +260,6 @@ class InputHandler
        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) {}
@@ -243,6 +267,7 @@ class InputHandler
        virtual void clear() {}
 
        JoystickController joystick;
+       KeyCache keycache;
 };
 /*
        Separated input handler
@@ -255,90 +280,125 @@ class RealInputHandler : public InputHandler
        {
                m_receiver->joystick = &joystick;
        }
-       virtual bool isKeyDown(const KeyPress &keyCode)
+
+       virtual ~RealInputHandler()
        {
-               return m_receiver->IsKeyDown(keyCode);
+               m_receiver->joystick = nullptr;
        }
-       virtual bool wasKeyDown(const KeyPress &keyCode)
+
+       virtual bool isKeyDown(GameKeyType k)
        {
-               return m_receiver->WasKeyDown(keyCode);
+               return m_receiver->IsKeyDown(keycache.key[k]) || joystick.isKeyDown(k);
        }
-       virtual void listenForKey(const KeyPress &keyCode)
+       virtual void setKeypress(const KeyPress &keyCode)
        {
-               m_receiver->listenForKey(keyCode);
+               m_receiver->keyIsDown.set(keyCode);
+               m_receiver->keyWasDown.set(keyCode);
        }
-       virtual void dontListenForKeys() { m_receiver->dontListenForKeys(); }
-       virtual v2s32 getMousePos()
+       virtual void unsetKeypress(const KeyPress &keyCode)
        {
-               if (RenderingEngine::get_raw_device()->getCursorControl()) {
-                       return RenderingEngine::get_raw_device()
-                                       ->getCursorControl()
-                                       ->getPosition();
-               }
-
-               return m_mousepos;
+               m_receiver->keyIsDown.unset(keyCode);
        }
-
-       virtual void setMousePos(s32 x, s32 y)
+       virtual bool wasKeyDown(GameKeyType k)
        {
-               if (RenderingEngine::get_raw_device()->getCursorControl()) {
-                       RenderingEngine::get_raw_device()
-                                       ->getCursorControl()
-                                       ->setPosition(x, y);
-               } else {
-                       m_mousepos = v2s32(x, y);
-               }
+               return m_receiver->WasKeyDown(keycache.key[k]) || joystick.wasKeyDown(k);
        }
-
-       virtual bool getLeftState()
+       virtual bool wasKeyPressed(GameKeyType k)
        {
-               return m_receiver->left_active || joystick.isKeyDown(KeyType::MOUSE_L);
+               return m_receiver->WasKeyPressed(keycache.key[k]) || joystick.wasKeyPressed(k);
        }
-       virtual bool getRightState()
+       virtual bool wasKeyReleased(GameKeyType k)
        {
-               return m_receiver->right_active || joystick.isKeyDown(KeyType::MOUSE_R);
+               return m_receiver->WasKeyReleased(keycache.key[k]) || joystick.wasKeyReleased(k);
        }
 
-       virtual bool getLeftClicked()
+       virtual float getMovementSpeed()
        {
-               return m_receiver->leftclicked || joystick.getWasKeyDown(KeyType::MOUSE_L);
+               bool f = m_receiver->IsKeyDown(keycache.key[KeyType::FORWARD]),
+                       b = m_receiver->IsKeyDown(keycache.key[KeyType::BACKWARD]),
+                       l = m_receiver->IsKeyDown(keycache.key[KeyType::LEFT]),
+                       r = m_receiver->IsKeyDown(keycache.key[KeyType::RIGHT]);
+               if (f || b || l || r)
+               {
+                       // if contradictory keys pressed, stay still
+                       if (f && b && l && r)
+                               return 0.0f;
+                       else if (f && b && !l && !r)
+                               return 0.0f;
+                       else if (!f && !b && l && r)
+                               return 0.0f;
+                       return 1.0f; // If there is a keyboard event, assume maximum speed
+               }
+               return joystick.getMovementSpeed();
+       }
+
+       virtual float getMovementDirection()
+       {
+               float x = 0, z = 0;
+
+               /* Check keyboard for input */
+               if (m_receiver->IsKeyDown(keycache.key[KeyType::FORWARD]))
+                       z += 1;
+               if (m_receiver->IsKeyDown(keycache.key[KeyType::BACKWARD]))
+                       z -= 1;
+               if (m_receiver->IsKeyDown(keycache.key[KeyType::RIGHT]))
+                       x += 1;
+               if (m_receiver->IsKeyDown(keycache.key[KeyType::LEFT]))
+                       x -= 1;
+
+               if (x != 0 || z != 0) /* If there is a keyboard event, it takes priority */
+                       return atan2(x, z);
+               else
+                       return joystick.getMovementDirection();
        }
-       virtual bool getRightClicked()
+
+       virtual bool cancelPressed()
        {
-               return m_receiver->rightclicked || joystick.getWasKeyDown(KeyType::MOUSE_R);
+               return wasKeyDown(KeyType::ESC) || m_receiver->WasKeyDown(CancelKey);
        }
 
-       virtual void resetLeftClicked()
+       virtual void clearWasKeyPressed()
        {
-               m_receiver->leftclicked = false;
-               joystick.clearWasKeyDown(KeyType::MOUSE_L);
+               m_receiver->clearWasKeyPressed();
        }
-       virtual void resetRightClicked() {
-               m_receiver->rightclicked = false;
-               joystick.clearWasKeyDown(KeyType::MOUSE_R);
+       virtual void clearWasKeyReleased()
+       {
+               m_receiver->clearWasKeyReleased();
        }
 
-       virtual bool getLeftReleased()
+       virtual void listenForKey(const KeyPress &keyCode)
        {
-               return m_receiver->leftreleased || joystick.wasKeyReleased(KeyType::MOUSE_L);
+               m_receiver->listenForKey(keyCode);
        }
-       virtual bool getRightReleased()
+       virtual void dontListenForKeys()
        {
-               return m_receiver->rightreleased || joystick.wasKeyReleased(KeyType::MOUSE_R);
+               m_receiver->dontListenForKeys();
        }
 
-       virtual void resetLeftReleased()
+       virtual v2s32 getMousePos()
        {
-               m_receiver->leftreleased = false;
-               joystick.clearWasKeyReleased(KeyType::MOUSE_L);
+               auto control = RenderingEngine::get_raw_device()->getCursorControl();
+               if (control) {
+                       return control->getPosition();
+               }
+
+               return m_mousepos;
        }
-       virtual void resetRightReleased()
+
+       virtual void setMousePos(s32 x, s32 y)
        {
-               m_receiver->rightreleased = false;
-               joystick.clearWasKeyReleased(KeyType::MOUSE_R);
+               auto control = RenderingEngine::get_raw_device()->getCursorControl();
+               if (control) {
+                       control->setPosition(x, y);
+               } else {
+                       m_mousepos = v2s32(x, y);
+               }
        }
 
-       virtual s32 getMouseWheel() { return m_receiver->getMouseWheel(); }
+       virtual s32 getMouseWheel()
+       {
+               return m_receiver->getMouseWheel();
+       }
 
        void clear()
        {
@@ -356,94 +416,32 @@ class RandomInputHandler : public InputHandler
 public:
        RandomInputHandler() = default;
 
-       virtual bool isKeyDown(const KeyPress &keyCode) { return keydown[keyCode]; }
-       virtual bool wasKeyDown(const KeyPress &keyCode) { return false; }
+       bool isRandom() const
+       {
+               return true;
+       }
+
+       virtual bool isKeyDown(GameKeyType k) { return keydown[keycache.key[k]]; }
+       virtual void setKeypress(const KeyPress &keyCode)
+       {
+               keydown.set(keyCode);
+       }
+       virtual void unsetKeypress(const KeyPress &keyCode)
+       {
+               keydown.unset(keyCode);
+       }
+       virtual bool wasKeyDown(GameKeyType k) { return false; }
+       virtual bool wasKeyPressed(GameKeyType k) { return false; }
+       virtual bool wasKeyReleased(GameKeyType k) { return false; }
+       virtual bool cancelPressed() { return false; }
+       virtual float getMovementSpeed() { return movementSpeed; }
+       virtual float getMovementDirection() { return movementDirection; }
        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 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 s32 getMouseWheel() { return 0; }
 
-       virtual void step(float dtime)
-       {
-               {
-                       static float counter1 = 0;
-                       counter1 -= dtime;
-                       if (counter1 < 0.0) {
-                               counter1 = 0.1 * Rand(1, 40);
-                               keydown.toggle(getKeySetting("keymap_jump"));
-                       }
-               }
-               {
-                       static float counter1 = 0;
-                       counter1 -= dtime;
-                       if (counter1 < 0.0) {
-                               counter1 = 0.1 * Rand(1, 40);
-                               keydown.toggle(getKeySetting("keymap_special1"));
-                       }
-               }
-               {
-                       static float counter1 = 0;
-                       counter1 -= dtime;
-                       if (counter1 < 0.0) {
-                               counter1 = 0.1 * Rand(1, 40);
-                               keydown.toggle(getKeySetting("keymap_forward"));
-                       }
-               }
-               {
-                       static float counter1 = 0;
-                       counter1 -= dtime;
-                       if (counter1 < 0.0) {
-                               counter1 = 0.1 * Rand(1, 40);
-                               keydown.toggle(getKeySetting("keymap_left"));
-                       }
-               }
-               {
-                       static float counter1 = 0;
-                       counter1 -= dtime;
-                       if (counter1 < 0.0) {
-                               counter1 = 0.1 * Rand(1, 20);
-                               mousespeed = v2s32(Rand(-20, 20), Rand(-15, 20));
-                       }
-               }
-               {
-                       static float counter1 = 0;
-                       counter1 -= dtime;
-                       if (counter1 < 0.0) {
-                               counter1 = 0.1 * Rand(1, 30);
-                               leftdown = !leftdown;
-                               if (leftdown)
-                                       leftclicked = true;
-                               if (!leftdown)
-                                       leftreleased = true;
-                       }
-               }
-               {
-                       static float counter1 = 0;
-                       counter1 -= dtime;
-                       if (counter1 < 0.0) {
-                               counter1 = 0.1 * Rand(1, 15);
-                               rightdown = !rightdown;
-                               if (rightdown)
-                                       rightclicked = true;
-                               if (!rightdown)
-                                       rightreleased = true;
-                       }
-               }
-               mousepos += mousespeed;
-       }
+       virtual void step(float dtime);
 
        s32 Rand(s32 min, s32 max);
 
@@ -451,10 +449,6 @@ class RandomInputHandler : public InputHandler
        KeyList keydown;
        v2s32 mousepos;
        v2s32 mousespeed;
-       bool leftdown = false;
-       bool rightdown = false;
-       bool leftclicked = false;
-       bool rightclicked = false;
-       bool leftreleased = false;
-       bool rightreleased = false;
+       float movementSpeed;
+       float movementDirection;
 };