1 /*
2 
3 *************************************************************************
4 
5 ArmageTron -- Just another Tron Lightcycle Game in 3D.
6 Copyright (C) 2000  Manuel Moos (manuel@moosnet.de)
7 
8 **************************************************************************
9 
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License
12 as published by the Free Software Foundation; either version 2
13 of the License, or (at your option) any later version.
14 
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 GNU General Public License for more details.
19 
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23 
24 ***************************************************************************
25 
26 */
27 
28 #ifndef ArmageTron_INPUT_H
29 #define ArmageTron_INPUT_H
30 
31 #include "rSDL.h"
32 #include "tString.h"
33 #include "defs.h"
34 #include "tLinkedList.h"
35 #include "tLocale.h"
36 #include "tSafePTR.h"
37 
38 #define uMAX_PLAYERS 8
39 
40 extern bool su_mouseGrab;         //! true if the mouse cursor is to be confined into the window
41 extern REAL su_doubleBindTimeout; //! timeout value for double binds; second keypress for the same action gets ignored from a different key when within the timeout
42 
43 // ***************************
44 //      player controls
45 // ***************************
46 
47 #define uMAX_ACTIONS 300
48 
49 // the possible actions; player actions and global actions
50 
51 class uAction:public tListItem<uAction>{
52 protected:
53     int localID;  // unique id on this host
54     int globalID; // unique ID send from the server
55 public:
56     typedef enum {uINPUT_DIGITAL,uINPUT_ANALOG} uInputType;
57 
58     uInputType     	type;
59     int				priority;
60     tString        	internalName;
61     const tOutput  	description;
62     const tOutput  	helpText;
63 
64 #ifdef SLOPPYLOCALE
65     //  uAction(uAction *&anchor,const char *name,const char *desc,const char *help,
66     //	 uInputType t=uINPUT_DIGITAL);
67 #endif
68     uAction(uAction *&anchor,const char* name,
69             int priority = 0,
70             uInputType t=uINPUT_DIGITAL);
71 
72     uAction(uAction *&anchor,const char* name,
73             const tOutput& desc,
74             const tOutput& help,
75             int priority = 0,
76             uInputType t=uINPUT_DIGITAL);
77 
78     virtual ~uAction();
79 
80     //! find an action of the given name
81     static uAction * Find( char const * name );
82 
ID()83     unsigned short ID() const{return globalID;}
84 };
85 
86 
87 // player actions (move,shoot) and global actions (stop game, pause..)
88 
89 class uActionPlayer:public uAction{
90 public:
91     uActionPlayer(const char *name,
92                   int priority = 0,
93                   uInputType 	t=uINPUT_DIGITAL);
94 
95     uActionPlayer(const char *name,
96                   const tOutput& desc,
97                   const tOutput& help,
98                   int priority = 0,
99                   uInputType t=uINPUT_DIGITAL);
100 
101     virtual ~uActionPlayer();
102 
103     bool operator==(const uActionPlayer &x);
104 
105     static uActionPlayer *Find(int globalID);
106 };
107 
108 class uActionCamera:public uAction{
109 public:
110     uActionCamera(const char *name,
111                   int priority = 0,
112                   uInputType 	t=uINPUT_DIGITAL);
113 
114     virtual ~uActionCamera();
115 
116     bool operator==(const uActionCamera &x);
117 };
118 
119 // global actions
120 class uActionGlobal:public uAction{
121 public:
122     uActionGlobal(const char *name,
123                   int priority = 0,
124                   uInputType 	t=uINPUT_DIGITAL);
125 
126     virtual ~uActionGlobal();
127 
128     bool operator==(const uActionGlobal &x);
129 
130     // binding that should interrupt chat/console input
131     static bool IsBreakingGlobalBind(int sym);
132 };
133 
134 
135 
136 
137 // *********************************************
138 // generic keypress/mouse movement binding class
139 // *********************************************
140 
141 class uBind: public tReferencable< uBind >
142 {
143     friend class uMenuItemInput;
144 
145     virtual bool Delayable()=0;
146     virtual bool DoActivate(REAL x)=0;
147     REAL lastValue_;
148     REAL delayedValue_;
149 
150     int lastSym_;     //! the last keysym used to activate this
151     double lastTime_; //! the time of the last usage
152 
153 public:
154     uAction *act;
155 
156     uBind(uAction *a );
157     uBind(std::istream &s);
158     virtual ~uBind();
159 
160     virtual void Write(std::ostream &s);
161 
162     virtual bool CheckPlayer(int p)=0;
163 
164     bool Activate(REAL x, bool delayed );
165     void HanldeDelayed();
166 
167     //! checks if the sym is used for double binding, return true
168     bool IsDoubleBind( int sym );
169 };
170 
171 // extra binds for mouse (and joystick, as soon as SDL supports it) movement:
172 #define SDLK_MOUSE_X_PLUS   (SDLK_LAST+1)
173 #define SDLK_MOUSE_X_MINUS  (SDLK_LAST+2)
174 #define SDLK_MOUSE_Y_PLUS   (SDLK_LAST+3)
175 #define SDLK_MOUSE_Y_MINUS  (SDLK_LAST+4)
176 #define SDLK_MOUSE_Z_PLUS   (SDLK_LAST+5)
177 #define SDLK_MOUSE_Z_MINUS  (SDLK_LAST+6)
178 #define SDLK_MOUSE_BUTTON_1 (SDLK_LAST+7)
179 #define SDLK_MOUSE_BUTTON_2 (SDLK_LAST+8)
180 #define SDLK_MOUSE_BUTTON_3 (SDLK_LAST+9)
181 #define SDLK_MOUSE_BUTTON_4 (SDLK_LAST+10)
182 #define SDLK_MOUSE_BUTTON_5 (SDLK_LAST+11)
183 #define SDLK_MOUSE_BUTTON_6 (SDLK_LAST+12)
184 #define SDLK_MOUSE_BUTTON_7 (SDLK_LAST+13)
185 #define SDLK_NEWLAST        (SDLK_LAST+14)
186 #define MOUSE_BUTTONS 7
187 
188 // one key_action for every keysym
189 extern tJUST_CONTROLLED_PTR< uBind > keymap[SDLK_NEWLAST];
190 
191 // *****************
192 // Player binds
193 // *****************
194 
195 class uBindPlayer:public uBind{
196     int            ePlayer;
197 
198     virtual bool Delayable();
199     virtual bool DoActivate(REAL x);
200 
201     uBindPlayer(uAction *a,int p);
202 public:
203     virtual ~uBindPlayer();
204 
205     static uBindPlayer * NewBind( uAction * action, int player );
206     static uBindPlayer * NewBind( std::istream & s );
207 
208     static bool IsKeyWord(const char *n);
209 
210     virtual bool CheckPlayer(int p);
211 
212     virtual void Write(std::ostream &s);
213 };
214 
215 
216 // *****************
217 // Global actions
218 // *****************
219 
220 typedef bool ACTION_FUNC(REAL x);
221 
222 class uActionGlobalFunc: public tListItem<uActionGlobalFunc>{
223     ACTION_FUNC   *func;
224     uActionGlobal *act;
225     bool           rebindable;
226 
227 public:
228     uActionGlobalFunc(uActionGlobal *a, ACTION_FUNC *f, bool rebind = true );
229     static bool IsBreakingGlobalBind(uAction *act);
230     static bool GlobalAct(uAction *act, REAL x);
231 };
232 
233 
234 // ***************************
235 //      player prototype
236 // ***************************
237 
238 class uPlayerPrototype{
239 protected:
240     static uPlayerPrototype* playerConfig[uMAX_PLAYERS];
241     int id;
242 public:
243     uPlayerPrototype();
244     virtual ~uPlayerPrototype();
245 
246     virtual bool         Act(uAction *act, REAL x)=0;
247     virtual const char * Name() const            =0;
248 
249     static uPlayerPrototype* PlayerConfig(int i);
250     static int Num();
251 };
252 
253 
254 
255 // the input configuration menu
256 void su_InputConfig(int player);
257 void su_InputConfigCamera(int player);
258 void su_InputConfigGlobal();
259 bool su_HandleEvent(SDL_Event &e, bool delayed );	// handle event during gameplay
260 void su_HandleDelayedEvents( );				// set menu state
261 
262 void su_InputSync(); // tells the input system that a new frame has been drawn;
263 // autorepeat functions may be called.
264 void su_ClearKeys(); // clears all keys into the unpressed state.
265 #endif
266 
267