| 1 |
/** |
| 2 |
* Furnace Tracker - multi-system chiptune tracker |
| 3 |
* Copyright (C) 2021-2025 tildearrow and contributors |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 2 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License along |
| 16 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 17 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
// portions based on imgui_widgets.cpp |
| 21 |
|
| 22 |
#ifndef IMGUI_DEFINE_MATH_OPERATORS |
| 23 |
#define IMGUI_DEFINE_MATH_OPERATORS |
| 24 |
#endif |
| 25 |
#include "plot_nolerp.h" |
| 26 |
#include "imgui.h" |
| 27 |
#include "imgui_internal.h" |
| 28 |
|
| 29 |
#include "../ta-utils.h" |
| 30 |
|
| 31 |
struct FurnacePlotArrayGetterData |
| 32 |
{ |
| 33 |
const float* Values; |
| 34 |
int Stride; |
| 35 |
|
| 36 |
FurnacePlotArrayGetterData(const float* values, int stride) { Values = values; Stride = stride; } |
| 37 |
}; |
| 38 |
|
| 39 |
static float Plot_ArrayGetter(void* data, int idx) |
| 40 |
{ |
| 41 |
FurnacePlotArrayGetterData* plot_data = (FurnacePlotArrayGetterData*)data; |
| 42 |
const float v = *(const float*)(const void*)((const unsigned char*)plot_data->Values + (size_t)idx * plot_data->Stride); |
| 43 |
return v; |
| 44 |
} |
| 45 |
|
| 46 |
struct FurnacePlotIntArrayGetterData |
| 47 |
{ |
| 48 |
const int* Values; |
| 49 |
int Stride; |
| 50 |
|
| 51 |
FurnacePlotIntArrayGetterData(const int* values, int stride) { Values = values; Stride = stride; } |
| 52 |
}; |
| 53 |
|
| 54 |
static int Plot_IntArrayGetter(void* data, int idx) |
| 55 |
{ |
| 56 |
FurnacePlotIntArrayGetterData* plot_data = (FurnacePlotIntArrayGetterData*)data; |
| 57 |
const int v = *(const int*)(const void*)((const unsigned char*)plot_data->Values + (size_t)idx * plot_data->Stride); |
| 58 |
return v; |
| 59 |
} |
| 60 |
|
| 61 |
int PlotNoLerpEx(ImGuiPlotType plot_type, const char* label, float (*values_getter)(void* data, int idx), void* data, int values_count, int values_offset, const char* overlay_text, float scale_min, float scale_max, ImVec2 frame_size) |
| 62 |
{ |
| 63 |
ImGuiContext& g = *GImGui; |
| 64 |
ImGuiWindow* window = ImGui::GetCurrentWindow(); |
| 65 |
if (window->SkipItems) |
| 66 |
return -1; |
| 67 |
|
| 68 |
const ImGuiStyle& style = g.Style; |
| 69 |
const ImGuiID id = window->GetID(label); |
| 70 |
|
| 71 |
const ImVec2 label_size = ImGui::CalcTextSize(label, NULL, true); |
| 72 |
if (frame_size.x == 0.0f) |
| 73 |
frame_size.x = ImGui::CalcItemWidth(); |
| 74 |
if (frame_size.y == 0.0f) |
| 75 |
frame_size.y = label_size.y + (style.FramePadding.y * 2); |
| 76 |
|
| 77 |
const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + frame_size); |
| 78 |
const ImRect inner_bb(frame_bb.Min + style.FramePadding, frame_bb.Max - style.FramePadding); |
| 79 |
const ImRect total_bb(frame_bb.Min, frame_bb.Max + ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0)); |
| 80 |
ImGui::ItemSize(total_bb, style.FramePadding.y); |
| 81 |
if (!ImGui::ItemAdd(total_bb, 0, &frame_bb, ImGuiItemFlags_NoInertialScroll)) |
| 82 |
return -1; |
| 83 |
const bool hovered = ImGui::ItemHoverable(frame_bb, id, 0); |
| 84 |
|
| 85 |
// Determine scale from values if not specified |
| 86 |
if (scale_min == FLT_MAX || scale_max == FLT_MAX) |
| 87 |
{ |
| 88 |
float v_min = FLT_MAX; |
| 89 |
float v_max = -FLT_MAX; |
| 90 |
for (int i = 0; i < values_count; i++) |
| 91 |
{ |
| 92 |
const float v = values_getter(data, i); |
| 93 |
if (v != v) // Ignore NaN values |
| 94 |
continue; |
| 95 |
v_min = ImMin(v_min, v); |
| 96 |
v_max = ImMax(v_max, v); |
| 97 |
} |
| 98 |
if (scale_min == FLT_MAX) |
| 99 |
scale_min = v_min; |
| 100 |
if (scale_max == FLT_MAX) |
| 101 |
scale_max = v_max; |
| 102 |
} |
| 103 |
|
| 104 |
ImGui::RenderFrame(frame_bb.Min, frame_bb.Max, ImGui::GetColorU32(ImGuiCol_FrameBg), true, style.FrameRounding); |
| 105 |
|
| 106 |
const int values_count_min = (plot_type == ImGuiPlotType_Lines) ? 2 : 1; |
| 107 |
int idx_hovered = -1; |
| 108 |
if (values_count >= values_count_min) |
| 109 |
{ |
| 110 |
int res_w = ImMin((int)frame_size.x, values_count) + ((plot_type == ImGuiPlotType_Lines) ? -1 : 0); |
| 111 |
int item_count = values_count + ((plot_type == ImGuiPlotType_Lines) ? -1 : 0); |
| 112 |
|
| 113 |
// Tooltip on hover |
| 114 |
if (hovered && inner_bb.Contains(g.IO.MousePos)) |
| 115 |
{ |
| 116 |
const float t = ImClamp((g.IO.MousePos.x - inner_bb.Min.x) / (inner_bb.Max.x - inner_bb.Min.x), 0.0f, 0.9999f); |
| 117 |
const int v_idx = (int)(t * item_count); |
| 118 |
IM_ASSERT(v_idx >= 0 && v_idx < values_count); |
| 119 |
|
| 120 |
const float v0 = values_getter(data, (v_idx + values_offset) % values_count); |
| 121 |
if (plot_type == ImGuiPlotType_Lines) |
| 122 |
ImGui::SetTooltip("%d: %8.4g", v_idx, v0); |
| 123 |
else if (plot_type == ImGuiPlotType_Histogram) |
| 124 |
ImGui::SetTooltip("%d: %8.4g", v_idx, v0); |
| 125 |
idx_hovered = v_idx; |
| 126 |
} |
| 127 |
|
| 128 |
const float t_step = 1.0f / (float)res_w; |
| 129 |
const float inv_scale = (scale_min == scale_max) ? 0.0f : (1.0f / (scale_max - scale_min)); |
| 130 |
|
| 131 |
float v0 = values_getter(data, (0 + values_offset) % values_count); |
| 132 |
float t0 = 0.0f; |
| 133 |
ImVec2 tp0 = ImVec2( t0, 1.0f - ImSaturate((v0 - scale_min) * inv_scale) ); // Point in the normalized space of our target rectangle |
| 134 |
|
| 135 |
const ImU32 col_base = ImGui::GetColorU32((plot_type == ImGuiPlotType_Lines) ? ImGuiCol_PlotLines : ImGuiCol_PlotHistogram); |
| 136 |
const ImU32 col_hovered = ImGui::GetColorU32((plot_type == ImGuiPlotType_Lines) ? ImGuiCol_PlotLinesHovered : ImGuiCol_PlotHistogramHovered); |
| 137 |
|
| 138 |
for (int n = 0; n < res_w; n++) |
| 139 |
{ |
| 140 |
const float t1 = t0 + t_step; |
| 141 |
const int v1_idx = (int)(t0 * item_count + 0.5f); |
| 142 |
IM_ASSERT(v1_idx >= 0 && v1_idx < values_count); |
| 143 |
const float v1 = values_getter(data, (v1_idx + values_offset + 1) % values_count); |
| 144 |
const ImVec2 tp1 = ImVec2( t1, 1.0f - ImSaturate((v1 - scale_min) * inv_scale) ); |
| 145 |
|
| 146 |
// NB: Draw calls are merged together by the DrawList system. Still, we should render our batch are lower level to save a bit of CPU. |
| 147 |
ImVec2 pos0 = ImLerp(inner_bb.Min, inner_bb.Max, tp0); |
| 148 |
ImVec2 pos1 = ImLerp(inner_bb.Min, inner_bb.Max, tp1); |
| 149 |
ImVec2 pos2 = ImLerp(inner_bb.Min, inner_bb.Max, tp0); |
| 150 |
ImVec2 pos3 = ImLerp(inner_bb.Min, inner_bb.Max, tp1); |
| 151 |
pos1.y=pos0.y; |
| 152 |
pos2.x=pos3.x; |
| 153 |
if (plot_type == ImGuiPlotType_Lines) |
| 154 |
{ |
| 155 |
window->DrawList->AddLine(pos0, pos1, idx_hovered == v1_idx ? col_hovered : col_base); |
| 156 |
window->DrawList->AddLine(pos2, pos3, idx_hovered == v1_idx ? col_hovered : col_base); |
| 157 |
} |
| 158 |
else if (plot_type == ImGuiPlotType_Histogram) |
| 159 |
{ |
| 160 |
if (pos1.x >= pos0.x + 2.0f) |
| 161 |
pos1.x -= 1.0f; |
| 162 |
window->DrawList->AddRectFilled(pos0, pos1, idx_hovered == v1_idx ? col_hovered : col_base); |
| 163 |
} |
| 164 |
|
| 165 |
t0 = t1; |
| 166 |
tp0 = tp1; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
// Text overlay |
| 171 |
if (overlay_text) |
| 172 |
ImGui::RenderTextClipped(ImVec2(frame_bb.Min.x, frame_bb.Min.y + style.FramePadding.y), frame_bb.Max, overlay_text, NULL, NULL, ImVec2(0.5f, 0.0f)); |
| 173 |
|
| 174 |
if (label_size.x > 0.0f) |
| 175 |
ImGui::RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, inner_bb.Min.y), label); |
| 176 |
|
| 177 |
// Return hovered index or -1 if none are hovered. |
| 178 |
// This is currently not exposed in the public API because we need a larger redesign of the whole thing, but in the short-term we are making it available in PlotEx(). |
| 179 |
return idx_hovered; |
| 180 |
} |
| 181 |
|
| 182 |
void PlotNoLerp(const char* label, const float* values, int values_count, int values_offset, const char* overlay_text, float scale_min, float scale_max, ImVec2 graph_size, int stride) |
| 183 |
{ |
| 184 |
FurnacePlotArrayGetterData data(values, stride); |
| 185 |
PlotNoLerpEx(ImGuiPlotType_Lines, label, &Plot_ArrayGetter, (void*)&data, values_count, values_offset, overlay_text, scale_min, scale_max, graph_size); |
| 186 |
} |
| 187 |
|
| 188 |
int PlotBitfieldEx(const char* label, int (*values_getter)(void* data, int idx), void* data, int values_count, int values_offset, const char** overlay_text, int bits, ImVec2 frame_size, const bool* values_highlight, ImVec4 highlightColor, ImVec4 color, std::string (*hoverFunc)(int,float,void*), void* hoverFuncUser) |
| 189 |
{ |
| 190 |
ImGuiContext& g = *GImGui; |
| 191 |
ImGuiWindow* window = ImGui::GetCurrentWindow(); |
| 192 |
if (window->SkipItems) |
| 193 |
return -1; |
| 194 |
|
| 195 |
const ImGuiStyle& style = g.Style; |
| 196 |
const ImGuiID id = window->GetID(label); |
| 197 |
|
| 198 |
const ImVec2 label_size = ImGui::CalcTextSize(label, NULL, true); |
| 199 |
if (frame_size.x == 0.0f) |
| 200 |
frame_size.x = ImGui::CalcItemWidth(); |
| 201 |
if (frame_size.y == 0.0f) |
| 202 |
frame_size.y = label_size.y + (style.FramePadding.y * 2); |
| 203 |
|
| 204 |
const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + frame_size); |
| 205 |
const ImRect inner_bb(frame_bb.Min + style.FramePadding, frame_bb.Max - style.FramePadding); |
| 206 |
const ImRect total_bb(frame_bb.Min, frame_bb.Max + ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0)); |
| 207 |
ImGui::ItemSize(total_bb, style.FramePadding.y); |
| 208 |
if (!ImGui::ItemAdd(total_bb, 0, &frame_bb, ImGuiItemFlags_NoInertialScroll)) |
| 209 |
return -1; |
| 210 |
const bool hovered = ImGui::ItemHoverable(frame_bb, id, 0); |
| 211 |
|
| 212 |
ImGui::RenderFrame(frame_bb.Min, frame_bb.Max, ImGui::GetColorU32(ImGuiCol_FrameBg), true, style.FrameRounding); |
| 213 |
|
| 214 |
const int values_count_min = 1; |
| 215 |
int idx_hovered = -1; |
| 216 |
if (values_count >= values_count_min) |
| 217 |
{ |
| 218 |
int res_w = ImMin((int)frame_size.x, values_count); |
| 219 |
int item_count = values_count; |
| 220 |
|
| 221 |
// Tooltip on hover |
| 222 |
if (hovered && inner_bb.Contains(g.IO.MousePos)) |
| 223 |
{ |
| 224 |
const float t = ImClamp((g.IO.MousePos.x - inner_bb.Min.x) / (inner_bb.Max.x - inner_bb.Min.x), 0.0f, 0.9999f); |
| 225 |
const int v_idx = (int)(t * item_count); |
| 226 |
IM_ASSERT(v_idx >= 0 && v_idx < values_count); |
| 227 |
|
| 228 |
const float v0 = values_getter(data, (v_idx + values_offset) % values_count); |
| 229 |
//ImGui::SetTooltip("%d: %8.4g", v_idx, v0); |
| 230 |
|
| 231 |
if (hoverFunc) { |
| 232 |
std::string hoverText=hoverFunc(v_idx,v0,hoverFuncUser); |
| 233 |
if (!hoverText.empty()) { |
| 234 |
ImGui::SetTooltip("%s",hoverText.c_str()); |
| 235 |
} |
| 236 |
} else { |
| 237 |
ImGui::SetTooltip("%d: %d (%X)", v_idx, (int)v0, (int)v0); |
| 238 |
} |
| 239 |
|
| 240 |
idx_hovered = v_idx; |
| 241 |
} |
| 242 |
|
| 243 |
const float t_step = 1.0f / (float)res_w; |
| 244 |
|
| 245 |
float t0 = 0.0f; |
| 246 |
ImVec2 tp0 = ImVec2( t0, 0.0f ); // Point in the normalized space of our target rectangle |
| 247 |
const ImU32 col_base = ImGui::GetColorU32(color); |
| 248 |
const ImU32 col_hovered = ImGui::GetColorU32(color); |
| 249 |
|
| 250 |
for (int n = 0; n < res_w; n++) |
| 251 |
{ |
| 252 |
const float t1 = t0 + t_step; |
| 253 |
const int v1_idx = (int)(t0 * item_count + 0.5f); |
| 254 |
IM_ASSERT(v1_idx >= 0 && v1_idx < values_count); |
| 255 |
const int v1 = values_getter(data, (v1_idx + values_offset) % values_count); |
| 256 |
ImVec2 tp1 = ImVec2( t1, 0.0f ); |
| 257 |
for (int o = 0; o < bits; o++) { |
| 258 |
tp0.y=float(bits-o)/float(bits); |
| 259 |
tp1.y=float(bits-o-1)/float(bits); |
| 260 |
// NB: Draw calls are merged together by the DrawList system. Still, we should render our batch are lower level to save a bit of CPU. |
| 261 |
ImVec2 pos0 = ImLerp(inner_bb.Min, inner_bb.Max, tp0); |
| 262 |
ImVec2 pos1 = ImLerp(inner_bb.Min, inner_bb.Max, tp1); |
| 263 |
if (pos1.x >= pos0.x + 2.0f) |
| 264 |
pos1.x -= 1.0f; |
| 265 |
if (pos1.y <= pos0.y - 2.0f) |
| 266 |
pos1.y += 1.0f; |
| 267 |
if (v1&(1<<o)) { |
| 268 |
ImU32 rCol=(idx_hovered == v1_idx ? col_hovered : col_base); |
| 269 |
if (values_highlight!=NULL) { |
| 270 |
if (values_highlight[v1_idx]) rCol=ImGui::GetColorU32(highlightColor); |
| 271 |
} |
| 272 |
window->DrawList->AddRectFilled(pos0, pos1, rCol); |
| 273 |
} |
| 274 |
} |
| 275 |
tp0 = tp1; |
| 276 |
t0 = t1; |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
// Text overlay |
| 281 |
if (overlay_text) { |
| 282 |
float lineHeight=ImGui::GetTextLineHeight()/2.0; |
| 283 |
for (int i=0; i<bits && overlay_text[i]; i++) { |
| 284 |
ImGui::PushStyleColor(ImGuiCol_Text,ImVec4(0,0,0,1.0f)); |
| 285 |
const char* text=_(overlay_text[i]); |
| 286 |
ImGui::RenderTextClipped(ImVec2(frame_bb.Min.x-1, frame_bb.Min.y-lineHeight-1), ImVec2(frame_bb.Max.x-1,frame_bb.Max.y+lineHeight-1), text, NULL, NULL, ImVec2(0.0f, (0.5+double(bits-1-i))/double(bits))); |
| 287 |
ImGui::RenderTextClipped(ImVec2(frame_bb.Min.x-1, frame_bb.Min.y-lineHeight+1), ImVec2(frame_bb.Max.x-1,frame_bb.Max.y+lineHeight+1), text, NULL, NULL, ImVec2(0.0f, (0.5+double(bits-1-i))/double(bits))); |
| 288 |
ImGui::RenderTextClipped(ImVec2(frame_bb.Min.x+1, frame_bb.Min.y-lineHeight-1), ImVec2(frame_bb.Max.x+1,frame_bb.Max.y+lineHeight-1), text, NULL, NULL, ImVec2(0.0f, (0.5+double(bits-1-i))/double(bits))); |
| 289 |
ImGui::RenderTextClipped(ImVec2(frame_bb.Min.x+1, frame_bb.Min.y-lineHeight+1), ImVec2(frame_bb.Max.x+1,frame_bb.Max.y+lineHeight+1), text, NULL, NULL, ImVec2(0.0f, (0.5+double(bits-1-i))/double(bits))); |
| 290 |
ImGui::PopStyleColor(); |
| 291 |
ImGui::RenderTextClipped(ImVec2(frame_bb.Min.x, frame_bb.Min.y-lineHeight), ImVec2(frame_bb.Max.x,frame_bb.Max.y+lineHeight), text, NULL, NULL, ImVec2(0.0f, (0.5+double(bits-1-i))/double(bits))); |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
if (label_size.x > 0.0f) |
| 296 |
ImGui::RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, inner_bb.Min.y), label); |
| 297 |
|
| 298 |
// Return hovered index or -1 if none are hovered. |
| 299 |
// This is currently not exposed in the public API because we need a larger redesign of the whole thing, but in the short-term we are making it available in PlotEx(). |
| 300 |
return idx_hovered; |
| 301 |
} |
| 302 |
|
| 303 |
void PlotBitfield(const char* label, const int* values, int values_count, int values_offset, const char** overlay_text, int bits, ImVec2 graph_size, int stride, const bool* values_highlight, ImVec4 highlightColor, ImVec4 color, std::string (*hoverFunc)(int,float,void*), void* hoverFuncUser) |
| 304 |
{ |
| 305 |
FurnacePlotIntArrayGetterData data(values, stride); |
| 306 |
PlotBitfieldEx(label, &Plot_IntArrayGetter, (void*)&data, values_count, values_offset, overlay_text, bits, graph_size, values_highlight, highlightColor, color, hoverFunc, hoverFuncUser); |
| 307 |
} |
| 308 |
|
| 309 |
int PlotCustomEx(ImGuiPlotType plot_type, const char* label, float (*values_getter)(void* data, int idx), void* data, int values_count, int values_display_offset, const char* overlay_text, float scale_min, float scale_max, ImVec2 frame_size, ImVec4 color, int highlight, std::string (*hoverFunc)(int,float,void*), void* hoverFuncUser, bool blockMode, std::string (*guideFunc)(float), const bool* values_highlight, ImVec4 highlightColor) |
| 310 |
{ |
| 311 |
ImGuiContext& g = *GImGui; |
| 312 |
ImGuiWindow* window = ImGui::GetCurrentWindow(); |
| 313 |
if (window->SkipItems) |
| 314 |
return -1; |
| 315 |
|
| 316 |
const ImGuiStyle& style = g.Style; |
| 317 |
const ImGuiID id = window->GetID(label); |
| 318 |
|
| 319 |
const ImVec2 label_size = ImGui::CalcTextSize(label, NULL, true); |
| 320 |
if (frame_size.x == 0.0f) |
| 321 |
frame_size.x = ImGui::CalcItemWidth(); |
| 322 |
if (frame_size.y == 0.0f) |
| 323 |
frame_size.y = label_size.y + (style.FramePadding.y * 2); |
| 324 |
|
| 325 |
const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + frame_size); |
| 326 |
const ImRect inner_bb(frame_bb.Min + style.FramePadding, frame_bb.Max - style.FramePadding); |
| 327 |
const ImRect total_bb(frame_bb.Min, frame_bb.Max + ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0)); |
| 328 |
ImGui::ItemSize(total_bb, style.FramePadding.y); |
| 329 |
if (!ImGui::ItemAdd(total_bb, 0, &frame_bb, ImGuiItemFlags_NoInertialScroll)) |
| 330 |
return -1; |
| 331 |
const bool hovered = ImGui::ItemHoverable(frame_bb, id, 0); |
| 332 |
|
| 333 |
// Determine scale from values if not specified |
| 334 |
if (scale_min == FLT_MAX || scale_max == FLT_MAX) |
| 335 |
{ |
| 336 |
float v_min = FLT_MAX; |
| 337 |
float v_max = -FLT_MAX; |
| 338 |
for (int i = 0; i < values_count; i++) |
| 339 |
{ |
| 340 |
const float v = values_getter(data, i); |
| 341 |
if (v != v) // Ignore NaN values |
| 342 |
continue; |
| 343 |
v_min = ImMin(v_min, v); |
| 344 |
v_max = ImMax(v_max, v); |
| 345 |
} |
| 346 |
if (scale_min == FLT_MAX) |
| 347 |
scale_min = v_min; |
| 348 |
if (scale_max == FLT_MAX) |
| 349 |
scale_max = v_max; |
| 350 |
} |
| 351 |
|
| 352 |
if (blockMode) scale_max+=1.0f; |
| 353 |
|
| 354 |
ImU32 bgColor=ImGui::GetColorU32(ImVec4(color.x,color.y,color.z,color.w*0.15)); |
| 355 |
|
| 356 |
ImGui::RenderFrame(frame_bb.Min, frame_bb.Max, ImGui::GetColorU32(ImGuiCol_FrameBg), true, style.FrameRounding); |
| 357 |
|
| 358 |
const int values_count_min = (plot_type == ImGuiPlotType_Lines) ? 2 : 1; |
| 359 |
int idx_hovered = -1; |
| 360 |
if (values_count >= values_count_min) |
| 361 |
{ |
| 362 |
int res_w = ImMin((int)frame_size.x, values_count) + ((plot_type == ImGuiPlotType_Lines) ? -1 : 0); |
| 363 |
int item_count = values_count + ((plot_type == ImGuiPlotType_Lines) ? -1 : 0); |
| 364 |
|
| 365 |
// Tooltip on hover |
| 366 |
if (hovered && inner_bb.Contains(g.IO.MousePos)) |
| 367 |
{ |
| 368 |
const float t = ImClamp((g.IO.MousePos.x - inner_bb.Min.x) / (inner_bb.Max.x - inner_bb.Min.x), 0.0f, 0.9999f); |
| 369 |
const int v_idx = (int)(t * item_count); |
| 370 |
IM_ASSERT(v_idx >= 0 && v_idx < values_count); |
| 371 |
|
| 372 |
const float v0 = values_getter(data, (v_idx) % values_count); |
| 373 |
const float v1 = values_getter(data, (v_idx + 1) % values_count); |
| 374 |
if (hoverFunc) { |
| 375 |
std::string hoverText=hoverFunc(v_idx+values_display_offset,v0,hoverFuncUser); |
| 376 |
if (!hoverText.empty()) { |
| 377 |
ImGui::SetTooltip("%s",hoverText.c_str()); |
| 378 |
} |
| 379 |
} else { |
| 380 |
if (plot_type == ImGuiPlotType_Lines) |
| 381 |
ImGui::SetTooltip("%d: %8.4g\n%d: %8.4g", v_idx, v0, v_idx + 1, v1); |
| 382 |
else if (plot_type == ImGuiPlotType_Histogram) |
| 383 |
ImGui::SetTooltip("%d: %d", v_idx+values_display_offset, (int)v0); |
| 384 |
} |
| 385 |
idx_hovered = v_idx; |
| 386 |
} |
| 387 |
|
| 388 |
const float t_step = 1.0f / (float)res_w; |
| 389 |
const float inv_scale = (scale_min == scale_max) ? 0.0f : (1.0f / (scale_max - scale_min)); |
| 390 |
|
| 391 |
float v0 = values_getter(data, (0) % values_count); |
| 392 |
float t0 = 0.0f; |
| 393 |
ImVec2 tp0 = ImVec2( t0, 1.0f - ImSaturate((v0 - scale_min) * inv_scale) ); // Point in the normalized space of our target rectangle |
| 394 |
float histogram_zero_line_t = (scale_min * scale_max < 0.0f) ? (1 + (blockMode?(scale_min-0.5):scale_min) * inv_scale) : (scale_min < 0.0f ? 0.0f : 1.0f); // Where does the zero line stands |
| 395 |
|
| 396 |
const ImU32 col_base = ImGui::GetColorU32(color); |
| 397 |
const ImU32 col_hovered = ImGui::GetColorU32(color); |
| 398 |
|
| 399 |
if (highlight>0) { |
| 400 |
window->DrawList->AddRectFilled( |
| 401 |
ImVec2(ImLerp(inner_bb.Min, inner_bb.Max, ImVec2(0,0))), |
| 402 |
ImVec2(ImLerp(inner_bb.Min, inner_bb.Max, ImVec2((highlight>=values_count)?1:(double(highlight)/double(values_count)),1))), |
| 403 |
bgColor); |
| 404 |
} |
| 405 |
|
| 406 |
if (blockMode) { |
| 407 |
window->DrawList->AddLine(ImLerp(inner_bb.Min,inner_bb.Max,ImVec2(0.0f,histogram_zero_line_t)),ImLerp(inner_bb.Min,inner_bb.Max,ImVec2(1.0f,histogram_zero_line_t)),col_base); |
| 408 |
} |
| 409 |
|
| 410 |
ImVec2 chevron[3]; |
| 411 |
|
| 412 |
for (int n = 0; n < res_w; n++) |
| 413 |
{ |
| 414 |
const float t1 = t0 + t_step; |
| 415 |
const int v1_idx = (int)(t0 * item_count + 0.5f); |
| 416 |
IM_ASSERT(v1_idx >= 0 && v1_idx < values_count); |
| 417 |
const float v1 = values_getter(data, (v1_idx + 1) % values_count); |
| 418 |
const ImVec2 tp1 = ImVec2( t1, 1.0f - ImSaturate((v1 - scale_min) * inv_scale) ); |
| 419 |
|
| 420 |
// NB: Draw calls are merged together by the DrawList system. Still, we should render our batch are lower level to save a bit of CPU. |
| 421 |
ImVec2 pos0 = ImLerp(inner_bb.Min, inner_bb.Max, tp0); |
| 422 |
ImVec2 pos1 = ImLerp(inner_bb.Min, inner_bb.Max, (plot_type == ImGuiPlotType_Lines) ? tp1 : ImVec2(tp1.x, blockMode?tp0.y:histogram_zero_line_t)); |
| 423 |
if (plot_type == ImGuiPlotType_Lines) |
| 424 |
{ |
| 425 |
window->DrawList->AddLine(pos0, pos1, idx_hovered == v1_idx ? col_hovered : col_base); |
| 426 |
} |
| 427 |
else if (plot_type == ImGuiPlotType_Histogram) |
| 428 |
{ |
| 429 |
if (pos1.x >= pos0.x + 2.0f) |
| 430 |
pos1.x -= 1.0f; |
| 431 |
if (blockMode) { |
| 432 |
pos0.y-=(inner_bb.Max.y-inner_bb.Min.y)*inv_scale; |
| 433 |
//pos1.y+=1.0f; |
| 434 |
} |
| 435 |
ImU32 rCol=(idx_hovered == v1_idx ? col_hovered : col_base); |
| 436 |
if (values_highlight!=NULL) { |
| 437 |
if (values_highlight[v1_idx]) rCol=ImGui::GetColorU32(highlightColor); |
| 438 |
} |
| 439 |
if (blockMode) { |
| 440 |
if ((int)v0>=(int)(scale_max+0.5)) { |
| 441 |
float chScale=(pos1.x-pos0.x)*0.125; |
| 442 |
if (chScale>frame_size.y*0.05) chScale=frame_size.y*0.05; |
| 443 |
chevron[0]=ImVec2((pos1.x+pos0.x)*0.5-(2.0f*chScale),pos1.y+4.0f*chScale); |
| 444 |
chevron[1]=ImVec2((pos1.x+pos0.x)*0.5,pos1.y+2.0f*chScale); |
| 445 |
chevron[2]=ImVec2((pos1.x+pos0.x)*0.5+(2.0f*chScale),pos1.y+4.0f*chScale); |
| 446 |
window->DrawList->AddPolyline(chevron, 3, rCol, 0, chScale); |
| 447 |
} else if ((int)v0<(int)(scale_min)) { |
| 448 |
float chScale=(pos1.x-pos0.x)*0.125; |
| 449 |
if (chScale>frame_size.y*0.05) chScale=frame_size.y*0.05; |
| 450 |
chevron[0]=ImVec2((pos1.x+pos0.x)*0.5-(2.0f*chScale),pos1.y-4.0f*chScale); |
| 451 |
chevron[1]=ImVec2((pos1.x+pos0.x)*0.5,pos1.y-2.0f*chScale); |
| 452 |
chevron[2]=ImVec2((pos1.x+pos0.x)*0.5+(2.0f*chScale),pos1.y-4.0f*chScale); |
| 453 |
window->DrawList->AddPolyline(chevron, 3, rCol, 0, chScale); |
| 454 |
} else { |
| 455 |
window->DrawList->AddRectFilled(pos0, pos1, rCol); |
| 456 |
} |
| 457 |
} else { |
| 458 |
window->DrawList->AddRectFilled(pos0, pos1, rCol); |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
t0 = t1; |
| 463 |
tp0 = tp1; |
| 464 |
v0 = v1; |
| 465 |
} |
| 466 |
} |
| 467 |
|
| 468 |
// Text overlay |
| 469 |
if (overlay_text) |
| 470 |
ImGui::RenderTextClipped(ImVec2(frame_bb.Min.x, frame_bb.Min.y + style.FramePadding.y), frame_bb.Max, overlay_text, NULL, NULL, ImVec2(0.5f, 0.0f)); |
| 471 |
|
| 472 |
if (label_size.x > 0.0f) |
| 473 |
ImGui::RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, inner_bb.Min.y), label); |
| 474 |
|
| 475 |
if (guideFunc && idx_hovered!=0) { |
| 476 |
std::string guide=guideFunc(scale_max); |
| 477 |
window->DrawList->AddText(frame_bb.Min+ImVec2(1.0f,1.0f),0xff000000,guide.c_str()); |
| 478 |
window->DrawList->AddText(frame_bb.Min+ImVec2(1.0f,-1.0f),0xff000000,guide.c_str()); |
| 479 |
window->DrawList->AddText(frame_bb.Min+ImVec2(-1.0f,1.0f),0xff000000,guide.c_str()); |
| 480 |
window->DrawList->AddText(frame_bb.Min+ImVec2(-1.0f,-1.0f),0xff000000,guide.c_str()); |
| 481 |
window->DrawList->AddText(frame_bb.Min,0xffffffff,guide.c_str()); |
| 482 |
|
| 483 |
std::string guide1=guideFunc(scale_min); |
| 484 |
ImVec2 maxPos=frame_bb.Min; |
| 485 |
// TODO: get rid of LegacySize somehow |
| 486 |
maxPos.y=frame_bb.Max.y-ImGui::GetFont()->LegacySize; |
| 487 |
window->DrawList->AddText(maxPos+ImVec2(1.0f,1.0f),0xff000000,guide1.c_str()); |
| 488 |
window->DrawList->AddText(maxPos+ImVec2(1.0f,-1.0f),0xff000000,guide1.c_str()); |
| 489 |
window->DrawList->AddText(maxPos+ImVec2(-1.0f,1.0f),0xff000000,guide1.c_str()); |
| 490 |
window->DrawList->AddText(maxPos+ImVec2(-1.0f,-1.0f),0xff000000,guide1.c_str()); |
| 491 |
window->DrawList->AddText(maxPos,0xffffffff,guide1.c_str()); |
| 492 |
} |
| 493 |
|
| 494 |
// Return hovered index or -1 if none are hovered. |
| 495 |
// This is currently not exposed in the public API because we need a larger redesign of the whole thing, but in the short-term we are making it available in PlotEx(). |
| 496 |
return idx_hovered; |
| 497 |
} |
| 498 |
|
| 499 |
void PlotCustom(const char* label, const float* values, int values_count, int values_offset, const char* overlay_text, float scale_min, float scale_max, ImVec2 graph_size, int stride, ImVec4 color, int highlight, std::string (*hoverFunc)(int,float,void*), void* hoverFuncUser, bool blockMode, std::string (*guideFunc)(float), const bool* values_highlight, ImVec4 highlightColor) |
| 500 |
{ |
| 501 |
FurnacePlotArrayGetterData data(values, stride); |
| 502 |
PlotCustomEx(ImGuiPlotType_Histogram, label, &Plot_ArrayGetter, (void*)&data, values_count, values_offset, overlay_text, scale_min, scale_max, graph_size, color, highlight, hoverFunc, hoverFuncUser, blockMode, guideFunc, values_highlight, highlightColor); |
| 503 |
} |