1
0
Fork 0
mirror of https://github.com/gwm17/implot.git synced 2024-11-23 02:38:53 -05:00

make query ranges dragable

This commit is contained in:
Evan Pezent 2020-04-29 09:55:07 -05:00
parent 9fbacad16a
commit 6073a4ebb5
3 changed files with 88 additions and 16 deletions

View File

@ -221,7 +221,7 @@ struct ImPlotAxis {
/// Holds Plot state information that must persist between frames
struct ImPlot {
ImPlot() {
Selecting = Querying = Queried = false;
Selecting = Querying = Queried = DraggingQuery = false;
SelectStart = QueryStart = ImVec2(0,0);
Flags = ImPlotFlags_Default;
ColorIdx = 0;
@ -234,7 +234,8 @@ struct ImPlot {
bool Querying;
bool Queried;
ImVec2 QueryStart;
ImRect QueryRect;
ImRect QueryRect; // relative to BB_grid!!
bool DraggingQuery;
ImPlotRange QueryRange;
ImPlotAxis XAxis;
ImPlotAxis YAxis;
@ -657,6 +658,56 @@ bool BeginPlot(const char* title, const char* x_label, const char* y_label, cons
// legend hovered from last frame
const bool hov_legend = HasFlag(plot.Flags, ImPlotFlags_Legend) ? gp.Hov_Frame && plot.BB_Legend.Contains(IO.MousePos) : false;
bool hov_query = false;
if (plot.Queried && !plot.Querying) {
ImRect bb_query;
if (HasFlag(plot.Flags, ImPlotFlags_PixelQuery)) {
bb_query = plot.QueryRect;
bb_query.Min += gp.BB_Grid.Min;
bb_query.Max += gp.BB_Grid.Min;
}
else {
gp.UpdateTransforms();
ImVec2 p1 = gp.ToPixels(plot.QueryRange.XMin, plot.QueryRange.YMin);
ImVec2 p2 = gp.ToPixels(plot.QueryRange.XMax, plot.QueryRange.YMax);
bb_query.Min = ImVec2(ImMin(p1.x,p2.x), ImMin(p1.y,p2.y));
bb_query.Max = ImVec2(ImMax(p1.x,p2.x), ImMax(p1.y,p2.y));
}
hov_query = bb_query.Contains(IO.MousePos);
}
// QUERY DRAG -------------------------------------------------------------
if (plot.DraggingQuery && (IO.MouseReleased[0] || !IO.MouseDown[0])) {
plot.DraggingQuery = false;
}
if (plot.DraggingQuery) {
SetMouseCursor(ImGuiMouseCursor_ResizeAll);
if (!HasFlag(plot.Flags, ImPlotFlags_PixelQuery)) {
ImVec2 p1 = gp.ToPixels(plot.QueryRange.XMin, plot.QueryRange.YMin);
ImVec2 p2 = gp.ToPixels(plot.QueryRange.XMax, plot.QueryRange.YMax);
plot.QueryRect.Min = ImVec2(ImMin(p1.x,p2.x), ImMin(p1.y,p2.y)) + IO.MouseDelta;
plot.QueryRect.Max = ImVec2(ImMax(p1.x,p2.x), ImMax(p1.y,p2.y)) + IO.MouseDelta;
p1 = gp.FromPixels(plot.QueryRect.Min);
p2 = gp.FromPixels(plot.QueryRect.Max);
plot.QueryRect.Min -= gp.BB_Grid.Min;
plot.QueryRect.Max -= gp.BB_Grid.Min;
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);
}
else {
plot.QueryRect.Min += IO.MouseDelta;
plot.QueryRect.Max += IO.MouseDelta;
}
}
if (gp.Hov_Frame && hov_query && !plot.DraggingQuery && !plot.Selecting && !hov_legend) {
SetMouseCursor(ImGuiMouseCursor_ResizeAll);
if (IO.MouseDown[0] && !plot.XAxis.Dragging && !plot.YAxis.Dragging) {
plot.DraggingQuery = true;
}
}
// DRAG INPUT -------------------------------------------------------------
// end drags
@ -695,9 +746,9 @@ bool BeginPlot(const char* title, const char* x_label, const char* y_label, cons
ImGui::SetMouseCursor(ImGuiMouseCursor_ResizeAll);
}
// start drag
if (gp.Hov_Frame && hov_x_axis_region && IO.MouseDragMaxDistanceSqr[0] > 5 && !plot.Selecting && !hov_legend)
if (gp.Hov_Frame && hov_x_axis_region && IO.MouseDragMaxDistanceSqr[0] > 5 && !plot.Selecting && !hov_legend && !hov_query && !plot.DraggingQuery)
plot.XAxis.Dragging = true;
if (gp.Hov_Frame && hov_y_axis_region && IO.MouseDragMaxDistanceSqr[0] > 5 && !plot.Selecting && !hov_legend)
if (gp.Hov_Frame && hov_y_axis_region && IO.MouseDragMaxDistanceSqr[0] > 5 && !plot.Selecting && !hov_legend && !hov_query && !plot.DraggingQuery)
plot.YAxis.Dragging = true;
// SCROLL INPUT -----------------------------------------------------------
@ -767,6 +818,8 @@ bool BeginPlot(const char* title, const char* x_label, const char* y_label, cons
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.QueryRect.Min -= gp.BB_Grid.Min;
plot.QueryRect.Max -= gp.BB_Grid.Min;
plot.QueryRange.XMin = ImMin(p1.x, p2.x);
plot.QueryRange.XMax = ImMax(p1.x, p2.x);
plot.QueryRange.YMin = ImMin(p1.y, p2.y);
@ -810,7 +863,7 @@ bool BeginPlot(const char* title, const char* x_label, const char* y_label, cons
// DOUBLE CLICK -----------------------------------------------------------
if ( IO.MouseDoubleClicked[0] && gp.Hov_Frame && gp.Hov_Grid && !hov_legend)
if ( IO.MouseDoubleClicked[0] && gp.Hov_Frame && gp.Hov_Grid && !hov_legend && !hov_query)
gp.FitThisFrame = true;
else
gp.FitThisFrame = false;
@ -1078,8 +1131,8 @@ void EndPlot() {
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);
DrawList.AddRectFilled(plot.QueryRect.Min + gp.BB_Grid.Min, plot.QueryRect.Max + gp.BB_Grid.Min, gp.Col_QryBg);
DrawList.AddRect( plot.QueryRect.Min + gp.BB_Grid.Min, plot.QueryRect.Max + gp.BB_Grid.Min, gp.Col_QryBd);
}
}
else if (plot.Queried) {
@ -1273,8 +1326,8 @@ ImPlotRange GetPlotQuery() {
ImPlot& plot = *gp.CurrentPlot;
if (HasFlag(plot.Flags, ImPlotFlags_PixelQuery)) {
gp.UpdateTransforms();
ImVec2 p1 = gp.FromPixels(plot.QueryRect.Min);
ImVec2 p2 = gp.FromPixels(plot.QueryRect.Max);
ImVec2 p1 = gp.FromPixels(plot.QueryRect.Min + gp.BB_Grid.Min);
ImVec2 p2 = gp.FromPixels(plot.QueryRect.Max + gp.BB_Grid.Min);
plot.QueryRange.XMin = ImMin(p1.x, p2.x);
plot.QueryRange.XMax = ImMax(p1.x, p2.x);
plot.QueryRange.YMin = ImMin(p1.y, p2.y);

View File

@ -55,12 +55,21 @@ struct RollingData {
}
};
// Put big data here
struct DemoData {
DemoData() {
}
};
}
namespace ImGui {
void ShowImPlotDemoWindow(bool* p_open) {
static DemoData data;
ImVec2 main_viewport_pos = ImGui::GetMainViewport()->Pos;
ImGui::SetNextWindowPos(ImVec2(main_viewport_pos.x + 650, main_viewport_pos.y + 20), ImGuiCond_FirstUseEver);
ImGui::SetNextWindowSize(ImVec2(550, 680), ImGuiCond_FirstUseEver);
@ -389,6 +398,7 @@ void ShowImPlotDemoWindow(bool* p_open) {
//-------------------------------------------------------------------------
if (ImGui::CollapsingHeader("Drag and Drop")) {
srand(10000000 * ImGui::GetTime());
static bool paused = false;
static bool init = true;
static ScrollingData data[10];
@ -396,15 +406,17 @@ void ShowImPlotDemoWindow(bool* p_open) {
if (init) {
for (int i = 0; i < 10; ++i) {
show[i] = false;
data[i].AddPoint(0, 0.25f + 0.5f * (float)rand() / float(RAND_MAX));
}
init = false;
}
ImGui::BulletText("Drag data items from the left column onto the plot.");
ImGui::BeginGroup();
if (ImGui::Button("Clear", {100, 0})) {
for (int i = 0; i < 10; ++i)
for (int i = 0; i < 10; ++i) {
show[i] = false;
data[i].Data.shrink(0);
data[i].Offset = 0;
}
}
if (ImGui::Button(paused ? "Resume" : "Pause", {100,0}))
paused = !paused;
@ -425,7 +437,10 @@ void ShowImPlotDemoWindow(bool* p_open) {
if (!paused) {
t += ImGui::GetIO().DeltaTime;
for (int i = 0; i < 10; ++i) {
data[i].AddPoint(t, data[i].Data.back().y + (0.005f + 0.0002f * (float)rand() / float(RAND_MAX)) * (-1 + 2 * (float)rand() / float(RAND_MAX)));
if (show[i])
data[i].AddPoint(t, data[i].Data.empty() ?
0.25f + 0.5f * (float)rand() / float(RAND_MAX) :
data[i].Data.back().y + (0.005f + 0.0002f * (float)rand() / float(RAND_MAX)) * (-1 + 2 * (float)rand() / float(RAND_MAX)));
}
}
ImGui::SetNextPlotRangeX(t - 10, t, paused ? ImGuiCond_Once : ImGuiCond_Always);
@ -479,6 +494,10 @@ void ShowImPlotDemoWindow(bool* p_open) {
ImGui::RestorePlotPalette();
}
//-------------------------------------------------------------------------
// if (ImGui::CollapsingHeader("Benchmark")) {
// }
//-------------------------------------------------------------------------
ImGui::End();
}