GlPortal Class Reference  0.1.1
Source code documentation of the free and open 3D action puzzle game.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GWENInput.hpp
Go to the documentation of this file.
1 #ifndef RADIX_GWENINPUT_HPP
2 #define RADIX_GWENINPUT_HPP
3 
4 #include <Gwen/InputHandler.h>
5 #include <Gwen/Gwen.h>
6 #include <Gwen/Controls/Canvas.h>
7 
8 #include <SDL2/SDL_events.h>
9 #include <SDL2/SDL_keycode.h>
10 
11 namespace radix {
12 
13 class GWENInput {
14 public:
16  canvas(nullptr) {
17  }
18 
19  void init(Gwen::Controls::Canvas *c) {
20  canvas = c;
21  }
22 
23  unsigned char translateKeyCode(int code) {
24  switch (code) {
25  case SDLK_BACKSPACE:
26  return Gwen::Key::Backspace;
27 
28  case SDLK_RETURN:
29  return Gwen::Key::Return;
30 
31  case SDLK_ESCAPE:
32  return Gwen::Key::Escape;
33 
34  case SDLK_TAB:
35  return Gwen::Key::Tab;
36 
37  case SDLK_SPACE:
38  return Gwen::Key::Space;
39 
40  case SDLK_UP:
41  return Gwen::Key::Up;
42 
43  case SDLK_DOWN:
44  return Gwen::Key::Down;
45 
46  case SDLK_LEFT:
47  return Gwen::Key::Left;
48 
49  case SDLK_RIGHT:
50  return Gwen::Key::Right;
51 
52  case SDLK_HOME:
53  return Gwen::Key::Home;
54 
55  case SDLK_END:
56  return Gwen::Key::End;
57 
58  case SDLK_DELETE:
59  return Gwen::Key::Delete;
60 
61  case SDLK_LCTRL:
62  case SDLK_RCTRL:
63  return Gwen::Key::Control;
64 
65  case SDLK_LALT:
66  case SDLK_RALT:
67  return Gwen::Key::Alt;
68 
69  case SDLK_LSHIFT:
70  case SDLK_RSHIFT:
71  return Gwen::Key::Shift;
72 
73  default:
74  break;
75  }
76  return Gwen::Key::Invalid;
77  }
78 
79  bool processEvent(const SDL_Event &evt) {
80  if (not canvas) {
81  return false;
82  }
83 
84  switch (evt.type) {
85  case SDL_MOUSEMOTION:
86  return canvas->InputMouseMoved(evt.motion.x, evt.motion.y, evt.motion.xrel, evt.motion.yrel);
87 
88  case SDL_MOUSEWHEEL:
89  return canvas->InputMouseWheel(evt.wheel.y);
90 
91  case SDL_MOUSEBUTTONDOWN:
92  case SDL_MOUSEBUTTONUP: {
93  int button = -1;
94  switch (evt.button.button) {
95  case SDL_BUTTON_LEFT:
96  button = 0;
97  break;
98  case SDL_BUTTON_RIGHT:
99  button = 1;
100  break;
101  case SDL_BUTTON_MIDDLE:
102  button = 2;
103  break;
104  }
105  return canvas->InputMouseButton(button, evt.type == SDL_MOUSEBUTTONDOWN);
106  }
107 
108  /*case ALLEGRO_EVENT_KEY_CHAR:
109  {
110  return canvas->InputCharacter(event.keyboard.unichar);
111  }*/
112 
113  case SDL_KEYDOWN:
114  case SDL_KEYUP: {
115  bool down = (evt.type == SDL_KEYDOWN);
116 
117  if (evt.key.keysym.sym and down and
118  evt.key.keysym.sym >= ' ' and evt.key.keysym.sym <= '~') {
119  return canvas->InputCharacter(evt.key.keysym.sym);
120  }
121 
122  unsigned char key = translateKeyCode(evt.key.keysym.sym);
123  return canvas->InputKey(key, down);
124  }
125  }
126 
127  return false;
128  }
129 
130 protected:
131  Gwen::Controls::Canvas *canvas;
132 };
133 
134 } /* namespace radix */
135 
136 #endif /* RADIX_GWENINPUT_HPP */
GWENInput()
Definition: GWENInput.hpp:15
bool processEvent(const SDL_Event &evt)
Definition: GWENInput.hpp:79
Definition: GameController.hpp:7
Definition: GWENInput.hpp:13
Gwen::Controls::Canvas * canvas
Definition: GWENInput.hpp:131
void init(Gwen::Controls::Canvas *c)
Definition: GWENInput.hpp:19
unsigned char translateKeyCode(int code)
Definition: GWENInput.hpp:23