]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/client/inputhandler.h
Clean up getTime helpers
[dragonfireclient.git] / src / client / inputhandler.h
index 3da856f7b80d0e25468e8d57494b953b8025830f..824b0da2ee77e97581b1af066b6bb9919737529a 100644 (file)
@@ -17,10 +17,11 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
-#ifndef __INPUT_HANDLER_H__
-#define __INPUT_HANDLER_H__
+#ifndef INPUT_HANDLER_H
+#define INPUT_HANDLER_H
 
 #include "irrlichttypes_extrabloated.h"
+#include "joystick_controller.h"
 
 class MyEventReceiver : public IEventReceiver
 {
@@ -42,11 +43,15 @@ class MyEventReceiver : public IEventReceiver
 
                // Remember whether each key is down or up
                if (event.EventType == irr::EET_KEY_INPUT_EVENT) {
-                       if (event.KeyInput.PressedDown) {
-                               keyIsDown.set(event.KeyInput);
-                               keyWasDown.set(event.KeyInput);
-                       } else {
-                               keyIsDown.unset(event.KeyInput);
+                       const KeyPress &keyCode = event.KeyInput;
+                       if (keysListenedFor[keyCode]) {
+                               if (event.KeyInput.PressedDown) {
+                                       keyIsDown.set(keyCode);
+                                       keyWasDown.set(keyCode);
+                               } else {
+                                       keyIsDown.unset(keyCode);
+                               }
+                               return true;
                        }
                }
 
@@ -58,6 +63,14 @@ class MyEventReceiver : public IEventReceiver
                        return true;
                }
 #endif
+
+               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);
+               }
                // handle mouse events
                if (event.EventType == irr::EET_MOUSE_INPUT_EVENT) {
                        if (noMenuActive() == false) {
@@ -86,17 +99,16 @@ class MyEventReceiver : public IEventReceiver
                                }
                        }
                } else if (event.EventType == irr::EET_LOG_TEXT_EVENT) {
-                       static const enum LogMessageLevel irr_loglev_conv[] = {
-                               LMT_VERBOSE, // ELL_DEBUG
-                               LMT_INFO, // ELL_INFORMATION
-                               LMT_ACTION, // ELL_WARNING
-                               LMT_ERROR, // ELL_ERROR
-                               LMT_ERROR, // ELL_NONE
+                       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 < sizeof(irr_loglev_conv));
-                       log_printline(irr_loglev_conv[event.LogEvent.Level],
-                               std::string("Irrlicht: ")
-                               + (const char*) event.LogEvent.Text);
+                       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;
                }
                /* always return false in order to continue processing events */
@@ -117,6 +129,15 @@ class MyEventReceiver : public IEventReceiver
                return b;
        }
 
+       void listenForKey(const KeyPress &keyCode)
+       {
+               keysListenedFor.set(keyCode);
+       }
+       void dontListenForKeys()
+       {
+               keysListenedFor.clear();
+       }
+
        s32 getMouseWheel()
        {
                s32 a = mouse_wheel;
@@ -160,6 +181,8 @@ class MyEventReceiver : public IEventReceiver
 
        s32 mouse_wheel;
 
+       JoystickController *joystick;
+
 #ifdef HAVE_TOUCHSCREENGUI
        TouchScreenGUI* m_touchscreengui;
 #endif
@@ -169,6 +192,12 @@ class MyEventReceiver : public IEventReceiver
        KeyList keyIsDown;
        // Whether a key has been pressed or not
        KeyList keyWasDown;
+       // List of keys we listen for
+       // TODO perhaps the type of this is not really
+       // performant as KeyList is designed for few but
+       // often changing keys, and keysListenedFor is expected
+       // to change seldomly but contain lots of keys.
+       KeyList keysListenedFor;
 };
 
 
@@ -184,6 +213,7 @@ class RealInputHandler : public InputHandler
                m_receiver(receiver),
                m_mousepos(0,0)
        {
+               m_receiver->joystick = &joystick;
        }
        virtual bool isKeyDown(const KeyPress &keyCode)
        {
@@ -193,6 +223,14 @@ class RealInputHandler : public InputHandler
        {
                return m_receiver->WasKeyDown(keyCode);
        }
+       virtual void listenForKey(const KeyPress &keyCode)
+       {
+               m_receiver->listenForKey(keyCode);
+       }
+       virtual void dontListenForKeys()
+       {
+               m_receiver->dontListenForKeys();
+       }
        virtual v2s32 getMousePos()
        {
                if (m_device->getCursorControl()) {
@@ -262,6 +300,7 @@ class RealInputHandler : public InputHandler
 
        void clear()
        {
+               joystick.clear();
                m_receiver->clearInput();
        }
 private: