Skip to content

Commit 97ed97b

Browse files
committed
Plot: Register an ID to take the click the same way as other framed widgets. Set HoveredId in the FramePadding zone (between inner_bb and frame_bb).
1 parent cef4e08 commit 97ed97b

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

imgui_internal.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1493,7 +1493,7 @@ namespace ImGui
14931493
IMGUI_API void ColorPickerOptionsPopup(const float* ref_col, ImGuiColorEditFlags flags);
14941494

14951495
// Plot
1496-
IMGUI_API void PlotEx(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 graph_size);
1496+
IMGUI_API void PlotEx(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);
14971497

14981498
// Shade functions (write over already created vertices)
14991499
IMGUI_API void ShadeVertsLinearColorGradientKeepAlpha(ImDrawList* draw_list, int vert_start_idx, int vert_end_idx, ImVec2 gradient_p0, ImVec2 gradient_p1, ImU32 col0, ImU32 col1);

imgui_widgets.cpp

+10-9
Original file line numberDiff line numberDiff line change
@@ -5214,28 +5214,29 @@ bool ImGui::ListBox(const char* label, int* current_item, bool (*items_getter)(v
52145214
// - PlotHistogram()
52155215
//-------------------------------------------------------------------------
52165216

5217-
void ImGui::PlotEx(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 graph_size)
5217+
void ImGui::PlotEx(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)
52185218
{
52195219
ImGuiWindow* window = GetCurrentWindow();
52205220
if (window->SkipItems)
52215221
return;
52225222

52235223
ImGuiContext& g = *GImGui;
52245224
const ImGuiStyle& style = g.Style;
5225+
const ImGuiID id = window->GetID(label);
52255226

52265227
const ImVec2 label_size = CalcTextSize(label, NULL, true);
5227-
if (graph_size.x == 0.0f)
5228-
graph_size.x = CalcItemWidth();
5229-
if (graph_size.y == 0.0f)
5230-
graph_size.y = label_size.y + (style.FramePadding.y * 2);
5228+
if (frame_size.x == 0.0f)
5229+
frame_size.x = CalcItemWidth();
5230+
if (frame_size.y == 0.0f)
5231+
frame_size.y = label_size.y + (style.FramePadding.y * 2);
52315232

5232-
const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(graph_size.x, graph_size.y));
5233+
const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + frame_size);
52335234
const ImRect inner_bb(frame_bb.Min + style.FramePadding, frame_bb.Max - style.FramePadding);
52345235
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));
52355236
ItemSize(total_bb, style.FramePadding.y);
52365237
if (!ItemAdd(total_bb, 0, &frame_bb))
52375238
return;
5238-
const bool hovered = ItemHoverable(inner_bb, 0);
5239+
const bool hovered = ItemHoverable(frame_bb, id);
52395240

52405241
// Determine scale from values if not specified
52415242
if (scale_min == FLT_MAX || scale_max == FLT_MAX)
@@ -5258,12 +5259,12 @@ void ImGui::PlotEx(ImGuiPlotType plot_type, const char* label, float (*values_ge
52585259

52595260
if (values_count > 0)
52605261
{
5261-
int res_w = ImMin((int)graph_size.x, values_count) + ((plot_type == ImGuiPlotType_Lines) ? -1 : 0);
5262+
int res_w = ImMin((int)frame_size.x, values_count) + ((plot_type == ImGuiPlotType_Lines) ? -1 : 0);
52625263
int item_count = values_count + ((plot_type == ImGuiPlotType_Lines) ? -1 : 0);
52635264

52645265
// Tooltip on hover
52655266
int v_hovered = -1;
5266-
if (hovered)
5267+
if (hovered && inner_bb.Contains(g.IO.MousePos))
52675268
{
52685269
const float t = ImClamp((g.IO.MousePos.x - inner_bb.Min.x) / (inner_bb.Max.x - inner_bb.Min.x), 0.0f, 0.9999f);
52695270
const int v_idx = (int)(t * item_count);

0 commit comments

Comments
 (0)