Cleanup gui stuff

This commit is contained in:
fed 2023-03-08 13:55:29 +01:00
parent 758b2d2006
commit c3e6b21176
7 changed files with 59 additions and 62 deletions

View File

@ -4,30 +4,25 @@
#include "game/game.hpp" #include "game/game.hpp"
#include "game/dvars.hpp" #include "game/dvars.hpp"
#include "scheduler.hpp" #include "component/scheduler.hpp"
#include "command.hpp" #include "component/command.hpp"
#include "component/fastfiles.hpp"
#include "gui.hpp" #include "gui.hpp"
#include "fastfiles.hpp"
#include <utils/string.hpp> #include <utils/string.hpp>
#include <utils/hook.hpp> #include <utils/hook.hpp>
namespace asset_list namespace gui::asset_list
{ {
namespace namespace
{ {
bool shown_assets[game::XAssetType::ASSET_TYPE_COUNT]; bool shown_assets[game::XAssetType::ASSET_TYPE_COUNT]{};
std::string asset_type_filter; std::string asset_type_filter;
std::string assets_name_filter[game::XAssetType::ASSET_TYPE_COUNT]; std::string assets_name_filter[game::XAssetType::ASSET_TYPE_COUNT];
void on_frame() void render_window()
{ {
static auto* enabled = &gui::enabled_menus["asset_list"]; static auto* enabled = &gui::enabled_menus["asset_list"];
if (!*enabled)
{
return;
}
ImGui::Begin("Asset list", enabled); ImGui::Begin("Asset list", enabled);
ImGui::InputText("asset type", &asset_type_filter); ImGui::InputText("asset type", &asset_type_filter);
@ -92,9 +87,9 @@ namespace asset_list
public: public:
void post_unpack() override 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)

View File

@ -4,8 +4,8 @@
#include "game/game.hpp" #include "game/game.hpp"
#include "game/dvars.hpp" #include "game/dvars.hpp"
#include "scheduler.hpp" #include "component/scheduler.hpp"
#include "command.hpp" #include "component/command.hpp"
#include "gui.hpp" #include "gui.hpp"
#include "game/scripting/lua/context.hpp" #include "game/scripting/lua/context.hpp"
@ -693,7 +693,8 @@ namespace gui::debug
public: public:
void post_unpack() override void post_unpack() override
{ {
gui::on_frame(draw_window); gui::register_menu("debug", "Debug", draw_window);
gui::on_frame([]() gui::on_frame([]()
{ {
if (!game::SV_Loaded() || cl_paused->current.integer) if (!game::SV_Loaded() || cl_paused->current.integer)

View File

@ -4,10 +4,10 @@
#include "game/game.hpp" #include "game/game.hpp"
#include "game/dvars.hpp" #include "game/dvars.hpp"
#include "scheduler.hpp" #include "component/scheduler.hpp"
#include "command.hpp" #include "component/command.hpp"
#include "component/scripting.hpp"
#include "gui.hpp" #include "gui.hpp"
#include "scripting.hpp"
#include "game/scripting/execution.hpp" #include "game/scripting/execution.hpp"
@ -807,14 +807,8 @@ namespace gui::entity_list
ImGui::End(); ImGui::End();
} }
void on_frame() void render_window()
{ {
static auto* enabled = &gui::enabled_menus["entity_list"];
if (!*enabled)
{
return;
}
data_.access([](data_t& data) data_.access([](data_t& data)
{ {
if (!game::CL_IsCgameInitialized()) if (!game::CL_IsCgameInitialized())
@ -845,7 +839,8 @@ namespace gui::entity_list
public: public:
void post_unpack() override void post_unpack() override
{ {
gui::on_frame(on_frame); gui::register_menu("entity_list", "Entity List", render_window);
scheduler::loop([]() scheduler::loop([]()
{ {
try try

View File

@ -4,9 +4,9 @@
#include "game/game.hpp" #include "game/game.hpp"
#include "game/dvars.hpp" #include "game/dvars.hpp"
#include "scheduler.hpp" #include "component/scheduler.hpp"
#include "component/console.hpp"
#include "gui.hpp" #include "gui.hpp"
#include "console.hpp"
#include <utils/string.hpp> #include <utils/string.hpp>
#include <utils/hook.hpp> #include <utils/hook.hpp>
@ -34,9 +34,17 @@ namespace gui
LPARAM lParam; LPARAM lParam;
}; };
struct menu_t
{
std::string name;
std::string title;
std::function<void()> render;
};
utils::concurrency::container<std::vector<frame_callback>> on_frame_callbacks; utils::concurrency::container<std::vector<frame_callback>> on_frame_callbacks;
utils::concurrency::container<std::deque<notification_t>> notifications; utils::concurrency::container<std::deque<notification_t>> notifications;
utils::concurrency::container<std::vector<event>> event_queue; utils::concurrency::container<std::vector<event>> event_queue;
std::vector<menu_t> menus;
ID3D11Device* device; ID3D11Device* device;
ID3D11DeviceContext* device_context; ID3D11DeviceContext* device_context;
@ -161,11 +169,10 @@ namespace gui
{ {
if (ImGui::BeginMenu("Windows")) if (ImGui::BeginMenu("Windows"))
{ {
menu_checkbox("Asset list", "asset_list"); for (const auto& menu : menus)
menu_checkbox("Entity list", "entity_list"); {
menu_checkbox("Console", "console"); menu_checkbox(menu.title, menu.name);
menu_checkbox("Script console", "script_console"); }
menu_checkbox("Debug", "debug");
ImGui::EndMenu(); ImGui::EndMenu();
} }
@ -210,21 +217,6 @@ namespace gui
return result; 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; utils::hook::detour wnd_proc_hook;
LRESULT wnd_proc_stub(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) 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<event>& queue) event_queue.access([hWnd, msg, wParam, lParam](std::vector<event>& 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<frame_callback>& callbacks) on_frame_callbacks.access([always, callback](std::vector<frame_callback>& 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())); 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<void()>& 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 class component final : public component_interface
{ {
public: public:

View File

@ -20,4 +20,7 @@ namespace gui
bool is_menu_open(const std::string& name); 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 notification(const std::string& title, const std::string& text, const std::chrono::milliseconds duration = 3s);
void copy_to_clipboard(const std::string& text); void copy_to_clipboard(const std::string& text);
void register_menu(const std::string& name, const std::string& title,
const std::function<void()>& callback, bool always = false);
} }

View File

@ -4,8 +4,8 @@
#include "game/game.hpp" #include "game/game.hpp"
#include "game/dvars.hpp" #include "game/dvars.hpp"
#include "scheduler.hpp" #include "component/scheduler.hpp"
#include "command.hpp" #include "component/command.hpp"
#include "gui.hpp" #include "gui.hpp"
#include "game/scripting/lua/context.hpp" #include "game/scripting/lua/context.hpp"
@ -114,13 +114,8 @@ namespace gui::script_console
return 0; return 0;
} }
void on_frame() void render_window()
{ {
if (!gui::enabled_menus["script_console"])
{
return;
}
menu_data.access([](menu_data_t& menu_data_) menu_data.access([](menu_data_t& menu_data_)
{ {
if (!game::CL_IsCgameInitialized()) if (!game::CL_IsCgameInitialized())
@ -132,7 +127,8 @@ namespace gui::script_console
static const auto input_text_flags = ImGuiInputTextFlags_EnterReturnsTrue | ImGuiInputTextFlags_CallbackCompletion | static const auto input_text_flags = ImGuiInputTextFlags_EnterReturnsTrue | ImGuiInputTextFlags_CallbackCompletion |
ImGuiInputTextFlags_CallbackHistory; 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")) if (ImGui::BeginPopup("Options"))
{ {
@ -222,7 +218,7 @@ namespace gui::script_console
public: public:
void post_unpack() override 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); scheduler::loop(run_commands, scheduler::pipeline::server);
} }
}; };

View File

@ -4,7 +4,7 @@
#include "game/game.hpp" #include "game/game.hpp"
#include "game_console.hpp" #include "game_console.hpp"
#include "gui.hpp" #include "gui/gui.hpp"
#include "game/ui_scripting/execution.hpp" #include "game/ui_scripting/execution.hpp"
#include <utils/hook.hpp> #include <utils/hook.hpp>