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

Merge branch 'legend-dnd' of https://github.com/PeterJohnson/implot into PeterJohnson-legend-dnd

This commit is contained in:
epezent 2020-08-31 23:28:40 -05:00
commit fa2c704bb2
3 changed files with 81 additions and 1 deletions

View File

@ -1826,6 +1826,75 @@ bool IsLegendEntryHovered(const char* label_id) {
return false; return false;
} }
bool BeginLegendDragDropSource(const char* label_id, ImGuiDragDropFlags flags) {
ImPlotContext& gp = *GImPlot;
IM_ASSERT_USER_ERROR(gp.CurrentPlot != NULL, "BeginLegendDragDropSource() needs to be called between BeginPlot() and EndPlot()!");
ImGuiID source_id = ImGui::GetID(label_id);
ImPlotItem* item = gp.CurrentPlot->Items.GetByKey(source_id);
bool is_hovered = item && item->Highlight;
ImGuiContext& g = *GImGui;
ImGuiWindow* window = g.CurrentWindow;
ImGuiMouseButton mouse_button = ImGuiMouseButton_Left;
if (g.IO.MouseDown[mouse_button] == false) {
if (g.ActiveId == source_id)
ImGui::ClearActiveID();
return false;
}
if (is_hovered && g.IO.MouseClicked[mouse_button]) {
ImGui::SetActiveID(source_id, window);
ImGui::FocusWindow(window);
}
if (g.ActiveId != source_id)
return false;
// Allow the underlying widget to display/return hovered during the mouse
// release frame, else we would get a flicker.
g.ActiveIdAllowOverlap = is_hovered;
// Disable navigation and key inputs while dragging
g.ActiveIdUsingNavDirMask = ~(ImU32)0;
g.ActiveIdUsingNavInputMask = ~(ImU32)0;
g.ActiveIdUsingKeyInputMask = ~(ImU64)0;
if (ImGui::IsMouseDragging(mouse_button)) {
if (!g.DragDropActive) {
ImGui::ClearDragDrop();
ImGuiPayload& payload = g.DragDropPayload;
payload.SourceId = source_id;
payload.SourceParentId = 0;
g.DragDropActive = true;
g.DragDropSourceFlags = 0;
g.DragDropMouseButton = mouse_button;
}
g.DragDropSourceFrameCount = g.FrameCount;
g.DragDropWithinSource = true;
if (!(flags & ImGuiDragDropFlags_SourceNoPreviewTooltip)) {
// Target can request the Source to not display its tooltip (we use a
// dedicated flag to make this request explicit) We unfortunately can't
// just modify the source flags and skip the call to BeginTooltip, as
// caller may be emitting contents.
ImGui::BeginTooltip();
if (g.DragDropAcceptIdPrev && (g.DragDropAcceptFlags & ImGuiDragDropFlags_AcceptNoPreviewTooltip)) {
ImGuiWindow* tooltip_window = g.CurrentWindow;
tooltip_window->SkipItems = true;
tooltip_window->HiddenFramesCanSkipItems = 1;
}
}
return true;
}
return false;
}
void EndDragDropSource() {
ImGui::EndDragDropSource();
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// STYLING // STYLING
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------

View File

@ -534,6 +534,11 @@ void PushPlotClipRect();
// Pop plot clip rect. // Pop plot clip rect.
void PopPlotClipRect(); void PopPlotClipRect();
// Begin a drag and drop source from a legend entry. The only supported flag is SourceNoPreviewTooltip
bool BeginLegendDragDropSource(const char* label_id, ImGuiDragDropFlags flags = 0);
// End drag and drop source.
void EndDragDropSource();
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Demo (add implot_demo.cpp to your sources!) // Demo (add implot_demo.cpp to your sources!)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------

View File

@ -715,6 +715,7 @@ void ShowDemoWindow(bool* p_open) {
init = false; init = false;
} }
ImGui::BulletText("Drag data items from the left column onto the plot or onto a specific y-axis."); ImGui::BulletText("Drag data items from the left column onto the plot or onto a specific y-axis.");
ImGui::BulletText("Drag data items from the legend onto a specific y-axis.");
ImGui::BeginGroup(); ImGui::BeginGroup();
if (ImGui::Button("Clear", ImVec2(100, 0))) { if (ImGui::Button("Clear", ImVec2(100, 0))) {
for (int i = 0; i < K_CHANNELS; ++i) { for (int i = 0; i < K_CHANNELS; ++i) {
@ -754,6 +755,11 @@ void ShowDemoWindow(bool* p_open) {
sprintf(label, "data_%d", i); sprintf(label, "data_%d", i);
ImPlot::SetPlotYAxis(yAxis[i]); ImPlot::SetPlotYAxis(yAxis[i]);
ImPlot::PlotLine(label, &data[i].Data[0].x, &data[i].Data[0].y, data[i].Data.size(), data[i].Offset, 2 * sizeof(t_float)); ImPlot::PlotLine(label, &data[i].Data[0].x, &data[i].Data[0].y, data[i].Data.size(), data[i].Offset, 2 * sizeof(t_float));
if (ImPlot::BeginLegendDragDropSource(label)) {
ImGui::SetDragDropPayload("DND_PLOT", &i, sizeof(int));
ImGui::TextUnformatted(label);
ImPlot::EndDragDropSource();
}
} }
} }
if (ImGui::BeginDragDropTarget()) { if (ImGui::BeginDragDropTarget()) {
@ -1327,4 +1333,4 @@ void ShowBenchmarkTool() {
} }
} }
} }