More fixes

This commit is contained in:
Federico Cecchetto 2021-09-14 02:48:11 +02:00
parent 192df6edc4
commit ccaefd866c
9 changed files with 138 additions and 54 deletions

View File

@ -28,7 +28,6 @@ namespace ui_scripting
scheduler::loop([]() scheduler::loop([]()
{ {
ShowCursor(true);
ui_scripting::lua::engine::run_frame(); ui_scripting::lua::engine::run_frame();
}, scheduler::pipeline::renderer); }, scheduler::pipeline::renderer);

View File

@ -206,18 +206,6 @@ namespace ui_scripting::lua
element_type["settextoffset"] = &element::set_text_offset; element_type["settextoffset"] = &element::set_text_offset;
element_type["setslice"] = &element::set_slice; 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) element_type["getrect"] = [](const sol::this_state s, element& element)
{ {
auto rect = sol::table::create(s.lua_state()); 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, element_type["onnotifyonce"] = [&handler](element& element, const std::string& event,
const event_callback& callback) const event_callback& callback)
{ {
@ -408,6 +408,53 @@ namespace ui_scripting::lua
auto menu_type = state.new_usertype<menu>("menu"); auto menu_type = state.new_usertype<menu>("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<int>());
}
if (arg.get_type() == sol::type::string)
{
event.arguments.push_back(arg.as<std::string>());
}
}
handler.dispatch(event);
};
menu_type["addchild"] = [](const sol::this_state s, menu& menu, element& element) menu_type["addchild"] = [](const sol::this_state s, menu& menu, element& element)
{ {
menu.add_child(&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 struct game
{ {
}; };
@ -459,24 +531,38 @@ namespace ui_scripting::lua
return pos; 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()) if (menus.find(name) == menus.end())
{ {
return; 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()) if (menus.find(name) == menus.end())
{ {
return; 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) 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"); throw std::runtime_error("Not in game");
} }
std::vector<scripting::script_value> arguments{}; ::scheduler::once([s, name, args = std::vector<sol::object>(va.begin(), va.end())]()
for (auto arg : va)
{ {
arguments.push_back(convert({s, arg})); std::vector<scripting::script_value> 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::entity>(); const auto player = scripting::call("getentbynum", {0}).as<scripting::entity>();
scripting::notify(player, name, arguments); scripting::notify(player, name, arguments);
}, ::scheduler::pipeline::server); }, ::scheduler::pipeline::server);

View File

@ -350,7 +350,14 @@ namespace ui_scripting::lua::engine
return; 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) void close_menu(const std::string& name)
@ -360,7 +367,14 @@ namespace ui_scripting::lua::engine
return; return;
} }
menus[name].close(); const auto menu = &menus[name];
event event;
event.element = menu;
event.name = "close";
notify(event);
menu->close();
} }
void start() void start()

View File

@ -7,7 +7,7 @@ namespace ui_scripting::lua
struct event struct event
{ {
std::string name; std::string name;
element* element{}; const void* element{};
std::vector<std::variant<int, std::string>> arguments; std::vector<std::variant<int, std::string>> arguments;
}; };
} }

View File

@ -35,7 +35,7 @@ namespace ui_scripting::lua
for (auto i = tasks.begin(); i != tasks.end();) 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; ++i;
continue; continue;
@ -87,7 +87,7 @@ namespace ui_scripting::lua
{ {
if (task.id == handle.id) 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) 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; task.is_deleted = true;
break; break;

View File

@ -16,7 +16,7 @@ namespace ui_scripting::lua
{ {
public: public:
std::string event = {}; std::string event = {};
element* element{}; void* element{};
event_callback callback = {}; event_callback callback = {};
bool is_volatile = false; bool is_volatile = false;
bool is_deleted = false; bool is_deleted = false;

View File

@ -27,7 +27,7 @@ namespace ui_scripting::lua
{ {
for (auto& condition : task.endon_conditions) 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; task.is_deleted = true;
break; break;

View File

@ -16,37 +16,23 @@ namespace ui_scripting
void menu::open() void menu::open()
{ {
if (this->visible) *game::keyCatchers |= 0x40;
{
return;
}
this->cursor_was_enabled = *game::keyCatchers & 0x40;
if (!this->cursor_was_enabled && this->cursor)
{
*game::keyCatchers |= 0x40;
}
this->visible = true; this->visible = true;
} }
void menu::close() void menu::close()
{ {
if (!this->visible) *game::keyCatchers &= ~0x40;
{
return;
}
if (!this->cursor_was_enabled && this->cursor)
{
*game::keyCatchers &= ~0x40;
}
this->visible = false; this->visible = false;
} }
void menu::render() const void menu::render() const
{ {
if (this->cursor)
{
*game::keyCatchers |= 0x40;
}
for (auto& element : this->children) for (auto& element : this->children)
{ {
element->render(); element->render();

View File

@ -17,7 +17,6 @@ namespace ui_scripting
bool visible = false; bool visible = false;
bool cursor = false; bool cursor = false;
bool cursor_was_enabled = false;
void open(); void open();
void close(); void close();