Planeshift

pscharcontrol.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2006 Atomic Blue ([email protected], http://www.atomicblue.org)
00003  *
00004  *
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * as published by the Free Software Foundation (version 2 of the License)
00008  * This program is distributed in the hope that it will be useful,
00009  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  * GNU General Public License for more details.
00012  * You should have received a copy of the GNU General Public License
00013  * along with this program; if not, write to the Free Software
00014  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00015  *
00016  */
00017 
00018 #ifndef __PSCHARCONTROL_H__
00019 #define __PSCHARCONTROL_H__
00020 //=============================================================================
00021 // Crystal Space Includes
00022 //=============================================================================
00023 #include <csutil/hash.h>
00024 #include <csutil/csstring.h>
00025 #include <iutil/eventnames.h>
00026 
00027 //=============================================================================
00028 // Project Includes
00029 //=============================================================================
00030 
00031 //=============================================================================
00032 // Local Includes
00033 //=============================================================================
00034 #include "psmovement.h"
00035 
00036 class psTriggerHandler;
00037 class psCharController;
00038 
00039 
00049 struct psControl
00050 {
00052     typedef void (psTriggerHandler::*TriggerFunction)(const psControl*, bool);
00053 
00055     typedef const void* DataPtr;
00056 
00057     enum PressType
00058     {
00059         NORMAL,  
00060         TOGGLE   
00061     };
00062 
00063     enum Device
00064     {
00065         NONE,      
00066         KEYBOARD,  
00067         MOUSE      
00068     };
00069 
00070     psControl(const char* n, PressType t, TriggerFunction f)
00071         : name(n), state(false), device(NONE), button(0), mods(0), type(t), function(f), data(NULL) {}
00072 
00073     csString name;             
00074     bool state;                
00075     Device device;             
00076     uint button;               
00077     uint32 mods;               
00078     PressType type;            
00079     TriggerFunction function;  
00080     DataPtr data;              
00081 
00082     void Execute() const;      
00083     csString ToString() const; 
00084 
00085     static psTriggerHandler* handler;  
00086 };
00087 
00088 
00090 csString ComboToString(psControl::Device device, uint button, uint32 mods);
00091 
00093 csString GetDisplayName(const char* name);
00094 
00099 // CS strangeness requires different methods for mouse and key modifiers
00100 uint32 GetPSKeyMods(const iEvent* event);
00101 uint32 GetPSMouseMods(const iEvent* event);
00102 
00107 #define PS_MODS_MASK CSMASK_ALLSHIFTS
00108 
00109 
00122 class psControlManager
00123 {
00124 public:
00125     psControlManager(iEventNameRegistry* eventname_reg, psTriggerHandler* handler);
00126 
00127     bool HandleEvent(iEvent &event);
00128 
00130     void NewTrigger(const char* name, psControl::PressType type, psControl::TriggerFunction function);
00131 
00133     bool MapTrigger(const char* name, psControl::Device device, uint button, uint32 mods);
00134 
00135     void ResetTrigger(psControl* trigger);    
00136     void ResetAllTriggers();                  
00137 
00139     psControl* GetTrigger(const char* name);
00140 
00142     psControl* GetMappedTrigger(psControl::Device device, uint button, uint32 mods);
00143 
00145     csArray<psControl*>* GetMappedTriggers(psControl::Device device, uint button);
00146 
00148     const csPDelArray<psControl> &GetAllTriggers()
00149     {
00150         return triggers;
00151     }
00152 
00154     bool SetTriggerData(const char* name, const void* ptr);
00155 
00156 protected:
00157     typedef csHash<psControl*,uint> psControlMap;  
00158 
00159     psControlMap keyboard;   
00160     psControlMap mouse;      
00161 
00162     csPDelArray<psControl> triggers;  
00163 
00164     // Event ID cache
00165     csEventID event_key_down;
00166     csEventID event_key_up;
00167     csEventID event_mouse_down;
00168     csEventID event_mouse_up;
00169 
00171     static psControl* GetFromMap(const psControlMap &ctrlmap, uint button, uint32 mods);
00172 
00174     csArray<psControl*>* GetArrayFromMap(const psControlMap &ctrlmap, uint button);
00175 
00177     void HandleButton(psControl::Device device, uint button, uint32 mods, bool newState);
00178 
00179 
00180 };
00181 
00182 
00190 class psTriggerHandler
00191 {
00192 public:
00193     psTriggerHandler(psCharController* controler);
00194     ~psTriggerHandler();
00195 
00196     // Functions called by triggers  (psControl stores pointers to these)
00197     void HandleBrightnessUp(const psControl* trigger, bool value);
00198     void HandleBrightnessDown(const psControl* trigger, bool value);
00199     void HandleBrightnessReset(const psControl* trigger, bool value);
00200     void HandleMovement(const psControl* trigger, bool value);
00201     void HandleMovementJump(const psControl* trigger, bool value);
00202     void HandleMode(const psControl* trigger, bool value);
00203     void HandleAutoMove(const psControl* trigger, bool value);
00204     void HandleLook(const psControl* trigger, bool value);
00205     void HandleZoom(const psControl* trigger, bool value);
00206     void HandleMouseLook(const psControl* trigger, bool value);
00207     void HandleMouseLookToggle(const psControl* trigger, bool value);
00208     void HandleMouseZoom(const psControl* trigger, bool value);
00209     void HandleMouseMove(const psControl* trigger, bool value);
00210     void HandleCameraMode(const psControl* trigger, bool value);
00211     void HandleCenterCamera(const psControl* trigger, bool value);
00212     void HandleMovementAction(const psControl* trigger, bool value);
00213     void HandleShortcut(const psControl* trigger, bool value);
00214     void HandleWindow(const psControl* trigger, bool value);
00215     void HandleModeRun(const psControl* trigger, bool value);
00216     void HandleModeSneak(const psControl* trigger, bool value);
00217 
00218 protected:
00219     psCharController* charcontrol;
00220     psMovementManager* movement;
00221 };
00222 
00223 
00229 class psCharController
00230 {
00231 public:
00232     psCharController(iEventNameRegistry* eventname_reg);
00233     ~psCharController();
00234 
00235     bool Initialize();
00236     bool IsReady();
00237 
00239     bool HandleEvent(iEvent &event);
00240 
00242     psMovementManager* GetMovementManager()
00243     {
00244         return &movement;
00245     }
00246 
00248     const psControl* GetTrigger(const char* name);
00249 
00251     const psControl* GetMappedTrigger(psControl::Device device, uint button, uint32 mods);
00252 
00254     bool RemapTrigger(const char* name, psControl::Device device, uint button, uint32 mods);
00255 
00257     bool MatchTrigger(const char* name, psControl::Device device, uint button, uint32 mods);
00258 
00260     void LoadKeyFile();
00261 
00263     void LoadDefaultKeys();
00264 
00265     void CenterMouse(bool value);
00266     void CancelMouseLook();
00267 
00268 protected:
00269     psTriggerHandler handler;    
00270     psControlManager controls;   
00271     psMovementManager movement;  
00272 
00273     void CreateKeys();                
00274     bool LoadKeys(const char* file);  
00275     void SaveKeys();                  
00276 
00277     bool ready;  
00278 
00279     csEventID event_mouseclick;
00280 };
00281 
00282 #endif
00283