diff options
-rw-r--r-- | source/game/StarInput.hpp | 293 |
1 files changed, 147 insertions, 146 deletions
diff --git a/source/game/StarInput.hpp b/source/game/StarInput.hpp index 2b51c99..96d4cc6 100644 --- a/source/game/StarInput.hpp +++ b/source/game/StarInput.hpp @@ -8,184 +8,185 @@ namespace Star { - STAR_CLASS(Input); - STAR_EXCEPTION(InputException, StarException); +STAR_CLASS(Input); +STAR_EXCEPTION(InputException, StarException); - typedef Variant<Key, MouseButton, ControllerButton> InputVariant; +typedef Variant<Key, MouseButton, ControllerButton> InputVariant; - template <> - struct hash<InputVariant> { - size_t operator()(InputVariant const& v) const; +template <> +struct hash<InputVariant> { + size_t operator()(InputVariant const& v) const; +}; + +class Input { +public: + + static Json inputEventToJson(InputEvent const& event); + + struct KeyBind { + Key key = Key::Zero; + KeyMod mods = KeyMod::NoMod; + uint8_t priority = 0; + + inline bool operator<(KeyBind const& rhs) const { + return priority < rhs.priority; + } + + inline bool operator>(KeyBind const& rhs) const { + return priority > rhs.priority; + } }; - class Input { - public: + struct MouseBind { + MouseButton button = MouseButton::Left; + KeyMod mods = KeyMod::NoMod; + uint8_t priority = 0; + }; - static Json inputEventToJson(InputEvent const& event); + struct ControllerBind { + unsigned int controller = 0; + ControllerButton button = ControllerButton::Invalid; + }; - struct KeyBind { - Key key = Key::Zero; - KeyMod mods = KeyMod::NoMod; - uint8_t priority = 0; + typedef MVariant<KeyBind, MouseBind, ControllerBind> Bind; - inline bool operator<(KeyBind const& rhs) const { - return priority < rhs.priority; - } + static Bind bindFromJson(Json const& json); + static Json bindToJson(Bind const& bind); - inline bool operator>(KeyBind const& rhs) const { - return priority > rhs.priority; - } - }; - - struct MouseBind { - MouseButton button = MouseButton::Left; - KeyMod mods = KeyMod::NoMod; - uint8_t priority = 0; - }; - - struct ControllerBind { - unsigned int controller = 0; - ControllerButton button = ControllerButton::Invalid; - }; - - typedef MVariant<KeyBind, MouseBind, ControllerBind> Bind; - - static Bind bindFromJson(Json const& json); - static Json bindToJson(Bind const& bind); - - struct BindCategory; - - struct BindEntry { - // The internal ID of this entry. - String id; - // The user-facing name of this entry. - String name; - // The category this entry belongs to. - BindCategory const* category; - - // The default binds. - List<Bind> defaultBinds; - // The user-configured binds. - List<Bind> customBinds; - - BindEntry(String entryId, Json const& config, BindCategory const& parentCategory); - void updated(); - }; - - struct BindRef { - KeyMod mods; - uint8_t priority = 0; - BindEntry* entry = nullptr; // Invalidated on reload, careful! - - BindRef(BindEntry& bindEntry, KeyBind& keyBind); - BindRef(BindEntry& bindEntry, MouseBind& mouseBind); - BindRef(BindEntry& bindEntry); - }; - - struct BindCategory { - String id; - String name; - Json config; - - StableHashMap<String, BindEntry> entries; - - BindCategory(String categoryId, Json const& categoryConfig); - }; - - struct InputState { - unsigned presses = 0; - unsigned releases = 0; - bool pressed = false; - bool held = false; - bool released = false; - - // Calls the passed functions for each press and release. - template <typename PressFunction, typename ReleaseFunction> - void forEach(PressFunction&& pressed, ReleaseFunction&& released) { - for (size_t i = 0; i != releases; ++i) { - pressed(); - released(); - } - } + struct BindCategory; - inline void reset() { - presses = releases = 0; - pressed = released = false; + struct BindEntry { + // The internal ID of this entry. + String id; + // The user-facing name of this entry. + String name; + // The category this entry belongs to. + BindCategory const* category; + + // The default binds. + List<Bind> defaultBinds; + // The user-configured binds. + List<Bind> customBinds; + + BindEntry(String entryId, Json const& config, BindCategory const& parentCategory); + void updated(); + }; + + struct BindRef { + KeyMod mods; + uint8_t priority = 0; + BindEntry* entry = nullptr; // Invalidated on reload, careful! + + BindRef(BindEntry& bindEntry, KeyBind& keyBind); + BindRef(BindEntry& bindEntry, MouseBind& mouseBind); + BindRef(BindEntry& bindEntry); + }; + + struct BindCategory { + String id; + String name; + Json config; + + StableHashMap<String, BindEntry> entries; + + BindCategory(String categoryId, Json const& categoryConfig); + }; + + struct InputState { + unsigned presses = 0; + unsigned releases = 0; + bool pressed = false; + bool held = false; + bool released = false; + + // Calls the passed functions for each press and release. + template <typename PressFunction, typename ReleaseFunction> + void forEach(PressFunction&& pressed, ReleaseFunction&& released) { + for (size_t i = 0; i != releases; ++i) { + pressed(); + released(); } + } + + inline void reset() { + presses = releases = 0; + pressed = released = false; + } - inline void press() { pressed = ++presses; held = true; } - inline void release() { released = ++releases; held = false; } - }; + inline void press() { pressed = ++presses; held = true; } + inline void release() { released = ++releases; held = false; } + }; - // Get pointer to the singleton root instance, if it exists. Otherwise, - // returns nullptr. - static Input* singletonPtr(); + // Get pointer to the singleton Input instance, if it exists. Otherwise, + // returns nullptr. + static Input* singletonPtr(); - // Gets reference to GuiContext singleton, throws GuiContextException if root - // is not initialized. - static Input& singleton(); + // Gets reference to Input singleton, throws InputException if root + // is not initialized. + static Input& singleton(); - Input(); - ~Input(); + Input(); + ~Input(); - Input(Input const&) = delete; - Input& operator=(Input const&) = delete; + Input(Input const&) = delete; + Input& operator=(Input const&) = delete; - List<std::pair<InputEvent, bool>> const& inputEventsThisFrame() const; + List<std::pair<InputEvent, bool>> const& inputEventsThisFrame() const; - // Clears input state. Should be done at the very start or end of the client loop. - void reset(); + // Clears input state. Should be done at the very start or end of the client loop. + void reset(); - void update(); + void update(); - // Handles an input event. - bool handleInput(InputEvent const& input, bool gameProcessed); + // Handles an input event. + bool handleInput(InputEvent const& input, bool gameProcessed); - void rebuildMappings(); + void rebuildMappings(); - // Loads input categories and their binds from Assets. - void reload(); + // Loads input categories and their binds from Assets. + void reload(); - void setTextInputActive(bool active); + void setTextInputActive(bool active); - Maybe<unsigned> bindDown(String const& categoryId, String const& bindId); - bool bindHeld(String const& categoryId, String const& bindId); - Maybe<unsigned> bindUp (String const& categoryId, String const& bindId); + Maybe<unsigned> bindDown(String const& categoryId, String const& bindId); + bool bindHeld(String const& categoryId, String const& bindId); + Maybe<unsigned> bindUp (String const& categoryId, String const& bindId); - void resetBinds(String const& categoryId, String const& bindId); - void setBinds(String const& categoryId, String const& bindId, Json const& binds); - Json getDefaultBinds(String const& categoryId, String const& bindId); - Json getBinds(String const& categoryId, String const& bindId); - private: - List<BindEntry*> filterBindEntries(List<BindRef> const& binds, KeyMod mods) const; + void resetBinds(String const& categoryId, String const& bindId); + void setBinds(String const& categoryId, String const& bindId, Json const& binds); + Json getDefaultBinds(String const& categoryId, String const& bindId); + Json getBinds(String const& categoryId, String const& bindId); +private: + List<BindEntry*> filterBindEntries(List<BindRef> const& binds, KeyMod mods) const; - BindEntry* bindEntryPtr(String const& categoryId, String const& bindId); - BindEntry& bindEntry(String const& categoryId, String const& bindId); + BindEntry* bindEntryPtr(String const& categoryId, String const& bindId); + BindEntry& bindEntry(String const& categoryId, String const& bindId); - InputState* bindStatePtr(String const& categoryId, String const& bindId); - InputState* inputStatePtr(InputVariant key); + InputState* bindStatePtr(String const& categoryId, String const& bindId); + InputState* inputStatePtr(InputVariant key); - static Input* s_singleton; + static Input* s_singleton; - // Regenerated on reload. - StableHashMap<String, BindCategory> m_bindCategories; - // Contains raw pointers to bind entries in categories, so also regenerated on reload. - HashMap<InputVariant, List<BindRef>> m_bindMappings; + // Regenerated on reload. + StableHashMap<String, BindCategory> m_bindCategories; + // Contains raw pointers to bind entries in categories, so also regenerated on reload. + HashMap<InputVariant, List<BindRef>> m_bindMappings; - ListenerPtr m_rootReloadListener; + ListenerPtr m_rootReloadListener; - // Per-frame input event storage for Lua. - List<std::pair<InputEvent, bool>> m_inputEvents; + // Per-frame input event storage for Lua. + List<std::pair<InputEvent, bool>> m_inputEvents; - // Per-frame input state maps. - //Input states - HashMap<InputVariant, InputState> m_inputStates; - //Bind states - HashMap<BindEntry const*, InputState> m_bindStates; + // Per-frame input state maps. + //Input states + HashMap<InputVariant, InputState> m_inputStates; + //Bind states + HashMap<BindEntry const*, InputState> m_bindStates; + + KeyMod m_pressedMods; + bool m_textInputActive; +}; - KeyMod m_pressedMods; - bool m_textInputActive; - }; } #endif
\ No newline at end of file |