00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00029
00030 #ifndef INPUT_CONTROL_H
00031 #define INPUT_CONTROL_H
00032
00033 #include <PVLE/Export.h>
00034 #include <PVLE/Util/Util.h>
00035 #include <bitset>
00036 #include <osg/Vec3>
00037
00038
00042 class PVLE_EXPORT ControlState {
00043 public:
00044 static const UINT NB_USER_SWITCHES = 30;
00045
00050 enum ESwitchId {
00051 NONE=0,
00052 FORWARD, BACKWARD, LEFT, RIGHT, STRAFE_LEFT, STRAFE_RIGHT, UP, DOWN,
00053 ROLL_LEFT, ROLL_RIGHT,
00054 JUMP, CROUCH,
00055 FAST_TOGGLE, ALWAYS_FAST,
00056 FASTER, SLOWER,
00057 FIRE1, FIRE2, FIRE3, FIRE4, FIRE5,
00058 WEAPON_NEXT, WEAPON_PREV, WEAPON_TOGGLE,
00059 WEAPON1, WEAPON2, WEAPON3, WEAPON4, WEAPON5, WEAPON6, WEAPON7, WEAPON8, WEAPON9, WEAPON10,
00060 WEAPON11, WEAPON12, WEAPON13, WEAPON14, WEAPON15, WEAPON16, WEAPON17, WEAPON18, WEAPON19, WEAPON20,
00061 DROP_WEAPON, DROP_ALTERNATE,
00062 TARGET, TARGET_NEXT, TARGET_PREV, SHOW_INFO,
00063 VIEW_NEXT, VIEW_PREV,
00064 ZOOM_IN, ZOOM_OUT,
00065 VIEW1, VIEW2, VIEW3, VIEW4, VIEW5, VIEW6, VIEW7, VIEW8, VIEW9, VIEW10, VIEW11, VIEW12, VIEW13, VIEW14, VIEW15,
00066 PAUSE,
00067 SCREENSHOT,
00068 TOGGLE_FULLSCREEN,
00069 BRAKE, RESET,
00070 MENU, SCORE_MENU,
00071 OK, CANCEL,
00072
00073 MAX_STARNDARD_SWITCH,
00074 USER_SWITCH_0 = MAX_STARNDARD_SWITCH,
00075
00076 MAX_SWITCH = USER_SWITCH_0 + NB_USER_SWITCHES
00077 };
00078
00079
00081 enum EAxisId {
00082 AXIS_X=0, AXIS_Y, AXIS_Z,
00083 AXIS_SCROLL_1, AXIS_SCROLL_2, AXIS_SCROLL_3, AXIS_SCROLL_4,
00084 MAX_AXIS
00085 };
00086
00087 ControlState();
00088
00090 ControlState & operator+=(ESwitchId switchId) { set(switchId); return *this; }
00092 ControlState & operator-=(ESwitchId switchId) { reset(switchId); return *this; }
00094 bool operator[](ESwitchId switchId) const { return vSwitchs[switchId]; }
00095
00097 void set(ESwitchId switchId) { if (vSwitchs[switchId]) return; vSwitchs[switchId] = true; ++nbSwitchesOn; }
00099 void reset(ESwitchId switchId) { if (!vSwitchs[switchId]) return; ASSERT(nbSwitchesOn>0); vSwitchs[switchId] = false; --nbSwitchesOn; }
00101 bool get(ESwitchId switchId) const { return vSwitchs[switchId]; }
00103 void resetAllSwitches() { vSwitchs.reset(); nbSwitchesOn = 0; }
00104
00106 void setAxis(EAxisId axis, float val) { ASSERT(axis>=0 && axis<MAX_AXIS); pAxis[axis] = val; }
00108 void addToAxis(EAxisId axis, float val) { ASSERT(axis>=0 && axis<MAX_AXIS); pAxis[axis] += val; }
00110 float getAxis(EAxisId axis) const { ASSERT(axis>=0 && axis<MAX_AXIS); return pAxis[axis]; }
00112 void resetAxis(EAxisId axis) { ASSERT(axis>=0 && axis<MAX_AXIS); pAxis[axis] = 0; }
00114 void resetAllAxis();
00116 bool isAxisZero(EAxisId axis, float epsilon = 0.0001f) { ASSERT(axis>=0 && axis<MAX_AXIS); return (fabs(pAxis[axis]) < epsilon); }
00117
00119 bool testAnySwitchOn() const { return nbSwitchesOn>0; }
00120
00123 bool testAnyAxisOn(float epsilon = 1e-4f) const {
00124
00125 BOOST_FOREACH(float axisValue, pAxis) if (fabs(axisValue) > epsilon) return true;
00126 return false;
00127 }
00128
00130 bool testAnyControlOn(float epsilon = 0.0001f) const { return testAnySwitchOn() || testAnyAxisOn(epsilon); }
00131
00133 void setPointer(float pointerX, float pointerY) { pointer = osg::Vec3f(pointerX, pointerY, 0); }
00135 void setPointer(const osg::Vec3f & pointer) { this->pointer = pointer; }
00137 osg::Vec3f getPointer() const { return pointer; }
00140 float getPointerX() const { return pointer.x(); }
00143 float getPointerY() const { return pointer.y(); }
00147 osg::Vec3f getPickPosition() const { return pickPos; }
00150 void setPickPosition(const osg::Vec3f & pickPos) { this->pickPos = pickPos; }
00151
00152 protected:
00153 UINT nbSwitchesOn;
00154 std::bitset<MAX_SWITCH> vSwitchs;
00155 float pAxis[MAX_AXIS];
00156 osg::Vec3f pointer;
00157 osg::Vec3f pickPos;
00158 };
00159
00160
00161
00162
00163 #include <PVLE/Config.h>
00164 #ifdef PVLE_NETWORKING
00165 #include <tnl/tnlNetEvent.h>
00166 #endif
00167
00172 class ControlEvent {
00173 public:
00174 enum EEventType {
00175 SWITCH_DOWN, SWITCH_UP, AXIS,
00176 LAST_NETWORK_EVENT,
00177
00178 GFX_FRAME = LAST_NETWORK_EVENT, GFX_RESIZE
00179 };
00180
00182 ControlEvent(double time, EEventType type) : time(time), type(type) {}
00184 ControlEvent(double time, ControlState::ESwitchId switchId, int unmappedKey, bool down) : time(time) { data.switchId.first = switchId; data.switchId.second = unmappedKey; type = down ? SWITCH_DOWN : SWITCH_UP; }
00186 ControlEvent(double time, ControlState::EAxisId axisId, float movement) : time(time), type(AXIS) { data.axisMovement.first = axisId; data.axisMovement.second = movement; }
00188 ControlEvent(const ControlEvent & v) : time(v.time), type(v.type) {
00189 if (type == AXIS) data.axisMovement = v.data.axisMovement;
00190 else if (type == SWITCH_DOWN || type == SWITCH_UP) data.switchId = v.data.switchId;
00191 }
00192
00194 inline double getTime() const { return time; }
00196 inline EEventType getType() const { return type; }
00197
00199 inline ControlState::ESwitchId getSwitchId() const { ASSERT(type == SWITCH_DOWN || type == SWITCH_UP); return data.switchId.first; }
00201 inline int getUnmappedKey() const { ASSERT(type == SWITCH_DOWN || type == SWITCH_UP); return data.switchId.second; }
00203 inline ControlState::EAxisId getAxisId() const { ASSERT(type == AXIS); return data.axisMovement.first; }
00205 inline float getAxisMovement() const { ASSERT(type == AXIS); return data.axisMovement.second; }
00206
00207 protected:
00208 double time;
00209 EEventType type;
00210
00212 union TEventDependantData {
00215 TrivialPair<ControlState::ESwitchId, int> switchId;
00216 TrivialPair<ControlState::EAxisId, float> axisMovement;
00217 };
00218 TEventDependantData data;
00219 };
00220
00221 typedef std::vector<ControlEvent> ControlEventList;
00222
00223
00224 #ifdef PVLE_NETWORKING
00226 class PVLE_EXPORT NetControlEvent : public ControlEvent, public TNL::NetEvent {
00227 public:
00228 TNL_DECLARE_CLASS(NetControlEvent);
00229
00231 NetControlEvent(double time, EEventType type) : ControlEvent(time, type), NetEvent(GuaranteedOrdered, DirClientToServer) {}
00233 NetControlEvent(double time, ControlState::ESwitchId switchId, int unmappedKey, bool down) : ControlEvent(time, switchId, unmappedKey, down), NetEvent(GuaranteedOrdered, DirClientToServer) {}
00235 NetControlEvent(double time, ControlState::EAxisId axisId, float movement) : ControlEvent(time, axisId, movement), NetEvent(GuaranteedOrdered, DirClientToServer) {}
00237 NetControlEvent(const ControlEvent & v) : ControlEvent(v) {}
00238
00241 virtual void pack(TNL::EventConnection * pConnection, TNL::BitStream * pStream);
00242 virtual void unpack(TNL::EventConnection * pConnection, TNL::BitStream * pStream);
00243 virtual void process(TNL::EventConnection * pConnection);
00245
00246 protected:
00247 friend class TNL::NetClassRepInstance<NetControlEvent>;
00249 NetControlEvent() : ControlEvent(-1, SWITCH_DOWN), NetEvent(GuaranteedOrdered, DirClientToServer) {}
00250
00251 };
00252
00253 #else // PVLE_NETWORKING
00254
00255
00256
00257 class PVLE_EXPORT NetControlEvent : public ControlEvent {
00258 public:
00259 NetControlEvent(double time, EEventType type) : ControlEvent(time, type) {}
00261 NetControlEvent(double time, ControlState::ESwitchId switchId, int unmappedKey, bool down) : ControlEvent(time, switchId, unmappedKey, down) {}
00263 NetControlEvent(double time, ControlState::EAxisId axisId, float movement) : ControlEvent(time, axisId, movement) {}
00265 NetControlEvent(const ControlEvent & v) : ControlEvent(v) {}
00266 };
00267
00268 #endif // PVLE_NETWORKING
00269
00270
00271
00272 namespace TNL {
00273 #ifdef PVLE_NETWORKING
00274 class EventConnection;
00275 #else
00276 typedef void EventConnection;
00277 #endif
00278 }
00279
00280
00284 class Controler {
00285 public:
00286 Controler() : pConnection(NULL) { vEvents.reserve(INITIAL_EVENTS_SIZE); }
00287 ControlState & getControlState() { return state; }
00288 const ControlState & getControlState() const { return state; }
00289
00290 ControlEventList::const_iterator getEventBegin() const { return vEvents.begin(); }
00291 ControlEventList::const_iterator getEventEnd() const { return vEvents.end(); }
00292 void clearEvents() { vEvents.clear(); }
00293 void clearAxis() { state.resetAllAxis(); }
00294 void clear() { vEvents.clear(); state.resetAllAxis(); }
00295
00299
00301 inline void doSwitch(double curTime, ControlState::ESwitchId switchId, int unmappedKey, bool pushed) {
00302 vEvents.push_back(ControlEvent(curTime, switchId, unmappedKey, pushed));
00303 if (pushed) state += switchId;
00304 else state -= switchId;
00305 }
00307 inline void doSwitchPush(double curTime, ControlState::ESwitchId switchId, int unmappedKey) {
00308 vEvents.push_back(ControlEvent(curTime, switchId, unmappedKey, true));
00309 state += switchId;
00310 }
00312 inline void doSwitchRelease(double curTime, ControlState::ESwitchId switchId, int unmappedKey) {
00313 vEvents.push_back(ControlEvent(curTime, switchId, unmappedKey, false));
00314 state -= switchId;
00315 }
00317 inline void doSwitchPR(double curTime, ControlState::ESwitchId switchId, int unmappedKey) {
00318 vEvents.push_back(ControlEvent(curTime, switchId, unmappedKey, true));
00319 vEvents.push_back(ControlEvent(curTime, switchId, unmappedKey, false));
00320 state -= switchId;
00321 }
00322
00324 inline void axisMove(double curTime, ControlState::EAxisId axisId, float movement) {
00325 #ifndef NO_AXIS_EVENT
00326 vEvents.push_back(ControlEvent(curTime, axisId, movement));
00327 #endif
00328 state.addToAxis(axisId, movement);
00329 }
00331
00332 #ifdef PVLE_NETWORKING
00335
00337 void setClientConnection(TNL::EventConnection * pConnection) { this->pConnection = pConnection; }
00338 TNL::EventConnection * getClientConnection() { return pConnection; }
00339 const TNL::EventConnection * getClientConnection() const { return pConnection; }
00341 #endif
00342
00343 protected:
00344 static const UINT INITIAL_EVENTS_SIZE = 8;
00345 ControlState state;
00346 ControlEventList vEvents;
00347
00348 TNL::EventConnection * pConnection;
00349 };
00350
00351
00352
00353
00354
00355
00356
00357 #include <PVLE/Util/Util.h>
00358 #include <map>
00359
00361 typedef std::map<int, ControlState::ESwitchId> Bindings;
00363 const UINT BUTTON_MOUSE_MODIFIER = 0x10000;
00365 const UINT BUTTON_JOY_MODIFIER = 0x10020;
00366
00367
00368
00369 namespace boost {
00370 namespace serialization {
00371 class access;
00372 }
00373 }
00374
00377 class AxisControl {
00378 public:
00379 AxisControl() : axisId(ControlState::MAX_AXIS), accel(1), sensitivity(1), deadzone(0) {}
00380
00381 AxisControl(ControlState::EAxisId axisId, float accel, float sensitivity, float deadzone)
00382 : axisId(axisId), accel(accel), sensitivity(sensitivity), deadzone(deadzone) {}
00383
00384 ControlState::EAxisId axisId;
00385 float accel;
00386 float sensitivity;
00387 float deadzone;
00388
00389 private:
00390
00391
00392
00393
00394
00395
00396
00397
00398 };
00399
00401 typedef std::map<int, AxisControl> AxisBindings;
00403 const UINT AXIS_MOUSE_MODIFIER = 0;
00405 const UINT AXIS_JOY_MODIFIER = 0x10;
00406
00407
00408 #include <boost/filesystem/path.hpp>
00409
00414 PVLE_EXPORT void saveBindings(const Bindings & k, const AxisBindings & a, const boost::filesystem::path & path, const char ** userSwitchesNames = NULL, UINT nbUserSwitchesNames = 0, UINT userBindingsFileVersion = 0);
00415
00417 PVLE_EXPORT bool loadBindings(Bindings & k, AxisBindings & a, const boost::filesystem::path & path, const char ** userSwitchesNames = NULL, UINT nbUserSwitchesNames = 0, UINT userBindingsFileVersion = 0);
00418
00419 PVLE_EXPORT void setDefaultBindings(Bindings & k, AxisBindings & a);
00420
00421
00422 #endif // INPUT_CONTROL_H