From 46575cebdf654d8de790ca522b8b70d79d03c39a Mon Sep 17 00:00:00 2001 From: Stepland <10530295-Buggyroom@users.noreply.gitlab.com> Date: Tue, 15 Nov 2022 00:03:59 +0100 Subject: [PATCH] Replace preview music Play/Stop buttons with an icon button --- src/editor_state.cpp | 4 ++-- src/imgui_extras.cpp | 16 +++++++++++++++- src/imgui_extras.hpp | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/src/editor_state.cpp b/src/editor_state.cpp index 1cc79a1..7921d91 100644 --- a/src/editor_state.cpp +++ b/src/editor_state.cpp @@ -682,11 +682,11 @@ void EditorState::display_file_properties() { } } if (music_preview_is_playing()) { - if (ImGui::Button("Stop##Mucic Preview")) { + if (feis::StopButton("##Stop Mucic Preview")) { stop_music_preview(); } } else { - if (ImGui::Button("Play##Music Preview")) { + if (ImGui::ArrowButton("##Play Music Preview", ImGuiDir_Right)) { play_music_preview(); } } diff --git a/src/imgui_extras.cpp b/src/imgui_extras.cpp index 270c9c5..7af3c5d 100644 --- a/src/imgui_extras.cpp +++ b/src/imgui_extras.cpp @@ -1,7 +1,10 @@ #include "imgui_extras.hpp" +#include +#include + #include -#include "imgui.h" +#include "imgui_internal.h" bool feis::ColorEdit4(const char* label, sf::Color& col, ImGuiColorEditFlags flags) { float array_col[4] = { @@ -87,3 +90,14 @@ void feis::HelpMarker(const char* desc) { ImGui::EndTooltip(); } } + +bool feis::StopButton(const char* str_id) { + return IconButton(str_id, [](const ImRect& bb, const ImU32& color){ + const auto size = sf::Vector2f{bb.GetSize()}; + ImGui::GetWindowDrawList()->AddRectFilled( + sf::Vector2f{bb.Min} + size / 4.f, + sf::Vector2f{bb.Max} - size / 4.f + sf::Vector2f{1.f, 1.f}, + color + ); + }); +} diff --git a/src/imgui_extras.hpp b/src/imgui_extras.hpp index 3530f27..11192ae 100644 --- a/src/imgui_extras.hpp +++ b/src/imgui_extras.hpp @@ -1,10 +1,12 @@ #pragma once +#include #include #include #include #include +#include #include "special_numeric_types.hpp" @@ -23,6 +25,40 @@ namespace feis { const ImGuiInputTextFlags flags = ImGuiInputTextFlags_None ); void HelpMarker(const char* desc); + bool StopButton(const char* str_id); + + template + bool IconButton(const char* str_id, const T& draw_icon) { + float sz = ImGui::GetFrameHeight(); + sf::Vector2f size{sz, sz}; + ImGuiButtonFlags flags = ImGuiButtonFlags_None; + auto window = ImGui::GetCurrentWindow(); + if (window->SkipItems) { + return false; + } + + ImGuiContext& g = *GImGui; + const ImGuiID id = window->GetID(str_id); + const ImRect bb(window->DC.CursorPos, sf::Vector2f(window->DC.CursorPos) + size); + const float default_size = ImGui::GetFrameHeight(); + ImGui::ItemSize(size, (size.y >= default_size) ? g.Style.FramePadding.y : -1.0f); + if (!ImGui::ItemAdd(bb, id)) + return false; + + if (g.LastItemData.InFlags & ImGuiItemFlags_ButtonRepeat) + flags |= ImGuiButtonFlags_Repeat; + + bool hovered, held; + bool pressed = ImGui::ButtonBehavior(bb, id, &hovered, &held, flags); + + // Render + const ImU32 bg_col = ImGui::GetColorU32((held && hovered) ? ImGuiCol_ButtonActive : hovered ? ImGuiCol_ButtonHovered : ImGuiCol_Button); + const ImU32 text_col = ImGui::GetColorU32(ImGuiCol_Text); + ImGui::RenderNavHighlight(bb, id); + ImGui::RenderFrame(bb.Min, bb.Max, bg_col, true, g.Style.FrameRounding); + draw_icon(bb, text_col); + return pressed; + } } namespace colors {