1
0
Fork 0
mirror of https://github.com/gwm17/implot.git synced 2024-10-09 23:57:26 -04:00

better query

This commit is contained in:
Evan Pezent 2020-04-28 20:17:26 -05:00
parent 7d217904a8
commit 363d469122
3 changed files with 221 additions and 148 deletions

View File

@ -106,7 +106,7 @@ inline float ConstrainInf(float val) {
} }
inline float ConstrainLog(float val) { inline float ConstrainLog(float val) {
return val <= 0 ? 0.1 : val; return val <= 0 ? 0.001 : val;
} }
inline bool NanOrInf(float val) { inline bool NanOrInf(float val) {
@ -221,9 +221,8 @@ struct ImPlotAxis {
/// Holds Plot state information that must persist between frames /// Holds Plot state information that must persist between frames
struct ImPlot { struct ImPlot {
ImPlot() { ImPlot() {
Selecting = false; Selecting = Querying = Queried = false;
Querying = false; SelectStart = QueryStart = ImVec2(0,0);
SelectStart = {0,0};
Flags = ImPlotFlags_Default; Flags = ImPlotFlags_Default;
ColorIdx = 0; ColorIdx = 0;
} }
@ -231,8 +230,12 @@ struct ImPlot {
ImRect BB_Legend; ImRect BB_Legend;
bool Selecting; bool Selecting;
bool Querying;
ImVec2 SelectStart; ImVec2 SelectStart;
bool Querying;
bool Queried;
ImVec2 QueryStart;
ImRect QueryRect;
ImPlotRange QueryRange;
ImPlotAxis XAxis; ImPlotAxis XAxis;
ImPlotAxis YAxis; ImPlotAxis YAxis;
ImPlotFlags Flags; ImPlotFlags Flags;
@ -255,15 +258,12 @@ struct ImNextPlotData {
struct ImPlotContext { struct ImPlotContext {
ImPlotContext() { ImPlotContext() {
CurrentPlot = NULL; CurrentPlot = NULL;
PreviousPlot = NULL;
RestorePlotPalette(); RestorePlotPalette();
} }
/// ALl Plots /// ALl Plots
ImPool<ImPlot> Plots; ImPool<ImPlot> Plots;
/// Current Plot /// Current Plot
ImPlot* CurrentPlot; ImPlot* CurrentPlot;
/// Previous Plot
ImPlot* PreviousPlot;
// Legend // Legend
ImVector<int> _LegendIndices; ImVector<int> _LegendIndices;
@ -398,6 +398,8 @@ struct ImPlotContext {
ImVector<ImGuiColorMod> ColorModifiers; // Stack for PushStyleColor()/PopStyleColor() ImVector<ImGuiColorMod> ColorModifiers; // Stack for PushStyleColor()/PopStyleColor()
ImVector<ImGuiStyleMod> StyleModifiers; // Stack for PushStyleVar()/PopStyleVar() ImVector<ImGuiStyleMod> StyleModifiers; // Stack for PushStyleVar()/PopStyleVar()
ImNextPlotData NextPlotData; ImNextPlotData NextPlotData;
}; };
/// Global plot context /// Global plot context
@ -723,62 +725,87 @@ bool BeginPlot(const char* title, const char* x_label, const char* y_label, cons
} }
} }
// BOX-SELECTION ---------------------------------------------------------- // BOX-SELECTION AND QUERY ------------------------------------------------
// confirm selection // confirm selection
if ((plot.Selecting || plot.Querying) && IO.MouseReleased[1]) { if (plot.Selecting && (IO.MouseReleased[1] || !IO.MouseDown[1])) {
if (plot.Selecting && HasFlag(plot.Flags, ImPlotFlags_Selection)) { gp.UpdateTransforms();
gp.UpdateTransforms(); ImVec2 select_size = plot.SelectStart - IO.MousePos;
ImVec2 select_size = plot.SelectStart - IO.MousePos; if (HasFlag(plot.Flags, ImPlotFlags_Selection) && ImFabs(select_size.x) > 2 && ImFabs(select_size.y) > 2) {
if (ImFabs(select_size.x) > 2 && ImFabs(select_size.y) > 2) { ImVec2 p1 = gp.FromPixels(plot.SelectStart);
ImVec2 p1 = gp.FromPixels(plot.SelectStart); ImVec2 p2 = gp.FromPixels(IO.MousePos);
ImVec2 p2 = gp.FromPixels(IO.MousePos); if (!lock_x_min && !IO.KeyAlt)
if (!lock_x_min && !IO.KeyAlt) plot.XAxis.Min = ImMin(p1.x, p2.x);
plot.XAxis.Min = ImMin(p1.x, p2.x); if (!lock_x_max && !IO.KeyAlt)
if (!lock_x_max && !IO.KeyAlt) plot.XAxis.Max = ImMax(p1.x, p2.x);
plot.XAxis.Max = ImMax(p1.x, p2.x); if (!lock_y_min && !IO.KeyShift)
if (!lock_y_min && !IO.KeyShift) plot.YAxis.Min = ImMin(p1.y, p2.y);
plot.YAxis.Min = ImMin(p1.y, p2.y); if (!lock_y_max && !IO.KeyShift)
if (!lock_y_max && !IO.KeyShift) plot.YAxis.Max = ImMax(p1.y, p2.y);
plot.YAxis.Max = ImMax(p1.y, p2.y);
}
} }
plot.Selecting = false; plot.Selecting = false;
plot.Querying = false;
}
if (plot.Querying && IO.MouseReleased[2]) {
plot.Querying = false;
} }
// bad selection // bad selection
if (plot.Selecting && (!HasFlag(plot.Flags, ImPlotFlags_Selection) || lock_plot) && ImLengthSqr(plot.SelectStart - IO.MousePos) > 4) { if (plot.Selecting && (!HasFlag(plot.Flags, ImPlotFlags_Selection) || lock_plot) && ImLengthSqr(plot.SelectStart - IO.MousePos) > 4) {
ImGui::SetMouseCursor(ImGuiMouseCursor_NotAllowed); ImGui::SetMouseCursor(ImGuiMouseCursor_NotAllowed);
} }
// toggle between select/query
if (plot.Selecting && IO.KeyCtrl) {
plot.Selecting = false;
plot.Querying = true;
}
if (plot.Querying && !IO.KeyCtrl && !IO.MouseDown[2]) {
plot.Selecting = true;
plot.Querying = false;
}
// cancel selection // cancel selection
if ((plot.Selecting || plot.Querying) && (IO.MouseClicked[0] || IO.MouseDown[0])) { if (plot.Selecting && (IO.MouseClicked[0] || IO.MouseDown[0])) {
plot.Selecting = false; plot.Selecting = false;
plot.Querying = false;
} }
// begin selection or query // begin selection or query
if (gp.Hov_Frame && gp.Hov_Grid && IO.MouseClicked[1]) { if (gp.Hov_Frame && gp.Hov_Grid && IO.MouseClicked[1]) {
plot.SelectStart = IO.MousePos; plot.SelectStart = IO.MousePos;
if (IO.KeyCtrl) plot.Selecting = true;
plot.Querying = true; }
else // update query
plot.Selecting = true; if (plot.Querying) {
gp.UpdateTransforms();
plot.QueryRect.Min.x = IO.KeyAlt ? gp.BB_Grid.Min.x : ImMin(plot.QueryStart.x, IO.MousePos.x);
plot.QueryRect.Max.x = IO.KeyAlt ? gp.BB_Grid.Max.x : ImMax(plot.QueryStart.x, IO.MousePos.x);
plot.QueryRect.Min.y = IO.KeyShift ? gp.BB_Grid.Min.y : ImMin(plot.QueryStart.y, IO.MousePos.y);
plot.QueryRect.Max.y = IO.KeyShift ? gp.BB_Grid.Max.y : ImMax(plot.QueryStart.y, IO.MousePos.y);
ImVec2 p1 = gp.FromPixels(plot.QueryRect.Min);
ImVec2 p2 = gp.FromPixels(plot.QueryRect.Max);
plot.QueryRange.XMin = ImMin(p1.x, p2.x);
plot.QueryRange.XMax = ImMax(p1.x, p2.x);
plot.QueryRange.YMin = ImMin(p1.y, p2.y);
plot.QueryRange.YMax = ImMax(p1.y, p2.y);
}
// end query
if (plot.Querying && (IO.MouseReleased[2] || IO.MouseReleased[1])) {
plot.Querying = false;
if (plot.QueryRect.GetWidth() > 2 && plot.QueryRect.GetHeight() > 2) {
plot.Queried = true;
}
else {
plot.Queried = false;
plot.QueryRange = ImPlotRange();
}
} }
// begin query // begin query
if (gp.Hov_Frame && gp.Hov_Grid && IO.MouseClicked[2]) { if ((gp.Hov_Frame && gp.Hov_Grid && IO.MouseClicked[2])) {
plot.SelectStart = IO.MousePos; plot.QueryRect = ImRect(0,0,0,0);
plot.QueryRange = ImPlotRange();
plot.Querying = true; plot.Querying = true;
plot.Queried = true;
plot.QueryStart = IO.MousePos;
}
// toggle between select/query
if (plot.Selecting && IO.KeyCtrl) {
plot.Selecting = false;
plot.QueryRect = ImRect(0,0,0,0);
plot.QueryRange = ImPlotRange();
plot.Querying = true;
plot.Queried = true;
plot.QueryStart = plot.SelectStart;
}
if (plot.Querying && !IO.KeyCtrl && !IO.MouseDown[2]) {
plot.Selecting = true;
plot.Querying = false;
plot.Queried = false;
plot.QueryRect = ImRect(0,0,0,0);
plot.QueryRange = ImPlotRange();
} }
// DOUBLE CLICK ----------------------------------------------------------- // DOUBLE CLICK -----------------------------------------------------------
@ -948,27 +975,29 @@ void PlotContextMenu(ImPlot& plot) {
} }
ImGui::Separator(); ImGui::Separator();
if ((ImGui::BeginMenu("Settings"))) { if ((ImGui::BeginMenu("Settings"))) {
if (ImGui::MenuItem("Box Select",NULL,HasFlag(plot.Flags, ImPlotFlags_Selection))) {
FlipFlag(plot.Flags, ImPlotFlags_Selection);
}
if (ImGui::MenuItem("Pixel Query",NULL,HasFlag(plot.Flags, ImPlotFlags_PixelQuery))) {
FlipFlag(plot.Flags, ImPlotFlags_PixelQuery);
}
if (ImGui::MenuItem("Crosshairs",NULL,HasFlag(plot.Flags, ImPlotFlags_Crosshairs))) {
FlipFlag(plot.Flags, ImPlotFlags_Crosshairs);
}
if (ImGui::MenuItem("Mouse Position",NULL,HasFlag(plot.Flags, ImPlotFlags_MousePos))) {
FlipFlag(plot.Flags, ImPlotFlags_MousePos);
}
if (ImGui::MenuItem("Cull Data",NULL,HasFlag(plot.Flags, ImPlotFlags_CullData))) { if (ImGui::MenuItem("Cull Data",NULL,HasFlag(plot.Flags, ImPlotFlags_CullData))) {
FlipFlag(plot.Flags, ImPlotFlags_CullData); FlipFlag(plot.Flags, ImPlotFlags_CullData);
} }
if (ImGui::MenuItem("Anti-Aliased Lines",NULL,HasFlag(plot.Flags, ImPlotFlags_AntiAliased))) { if (ImGui::MenuItem("Anti-Aliased Lines",NULL,HasFlag(plot.Flags, ImPlotFlags_AntiAliased))) {
FlipFlag(plot.Flags, ImPlotFlags_AntiAliased); FlipFlag(plot.Flags, ImPlotFlags_AntiAliased);
} }
if (ImGui::MenuItem("Mouse Position",NULL,HasFlag(plot.Flags, ImPlotFlags_MousePos))) {
FlipFlag(plot.Flags, ImPlotFlags_MousePos);
}
if (ImGui::MenuItem("Selection Box",NULL,HasFlag(plot.Flags, ImPlotFlags_Selection))) {
FlipFlag(plot.Flags, ImPlotFlags_Selection);
}
ImGui::EndMenu(); ImGui::EndMenu();
} }
if (ImGui::MenuItem("Legend",NULL,HasFlag(plot.Flags, ImPlotFlags_Legend))) { if (ImGui::MenuItem("Legend",NULL,HasFlag(plot.Flags, ImPlotFlags_Legend))) {
FlipFlag(plot.Flags, ImPlotFlags_Legend); FlipFlag(plot.Flags, ImPlotFlags_Legend);
} }
if (ImGui::MenuItem("Crosshairs",NULL,HasFlag(plot.Flags, ImPlotFlags_Crosshairs))) {
FlipFlag(plot.Flags, ImPlotFlags_Crosshairs);
}
#if 0 #if 0
if (ImGui::BeginMenu("Metrics")) { if (ImGui::BeginMenu("Metrics")) {
ImGui::PushItemWidth(75); ImGui::PushItemWidth(75);
@ -1014,6 +1043,54 @@ void EndPlot() {
PushClipRect(gp.BB_Grid.Min, gp.BB_Grid.Max, true); PushClipRect(gp.BB_Grid.Min, gp.BB_Grid.Max, true);
// render ticks
if (HasFlag(plot.XAxis.Flags, ImAxisFlags_TickMarks)) {
for (auto &xt : gp.XTicks)
DrawList.AddLine({xt.PixelPos, gp.BB_Grid.Max.y},{xt.PixelPos, gp.BB_Grid.Max.y - (xt.Major ? 10.0f : 5.0f)}, gp.Col_Border, 1);
}
if (HasFlag(plot.YAxis.Flags, ImAxisFlags_TickMarks)) {
for (auto &yt : gp.YTicks)
DrawList.AddLine({gp.BB_Grid.Min.x, yt.PixelPos}, {gp.BB_Grid.Min.x + (yt.Major ? 10.0f : 5.0f), yt.PixelPos}, gp.Col_Border, 1);
}
// render selection/query
if (plot.Selecting) {
ImRect select_bb(ImMin(IO.MousePos, plot.SelectStart), ImMax(IO.MousePos, plot.SelectStart));
if (plot.Selecting && !lock_plot && HasFlag(plot.Flags, ImPlotFlags_Selection)) {
if (IO.KeyAlt && IO.KeyShift && select_bb.GetWidth() > 2 && select_bb.GetHeight() > 2) {
DrawList.AddRectFilled(gp.BB_Grid.Min, gp.BB_Grid.Max, gp.Col_SlctBg);
DrawList.AddRect( gp.BB_Grid.Min, gp.BB_Grid.Max, gp.Col_SlctBd);
}
else if ((lock_x || IO.KeyAlt) && select_bb.GetHeight() > 2) {
DrawList.AddRectFilled(ImVec2(gp.BB_Grid.Min.x, select_bb.Min.y), ImVec2(gp.BB_Grid.Max.x, select_bb.Max.y), gp.Col_SlctBg);
DrawList.AddRect( ImVec2(gp.BB_Grid.Min.x, select_bb.Min.y), ImVec2(gp.BB_Grid.Max.x, select_bb.Max.y), gp.Col_SlctBd);
}
else if ((lock_y || IO.KeyShift) && select_bb.GetWidth() > 2) {
DrawList.AddRectFilled(ImVec2(select_bb.Min.x, gp.BB_Grid.Min.y), ImVec2(select_bb.Max.x, gp.BB_Grid.Max.y), gp.Col_SlctBg);
DrawList.AddRect( ImVec2(select_bb.Min.x, gp.BB_Grid.Min.y), ImVec2(select_bb.Max.x, gp.BB_Grid.Max.y), gp.Col_SlctBd);
}
else if (select_bb.GetWidth() > 2 && select_bb.GetHeight() > 2) {
DrawList.AddRectFilled(select_bb.Min, select_bb.Max, gp.Col_SlctBg);
DrawList.AddRect( select_bb.Min, select_bb.Max, gp.Col_SlctBd);
}
}
}
if (plot.Querying || (HasFlag(plot.Flags, ImPlotFlags_PixelQuery) && plot.Queried)) {
if (plot.QueryRect.GetWidth() > 2 && plot.QueryRect.GetHeight() > 2) {
DrawList.AddRectFilled(plot.QueryRect.Min, plot.QueryRect.Max, gp.Col_QryBg);
DrawList.AddRect( plot.QueryRect.Min, plot.QueryRect.Max, gp.Col_QryBd);
}
}
else if (plot.Queried) {
ImVec2 p1 = gp.ToPixels(plot.QueryRange.XMin, plot.QueryRange.YMin);
ImVec2 p2 = gp.ToPixels(plot.QueryRange.XMax, plot.QueryRange.YMax);
ImVec2 Min(ImMin(p1.x,p2.x), ImMin(p1.y,p2.y));
ImVec2 Max(ImMax(p1.x,p2.x), ImMax(p1.y,p2.y));
DrawList.AddRectFilled(Min, Max, gp.Col_QryBg);
DrawList.AddRect( Min, Max, gp.Col_QryBd);
}
// render legend // render legend
const float txt_ht = GetTextLineHeight(); const float txt_ht = GetTextLineHeight();
const ImVec2 legend_offset(10, 10); const ImVec2 legend_offset(10, 10);
@ -1072,57 +1149,6 @@ void EndPlot() {
} }
} }
// render ticks
if (HasFlag(plot.XAxis.Flags, ImAxisFlags_TickMarks)) {
for (auto &xt : gp.XTicks)
DrawList.AddLine({xt.PixelPos, gp.BB_Grid.Max.y},{xt.PixelPos, gp.BB_Grid.Max.y - (xt.Major ? 10.0f : 5.0f)}, gp.Col_Border, 1);
}
if (HasFlag(plot.YAxis.Flags, ImAxisFlags_TickMarks)) {
for (auto &yt : gp.YTicks)
DrawList.AddLine({gp.BB_Grid.Min.x, yt.PixelPos}, {gp.BB_Grid.Min.x + (yt.Major ? 10.0f : 5.0f), yt.PixelPos}, gp.Col_Border, 1);
}
// render selection/query
if (plot.Selecting || plot.Querying) {
ImRect select_bb(ImMin(IO.MousePos, plot.SelectStart), ImMax(IO.MousePos, plot.SelectStart));
if (plot.Selecting && !lock_plot && HasFlag(plot.Flags, ImPlotFlags_Selection)) {
if (IO.KeyAlt && IO.KeyShift && select_bb.GetWidth() > 2 && select_bb.GetHeight() > 2) {
DrawList.AddRectFilled(gp.BB_Grid.Min, gp.BB_Grid.Max, gp.Col_SlctBg);
DrawList.AddRect( gp.BB_Grid.Min, gp.BB_Grid.Max, gp.Col_SlctBd);
}
else if ((lock_x || IO.KeyAlt) && select_bb.GetHeight() > 2) {
DrawList.AddRectFilled(ImVec2(gp.BB_Grid.Min.x, select_bb.Min.y), ImVec2(gp.BB_Grid.Max.x, select_bb.Max.y), gp.Col_SlctBg);
DrawList.AddRect( ImVec2(gp.BB_Grid.Min.x, select_bb.Min.y), ImVec2(gp.BB_Grid.Max.x, select_bb.Max.y), gp.Col_SlctBd);
}
else if ((lock_y || IO.KeyShift) && select_bb.GetWidth() > 2) {
DrawList.AddRectFilled(ImVec2(select_bb.Min.x, gp.BB_Grid.Min.y), ImVec2(select_bb.Max.x, gp.BB_Grid.Max.y), gp.Col_SlctBg);
DrawList.AddRect( ImVec2(select_bb.Min.x, gp.BB_Grid.Min.y), ImVec2(select_bb.Max.x, gp.BB_Grid.Max.y), gp.Col_SlctBd);
}
else if (select_bb.GetWidth() > 2 && select_bb.GetHeight() > 2) {
DrawList.AddRectFilled(select_bb.Min, select_bb.Max, gp.Col_SlctBg);
DrawList.AddRect( select_bb.Min, select_bb.Max, gp.Col_SlctBd);
}
}
else if (plot.Querying && select_bb.GetWidth() > 2 && select_bb.GetHeight() > 2) {
if (IO.KeyAlt && IO.KeyShift && select_bb.GetWidth() > 2 && select_bb.GetHeight() > 2) {
DrawList.AddRectFilled(gp.BB_Grid.Min, gp.BB_Grid.Max, gp.Col_QryBg);
DrawList.AddRect( gp.BB_Grid.Min, gp.BB_Grid.Max, gp.Col_QryBd);
}
else if (IO.KeyAlt && select_bb.GetHeight() > 2) {
DrawList.AddRectFilled(ImVec2(gp.BB_Grid.Min.x, select_bb.Min.y), ImVec2(gp.BB_Grid.Max.x, select_bb.Max.y), gp.Col_QryBg);
DrawList.AddRect( ImVec2(gp.BB_Grid.Min.x, select_bb.Min.y), ImVec2(gp.BB_Grid.Max.x, select_bb.Max.y), gp.Col_QryBd);
}
else if (IO.KeyShift && select_bb.GetWidth() > 2) {
DrawList.AddRectFilled(ImVec2(select_bb.Min.x, gp.BB_Grid.Min.y), ImVec2(select_bb.Max.x, gp.BB_Grid.Max.y), gp.Col_QryBg);
DrawList.AddRect( ImVec2(select_bb.Min.x, gp.BB_Grid.Min.y), ImVec2(select_bb.Max.x, gp.BB_Grid.Max.y), gp.Col_QryBd);
}
else if (select_bb.GetWidth() > 2 && select_bb.GetHeight() > 2) {
DrawList.AddRectFilled(select_bb.Min, select_bb.Max, gp.Col_QryBg);
DrawList.AddRect( select_bb.Min, select_bb.Max, gp.Col_QryBd);
}
}
}
// render crosshairs // render crosshairs
if (HasFlag(plot.Flags, ImPlotFlags_Crosshairs) && gp.Hov_Grid && gp.Hov_Frame && if (HasFlag(plot.Flags, ImPlotFlags_Crosshairs) && gp.Hov_Grid && gp.Hov_Frame &&
!(plot.XAxis.Dragging || plot.YAxis.Dragging) && !plot.Selecting && !plot.Querying && !hov_legend) { !(plot.XAxis.Dragging || plot.YAxis.Dragging) && !plot.Selecting && !plot.Querying && !hov_legend) {
@ -1158,7 +1184,7 @@ void EndPlot() {
// FIT DATA -------------------------------------------------------------- // FIT DATA --------------------------------------------------------------
if (gp.FitThisFrame && gp.VisibleItemCount > 0) { if (gp.FitThisFrame && (gp.VisibleItemCount > 0 || plot.Queried)) {
if (!HasFlag(plot.XAxis.Flags, ImAxisFlags_LockMin) && !NanOrInf(gp.Extents.Min.x)) if (!HasFlag(plot.XAxis.Flags, ImAxisFlags_LockMin) && !NanOrInf(gp.Extents.Min.x))
plot.XAxis.Min = gp.Extents.Min.x; plot.XAxis.Min = gp.Extents.Min.x;
if (!HasFlag(plot.XAxis.Flags, ImAxisFlags_LockMax) && !NanOrInf(gp.Extents.Max.x)) if (!HasFlag(plot.XAxis.Flags, ImAxisFlags_LockMax) && !NanOrInf(gp.Extents.Max.x))
@ -1182,7 +1208,6 @@ void EndPlot() {
// Reset legend items // Reset legend items
gp._LegendIndices.shrink(0); gp._LegendIndices.shrink(0);
// Null current plot/data // Null current plot/data
gp.PreviousPlot = gp.CurrentPlot;
gp.CurrentPlot = NULL; gp.CurrentPlot = NULL;
// Reset next plot data // Reset next plot data
gp.NextPlotData = ImNextPlotData(); gp.NextPlotData = ImNextPlotData();
@ -1217,44 +1242,45 @@ void SetNextPlotRangeY(float y_min, float y_max, ImGuiCond cond) {
gp.NextPlotData.YMax = y_max; gp.NextPlotData.YMax = y_max;
} }
bool IsPlotHovered() { return gp.Hov_Grid; } bool IsPlotHovered() {
ImVec2 GetPlotMousePos() { return gp.LastMousePos; } IM_ASSERT_USER_ERROR(gp.CurrentPlot != NULL, "IsPlotHovered() Needs to be called between BeginPlot() and EndPlot()!");
return gp.Hov_Grid;
}
ImVec2 GetPlotMousePos() {
IM_ASSERT_USER_ERROR(gp.CurrentPlot != NULL, "GetPlotMousePos() Needs to be called between BeginPlot() and EndPlot()!");
return gp.LastMousePos;
}
ImPlotRange GetPlotRange() { ImPlotRange GetPlotRange() {
ImPlot* plt = gp.CurrentPlot ? gp.CurrentPlot : gp.PreviousPlot ? gp.PreviousPlot : NULL; IM_ASSERT_USER_ERROR(gp.CurrentPlot != NULL, "GetPlotRange() Needs to be called between BeginPlot() and EndPlot()!");
if (plt == NULL) ImPlot& plot = *gp.CurrentPlot;
return ImPlotRange();
ImPlotRange range; ImPlotRange range;
range.XMin = plt->XAxis.Min; range.XMin = plot.XAxis.Min;
range.XMax = plt->XAxis.Max; range.XMax = plot.XAxis.Max;
range.YMin = plt->YAxis.Min; range.YMin = plot.YAxis.Min;
range.YMax = plt->YAxis.Max; range.YMax = plot.YAxis.Max;
return range; return range;
} }
bool IsPlotQueried() { bool IsPlotQueried() {
if (gp.CurrentPlot) { IM_ASSERT_USER_ERROR(gp.CurrentPlot != NULL, "IsPlotQueried() Needs to be called between BeginPlot() and EndPlot()!");
return gp.CurrentPlot->Querying; return gp.CurrentPlot->Queried;
}
else if (gp.PreviousPlot) {
return gp.PreviousPlot->Querying;
}
return false;
} }
ImPlotRange GetPlotQuery() { ImPlotRange GetPlotQuery() {
ImPlot* plt = gp.CurrentPlot ? gp.CurrentPlot : gp.PreviousPlot ? gp.PreviousPlot : NULL; IM_ASSERT_USER_ERROR(gp.CurrentPlot != NULL, "GetPlotQuery() Needs to be called between BeginPlot() and EndPlot()!");
if (plt == NULL) ImPlot& plot = *gp.CurrentPlot;
return ImPlotRange(); if (HasFlag(plot.Flags, ImPlotFlags_PixelQuery)) {
ImVec2 p1 = gp.FromPixels(plt->SelectStart); gp.UpdateTransforms();
ImVec2 p2 = gp.FromPixels(ImGui::GetIO().MousePos); ImVec2 p1 = gp.FromPixels(plot.QueryRect.Min);
ImVec2 p2 = gp.FromPixels(plot.QueryRect.Max);
ImPlotRange range; plot.QueryRange.XMin = ImMin(p1.x, p2.x);
range.XMin = ImGui::GetIO().KeyAlt ? plt->XAxis.Min : ImMin(p1.x, p2.x); plot.QueryRange.XMax = ImMax(p1.x, p2.x);
range.XMax = ImGui::GetIO().KeyAlt ? plt->XAxis.Max : ImMax(p1.x, p2.x); plot.QueryRange.YMin = ImMin(p1.y, p2.y);
range.YMin = ImGui::GetIO().KeyShift ? plt->YAxis.Min : ImMin(p1.y, p2.y); plot.QueryRange.YMax = ImMax(p1.y, p2.y);
range.YMax = ImGui::GetIO().KeyShift ? plt->YAxis.Max : ImMax(p1.y, p2.y); }
return range; return plot.QueryRange;
} }
//============================================================================= //=============================================================================

View File

@ -41,10 +41,11 @@ enum ImPlotFlags_ {
ImPlotFlags_Legend = 1 << 1, // a legend will be displayed in the top-left ImPlotFlags_Legend = 1 << 1, // a legend will be displayed in the top-left
ImPlotFlags_Highlight = 1 << 2, // plot items will be highlighted when their legend entry is hovered ImPlotFlags_Highlight = 1 << 2, // plot items will be highlighted when their legend entry is hovered
ImPlotFlags_Selection = 1 << 3, // the user will be able to box-select with right-mouse ImPlotFlags_Selection = 1 << 3, // the user will be able to box-select with right-mouse
ImPlotFlags_ContextMenu = 1 << 4, // the user will be able to open a context menu with double-right click ImPlotFlags_PixelQuery = 1 << 4, // query ranges will not change their pixel position if the plot is scrolled/zoomed
ImPlotFlags_Crosshairs = 1 << 5, // the default mouse cursor will be replaced with a crosshair when hovered ImPlotFlags_ContextMenu = 1 << 5, // the user will be able to open a context menu with double-right click
ImPlotFlags_CullData = 1 << 6, // plot data outside the plot area will be culled from rendering ImPlotFlags_Crosshairs = 1 << 6, // the default mouse cursor will be replaced with a crosshair when hovered
ImPlotFlags_AntiAliased = 1 << 7, // lines and fills will be anti-aliased (not recommended) ImPlotFlags_CullData = 1 << 7, // plot data outside the plot area will be culled from rendering
ImPlotFlags_AntiAliased = 1 << 8, // lines and fills will be anti-aliased (not recommended)
ImPlotFlags_Default = ImPlotFlags_MousePos | ImPlotFlags_Legend | ImPlotFlags_Highlight | ImPlotFlags_Selection | ImPlotFlags_ContextMenu | ImPlotFlags_CullData ImPlotFlags_Default = ImPlotFlags_MousePos | ImPlotFlags_Legend | ImPlotFlags_Highlight | ImPlotFlags_Selection | ImPlotFlags_ContextMenu | ImPlotFlags_CullData
}; };
@ -224,4 +225,7 @@ void PopPlotStyleVar(int count = 1);
// Shows the ImPlot demo. Add implot_demo.cpp to your sources! // Shows the ImPlot demo. Add implot_demo.cpp to your sources!
void ShowImPlotDemoWindow(bool* p_open = NULL); void ShowImPlotDemoWindow(bool* p_open = NULL);
// Shows the ImPlot benchmarking test. Add implot_demo.cpp to your sources!
void ShowImPlotBenchmarkWindow(bool * p_open = NULL);
} // namespace ImGui } // namespace ImGui

View File

@ -76,6 +76,7 @@ void ShowImPlotDemoWindow(bool* p_open) {
ImGui::Indent(); ImGui::Indent();
ImGui::BulletText("Hold Alt to expand box selection horizontally."); ImGui::BulletText("Hold Alt to expand box selection horizontally.");
ImGui::BulletText("Hold Shift to expand box selection vertically."); ImGui::BulletText("Hold Shift to expand box selection vertically.");
ImGui::BulletText("Left click while box selecting to cancel the selection.");
ImGui::Unindent(); ImGui::Unindent();
ImGui::BulletText("Middle click (or Ctrl + right click) and drag to create a query range."); ImGui::BulletText("Middle click (or Ctrl + right click) and drag to create a query range.");
ImGui::Indent(); ImGui::Indent();
@ -290,7 +291,7 @@ void ShowImPlotDemoWindow(bool* p_open) {
} }
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
if (ImGui::CollapsingHeader("Log Scale")) { if (ImGui::CollapsingHeader("Log Scale")) {
ImGui::BulletText("Open the plot context menu (double right click) to scales."); ImGui::BulletText("Open the plot context menu (double right click) to change scales.");
static float xs[1001], ys1[1001], ys2[1001], ys3[1001]; static float xs[1001], ys1[1001], ys2[1001], ys3[1001];
for (int i = 0; i < 1001; ++i) { for (int i = 0; i < 1001; ++i) {
xs[i] = (float)(i*0.1f); xs[i] = (float)(i*0.1f);
@ -313,6 +314,7 @@ void ShowImPlotDemoWindow(bool* p_open) {
ImGui::BulletText("Middle click (or Ctrl + right click) and drag to query points."); ImGui::BulletText("Middle click (or Ctrl + right click) and drag to query points.");
ImGui::BulletText("Hold the Alt and/or Shift keys to expand the query range."); ImGui::BulletText("Hold the Alt and/or Shift keys to expand the query range.");
static ImVector<ImVec2> data; static ImVector<ImVec2> data;
ImPlotRange range, query;
if (ImGui::BeginPlot("##Drawing", NULL, NULL, ImVec2(-1,300), ImPlotFlags_Default, ImAxisFlags_GridLines, ImAxisFlags_GridLines)) { if (ImGui::BeginPlot("##Drawing", NULL, NULL, ImVec2(-1,300), ImPlotFlags_Default, ImAxisFlags_GridLines, ImAxisFlags_GridLines)) {
if (ImGui::IsPlotHovered() && ImGui::IsMouseClicked(0) && ImGui::GetIO().KeyCtrl) if (ImGui::IsPlotHovered() && ImGui::IsMouseClicked(0) && ImGui::GetIO().KeyCtrl)
data.push_back(ImGui::GetPlotMousePos()); data.push_back(ImGui::GetPlotMousePos());
@ -338,11 +340,52 @@ void ShowImPlotDemoWindow(bool* p_open) {
} }
} }
ImGui::PopPlotStyleVar(2); ImGui::PopPlotStyleVar(2);
range = ImGui::GetPlotRange();
query = ImGui::GetPlotQuery();
ImGui::EndPlot(); ImGui::EndPlot();
} }
ImPlotRange range = ImGui::GetPlotRange(); ImGui::Text("The current plot range is: [%g,%g,%g,%g]", range.XMin, range.XMax, range.YMin, range.YMax);
ImGui::Text("The current plot range is: [%g,%g,%g,%g]", range.XMin, range.XMax, range.YMin, range.YMax); ImGui::Text("The current query range is: [%g,%g,%g,%g]", query.XMin, query.XMax, query.YMin, query.YMax);
} }
//-------------------------------------------------------------------------
if (ImGui::CollapsingHeader("Views")) {
// mimic's soulthread's imgui_plot demo
static float x_data[512];
static float y_data1[512];
static float y_data2[512];
static float y_data3[512];
static float sampling_freq = 44100;
static float freq = 500;
for (size_t i = 0; i < 512; ++i) {
const float t = i / sampling_freq;
x_data[i] = t;
const float arg = 2 * 3.14 * freq * t;
y_data1[i] = sin(arg);
y_data2[i] = y_data1[i] * -0.6 + sin(2 * arg) * 0.4;
y_data3[i] = y_data2[i] * -0.6 + sin(3 * arg) * 0.4;
}
ImGui::BulletText("Query the first plot to render a subview in the second plot.");
ImGui::BulletText("Toggle \"Pixel Query\" in the context menu and then pan the plot.");
ImGui::SetNextPlotRange(0,0.01f,-1,1);
ImAxisFlags flgs = ImAxisFlags_Default & ~ImAxisFlags_TickLabels;
ImPlotRange query;
if (ImGui::BeginPlot("##View1",NULL,NULL,ImVec2(-1,150), ImPlotFlags_Default, flgs, flgs)) {
ImGui::Plot("Signal 1", x_data, y_data1, 512);
ImGui::Plot("Signal 2", x_data, y_data2, 512);
ImGui::Plot("Signal 3", x_data, y_data3, 512);
query = ImGui::GetPlotQuery();
ImGui::EndPlot();
}
ImGui::SetNextPlotRange(query.XMin, query.XMax, query.YMin, query.YMax, ImGuiCond_Always);
if (ImGui::BeginPlot("##View2",NULL,NULL,ImVec2(-1,150), 0, 0, 0)) {
ImGui::Plot("Signal 1", x_data, y_data1, 512);
ImGui::Plot("Signal 2", x_data, y_data2, 512);
ImGui::Plot("Signal 3", x_data, y_data3, 512);
ImGui::EndPlot();
}
}
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
if (ImGui::CollapsingHeader("Drag and Drop")) { if (ImGui::CollapsingHeader("Drag and Drop")) {