Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
in_mouse.cpp
Go to the documentation of this file.
1 /*
2  * Vega Strike
3  * Copyright (C) 2001-2002 Daniel Horn
4  *
5  * http://vegastrike.sourceforge.net/
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */
21 
22 #include "vegastrike.h"
23 #include "in_handler.h"
24 #include "in_mouse.h"
25 #include <deque>
26 #include "vs_globals.h"
27 #include "config_xml.h"
28 #include "in_joystick.h"
29 #include "gldrv/winsys.h"
30 using std::deque;
31 #define NUM_BUTTONS 15
32 
35 {
36  return NUM_BUTTONS;
37 }
40 
41 int mousex = 0;
42 int mousey = 0;
43 void GetMouseXY( int &mx, int &my )
44 {
45  mx = mousex;
46  my = mousey;
47 }
49 {
50  int ret = 0;
51  for (int i = 0; i < NUM_BUTTONS; i++)
52  ret |= (MouseState[i] == PRESS || MouseState[i] == DOWN) ? (1<<i) : 0;
53  return ret;
54 }
55 
56 struct MouseEvent
57 {
59  int button;
60  int state;
61  int mod;
62  int x;
63  int y;
64  MouseEvent( EventType type, int button, int state, int mod, int x, int y ) : type( type )
65  , button( button )
66  , state( state )
67  , mod( mod )
68  , x( x )
69  , y( y ) {}
70 };
71 
72 static deque< MouseEvent >eventQueue;
73 void mouseClickQueue( int button, int state, int x, int y )
74 {
75  int mod = 0;
76  eventQueue.push_back( MouseEvent( MouseEvent::CLICK, button, state, mod, x, y ) );
77 }
78 int delx = 0;
79 int dely = 0;
80 void AddDelta( int dx, int dy )
81 {
82  delx += dx;
83  dely += dy;
84 }
85 int warpallowage = 2;
86 void DealWithWarp( int x, int y )
87 {
88  static bool warp_pointer = XMLSupport::parse_bool( vs_config->getVariable( "joystick", "warp_mouse", "false" ) );
89  static int mouse_warp_zone = XMLSupport::parse_int( vs_config->getVariable( "joystick", "warp_mouse_zone", "100" ) );
90  if (warp_pointer) {
91  if ( joystick[MOUSE_JOYSTICK]->player < _Universe->numPlayers() ) {
92  if (x < mouse_warp_zone || y < mouse_warp_zone || x > g_game.x_resolution-mouse_warp_zone || y
93  > g_game.y_resolution-mouse_warp_zone) {
94 
95  int delx = -x+g_game.x_resolution/2;
96  int dely = -y+g_game.y_resolution/2;
97  mousex += delx;
98  mousey += dely;
99  deque< MouseEvent >::iterator i;
100  for (i = eventQueue.begin(); i != eventQueue.end(); i++) {
101  i->x += delx;
102  i->y += dely;
103  }
104  if (warpallowage-- >= 0)
106  }
107  }
108  }
109 }
110 
111 void mouseDragQueue( int x, int y )
112 {
113  eventQueue.push_back( MouseEvent( MouseEvent::DRAG, -1, -1, -1, x, y ) );
114  DealWithWarp( x, y );
115 }
116 
117 void mouseMotionQueue( int x, int y )
118 {
119  eventQueue.push_back( MouseEvent( MouseEvent::MOTION, -1, -1, -1, x, y ) );
120  DealWithWarp( x, y );
121 }
122 
124 {
125  static int adj = 0;
126  if (b+adj < WS_LEFT_BUTTON)
127  adj = WS_LEFT_BUTTON-b;
128  b += adj;
129  switch (b)
130  {
131  case WS_LEFT_BUTTON:
132  return 0;
133 
134  case WS_RIGHT_BUTTON:
135  return 2;
136 
137  case WS_MIDDLE_BUTTON:
138  return 1;
139 
140  case WS_WHEEL_UP:
141  return 3;
142 
143  case WS_WHEEL_DOWN:
144  return 4;
145 
146  default:
147  return ( (b-WS_LEFT_BUTTON) >= NUM_BUTTONS ) ? NUM_BUTTONS-1 : b-WS_LEFT_BUTTON;
148  }
149  return 0;
150 }
151 void mouseClick0( int button, int state, int mod, int x, int y )
152 {
153  button = lookupMouseButton( button );
154  if (button >= NUM_BUTTONS) return;
155  AddDelta( x-mousex, y-mousey );
156  mousex = x;
157  mousey = y;
158  mouseBindings[button] ( state == WS_MOUSE_DOWN ? PRESS : RELEASE, x, y, 0, 0, mod );
159  MouseState[button] = (state == WS_MOUSE_DOWN) ? DOWN : UP;
160 }
161 void SetDelta( int dx, int dy )
162 {
163  delx = dx;
164  dely = dy;
165 }
166 void GetMouseDelta( int &dx, int &dy )
167 {
168  dx = delx;
169  dy = dely;
170  delx = dely = 0;
171 }
172 
173 void mouseDrag( int x, int y )
174 {
175  for (int i = 0; i < NUM_BUTTONS+1; i++)
176  mouseBindings[i] ( MouseState[i], x, y, x-mousex, y-mousey, 0 );
177  AddDelta( x-mousex, y-mousey );
178  mousex = x;
179  mousey = y;
180 }
181 
182 void mouseMotion( int x, int y )
183 {
184  for (int i = 0; i < NUM_BUTTONS+1; i++)
185  mouseBindings[i] ( MouseState[i], x, y, x-mousex, y-mousey, 0 );
186  AddDelta( x-mousex, y-mousey );
187  mousex = x;
188  mousey = y;
189 }
190 
191 static void DefaultMouseHandler( KBSTATE, int x, int y, int delx, int dely, int mod ) {}
192 
193 void UnbindMouse( int key )
194 {
196 }
197 void BindKey( int key, MouseHandler handler )
198 {
199  mouseBindings[key] = handler;
200  handler( RESET, mousex, mousey, 0, 0, 0 );
201 }
203 {
207 }
208 
209 void InitMouse()
210 {
211  for (int a = 0; a < NUM_BUTTONS+1; a++)
212  UnbindMouse( a );
213  RestoreMouse();
214 }
215 
217 {
218  warpallowage = 2;
219  while ( eventQueue.size() ) {
220  MouseEvent e = eventQueue.front();
221  switch (e.type)
222  {
223  case MouseEvent::CLICK:
224  mouseClick0( e.button, e.state, e.mod, e.x, e.y );
225  break;
226  case MouseEvent::DRAG:
227  mouseDrag( e.x, e.y );
228  break;
229  case MouseEvent::MOTION:
230  mouseMotion( e.x, e.y );
231  break;
232  }
233  eventQueue.pop_front();
234  }
235 }
236