From: <mk...@us...> - 2003-01-22 08:17:19
|
Update of /cvsroot/csp/APPLICATIONS/CSPFlightSim/Include In directory sc8-pr-cvs1:/tmp/cvs-serv2348/Include Modified Files: Tag: simdata HID.h Added Files: Tag: simdata InputInterface.h Log Message: revised vhid layer --- NEW FILE: InputInterface.h --- // Combat Simulator Project - FlightSim Demo // Copyright (C) 2002 The Combat Simulator Project // http://csp.sourceforge.net // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation; either version 2 // of the License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. /** * @file InputInterface.h * **/ #ifndef __INPUTINTERFACE_H__ #define __INPUTINTERFACE_H__ #include "SDL_events.h" class VirtualHID; class InputInterface { public: virtual ~InputInterface() {} virtual bool OnKey(SDL_KeyboardEvent const &) { return false; } virtual bool OnJoystickButton(SDL_JoyButtonEvent const &) { return false; } virtual bool OnJoystickAxisMotion(SDL_JoyAxisEvent const &) { return false; } virtual bool OnMouseMove(SDL_MouseMotionEvent const &) { return false; } virtual bool OnMouseButton(SDL_MouseButtonEvent const &) { return false; } }; #define ACTION_INTERFACE(obj_class, method) \ virtual void method(); \ static void on##method(InputInterface* obj, int, int) {\ obj_class* target = dynamic_cast<obj_class*>(obj);\ assert(target); \ target->method();\ } #define CLICK_INTERFACE(obj_class, method) \ virtual void method(int, int); \ static void on##method(InputInterface* obj, int x, int y) { \ obj_class* target = dynamic_cast<obj_class*>(obj);\ assert(target); \ target->method(x, y);\ } #define MOTION_INTERFACE(obj_class, method) \ virtual void method(int, int, int, int); \ static void on##method(InputInterface* obj, int x, int y, int dx, int dy) {\ obj_class* target = dynamic_cast<obj_class*>(obj);\ assert(target); \ target->method(x, y, dx, dy);\ } #define AXIS_INTERFACE(obj_class, method) \ virtual void method(double); \ static void on##method(InputInterface* obj, double value) {\ obj_class* target = dynamic_cast<obj_class*>(obj);\ assert(target); \ target->method(value);\ } #endif // __INPUTINTERFACE_H__ Index: HID.h =================================================================== RCS file: /cvsroot/csp/APPLICATIONS/CSPFlightSim/Include/Attic/HID.h,v retrieving revision 1.1.2.1 retrieving revision 1.1.2.2 diff -C2 -d -r1.1.2.1 -r1.1.2.2 *** HID.h 4 Jan 2003 23:49:50 -0000 1.1.2.1 --- HID.h 22 Jan 2003 08:17:13 -0000 1.1.2.2 *************** *** 23,26 **** --- 23,41 ---- **/ + /************************************************************** + * TODO jan 2003 + * + * add joystick hat support + * map loading / parsing + * map caching + * automatic map swapping based on player object type + * rotary banding + * axis calibration, detents, and non-linear corrections + * finish the definition of the map scripting language + * complete the python tool to convert map scripts to binary + * add xml support to the python script tool + * + */ + #ifndef __HID_H__ #define __HID_H__ *************** *** 30,37 **** #include "SDL_keyboard.h" #include <vector> - #include <hash_map> ! #include "SimObject.h" --- 45,55 ---- #include "SDL_keyboard.h" + #include <string> #include <vector> ! #include <SimData/hash_map.h> ! ! ! class InputInterface; *************** *** 45,56 **** public: ! virtual bool OnKeyDown(SDL_keysym &key) = 0; ! virtual bool OnKeyUp(SDL_keysym &key) = 0; ! virtual bool OnJoystickButtonDown(int joynum, int butnum) = 0; ! virtual bool OnJoystickButtonUp(int joynum, int butnum) = 0; ! virtual void OnJoystickAxisMotion(int joynum, int axis, int val) = 0; virtual bool OnMouseMove(SDL_MouseMotionEvent const &) = 0; ! virtual bool OnMouseButtonDown(SDL_MouseButtonEvent const &) = 0; ! virtual bool OnMouseButtonUp(SDL_MouseButtonEvent const &) = 0; /* virtual void OnJoystickHatMotion(int joynum, int hat, int val) = 0; --- 63,74 ---- public: ! HID() {} ! virtual ~HID() {} ! ! virtual bool OnKey(SDL_KeyboardEvent const &) = 0; ! virtual bool OnJoystickButton(SDL_JoyButtonEvent const &) = 0; ! virtual bool OnJoystickAxisMotion(SDL_JoyAxisEvent const &) = 0; virtual bool OnMouseMove(SDL_MouseMotionEvent const &) = 0; ! virtual bool OnMouseButton(SDL_MouseButtonEvent const &) = 0; /* virtual void OnJoystickHatMotion(int joynum, int hat, int val) = 0; *************** *** 58,62 **** virtual void OnUpdate(double) = 0; ! virtual void bindObject(SimObject *) = 0; }; --- 76,82 ---- virtual void OnUpdate(double) = 0; ! virtual void bindObject(InputInterface *) = 0; ! ! virtual bool OnEvent(SDL_Event &event); }; *************** *** 65,69 **** { public: ! typedef enum { RELEASE, PRESS } EDir; struct LinearCorrection { --- 85,89 ---- { public: ! EventMapping(); struct LinearCorrection { *************** *** 74,111 **** struct Axis { ! Axis(int id_ = -1): id(id_) {} ! int id; }; struct Action { double time; ! int id; }; typedef std::vector<Action> Script; ! typedef std::hash_map<int, Script> script_map; typedef script_map::const_iterator EventScript; - typedef std::hash_map<int, int> motion_map; ! int getKeyID(SDL_keysym &key, EDir dir, int mode) const; ! int getJoystickButtonID(int device, int button, EDir dir, int mode) const; ! int getMouseButtonID(int device, int button, EDir dir, int kmode, int mode) const; ! int getMouseMotionID(int device, int state, int kmode, int mode) const; ! Script const *getKeyScript(SDL_keysym &key, EDir dir, int mode) const; ! Script const *getJoystickButtonScript(int device, int button, EDir dir, int mode) const; ! Script const *getMouseButtonScript(int device, int button, EDir dir, int kmode, int mode) const; Axis const *getJoystickAxis(int device, int axis) const; ! int getMouseMotion(int device, int state, int kmode, int mode) const; ! void addKeyMap(SDL_keysym &key, EDir dir, int mode, Script const &s); ! void addJoystickButtonMap(int device, int button, EDir dir, int mode, Script const &s); void addJoystickAxisMap(int device, int axis, Axis const &a); ! void addMouseMotionMap(int device, int state, int kmod, int mode, int map_to); // convenience method for testing purposes only ! void addKeyMap(SDLKey vkey, SDLMod kmode, EDir dir, int mode, int action, double time); // convenience method for testing purposes only ! void addJoystickButtonMap(int device, int button, EDir dir, int mode, int action, double time); private: --- 94,146 ---- struct Axis { ! Axis(std::string const &id_ = ""): id(id_) {} ! std::string id; }; struct Action { + Action(std::string const &id_ = "", + double time_ = 0.0, + int jmod_ = -1, + int mode_ = -1, + int loop_ = -1): + id(id_), mode(mode_), jmod(jmod_), loop(loop_), time(time_) {} + std::string id; + int mode; + int jmod; + int loop; double time; ! }; ! ! struct Motion { ! Motion(std::string const &id_ = ""): id(id_) {} ! std::string id; }; typedef std::vector<Action> Script; ! typedef HASH_MAP<int, Script> script_map; ! typedef HASH_MAP<int, Motion> motion_map; typedef script_map::const_iterator EventScript; ! int getKeyID(int device, SDL_keysym const &key, int state, int mode) const; ! int getJoystickButtonID(int device, int button, int state, int jmod, int mode) const; ! int getMouseButtonID(int device, int button, int state, int kmod, int mode) const; ! int getMouseMotionID(int device, int state, int kmod, int mode) const; ! Script const *getKeyScript(int device, SDL_keysym const &key, int state, int mode) const; ! Script const *getJoystickButtonScript(int device, int button, int state, int jmod, int mode) const; ! Script const *getMouseButtonScript(int device, int button, int state, int kmod, int mode) const; Axis const *getJoystickAxis(int device, int axis) const; ! Motion const *getMouseMotion(int device, int state, int kmod, int mode) const; ! void addKeyMap(int device, SDL_keysym &key, int state, int mode, Script const &s); ! void addJoystickButtonMap(int device, int button, int state, int jmod, int mode, Script const &s); void addJoystickAxisMap(int device, int axis, Axis const &a); ! void addMouseMotionMap(int device, int state, int kmod, int mode, Motion const &motion); ! void addMouseButtonMap(int device, int button, int state, int kmod, int mode, Script const &s); // convenience method for testing purposes only ! void addKeyMap(int device, SDLKey vkey, SDLMod kmode, int state, int mode, Action const &action); // convenience method for testing purposes only ! void addJoystickButtonMap(int device, int button, int state, int jmod, int mode, Action const &action); private: *************** *** 124,130 **** { public: ! typedef void (*ActionAdapter)(SimObject *, int, int); ! typedef void (*MotionAdapter)(SimObject *, int, int, int, int); ! typedef void (*AxisAdapter)(SimObject *, int); VirtualHID(); --- 159,165 ---- { public: ! typedef void (*ActionAdapter)(InputInterface *, int, int); ! typedef void (*MotionAdapter)(InputInterface *, int, int, int, int); ! typedef void (*AxisAdapter)(InputInterface *, double); VirtualHID(); *************** *** 132,145 **** virtual void setMapping(EventMapping const & map); ! virtual void bindObject(SimObject *object); ! virtual bool OnKeyDown(SDL_keysym &key); ! virtual bool OnKeyUp(SDL_keysym &key); ! virtual bool OnJoystickButtonDown(int joynum, int butnum); ! virtual bool OnJoystickButtonUp(int joynum, int butnum); ! virtual void OnJoystickAxisMotion(int joynum, int axis, int val); virtual bool OnMouseMove(SDL_MouseMotionEvent const &event); ! virtual bool OnMouseButtonDown(SDL_MouseButtonEvent const &event); ! virtual bool OnMouseButtonUp(SDL_MouseButtonEvent const &event); virtual void OnUpdate(double dt); --- 167,177 ---- virtual void setMapping(EventMapping const & map); ! virtual void bindObject(InputInterface *object); ! virtual bool OnKey(SDL_KeyboardEvent const &event); ! virtual bool OnJoystickButton(SDL_JoyButtonEvent const &event); ! virtual bool OnJoystickAxisMotion(SDL_JoyAxisEvent const &event); virtual bool OnMouseMove(SDL_MouseMotionEvent const &event); ! virtual bool OnMouseButton(SDL_MouseButtonEvent const &event); virtual void OnUpdate(double dt); *************** *** 147,156 **** protected: virtual void setScript(EventMapping::Script const *s, int x = -1, int y = -1); - virtual void bindEvents() = 0; virtual void setVirtualMode(int mode); ! typedef std::hash_map<int, ActionAdapter> ActionMap; ! typedef std::hash_map<int, MotionAdapter> MotionMap; ! typedef std::vector<AxisAdapter> AxisMap; ActionMap m_Actions; --- 179,195 ---- protected: virtual void setScript(EventMapping::Script const *s, int x = -1, int y = -1); virtual void setVirtualMode(int mode); + virtual void setJoystickModifier(int jmod); + + struct eqstr { + bool operator()(const char* s1, const char* s2) const { + return strcmp(s1, s2) == 0; + } + }; ! // too bad partially specialized template typedef's don't exist yet... ! typedef HASH_MAP<const char *, ActionAdapter, HASH<const char *>, eqstr> ActionMap; ! typedef HASH_MAP<const char *, MotionAdapter, HASH<const char *>, eqstr> MotionMap; ! typedef HASH_MAP<const char *, AxisAdapter, HASH<const char *>, eqstr> AxisMap; ActionMap m_Actions; *************** *** 159,165 **** int m_VirtualMode; const EventMapping *m_Map; ! SimObject *m_Object; EventMapping::Script const *m_ActiveScript; --- 198,205 ---- int m_VirtualMode; + bool m_JoystickModifier; const EventMapping *m_Map; ! InputInterface *m_Object; EventMapping::Script const *m_ActiveScript; *************** *** 167,171 **** double m_ScriptTime; ! int m_LastMouseMoveID; MotionAdapter m_LastMouseMoveAdapter; int m_MouseEventX; --- 207,211 ---- double m_ScriptTime; ! std::string m_LastMouseMoveID; MotionAdapter m_LastMouseMoveAdapter; int m_MouseEventX; *************** *** 174,223 **** - #define ACTION_INTERFACE(obj_class, method) static void on##method(SimObject* obj, int, int) {\ - obj_class* target = dynamic_cast<obj_class*>(obj);\ - assert(target); \ - target->method();\ - } - - #define CLICK_INTERFACE(obj_class, method) static void on##method(SimObject* obj, int x, int y) {\ - obj_class* target = dynamic_cast<obj_class*>(obj);\ - assert(target); \ - target->method(x, y);\ - } - - #define MOTION_INTERFACE(obj_class, method) static void on##method(SimObject* obj, int x, int y, int dx, int dy) {\ - obj_class* target = dynamic_cast<obj_class*>(obj);\ - assert(target); \ - target->method(x, y, dx, dy);\ - } - - #define AXIS_INTERFACE(obj_class, method) static void on##method(SimObject* obj, int value) {\ - obj_class* target = dynamic_cast<obj_class*>(obj);\ - assert(target); \ - target->set##method(value);\ - } - - class AircraftObject: public SimObject - { - public: - virtual void RetractGear() { cout << "retracting gear\n"; } - virtual void ExtendGear() { cout << "extending gear\n"; } - virtual void ToggleGear() { cout << "toggling gear\n"; } - virtual void setThrottle(double x) { cout << "throttle = " << x << endl; } - virtual void PanView(int x, int y, int dx, int dy) { - cout << "mouse " << x << ", " << y << " (" << dx << ", " << dy << ")\n"; - } - virtual void dump() {} - virtual void OnUpdate(double) {} - virtual void initialize() {} - virtual unsigned int OnRender() {} - - ACTION_INTERFACE(AircraftObject, RetractGear); - ACTION_INTERFACE(AircraftObject, ExtendGear); - ACTION_INTERFACE(AircraftObject, ToggleGear); - MOTION_INTERFACE(AircraftObject, PanView); - AXIS_INTERFACE(AircraftObject, Throttle); - - }; --- 214,217 ---- *************** *** 231,248 **** m_Motions[id] = &obj_class::on##method; - - class AircraftInterface: public VirtualHID - { - public: - enum { RETRACT_GEAR, EXTEND_GEAR, TOGGLE_GEAR }; - enum { THROTTLE, AILERONS, ELEVATORS, RUDDER }; - enum { PAN_VIEW }; - - AircraftInterface(); - - protected: - virtual void bindEvents(); - }; - /* --- 225,228 ---- |