1
0
mirror of https://github.com/ocornut/imgui.git synced 2024-11-12 02:00:58 +01:00

Added IsItemHoveredOrFocused() (provisional name), better handling of popup/tooltip positioning when using mouse+nav (#323)

This commit is contained in:
ocornut 2016-08-21 13:25:40 +02:00
parent ea2425ad9a
commit 87eb749cbc
2 changed files with 13 additions and 6 deletions

View File

@ -3961,6 +3961,12 @@ bool ImGui::IsItemFocused()
return g.NavId && !g.NavDisableHighlight && g.NavId == g.CurrentWindow->DC.LastItemId;
}
bool ImGui::IsItemHoveredOrFocused()
{
ImGuiContext& g = *GImGui;
return g.NavDisableMouseHover ? IsItemFocused() : IsItemHovered();
}
bool ImGui::IsItemClicked(int mouse_button)
{
return IsMouseClicked(mouse_button) && IsItemHovered();
@ -4090,7 +4096,7 @@ void ImGui::OpenPopupEx(const char* str_id, bool reopen_existing)
ImGuiID id = window->GetID(str_id);
int current_stack_size = g.CurrentPopupStack.Size;
ImVec2 mouse_pos = g.IO.MousePos;
ImVec2 popup_pos = (g.ActiveIdSource == ImGuiInputSource_Nav) ? NavCalcPreferredMousePos() : mouse_pos;
ImVec2 popup_pos = (!g.NavDisableHighlight && g.NavDisableMouseHover) ? NavCalcPreferredMousePos() : mouse_pos;
ImGuiPopupRef popup_ref = ImGuiPopupRef(id, window, window->GetID("##menus"), popup_pos, mouse_pos); // Tagged as new ref because constructor sets Window to NULL (we are passing the ParentWindow info here)
if (g.OpenPopupStack.Size < current_stack_size + 1)
g.OpenPopupStack.push_back(popup_ref);
@ -4820,7 +4826,7 @@ bool ImGui::Begin(const char* name, bool* p_open, const ImVec2& size_on_first_us
// Position tooltip (always follows mouse)
if ((flags & ImGuiWindowFlags_Tooltip) != 0 && !window_pos_set_by_api)
{
ImVec2 ref_pos = (g.ActiveIdSource == ImGuiInputSource_Nav) ? NavCalcPreferredMousePos() : g.IO.MousePos;
ImVec2 ref_pos = (!g.NavDisableHighlight && g.NavDisableMouseHover) ? NavCalcPreferredMousePos() : g.IO.MousePos;
ImRect rect_to_avoid(ref_pos.x - 16, ref_pos.y - 8, ref_pos.x + 24, ref_pos.y + 24); // FIXME: Completely hard-coded. Perhaps center on cursor hit-point instead?
window->PosFloat = FindBestPopupWindowPos(ref_pos, window->Size, &window->AutoPosLastDirection, rect_to_avoid);
if (window->AutoPosLastDirection == -1)
@ -9876,7 +9882,7 @@ bool ImGui::ColorButton(const ImVec4& col, bool small_height, bool outline_borde
RenderNavHighlight(bb, id);
RenderFrame(bb.Min, bb.Max, GetColorU32(col), outline_border, style.FrameRounding);
if (g.HoveredId == id)
if (IsItemHoveredOrFocused())
SetTooltip("Color:\n(%.2f,%.2f,%.2f,%.2f)\n#%02X%02X%02X%02X", col.x, col.y, col.z, col.w, IM_F32_TO_INT8_SAT(col.x), IM_F32_TO_INT8_SAT(col.y), IM_F32_TO_INT8_SAT(col.z), IM_F32_TO_INT8_SAT(col.z));
return pressed;
@ -9994,7 +10000,7 @@ bool ImGui::ColorEdit4(const char* label, float col[4], bool alpha)
g.ColorEditModeStorage.SetInt(id, (edit_mode + 1) % 3); // Don't set local copy of 'edit_mode' right away!
// Recreate our own tooltip over's ColorButton() one because we want to display correct alpha here
if (IsItemHovered())
if (IsItemHoveredOrFocused())
SetTooltip("Color:\n(%.2f,%.2f,%.2f,%.2f)\n#%02X%02X%02X%02X", col[0], col[1], col[2], col[3], IM_F32_TO_INT8_SAT(col[0]), IM_F32_TO_INT8_SAT(col[1]), IM_F32_TO_INT8_SAT(col[2]), IM_F32_TO_INT8_SAT(col[3]));
if (window->DC.ColorEditMode == ImGuiColorEditMode_UserSelectShowButton)
@ -10653,7 +10659,7 @@ void ImGui::ShowMetricsWindow(bool* p_open)
}
ImDrawIdx* idx_buffer = (draw_list->IdxBuffer.Size > 0) ? draw_list->IdxBuffer.Data : NULL;
bool pcmd_node_open = ImGui::TreeNode((void*)(pcmd - draw_list->CmdBuffer.begin()), "Draw %-4d %s vtx, tex = %p, clip_rect = (%.0f,%.0f)..(%.0f,%.0f)", pcmd->ElemCount, draw_list->IdxBuffer.Size > 0 ? "indexed" : "non-indexed", pcmd->TextureId, pcmd->ClipRect.x, pcmd->ClipRect.y, pcmd->ClipRect.z, pcmd->ClipRect.w);
if (show_clip_rects && (ImGui::IsItemHovered() || ImGui::IsItemFocused()))
if (show_clip_rects && ImGui::IsItemHoveredOrFocused())
{
ImRect clip_rect = pcmd->ClipRect;
ImRect vtxs_rect;
@ -10677,7 +10683,7 @@ void ImGui::ShowMetricsWindow(bool* p_open)
buf_p += sprintf(buf_p, "%s %04d { pos = (%8.2f,%8.2f), uv = (%.6f,%.6f), col = %08X }\n", (n == 0) ? "vtx" : " ", vtx_i, v.pos.x, v.pos.y, v.uv.x, v.uv.y, v.col);
}
ImGui::Selectable(buf, false);
if (ImGui::IsItemHovered() || ImGui::IsItemFocused())
if (ImGui::IsItemHoveredOrFocused())
overlay_draw_list->AddPolyline(triangles_pos, 3, IM_COL32(255,255,0,255), true, 1.0f, false); // Add triangle without AA, more readable for large-thin triangle
}
ImGui::TreePop();

View File

@ -399,6 +399,7 @@ namespace ImGui
IMGUI_API bool IsItemHoveredRect(); // is 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(); // is 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 IsItemFocused(); // is the last item focused for keyboard/gamepad navigation?
IMGUI_API bool IsItemHoveredOrFocused(); // select best suitable between IsItemHovered() when mouse is used and IsItemFocused() when navigation is used. useful for tooltips that needs to work with both controls.
IMGUI_API bool IsItemClicked(int mouse_button = 0); // is the last item clicked? (e.g. button/node just clicked on)
IMGUI_API bool IsItemVisible(); // is the last item visible? (aka not out of sight due to clipping/scrolling.)
IMGUI_API bool IsAnyItemHovered();