yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
implot_support.cc
Go to the documentation of this file.
2
3#include <vector>
4
5#include "absl/strings/str_format.h"
9
10namespace yaze {
11namespace gui {
12namespace plotting {
13namespace {
14
15ImVec4 WithAlpha(const ImVec4& color, float alpha) {
16 return ImVec4(color.x, color.y, color.z, color.w * alpha);
17}
18
19bool IsCloseColor(const ImVec4& a, const ImVec4& b) {
20 const float dr = a.x - b.x;
21 const float dg = a.y - b.y;
22 const float db = a.z - b.z;
23 return dr * dr + dg * dg + db * db < 0.15f * 0.15f;
24}
25
26// Series 0 keeps the theme's plot_lines color; later series take ImPlot's
27// Deep palette so multi-series plots stay distinguishable. ImPlot keeps
28// colormaps for the life of the context and asserts on a reused name, and
29// the list depends only on plot_lines, so the name is keyed on that color.
30ImPlotColormap ThemeColormap(const Theme& theme) {
31 const ImVec4 primary =
32 WithAlpha(ConvertColorToImVec4(theme.plot_lines), 1.0f);
33 const std::string name = absl::StrFormat(
34 "yaze_theme_%08X", ImGui::ColorConvertFloat4ToU32(primary));
35 const ImPlotColormap existing = ImPlot::GetColormapIndex(name.c_str());
36 if (existing != -1) {
37 return existing;
38 }
39
40 std::vector<ImVec4> colors = {primary};
41 const int deep_size = ImPlot::GetColormapSize(ImPlotColormap_Deep);
42 for (int i = 0; i < deep_size; ++i) {
43 const ImVec4 color = ImPlot::GetColormapColor(i, ImPlotColormap_Deep);
44 if (!IsCloseColor(color, primary)) {
45 colors.push_back(color);
46 }
47 }
48 return ImPlot::AddColormap(name.c_str(), colors.data(),
49 static_cast<int>(colors.size()), true);
50}
51
52ImVec2 ScaledSpacing(float multiplier) {
53 const float spacing = LayoutHelpers::GetStandardSpacing();
54 return ImVec2(spacing * multiplier, spacing * multiplier);
55}
56
57ImPlotStyle BuildBaseStyle(const Theme& theme, const PlotStyleConfig& config) {
58 ImPlotStyle style;
59 style.DigitalSpacing = ImGui::GetStyle().ItemSpacing.y * 0.5f;
60 style.PlotBorderSize = 1.0f;
61 style.MinorAlpha = config.grid_alpha;
62 style.MajorTickLen = ImVec2(6.0f, 6.0f);
63 style.MinorTickLen = ImVec2(3.0f, 3.0f);
64 style.MajorTickSize = ImVec2(1.0f, 1.0f);
65 style.MinorTickSize = ImVec2(1.0f, 1.0f);
66 style.MajorGridSize = ImVec2(1.0f, 1.0f);
67 style.MinorGridSize = ImVec2(1.0f, 1.0f);
68 style.PlotPadding = ScaledSpacing(1.5f);
69 style.LabelPadding = ScaledSpacing(0.75f);
70 style.LegendPadding = ScaledSpacing(1.0f);
71 style.LegendInnerPadding = ScaledSpacing(0.5f);
72 style.LegendSpacing = ImVec2(LayoutHelpers::GetStandardSpacing(),
74 style.MousePosPadding = ScaledSpacing(1.0f);
75 style.AnnotationPadding = ScaledSpacing(0.35f);
76 style.FitPadding = ImVec2(0.05f, 0.05f);
77 style.PlotDefaultSize = ImVec2(420, 280);
78 style.PlotMinSize = ImVec2(220, 160);
79 style.UseISO8601 = config.use_iso_8601;
80 style.Use24HourClock = config.use_24h_clock;
81 style.UseLocalTime = true;
82
83 style.Colormap = ThemeColormap(theme);
84 style.Colors[ImPlotCol_FrameBg] =
85 WithAlpha(ConvertColorToImVec4(theme.surface), config.background_alpha);
86 style.Colors[ImPlotCol_PlotBg] =
87 WithAlpha(ConvertColorToImVec4(theme.child_bg), config.background_alpha);
88 style.Colors[ImPlotCol_PlotBorder] =
89 WithAlpha(ConvertColorToImVec4(theme.border), 1.0f);
90 style.Colors[ImPlotCol_LegendBg] =
91 WithAlpha(ConvertColorToImVec4(theme.popup_bg), config.background_alpha);
92 style.Colors[ImPlotCol_LegendBorder] =
93 WithAlpha(ConvertColorToImVec4(theme.border), 1.0f);
94 style.Colors[ImPlotCol_LegendText] =
95 WithAlpha(ConvertColorToImVec4(theme.text_primary), 1.0f);
96 style.Colors[ImPlotCol_TitleText] =
97 WithAlpha(ConvertColorToImVec4(theme.text_primary), 1.0f);
98 style.Colors[ImPlotCol_InlayText] =
99 WithAlpha(ConvertColorToImVec4(theme.text_secondary), 1.0f);
100 style.Colors[ImPlotCol_AxisText] =
101 WithAlpha(ConvertColorToImVec4(theme.text_secondary), 0.95f);
102 style.Colors[ImPlotCol_AxisGrid] =
103 WithAlpha(ConvertColorToImVec4(theme.editor_grid), config.grid_alpha);
104 style.Colors[ImPlotCol_AxisTick] =
105 WithAlpha(ConvertColorToImVec4(theme.text_secondary), 0.6f);
106 style.Colors[ImPlotCol_AxisBg] =
107 WithAlpha(ConvertColorToImVec4(theme.child_bg), config.background_alpha);
108 style.Colors[ImPlotCol_AxisBgHovered] =
109 WithAlpha(ConvertColorToImVec4(theme.hover_highlight), 0.65f);
110 style.Colors[ImPlotCol_AxisBgActive] =
111 WithAlpha(ConvertColorToImVec4(theme.active_selection), 0.65f);
112 style.Colors[ImPlotCol_Selection] =
113 WithAlpha(ConvertColorToImVec4(theme.accent), 0.25f);
114 style.Colors[ImPlotCol_Crosshairs] =
115 WithAlpha(ConvertColorToImVec4(theme.editor_cursor), 0.75f);
116
117 return style;
118}
119
120ImPlotSpec BuildBaseItemSpec(const Theme& /*theme*/,
121 const PlotStyleConfig& config) {
122 // Line, fill and marker colors stay IMPLOT_AUTO_COL so each series takes the
123 // next entry of the theme colormap. Forcing them made every series in a plot
124 // the same color. Items that need a fixed color copy this spec and set it.
125 ImPlotSpec spec;
126 spec.LineWeight = config.line_weight;
127 spec.FillAlpha = config.fill_alpha;
128 spec.MarkerSize = config.marker_size;
129 spec.Size = ImGui::GetFontSize() * 0.85f; // digital bar height
130 return spec;
131}
132
133} // namespace
134
136 if (ImPlot::GetCurrentContext() == nullptr) {
137 ImPlot::CreateContext();
138 }
139}
140
141ImPlotStyle BuildStyleFromTheme(const Theme& theme,
142 const PlotStyleConfig& config) {
144 return BuildBaseStyle(theme, config);
145}
146
147ImPlotSpec BuildItemSpecFromTheme(const Theme& theme,
148 const PlotStyleConfig& config) {
149 return BuildBaseItemSpec(theme, config);
150}
151
153 const PlotStyleConfig& config)
154 : previous_style_(), item_spec_(BuildBaseItemSpec(theme, config)) {
156 previous_style_ = ImPlot::GetStyle();
157 ImPlot::GetStyle() = BuildBaseStyle(theme, config);
158}
159
161 ImPlot::GetStyle() = previous_style_;
162}
163
166 should_end_ = ImPlot::BeginPlot(config.id.c_str(), config.size, config.flags);
167 if (should_end_) {
168 ImPlot::SetupAxes(config.x_label, config.y_label, config.x_axis_flags,
169 config.y_axis_flags);
170 }
171}
172
174 if (should_end_) {
175 ImPlot::EndPlot();
176 }
177}
178
179} // namespace plotting
180} // namespace gui
181} // namespace yaze
static float GetStandardSpacing()
PlotGuard(const PlotConfig &config)
PlotStyleScope(const Theme &theme, const PlotStyleConfig &config={})
bool IsCloseColor(const ImVec4 &a, const ImVec4 &b)
ImPlotStyle BuildBaseStyle(const Theme &theme, const PlotStyleConfig &config)
ImPlotSpec BuildBaseItemSpec(const Theme &, const PlotStyleConfig &config)
ImPlotSpec BuildItemSpecFromTheme(const Theme &theme, const PlotStyleConfig &config)
ImPlotStyle BuildStyleFromTheme(const Theme &theme, const PlotStyleConfig &config)
ImVec4 ConvertColorToImVec4(const Color &color)
Definition color.h:140
Comprehensive theme structure for YAZE.