Demo: Improved popups demo and comments.

This commit is contained in:
ocornut 2021-04-15 15:53:50 +02:00
parent 7c9c5dbe9a
commit 5fed6bdc72
4 changed files with 89 additions and 39 deletions

View File

@ -54,6 +54,7 @@ Other Changes:
- LabelText: Fixed clipping of multi-line value text when label is single-line. (#4004) - LabelText: Fixed clipping of multi-line value text when label is single-line. (#4004)
- LabelText: Fixed vertical alignment of single-line value text when label is multi-line. (#4004) - LabelText: Fixed vertical alignment of single-line value text when label is multi-line. (#4004)
- Popups: Added 'OpenPopup(ImGuiID id)' overload to facilitate calling from nested stacks. (#3993, #331) [@zlash] - Popups: Added 'OpenPopup(ImGuiID id)' overload to facilitate calling from nested stacks. (#3993, #331) [@zlash]
- Demo: Improved popups demo and comments.
- Backends: SDL: Rework global mouse pos availability check listing supported platforms explicitly, - Backends: SDL: Rework global mouse pos availability check listing supported platforms explicitly,
effectively fixing mouse access on Raspberry Pi. (#2837, #3950) [@lethal-guitar, @hinxx] effectively fixing mouse access on Raspberry Pi. (#2837, #3950) [@lethal-guitar, @hinxx]
- Backends: Win32: Clearing keyboard down array when losing focus (WM_KILLFOCUS). (#2062, #3532, #3961) - Backends: Win32: Clearing keyboard down array when losing focus (WM_KILLFOCUS). (#2062, #3532, #3961)

View File

@ -8363,10 +8363,21 @@ void ImGui::OpenPopupOnItemClick(const char* str_id, ImGuiPopupFlags popup_flags
} }
// This is a helper to handle the simplest case of associating one named popup to one given widget. // This is a helper to handle the simplest case of associating one named popup to one given widget.
// - You can pass a NULL str_id to use the identifier of the last item. // - To create a popup associated to the last item, you generally want to pass a NULL value to str_id.
// - You may want to handle this on user side if you have specific needs (e.g. tweaking IsItemHovered() parameters). // - To create a popup with a specific identifier, pass it in str_id.
// - This is essentially the same as calling OpenPopupOnItemClick() + BeginPopup() but written to avoid // - This is useful when using using BeginPopupContextItem() on an item which doesn't have an identifier, e.g. a Text() call.
// computing the ID twice because BeginPopupContextXXX functions may be called very frequently. // - This is useful when multiple code locations may want to manipulate/open the same popup, given an explicit id.
// - You may want to handle the whole on user side if you have specific needs (e.g. tweaking IsItemHovered() parameters).
// This is essentially the same as:
// id = str_id ? GetID(str_id) : GetItemID();
// OpenPopupOnItemClick(str_id);
// return BeginPopup(id);
// Which is essentially the same as:
// id = str_id ? GetID(str_id) : GetItemID();
// if (IsItemHovered() && IsMouseReleased(ImGuiMouseButton_Right))
// OpenPopup(id);
// return BeginPopup(id);
// The main difference being that this is tweaked to avoid computing the ID twice.
bool ImGui::BeginPopupContextItem(const char* str_id, ImGuiPopupFlags popup_flags) bool ImGui::BeginPopupContextItem(const char* str_id, ImGuiPopupFlags popup_flags)
{ {
ImGuiWindow* window = GImGui->CurrentWindow; ImGuiWindow* window = GImGui->CurrentWindow;

View File

@ -661,7 +661,7 @@ namespace ImGui
// - They are convenient to easily create context menus, hence the name. // - They are convenient to easily create context menus, hence the name.
// - IMPORTANT: Notice that BeginPopupContextXXX takes ImGuiPopupFlags just like OpenPopup() and unlike BeginPopup(). For full consistency, we may add ImGuiWindowFlags to the BeginPopupContextXXX functions in the future. // - IMPORTANT: Notice that BeginPopupContextXXX takes ImGuiPopupFlags just like OpenPopup() and unlike BeginPopup(). For full consistency, we may add ImGuiWindowFlags to the BeginPopupContextXXX functions in the future.
// - IMPORTANT: we exceptionally default their flags to 1 (== ImGuiPopupFlags_MouseButtonRight) for backward compatibility with older API taking 'int mouse_button = 1' parameter, so if you add other flags remember to re-add the ImGuiPopupFlags_MouseButtonRight. // - IMPORTANT: we exceptionally default their flags to 1 (== ImGuiPopupFlags_MouseButtonRight) for backward compatibility with older API taking 'int mouse_button = 1' parameter, so if you add other flags remember to re-add the ImGuiPopupFlags_MouseButtonRight.
IMGUI_API bool BeginPopupContextItem(const char* str_id = NULL, ImGuiPopupFlags popup_flags = 1); // open+begin popup when clicked on last item. if you can pass a NULL str_id only if the previous item had an id. If you want to use that on a non-interactive item such as Text() you need to pass in an explicit ID here. read comments in .cpp! IMGUI_API bool BeginPopupContextItem(const char* str_id = NULL, ImGuiPopupFlags popup_flags = 1); // open+begin popup when clicked on last item. Use str_id==NULL to associate the popup to previous item. If you want to use that on a non-interactive item such as Text() you need to pass in an explicit ID here. read comments in .cpp!
IMGUI_API bool BeginPopupContextWindow(const char* str_id = NULL, ImGuiPopupFlags popup_flags = 1);// open+begin popup when clicked on current window. IMGUI_API bool BeginPopupContextWindow(const char* str_id = NULL, ImGuiPopupFlags popup_flags = 1);// open+begin popup when clicked on current window.
IMGUI_API bool BeginPopupContextVoid(const char* str_id = NULL, ImGuiPopupFlags popup_flags = 1); // open+begin popup when clicked in void (where there are no windows). IMGUI_API bool BeginPopupContextVoid(const char* str_id = NULL, ImGuiPopupFlags popup_flags = 1); // open+begin popup when clicked in void (where there are no windows).
// Popups: query functions // Popups: query functions

View File

@ -3192,46 +3192,84 @@ static void ShowDemoWindowPopups()
if (ImGui::TreeNode("Context menus")) 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: // BeginPopupContextItem() is a helper to provide common/simple popup behavior of essentially doing:
// if (IsItemHovered() && IsMouseReleased(ImGuiMouseButton_Right)) // if (id == 0)
// OpenPopup(id); // id = GetItemID(); // Use last item id
// return BeginPopup(id); // if (IsItemHovered() && IsMouseReleased(ImGuiMouseButton_Right))
// For more advanced uses you may want to replicate and customize this code. // OpenPopup(id);
// See details in BeginPopupContextItem(). // return BeginPopup(id);
static float value = 0.5f; // For advanced advanced uses you may want to replicate and customize this code.
ImGui::Text("Value = %.3f (<-- right-click here)", value); // See more details in BeginPopupContextItem().
if (ImGui::BeginPopupContextItem("item context menu"))
// 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.
{ {
if (ImGui::Selectable("Set to zero")) value = 0.0f; const char* names[5] = { "Label1", "Label2", "Label3", "Label4", "Label5" };
if (ImGui::Selectable("Set to PI")) value = 3.1415f; for (int n = 0; n < 5; n++)
ImGui::SetNextItemWidth(-FLT_MIN); {
ImGui::DragFloat("##Value", &value, 0.1f, 0.0f, 0.0f); ImGui::Selectable(names[n]);
ImGui::EndPopup(); 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 // Example 2
// Begin() call. So here we will make it that clicking on the text field with the right mouse button (1) // Popup on a Text() element which doesn't have an identifier: we need to provide an identifier to BeginPopupContextItem().
// will toggle the visibility of the popup above. // Using an explicit identifier is also convenient if you want to activate the popups from different locations.
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())
{ {
ImGui::Text("Edit name:"); HelpMarker("Text() elements don't have stable identifiers so we need to provide one.");
ImGui::InputText("##edit", name, IM_ARRAYSIZE(name)); static float value = 0.5f;
if (ImGui::Button("Close")) ImGui::Text("Value = %.3f <-- (1) right-click this value", value);
ImGui::CloseCurrentPopup(); if (ImGui::BeginPopupContextItem("my popup"))
ImGui::EndPopup(); {
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(); ImGui::TreePop();
} }