From: <mk...@us...> - 2003-01-22 08:17:19
|
Update of /cvsroot/csp/APPLICATIONS/CSPFlightSim/Source In directory sc8-pr-cvs1:/tmp/cvs-serv2348/Source Modified Files: Tag: simdata HID.cpp Log Message: revised vhid layer Index: HID.cpp =================================================================== RCS file: /cvsroot/csp/APPLICATIONS/CSPFlightSim/Source/Attic/HID.cpp,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.cpp 4 Jan 2003 23:49:51 -0000 1.1.2.1 --- HID.cpp 22 Jan 2003 08:17:13 -0000 1.1.2.2 *************** *** 24,27 **** --- 24,59 ---- #include "HID.h" + #include "InputInterface.h" + #include "SDL_events.h" + + #include <iostream> + + + ///////////////////////////////////////////////////////////////////////////// + // class HID + + bool HID::OnEvent(SDL_Event &event) { + switch (event.type) { + case SDL_MOUSEMOTION: + return OnMouseMove(event.motion); + case SDL_MOUSEBUTTONUP: + case SDL_MOUSEBUTTONDOWN: + return OnMouseButton(event.button); + case SDL_KEYUP: + case SDL_KEYDOWN: + return OnKey(event.key); + case SDL_JOYAXISMOTION: + return OnJoystickAxisMotion(event.jaxis); + case SDL_JOYHATMOTION: + //return OnJoystickHatMotion(event.jhat); + return false; + case SDL_JOYBUTTONUP: + case SDL_JOYBUTTONDOWN: + return OnJoystickButton(event.jbutton); + default: + break; + } + return false; + } *************** *** 31,46 **** ! int EventMapping::getKeyID(SDL_keysym &key, EDir dir, int mode) const { ! int press = (dir == PRESS) ? (1 << 28) : 0; ! return (key.sym & 0xFFF) + ((key.mod & 0xFFF) << 12) + ((mode & 0x00F) << 24) + press; } ! int EventMapping::getJoystickButtonID(int device, int button, EDir dir, int mode) const { ! int press = (dir == PRESS) ? (1 << 28) : 0; ! return (device & 0xFFF) + ((button & 0xFFF) << 12) + ((mode & 0x00F) << 24) + press; } ! int EventMapping::getMouseButtonID(int device, int button, EDir dir, int kmod, int mode) const { ! int press = (dir == PRESS) ? (1 << 28) : 0; return (device & 0xF) + ((button & 0xFF) << 4) + ((kmod & 0xFFF) << 12) + ((mode & 0xF) << 24) + press; } --- 63,81 ---- ! EventMapping::EventMapping() { } ! int EventMapping::getKeyID(int device, SDL_keysym const &key, int state, int mode) const { ! int press = (state == SDL_PRESSED) ? (1 << 31) : 0; ! return (device & 0xF) + ((key.sym & 0x3FF) << 4) + ((key.mod & 0xFFF) << 14) + ((mode & 0x00F) << 26) + press; } ! int EventMapping::getJoystickButtonID(int device, int button, int state, int jmod, int mode) const { ! int press = (state == SDL_PRESSED) ? (1 << 31) : 0; ! return (device & 0xF) + ((button & 0xFFF) << 4) + ((jmod & 0xF) << 16) + ((mode & 0xF) << 20) + press; ! } ! ! int EventMapping::getMouseButtonID(int device, int button, int state, int kmod, int mode) const { ! int press = (state == SDL_PRESSED) ? (1 << 31) : 0; return (device & 0xF) + ((button & 0xFF) << 4) + ((kmod & 0xFFF) << 12) + ((mode & 0xF) << 24) + press; } *************** *** 51,56 **** ! EventMapping::Script const *EventMapping::getKeyScript(SDL_keysym &key, EDir dir, int mode) const { ! int id = getKeyID(key, dir, mode); EventScript kScript = m_KeyMap.find(id); if (kScript == m_KeyMap.end()) return NULL; --- 86,91 ---- ! EventMapping::Script const *EventMapping::getKeyScript(int device, SDL_keysym const &key, int state, int mode) const { ! int id = getKeyID(device, key, state, mode); EventScript kScript = m_KeyMap.find(id); if (kScript == m_KeyMap.end()) return NULL; *************** *** 58,63 **** } ! EventMapping::Script const *EventMapping::getJoystickButtonScript(int device, int button, EDir dir, int mode) const { ! int id = getJoystickButtonID(device, button, dir, mode); EventScript bScript = m_JoystickButtonMap.find(id); if (bScript == m_JoystickButtonMap.end()) return NULL; --- 93,98 ---- } ! EventMapping::Script const *EventMapping::getJoystickButtonScript(int device, int button, int state, int jmod, int mode) const { ! int id = getJoystickButtonID(device, button, state, jmod, mode); EventScript bScript = m_JoystickButtonMap.find(id); if (bScript == m_JoystickButtonMap.end()) return NULL; *************** *** 75,118 **** } ! void EventMapping::addKeyMap(SDL_keysym &key, EDir dir, int mode, Script const &s) { ! int id = getKeyID(key, dir, mode); m_KeyMap[id] = s; } ! void EventMapping::addJoystickButtonMap(int device, int button, EDir dir, int mode, Script const &s) { ! int id = getJoystickButtonID(device, button, dir, mode); m_JoystickButtonMap[id] = s; } ! void EventMapping::addMouseMotionMap(int device, int state, int kmod, int mode, int map_to) { int id = getMouseMotionID(device, state, kmod, mode); ! m_MouseMotionMap[id] = map_to; } // convenience method for testing purposes only ! void EventMapping::addKeyMap(SDLKey vkey, SDLMod kmod, EDir dir, int mode, int action, double time) { SDL_keysym key; key.sym = vkey; key.mod = kmod; Script s; ! Action a; ! a.id = action; ! a.time = time; ! s.push_back(a); ! addKeyMap(key, dir, mode, s); } // convenience method for testing purposes only ! void EventMapping::addJoystickButtonMap(int device, int button, EDir dir, int mode, int action, double time) { Script s; ! Action a; ! a.id = action; ! a.time = time; ! s.push_back(a); ! addJoystickButtonMap(device, button, dir, mode, s); } ! EventMapping::Script const *EventMapping::getMouseButtonScript(int device, int button, EDir dir, int kmod, int mode) const { ! int id = getMouseButtonID(device, button, dir, kmod, mode); EventScript mScript = m_MouseButtonMap.find(id); if (mScript == m_MouseButtonMap.end()) return NULL; --- 110,152 ---- } ! void EventMapping::addKeyMap(int device, SDL_keysym &key, int state, int mode, Script const &s) { ! int id = getKeyID(device, key, state, mode); m_KeyMap[id] = s; } ! void EventMapping::addJoystickButtonMap(int device, int button, int state, int jmod, int mode, Script const &s) { ! int id = getJoystickButtonID(device, button, state, jmod, mode); m_JoystickButtonMap[id] = s; } ! void EventMapping::addMouseMotionMap(int device, int state, int kmod, int mode, Motion const &motion) { int id = getMouseMotionID(device, state, kmod, mode); ! m_MouseMotionMap[id] = motion; ! } ! ! void EventMapping::addMouseButtonMap(int device, int button, int state, int kmod, int mode, Script const &s) { ! int id = getMouseButtonID(device, button, state, kmod, mode); ! m_MouseButtonMap[id] = s; } // convenience method for testing purposes only ! void EventMapping::addKeyMap(int device, SDLKey vkey, SDLMod kmod, int state, int mode, Action const &action) { SDL_keysym key; key.sym = vkey; key.mod = kmod; Script s; ! s.push_back(action); ! addKeyMap(device, key, state, mode, s); } // convenience method for testing purposes only ! void EventMapping::addJoystickButtonMap(int device, int button, int state, int jmod, int mode, Action const &action) { Script s; ! s.push_back(action); ! addJoystickButtonMap(device, button, state, jmod, mode, s); } ! EventMapping::Script const *EventMapping::getMouseButtonScript(int device, int button, int state, int kmod, int mode) const { ! int id = getMouseButtonID(device, button, state, kmod, mode); EventScript mScript = m_MouseButtonMap.find(id); if (mScript == m_MouseButtonMap.end()) return NULL; *************** *** 120,128 **** } ! int EventMapping::getMouseMotion(int device, int state, int kmod, int mode) const { int id = getMouseMotionID(device, state, kmod, mode); motion_map::const_iterator map = m_MouseMotionMap.find(id); ! if (map == m_MouseMotionMap.end()) return -1; ! return map->second; } --- 154,163 ---- } ! //int EventMapping::getMouseMotion(int device, int state, int kmod, int mode) const { ! EventMapping::Motion const *EventMapping::getMouseMotion(int device, int state, int kmod, int mode) const { int id = getMouseMotionID(device, state, kmod, mode); motion_map::const_iterator map = m_MouseMotionMap.find(id); ! if (map == m_MouseMotionMap.end()) return NULL; ! return &(map->second); } *************** *** 136,141 **** m_Map = 0; m_VirtualMode = 0; m_ActiveScript = 0; ! m_LastMouseMoveID = -1; } --- 171,180 ---- m_Map = 0; m_VirtualMode = 0; + m_JoystickModifier = 0; m_ActiveScript = 0; ! m_LastMouseMoveID = ""; ! m_Actions.clear(); ! m_Motions.clear(); ! m_Axes.clear(); } *************** *** 146,162 **** } ! void VirtualHID::bindObject(SimObject *object) { m_Object = object; } ! ! bool VirtualHID::OnKeyDown(SDL_keysym &key) { ! if (!m_Object || !m_Map) return false; ! EventMapping::Script const *s = m_Map->getKeyScript(key, EventMapping::PRESS, 0); ! if (s == NULL) return false; ! setScript(s); ! return true; ! } ! bool VirtualHID::OnKeyUp(SDL_keysym &key) { ! if (!m_Object || !m_Map) return false; ! EventMapping::Script const *s = m_Map->getKeyScript(key, EventMapping::RELEASE, 0); if (s == NULL) return false; setScript(s); --- 185,195 ---- } ! void VirtualHID::bindObject(InputInterface *object) { m_Object = object; } ! bool VirtualHID::OnKey(SDL_KeyboardEvent const &event) { ! if (!m_Object) return false; ! if (m_Object->OnKey(event)) return true; ! if (!m_Map) return false; ! EventMapping::Script const *s = m_Map->getKeyScript(event.which, event.keysym, event.state, 0); if (s == NULL) return false; setScript(s); *************** *** 164,170 **** } ! bool VirtualHID::OnJoystickButtonDown(int joynum, int butnum) { ! if (!m_Object || !m_Map) return false; ! EventMapping::Script const *s = m_Map->getJoystickButtonScript(joynum, butnum, EventMapping::PRESS, m_VirtualMode); if (s == NULL) return false; setScript(s); --- 197,206 ---- } ! bool VirtualHID::OnJoystickButton(SDL_JoyButtonEvent const &event) { ! if (!m_Object) return false; ! if (m_Object->OnJoystickButton(event)) return true; ! if (!m_Map) return false; ! EventMapping::Script const *s = ! m_Map->getJoystickButtonScript(event.which, event.button, event.state, m_JoystickModifier, m_VirtualMode); if (s == NULL) return false; setScript(s); *************** *** 172,204 **** } ! bool VirtualHID::OnJoystickButtonUp(int joynum, int butnum) { ! if (!m_Object || !m_Map) return false; ! EventMapping::Script const *s = m_Map->getJoystickButtonScript(joynum, butnum, EventMapping::RELEASE, m_VirtualMode); ! if (s == NULL) return false; ! setScript(s); return true; } - void VirtualHID::OnJoystickAxisMotion(int joynum, int axis, int val) { - if (!m_Object || !m_Map) return; - EventMapping::Axis const *Axis = m_Map->getJoystickAxis(joynum, axis); - int id = Axis->id; - if (id < 0 || id > m_Axes.size()) return; - AxisAdapter adapter = m_Axes[id]; - if (adapter) adapter(m_Object, val); - } - void VirtualHID::setVirtualMode(int mode) { m_VirtualMode = mode; } bool VirtualHID::OnMouseMove(SDL_MouseMotionEvent const &event) { ! if (!m_Object || !m_Map) return false; int kmod = SDL_GetModState(); ! int id = m_Map->getMouseMotion(event.which, event.state, kmod, m_VirtualMode); if (id != m_LastMouseMoveID) { m_LastMouseMoveID = id; m_LastMouseMoveAdapter = NULL; ! MotionMap::const_iterator map = m_Motions.find(id); if (map == m_Motions.end()) return false; m_LastMouseMoveAdapter = map->second; --- 208,242 ---- } ! bool VirtualHID::OnJoystickAxisMotion(SDL_JoyAxisEvent const &event) { ! if (!m_Object) return false; ! if (m_Object->OnJoystickAxisMotion(event)) return true; ! if (!m_Map) return false; ! EventMapping::Axis const *a = m_Map->getJoystickAxis(event.which, event.axis); ! if (a == NULL || a->id == "") return false; ! AxisAdapter adapter = m_Axes[a->id.c_str()]; ! if (adapter) adapter(m_Object, event.value * (1.0/32768.0)); return true; } void VirtualHID::setVirtualMode(int mode) { m_VirtualMode = mode; } + void VirtualHID::setJoystickModifier(int jmod) { + m_JoystickModifier = jmod; + } + bool VirtualHID::OnMouseMove(SDL_MouseMotionEvent const &event) { ! if (!m_Object) return false; ! if (m_Object->OnMouseMove(event)) return true; ! if (!m_Map) return false; int kmod = SDL_GetModState(); ! EventMapping::Motion const *m = m_Map->getMouseMotion(event.which, event.state, kmod, m_VirtualMode); ! if (m == NULL || m->id == "") return false; ! std::string const &id = m->id; if (id != m_LastMouseMoveID) { m_LastMouseMoveID = id; m_LastMouseMoveAdapter = NULL; ! MotionMap::const_iterator map = m_Motions.find(id.c_str()); if (map == m_Motions.end()) return false; m_LastMouseMoveAdapter = map->second; *************** *** 210,226 **** } ! bool VirtualHID::OnMouseButtonDown(SDL_MouseButtonEvent const &event) { ! if (!m_Object || !m_Map) return false; ! int kmod = SDL_GetModState(); ! EventMapping::Script const *s = m_Map->getMouseButtonScript(event.which, event.button, EventMapping::PRESS, kmod, m_VirtualMode); ! if (s == NULL) return false; ! setScript(s, event.x, event.y); ! return true; ! } ! ! bool VirtualHID::OnMouseButtonUp(SDL_MouseButtonEvent const &event) { ! if (!m_Object || !m_Map) return false; int kmod = SDL_GetModState(); ! EventMapping::Script const *s = m_Map->getMouseButtonScript(event.which, event.button, EventMapping::RELEASE, kmod, m_VirtualMode); if (s == NULL) return false; setScript(s, event.x, event.y); --- 248,257 ---- } ! bool VirtualHID::OnMouseButton(SDL_MouseButtonEvent const &event) { ! if (!m_Object) return false; ! if (m_Object->OnMouseButton(event)) return true; ! if (!m_Map) return false; int kmod = SDL_GetModState(); ! EventMapping::Script const *s = m_Map->getMouseButtonScript(event.which, event.button, event.state, kmod, m_VirtualMode); if (s == NULL) return false; setScript(s, event.x, event.y); *************** *** 230,249 **** void VirtualHID::OnUpdate(double dt) { if (!m_ActiveScript) return; m_ScriptTime += dt; while (m_ScriptAction->time <= m_ScriptTime) { ! int id = m_ScriptAction->id; ! if (id < -15) { ! // nop ! } else ! if (id < 0) { ! setVirtualMode(-id); ! } else { ! // FIXME: don't use [] ! ActionAdapter adapter = m_Actions[id]; ! if (adapter) adapter(m_Object, m_MouseEventX, m_MouseEventY); } ! if (++m_ScriptAction == m_ActiveScript->end()) { ! m_ActiveScript = 0; break; } } --- 261,295 ---- void VirtualHID::OnUpdate(double dt) { if (!m_ActiveScript) return; + // wait out the delay until the next action should start + // strings of actions with delay = 0 will be executed in + // a single call of OnUpdate() m_ScriptTime += dt; while (m_ScriptAction->time <= m_ScriptTime) { ! const char *id = m_ScriptAction->id.c_str(); ! // action time represents a relative delay instead of absolute ! m_ScriptTime = 0.0; ! if (m_ScriptAction->mode >= 0) { ! setVirtualMode(m_ScriptAction->mode); } ! if (m_ScriptAction->jmod >= 0) { ! setJoystickModifier(m_ScriptAction->jmod); ! } ! // execute the action ! ActionAdapter adapter = m_Actions[id]; ! if (adapter) { ! adapter(m_Object, m_MouseEventX, m_MouseEventY); ! } else { ! std::cout << "Missing HID interface for command '" << id << "'\n"; ! } ! // advance, end, or loop the script ! int loop = m_ScriptAction->loop; ! if (loop >= 0 && (unsigned)loop < m_ActiveScript->size()) { ! m_ScriptAction = m_ActiveScript->begin() + loop; break; + } else { + if (++m_ScriptAction == m_ActiveScript->end()) { + m_ActiveScript = 0; + break; + } } } *************** *** 257,281 **** m_ScriptTime = 0.0; OnUpdate(0.0); - } - - - //////////////////////////////////////////////////////////////////// - // class AircraftInterface: public VirtualHID - - AircraftInterface::AircraftInterface(): VirtualHID() { - // temparory for testing... - bindObject(new AircraftObject()); - bindEvents(); - } - - void AircraftInterface::bindEvents() { - m_Actions.clear(); - m_Axes.clear(); - m_Axes.resize(16, NULL); - BIND_ACTION(AircraftObject, RETRACT_GEAR, RetractGear); - BIND_ACTION(AircraftObject, EXTEND_GEAR, ExtendGear); - BIND_ACTION(AircraftObject, TOGGLE_GEAR, ToggleGear); - BIND_MOTION(AircraftObject, PAN_VIEW, PanView); - BIND_AXIS(AircraftObject, THROTTLE, Throttle); } --- 303,306 ---- |