zenilib  0.5.3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Gamestate_II.cpp
Go to the documentation of this file.
1 /* This file is part of the Zenipex Library (zenilib).
2  * Copyright (C) 2011 Mitchell Keith Bloch (bazald).
3  *
4  * zenilib is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser 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  * zenilib 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 Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with zenilib. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <zeni_rest.h>
19 
20 #include <algorithm>
21 #include <cmath>
22 
23 #include <Zeni/Define.h>
24 
25 #if defined(_DEBUG) && defined(_WINDOWS)
26 #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
27 #define new DEBUG_NEW
28 #endif
29 
30 namespace Zeni {
31 
32 #ifndef ANDROID
33  Zeni_Input_ID::Zeni_Input_ID(const Uint16 &type_, const int &subid_, const int &which_)
34  : type(type_),
35  subid(subid_),
36  which(which_),
37  previous_confidence(0.0f)
38  {
39  }
40 #endif
41 
42  bool Zeni_Input_ID::operator<(const Zeni_Input_ID &rhs) const {
43  return type < rhs.type ||
44  (type == rhs.type && (subid < rhs.subid ||
45  (subid == rhs.subid && which < rhs.which)));
46  }
47 
49  : m_joyball_min(ZENI_DEFAULT_II_JOYBALL_MIN),
50  m_joyball_max(ZENI_DEFAULT_II_JOYBALL_MAX),
51  m_joystick_min(ZENI_DEFAULT_II_JOYSTICK_MIN),
52  m_joystick_max(ZENI_DEFAULT_II_JOYSTICK_MAX),
53  m_mouse_min(ZENI_DEFAULT_II_MOUSE_MIN),
54  m_mouse_max(ZENI_DEFAULT_II_MOUSE_MAX),
55  m_firing_missed_events(false)
56  {
57  }
58 
59 #ifndef ANDROID
61  switch(event.type) {
63  {
64  float confidence = (float(event.caxis.value) + 0.5f) / 32767.5f;
65  const float ac = float(fabs(confidence));
66  const float nm = confidence < 0.0f ? -1.0f : 1.0f;
67  confidence = nm * std::min(std::max(ac - m_joystick_min, 0.0f) / (m_joystick_max - m_joystick_min), 1.0f);
68 
69  fire_event(Zeni_Input_ID(SDL_CONTROLLERAXISMOTION, event.caxis.axis, event.caxis.which), confidence);
70  }
71  break;
74  {
75  const float confidence = event.jbutton.state == SDL_PRESSED ? 1.0f : 0.0f;
76 
77  fire_event(Zeni_Input_ID(SDL_CONTROLLERAXISMOTION, event.cbutton.button, event.cbutton.which), confidence);
78  }
79  break;
80  case SDL_KEYDOWN:
81  case SDL_KEYUP:
82  {
83  const float confidence = event.key.state == SDL_PRESSED ? 1.0f : 0.0f;
84 
85  fire_event(Zeni_Input_ID(SDL_KEYDOWN, event.key.keysym.sym, 0 /*event.key.which*/), confidence);
86  }
87  break;
88  case SDL_MOUSEMOTION:
89  {
90  const int ac = abs(event.motion.xrel);
91  const int nm = event.motion.xrel < 0.0f ? -1 : 1;
92  const float confidence = nm * std::min(std::max(float(ac - m_mouse_min), 0.0f) / (m_mouse_max - m_mouse_min), 1.0f);
93 
94  fire_event(Zeni_Input_ID(SDL_MOUSEMOTION, 0, 0 /*event.motion.which*/), confidence);
95  }
96  {
97  const int ac = abs(event.motion.yrel);
98  const int nm = event.motion.yrel < 0.0f ? -1 : 1;
99  const float confidence = nm * std::min(std::max(float(ac - m_mouse_min), 0.0f) / (m_mouse_max - m_mouse_min), 1.0f);
100 
101  fire_event(Zeni_Input_ID(SDL_MOUSEMOTION, 1, 0 /*event.motion.which*/), confidence);
102  }
103  break;
104  case SDL_MOUSEBUTTONDOWN:
105  case SDL_MOUSEBUTTONUP:
106  {
107  const float confidence = event.button.state == SDL_PRESSED ? 1.0f : 0.0f;
108 
109  fire_event(Zeni_Input_ID(SDL_MOUSEBUTTONDOWN, event.button.button, 0 /*event.button.which*/), confidence);
110  }
111  break;
112  default:
114  break;
115  }
116  }
117 #endif
118 
119  void Gamestate_II::on_event(const Zeni_Input_ID &id, const float &confidence, const int &) {
120 #ifndef ANDROID
121  if(id.type == SDL_KEYDOWN && id.subid == SDLK_ESCAPE && confidence == 1.0f)
123 #endif
124  }
125 
128  }
129 
132  }
133 
135  std::map<Zeni_Input_ID, int>::const_iterator it = m_ii.find(event);
136  if(it != m_ii.end())
137  return it->second;
138  return 0;
139  }
140 
142  std::map<int, Zeni_Input_ID>::const_iterator it = m_rii.find(action);
143  if(it != m_rii.end())
144  return it->second;
145  return Zeni_Input_ID();
146  }
147 
148  void Gamestate_II::set_action(const Zeni_Input_ID &event, const int &action) {
149  m_ii[event] = action;
150  m_rii[action] = event;
151  }
152 
154  Game &gr = get_Game();
155 
156  for(std::map<Zeni_Input_ID, int>::iterator it = m_ii.begin(), iend = m_ii.end(); it != iend; ++it) {
157  switch(it->first.type) {
158 #ifndef ANDROID
159  case SDL_KEYDOWN:
160  {
161  const float confidence = gr.get_key_state(it->first.subid) ? 1.0f : 0.0f;
162 
163  if(m_firing_missed_events && it->first.previous_confidence != confidence)
164  on_event(it->first, confidence, it->second);
165 
166  it->first.previous_confidence = confidence;
167  }
168  break;
169 
170  case SDL_MOUSEBUTTONDOWN:
171  {
172  const float confidence = gr.get_mouse_button_state(it->first.subid) ? 1.0f : 0.0f;
173 
174  if(m_firing_missed_events && it->first.previous_confidence != confidence)
175  on_event(it->first, confidence, it->second);
176 
177  it->first.previous_confidence = confidence;
178  }
179  break;
180 
182  {
183  const float confidence = gr.get_controller_button_state(it->first.which, SDL_GameControllerButton(it->first.subid)) ? 1.0f : 0.0f;
184 
185  if(m_firing_missed_events && it->first.previous_confidence != confidence)
186  on_event(it->first, confidence, it->second);
187 
188  it->first.previous_confidence = confidence;
189  }
190  break;
191 #endif
192 
193  default:
194  break;
195  }
196  }
197  }
198 
199  void Gamestate_II::fire_event(const Zeni_Input_ID &id, const float &confidence) {
200  std::map<Zeni_Input_ID, int>::iterator it = m_ii.find(id);
201  if(it != m_ii.end()) {
202  float &pc = it->first.previous_confidence;
203  if(pc != confidence) {
204  pc = confidence;
205  on_event(id, confidence, it->second);
206  }
207  }
208  else
209  on_event(id, confidence, 0);
210  }
211 
212 }
213 
214 #include <Zeni/Undefine.h>
SDL_JoystickID which
Definition: SDL_events.h:345
int subid
event.keysym.sym, event.button, event.axis, mouse axis (x==0, y==1)
Definition: Gamestate_II.h:51
SDL_MouseMotionEvent motion
Definition: SDL_events.h:503
virtual void on_uncover()
Called when a Gamestate is popped off Game, making this Gamestate on top.
SDL_ControllerAxisEvent caxis
Definition: SDL_events.h:511
GLint GLenum GLsizei GLsizei GLsizei GLint GLenum GLenum type
Definition: gl2ext.h:845
virtual void set_action(const Zeni_Input_ID &event, const int &action)
GLclampf f
Definition: glew.h:3390
bool get_key_state(const int &key) const
Get the state of a key.
Definition: Game.cpp:287
virtual Zeni_Input_ID get_event(const int &action)
bool get_mouse_button_state(const int &button) const
Get the state of a mouse button.
Definition: Game.cpp:294
SDL_GameControllerButton
#define ZENI_DEFAULT_II_JOYBALL_MAX
Definition: Define.h:96
SDL_JoystickID which
Definition: SDL_events.h:362
Game & get_Game()
Get access to the singleton.
Definition: Game.cpp:58
The Gamestate Stack.
Definition: Game.h:71
Uint16 type
directly copied from SDL_Event; UP types are converted to DOWN types
Definition: Gamestate_II.h:50
#define ZENI_DEFAULT_II_MOUSE_MIN
Definition: Define.h:99
Zeni_Input_ID(const Uint16 &type_=SDL_KEYDOWN, const int &subid_=0, const int &which_=0)
#define ZENI_DEFAULT_II_MOUSE_MAX
Definition: Define.h:100
virtual void on_push()
Called when the Gamestate is pushed onto the stack in Game.
virtual void on_event(const SDL_Event &event)
First check for events. Called by Game as part of the main gameloop.
Definition: Gamestate.cpp:36
SDL_Keysym keysym
Definition: SDL_events.h:185
int which
Joystick Identifier; ignored for other events (should be 0)
Definition: Gamestate_II.h:52
virtual int get_action(const Zeni_Input_ID &event)
virtual void on_event(const SDL_Event &event)
First check for events. Called by Game as part of the main gameloop.
bool operator<(const Zeni_Input_ID &rhs) const
SDL_KeyboardEvent key
Definition: SDL_events.h:500
SDL_ControllerButtonEvent cbutton
Definition: SDL_events.h:512
#define ZENI_DEFAULT_II_JOYSTICK_MAX
Definition: Define.h:98
SDL_Keycode sym
Definition: SDL_keyboard.h:50
SDL_MouseButtonEvent button
Definition: SDL_events.h:504
uint16_t Uint16
An unsigned 16-bit integer type.
Definition: SDL_stdinc.h:137
General event structure.
Definition: SDL_events.h:495
bool get_controller_button_state(const int &which, const SDL_GameControllerButton &button) const
Get the state of a joystick button.
Definition: Game.cpp:313
#define min(x, y)
Definition: os.h:75
#define SDL_PRESSED
Definition: SDL_events.h:50
#define ZENI_DEFAULT_II_JOYSTICK_MIN
Definition: Define.h:97
#define max(x, y)
Definition: os.h:79
#define ZENI_DEFAULT_II_JOYBALL_MIN
Definition: Define.h:95
double fabs(double x)
Definition: s_fabs.c:29
void push_Popup_Menu_State()
Definition: Game.cpp:547
#define false
Definition: ftrandom.c:50
Uint32 type
Definition: SDL_events.h:497
cl_event event
Definition: glew.h:3556