Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
MouseController.cpp
Go to the documentation of this file.
1 /* Project nGenEx
2  Destroyer Studios LLC
3  Copyright © 1997-2004. All Rights Reserved.
4 
5  SUBSYSTEM: nGenEx.lib
6  FILE: MouseController.cpp
7  AUTHOR: John DiCamillo
8 
9 
10  OVERVIEW
11  ========
12  MouseController Input class
13 */
14 
15 #include "MemDebug.h"
16 #include "MouseController.h"
17 #include "Keyboard.h"
18 #include "Mouse.h"
19 #include "Game.h"
20 #include "Video.h"
21 
22 // +--------------------------------------------------------------------+
23 
24 static MouseController* instance = 0;
25 static DWORD rbutton_latch = 0;
26 static DWORD mbutton_latch = 0;
27 static DWORD active_latch = 0;
28 
29 // +--------------------------------------------------------------------+
30 
32 : p(0), r(0), w(0), dx(0), dy(0), t(0)
33 {
34  instance = this;
35  select = 0;
36  sensitivity = 10;
37  swapped = 0;
38  active = false;
39 
40  active_key = 20; // caps lock
41 
42  rbutton_latch = 0;
43 
44  for (int i = 0; i < MotionController::MaxActions; i++)
45  action[i] = 0;
46 }
47 
49 {
50  instance = 0;
51 }
52 
55 {
56  return instance;
57 }
58 
59 // +--------------------------------------------------------------------+
60 
61 void
63 {
64  for (int i = 0; i < nkeys; i++) {
65  KeyMapEntry k = mapping[i];
66 
67  if (k.act >= KEY_MAP_FIRST && k.act <= KEY_MAP_LAST) {
68 
69  if (k.act == KEY_MOUSE_SENSE)
70  sensitivity = k.key;
71 
72  else if (k.act == KEY_MOUSE_SWAP)
73  swapped = k.key;
74 
75  else if (k.act == KEY_MOUSE_INVERT)
76  inverted = k.key;
77 
78  else if (k.act == KEY_MOUSE_SELECT)
79  select = k.key;
80 
81  else if (k.act == KEY_MOUSE_ACTIVE)
82  active_key = k.key;
83  }
84  }
85 }
86 
87 // +--------------------------------------------------------------------+
88 
89 static inline double sqr(double a) { return a*a; }
90 
91 void
93 {
94  p = r = w = 0;
95  action[0] = 0;
96  action[1] = 0;
97  action[2] = 0;
98  action[3] = 0;
99 
101  active_latch = 1;
102  }
103  else {
104  if (active_latch) {
105  active_latch = 0;
106  active = !active;
107  }
108  }
109 
110  if (!select || !active)
111  return;
112 
113  action[0] = Mouse::LButton();
114 
115  int roll_enable = 0;
116 
117  if (Mouse::RButton()) {
118  roll_enable = 1;
119 
120  if (!rbutton_latch)
121  rbutton_latch = Game::RealTime();
122  }
123  else {
124  if (rbutton_latch) {
125  rbutton_latch = Game::RealTime() - rbutton_latch;
126  if (rbutton_latch < 250)
127  action[1] = 1;
128  }
129 
130  rbutton_latch = 0;
131  }
132 
133  if (Mouse::MButton()) {
134  if (!mbutton_latch)
135  mbutton_latch = Game::RealTime();
136  }
137  else {
138  if (mbutton_latch) {
139  action[3] = 1;
140  }
141 
142  mbutton_latch = 0;
143  }
144 
145  double step = 0;
146  int cx = Video::GetInstance()->Width()/2;
147  int cy = Video::GetInstance()->Height()/2;
148 
149  dx += Mouse::X() - cx;
150  dy += Mouse::Y() - cy;
151 
152  step = fabs(dx)/cx;
153 
154  if (roll_enable || select == 1)
155  step *= 3 * sensitivity;
156  else
157  step *= step * sensitivity/4;
158 
159  if (roll_enable) {
160  if (dx > 0)
161  r = -step;
162  else if (dx < 0)
163  r = step;
164  }
165  else {
166  if (dx > 0)
167  w = step;
168  else if (dx < 0)
169  w = -step;
170  }
171 
172  step = fabs(dy)/cy;
173 
174  if (select == 1)
175  step *= 2 * sensitivity;
176  else
177  step *= step * sensitivity/4;
178 
179  if (inverted) {
180  step *= -1;
181  }
182 
183  if (dy > 0)
184  p = step;
185  else if (dy < 0)
186  p = -step;
187 
188  if (select == 1) {
189  ::SetCursorPos(cx, cy);
190 
191  double drain = cx * 4 * Game::FrameTime();
192 
193  if (dx > drain) {
194  dx -= drain;
195  }
196  else if (dx < -drain) {
197  dx += drain;
198  }
199  else {
200  dx = 0;
201  }
202 
203  if (dy > drain) {
204  dy -= drain;
205  }
206  else if (dy < -drain) {
207  dy += drain;
208  }
209  else {
210  dy = 0;
211  }
212  }
213  else {
214  dx = 0;
215  dy = 0;
216  }
217 
218  if (Mouse::Wheel() > 0) {
219  if (t < 0.25)
220  t += 0.025;
221  else
222  t += 0.1;
223 
224  if (t > 1) t = 1;
225  }
226 
227  else if (Mouse::Wheel() < 0) {
228  if (t < 0.25)
229  t -= 0.025;
230  else
231  t -= 0.1;
232 
233  if (t < 0) t = 0;
234  }
235 }
236 
237 // +--------------------------------------------------------------------+
238 
239 int
241 {
242  if (n >= KEY_ACTION_0 && n <= KEY_ACTION_3)
243  return action[n - KEY_ACTION_0];
244 
245  return 0;
246 }
247