Tinman  1.0
input.h
Go to the documentation of this file.
1 // Copyright (C) 2014 ISAAC LACOBA MOLINA
2 // Tinman author: Isaac Lacoba Molina
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 //
17 
18 #ifndef INPUT_HPP
19 #define INPUT_HPP
20 #include <map>
21 #include <vector>
22 #include <memory>
23 #include <unistd.h>
24 
25 #include <OgreRoot.h>
26 #include <OgreRenderWindow.h>
27 #include <OgreWindowEventUtilities.h>
28 
29 #include <CEGUI/CEGUI.h>
30 
31 #include <OIS/OISEvents.h>
32 #include <OIS/OISInputManager.h>
33 #include <OIS/OISKeyboard.h>
34 #include <OIS/OISMouse.h>
35 
36 enum class EventType{repeat, doItOnce};
38 
39 class EventListener: public Ogre::WindowEventListener,
40  public OIS::KeyListener,
41  public OIS::MouseListener {
42 
43  typedef bool OnKeyPressed;
44  typedef std::pair<OIS::MouseButtonID, EventTrigger> MouseKey;
45  typedef std::pair<OIS::KeyCode, EventTrigger> KeyBoardKey;
46 
47  OIS::InputManager* input_manager_;
48  OIS::Mouse* mouse_;
49  OIS::Keyboard* keyboard_;
50 
51  MouseKey mouse_key_;
52 
53  std::map<MouseKey, std::function<void()>> mouse_triggers_;
54 
55  public:
56  typedef std::shared_ptr<EventListener> shared;
57  typedef std::vector<KeyBoardKey> KeyEvents;
58 
59  float x, y;
60  bool exit_;
61 
62  EventListener(Ogre::RenderWindow* window);
63 
64  void capture(void);
65  void check_events();
66 
67  void add_hook(MouseKey key,std::function<void()> callback);
68  void add_hook(KeyBoardKey keystroke, EventType type, std::function<void()> callback);
69  void clear_hooks();
70 
71  bool keyPressed(const OIS::KeyEvent& arg);
72  bool keyReleased(const OIS::KeyEvent& arg);
73  bool mouseMoved(const OIS::MouseEvent& evt);
74  bool mousePressed(const OIS::MouseEvent& evt, OIS::MouseButtonID id);
75  bool mouseReleased(const OIS::MouseEvent& evt, OIS::MouseButtonID id);
76 
77  void windowClosed(Ogre::RenderWindow* window);
78 
79  bool shutdown(void);
80  bool gui_shutdown(const CEGUI::EventArgs &event);
81 
82  private:
83  std::map<std::string, OIS::KeyCode> keys_;
84 
85  KeyEvents events_;
86  std::map<KeyBoardKey, std::function<void()>> repeat_triggers_, doitonce_triggers_;
87 
88  void create_input_manager(Ogre::RenderWindow* window);
89  void remove_key_from_buffer(KeyBoardKey event);
90 
92  void trigger_mouse_events();
93 
94  CEGUI::MouseButton convertMouseButton(OIS::MouseButtonID id);
95  };
96 #endif
EventTrigger
Definition: input.h:37
void windowClosed(Ogre::RenderWindow *window)
Definition: input.cpp:159
float x
Definition: input.h:59
void trigger_keyboard_events()
Definition: input.cpp:81
bool shutdown(void)
Definition: input.cpp:98
MouseKey mouse_key_
Definition: input.h:51
std::pair< OIS::KeyCode, EventTrigger > KeyBoardKey
Definition: input.h:45
EventType
Definition: input.h:36
CEGUI::MouseButton convertMouseButton(OIS::MouseButtonID id)
Definition: input.cpp:188
std::map< std::string, OIS::KeyCode > keys_
Definition: input.h:83
OIS::Mouse * mouse_
Definition: input.h:48
void create_input_manager(Ogre::RenderWindow *window)
Definition: input.cpp:164
bool keyPressed(const OIS::KeyEvent &arg)
Definition: input.cpp:111
OIS::Keyboard * keyboard_
Definition: input.h:49
OIS::InputManager * input_manager_
Definition: input.h:47
void capture(void)
Definition: input.cpp:58
KeyEvents events_
Definition: input.h:85
std::map< KeyBoardKey, std::function< void()> > repeat_triggers_
Definition: input.h:86
float y
Definition: input.h:59
void clear_hooks()
Definition: input.cpp:104
bool OnKeyPressed
Definition: input.h:43
bool keyReleased(const OIS::KeyEvent &arg)
Definition: input.cpp:123
void trigger_mouse_events()
Definition: input.cpp:73
void remove_key_from_buffer(KeyBoardKey event)
Definition: input.cpp:180
std::pair< OIS::MouseButtonID, EventTrigger > MouseKey
Definition: input.h:44
std::map< MouseKey, std::function< void()> > mouse_triggers_
Definition: input.h:53
std::vector< KeyBoardKey > KeyEvents
Definition: input.h:57
EventListener(Ogre::RenderWindow *window)
Definition: input.cpp:21
bool mousePressed(const OIS::MouseEvent &evt, OIS::MouseButtonID id)
Definition: input.cpp:140
std::map< KeyBoardKey, std::function< void()> > doitonce_triggers_
Definition: input.h:86
bool exit_
Definition: input.h:60
bool mouseMoved(const OIS::MouseEvent &evt)
Definition: input.cpp:134
std::shared_ptr< EventListener > shared
Definition: input.h:56
bool gui_shutdown(const CEGUI::EventArgs &event)
Definition: input.h:39
void add_hook(MouseKey key, std::function< void()> callback)
Definition: input.cpp:52
bool mouseReleased(const OIS::MouseEvent &evt, OIS::MouseButtonID id)
Definition: input.cpp:151
void check_events()
Definition: input.cpp:64