From abf6460cc97e52cfae9676d7f4b197ce70c5fec8 Mon Sep 17 00:00:00 2001 From: praydog Date: Mon, 11 Dec 2023 09:49:52 -0800 Subject: [PATCH] Adv: Style fixes, logic simplification --- src/Framework.cpp | 27 ++++-- src/Framework.hpp | 31 +++---- src/Mod.hpp | 211 +++++++++++++++++++++++++--------------------- 3 files changed, 145 insertions(+), 124 deletions(-) diff --git a/src/Framework.cpp b/src/Framework.cpp index b9c189db..7bc70180 100644 --- a/src/Framework.cpp +++ b/src/Framework.cpp @@ -1185,7 +1185,9 @@ void Framework::draw_ui() { ImGui::Columns(1); // Mods: - sidebar_entries.insert(sidebar_entries.begin(), {"About", false}); + auto& sidebar_entries = m_sidebar_state.entries; + sidebar_entries.clear(); + sidebar_entries.emplace_back("About", false); if (ImGui::BeginTable("UEVRTable", 2, ImGuiTableFlags_::ImGuiTableFlags_BordersInnerV | ImGuiTableFlags_::ImGuiTableFlags_BordersOuterV | ImGuiTableFlags_::ImGuiTableFlags_SizingFixedFit)) { ImGui::TableSetupColumn("UEVRLeftPaneColumn", ImGuiTableColumnFlags_WidthFixed, 150.0f); @@ -1219,14 +1221,29 @@ void Framework::draw_ui() { std::vector mod_sidebar_ranges{}; for (auto& mod : m_mods->get_mods()) { + if (mod->is_advanced_mod() && !m_advanced_view_enabled) { + continue; + } + auto entries = mod->get_sidebar_entries(); if (!entries.empty()) { - mod_sidebar_ranges.push_back(Info{sidebar_entries.size(), sidebar_entries.size() + entries.size(), mod, true}); - sidebar_entries.insert(sidebar_entries.end(), entries.begin(), entries.end()); + size_t displayed_entries = 0; + for (auto& entry : entries) { + if (entry.m_advanced_entry && !m_advanced_view_enabled) { + continue; + } + + sidebar_entries.emplace_back(entry.m_label.c_str(), entry.m_advanced_entry); + ++displayed_entries; + } + + if (displayed_entries > 0) { + mod_sidebar_ranges.push_back(Info{sidebar_entries.size() - displayed_entries, sidebar_entries.size(), mod, true}); + } } else { mod_sidebar_ranges.push_back(Info{sidebar_entries.size(), sidebar_entries.size() + 1, mod, false}); - sidebar_entries.push_back({mod->get_name().data(), mod->is_advanced_mod()} ); + sidebar_entries.emplace_back(mod->get_name().data(), mod->is_advanced_mod()); } } @@ -1348,8 +1365,6 @@ void Framework::draw_ui() { if (m_last_draw_ui && !m_draw_ui) { m_windows_message_hook->window_toggle_cursor(m_cursor_state); } - - sidebar_entries.clear(); } void Framework::draw_about() { diff --git a/src/Framework.hpp b/src/Framework.hpp index 460e2fbe..8b321f94 100644 --- a/src/Framework.hpp +++ b/src/Framework.hpp @@ -57,8 +57,9 @@ class UEVRSharedMemory { Data* m_data{}; }; - struct SidebarEntryInfo { + SidebarEntryInfo(std::string_view label, bool advanced) : m_label(label), m_advanced_entry(advanced) {} + std::string m_label{}; bool m_advanced_entry{false}; }; @@ -196,14 +197,8 @@ class Framework { if (delta < std::chrono::milliseconds(100)) { return; } - int32_t selected_entry_local = m_sidebar_state.selected_entry + 1; - if (!m_advanced_view_enabled) { - while (sidebar_entries[selected_entry_local].m_advanced_entry) { - ++selected_entry_local; - } - } - m_sidebar_state.selected_entry = selected_entry_local; + ++m_sidebar_state.selected_entry; m_last_page_inc_time = now; } @@ -213,27 +208,20 @@ class Framework { if (delta < std::chrono::milliseconds(100)) { return; } - int32_t selected_entry_local = m_sidebar_state.selected_entry - 1; - if (!m_advanced_view_enabled) { - while (sidebar_entries[selected_entry_local].m_advanced_entry) { - --selected_entry_local; - } - } - m_sidebar_state.selected_entry = selected_entry_local; + --m_sidebar_state.selected_entry; m_last_page_dec_time = now; } + bool is_advanced_view_enabled() const { + return m_advanced_view_enabled; + } + private: void consume_input(); void update_fonts(); void invalidate_device_objects(); -public: - bool is_advanced_view_enabled() { - return m_advanced_view_enabled; - } - private: void draw_ui(); void draw_about(); @@ -273,7 +261,6 @@ class Framework { bool m_cursor_state_changed{true}; bool m_ui_option_transparent{true}; bool m_ui_passthrough{false}; - std::vector sidebar_entries{}; ImVec2 m_last_window_pos{}; ImVec2 m_last_window_size{}; @@ -339,6 +326,8 @@ class Framework { struct { int32_t selected_entry{0}; bool initialized{false}; + + std::vector entries{}; } m_sidebar_state{}; template using ComPtr = Microsoft::WRL::ComPtr; diff --git a/src/Mod.hpp b/src/Mod.hpp index 9f40ec73..097f2c9c 100644 --- a/src/Mod.hpp +++ b/src/Mod.hpp @@ -80,11 +80,11 @@ class ModValue : public IModValue { return m_config_name; } - const auto& is_advanced_option() const { + bool is_advanced_option() const { return m_advanced_option; } - auto should_draw_option() -> bool { + bool should_draw_option() const { return g_framework->is_advanced_view_enabled() || !this->m_advanced_option; } @@ -109,20 +109,23 @@ class ModToggle : public ModValue { } bool draw(std::string_view name) override { - if (should_draw_option()) { - ImGui::PushID(this); - auto ret = ImGui::Checkbox(name.data(), &m_value); - ImGui::PopID(); - - return ret; + if (!should_draw_option()) { + return false; } - return false; + + ImGui::PushID(this); + auto ret = ImGui::Checkbox(name.data(), &m_value); + ImGui::PopID(); + + return ret; } void draw_value(std::string_view name) override { - if (should_draw_option()) { - ImGui::Text("%s: %i", name.data(), m_value); + if (!should_draw_option()) { + return; } + + ImGui::Text("%s: %i", name.data(), m_value); } bool toggle() { @@ -142,21 +145,23 @@ class ModFloat : public ModValue { } bool draw(std::string_view name) override { - if (should_draw_option()) { - ImGui::PushID(this); - auto ret = ImGui::InputFloat(name.data(), &m_value); - ImGui::PopID(); - - return ret; + if (!should_draw_option()) { + return false; } - return false; + + ImGui::PushID(this); + auto ret = ImGui::InputFloat(name.data(), &m_value); + ImGui::PopID(); + + return ret; } void draw_value(std::string_view name) override { - if (should_draw_option()) { - ImGui::Text("%s: %f", name.data(), m_value); + if (!should_draw_option()) { + return; } - + + ImGui::Text("%s: %f", name.data(), m_value); } }; @@ -175,20 +180,24 @@ class ModSlider : public ModFloat { } bool draw(std::string_view name) override { - if (should_draw_option()) { - ImGui::PushID(this); - auto ret = ImGui::SliderFloat(name.data(), &m_value, m_range.x, m_range.y); - ImGui::PopID(); - - return ret; + if (!should_draw_option()) { + return false; } - return false; + + + ImGui::PushID(this); + auto ret = ImGui::SliderFloat(name.data(), &m_value, m_range.x, m_range.y); + ImGui::PopID(); + + return ret; } void draw_value(std::string_view name) override { - if (should_draw_option()) { - ImGui::Text("%s: %f [%f, %f]", name.data(), m_value, m_range.x, m_range.y); + if (!should_draw_option()) { + return; } + + ImGui::Text("%s: %f [%f, %f]", name.data(), m_value, m_range.x, m_range.y); } auto& range() { @@ -213,20 +222,23 @@ class ModInt32 : public ModValue { } bool draw(std::string_view name) override { - if (should_draw_option()) { - ImGui::PushID(this); - auto ret = ImGui::InputInt(name.data(), &m_value); - ImGui::PopID(); - - return ret; + if (!should_draw_option()) { + return false; } - return false; + + ImGui::PushID(this); + auto ret = ImGui::InputInt(name.data(), &m_value); + ImGui::PopID(); + + return ret; } void draw_value(std::string_view name) override { - if (should_draw_option()) { - ImGui::Text("%s: %i", name.data(), m_value); + if (!should_draw_option()) { + return; } + + ImGui::Text("%s: %i", name.data(), m_value); } }; @@ -245,14 +257,15 @@ class ModSliderInt32 : public ModInt32 { } bool draw(std::string_view name) override { - if (should_draw_option()) { - ImGui::PushID(this); - auto ret = ImGui::SliderInt(name.data(), &m_value, m_int_range.min, m_int_range.max); - ImGui::PopID(); - - return ret; + if (!should_draw_option()) { + return false; } - return false; + + ImGui::PushID(this); + auto ret = ImGui::SliderInt(name.data(), &m_value, m_int_range.min, m_int_range.max); + ImGui::PopID(); + + return ret; } void draw_value(std::string_view name) override { @@ -288,25 +301,28 @@ class ModCombo : public ModValue { } bool draw(std::string_view name) override { - if (should_draw_option()) { - // clamp m_value to valid range - m_value = std::clamp(m_value, 0, static_cast(m_options.size()) - 1); + if (!should_draw_option()) { + return false; + } - ImGui::PushID(this); - auto ret = ImGui::Combo(name.data(), &m_value, m_options.data(), static_cast(m_options.size())); - ImGui::PopID(); + // clamp m_value to valid range + m_value = std::clamp(m_value, 0, static_cast(m_options.size()) - 1); - return ret; - } - return false; + ImGui::PushID(this); + auto ret = ImGui::Combo(name.data(), &m_value, m_options.data(), static_cast(m_options.size())); + ImGui::PopID(); + + return ret; } void draw_value(std::string_view name) override { - if (should_draw_option()) { - m_value = std::clamp(m_value, 0, static_cast(m_options.size()) - 1); - - ImGui::Text("%s: %s", name.data(), m_options[m_value]); + if (!should_draw_option()) { + return; } + + m_value = std::clamp(m_value, 0, static_cast(m_options.size()) - 1); + + ImGui::Text("%s: %s", name.data(), m_options[m_value]); } void config_load(const utility::Config& cfg, bool set_defaults) override { @@ -348,56 +364,57 @@ class ModKey: public ModInt32 { } bool draw(std::string_view name) override { - if (should_draw_option()) { - if (name.empty()) { - return false; - } + if (!should_draw_option()) { + return false; + } - ImGui::PushID(this); - ImGui::Button(name.data()); + if (name.empty()) { + return false; + } - if (ImGui::IsItemHovered() && ImGui::GetIO().MouseDown[0]) { - m_waiting_for_new_key = true; - } + ImGui::PushID(this); + ImGui::Button(name.data()); + + if (ImGui::IsItemHovered() && ImGui::GetIO().MouseDown[0]) { + m_waiting_for_new_key = true; + } - if (m_waiting_for_new_key) { - const auto &keys = g_framework->get_keyboard_state(); - for (int32_t k{ 0 }; k < keys.size(); ++k) { - if (k == VK_LBUTTON || k == VK_RBUTTON) { - continue; - } - - if (keys[k]) { - m_value = is_erase_key(k) ? UNBOUND_KEY : k; - m_waiting_for_new_key = false; - break; - } + if (m_waiting_for_new_key) { + const auto &keys = g_framework->get_keyboard_state(); + for (int32_t k{ 0 }; k < keys.size(); ++k) { + if (k == VK_LBUTTON || k == VK_RBUTTON) { + continue; } - ImGui::SameLine(); - ImGui::Text("Press any key..."); + if (keys[k]) { + m_value = is_erase_key(k) ? UNBOUND_KEY : k; + m_waiting_for_new_key = false; + break; + } } - else { - ImGui::SameLine(); - - if (m_value >= 0 && m_value <= 255) { - if (keycodes.contains(m_value)) { - ImGui::Text("%s", keycodes[m_value].c_str()); - } - else { - ImGui::Text("%i (Unknown)", m_value); - } + + ImGui::SameLine(); + ImGui::Text("Press any key..."); + } + else { + ImGui::SameLine(); + + if (m_value >= 0 && m_value <= 255) { + if (keycodes.contains(m_value)) { + ImGui::Text("%s", keycodes[m_value].c_str()); } else { - ImGui::Text("Not bound"); + ImGui::Text("%i (Unknown)", m_value); } } + else { + ImGui::Text("Not bound"); + } + } - ImGui::PopID(); + ImGui::PopID(); - return true; - } - return false; + return true; } bool is_key_down() const {