From c3e6b21176deecdb37dc227386043bf91fbf969a Mon Sep 17 00:00:00 2001 From: fed <58637860+fedddddd@users.noreply.github.com> Date: Wed, 8 Mar 2023 13:55:29 +0100 Subject: [PATCH] Cleanup gui stuff --- .../asset_list.cpp} | 21 +++---- .../{gui_debug.cpp => gui/debug.cpp} | 7 ++- .../entity_list.cpp} | 17 ++---- src/client/component/{ => gui}/gui.cpp | 55 +++++++++++-------- src/client/component/{ => gui}/gui.hpp | 3 + .../script_console.cpp} | 16 ++---- src/client/component/input.cpp | 2 +- 7 files changed, 59 insertions(+), 62 deletions(-) rename src/client/component/{gui_asset_list.cpp => gui/asset_list.cpp} (86%) rename src/client/component/{gui_debug.cpp => gui/debug.cpp} (99%) rename src/client/component/{gui_entity_list.cpp => gui/entity_list.cpp} (98%) rename src/client/component/{ => gui}/gui.cpp (91%) rename src/client/component/{ => gui}/gui.hpp (85%) rename src/client/component/{gui_script_console.cpp => gui/script_console.cpp} (94%) diff --git a/src/client/component/gui_asset_list.cpp b/src/client/component/gui/asset_list.cpp similarity index 86% rename from src/client/component/gui_asset_list.cpp rename to src/client/component/gui/asset_list.cpp index d94241cb..c474719b 100644 --- a/src/client/component/gui_asset_list.cpp +++ b/src/client/component/gui/asset_list.cpp @@ -4,30 +4,25 @@ #include "game/game.hpp" #include "game/dvars.hpp" -#include "scheduler.hpp" -#include "command.hpp" +#include "component/scheduler.hpp" +#include "component/command.hpp" +#include "component/fastfiles.hpp" #include "gui.hpp" -#include "fastfiles.hpp" #include #include -namespace asset_list +namespace gui::asset_list { namespace { - bool shown_assets[game::XAssetType::ASSET_TYPE_COUNT]; + bool shown_assets[game::XAssetType::ASSET_TYPE_COUNT]{}; std::string asset_type_filter; std::string assets_name_filter[game::XAssetType::ASSET_TYPE_COUNT]; - void on_frame() + void render_window() { static auto* enabled = &gui::enabled_menus["asset_list"]; - if (!*enabled) - { - return; - } - ImGui::Begin("Asset list", enabled); ImGui::InputText("asset type", &asset_type_filter); @@ -92,9 +87,9 @@ namespace asset_list public: void post_unpack() override { - gui::on_frame(on_frame); + gui::register_menu("asset_list", "Asset List", render_window); } }; } -REGISTER_COMPONENT(asset_list::component) +REGISTER_COMPONENT(gui::asset_list::component) diff --git a/src/client/component/gui_debug.cpp b/src/client/component/gui/debug.cpp similarity index 99% rename from src/client/component/gui_debug.cpp rename to src/client/component/gui/debug.cpp index d41cabdb..9291672c 100644 --- a/src/client/component/gui_debug.cpp +++ b/src/client/component/gui/debug.cpp @@ -4,8 +4,8 @@ #include "game/game.hpp" #include "game/dvars.hpp" -#include "scheduler.hpp" -#include "command.hpp" +#include "component/scheduler.hpp" +#include "component/command.hpp" #include "gui.hpp" #include "game/scripting/lua/context.hpp" @@ -693,7 +693,8 @@ namespace gui::debug public: void post_unpack() override { - gui::on_frame(draw_window); + gui::register_menu("debug", "Debug", draw_window); + gui::on_frame([]() { if (!game::SV_Loaded() || cl_paused->current.integer) diff --git a/src/client/component/gui_entity_list.cpp b/src/client/component/gui/entity_list.cpp similarity index 98% rename from src/client/component/gui_entity_list.cpp rename to src/client/component/gui/entity_list.cpp index cd991e8c..7458ce77 100644 --- a/src/client/component/gui_entity_list.cpp +++ b/src/client/component/gui/entity_list.cpp @@ -4,10 +4,10 @@ #include "game/game.hpp" #include "game/dvars.hpp" -#include "scheduler.hpp" -#include "command.hpp" +#include "component/scheduler.hpp" +#include "component/command.hpp" +#include "component/scripting.hpp" #include "gui.hpp" -#include "scripting.hpp" #include "game/scripting/execution.hpp" @@ -807,14 +807,8 @@ namespace gui::entity_list ImGui::End(); } - void on_frame() + void render_window() { - static auto* enabled = &gui::enabled_menus["entity_list"]; - if (!*enabled) - { - return; - } - data_.access([](data_t& data) { if (!game::CL_IsCgameInitialized()) @@ -845,7 +839,8 @@ namespace gui::entity_list public: void post_unpack() override { - gui::on_frame(on_frame); + gui::register_menu("entity_list", "Entity List", render_window); + scheduler::loop([]() { try diff --git a/src/client/component/gui.cpp b/src/client/component/gui/gui.cpp similarity index 91% rename from src/client/component/gui.cpp rename to src/client/component/gui/gui.cpp index c421ecf7..31bd0bd4 100644 --- a/src/client/component/gui.cpp +++ b/src/client/component/gui/gui.cpp @@ -4,9 +4,9 @@ #include "game/game.hpp" #include "game/dvars.hpp" -#include "scheduler.hpp" +#include "component/scheduler.hpp" +#include "component/console.hpp" #include "gui.hpp" -#include "console.hpp" #include #include @@ -34,9 +34,17 @@ namespace gui LPARAM lParam; }; + struct menu_t + { + std::string name; + std::string title; + std::function render; + }; + utils::concurrency::container> on_frame_callbacks; utils::concurrency::container> notifications; utils::concurrency::container> event_queue; + std::vector menus; ID3D11Device* device; ID3D11DeviceContext* device_context; @@ -161,11 +169,10 @@ namespace gui { if (ImGui::BeginMenu("Windows")) { - menu_checkbox("Asset list", "asset_list"); - menu_checkbox("Entity list", "entity_list"); - menu_checkbox("Console", "console"); - menu_checkbox("Script console", "script_console"); - menu_checkbox("Debug", "debug"); + for (const auto& menu : menus) + { + menu_checkbox(menu.title, menu.name); + } ImGui::EndMenu(); } @@ -210,21 +217,6 @@ namespace gui return result; } - void dxgi_swap_chain_present_stub(utils::hook::assembler& a) - { - a.pushad64(); - a.call_aligned(gui_on_frame); - a.popad64(); - - a.mov(r8d, esi); - a.mov(edx, r15d); - a.mov(rcx, rdi); - a.call(rbx); - a.mov(ecx, eax); - - a.jmp(0x1407A14D1); - } - utils::hook::detour wnd_proc_hook; LRESULT wnd_proc_stub(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { @@ -232,7 +224,7 @@ namespace gui { event_queue.access([hWnd, msg, wParam, lParam](std::vector& queue) { - queue.push_back({hWnd, msg, wParam, lParam}); + queue.emplace_back(hWnd, msg, wParam, lParam); }); } @@ -271,7 +263,7 @@ namespace gui { on_frame_callbacks.access([always, callback](std::vector& callbacks) { - callbacks.push_back({callback, always}); + callbacks.emplace_back(callback, always); }); } @@ -300,6 +292,21 @@ namespace gui gui::notification("Text copied to clipboard", utils::string::va("\"%s\"", text.data())); } + void register_menu(const std::string& name, const std::string& title, + const std::function& callback, bool always) + { + menus.emplace_back(name, title, callback); + enabled_menus[name] = false; + + on_frame([=] + { + if (enabled_menus.at(name)) + { + callback(); + } + }, always); + } + class component final : public component_interface { public: diff --git a/src/client/component/gui.hpp b/src/client/component/gui/gui.hpp similarity index 85% rename from src/client/component/gui.hpp rename to src/client/component/gui/gui.hpp index ad8b3aee..8a6373cf 100644 --- a/src/client/component/gui.hpp +++ b/src/client/component/gui/gui.hpp @@ -20,4 +20,7 @@ namespace gui bool is_menu_open(const std::string& name); void notification(const std::string& title, const std::string& text, const std::chrono::milliseconds duration = 3s); void copy_to_clipboard(const std::string& text); + + void register_menu(const std::string& name, const std::string& title, + const std::function& callback, bool always = false); } \ No newline at end of file diff --git a/src/client/component/gui_script_console.cpp b/src/client/component/gui/script_console.cpp similarity index 94% rename from src/client/component/gui_script_console.cpp rename to src/client/component/gui/script_console.cpp index 2c036934..c5a785b5 100644 --- a/src/client/component/gui_script_console.cpp +++ b/src/client/component/gui/script_console.cpp @@ -4,8 +4,8 @@ #include "game/game.hpp" #include "game/dvars.hpp" -#include "scheduler.hpp" -#include "command.hpp" +#include "component/scheduler.hpp" +#include "component/command.hpp" #include "gui.hpp" #include "game/scripting/lua/context.hpp" @@ -114,13 +114,8 @@ namespace gui::script_console return 0; } - void on_frame() + void render_window() { - if (!gui::enabled_menus["script_console"]) - { - return; - } - menu_data.access([](menu_data_t& menu_data_) { if (!game::CL_IsCgameInitialized()) @@ -132,7 +127,8 @@ namespace gui::script_console static const auto input_text_flags = ImGuiInputTextFlags_EnterReturnsTrue | ImGuiInputTextFlags_CallbackCompletion | ImGuiInputTextFlags_CallbackHistory; - ImGui::Begin("Script console", &gui::enabled_menus["script_console"]); + static auto* enabled = &gui::enabled_menus["script_console"]; + ImGui::Begin("Script console", enabled); if (ImGui::BeginPopup("Options")) { @@ -222,7 +218,7 @@ namespace gui::script_console public: void post_unpack() override { - gui::on_frame(on_frame); + gui::register_menu("script_console", "Script Console", render_window); scheduler::loop(run_commands, scheduler::pipeline::server); } }; diff --git a/src/client/component/input.cpp b/src/client/component/input.cpp index 6ce036bd..a2d9bb30 100644 --- a/src/client/component/input.cpp +++ b/src/client/component/input.cpp @@ -4,7 +4,7 @@ #include "game/game.hpp" #include "game_console.hpp" -#include "gui.hpp" +#include "gui/gui.hpp" #include "game/ui_scripting/execution.hpp" #include