From 4723d97cfb8566c2e984d1634bf08614ee5da407 Mon Sep 17 00:00:00 2001 From: ocornut Date: Mon, 21 Dec 2015 00:04:33 +0100 Subject: [PATCH] Added BeginPopupContextItem() comment (#439) --- imgui.cpp | 13 +++++++++---- imgui.h | 8 ++++---- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/imgui.cpp b/imgui.cpp index 9e0bb5d2..4c9d6950 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -3207,7 +3207,7 @@ void ImGui::CloseCurrentPopup() ClosePopupToLevel(popup_idx); } -static void ClearSetNextWindowData() +static inline void ClearSetNextWindowData() { ImGuiState& g = *GImGui; g.SetNextWindowPosCond = g.SetNextWindowSizeCond = g.SetNextWindowContentSizeCond = g.SetNextWindowCollapsedCond = g.SetNextWindowFocus = 0; @@ -3282,11 +3282,16 @@ void ImGui::EndPopup() ImGui::PopStyleVar(); } -// FIXME: Allow to reopen existing, would need to a/ read DC.LastItemHoveredRect instead of DC.LastItemHoveredAndUsable and b/ add parameter to API (more tricky) +// This is a helper to handle the most simple case of associating one named popup to one given widget. +// 1. If you have many possible popups (for different "instances" of a same widget, or for wholly different widgets), you may be better off handling +// this yourself so you can store data relative to the widget that opened the popup instead of choosing different popup identifiers. +// 2. If you want right-clicking on the same item to reopen the popup at new location, use the same code replacing IsItemHovered() with IsItemHoveredRect(). +// Because: hovering an item in a window below the popup won't normally trigger is hovering behavior/coloring. The pattern of ignoring the fact that +// the item isn't interactable (because it is blocked by the active popup) may useful in some situation when e.g. large canvas as one item, content of menu +// driven by click position. bool ImGui::BeginPopupContextItem(const char* str_id, int mouse_button) { - ImGuiWindow* window = GetCurrentWindowRead(); - if (window->DC.LastItemHoveredAndUsable && ImGui::IsMouseClicked(mouse_button)) + if (ImGui::IsItemHovered() && ImGui::IsMouseClicked(mouse_button)) ImGui::OpenPopupEx(str_id, false); return ImGui::BeginPopup(str_id); } diff --git a/imgui.h b/imgui.h index cbe5b524..320afe04 100644 --- a/imgui.h +++ b/imgui.h @@ -352,9 +352,9 @@ namespace ImGui IMGUI_API void OpenPopup(const char* str_id); // mark popup as open. popups are closed when user click outside, or activate a pressable item, or CloseCurrentPopup() is called within a BeginPopup()/EndPopup() block. popup identifiers are relative to the current ID-stack (so OpenPopup and BeginPopup needs to be at the same level). IMGUI_API bool BeginPopup(const char* str_id); // return true if popup if opened and start outputting to it. only call EndPopup() if BeginPopup() returned true! IMGUI_API bool BeginPopupModal(const char* name, bool* p_opened = NULL, ImGuiWindowFlags extra_flags = 0); // modal dialog (can't close them by clicking outside) - IMGUI_API bool BeginPopupContextItem(const char* str_id, int mouse_button = 1); // helper to open and begin popup when clicked on last item - IMGUI_API bool BeginPopupContextWindow(bool also_over_items = true, const char* str_id = NULL, int mouse_button = 1); // helper to open and begin popup when clicked on current window - IMGUI_API bool BeginPopupContextVoid(const char* str_id = NULL, int mouse_button = 1); // helper to open and begin popup when clicked in void (no window) + IMGUI_API bool BeginPopupContextItem(const char* str_id, int mouse_button = 1); // helper to open and begin popup when clicked on last item. read comments in .cpp! + IMGUI_API bool BeginPopupContextWindow(bool also_over_items = true, const char* str_id = NULL, int mouse_button = 1); // helper to open and begin popup when clicked on current window. + IMGUI_API bool BeginPopupContextVoid(const char* str_id = NULL, int mouse_button = 1); // helper to open and begin popup when clicked in void (no window). IMGUI_API void EndPopup(); IMGUI_API void CloseCurrentPopup(); // close the popup we have begin-ed into. clicking on a MenuItem or Selectable automatically close the current popup. @@ -368,7 +368,7 @@ namespace ImGui // Utilities IMGUI_API bool IsItemHovered(); // was the last item hovered by mouse? - IMGUI_API bool IsItemHoveredRect(); // was the last item hovered by mouse? even if another item is active while we are hovering this + IMGUI_API bool IsItemHoveredRect(); // was the last item hovered by mouse? even if another item is active or window is blocked by popup while we are hovering this IMGUI_API bool IsItemActive(); // was the last item active? (e.g. button being held, text field being edited- items that don't interact will always return false) IMGUI_API bool IsItemVisible(); // was the last item visible? (aka not out of sight due to clipping/scrolling.) IMGUI_API bool IsAnyItemHovered();