#ifndef COMPONENTS_LUA_INPUTACTIONS #define COMPONENTS_LUA_INPUTACTIONS #include #include #include #include #include #include #include #include namespace LuaUtil::InputAction { enum class Type { Boolean, Number, Range, }; struct Info { std::string mKey; Type mType; std::string mL10n; std::string mName; std::string mDescription; sol::main_object mDefaultValue; }; class MultiTree { public: using Node = size_t; Node insert(); bool multiEdge(Node target, const std::vector& source); size_t size() const { return mParents.size(); } template // Function = void(Node) void traverse(Function callback) const; void clear() { mParents.clear(); mChildren.clear(); } private: std::vector> mParents; std::vector> mChildren; bool validateTree() const; }; class Registry { public: using ConstIterator = std::vector::const_iterator; void insert(const Info& info); size_t size() const { return mKeys.size(); } std::optional firstKey() const { return mKeys.empty() ? std::nullopt : std::optional(mKeys[0]); } std::optional nextKey(std::string_view key) const; std::optional operator[](std::string_view actionKey); bool bind( std::string_view key, const LuaUtil::Callback& callback, const std::vector& dependencies); sol::object valueOfType(std::string_view key, Type type); void update(double dt); void registerHandler(std::string_view key, const LuaUtil::Callback& handler) { mHandlers[safeIdByKey(key)].push_back(handler); } void clear() { mKeys.clear(); mIds.clear(); mInfo.clear(); mHandlers.clear(); mBindings.clear(); mValues.clear(); mBindingTree.clear(); } private: using Id = MultiTree::Node; Id safeIdByKey(std::string_view key); struct Binding { LuaUtil::Callback mCallback; std::vector mDependencies; }; std::vector mKeys; std::unordered_map> mIds; std::vector mInfo; std::vector> mHandlers; std::vector> mBindings; std::vector mValues; MultiTree mBindingTree; }; } namespace LuaUtil::InputTrigger { struct Info { std::string mKey; std::string mL10n; std::string mName; std::string mDescription; }; class Registry { public: std::optional firstKey() const { return mIds.empty() ? std::nullopt : std::optional(mIds.begin()->first); } std::optional nextKey(std::string_view key) const { auto it = mIds.find(key); if (it == mIds.end() || ++it == mIds.end()) return std::nullopt; return it->first; } std::optional operator[](std::string_view key); void insert(const Info& info); void registerHandler(std::string_view key, const LuaUtil::Callback& callback); void activate(std::string_view key); void clear() { mInfo.clear(); mHandlers.clear(); mIds.clear(); } private: using Id = size_t; Id safeIdByKey(std::string_view key); std::unordered_map> mIds; std::vector mInfo; std::vector> mHandlers; }; } #endif // COMPONENTS_LUA_INPUTACTIONS