|
@@ -3192,46 +3192,84 @@ static void ShowDemoWindowPopups()
|
|
|
|
|
|
if (ImGui::TreeNode("Context menus"))
|
|
|
{
|
|
|
+ HelpMarker("\"Context\" functions are simple helpers to associate a Popup to a given Item or Window identifier.");
|
|
|
+
|
|
|
// BeginPopupContextItem() is a helper to provide common/simple popup behavior of essentially doing:
|
|
|
- // if (IsItemHovered() && IsMouseReleased(ImGuiMouseButton_Right))
|
|
|
- // OpenPopup(id);
|
|
|
- // return BeginPopup(id);
|
|
|
- // For more advanced uses you may want to replicate and customize this code.
|
|
|
- // See details in BeginPopupContextItem().
|
|
|
- static float value = 0.5f;
|
|
|
- ImGui::Text("Value = %.3f (<-- right-click here)", value);
|
|
|
- if (ImGui::BeginPopupContextItem("item context menu"))
|
|
|
- {
|
|
|
- if (ImGui::Selectable("Set to zero")) value = 0.0f;
|
|
|
- if (ImGui::Selectable("Set to PI")) value = 3.1415f;
|
|
|
- ImGui::SetNextItemWidth(-FLT_MIN);
|
|
|
- ImGui::DragFloat("##Value", &value, 0.1f, 0.0f, 0.0f);
|
|
|
- ImGui::EndPopup();
|
|
|
+ // if (id == 0)
|
|
|
+ // id = GetItemID(); // Use last item id
|
|
|
+ // if (IsItemHovered() && IsMouseReleased(ImGuiMouseButton_Right))
|
|
|
+ // OpenPopup(id);
|
|
|
+ // return BeginPopup(id);
|
|
|
+ // For advanced advanced uses you may want to replicate and customize this code.
|
|
|
+ // See more details in BeginPopupContextItem().
|
|
|
+
|
|
|
+ // Example 1
|
|
|
+ // When used after an item that has an ID (e.g. Button), we can skip providing an ID to BeginPopupContextItem(),
|
|
|
+ // and BeginPopupContextItem() will use the last item ID as the popup ID.
|
|
|
+ {
|
|
|
+ const char* names[5] = { "Label1", "Label2", "Label3", "Label4", "Label5" };
|
|
|
+ for (int n = 0; n < 5; n++)
|
|
|
+ {
|
|
|
+ ImGui::Selectable(names[n]);
|
|
|
+ if (ImGui::BeginPopupContextItem()) // <-- use last item id as popup id
|
|
|
+ {
|
|
|
+ ImGui::Text("This a popup for \"%s\"!", names[n]);
|
|
|
+ if (ImGui::Button("Close"))
|
|
|
+ ImGui::CloseCurrentPopup();
|
|
|
+ ImGui::EndPopup();
|
|
|
+ }
|
|
|
+ if (ImGui::IsItemHovered())
|
|
|
+ ImGui::SetTooltip("Right-click to open popup");
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- // We can also use OpenPopupOnItemClick() which is the same as BeginPopupContextItem() but without the
|
|
|
- // Begin() call. So here we will make it that clicking on the text field with the right mouse button (1)
|
|
|
- // will toggle the visibility of the popup above.
|
|
|
- ImGui::Text("(You can also right-click me to open the same popup as above.)");
|
|
|
- ImGui::OpenPopupOnItemClick("item context menu", 1);
|
|
|
-
|
|
|
- // When used after an item that has an ID (e.g.Button), we can skip providing an ID to BeginPopupContextItem().
|
|
|
- // BeginPopupContextItem() will use the last item ID as the popup ID.
|
|
|
- // In addition here, we want to include your editable label inside the button label.
|
|
|
- // We use the ### operator to override the ID (read FAQ about ID for details)
|
|
|
- static char name[32] = "Label1";
|
|
|
- char buf[64];
|
|
|
- sprintf(buf, "Button: %s###Button", name); // ### operator override ID ignoring the preceding label
|
|
|
- ImGui::Button(buf);
|
|
|
- if (ImGui::BeginPopupContextItem())
|
|
|
+ // Example 2
|
|
|
+ // Popup on a Text() element which doesn't have an identifier: we need to provide an identifier to BeginPopupContextItem().
|
|
|
+ // Using an explicit identifier is also convenient if you want to activate the popups from different locations.
|
|
|
{
|
|
|
- ImGui::Text("Edit name:");
|
|
|
- ImGui::InputText("##edit", name, IM_ARRAYSIZE(name));
|
|
|
- if (ImGui::Button("Close"))
|
|
|
- ImGui::CloseCurrentPopup();
|
|
|
- ImGui::EndPopup();
|
|
|
+ HelpMarker("Text() elements don't have stable identifiers so we need to provide one.");
|
|
|
+ static float value = 0.5f;
|
|
|
+ ImGui::Text("Value = %.3f <-- (1) right-click this value", value);
|
|
|
+ if (ImGui::BeginPopupContextItem("my popup"))
|
|
|
+ {
|
|
|
+ if (ImGui::Selectable("Set to zero")) value = 0.0f;
|
|
|
+ if (ImGui::Selectable("Set to PI")) value = 3.1415f;
|
|
|
+ ImGui::SetNextItemWidth(-FLT_MIN);
|
|
|
+ ImGui::DragFloat("##Value", &value, 0.1f, 0.0f, 0.0f);
|
|
|
+ ImGui::EndPopup();
|
|
|
+ }
|
|
|
+
|
|
|
+ // We can also use OpenPopupOnItemClick() to toggle the visibility of a given popup.
|
|
|
+ // Here we make it that right-clicking this other text element opens the same popup as above.
|
|
|
+ // The popup itself will be submitted by the code above.
|
|
|
+ ImGui::Text("(2) Or right-click this text");
|
|
|
+ ImGui::OpenPopupOnItemClick("my popup", ImGuiPopupFlags_MouseButtonRight);
|
|
|
+
|
|
|
+ // Back to square one: manually open the same popup.
|
|
|
+ if (ImGui::Button("(3) Or click this button"))
|
|
|
+ ImGui::OpenPopup("my popup");
|
|
|
+ }
|
|
|
+
|
|
|
+ // Example 3
|
|
|
+ // When using BeginPopupContextItem() with an implicit identifier (NULL == use last item ID),
|
|
|
+ // we need to make sure your item identifier is stable.
|
|
|
+ // In this example we showcase altering the item label while preserving its identifier, using the ### operator (see FAQ).
|
|
|
+ {
|
|
|
+ HelpMarker("Showcase using a popup ID linked to item ID, with the item having a changing label + stable ID using the ### operator.");
|
|
|
+ static char name[32] = "Label1";
|
|
|
+ char buf[64];
|
|
|
+ sprintf(buf, "Button: %s###Button", name); // ### operator override ID ignoring the preceding label
|
|
|
+ ImGui::Button(buf);
|
|
|
+ if (ImGui::BeginPopupContextItem())
|
|
|
+ {
|
|
|
+ ImGui::Text("Edit name:");
|
|
|
+ ImGui::InputText("##edit", name, IM_ARRAYSIZE(name));
|
|
|
+ if (ImGui::Button("Close"))
|
|
|
+ ImGui::CloseCurrentPopup();
|
|
|
+ ImGui::EndPopup();
|
|
|
+ }
|
|
|
+ ImGui::SameLine(); ImGui::Text("(<-- right-click here)");
|
|
|
}
|
|
|
- ImGui::SameLine(); ImGui::Text("(<-- right-click here)");
|
|
|
|
|
|
ImGui::TreePop();
|
|
|
}
|