Demo: Added simple item reordering demo in Widgets -> Drag and Drop section. (#2823, #143) [@rokups]

This commit is contained in:
omar 2019-10-07 17:52:31 +02:00
parent 8aad3482a4
commit 3b271b1847
2 changed files with 35 additions and 13 deletions

View File

@ -40,6 +40,7 @@ Other Changes:
- InputText, Nav: Fixed Home/End key broken when activating Keyboard Navigation. (#787)
- TreeNode: Fixed combination of ImGuiTreeNodeFlags_SpanFullWidth and ImGuiTreeNodeFlags_OpenOnArrow
incorrectly locating the arrow hit position to the left of the frame. (#2451, #2438, #1897)
- Demo: Added simple item reordering demo in Widgets -> Drag and Drop section. (#2823, #143) [@rokups]
-----------------------------------------------------------------------

View File

@ -1506,24 +1506,21 @@ static void ShowDemoWindowWidgets()
if (ImGui::TreeNode("Drag and Drop"))
{
if (ImGui::TreeNode("Drag and drop in standard widgets"))
{
// ColorEdit widgets automatically act as drag source and drag target.
// They are using standardized payload strings IMGUI_PAYLOAD_TYPE_COLOR_3F and IMGUI_PAYLOAD_TYPE_COLOR_4F to allow your own widgets
// to use colors in their drag and drop interaction. Also see the demo in Color Picker -> Palette demo.
ImGui::BulletText("Drag and drop in standard widgets");
ImGui::SameLine();
HelpMarker("You can drag from the colored squares.");
ImGui::Indent();
static float col1[3] = { 1.0f,0.0f,0.2f };
static float col2[4] = { 0.4f,0.7f,0.0f,0.5f };
static float col1[3] = { 1.0f, 0.0f, 0.2f };
static float col2[4] = { 0.4f, 0.7f, 0.0f, 0.5f };
ImGui::ColorEdit3("color 1", col1);
ImGui::ColorEdit4("color 2", col2);
ImGui::Unindent();
ImGui::TreePop();
}
if (ImGui::TreeNode("Drag and drop to copy/swap items"))
{
ImGui::BulletText("Drag and drop to copy/swap items");
ImGui::Indent();
enum Mode
{
Mode_Copy,
@ -1577,7 +1574,31 @@ static void ShowDemoWindowWidgets()
}
ImGui::PopID();
}
ImGui::Unindent();
ImGui::TreePop();
}
if (ImGui::TreeNode("Drag to reorder items (simple)"))
{
// Simple reordering
HelpMarker("We don't use the drag and drop api at all here! Instead we query when the item is held but not hovered, and order items accordingly.");
static const char* item_names[] = { "Item One", "Item Two", "Item Three", "Item Four", "Item Five" };
for (int n = 0; n < IM_ARRAYSIZE(item_names); n++)
{
const char* item = item_names[n];
ImGui::Selectable(item);
if (ImGui::IsItemActive() && !ImGui::IsItemHovered())
{
int n_next = n + (ImGui::GetMouseDragDelta(0).y < 0.f ? -1 : 1);
if (n_next >= 0 && n_next < IM_ARRAYSIZE(item_names))
{
item_names[n] = item_names[n_next];
item_names[n_next] = item;
ImGui::ResetMouseDragDelta();
}
}
}
ImGui::TreePop();
}
ImGui::TreePop();