BLT-With-Graphics-Template/include/blt/gfx/imgui/ImGuiUtils.h

305 lines
12 KiB
C
Raw Permalink Normal View History

2023-12-26 15:35:07 -05:00
#pragma once
2024-11-11 02:29:28 -05:00
#include "blt/gfx/font/IconsFontAwesome5.h" // from https://github.com/juliettef/IconFontCppHeaders
#include <blt/gfx/font/font_awesome_defines.h>
2023-12-26 21:14:01 -05:00
#include "imgui_spectrum.h"
2023-12-26 15:35:07 -05:00
#include <imgui.h>
2023-12-26 21:14:01 -05:00
#include <cmath>
2023-12-26 15:35:07 -05:00
namespace ImGui
{
2024-11-14 11:26:37 -05:00
namespace Spectrum
{
extern const unsigned int SourceSansProRegular_compressed_size;
extern const unsigned int SourceSansProRegular_compressed_data[]; // defined later in the file
}
2023-12-26 15:35:07 -05:00
2024-11-14 11:26:37 -05:00
inline void SetupImGuiStyle(bool bStyleDark_, float alpha_)
2023-12-26 15:35:07 -05:00
{
ImGuiStyle& style = ImGui::GetStyle();
// light style from Pacôme Danhiez (user itamago) https://github.com/ocornut/imgui/pull/511#issuecomment-175719267
style.Alpha = 1.0f;
style.FrameRounding = 3.0f;
2024-11-14 11:26:37 -05:00
style.Colors[ImGuiCol_Text] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f);
style.Colors[ImGuiCol_TextDisabled] = ImVec4(0.60f, 0.60f, 0.60f, 1.00f);
style.Colors[ImGuiCol_WindowBg] = ImVec4(0.94f, 0.94f, 0.94f, 0.94f);
2023-12-26 15:35:07 -05:00
//style.Colors[ImGuiCol_ChildWindowBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);
2024-11-14 11:26:37 -05:00
style.Colors[ImGuiCol_PopupBg] = ImVec4(1.00f, 1.00f, 1.00f, 0.94f);
style.Colors[ImGuiCol_Border] = ImVec4(0.00f, 0.00f, 0.00f, 0.39f);
style.Colors[ImGuiCol_BorderShadow] = ImVec4(1.00f, 1.00f, 1.00f, 0.10f);
style.Colors[ImGuiCol_FrameBg] = ImVec4(1.00f, 1.00f, 1.00f, 0.94f);
style.Colors[ImGuiCol_FrameBgHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.40f);
style.Colors[ImGuiCol_FrameBgActive] = ImVec4(0.26f, 0.59f, 0.98f, 0.67f);
style.Colors[ImGuiCol_TitleBg] = ImVec4(0.96f, 0.96f, 0.96f, 1.00f);
style.Colors[ImGuiCol_TitleBgCollapsed] = ImVec4(1.00f, 1.00f, 1.00f, 0.51f);
style.Colors[ImGuiCol_TitleBgActive] = ImVec4(0.82f, 0.82f, 0.82f, 1.00f);
style.Colors[ImGuiCol_MenuBarBg] = ImVec4(0.86f, 0.86f, 0.86f, 1.00f);
style.Colors[ImGuiCol_ScrollbarBg] = ImVec4(0.98f, 0.98f, 0.98f, 0.53f);
style.Colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.69f, 0.69f, 0.69f, 1.00f);
style.Colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.59f, 0.59f, 0.59f, 1.00f);
style.Colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.49f, 0.49f, 0.49f, 1.00f);
2023-12-26 15:35:07 -05:00
//style.Colors[ImGuiCol_ComboBg] = ImVec4(0.86f, 0.86f, 0.86f, 0.99f);
2024-11-14 11:26:37 -05:00
style.Colors[ImGuiCol_CheckMark] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f);
style.Colors[ImGuiCol_SliderGrab] = ImVec4(0.24f, 0.52f, 0.88f, 1.00f);
style.Colors[ImGuiCol_SliderGrabActive] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f);
style.Colors[ImGuiCol_Button] = ImVec4(0.26f, 0.59f, 0.98f, 0.40f);
style.Colors[ImGuiCol_ButtonHovered] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f);
style.Colors[ImGuiCol_ButtonActive] = ImVec4(0.06f, 0.53f, 0.98f, 1.00f);
style.Colors[ImGuiCol_Header] = ImVec4(0.26f, 0.59f, 0.98f, 0.31f);
style.Colors[ImGuiCol_HeaderHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.80f);
style.Colors[ImGuiCol_HeaderActive] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f);
2023-12-26 15:35:07 -05:00
//style.Colors[ImGuiCol_Column] = ImVec4(0.39f, 0.39f, 0.39f, 1.00f);
//style.Colors[ImGuiCol_ColumnHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.78f);
//style.Colors[ImGuiCol_ColumnActive] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f);
2024-11-14 11:26:37 -05:00
style.Colors[ImGuiCol_ResizeGrip] = ImVec4(1.00f, 1.00f, 1.00f, 0.50f);
style.Colors[ImGuiCol_ResizeGripHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.67f);
style.Colors[ImGuiCol_ResizeGripActive] = ImVec4(0.26f, 0.59f, 0.98f, 0.95f);
2023-12-26 15:35:07 -05:00
//style.Colors[ImGuiCol_CloseButton] = ImVec4(0.59f, 0.59f, 0.59f, 0.50f);
//style.Colors[ImGuiCol_CloseButtonHovered] = ImVec4(0.98f, 0.39f, 0.36f, 1.00f);
//style.Colors[ImGuiCol_CloseButtonActive] = ImVec4(0.98f, 0.39f, 0.36f, 1.00f);
2024-11-14 11:26:37 -05:00
style.Colors[ImGuiCol_PlotLines] = ImVec4(0.39f, 0.39f, 0.39f, 1.00f);
style.Colors[ImGuiCol_PlotLinesHovered] = ImVec4(1.00f, 0.43f, 0.35f, 1.00f);
style.Colors[ImGuiCol_PlotHistogram] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f);
style.Colors[ImGuiCol_PlotHistogramHovered] = ImVec4(1.00f, 0.60f, 0.00f, 1.00f);
style.Colors[ImGuiCol_TextSelectedBg] = ImVec4(0.26f, 0.59f, 0.98f, 0.35f);
2023-12-26 15:35:07 -05:00
//style.Colors[ImGuiCol_ModalWindowDarkening] = ImVec4(0.20f, 0.20f, 0.20f, 0.35f);
2024-11-14 11:26:37 -05:00
if (bStyleDark_)
2023-12-26 15:35:07 -05:00
{
for (int i = 0; i < ImGuiCol_COUNT; i++)
{
ImVec4& col = style.Colors[i];
float H, S, V;
2024-11-14 11:26:37 -05:00
ImGui::ColorConvertRGBtoHSV(col.x, col.y, col.z, H, S, V);
if (S < 0.1f)
2023-12-26 15:35:07 -05:00
{
2024-11-14 11:26:37 -05:00
V = 1.0f - V;
2023-12-26 15:35:07 -05:00
}
2024-11-14 11:26:37 -05:00
ImGui::ColorConvertHSVtoRGB(H, S, V, col.x, col.y, col.z);
if (col.w < 1.00f)
2023-12-26 15:35:07 -05:00
{
col.w *= alpha_;
}
}
2024-11-14 11:26:37 -05:00
} else
2023-12-26 15:35:07 -05:00
{
for (int i = 0; i < ImGuiCol_COUNT; i++)
{
ImVec4& col = style.Colors[i];
2024-11-14 11:26:37 -05:00
if (col.w < 1.00f)
2023-12-26 15:35:07 -05:00
{
col.x *= alpha_;
col.y *= alpha_;
col.z *= alpha_;
col.w *= alpha_;
}
}
}
}
2024-11-14 11:26:37 -05:00
inline bool CheckBoxFont(const char* name_, bool* pB_, const char* pOn_ = "[X]", const char* pOff_ = "[ ]")
2023-12-26 15:35:07 -05:00
{
2024-11-14 11:26:37 -05:00
if (*pB_)
2023-12-26 15:35:07 -05:00
{
ImGui::Text("%s", pOn_);
2024-11-14 11:26:37 -05:00
} else
2023-12-26 15:35:07 -05:00
{
ImGui::Text("%s", pOff_);
}
bool bHover = false;
bHover = bHover || ImGui::IsItemHovered();
ImGui::SameLine();
2024-11-14 11:26:37 -05:00
ImGui::Text("%s", name_);
2023-12-26 15:35:07 -05:00
bHover = bHover || ImGui::IsItemHovered();
2024-11-14 11:26:37 -05:00
if (bHover && ImGui::IsMouseClicked(0))
2023-12-26 15:35:07 -05:00
{
2024-11-14 11:26:37 -05:00
*pB_ = !*pB_;
2023-12-26 15:35:07 -05:00
return true;
}
return false;
}
2024-11-14 11:26:37 -05:00
inline bool CheckBoxTick(const char* name_, bool* pB_)
2023-12-26 15:35:07 -05:00
{
2024-11-14 11:26:37 -05:00
return CheckBoxFont(name_, pB_, ICON_FA_CHECK_SQUARE, ICON_FA_SQUARE);
2023-12-26 15:35:07 -05:00
}
2024-11-14 11:26:37 -05:00
inline bool MenuItemCheckbox(const char* name_, bool* pB_)
2023-12-26 15:35:07 -05:00
{
2024-11-14 11:26:37 -05:00
bool retval = ImGui::MenuItem(name_);
2023-12-26 15:35:07 -05:00
ImGui::SameLine();
2024-11-14 11:26:37 -05:00
if (*pB_)
2023-12-26 15:35:07 -05:00
{
ImGui::Text(ICON_FA_CHECK_SQUARE);
2024-11-14 11:26:37 -05:00
} else
2023-12-26 15:35:07 -05:00
{
ImGui::Text(ICON_FA_SQUARE);
}
2024-11-14 11:26:37 -05:00
if (retval)
2023-12-26 15:35:07 -05:00
{
2024-11-14 11:26:37 -05:00
*pB_ = !*pB_;
2023-12-26 15:35:07 -05:00
}
return retval;
}
2024-11-14 11:26:37 -05:00
2023-12-26 15:35:07 -05:00
struct FrameTimeHistogram
{
// configuration params - modify these at will
static const int NUM = 101; //last value is from T-1 to inf.
2024-11-14 11:26:37 -05:00
float dT = 0.001f; // in seconds, default 1ms
float refresh = 1.0f / 60.0f;// set this to your target refresh rate
2023-12-26 15:35:07 -05:00
static const int NUM_MARKERS = 2;
2024-11-14 11:26:37 -05:00
float markers[NUM_MARKERS] = {0.99f, 0.999f};
2023-12-26 15:35:07 -05:00
// data
2024-11-14 11:26:37 -05:00
ImVec2 size = ImVec2(3.0f * NUM, 40.0f);
float lastdT = 0.0f;
float timesTotal;
float countsTotal;
float times[NUM];
float counts[NUM];
float hitchTimes[NUM];
float hitchCounts[NUM];
2023-12-26 15:35:07 -05:00
FrameTimeHistogram()
{
Clear();
}
2024-11-14 11:26:37 -05:00
2023-12-26 15:35:07 -05:00
void Clear()
{
2024-11-14 11:26:37 -05:00
timesTotal = 0.0f;
2023-12-26 15:35:07 -05:00
countsTotal = 0.0f;
2024-11-14 11:26:37 -05:00
memset(times, 0, sizeof(times));
memset(counts, 0, sizeof(counts));
memset(hitchTimes, 0, sizeof(hitchTimes));
memset(hitchCounts, 0, sizeof(hitchCounts));
2023-12-26 15:35:07 -05:00
}
2024-11-14 11:26:37 -05:00
int GetBin(float time_)
2023-12-26 15:35:07 -05:00
{
2024-11-14 11:26:37 -05:00
int bin = (int) std::floor(time_ / dT);
if (bin >= NUM)
2023-12-26 15:35:07 -05:00
{
bin = NUM - 1;
}
return bin;
}
2024-11-14 11:26:37 -05:00
void Update(float deltaT_)
2023-12-26 15:35:07 -05:00
{
2024-11-14 11:26:37 -05:00
if (deltaT_ < 0.0f)
2023-12-26 15:35:07 -05:00
{
assert(false);
return;
}
2024-11-14 11:26:37 -05:00
int bin = GetBin(deltaT_);
times[bin] += deltaT_;
timesTotal += deltaT_;
2023-12-26 15:35:07 -05:00
counts[bin] += 1.0f;
countsTotal += 1.0f;
2024-11-14 11:26:37 -05:00
float hitch = abs(lastdT - deltaT_);
int deltaBin = GetBin(hitch);
hitchTimes[deltaBin] += hitch;
2023-12-26 15:35:07 -05:00
hitchCounts[deltaBin] += 1.0f;
lastdT = deltaT_;
}
2024-11-14 11:26:37 -05:00
void PlotRefreshLines(float total_ = 0.0f, float* pValues_ = NULL)
2023-12-26 15:35:07 -05:00
{
ImDrawList* draw = ImGui::GetWindowDrawList();
const ImGuiStyle& style = ImGui::GetStyle();
2024-11-14 11:26:37 -05:00
ImVec2 pad = style.FramePadding;
ImVec2 min = ImGui::GetItemRectMin();
2023-12-26 15:35:07 -05:00
min.x += pad.x;
2024-11-14 11:26:37 -05:00
ImVec2 max = ImGui::GetItemRectMax();
2023-12-26 15:35:07 -05:00
max.x -= pad.x;
2024-11-14 11:26:37 -05:00
float xRefresh = (max.x - min.x) * refresh / (dT * NUM);
float xCurr = xRefresh + min.x;
while (xCurr < max.x)
2023-12-26 15:35:07 -05:00
{
2024-11-14 11:26:37 -05:00
float xP = ceil(xCurr); // use ceil to get integer coords or else lines look odd
draw->AddLine(ImVec2(xP, min.y), ImVec2(xP, max.y), 0x50FFFFFF);
2023-12-26 15:35:07 -05:00
xCurr += xRefresh;
}
2024-11-14 11:26:37 -05:00
if (pValues_)
2023-12-26 15:35:07 -05:00
{
// calc markers
float currTotal = 0.0f;
2024-11-14 11:26:37 -05:00
int mark = 0;
for (int i = 0; i < NUM && mark < NUM_MARKERS; ++i)
2023-12-26 15:35:07 -05:00
{
currTotal += pValues_[i];
2024-11-14 11:26:37 -05:00
if (total_ * markers[mark] < currTotal)
2023-12-26 15:35:07 -05:00
{
2024-11-14 11:26:37 -05:00
float xP = ceil(
(float) (i + 1) / (float) NUM * (max.x - min.x) + min.x); // use ceil to get integer coords or else lines look odd
draw->AddLine(ImVec2(xP, min.y), ImVec2(xP, max.y), 0xFFFF0000);
2023-12-26 15:35:07 -05:00
++mark;
}
}
}
}
2024-11-14 11:26:37 -05:00
void CalcHistogramSize(int numShown_)
2023-12-26 15:35:07 -05:00
{
ImVec2 wRegion = ImGui::GetContentRegionMax();
float heightGone = 7.0f * ImGui::GetTextLineHeightWithSpacing();
wRegion.y -= heightGone;
wRegion.y /= (float) numShown_;
const ImGuiStyle& style = ImGui::GetStyle();
2024-11-14 11:26:37 -05:00
ImVec2 pad = style.FramePadding;
2023-12-26 15:35:07 -05:00
wRegion.x -= 2.0f * pad.x;
size = wRegion;
}
2024-11-14 11:26:37 -05:00
void Draw(const char* name_, bool* pOpen_ = NULL)
2023-12-26 15:35:07 -05:00
{
2024-11-14 11:26:37 -05:00
if (ImGui::Begin(name_, pOpen_))
2023-12-26 15:35:07 -05:00
{
int numShown = 0;
2024-11-14 11:26:37 -05:00
if (ImGui::CollapsingHeader("Time Histogram"))
2023-12-26 15:35:07 -05:00
{
++numShown;
2024-11-14 11:26:37 -05:00
ImGui::PlotHistogram("", times, NUM, 0, NULL, FLT_MAX, FLT_MAX, size);
PlotRefreshLines(timesTotal, times);
2023-12-26 15:35:07 -05:00
}
2024-11-14 11:26:37 -05:00
if (ImGui::CollapsingHeader("Count Histogram"))
2023-12-26 15:35:07 -05:00
{
++numShown;
2024-11-14 11:26:37 -05:00
ImGui::PlotHistogram("", counts, NUM, 0, NULL, FLT_MAX, FLT_MAX, size);
PlotRefreshLines(countsTotal, counts);
2023-12-26 15:35:07 -05:00
}
2024-11-14 11:26:37 -05:00
if (ImGui::CollapsingHeader("Hitch Time Histogram"))
2023-12-26 15:35:07 -05:00
{
++numShown;
2024-11-14 11:26:37 -05:00
ImGui::PlotHistogram("", hitchTimes, NUM, 0, NULL, FLT_MAX, FLT_MAX, size);
2023-12-26 15:35:07 -05:00
PlotRefreshLines();
}
2024-11-14 11:26:37 -05:00
if (ImGui::CollapsingHeader("Hitch Count Histogram"))
2023-12-26 15:35:07 -05:00
{
++numShown;
2024-11-14 11:26:37 -05:00
ImGui::PlotHistogram("", hitchCounts, NUM, 0, NULL, FLT_MAX, FLT_MAX, size);
2023-12-26 15:35:07 -05:00
PlotRefreshLines();
}
2024-11-14 11:26:37 -05:00
if (ImGui::Button("Clear"))
2023-12-26 15:35:07 -05:00
{
Clear();
}
2024-11-14 11:26:37 -05:00
CalcHistogramSize(numShown);
2023-12-26 15:35:07 -05:00
}
ImGui::End();
}
};
2024-11-14 11:26:37 -05:00
2023-12-26 15:35:07 -05:00
};