LCOV - code coverage report
Current view: top level - src/client - inputhandler.h (source / functions) Hit Total Coverage
Test: report Lines: 93 192 48.4 %
Date: 2015-07-11 18:23:49 Functions: 25 47 53.2 %

          Line data    Source code
       1             : /*
       2             : Minetest
       3             : Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
       4             : 
       5             : This program is free software; you can redistribute it and/or modify
       6             : it under the terms of the GNU Lesser General Public License as published by
       7             : the Free Software Foundation; either version 2.1 of the License, or
       8             : (at your option) any later version.
       9             : 
      10             : This program is distributed in the hope that it will be useful,
      11             : but WITHOUT ANY WARRANTY; without even the implied warranty of
      12             : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      13             : GNU Lesser General Public License for more details.
      14             : 
      15             : You should have received a copy of the GNU Lesser General Public License along
      16             : with this program; if not, write to the Free Software Foundation, Inc.,
      17             : 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
      18             : */
      19             : 
      20             : #ifndef __INPUT_HANDLER_H__
      21             : #define __INPUT_HANDLER_H__
      22             : 
      23             : #include "irrlichttypes_extrabloated.h"
      24             : 
      25           2 : class MyEventReceiver : public IEventReceiver
      26             : {
      27             : public:
      28             :         // This is the one method that we have to implement
      29        3144 :         virtual bool OnEvent(const SEvent& event)
      30             :         {
      31             :                 /*
      32             :                         React to nothing here if a menu is active
      33             :                 */
      34        3144 :                 if (noMenuActive() == false) {
      35             : #ifdef HAVE_TOUCHSCREENGUI
      36             :                         if (m_touchscreengui != 0) {
      37             :                                 m_touchscreengui->Toggle(false);
      38             :                         }
      39             : #endif
      40         140 :                         return g_menumgr.preprocessEvent(event);
      41             :                 }
      42             : 
      43             :                 // Remember whether each key is down or up
      44        3004 :                 if (event.EventType == irr::EET_KEY_INPUT_EVENT) {
      45         139 :                         if (event.KeyInput.PressedDown) {
      46         115 :                                 keyIsDown.set(event.KeyInput);
      47         115 :                                 keyWasDown.set(event.KeyInput);
      48             :                         } else {
      49          24 :                                 keyIsDown.unset(event.KeyInput);
      50             :                         }
      51             :                 }
      52             : 
      53             : #ifdef HAVE_TOUCHSCREENGUI
      54             :                 // case of touchscreengui we have to handle different events
      55             :                 if ((m_touchscreengui != 0) &&
      56             :                                 (event.EventType == irr::EET_TOUCH_INPUT_EVENT)) {
      57             :                         m_touchscreengui->translateEvent(event);
      58             :                         return true;
      59             :                 }
      60             : #endif
      61             :                 // handle mouse events
      62        3004 :                 if (event.EventType == irr::EET_MOUSE_INPUT_EVENT) {
      63        2584 :                         if (noMenuActive() == false) {
      64           0 :                                 left_active = false;
      65           0 :                                 middle_active = false;
      66           0 :                                 right_active = false;
      67             :                         } else {
      68        2584 :                                 left_active = event.MouseInput.isLeftPressed();
      69        2584 :                                 middle_active = event.MouseInput.isMiddlePressed();
      70        2584 :                                 right_active = event.MouseInput.isRightPressed();
      71             : 
      72        2584 :                                 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
      73           0 :                                         leftclicked = true;
      74             :                                 }
      75        2584 :                                 if (event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN) {
      76           0 :                                         rightclicked = true;
      77             :                                 }
      78        2584 :                                 if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
      79           1 :                                         leftreleased = true;
      80             :                                 }
      81        2584 :                                 if (event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP) {
      82           0 :                                         rightreleased = true;
      83             :                                 }
      84        2584 :                                 if (event.MouseInput.Event == EMIE_MOUSE_WHEEL) {
      85          21 :                                         mouse_wheel += event.MouseInput.Wheel;
      86             :                                 }
      87             :                         }
      88             :                 }
      89        3004 :                 if (event.EventType == irr::EET_LOG_TEXT_EVENT) {
      90          16 :                         dstream << std::string("Irrlicht log: ") + std::string(event.LogEvent.Text)
      91           8 :                                 << std::endl;
      92           8 :                         return true;
      93             :                 }
      94             :                 /* always return false in order to continue processing events */
      95        2996 :                 return false;
      96             :         }
      97             : 
      98       17490 :         bool IsKeyDown(const KeyPress &keyCode) const
      99             :         {
     100       17490 :                 return keyIsDown[keyCode];
     101             :         }
     102             : 
     103             :         // Checks whether a key was down and resets the state
     104       45499 :         bool WasKeyDown(const KeyPress &keyCode)
     105             :         {
     106       45499 :                 bool b = keyWasDown[keyCode];
     107       45499 :                 if (b)
     108           1 :                         keyWasDown.unset(keyCode);
     109       45499 :                 return b;
     110             :         }
     111             : 
     112        1166 :         s32 getMouseWheel()
     113             :         {
     114        1166 :                 s32 a = mouse_wheel;
     115        1166 :                 mouse_wheel = 0;
     116        1166 :                 return a;
     117             :         }
     118             : 
     119          22 :         void clearInput()
     120             :         {
     121          22 :                 keyIsDown.clear();
     122          22 :                 keyWasDown.clear();
     123             : 
     124          22 :                 leftclicked = false;
     125          22 :                 rightclicked = false;
     126          22 :                 leftreleased = false;
     127          22 :                 rightreleased = false;
     128             : 
     129          22 :                 left_active = false;
     130          22 :                 middle_active = false;
     131          22 :                 right_active = false;
     132             : 
     133          22 :                 mouse_wheel = 0;
     134          22 :         }
     135             : 
     136           1 :         MyEventReceiver()
     137           1 :         {
     138           1 :                 clearInput();
     139             : #ifdef HAVE_TOUCHSCREENGUI
     140             :                 m_touchscreengui = NULL;
     141             : #endif
     142           1 :         }
     143             : 
     144             :         bool leftclicked;
     145             :         bool rightclicked;
     146             :         bool leftreleased;
     147             :         bool rightreleased;
     148             : 
     149             :         bool left_active;
     150             :         bool middle_active;
     151             :         bool right_active;
     152             : 
     153             :         s32 mouse_wheel;
     154             : 
     155             : #ifdef HAVE_TOUCHSCREENGUI
     156             :         TouchScreenGUI* m_touchscreengui;
     157             : #endif
     158             : 
     159             : private:
     160             :         // The current state of keys
     161             :         KeyList keyIsDown;
     162             :         // Whether a key has been pressed or not
     163             :         KeyList keyWasDown;
     164             : };
     165             : 
     166             : 
     167             : /*
     168             :         Separated input handler
     169             : */
     170             : 
     171           2 : class RealInputHandler : public InputHandler
     172             : {
     173             : public:
     174           1 :         RealInputHandler(IrrlichtDevice *device, MyEventReceiver *receiver):
     175             :                 m_device(device),
     176             :                 m_receiver(receiver),
     177           1 :                 m_mousepos(0,0)
     178             :         {
     179           1 :         }
     180       17490 :         virtual bool isKeyDown(const KeyPress &keyCode)
     181             :         {
     182       17490 :                 return m_receiver->IsKeyDown(keyCode);
     183             :         }
     184       45499 :         virtual bool wasKeyDown(const KeyPress &keyCode)
     185             :         {
     186       45499 :                 return m_receiver->WasKeyDown(keyCode);
     187             :         }
     188        2290 :         virtual v2s32 getMousePos()
     189             :         {
     190        2290 :                 if (m_device->getCursorControl()) {
     191        2290 :                         return m_device->getCursorControl()->getPosition();
     192             :                 }
     193             :                 else {
     194           0 :                         return m_mousepos;
     195             :                 }
     196             :         }
     197        1146 :         virtual void setMousePos(s32 x, s32 y)
     198             :         {
     199        1146 :                 if (m_device->getCursorControl()) {
     200        1146 :                         m_device->getCursorControl()->setPosition(x, y);
     201             :                 }
     202             :                 else {
     203           0 :                         m_mousepos = v2s32(x,y);
     204             :                 }
     205        1146 :         }
     206             : 
     207        4117 :         virtual bool getLeftState()
     208             :         {
     209        4117 :                 return m_receiver->left_active;
     210             :         }
     211        3498 :         virtual bool getRightState()
     212             :         {
     213        3498 :                 return m_receiver->right_active;
     214             :         }
     215             : 
     216        1166 :         virtual bool getLeftClicked()
     217             :         {
     218        1166 :                 return m_receiver->leftclicked;
     219             :         }
     220         285 :         virtual bool getRightClicked()
     221             :         {
     222         285 :                 return m_receiver->rightclicked;
     223             :         }
     224        1166 :         virtual void resetLeftClicked()
     225             :         {
     226        1166 :                 m_receiver->leftclicked = false;
     227        1166 :         }
     228        1166 :         virtual void resetRightClicked()
     229             :         {
     230        1166 :                 m_receiver->rightclicked = false;
     231        1166 :         }
     232             : 
     233           0 :         virtual bool getLeftReleased()
     234             :         {
     235           0 :                 return m_receiver->leftreleased;
     236             :         }
     237           0 :         virtual bool getRightReleased()
     238             :         {
     239           0 :                 return m_receiver->rightreleased;
     240             :         }
     241        1166 :         virtual void resetLeftReleased()
     242             :         {
     243        1166 :                 m_receiver->leftreleased = false;
     244        1166 :         }
     245        1166 :         virtual void resetRightReleased()
     246             :         {
     247        1166 :                 m_receiver->rightreleased = false;
     248        1166 :         }
     249             : 
     250        1166 :         virtual s32 getMouseWheel()
     251             :         {
     252        1166 :                 return m_receiver->getMouseWheel();
     253             :         }
     254             : 
     255          21 :         void clear()
     256             :         {
     257          21 :                 m_receiver->clearInput();
     258          21 :         }
     259             : private:
     260             :         IrrlichtDevice  *m_device;
     261             :         MyEventReceiver *m_receiver;
     262             :         v2s32           m_mousepos;
     263             : };
     264             : 
     265           0 : class RandomInputHandler : public InputHandler
     266             : {
     267             : public:
     268           0 :         RandomInputHandler()
     269           0 :         {
     270           0 :                 leftdown = false;
     271           0 :                 rightdown = false;
     272           0 :                 leftclicked = false;
     273           0 :                 rightclicked = false;
     274           0 :                 leftreleased = false;
     275           0 :                 rightreleased = false;
     276           0 :                 keydown.clear();
     277           0 :         }
     278           0 :         virtual bool isKeyDown(const KeyPress &keyCode)
     279             :         {
     280           0 :                 return keydown[keyCode];
     281             :         }
     282           0 :         virtual bool wasKeyDown(const KeyPress &keyCode)
     283             :         {
     284           0 :                 return false;
     285             :         }
     286           0 :         virtual v2s32 getMousePos()
     287             :         {
     288           0 :                 return mousepos;
     289             :         }
     290           0 :         virtual void setMousePos(s32 x, s32 y)
     291             :         {
     292           0 :                 mousepos = v2s32(x, y);
     293           0 :         }
     294             : 
     295           0 :         virtual bool getLeftState()
     296             :         {
     297           0 :                 return leftdown;
     298             :         }
     299           0 :         virtual bool getRightState()
     300             :         {
     301           0 :                 return rightdown;
     302             :         }
     303             : 
     304           0 :         virtual bool getLeftClicked()
     305             :         {
     306           0 :                 return leftclicked;
     307             :         }
     308           0 :         virtual bool getRightClicked()
     309             :         {
     310           0 :                 return rightclicked;
     311             :         }
     312           0 :         virtual void resetLeftClicked()
     313             :         {
     314           0 :                 leftclicked = false;
     315           0 :         }
     316           0 :         virtual void resetRightClicked()
     317             :         {
     318           0 :                 rightclicked = false;
     319           0 :         }
     320             : 
     321           0 :         virtual bool getLeftReleased()
     322             :         {
     323           0 :                 return leftreleased;
     324             :         }
     325           0 :         virtual bool getRightReleased()
     326             :         {
     327           0 :                 return rightreleased;
     328             :         }
     329           0 :         virtual void resetLeftReleased()
     330             :         {
     331           0 :                 leftreleased = false;
     332           0 :         }
     333           0 :         virtual void resetRightReleased()
     334             :         {
     335           0 :                 rightreleased = false;
     336           0 :         }
     337             : 
     338           0 :         virtual s32 getMouseWheel()
     339             :         {
     340           0 :                 return 0;
     341             :         }
     342             : 
     343           0 :         virtual void step(float dtime)
     344             :         {
     345             :                 {
     346             :                         static float counter1 = 0;
     347           0 :                         counter1 -= dtime;
     348           0 :                         if (counter1 < 0.0) {
     349           0 :                                 counter1 = 0.1 * Rand(1, 40);
     350           0 :                                 keydown.toggle(getKeySetting("keymap_jump"));
     351             :                         }
     352             :                 }
     353             :                 {
     354             :                         static float counter1 = 0;
     355           0 :                         counter1 -= dtime;
     356           0 :                         if (counter1 < 0.0) {
     357           0 :                                 counter1 = 0.1 * Rand(1, 40);
     358           0 :                                 keydown.toggle(getKeySetting("keymap_special1"));
     359             :                         }
     360             :                 }
     361             :                 {
     362             :                         static float counter1 = 0;
     363           0 :                         counter1 -= dtime;
     364           0 :                         if (counter1 < 0.0) {
     365           0 :                                 counter1 = 0.1 * Rand(1, 40);
     366           0 :                                 keydown.toggle(getKeySetting("keymap_forward"));
     367             :                         }
     368             :                 }
     369             :                 {
     370             :                         static float counter1 = 0;
     371           0 :                         counter1 -= dtime;
     372           0 :                         if (counter1 < 0.0) {
     373           0 :                                 counter1 = 0.1 * Rand(1, 40);
     374           0 :                                 keydown.toggle(getKeySetting("keymap_left"));
     375             :                         }
     376             :                 }
     377             :                 {
     378             :                         static float counter1 = 0;
     379           0 :                         counter1 -= dtime;
     380           0 :                         if (counter1 < 0.0) {
     381           0 :                                 counter1 = 0.1 * Rand(1, 20);
     382           0 :                                 mousespeed = v2s32(Rand(-20, 20), Rand(-15, 20));
     383             :                         }
     384             :                 }
     385             :                 {
     386             :                         static float counter1 = 0;
     387           0 :                         counter1 -= dtime;
     388           0 :                         if (counter1 < 0.0) {
     389           0 :                                 counter1 = 0.1 * Rand(1, 30);
     390           0 :                                 leftdown = !leftdown;
     391           0 :                                 if (leftdown)
     392           0 :                                         leftclicked = true;
     393           0 :                                 if (!leftdown)
     394           0 :                                         leftreleased = true;
     395             :                         }
     396             :                 }
     397             :                 {
     398             :                         static float counter1 = 0;
     399           0 :                         counter1 -= dtime;
     400           0 :                         if (counter1 < 0.0) {
     401           0 :                                 counter1 = 0.1 * Rand(1, 15);
     402           0 :                                 rightdown = !rightdown;
     403           0 :                                 if (rightdown)
     404           0 :                                         rightclicked = true;
     405           0 :                                 if (!rightdown)
     406           0 :                                         rightreleased = true;
     407             :                         }
     408             :                 }
     409           0 :                 mousepos += mousespeed;
     410           0 :         }
     411             : 
     412           0 :         s32 Rand(s32 min, s32 max)
     413             :         {
     414           0 :                 return (myrand()%(max-min+1))+min;
     415             :         }
     416             : private:
     417             :         KeyList keydown;
     418             :         v2s32 mousepos;
     419             :         v2s32 mousespeed;
     420             :         bool leftdown;
     421             :         bool rightdown;
     422             :         bool leftclicked;
     423             :         bool rightclicked;
     424             :         bool leftreleased;
     425             :         bool rightreleased;
     426             : };
     427             : 
     428             : #endif

Generated by: LCOV version 1.11