From ec8e87561f7840a36373bafab9af105c3b5ddbd6 Mon Sep 17 00:00:00 2001 From: epezent Date: Thu, 17 Sep 2020 11:58:58 -0500 Subject: [PATCH 1/5] PlotImage --- implot.h | 5 +++++ implot_items.cpp | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/implot.h b/implot.h index cf2274e..2783729 100644 --- a/implot.h +++ b/implot.h @@ -393,9 +393,14 @@ template IMPLOT_API void PlotHeatmap(const char* label_id, const T* template IMPLOT_API void PlotDigital(const char* label_id, const T* xs, const T* ys, int count, int offset=0, int stride=sizeof(T)); IMPLOT_API void PlotDigitalG(const char* label_id, ImPlotPoint (*getter)(void* data, int idx), void* data, int count, int offset=0); +// Plots an image. +IMPLOT_API void PlotImage(const char* label_id, ImTextureID user_texture_id, const ImPlotPoint& bounds_min, const ImPlotPoint& bounds_max, const ImVec2& uv0 = ImVec2(0, 0), const ImVec2& uv1 = ImVec2(1,1), const ImVec4& tint_col = ImVec4(1,1,1,1)); + // Plots a centered text label at point x,y with optional pixel offset. Text color can be changed with ImPlot::PushStyleColor(ImPlotCol_InlayText, ...). IMPLOT_API void PlotText(const char* text, double x, double y, bool vertical=false, const ImVec2& pixel_offset=ImVec2(0,0)); + + //----------------------------------------------------------------------------- // Plot Utils //----------------------------------------------------------------------------- diff --git a/implot_items.cpp b/implot_items.cpp index 5a35344..b3cedff 100644 --- a/implot_items.cpp +++ b/implot_items.cpp @@ -1705,6 +1705,27 @@ void PlotRects(const char* label_id, ImPlotPoint (*getter_func)(void* data, int return PlotRectsEx(label_id, getter); } +//----------------------------------------------------------------------------- +// PLOT IMAGE +//----------------------------------------------------------------------------- + +void PlotImage(const char* label_id, ImTextureID user_texture_id, const ImPlotPoint& bmin, const ImPlotPoint& bmax, const ImVec2& uv0, const ImVec2& uv1, const ImVec4& tint_col) { + if (BeginItem(label_id)) { + if (FitThisFrame()) { + FitPoint(bmin); + FitPoint(bmax); + } + GetCurrentItem()->Color = tint_col; + ImDrawList& DrawList = *GetPlotDrawList(); + ImVec2 p1 = PlotToPixels(bmin.x, bmax.y); + ImVec2 p2 = PlotToPixels(bmax.x, bmin.y); + PushPlotClipRect(); + DrawList.AddImage(user_texture_id, p1, p2, uv0, uv1, ImGui::ColorConvertFloat4ToU32(tint_col)); + PopPlotClipRect();; + EndItem(); + } +} + //----------------------------------------------------------------------------- // PLOT TEXT //----------------------------------------------------------------------------- From 9e927df91ebf8977c6e12d085510ef2d36f31991 Mon Sep 17 00:00:00 2001 From: ozlb Date: Fri, 18 Sep 2020 00:35:14 +0200 Subject: [PATCH 2/5] ImPlotTimeFmt_HrMinSMs time format ImPlotTimeFmt_HrMinSMs, // 7:21:29.428pm (19:21:29.428) --- implot.cpp | 4 ++++ implot_internal.h | 3 ++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/implot.cpp b/implot.cpp index 9c57229..bf38d69 100644 --- a/implot.cpp +++ b/implot.cpp @@ -845,6 +845,7 @@ int FormatTime12(const ImPlotTime& t, char* buffer, int size, ImPlotTimeFmt fmt) case ImPlotTimeFmt_SUs: return snprintf(buffer, size, ":%02d.%03d %03d", sec, ms, us); case ImPlotTimeFmt_SMs: return snprintf(buffer, size, ":%02d.%03d", sec, ms); case ImPlotTimeFmt_S: return snprintf(buffer, size, ":%02d", sec); + case ImPlotTimeFmt_HrMinSMs: return snprintf(buffer, size, "%d:%02d:%02d.%03d%s", hr, min, sec, ms, ap); case ImPlotTimeFmt_HrMinS: return snprintf(buffer, size, "%d:%02d:%02d%s", hr, min, sec, ap); case ImPlotTimeFmt_HrMin: return snprintf(buffer, size, "%d:%02d%s", hr, min, ap); case ImPlotTimeFmt_Hr: return snprintf(buffer, size, "%d%s", hr, ap); @@ -881,6 +882,7 @@ int FormatTime24(const ImPlotTime& t, char* buffer, int size, ImPlotTimeFmt fmt) case ImPlotTimeFmt_SUs: return snprintf(buffer, size, ":%02d.%03d %03d", sec, ms, us); case ImPlotTimeFmt_SMs: return snprintf(buffer, size, ":%02d.%03d", sec, ms); case ImPlotTimeFmt_S: return snprintf(buffer, size, ":%02d", sec); + case ImPlotTimeFmt_HrMinSMs: return snprintf(buffer, size, "%02d:%02d:%02d.%03d", hr, min, sec, ms); case ImPlotTimeFmt_HrMinS: return snprintf(buffer, size, "%02d:%02d:%02d", hr, min, sec); case ImPlotTimeFmt_HrMin: return snprintf(buffer, size, "%02d:%02d", hr, min); case ImPlotTimeFmt_Hr: return snprintf(buffer, size, "%02d:00", hr); @@ -905,6 +907,7 @@ inline float GetTimeLabelWidth12(ImPlotTimeFmt fmt) { case ImPlotTimeFmt_SUs: return ImGui::CalcTextSize(":88.888 888").x; // :29.428 552 case ImPlotTimeFmt_SMs: return ImGui::CalcTextSize(":88.888").x; // :29.428 case ImPlotTimeFmt_S: return ImGui::CalcTextSize(":88").x; // :29 + case ImPlotTimeFmt_HrMinSMs: return ImGui::CalcTextSize("88:88:88.888pm").x; // 7:21:29.428pm case ImPlotTimeFmt_HrMinS: return ImGui::CalcTextSize("88:88:88pm").x; // 7:21:29pm case ImPlotTimeFmt_HrMin: return ImGui::CalcTextSize("88:88pm").x; // 7:21pm case ImPlotTimeFmt_Hr: return ImGui::CalcTextSize("88pm").x; // 7pm @@ -929,6 +932,7 @@ inline float GetTimeLabelWidth24(ImPlotTimeFmt fmt) { case ImPlotTimeFmt_SUs: return ImGui::CalcTextSize(":88.888 888").x; // :29.428 552 case ImPlotTimeFmt_SMs: return ImGui::CalcTextSize(":88.888").x; // :29.428 case ImPlotTimeFmt_S: return ImGui::CalcTextSize(":88").x; // :29 + case ImPlotTimeFmt_HrMinSMs: return ImGui::CalcTextSize("88:88:88.888").x; // 19:21:29.428 case ImPlotTimeFmt_HrMinS: return ImGui::CalcTextSize("88:88:88").x; // 19:21:29 case ImPlotTimeFmt_HrMin: return ImGui::CalcTextSize("88:88").x; // 19:21 case ImPlotTimeFmt_Hr: return ImGui::CalcTextSize("88:00").x; // 19:00 diff --git a/implot_internal.h b/implot_internal.h index 4b93004..e6eb290 100644 --- a/implot_internal.h +++ b/implot_internal.h @@ -187,6 +187,7 @@ enum ImPlotTimeFmt_ { ImPlotTimeFmt_SUs, // :29.428 552 ImPlotTimeFmt_SMs, // :29.428 ImPlotTimeFmt_S, // :29 + ImPlotTimeFmt_HrMinSMs, // 7:21:29.428pm (19:21:29.428) ImPlotTimeFmt_HrMinS, // 7:21:29pm (19:21:29) ImPlotTimeFmt_HrMin, // 7:21pm (19:21) ImPlotTimeFmt_Hr, // 7pm (19:00) @@ -874,4 +875,4 @@ IMPLOT_API void PlotRects(const char* label_id, const float* xs, const float* ys IMPLOT_API void PlotRects(const char* label_id, const double* xs, const double* ys, int count, int offset = 0, int stride = sizeof(double)); IMPLOT_API void PlotRects(const char* label_id, ImPlotPoint (*getter)(void* data, int idx), void* data, int count, int offset = 0); -} // namespace ImPlot \ No newline at end of file +} // namespace ImPlot From fd746c377677c0f1e76d623794956f3fafdea2e8 Mon Sep 17 00:00:00 2001 From: epezent Date: Thu, 17 Sep 2020 20:34:37 -0500 Subject: [PATCH 3/5] plot image demo --- implot.h | 5 ++--- implot_demo.cpp | 20 ++++++++++++++++++++ implot_items.cpp | 2 +- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/implot.h b/implot.h index 2783729..5af49b6 100644 --- a/implot.h +++ b/implot.h @@ -188,6 +188,7 @@ struct ImPlotPoint { double x, y; ImPlotPoint() { x = y = 0.0; } ImPlotPoint(double _x, double _y) { x = _x; y = _y; } + ImPlotPoint(const ImVec2& p) { x = p.x; y = p.y; } double operator[] (size_t idx) const { return (&x)[idx]; } double& operator[] (size_t idx) { return (&x)[idx]; } #ifdef IMPLOT_POINT_CLASS_EXTRA @@ -393,14 +394,12 @@ template IMPLOT_API void PlotHeatmap(const char* label_id, const T* template IMPLOT_API void PlotDigital(const char* label_id, const T* xs, const T* ys, int count, int offset=0, int stride=sizeof(T)); IMPLOT_API void PlotDigitalG(const char* label_id, ImPlotPoint (*getter)(void* data, int idx), void* data, int count, int offset=0); -// Plots an image. +// Plots an axis-aligned image. #bounds_min/bounds_max are in plot coordinatse (y-up) and #uv0/uv1 are in texture coordinates (y-down). IMPLOT_API void PlotImage(const char* label_id, ImTextureID user_texture_id, const ImPlotPoint& bounds_min, const ImPlotPoint& bounds_max, const ImVec2& uv0 = ImVec2(0, 0), const ImVec2& uv1 = ImVec2(1,1), const ImVec4& tint_col = ImVec4(1,1,1,1)); // Plots a centered text label at point x,y with optional pixel offset. Text color can be changed with ImPlot::PushStyleColor(ImPlotCol_InlayText, ...). IMPLOT_API void PlotText(const char* text, double x, double y, bool vertical=false, const ImVec2& pixel_offset=ImVec2(0,0)); - - //----------------------------------------------------------------------------- // Plot Utils //----------------------------------------------------------------------------- diff --git a/implot_demo.cpp b/implot_demo.cpp index 6f67bed..2b7f5e5 100644 --- a/implot_demo.cpp +++ b/implot_demo.cpp @@ -508,6 +508,26 @@ void ShowDemoWindow(bool* p_open) { ImPlot::PopColormap(); } //------------------------------------------------------------------------- + if (ImGui::CollapsingHeader("Images")) { + ImGui::BulletText("Below we are displaying the font texture, which is the only texture we have\naccess to in this demo."); + ImGui::BulletText("Use the 'ImTextureID' type as storage to pass pointers or identifiers to your\nown texture data."); + ImGui::BulletText("See ImGui Wiki page 'Image Loading and Displaying Examples'."); + static ImVec2 bmin(0,0); + static ImVec2 bmax(1,1); + static ImVec2 uv0(0,0); + static ImVec2 uv1(1,1); + static ImVec4 tint(1,1,1,1); + ImGui::SliderFloat2("Min", &bmin.x, -2, 2, "%.1f"); + ImGui::SliderFloat2("Max", &bmax.x, -2, 2, "%.1f"); + ImGui::SliderFloat2("UV0", &uv0.x, -2, 2, "%.1f"); + ImGui::SliderFloat2("UV1", &uv1.x, -2, 2, "%.1f"); + ImGui::ColorEdit4("Tint",&tint.x); + if (ImPlot::BeginPlot("##image")) { + ImPlot::PlotImage("my image",ImGui::GetIO().Fonts->TexID, bmin, bmax, uv0, uv1, tint); + ImPlot::EndPlot(); + } + } + //------------------------------------------------------------------------- if (ImGui::CollapsingHeader("Realtime Plots")) { ImGui::BulletText("Move your mouse to change the data!"); ImGui::BulletText("This example assumes 60 FPS. Higher FPS requires larger buffer size."); diff --git a/implot_items.cpp b/implot_items.cpp index b3cedff..9b7c931 100644 --- a/implot_items.cpp +++ b/implot_items.cpp @@ -1721,7 +1721,7 @@ void PlotImage(const char* label_id, ImTextureID user_texture_id, const ImPlotPo ImVec2 p2 = PlotToPixels(bmax.x, bmin.y); PushPlotClipRect(); DrawList.AddImage(user_texture_id, p1, p2, uv0, uv1, ImGui::ColorConvertFloat4ToU32(tint_col)); - PopPlotClipRect();; + PopPlotClipRect(); EndItem(); } } From 45e3433a1a84e7af172ead685cb7ee39d08d61f3 Mon Sep 17 00:00:00 2001 From: epezent Date: Thu, 17 Sep 2020 20:40:16 -0500 Subject: [PATCH 4/5] header cleanup --- implot.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/implot.h b/implot.h index 5af49b6..1fc1b8a 100644 --- a/implot.h +++ b/implot.h @@ -395,7 +395,7 @@ template IMPLOT_API void PlotDigital(const char* label_id, const T* IMPLOT_API void PlotDigitalG(const char* label_id, ImPlotPoint (*getter)(void* data, int idx), void* data, int count, int offset=0); // Plots an axis-aligned image. #bounds_min/bounds_max are in plot coordinatse (y-up) and #uv0/uv1 are in texture coordinates (y-down). -IMPLOT_API void PlotImage(const char* label_id, ImTextureID user_texture_id, const ImPlotPoint& bounds_min, const ImPlotPoint& bounds_max, const ImVec2& uv0 = ImVec2(0, 0), const ImVec2& uv1 = ImVec2(1,1), const ImVec4& tint_col = ImVec4(1,1,1,1)); +IMPLOT_API void PlotImage(const char* label_id, ImTextureID user_texture_id, const ImPlotPoint& bounds_min, const ImPlotPoint& bounds_max, const ImVec2& uv0=ImVec2(0,0), const ImVec2& uv1=ImVec2(1,1), const ImVec4& tint_col=ImVec4(1,1,1,1)); // Plots a centered text label at point x,y with optional pixel offset. Text color can be changed with ImPlot::PushStyleColor(ImPlotCol_InlayText, ...). IMPLOT_API void PlotText(const char* text, double x, double y, bool vertical=false, const ImVec2& pixel_offset=ImVec2(0,0)); @@ -594,7 +594,7 @@ IMPLOT_API void ShowStyleEditor(ImPlotStyle* ref = NULL); IMPLOT_API void ShowUserGuide(); // Sets the current _ImGui_ context. This is ONLY necessary if you are compiling -// ImPlot as a DLL (not recommended) separate from your ImGui compilation. It +// ImPlot as a DLL (not recommended) separate from your ImGui compilation. It // sets the global variable GImGui, which is not shared across DLL boundaries. // See GImGui documentation in imgui.cpp for more details. IMPLOT_API void SetImGuiContext(ImGuiContext* ctx); From 927c9c82a579a5940b1b068af39c7d0f8d9569c7 Mon Sep 17 00:00:00 2001 From: Evan Pezent Date: Thu, 17 Sep 2020 20:42:28 -0500 Subject: [PATCH 5/5] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 153174f..cc1f36a 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ ImPlot is an immediate mode, GPU accelerated plotting library for [Dear ImGui](h - stem plots - pie charts - heatmap charts + - images - and more likely to come - mix/match multiple plot items on a single plot - configurable axes ranges and scaling (linear/log)