MCPcopy Create free account
hub / github.com/Aegel5/SimpleSwitcher / ButtonBehavior

Method ButtonBehavior

imgui_tiny_app/imgui/imgui_widgets.cpp:541–780  ·  view source on GitHub ↗

- FIXME: For refactor we could output flags, incl mouse hovered vs nav keyboard vs nav triggered etc. And better standardize how widgets use 'GetColor32((held && hovered) ? ... : hovered ? ...)' vs 'GetColor32(held ? ... : hovered ? ...);' For mouse feedback we typically prefer the 'held && hovered' test, but for nav feedback not always. Outputting hovered=true on Activation may be misleading. - S

Source from the content-addressed store, hash-verified

539// (1) switching to use a single ButtonBehavior() with multiple _MouseButton flags.
540// or (2) surrounding those calls with PushItemFlag(ImGuiItemFlags_AllowDuplicateId, true); ... PopItemFlag()
541bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool* out_held, ImGuiButtonFlags flags)
542{
543 ImGuiContext& g = *GImGui;
544 ImGuiWindow* window = GetCurrentWindow();
545
546 // Default behavior inherited from item flags
547 // Note that _both_ ButtonFlags and ItemFlags are valid sources, so copy one into the item_flags and only check that.
548 ImGuiItemFlags item_flags = (g.LastItemData.ID == id ? g.LastItemData.ItemFlags : g.CurrentItemFlags);
549 if (flags & ImGuiButtonFlags_AllowOverlap)
550 item_flags |= ImGuiItemFlags_AllowOverlap;
551 if (item_flags & ImGuiItemFlags_NoFocus)
552 flags |= ImGuiButtonFlags_NoFocus | ImGuiButtonFlags_NoNavFocus;
553
554 // Default only reacts to left mouse button
555 if ((flags & ImGuiButtonFlags_MouseButtonMask_) == 0)
556 flags |= ImGuiButtonFlags_MouseButtonLeft;
557
558 // Default behavior requires click + release inside bounding box
559 if ((flags & ImGuiButtonFlags_PressedOnMask_) == 0)
560 flags |= (item_flags & ImGuiItemFlags_ButtonRepeat) ? ImGuiButtonFlags_PressedOnClick : ImGuiButtonFlags_PressedOnDefault_;
561
562 ImGuiWindow* backup_hovered_window = g.HoveredWindow;
563 const bool flatten_hovered_children = (flags & ImGuiButtonFlags_FlattenChildren) && g.HoveredWindow && g.HoveredWindow->RootWindowDockTree == window->RootWindowDockTree;
564 if (flatten_hovered_children)
565 g.HoveredWindow = window;
566
567#ifdef IMGUI_ENABLE_TEST_ENGINE
568 // Alternate registration spot, for when caller didn't use ItemAdd()
569 if (g.LastItemData.ID != id)
570 IMGUI_TEST_ENGINE_ITEM_ADD(id, bb, NULL);
571#endif
572
573 bool pressed = false;
574 bool hovered = ItemHoverable(bb, id, item_flags);
575
576 // Special mode for Drag and Drop used by openables (tree nodes, tabs etc.)
577 // where holding the button pressed for a long time while drag a payload item triggers the button.
578 if (g.DragDropActive)
579 {
580 if ((flags & ImGuiButtonFlags_PressedOnDragDropHold) && !(g.DragDropSourceFlags & ImGuiDragDropFlags_SourceNoHoldToOpenOthers) && IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByActiveItem))
581 {
582 hovered = true;
583 SetHoveredID(id);
584 if (g.HoveredIdTimer - g.IO.DeltaTime <= DRAGDROP_HOLD_TO_OPEN_TIMER && g.HoveredIdTimer >= DRAGDROP_HOLD_TO_OPEN_TIMER)
585 {
586 pressed = true;
587 g.DragDropHoldJustPressedId = id;
588 FocusWindow(window);
589 }
590 }
591 if (g.DragDropAcceptIdPrev == id && (g.DragDropAcceptFlagsPrev & ImGuiDragDropFlags_AcceptDrawAsHovered))
592 hovered = true;
593 }
594
595 if (flatten_hovered_children)
596 g.HoveredWindow = backup_hovered_window;
597
598 // Mouse handling

Callers

nothing calls this directly

Calls 4

GetCurrentWindowFunction · 0.85
MouseButtonToKeyFunction · 0.85
GetKeyDataFunction · 0.85
ImMaxFunction · 0.85

Tested by

no test coverage detected