From 3c1ef115259affaf5042df57d1921180473fb18d Mon Sep 17 00:00:00 2001 From: Evan Pezent Date: Wed, 29 Apr 2020 15:39:02 -0500 Subject: [PATCH] add custom rendering utils --- implot.cpp | 29 +++++++++++++++++++++++ implot.h | 63 ++++++++++++++++++++++++++++++++----------------- implot_demo.cpp | 15 ++++++++++-- 3 files changed, 83 insertions(+), 24 deletions(-) diff --git a/implot.cpp b/implot.cpp index ff0849c..8868733 100644 --- a/implot.cpp +++ b/implot.cpp @@ -1295,6 +1295,35 @@ void SetNextPlotRangeY(float y_min, float y_max, ImGuiCond cond) { gp.NextPlotData.YMax = y_max; } +ImVec2 GetPlotPos() { + IM_ASSERT_USER_ERROR(gp.CurrentPlot != NULL, "GetPlotPos() Needs to be called between BeginPlot() and EndPlot()!"); + return gp.BB_Grid.Min; +} + +ImVec2 GetPlotSize() { + IM_ASSERT_USER_ERROR(gp.CurrentPlot != NULL, "GetPlotSize() Needs to be called between BeginPlot() and EndPlot()!"); + return gp.BB_Grid.GetSize(); +} + +ImVec2 PixelsToPlot(const ImVec2& pix) { + IM_ASSERT_USER_ERROR(gp.CurrentPlot != NULL, "PixelsToPlot() Needs to be called between BeginPlot() and EndPlot()!"); + return gp.FromPixels(pix); +} + +ImVec2 PlotToPixels(const ImVec2& plt) { + IM_ASSERT_USER_ERROR(gp.CurrentPlot != NULL, "PlotToPixels() Needs to be called between BeginPlot() and EndPlot()!"); + return gp.ToPixels(plt); +} + +void PushPlotClipRect() { + IM_ASSERT_USER_ERROR(gp.CurrentPlot != NULL, "PushPlotClipRect() Needs to be called between BeginPlot() and EndPlot()!"); + PushClipRect(gp.BB_Grid.Min, gp.BB_Grid.Max, true); +} + +void PopPlotClipRect() { + PopClipRect(); +} + bool IsPlotHovered() { IM_ASSERT_USER_ERROR(gp.CurrentPlot != NULL, "IsPlotHovered() Needs to be called between BeginPlot() and EndPlot()!"); return gp.Hov_Grid; diff --git a/implot.h b/implot.h index a12c536..a1d0be0 100644 --- a/implot.h +++ b/implot.h @@ -147,28 +147,6 @@ bool BeginPlot(const char* title_id, // of an if statement conditioned on BeginPlot(). void EndPlot(); -/// Set the axes ranges of the next plot. Call right before BeginPlot(). If ImGuiCond_Always is used, the axes will be locked. -void SetNextPlotRange(float x_min, float x_max, float y_min, float y_max, ImGuiCond cond = ImGuiCond_Once); -/// Set the X axis range of the next plot. Call right before BeginPlot(). If ImGuiCond_Always is used, the axis will be locked. -void SetNextPlotRangeX(float x_min, float x_max, ImGuiCond cond = ImGuiCond_Once); -/// Set the X axis range of the next plot. Call right before BeginPlot(). If ImGuiCond_Always is used, the axis will be locked. -void SetNextPlotRangeY(float y_min, float y_max, ImGuiCond cond = ImGuiCond_Once); - -//----------------------------------------------------------------------------- -// Plot Queries -//----------------------------------------------------------------------------- - -/// Returns true if the plot area in the current or most recent plot is hovered. -bool IsPlotHovered(); -/// Returns the mouse position in x,y coordinates of the current or most recent plot. -ImVec2 GetPlotMousePos(); -/// Returns the current or most recent plot axis range. -ImPlotRange GetPlotRange(); -/// Returns true if the current or most recent plot is being queried. -bool IsPlotQueried(); -/// Returns the current or most recent plot querey range. -ImPlotRange GetPlotQuery(); - //----------------------------------------------------------------------------- // Plot Items //----------------------------------------------------------------------------- @@ -192,6 +170,21 @@ void PlotErrorBars(const char* label_id, ImVec4 (*getter)(void* data, int idx), // Plots a text label at point x,y. void PlotLabel(const char* text, float x, float y, bool vertical = false, const ImVec2& pixel_offset = ImVec2(0,0)); +//----------------------------------------------------------------------------- +// Plot Queries +//----------------------------------------------------------------------------- + +/// Returns true if the plot area in the current or most recent plot is hovered. +bool IsPlotHovered(); +/// Returns the mouse position in x,y coordinates of the current or most recent plot. +ImVec2 GetPlotMousePos(); +/// Returns the current or most recent plot axis range. +ImPlotRange GetPlotRange(); +/// Returns true if the current or most recent plot is being queried. +bool IsPlotQueried(); +/// Returns the current or most recent plot querey range. +ImPlotRange GetPlotQuery(); + //----------------------------------------------------------------------------- // Plot Styling //----------------------------------------------------------------------------- @@ -218,6 +211,32 @@ void PushPlotStyleVar(ImPlotStyleVar idx, int val); // Undo temporary style modification. void PopPlotStyleVar(int count = 1); +//----------------------------------------------------------------------------- +// Plot Utils +//----------------------------------------------------------------------------- + +/// Set the axes ranges of the next plot. Call right before BeginPlot(). If ImGuiCond_Always is used, the axes will be locked. +void SetNextPlotRange(float x_min, float x_max, float y_min, float y_max, ImGuiCond cond = ImGuiCond_Once); +/// Set the X axis range of the next plot. Call right before BeginPlot(). If ImGuiCond_Always is used, the axis will be locked. +void SetNextPlotRangeX(float x_min, float x_max, ImGuiCond cond = ImGuiCond_Once); +/// Set the X axis range of the next plot. Call right before BeginPlot(). If ImGuiCond_Always is used, the axis will be locked. +void SetNextPlotRangeY(float y_min, float y_max, ImGuiCond cond = ImGuiCond_Once); + +// Get the current Plot position (top-left) in pixels. +ImVec2 GetPlotPos(); +// Get the curent Plot size in pixels. +ImVec2 GetPlotSize(); + +// Convert pixels to a position in the current plot's coordinate system. +ImVec2 PixelsToPlot(const ImVec2& pix); +// Convert a position in the current plot's coordinate system to pixels. +ImVec2 PlotToPixels(const ImVec2& plt); + +// Push clip rect for rendering to current plot area +void PushPlotClipRect(); +// Pop plot clip rect +void PopPlotClipRect(); + //----------------------------------------------------------------------------- // Demo //----------------------------------------------------------------------------- diff --git a/implot_demo.cpp b/implot_demo.cpp index ee8a940..58adf4f 100644 --- a/implot_demo.cpp +++ b/implot_demo.cpp @@ -23,7 +23,6 @@ // ImPlot v0.1 WIP #include -#include #include namespace { @@ -48,7 +47,7 @@ struct RollingData { ImVector Data; RollingData() { Data.reserve(1000); } void AddPoint(float x, float y) { - float xmod = ImFmod(x, Span); + float xmod = fmodf(x, Span); if (!Data.empty() && xmod < Data.back().x) Data.shrink(0); Data.push_back(ImVec2(xmod, y)); @@ -493,6 +492,18 @@ void ShowImPlotDemoWindow(bool* p_open) { ImGui::PopPlotStyleVar(); ImGui::RestorePlotPalette(); } + if (ImGui::CollapsingHeader("Custom Rendering")) { + if (ImGui::BeginPlot("##CustomRend",NULL,NULL,{-1,300})) { + ImVec2 cntr = ImGui::PlotToPixels({0.5f, 0.5f}); + ImVec2 rmin = ImGui::PlotToPixels({0.25f, 0.75f}); + ImVec2 rmax = ImGui::PlotToPixels({0.75f, 0.25f}); + ImGui::PushPlotClipRect(); + ImGui::GetWindowDrawList()->AddCircleFilled(cntr,20,IM_COL32(255,255,0,255),20); + ImGui::GetWindowDrawList()->AddRect(rmin, rmax, IM_COL32(128,0,255,255)); + ImGui::PopPlotClipRect(); + ImGui::EndPlot(); + } + } //------------------------------------------------------------------------- // if (ImGui::CollapsingHeader("Benchmark")) {