Skip to content

Commit 3b271b1

Browse files
committed
Demo: Added simple item reordering demo in Widgets -> Drag and Drop section. (#2823, #143) [@rokups]
1 parent 8aad348 commit 3b271b1

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

docs/CHANGELOG.txt

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

4445

4546
-----------------------------------------------------------------------

imgui_demo.cpp

+34-13
Original file line numberDiff line numberDiff line change
@@ -1506,24 +1506,21 @@ static void ShowDemoWindowWidgets()
15061506

15071507
if (ImGui::TreeNode("Drag and Drop"))
15081508
{
1509+
if (ImGui::TreeNode("Drag and drop in standard widgets"))
15091510
{
15101511
// ColorEdit widgets automatically act as drag source and drag target.
15111512
// They are using standardized payload strings IMGUI_PAYLOAD_TYPE_COLOR_3F and IMGUI_PAYLOAD_TYPE_COLOR_4F to allow your own widgets
15121513
// to use colors in their drag and drop interaction. Also see the demo in Color Picker -> Palette demo.
1513-
ImGui::BulletText("Drag and drop in standard widgets");
1514-
ImGui::SameLine();
15151514
HelpMarker("You can drag from the colored squares.");
1516-
ImGui::Indent();
1517-
static float col1[3] = { 1.0f,0.0f,0.2f };
1518-
static float col2[4] = { 0.4f,0.7f,0.0f,0.5f };
1515+
static float col1[3] = { 1.0f, 0.0f, 0.2f };
1516+
static float col2[4] = { 0.4f, 0.7f, 0.0f, 0.5f };
15191517
ImGui::ColorEdit3("color 1", col1);
15201518
ImGui::ColorEdit4("color 2", col2);
1521-
ImGui::Unindent();
1519+
ImGui::TreePop();
15221520
}
15231521

1522+
if (ImGui::TreeNode("Drag and drop to copy/swap items"))
15241523
{
1525-
ImGui::BulletText("Drag and drop to copy/swap items");
1526-
ImGui::Indent();
15271524
enum Mode
15281525
{
15291526
Mode_Copy,
@@ -1577,7 +1574,31 @@ static void ShowDemoWindowWidgets()
15771574
}
15781575
ImGui::PopID();
15791576
}
1580-
ImGui::Unindent();
1577+
ImGui::TreePop();
1578+
}
1579+
1580+
if (ImGui::TreeNode("Drag to reorder items (simple)"))
1581+
{
1582+
// Simple reordering
1583+
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.");
1584+
static const char* item_names[] = { "Item One", "Item Two", "Item Three", "Item Four", "Item Five" };
1585+
for (int n = 0; n < IM_ARRAYSIZE(item_names); n++)
1586+
{
1587+
const char* item = item_names[n];
1588+
ImGui::Selectable(item);
1589+
1590+
if (ImGui::IsItemActive() && !ImGui::IsItemHovered())
1591+
{
1592+
int n_next = n + (ImGui::GetMouseDragDelta(0).y < 0.f ? -1 : 1);
1593+
if (n_next >= 0 && n_next < IM_ARRAYSIZE(item_names))
1594+
{
1595+
item_names[n] = item_names[n_next];
1596+
item_names[n_next] = item;
1597+
ImGui::ResetMouseDragDelta();
1598+
}
1599+
}
1600+
}
1601+
ImGui::TreePop();
15811602
}
15821603

15831604
ImGui::TreePop();
@@ -1607,10 +1628,10 @@ static void ShowDemoWindowWidgets()
16071628
if (item_type == 10){ ret = ImGui::TreeNodeEx("ITEM: TreeNode w/ ImGuiTreeNodeFlags_OpenOnDoubleClick", ImGuiTreeNodeFlags_OpenOnDoubleClick | ImGuiTreeNodeFlags_NoTreePushOnOpen); } // Testing tree node with ImGuiButtonFlags_PressedOnDoubleClick button policy.
16081629
if (item_type == 11){ const char* items[] = { "Apple", "Banana", "Cherry", "Kiwi" }; static int current = 1; ret = ImGui::ListBox("ITEM: ListBox", &current, items, IM_ARRAYSIZE(items), IM_ARRAYSIZE(items)); }
16091630

1610-
// Display the value of IsItemHovered() and other common item state functions.
1631+
// Display the value of IsItemHovered() and other common item state functions.
16111632
// Note that the ImGuiHoveredFlags_XXX flags can be combined.
1612-
// Because BulletText is an item itself and that would affect the output of IsItemXXX functions,
1613-
// we query every state in a single call to avoid storing them and to simplify the code
1633+
// Because BulletText is an item itself and that would affect the output of IsItemXXX functions,
1634+
// we query every state in a single call to avoid storing them and to simplify the code
16141635
ImGui::BulletText(
16151636
"Return value = %d\n"
16161637
"IsItemFocused() = %d\n"
@@ -1653,7 +1674,7 @@ static void ShowDemoWindowWidgets()
16531674
if (embed_all_inside_a_child_window)
16541675
ImGui::BeginChild("outer_child", ImVec2(0, ImGui::GetFontSize() * 20), true);
16551676

1656-
// Testing IsWindowFocused() function with its various flags.
1677+
// Testing IsWindowFocused() function with its various flags.
16571678
// Note that the ImGuiFocusedFlags_XXX flags can be combined.
16581679
ImGui::BulletText(
16591680
"IsWindowFocused() = %d\n"

0 commit comments

Comments
 (0)