Replace preview music Play/Stop buttons with an icon button

This commit is contained in:
Stepland 2022-11-15 00:03:59 +01:00
parent da7bbaaeec
commit 46575cebdf
3 changed files with 53 additions and 3 deletions

View File

@ -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();
}
}

View File

@ -1,7 +1,10 @@
#include "imgui_extras.hpp"
#include <SFML/System/Vector2.hpp>
#include <imgui.h>
#include <imgui_stdlib.h>
#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
);
});
}

View File

@ -1,10 +1,12 @@
#pragma once
#include <SFML/System/Vector2.hpp>
#include <string>
#include <SFML/Graphics/Color.hpp>
#include <imgui-SFML_export.h>
#include <imgui.h>
#include <imgui_internal.h>
#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<class T>
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 {