Planeshift
pscharcontrol.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006 Atomic Blue (info@planeshift.it, http://www.atomicblue.org)
3  *
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation (version 2 of the License)
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  *
16  */
17 
18 #ifndef __PSCHARCONTROL_H__
19 #define __PSCHARCONTROL_H__
20 //=============================================================================
21 // Crystal Space Includes
22 //=============================================================================
23 #include <csutil/hash.h>
24 #include <csutil/csstring.h>
25 #include <iutil/eventnames.h>
26 
27 //=============================================================================
28 // Project Includes
29 //=============================================================================
30 
31 //=============================================================================
32 // Local Includes
33 //=============================================================================
34 #include "psmovement.h"
35 
36 class psTriggerHandler;
37 class psCharController;
38 
39 
49 struct psControl
50 {
52  typedef void (psTriggerHandler::*TriggerFunction)(const psControl*, bool);
53 
55  typedef const void* DataPtr;
56 
57  enum PressType
58  {
61  };
62 
63  enum Device
64  {
65  NONE,
68  };
69 
70  psControl(const char* n, PressType t, TriggerFunction f)
71  : name(n), state(false), device(NONE), button(0), mods(0), type(t), function(f), data(NULL) {}
72 
73  csString name;
74  bool state;
76  uint button;
77  uint32 mods;
79  TriggerFunction function;
80  DataPtr data;
81 
82  void Execute() const;
83  csString ToString() const;
84 
86 };
87 
88 
90 csString ComboToString(psControl::Device device, uint button, uint32 mods);
91 
93 csString GetDisplayName(const char* name);
94 
99 // CS strangeness requires different methods for mouse and key modifiers
100 uint32 GetPSKeyMods(const iEvent* event);
101 uint32 GetPSMouseMods(const iEvent* event);
102 
107 #define PS_MODS_MASK CSMASK_ALLSHIFTS
108 
109 
123 {
124 public:
125  psControlManager(iEventNameRegistry* eventname_reg, psTriggerHandler* handler);
126 
127  bool HandleEvent(iEvent &event);
128 
130  void NewTrigger(const char* name, psControl::PressType type, psControl::TriggerFunction function);
131 
133  bool MapTrigger(const char* name, psControl::Device device, uint button, uint32 mods);
134 
135  void ResetTrigger(psControl* trigger);
136  void ResetAllTriggers();
137 
139  psControl* GetTrigger(const char* name);
140 
142  psControl* GetMappedTrigger(psControl::Device device, uint button, uint32 mods);
143 
145  csArray<psControl*>* GetMappedTriggers(psControl::Device device, uint button);
146 
148  const csPDelArray<psControl> &GetAllTriggers()
149  {
150  return triggers;
151  }
152 
154  bool SetTriggerData(const char* name, const void* ptr);
155 
156 protected:
157  typedef csHash<psControl*,uint> psControlMap;
158 
159  psControlMap keyboard;
160  psControlMap mouse;
161 
162  csPDelArray<psControl> triggers;
163 
164  // Event ID cache
165  csEventID event_key_down;
166  csEventID event_key_up;
167  csEventID event_mouse_down;
168  csEventID event_mouse_up;
169 
171  static psControl* GetFromMap(const psControlMap &ctrlmap, uint button, uint32 mods);
172 
174  csArray<psControl*>* GetArrayFromMap(const psControlMap &ctrlmap, uint button);
175 
177  void HandleButton(psControl::Device device, uint button, uint32 mods, bool newState);
178 
179 
180 };
181 
182 
191 {
192 public:
194  ~psTriggerHandler();
195 
196  // Functions called by triggers (psControl stores pointers to these)
197  void HandleBrightnessUp(const psControl* trigger, bool value);
198  void HandleBrightnessDown(const psControl* trigger, bool value);
199  void HandleBrightnessReset(const psControl* trigger, bool value);
200  void HandleMovement(const psControl* trigger, bool value);
201  void HandleMovementJump(const psControl* trigger, bool value);
202  void HandleMode(const psControl* trigger, bool value);
203  void HandleAutoMove(const psControl* trigger, bool value);
204  void HandleLook(const psControl* trigger, bool value);
205  void HandleZoom(const psControl* trigger, bool value);
206  void HandleMouseLook(const psControl* trigger, bool value);
207  void HandleMouseLookToggle(const psControl* trigger, bool value);
208  void HandleMouseZoom(const psControl* trigger, bool value);
209  void HandleMouseMove(const psControl* trigger, bool value);
210  void HandleCameraMode(const psControl* trigger, bool value);
211  void HandleCenterCamera(const psControl* trigger, bool value);
212  void HandleMovementAction(const psControl* trigger, bool value);
213  void HandleShortcut(const psControl* trigger, bool value);
214  void HandleWindow(const psControl* trigger, bool value);
215  void HandleModeRun(const psControl* trigger, bool value);
216  void HandleModeSneak(const psControl* trigger, bool value);
217 
218 protected:
221 };
222 
223 
230 {
231 public:
232  psCharController(iEventNameRegistry* eventname_reg);
233  ~psCharController();
234 
235  bool Initialize();
236  bool IsReady();
237 
239  bool HandleEvent(iEvent &event);
240 
243  {
244  return &movement;
245  }
246 
248  const psControl* GetTrigger(const char* name);
249 
251  const psControl* GetMappedTrigger(psControl::Device device, uint button, uint32 mods);
252 
254  bool RemapTrigger(const char* name, psControl::Device device, uint button, uint32 mods);
255 
257  bool MatchTrigger(const char* name, psControl::Device device, uint button, uint32 mods);
258 
260  void LoadKeyFile();
261 
263  void LoadDefaultKeys();
264 
265  void CenterMouse(bool value);
266  void CancelMouseLook();
267 
268 protected:
272 
273  void CreateKeys();
274  bool LoadKeys(const char* file);
275  void SaveKeys();
276 
277  bool ready;
278 
279  csEventID event_mouseclick;
280 };
281 
282 #endif
283 
Manages main character movements.
Definition: psmovement.h:84
bool state
Is this active?
Definition: pscharcontrol.h:74
psControlMap mouse
List of all mouse triggers.
Manages button functions and triggers.
Device device
Location of button.
Definition: pscharcontrol.h:75
Handles functions for each trigger.
Mouseclick.
Definition: pscharcontrol.h:67
psCharController * charcontrol
Defines a generic button trigger.
Definition: pscharcontrol.h:49
csString name
Name of this control.
Definition: pscharcontrol.h:73
const void * DataPtr
Pointer to an object used by a TriggerFunction.
Definition: pscharcontrol.h:55
psMovementManager * GetMovementManager()
Provides access to the movement system.
psTriggerHandler handler
Trigger functions.
csString ComboToString(psControl::Device device, uint button, uint32 mods)
Returns a string of the form "mods+button".
Triggers on press and un-triggers on another press.
Definition: pscharcontrol.h:60
csEventID event_key_down
psMovementManager * movement
csEventID event_mouse_down
DataPtr data
Optional pointer to data needed by function.
Definition: pscharcontrol.h:80
csHash< psControl *, uint > psControlMap
Each device gets a hash map of buttons to triggers.
TriggerFunction function
Function called when triggered.
Definition: pscharcontrol.h:79
uint32 mods
Modifiers to go with the button.
Definition: pscharcontrol.h:77
void(psTriggerHandler::* TriggerFunction)(const psControl *, bool)
Pointer to a member function in the psTriggerHandler class.
Definition: pscharcontrol.h:52
uint button
Button this is bound to.
Definition: pscharcontrol.h:76
PressType type
Trigger method.
Definition: pscharcontrol.h:78
csString GetDisplayName(const char *name)
Resolves the custom name for a trigger (shortcut&#39;s name, or removes "(sec)" qualifiers) ...
csString ToString() const
Returns button combo in string form.
uint32 GetPSKeyMods(const iEvent *event)
Function which is used to extract just the modifier bits we use from a CS event.
psControlMap keyboard
List of all keyboard triggers.
void void Initialize(iObjectRegistry *object_reg)
Manages all control and movement related activities.
uint32 GetPSMouseMods(const iEvent *event)
const csPDelArray< psControl > & GetAllTriggers()
Get the full array of triggers.
void Execute() const
Does whatever this does.
psMovementManager movement
Movement system.
csEventID event_mouse_up
psControlManager controls
Key mappings.
Triggers on press and un-triggers on release.
Definition: pscharcontrol.h:59
csPDelArray< psControl > triggers
Master list of all triggers.
csEventID event_key_up
csEventID event_mouseclick
static psTriggerHandler * handler
Static pointer to the function handler class.
Definition: pscharcontrol.h:85
bool ready
Ready to process events?
psControl(const char *n, PressType t, TriggerFunction f)
Definition: pscharcontrol.h:70