big time refactor + button styles
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
|
||||
#include "ImguiBase.h"
|
||||
|
||||
// hacky inlcude for custom button alignment
|
||||
#include "imgui_internal.h"
|
||||
|
||||
#include "imgui_impl_glfw.h"
|
||||
#include "imgui_impl_vulkan.h"
|
||||
|
||||
@@ -568,4 +571,88 @@ ImVec2 getWindowSize(GLFWwindow* window)
|
||||
int window_height;
|
||||
glfwGetWindowSize(window, &window_width, &window_height);
|
||||
return ImVec2(window_width, window_height);
|
||||
}
|
||||
}
|
||||
|
||||
void dropShadow()
|
||||
{
|
||||
dropShadow(DEFAULT_DROP_SHADOW_SIZE, ImGui::GetStyle().Colors[DEFAULT_DROP_SHADOW_COLOR_ID]);
|
||||
}
|
||||
|
||||
void dropShadow(const float shadowSize, const ImColor& color)
|
||||
{
|
||||
ImVec2 lastMin = ImGui::GetItemRectMin();
|
||||
ImVec2 lastMax = ImGui::GetItemRectMax();
|
||||
renderDropShadow(shadowSize, lastMin, lastMax, color);
|
||||
}
|
||||
|
||||
void renderDropShadow(const float shadowSize, const ImVec2& min, const ImVec2& max, const ImColor& color)
|
||||
{
|
||||
ImDrawList* windowDrawList = ImGui::GetWindowDrawList();
|
||||
windowDrawList->AddQuadFilled(
|
||||
{ min.x, max.y },
|
||||
{ max.x, max.y },
|
||||
{ max.x + shadowSize, max.y + shadowSize },
|
||||
{ min.x + shadowSize, max.y + shadowSize },
|
||||
color);
|
||||
windowDrawList->AddQuadFilled(
|
||||
{ max.x, min.y },
|
||||
{ max.x + shadowSize, min.y + shadowSize },
|
||||
{ max.x + shadowSize, max.y + shadowSize },
|
||||
{ max.x, max.y },
|
||||
color);
|
||||
}
|
||||
|
||||
bool dropButton(const char* label, const ImVec2& size, float shadowSize, bool smallButton)
|
||||
{
|
||||
ImGuiStyle style = ImGui::GetStyle();
|
||||
ImVec2 backupPadding = style.FramePadding;
|
||||
if (smallButton) style.FramePadding.y = 0.f;
|
||||
|
||||
const ImVec2 labelSize = ImGui::CalcTextSize(label, NULL, true);
|
||||
ImVec2 buttonMin = ImGui::GetCursorScreenPos();
|
||||
|
||||
ImVec2 buttonSize = size;
|
||||
if (buttonSize.x <= 0.f) buttonSize.x = labelSize.x + style.FramePadding.x * 2.0f;
|
||||
if (buttonSize.y <= 0.f) buttonSize.y = labelSize.y + style.FramePadding.y * 2.0f;
|
||||
|
||||
float baseLineOffset = ImGui::GetCurrentWindowRead()->DC.CurrLineTextBaseOffset;
|
||||
if (smallButton && style.FramePadding.y < baseLineOffset) buttonMin.y += baseLineOffset - style.FramePadding.y;
|
||||
|
||||
bool result = ImGui::InvisibleButton(label, buttonSize);
|
||||
bool held = ImGui::IsItemActive();
|
||||
bool hovered = ImGui::IsItemHovered();
|
||||
const ImU32 col = ImGui::GetColorU32((held && hovered) ? ImGuiCol_ButtonActive : hovered ? ImGuiCol_ButtonHovered : ImGuiCol_Button);
|
||||
|
||||
float maxOffset = std::fmaxf(shadowSize - 1.f, 0.f);
|
||||
ImVec2 offset = held ? ImVec2{ maxOffset, maxOffset } : ImVec2{ 0.f, 0.f };
|
||||
ImVec2 rectMin = buttonMin + offset;
|
||||
ImVec2 rectMax = buttonMin + buttonSize + offset;
|
||||
|
||||
ImGui::RenderFrame(rectMin, rectMax, col, true, style.FrameRounding);
|
||||
ImGui::RenderTextClipped(rectMin, rectMax, label, NULL, &labelSize, style.ButtonTextAlign);
|
||||
renderDropShadow(held ? 1.f : shadowSize, rectMin, rectMax, ImGui::GetStyle().Colors[DEFAULT_DROP_SHADOW_COLOR_ID]);
|
||||
|
||||
style.FramePadding = backupPadding;
|
||||
return result;
|
||||
}
|
||||
|
||||
bool checkboxWithDropShadow(const char* label, bool* v, const float shadowSize)
|
||||
{
|
||||
ImGuiStyle& style = ImGui::GetStyle();
|
||||
float oldSpacingX = style.ItemInnerSpacing.x;
|
||||
style.ItemInnerSpacing.x += shadowSize;
|
||||
bool result = ImGui::Checkbox(label, v);
|
||||
style.ItemInnerSpacing.x = oldSpacingX;
|
||||
|
||||
ImVec2 lastMin = ImGui::GetItemRectMin();
|
||||
renderDropShadow(shadowSize, lastMin, lastMin + ImVec2{ ImGui::GetFrameHeight(), ImGui::GetFrameHeight() }, ImGui::GetStyle().Colors[DEFAULT_DROP_SHADOW_COLOR_ID]);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
ImVec2 operator +(const ImVec2& a, const ImVec2& b) { return ImVec2{ a.x + b.x, a.y + b.y }; }
|
||||
ImVec2 operator +(const ImVec2& a, const float s) { return ImVec2{ a.x + s, a.y + s }; }
|
||||
ImVec2 operator -(const ImVec2& a, const ImVec2& b) { return ImVec2{ a.x - b.x, a.y - b.y }; }
|
||||
ImVec2 operator -(const ImVec2& a, const float s) { return ImVec2{ a.x - s, a.y - s }; }
|
||||
ImVec2 operator *(const ImVec2& a, float s) { return ImVec2{ a.x * s, a.y * s }; }
|
||||
ImVec2 operator /(const ImVec2& a, float s) { return ImVec2{ a.x / s, a.y / s }; }
|
||||
|
||||
@@ -57,3 +57,29 @@ public:
|
||||
int startImgui(ImGuiCallbacks& callbacks, const char* title, int windowWidth, int windowHeight);
|
||||
|
||||
ImVec2 getWindowSize(GLFWwindow* window);
|
||||
|
||||
constexpr float DEFAULT_DROP_SHADOW_SIZE = 3.f;
|
||||
constexpr ImGuiCol DEFAULT_DROP_SHADOW_COLOR_ID = ImGuiCol_TabUnfocused;
|
||||
|
||||
void dropShadow();
|
||||
void dropShadow(const float shadowSize, const ImColor& color);
|
||||
void renderDropShadow(const float shadowSize, const ImVec2& min, const ImVec2& max, const ImColor& color);
|
||||
|
||||
bool dropButton(const char* label, const ImVec2& size = ImVec2(0, 0), float shadowSize = DEFAULT_DROP_SHADOW_SIZE, bool smallButton = false);
|
||||
bool checkboxWithDropShadow(const char* label, bool* v, const float shadowSize = DEFAULT_DROP_SHADOW_SIZE);
|
||||
|
||||
template <typename T>
|
||||
float calcInputWidth(const char* formatStr, T value)
|
||||
{
|
||||
constexpr size_t MAX_BUFFER_SIZE = 256;
|
||||
char formatStrBuffer[MAX_BUFFER_SIZE];
|
||||
snprintf(formatStrBuffer, MAX_BUFFER_SIZE, formatStr, value);
|
||||
return ImGui::CalcTextSize(formatStrBuffer).x + ImGui::GetStyle().FramePadding.x * 2.f;
|
||||
}
|
||||
|
||||
ImVec2 operator +(const ImVec2& a, const ImVec2& b);
|
||||
ImVec2 operator +(const ImVec2& a, const float s);
|
||||
ImVec2 operator -(const ImVec2& a, const ImVec2& b);
|
||||
ImVec2 operator -(const ImVec2& a, const float s);
|
||||
ImVec2 operator *(const ImVec2& a, float s);
|
||||
ImVec2 operator /(const ImVec2& a, float s);
|
||||
|
||||
Reference in New Issue
Block a user