From ccaefd866cffcd02b495a351053eccb4e16ee52c Mon Sep 17 00:00:00 2001 From: Federico Cecchetto Date: Tue, 14 Sep 2021 02:48:11 +0200 Subject: [PATCH] More fixes --- src/client/component/ui_scripting.cpp | 1 - src/client/game/ui_scripting/lua/context.cpp | 132 +++++++++++++++--- src/client/game/ui_scripting/lua/engine.cpp | 18 ++- src/client/game/ui_scripting/lua/event.hpp | 2 +- .../game/ui_scripting/lua/event_handler.cpp | 6 +- .../game/ui_scripting/lua/event_handler.hpp | 2 +- .../game/ui_scripting/lua/scheduler.cpp | 2 +- src/client/game/ui_scripting/menu.cpp | 28 +--- src/client/game/ui_scripting/menu.hpp | 1 - 9 files changed, 138 insertions(+), 54 deletions(-) diff --git a/src/client/component/ui_scripting.cpp b/src/client/component/ui_scripting.cpp index 06ad4421..362b5501 100644 --- a/src/client/component/ui_scripting.cpp +++ b/src/client/component/ui_scripting.cpp @@ -28,7 +28,6 @@ namespace ui_scripting scheduler::loop([]() { - ShowCursor(true); ui_scripting::lua::engine::run_frame(); }, scheduler::pipeline::renderer); diff --git a/src/client/game/ui_scripting/lua/context.cpp b/src/client/game/ui_scripting/lua/context.cpp index 77d8971f..bc121d2d 100644 --- a/src/client/game/ui_scripting/lua/context.cpp +++ b/src/client/game/ui_scripting/lua/context.cpp @@ -206,18 +206,6 @@ namespace ui_scripting::lua element_type["settextoffset"] = &element::set_text_offset; element_type["setslice"] = &element::set_slice; - element_type["onnotify"] = [&handler](element& element, const std::string& event, - const event_callback& callback) - { - event_listener listener{}; - listener.callback = callback; - listener.element = &element; - listener.event = event; - listener.is_volatile = false; - - return handler.add_event_listener(std::move(listener)); - }; - element_type["getrect"] = [](const sol::this_state s, element& element) { auto rect = sol::table::create(s.lua_state()); @@ -371,6 +359,18 @@ namespace ui_scripting::lua } ); + element_type["onnotify"] = [&handler](element& element, const std::string& event, + const event_callback& callback) + { + event_listener listener{}; + listener.callback = callback; + listener.element = &element; + listener.event = event; + listener.is_volatile = false; + + return handler.add_event_listener(std::move(listener)); + }; + element_type["onnotifyonce"] = [&handler](element& element, const std::string& event, const event_callback& callback) { @@ -408,6 +408,53 @@ namespace ui_scripting::lua auto menu_type = state.new_usertype("menu"); + menu_type["onnotify"] = [&handler](menu& menu, const std::string& event, + const event_callback& callback) + { + event_listener listener{}; + listener.callback = callback; + listener.element = &menu; + listener.event = event; + listener.is_volatile = false; + + return handler.add_event_listener(std::move(listener)); + }; + + menu_type["onnotifyonce"] = [&handler](menu& menu, const std::string& event, + const event_callback& callback) + { + event_listener listener{}; + listener.callback = callback; + listener.element = &menu; + listener.event = event; + listener.is_volatile = true; + + return handler.add_event_listener(std::move(listener)); + }; + + menu_type["notify"] = [&handler](menu& element, const sol::this_state s, const std::string& _event, + sol::variadic_args va) + { + event event; + event.element = &element; + event.name = _event; + + for (auto arg : va) + { + if (arg.get_type() == sol::type::number) + { + event.arguments.push_back(arg.as()); + } + + if (arg.get_type() == sol::type::string) + { + event.arguments.push_back(arg.as()); + } + } + + handler.dispatch(event); + }; + menu_type["addchild"] = [](const sol::this_state s, menu& menu, element& element) { menu.add_child(&element); @@ -424,6 +471,31 @@ namespace ui_scripting::lua } ); + menu_type["isopen"] = [](menu& menu) + { + return menu.visible || (menu.type == menu_type::overlay && game::Menu_IsMenuOpenAndVisible(0, menu.overlay_menu.data())); + }; + + menu_type["open"] = [&handler](menu& menu) + { + event event; + event.element = &menu; + event.name = "close"; + handler.dispatch(event); + + menu.open(); + }; + + menu_type["close"] = [&handler](menu& menu) + { + event event; + event.element = &menu; + event.name = "close"; + handler.dispatch(event); + + menu.close(); + }; + struct game { }; @@ -459,24 +531,38 @@ namespace ui_scripting::lua return pos; }; - game_type["openmenu"] = [](const game&, const std::string& name) + game_type["openmenu"] = [&handler](const game&, const std::string& name) { if (menus.find(name) == menus.end()) { return; } - menus[name].open(); + const auto menu = &menus[name]; + + event event; + event.element = menu; + event.name = "close"; + handler.dispatch(event); + + menu->open(); }; - game_type["closemenu"] = [](const game&, const std::string& name) + game_type["closemenu"] = [&handler](const game&, const std::string& name) { if (menus.find(name) == menus.end()) { return; } - menus[name].close(); + const auto menu = &menus[name]; + + event event; + event.element = menu; + event.name = "close"; + handler.dispatch(event); + + menu->close(); }; game_type["onframe"] = [&scheduler](const game&, const sol::protected_function& callback) @@ -646,15 +732,15 @@ namespace ui_scripting::lua throw std::runtime_error("Not in game"); } - std::vector arguments{}; - - for (auto arg : va) + ::scheduler::once([s, name, args = std::vector(va.begin(), va.end())]() { - arguments.push_back(convert({s, arg})); - } + std::vector arguments{}; + + for (auto arg : args) + { + arguments.push_back(convert({s, arg})); + } - ::scheduler::once([s, va, name, arguments]() - { const auto player = scripting::call("getentbynum", {0}).as(); scripting::notify(player, name, arguments); }, ::scheduler::pipeline::server); diff --git a/src/client/game/ui_scripting/lua/engine.cpp b/src/client/game/ui_scripting/lua/engine.cpp index 6f7a882a..fe16b457 100644 --- a/src/client/game/ui_scripting/lua/engine.cpp +++ b/src/client/game/ui_scripting/lua/engine.cpp @@ -350,7 +350,14 @@ namespace ui_scripting::lua::engine return; } - menus[name].open(); + const auto menu = &menus[name]; + + event event; + event.element = menu; + event.name = "open"; + notify(event); + + menu->open(); } void close_menu(const std::string& name) @@ -360,7 +367,14 @@ namespace ui_scripting::lua::engine return; } - menus[name].close(); + const auto menu = &menus[name]; + + event event; + event.element = menu; + event.name = "close"; + notify(event); + + menu->close(); } void start() diff --git a/src/client/game/ui_scripting/lua/event.hpp b/src/client/game/ui_scripting/lua/event.hpp index 428a1eda..54f79b3a 100644 --- a/src/client/game/ui_scripting/lua/event.hpp +++ b/src/client/game/ui_scripting/lua/event.hpp @@ -7,7 +7,7 @@ namespace ui_scripting::lua struct event { std::string name; - element* element{}; + const void* element{}; std::vector> arguments; }; } diff --git a/src/client/game/ui_scripting/lua/event_handler.cpp b/src/client/game/ui_scripting/lua/event_handler.cpp index 73fbf336..727a92dd 100644 --- a/src/client/game/ui_scripting/lua/event_handler.cpp +++ b/src/client/game/ui_scripting/lua/event_handler.cpp @@ -35,7 +35,7 @@ namespace ui_scripting::lua for (auto i = tasks.begin(); i != tasks.end();) { - if (i->event != event.name || i->element->id != event.element->id) + if (i->event != event.name || i->element != event.element) { ++i; continue; @@ -87,7 +87,7 @@ namespace ui_scripting::lua { if (task.id == handle.id) { - task.endon_conditions.emplace_back(element->id, event); + task.endon_conditions.emplace_back((uint64_t)element, event); } } }; @@ -150,7 +150,7 @@ namespace ui_scripting::lua { for (auto& condition : task.endon_conditions) { - if (condition.first == event.element->id && condition.second == event.name) + if (condition.first == (uint64_t)event.element && condition.second == event.name) { task.is_deleted = true; break; diff --git a/src/client/game/ui_scripting/lua/event_handler.hpp b/src/client/game/ui_scripting/lua/event_handler.hpp index 88eb657a..a203a23a 100644 --- a/src/client/game/ui_scripting/lua/event_handler.hpp +++ b/src/client/game/ui_scripting/lua/event_handler.hpp @@ -16,7 +16,7 @@ namespace ui_scripting::lua { public: std::string event = {}; - element* element{}; + void* element{}; event_callback callback = {}; bool is_volatile = false; bool is_deleted = false; diff --git a/src/client/game/ui_scripting/lua/scheduler.cpp b/src/client/game/ui_scripting/lua/scheduler.cpp index 973adcec..8ecb930a 100644 --- a/src/client/game/ui_scripting/lua/scheduler.cpp +++ b/src/client/game/ui_scripting/lua/scheduler.cpp @@ -27,7 +27,7 @@ namespace ui_scripting::lua { for (auto& condition : task.endon_conditions) { - if (condition.first == event.element->id && condition.second == event.name) + if (condition.first == (uint64_t)event.element && condition.second == event.name) { task.is_deleted = true; break; diff --git a/src/client/game/ui_scripting/menu.cpp b/src/client/game/ui_scripting/menu.cpp index c535b49f..844c90ef 100644 --- a/src/client/game/ui_scripting/menu.cpp +++ b/src/client/game/ui_scripting/menu.cpp @@ -16,37 +16,23 @@ namespace ui_scripting void menu::open() { - if (this->visible) - { - return; - } - - this->cursor_was_enabled = *game::keyCatchers & 0x40; - if (!this->cursor_was_enabled && this->cursor) - { - *game::keyCatchers |= 0x40; - } - + *game::keyCatchers |= 0x40; this->visible = true; } void menu::close() { - if (!this->visible) - { - return; - } - - if (!this->cursor_was_enabled && this->cursor) - { - *game::keyCatchers &= ~0x40; - } - + *game::keyCatchers &= ~0x40; this->visible = false; } void menu::render() const { + if (this->cursor) + { + *game::keyCatchers |= 0x40; + } + for (auto& element : this->children) { element->render(); diff --git a/src/client/game/ui_scripting/menu.hpp b/src/client/game/ui_scripting/menu.hpp index 70121050..a726fdaa 100644 --- a/src/client/game/ui_scripting/menu.hpp +++ b/src/client/game/ui_scripting/menu.hpp @@ -17,7 +17,6 @@ namespace ui_scripting bool visible = false; bool cursor = false; - bool cursor_was_enabled = false; void open(); void close();