Skip to content

Commit

Permalink
FullscreenUI: Improve menu button borders
Browse files Browse the repository at this point in the history
  • Loading branch information
stenzek committed Dec 12, 2023
1 parent c467825 commit ad96b1e
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/core/fullscreen_ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5409,7 +5409,7 @@ void FullscreenUI::DrawSaveStateSelector(bool is_loading)
const float t = std::min(static_cast<float>(std::abs(std::sin(ImGui::GetTime() * 0.75) * 1.1)), 1.0f);
ImGui::PushStyleColor(ImGuiCol_Border, ImGui::GetColorU32(ImGuiCol_Border, t));

ImGui::RenderFrame(bb.Min, bb.Max, col, true, 0.0f);
ImGuiFullscreen::DrawMenuButtonFrame(bb.Min, bb.Max, col, true, 0.0f);

ImGui::PopStyleColor();
}
Expand Down Expand Up @@ -6031,7 +6031,7 @@ void FullscreenUI::DrawGameGrid(const ImVec2& heading_size)
const float t = static_cast<float>(std::min(std::abs(std::sin(ImGui::GetTime() * 0.75) * 1.1), 1.0));
ImGui::PushStyleColor(ImGuiCol_Border, ImGui::GetColorU32(ImGuiCol_Border, t));

ImGui::RenderFrame(bb.Min, bb.Max, col, true, 0.0f);
ImGuiFullscreen::DrawMenuButtonFrame(bb.Min, bb.Max, col, true, 0.0f);

ImGui::PopStyleColor();
}
Expand Down
74 changes: 66 additions & 8 deletions src/util/imgui_fullscreen.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// SPDX-FileCopyrightText: 2019-2022 Connor McLaughlin <stenzek@gmail.com>
// SPDX-FileCopyrightText: 2019-2023 Connor McLaughlin <stenzek@gmail.com>
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)

#define IMGUI_DEFINE_MATH_OPERATORS

#include "imgui_fullscreen.h"
#include "IconsFontAwesome5.h"
#include "gpu_device.h"
#include "imgui_animated.h"

#include "common/assert.h"
#include "common/easing.h"
#include "common/file_system.h"
Expand All @@ -15,9 +17,12 @@
#include "common/string_util.h"
#include "common/threading.h"
#include "common/timer.h"

#include "core/host.h"

#include "fmt/core.h"
#include "gpu_device.h"

#include "IconsFontAwesome5.h"
#include "imgui_internal.h"
#include "imgui_stdlib.h"

