From 892e24f71e5797d692b3c018a8ca56ab156da331 Mon Sep 17 00:00:00 2001 From: Hyper <34012267+hyperbx@users.noreply.github.com> Date: Wed, 8 Jan 2025 01:03:58 +0000 Subject: [PATCH] reddog: implemented custom button --- UnleashedRecomp/CMakeLists.txt | 4 +- UnleashedRecomp/ui/game_window.cpp | 2 + UnleashedRecomp/ui/reddog/reddog_controls.cpp | 78 +++++++++++++++++-- UnleashedRecomp/ui/reddog/reddog_controls.h | 2 + .../ui/reddog/windows/view_window.cpp | 17 +++- 5 files changed, 93 insertions(+), 10 deletions(-) diff --git a/UnleashedRecomp/CMakeLists.txt b/UnleashedRecomp/CMakeLists.txt index f99ff93..3542f12 100644 --- a/UnleashedRecomp/CMakeLists.txt +++ b/UnleashedRecomp/CMakeLists.txt @@ -468,8 +468,10 @@ BIN2C(TARGET_OBJ UnleashedRecomp SOURCE_FILE "${RESOURCES_SOURCE_PATH}/images/re BIN2C(TARGET_OBJ UnleashedRecomp SOURCE_FILE "${RESOURCES_SOURCE_PATH}/images/reddog/button_pin_2.dds" DEST_FILE "${RESOURCES_OUTPUT_PATH}/images/reddog/button_pin_2.dds" ARRAY_NAME "g_button_pin_2") BIN2C(TARGET_OBJ UnleashedRecomp SOURCE_FILE "${RESOURCES_SOURCE_PATH}/images/reddog/checkbox_1.dds" DEST_FILE "${RESOURCES_OUTPUT_PATH}/images/reddog/checkbox_1.dds" ARRAY_NAME "g_checkbox_1") BIN2C(TARGET_OBJ UnleashedRecomp SOURCE_FILE "${RESOURCES_SOURCE_PATH}/images/reddog/checkbox_2.dds" DEST_FILE "${RESOURCES_OUTPUT_PATH}/images/reddog/checkbox_2.dds" ARRAY_NAME "g_checkbox_2") -BIN2C(TARGET_OBJ UnleashedRecomp SOURCE_FILE "${RESOURCES_SOURCE_PATH}/images/reddog/debug_icon.dds" DEST_FILE "${RESOURCES_OUTPUT_PATH}/images/reddog/debug_icon.dds" ARRAY_NAME "g_debug_icon") +BIN2C(TARGET_OBJ UnleashedRecomp SOURCE_FILE "${RESOURCES_SOURCE_PATH}/images/reddog/common_button_1.dds" DEST_FILE "${RESOURCES_OUTPUT_PATH}/images/reddog/common_button_1.dds" ARRAY_NAME "g_common_button_1") +BIN2C(TARGET_OBJ UnleashedRecomp SOURCE_FILE "${RESOURCES_SOURCE_PATH}/images/reddog/common_button_2.dds" DEST_FILE "${RESOURCES_OUTPUT_PATH}/images/reddog/common_button_2.dds" ARRAY_NAME "g_common_button_2") BIN2C(TARGET_OBJ UnleashedRecomp SOURCE_FILE "${RESOURCES_SOURCE_PATH}/images/reddog/common_icon.dds" DEST_FILE "${RESOURCES_OUTPUT_PATH}/images/reddog/common_icon.dds" ARRAY_NAME "g_common_icon") +BIN2C(TARGET_OBJ UnleashedRecomp SOURCE_FILE "${RESOURCES_SOURCE_PATH}/images/reddog/debug_icon.dds" DEST_FILE "${RESOURCES_OUTPUT_PATH}/images/reddog/debug_icon.dds" ARRAY_NAME "g_debug_icon") BIN2C(TARGET_OBJ UnleashedRecomp SOURCE_FILE "${RESOURCES_SOURCE_PATH}/images/reddog/mouse_cursor.bmp" DEST_FILE "${RESOURCES_OUTPUT_PATH}/images/reddog/mouse_cursor.bmp" ARRAY_NAME "g_mouse_cursor") BIN2C(TARGET_OBJ UnleashedRecomp SOURCE_FILE "${RESOURCES_SOURCE_PATH}/images/reddog/mouse_cursor_h.bmp" DEST_FILE "${RESOURCES_OUTPUT_PATH}/images/reddog/mouse_cursor_h.bmp" ARRAY_NAME "g_mouse_cursor_h") BIN2C(TARGET_OBJ UnleashedRecomp SOURCE_FILE "${RESOURCES_SOURCE_PATH}/images/reddog/mouse_cursor_slant_l.bmp" DEST_FILE "${RESOURCES_OUTPUT_PATH}/images/reddog/mouse_cursor_slant_l.bmp" ARRAY_NAME "g_mouse_cursor_slant_l") diff --git a/UnleashedRecomp/ui/game_window.cpp b/UnleashedRecomp/ui/game_window.cpp index 626a6a2..d94b448 100644 --- a/UnleashedRecomp/ui/game_window.cpp +++ b/UnleashedRecomp/ui/game_window.cpp @@ -490,6 +490,8 @@ void GameWindow::ResetDimensions() Config::WindowY = s_y; Config::WindowWidth = s_width; Config::WindowHeight = s_height; + + SetDimensions(s_width, s_height, s_x, s_y); } uint32_t GameWindow::GetWindowFlags() diff --git a/UnleashedRecomp/ui/reddog/reddog_controls.cpp b/UnleashedRecomp/ui/reddog/reddog_controls.cpp index 32e1a39..6c52817 100644 --- a/UnleashedRecomp/ui/reddog/reddog_controls.cpp +++ b/UnleashedRecomp/ui/reddog/reddog_controls.cpp @@ -1,23 +1,78 @@ #include "reddog_controls.h" #include +#include #include #include +#include +#include +constexpr float BUTTON_SIZE = 32.0f; constexpr float CHECKBOX_SIZE = 16.0f; static std::unique_ptr g_upCheckbox1; static std::unique_ptr g_upCheckbox2; +static std::unique_ptr g_upCommonButton1; +static std::unique_ptr g_upCommonButton2; void Reddog::InitControlsResources() { g_upCheckbox1 = LoadTexture(g_checkbox_1, sizeof(g_checkbox_1)); g_upCheckbox2 = LoadTexture(g_checkbox_2, sizeof(g_checkbox_2)); + g_upCommonButton1 = LoadTexture(g_common_button_1, sizeof(g_common_button_1)); + g_upCommonButton2 = LoadTexture(g_common_button_2, sizeof(g_common_button_2)); +} + +bool Reddog::Button(const char* label) +{ + auto window = ImGui::GetCurrentWindow(); + + if (window->SkipItems) + return false; + + auto& ctx = *GImGui; + auto& style = ctx.Style; + auto id = window->GetID(label); + auto pos = window->DC.CursorPos; + + ImVec2 labelSize = { 0, 0 }; + + if (label && label[0] != '\0') + labelSize = ImGui::CalcTextSize(label, nullptr, true); + + ImVec2 size = { BUTTON_SIZE + labelSize.x, BUTTON_SIZE }; + ImRect rect(pos, { pos.x + size.x, pos.y + size.y }); + ImRect paddedRect({ rect.Min.x, rect.Min.y - 1 }, { rect.Max.x, rect.Max.y + 1 }); + + ImGui::ItemSize(paddedRect, style.FramePadding.y); + + if (!ImGui::ItemAdd(rect, id)) + return false; + + bool isHovered, isHeld; + bool isPressed = ImGui::ButtonBehavior(rect, id, &isHovered, &isHeld); + + auto texture = isHeld && isHovered + ? g_upCommonButton2.get() + : g_upCommonButton1.get(); + + auto left = PIXELS_TO_UV_COORDS(32, 32, 0, 0, 9, 32); + auto centre = PIXELS_TO_UV_COORDS(32, 32, 9, 0, 14, 32); + auto right = PIXELS_TO_UV_COORDS(32, 32, 23, 0, 9, 32); + + window->DrawList->AddImage(texture, rect.Min, { rect.Min.x + 9, rect.Max.y }, GET_UV_COORDS(left)); + window->DrawList->AddImage(texture, { rect.Min.x + 9, rect.Min.y }, { rect.Max.x - 9, rect.Max.y }, GET_UV_COORDS(centre)); + window->DrawList->AddImage(texture, { rect.Max.x - 9, rect.Min.y }, rect.Max, GET_UV_COORDS(right)); + + if (label && label[0] != '\0') + ImGui::RenderText({ rect.Min.x + (size.x - labelSize.x) * 0.5f, rect.Min.y + (size.y - labelSize.y) * 0.5f }, label); + + return isPressed; } bool Reddog::Checkbox(const char* label, bool* v) { - ImGuiWindow* window = ImGui::GetCurrentWindow(); + auto window = ImGui::GetCurrentWindow(); if (window->SkipItems) return false; @@ -29,16 +84,16 @@ bool Reddog::Checkbox(const char* label, bool* v) auto pos = window->DC.CursorPos; ImVec2 size = { CHECKBOX_SIZE, CHECKBOX_SIZE }; - ImRect checkBoundingBox(pos, ImVec2(pos.x + size.x, pos.y + size.y)); - ImRect totalBoundingBox(pos, ImVec2(pos.x + size.x + (labelSize.x > 0.0f ? style.ItemInnerSpacing.x + labelSize.x : 0.0f), pos.y + size.y)); + ImRect testRect(pos, { pos.x + size.x, pos.y + size.y }); + ImRect totalRect(pos, { pos.x + size.x + (labelSize.x > 0.0f ? style.ItemInnerSpacing.x + labelSize.x : 0.0f), pos.y + size.y }); - ImGui::ItemSize(totalBoundingBox, style.FramePadding.y); + ImGui::ItemSize(totalRect, style.FramePadding.y); - if (!ImGui::ItemAdd(totalBoundingBox, id)) + if (!ImGui::ItemAdd(totalRect, id)) return false; bool isHovered, isHeld; - bool isPressed = ImGui::ButtonBehavior(checkBoundingBox, id, &isHovered, &isHeld); + bool isPressed = ImGui::ButtonBehavior(testRect, id, &isHovered, &isHeld); if (isPressed) { @@ -50,10 +105,17 @@ bool Reddog::Checkbox(const char* label, bool* v) ? g_upCheckbox2.get() : g_upCheckbox1.get(); - window->DrawList->AddImage(texture, checkBoundingBox.Min, checkBoundingBox.Max); + window->DrawList->AddImage(texture, testRect.Min, testRect.Max); if (labelSize.x > 0.0f) - ImGui::RenderText({ checkBoundingBox.Max.x + style.ItemInnerSpacing.x, checkBoundingBox.Min.y + style.FramePadding.y }, label); + ImGui::RenderText({ testRect.Max.x + style.ItemInnerSpacing.x, testRect.Min.y + style.FramePadding.y }, label); return isPressed; } + +void Reddog::Separator(float upperPadding, float lowerPadding) +{ + ImGui::Dummy(ImVec2(0.0f, upperPadding)); + ImGui::Separator(); + ImGui::Dummy(ImVec2(0.0f, lowerPadding)); +} diff --git a/UnleashedRecomp/ui/reddog/reddog_controls.h b/UnleashedRecomp/ui/reddog/reddog_controls.h index cc7a3bf..adef2da 100644 --- a/UnleashedRecomp/ui/reddog/reddog_controls.h +++ b/UnleashedRecomp/ui/reddog/reddog_controls.h @@ -3,5 +3,7 @@ namespace Reddog { void InitControlsResources(); + bool Button(const char* label); bool Checkbox(const char* label, bool* v); + void Separator(float upperPadding = 0, float lowerPadding = 0); } diff --git a/UnleashedRecomp/ui/reddog/windows/view_window.cpp b/UnleashedRecomp/ui/reddog/windows/view_window.cpp index 2ff9524..135e668 100644 --- a/UnleashedRecomp/ui/reddog/windows/view_window.cpp +++ b/UnleashedRecomp/ui/reddog/windows/view_window.cpp @@ -1,6 +1,8 @@ #include "view_window.h" #include #include +#include +#include static ViewWindow g_window{ "View" }; @@ -8,7 +10,20 @@ void ViewWindow::Draw() { if (Begin()) { - Reddog::Checkbox("Render HUD", (bool*)g_memory.Translate(0x8328BB26)); + Reddog::Checkbox("Draw HUD (F8)", (bool*)g_memory.Translate(0x8328BB26)); + Reddog::Separator(); + + if (Reddog::Button("Reset Window Dimensions (F2)")) + { + Config::Fullscreen = GameWindow::SetFullscreen(false); + GameWindow::ResetDimensions(); + } + + if (Reddog::Button("Recentre Window (F3)")) + { + if (!GameWindow::IsFullscreen()) + GameWindow::SetDimensions(GameWindow::s_width, GameWindow::s_height); + } } End(); }