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_MAPPER_H
00031 #define INPUT_CONTROL_MAPPER_H
00032
00033 #include <PVLE/Export.h>
00034 #include <PVLE/Input/Control.h>
00035 #include <osg/Referenced>
00036 #include <osg/ref_ptr>
00037 #include <osgGA/GUIEventHandler>
00038 #include <vector>
00039 #include <map>
00040
00041
00042
00043
00044
00045 namespace osgViewer {
00046 class View;
00047 }
00048
00049
00050
00054 class ControlEventHandler : public virtual osg::Referenced {
00055 public:
00060
00061 virtual bool handleEvent(NetControlEvent * pEvent, TNL::EventConnection * pConnection) =0;
00062
00064 virtual void onAddedToControler(Controler * pControler) {}
00065 virtual void onRemovedFromControler(const Controler * pControler) {}
00066
00067 protected:
00070 virtual ~ControlEventHandler() {}
00071 };
00072
00073
00075 class ControlHandler : public ControlEventHandler {
00076 public:
00077 ControlHandler() : pControler(NULL) {}
00078
00079 virtual void onAddedToControler(Controler * pControler) { this->pControler = pControler; }
00080 virtual void onRemovedFromControler(const Controler * pControler) { this->pControler = NULL; }
00081
00085 virtual void handleFrame(double elapsed) =0;
00086
00087 protected:
00088 Controler * pControler;
00089 };
00090
00091
00092
00093
00105 class PVLE_EXPORT ControlMapper : public Controler, public osgGA::GUIEventHandler {
00106 public:
00107 ControlMapper()
00108 : pBindings(NULL), pAxisBindings(NULL), prevMouseX(0), prevMouseY(0), pointerMouse(false), sendUnmappedKeyboard(true), sendUnmappedButtons(false) {}
00109 ControlMapper(const Bindings * pBindings, const AxisBindings * pAxisBindings)
00110 : pBindings(pBindings), pAxisBindings(pAxisBindings), prevMouseX(0), prevMouseY(0), pointerMouse(false), sendUnmappedKeyboard(true), sendUnmappedButtons(false) {}
00111
00112 virtual bool handle(const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &aa);
00113
00119 typedef std::vector<osg::observer_ptr<ControlEventHandler> > THandlerList;
00122 const THandlerList & getHandlerList(UINT orderIndex) const;
00123
00125 enum EHandlerOrderIndexes {
00126 HANDLER_ORDER_CRITICAL,
00127 HANDLER_ORDER_MENU,
00128 HANDLER_ORDER_UNIT
00129 };
00130
00132 void addHandlerBack(ControlEventHandler * pHandler, UINT orderIndex) { getOrCreateHandlerList(orderIndex).push_back(pHandler); pHandler->onAddedToControler(this); }
00134
00135 void removeHandler(ControlEventHandler * pHandler);
00136 void removeHandler(ControlEventHandler * pHandler, UINT orderIndex);
00137 void removeHandlers(UINT orderIndex);
00138 void replaceHandler(ControlEventHandler * pOldHandler, ControlEventHandler * pNewHandler, UINT orderIndex);
00139 void clearHandlers() { handlerLists.clear(); }
00141
00142 const Bindings * getBindings() const { return pBindings; }
00143 void setBindings(const Bindings * pBindings) { this->pBindings = pBindings; }
00144 const AxisBindings * getAxisBindings() const { return pAxisBindings; }
00145 void setAxisBindings(const AxisBindings * pAxisBindings) { this->pAxisBindings = pAxisBindings; }
00146 void setBindings(const Bindings * pBindings, const AxisBindings * pAxisBindings) { this->pBindings = pBindings; this->pAxisBindings = pAxisBindings; }
00147
00150
00152 bool isPointerMouse() { return pointerMouse; }
00154 void enablePointerMouse() { pointerMouse = true; }
00156 void disablePointerMouse() { pointerMouse = false; }
00158 void computePickPos(osgViewer::View & view, osg::NodePath * pPickNodePath = NULL);
00160
00164 void setSendUnmappedKeyboard(bool state) { sendUnmappedKeyboard = state; }
00166 void setSendUnmappedButtons(bool state) { sendUnmappedButtons = state; }
00169 bool getSendUnmappedKeyboard() const { return sendUnmappedKeyboard; }
00172 bool getSendUnmappedButtons() const { return sendUnmappedButtons; }
00174
00177
00179 void onControlEventReceived(const ControlEvent & ce) {
00180 ASSERT(ce.getType() != ControlEvent::GFX_FRAME && ce.getType() != ControlEvent::GFX_RESIZE);
00181 if (ce.getType() == ControlEvent::AXIS) processHandlerListsOrAxisMove(ce.getTime(), ce.getAxisId(), ce.getAxisMovement());
00182 else {
00183 ASSERT(ce.getType() == ControlEvent::SWITCH_DOWN || ce.getType() == ControlEvent::SWITCH_UP);
00184 processHandlerListsOrDoSwitch(ce.getTime(), ce.getSwitchId(), ce.getUnmappedKey(), ce.getType() == ControlEvent::SWITCH_DOWN);
00185 }
00186 }
00188
00189 protected:
00190 typedef std::pair<UINT, THandlerList> THandlerListP;
00191 typedef std::map<UINT, THandlerList> THandlerListsList;
00192 THandlerListsList handlerLists;
00193
00194
00195 THandlerList & getHandlerList(UINT orderIndex);
00196 THandlerList & getOrCreateHandlerList(UINT orderIndex);
00197
00198
00199 typedef std::pair<THandlerList *, THandlerList::iterator> THandlerLocation;
00200 THandlerLocation findHandler(ControlEventHandler * pHandler);
00201
00203 bool processHandlerLists(const ControlEvent & ce);
00204
00205 void removeDeletedHandlers();
00206
00209 bool processHandlerListsOrAppend(const ControlEvent & ce) {
00210 if (!processHandlerLists(ce)) { vEvents.push_back(ce); return false; }
00211 return true;
00212 }
00214 inline void processHandlerListsOrDoSwitch(double curTime, ControlState::ESwitchId switchId, int unmappedKey, bool pushed) {
00215 if (!processHandlerListsOrAppend(ControlEvent(curTime, switchId, unmappedKey, pushed))) {
00216 if (pushed) state += switchId;
00217 else state -= switchId;
00218 }
00219 }
00221 inline void processHandlerListsOrAxisMove(double curTime, ControlState::EAxisId axisId, float movement) {
00222 #ifndef NO_AXIS_EVENT
00223 state.addToAxis(axisId, movement);
00224 #else
00225 if (!processHandlerListsOrAppend( ControlEvent(curTime, axisId, movement) )) {
00226 state.addToAxis(axisId, movement);
00227 }
00228 #endif
00229 }
00230
00231 bool pointerMouse;
00232 bool sendUnmappedKeyboard;
00233 bool sendUnmappedButtons;
00234 float computeMouseMovement(float mouseDiff, const AxisControl & ac);
00235 float prevMouseX, prevMouseY;
00236
00237 const Bindings * pBindings;
00238 const AxisBindings * pAxisBindings;
00239 };
00240
00241
00242 #endif // INPUT_CONTROL_MAPPER_H