Expand All @@ -34,6 +39,8 @@ Log_SetChannel(ImGuiFullscreen);
namespace ImGuiFullscreen {
using MessageDialogCallbackVariant = std::variant<InfoMessageDialogCallback, ConfirmMessageDialogCallback>;

static constexpr float MENU_BACKGROUND_ANIMATION_TIME = 0.25f;

static std::optional<Common::RGBA8Image> LoadTextureImage(const char* path);
static std::shared_ptr<GPUTexture> UploadTexture(const char* path, const Common::RGBA8Image& image);
static void TextureLoaderThread();
Expand Down Expand Up @@ -113,6 +120,11 @@ static std::string s_message_dialog_message;
static std::array<std::string, 3> s_message_dialog_buttons;
static MessageDialogCallbackVariant s_message_dialog_callback;

static ImAnimatedVec2 s_menu_button_frame_min_animated;
static ImAnimatedVec2 s_menu_button_frame_max_animated;
static bool s_had_hovered_menu_item = false;
static bool s_has_hovered_menu_item = false;

namespace {
struct FileSelectorItem
{
Expand Down Expand Up @@ -203,6 +215,7 @@ bool ImGuiFullscreen::Initialize(const char* placeholder_image_path)

s_texture_load_thread_quit.store(false, std::memory_order_release);
s_texture_load_thread = std::thread(TextureLoaderThread);
ResetMenuButtonFrame();
return true;
}

Expand Down Expand Up @@ -478,6 +491,8 @@ void ImGuiFullscreen::EndLayout()
DrawToast();

PopResetLayout();

s_had_hovered_menu_item = std::exchange(s_has_hovered_menu_item, false);
}

void ImGuiFullscreen::PushResetLayout()
Expand Down Expand Up @@ -775,7 +790,7 @@ bool ImGuiFullscreen::MenuButtonFrame(const char* str_id, bool enabled, float he
const float t = static_cast<float>(std::min(std::abs(std::sin(ImGui::GetTime() * 0.75) * 1.1), 1.0));
ImGui::PushStyleColor(ImGuiCol_Border, ImGui::GetColorU32(ImGuiCol_Border, t));

ImGui::RenderFrame(bb->Min, bb->Max, col, true, 0.0f);
DrawMenuButtonFrame(bb->Min, bb->Max, col, true, 0.0f);

ImGui::PopStyleColor();
}
Expand All @@ -793,6 +808,43 @@ bool ImGuiFullscreen::MenuButtonFrame(const char* str_id, bool enabled, float he
return pressed;
}

void ImGuiFullscreen::DrawMenuButtonFrame(const ImVec2& p_min, const ImVec2& p_max, ImU32 fill_col,
bool border /* = true */, float rounding /* = 0.0f */)
{
ImVec2 frame_min = p_min;
ImVec2 frame_max = p_max;

if (ImGui::GetIO().NavVisible)
{
if (!s_had_hovered_menu_item)
{
s_menu_button_frame_min_animated.Reset(frame_min);
s_menu_button_frame_max_animated.Reset(frame_max);
s_has_hovered_menu_item = true;
}
else
{
if (frame_min.x != s_menu_button_frame_min_animated.GetEndValue().x ||
frame_min.y != s_menu_button_frame_min_animated.GetEndValue().y)
{
s_menu_button_frame_min_animated.Start(s_menu_button_frame_min_animated.GetCurrentValue(), frame_min,
MENU_BACKGROUND_ANIMATION_TIME);
}
if (frame_max.x != s_menu_button_frame_max_animated.GetEndValue().x ||
frame_max.y != s_menu_button_frame_max_animated.GetEndValue().x)
{
s_menu_button_frame_max_animated.Start(s_menu_button_frame_max_animated.GetCurrentValue(), frame_max,
MENU_BACKGROUND_ANIMATION_TIME);
}
frame_min = s_menu_button_frame_min_animated.UpdateAndGetValue();
frame_max = s_menu_button_frame_max_animated.UpdateAndGetValue();
s_has_hovered_menu_item = true;
}
}

ImGui::RenderFrame(frame_min, frame_max, fill_col, border, rounding);
}

bool ImGuiFullscreen::MenuButtonFrame(const char* str_id, bool enabled, float height, bool* visible, bool* hovered,
ImVec2* min, ImVec2* max, ImGuiButtonFlags flags /*= 0*/,
float hover_alpha /*= 0*/)
Expand All @@ -804,6 +856,12 @@ bool ImGuiFullscreen::MenuButtonFrame(const char* str_id, bool enabled, float he
return result;
}

void ImGuiFullscreen::ResetMenuButtonFrame()
{
s_had_hovered_menu_item = false;
s_has_hovered_menu_item = false;
}

void ImGuiFullscreen::MenuHeading(const char* title, bool draw_line /*= true*/)
{
const float line_thickness = draw_line ? LayoutScale(1.0f) : 0.0f;
Expand Down Expand Up @@ -876,7 +934,7 @@ bool ImGuiFullscreen::ActiveButton(const char* title, bool is_active, bool enabl
{
ImVec2 pos, size;
GetMenuButtonFrameBounds(height, &pos, &size);
ImGui::RenderFrame(pos, pos + size, ImGui::GetColorU32(UIPrimaryColor), false);
DrawMenuButtonFrame(pos, pos + size, ImGui::GetColorU32(UIPrimaryColor), false);
}

ImRect bb;
Expand Down Expand Up @@ -1066,7 +1124,7 @@ bool ImGuiFullscreen::FloatingButton(const char* text, float x, float y, float w
const float t = std::min(static_cast<float>(std::abs(std::sin(ImGui::GetTime() * 0.75) * 1.1)), 1.0f);
const ImU32 col = ImGui::GetColorU32(held ? ImGuiCol_ButtonActive : ImGuiCol_ButtonHovered, 1.0f);
ImGui::PushStyleColor(ImGuiCol_Border, ImGui::GetColorU32(ImGuiCol_Border, t));
ImGui::RenderFrame(bb.Min, bb.Max, col, true, 0.0f);
DrawMenuButtonFrame(bb.Min, bb.Max, col, true, 0.0f);
ImGui::PopStyleColor();
}
}
Expand Down Expand Up @@ -1585,7 +1643,7 @@ bool ImGuiFullscreen::NavButton(const char* title, bool is_active, bool enabled
if (hovered)
{
const ImU32 col = ImGui::GetColorU32(held ? ImGuiCol_ButtonActive : ImGuiCol_ButtonHovered, 1.0f);
ImGui::RenderFrame(bb.Min, bb.Max, col, true, 0.0f);
DrawMenuButtonFrame(bb.Min, bb.Max, col, true, 0.0f);
}
}
else
Expand Down Expand Up @@ -1662,7 +1720,7 @@ bool ImGuiFullscreen::NavTab(const char* title, bool is_active, bool enabled /*
hovered ? ImGui::GetColorU32(held ? ImGuiCol_ButtonActive : ImGuiCol_ButtonHovered, 1.0f) :
ImGui::GetColorU32(is_active ? background : ImVec4(background.x, background.y, background.z, 0.5f));

ImGui::RenderFrame(bb.Min, bb.Max, col, true, 0.0f);
DrawMenuButtonFrame(bb.Min, bb.Max, col, true, 0.0f);

if (is_active)
{
Expand Down
3 changes: 3 additions & 0 deletions src/util/imgui_fullscreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ void EndMenuButtons();
void GetMenuButtonFrameBounds(float height, ImVec2* pos, ImVec2* size);
bool MenuButtonFrame(const char* str_id, bool enabled, float height, bool* visible, bool* hovered, ImVec2* min,
ImVec2* max, ImGuiButtonFlags flags = 0, float hover_alpha = 1.0f);
void DrawMenuButtonFrame(const ImVec2& p_min, const ImVec2& p_max, ImU32 fill_col, bool border = true,
float rounding = 0.0f);
void ResetMenuButtonFrame();
void MenuHeading(const char* title, bool draw_line = true);
bool MenuHeadingButton(const char* title, const char* value = nullptr, bool enabled = true, bool draw_line = true);
bool ActiveButton(const char* title, bool is_active, bool enabled = true,
Expand Down

0 comments on commit ad96b1e

Please sign in to comment.