Skip to content

Commit

Permalink
Plot: Register an ID to take the click the same way as other framed w…
Browse files Browse the repository at this point in the history
…idgets. Set HoveredId in the FramePadding zone (between inner_bb and frame_bb).
  • Loading branch information
ocornut committed Feb 5, 2019
1 parent cef4e08 commit 97ed97b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
2 changes: 1 addition & 1 deletion imgui_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -1493,7 +1493,7 @@ namespace ImGui
IMGUI_API void ColorPickerOptionsPopup(const float* ref_col, ImGuiColorEditFlags flags);

// Plot
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);
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);

// Shade functions (write over already created vertices)
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);
Expand Down
19 changes: 10 additions & 9 deletions imgui_widgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5214,28 +5214,29 @@ bool ImGui::ListBox(const char* label, int* current_item, bool (*items_getter)(v
// - PlotHistogram()
//-------------------------------------------------------------------------

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)
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)
{
ImGuiWindow* window = GetCurrentWindow();
if (window->SkipItems)
return;

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

const ImVec2 label_size = CalcTextSize(label, NULL, true);
if (graph_size.x == 0.0f)
graph_size.x = CalcItemWidth();
if (graph_size.y == 0.0f)
graph_size.y = label_size.y + (style.FramePadding.y * 2);
if (frame_size.x == 0.0f)
frame_size.x = CalcItemWidth();
if (frame_size.y == 0.0f)
frame_size.y = label_size.y + (style.FramePadding.y * 2);

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

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

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

// Tooltip on hover
int v_hovered = -1;
if (hovered)
if (hovered && inner_bb.Contains(g.IO.MousePos))
{
const float t = ImClamp((g.IO.MousePos.x - inner_bb.Min.x) / (inner_bb.Max.x - inner_bb.Min.x), 0.0f, 0.9999f);
const int v_idx = (int)(t * item_count);
Expand Down

0 comments on commit 97ed97b

Please sign in to comment.