Skip to content

Commit fa963b9

Browse files
committed
Inputs: Internal: added SetItemUsingMouseWheel() and mechanism to request mouse wheel for both HoveredId and ActiveId. (ocornut#2891)
Based on @kudaba PR.
1 parent eb88fee commit fa963b9

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

imgui.cpp

+20-2
Original file line numberDiff line numberDiff line change
@@ -3026,6 +3026,7 @@ void ImGui::SetActiveID(ImGuiID id, ImGuiWindow* window)
30263026

30273027
// Clear declaration of inputs claimed by the widget
30283028
// (Please note that this is WIP and not all keys/inputs are thoroughly declared by all widgets yet)
3029+
g.ActiveIdUsingMouseWheel = false;
30293030
g.ActiveIdUsingNavDirMask = 0x00;
30303031
g.ActiveIdUsingNavInputMask = 0x00;
30313032
g.ActiveIdUsingKeyInputMask = 0x00;
@@ -3041,6 +3042,7 @@ void ImGui::SetHoveredID(ImGuiID id)
30413042
ImGuiContext& g = *GImGui;
30423043
g.HoveredId = id;
30433044
g.HoveredIdAllowOverlap = false;
3045+
g.HoveredIdUsingMouseWheel = false;
30443046
if (id != 0 && g.HoveredIdPreviousFrame != id)
30453047
g.HoveredIdTimer = g.HoveredIdNotActiveTimer = 0.0f;
30463048
}
@@ -3606,6 +3608,9 @@ void ImGui::UpdateMouseWheel()
36063608
if (g.IO.MouseWheel == 0.0f && g.IO.MouseWheelH == 0.0f)
36073609
return;
36083610

3611+
if ((g.ActiveId != 0 && g.ActiveIdUsingMouseWheel) || (g.HoveredIdPreviousFrame != 0 && g.HoveredIdPreviousFrameUsingMouseWheel))
3612+
return;
3613+
36093614
ImGuiWindow* window = g.WheelingWindow ? g.WheelingWindow : g.HoveredWindow;
36103615
if (!window || window->Collapsed)
36113616
return;
@@ -3839,8 +3844,10 @@ void ImGui::NewFrame()
38393844
if (g.HoveredId && g.ActiveId != g.HoveredId)
38403845
g.HoveredIdNotActiveTimer += g.IO.DeltaTime;
38413846
g.HoveredIdPreviousFrame = g.HoveredId;
3847+
g.HoveredIdPreviousFrameUsingMouseWheel = g.HoveredIdUsingMouseWheel;
38423848
g.HoveredId = 0;
38433849
g.HoveredIdAllowOverlap = false;
3850+
g.HoveredIdUsingMouseWheel = false;
38443851
g.HoveredIdDisabled = false;
38453852

38463853
// Update ActiveId data (clear reference to active widget if the widget isn't alive anymore)
@@ -4768,12 +4775,23 @@ bool ImGui::IsItemEdited()
47684775
void ImGui::SetItemAllowOverlap()
47694776
{
47704777
ImGuiContext& g = *GImGui;
4771-
if (g.HoveredId == g.CurrentWindow->DC.LastItemId)
4778+
ImGuiID id = g.CurrentWindow->DC.LastItemId;
4779+
if (g.HoveredId == id)
47724780
g.HoveredIdAllowOverlap = true;
4773-
if (g.ActiveId == g.CurrentWindow->DC.LastItemId)
4781+
if (g.ActiveId == id)
47744782
g.ActiveIdAllowOverlap = true;
47754783
}
47764784

4785+
void ImGui::SetItemUsingMouseWheel()
4786+
{
4787+
ImGuiContext& g = *GImGui;
4788+
ImGuiID id = g.CurrentWindow->DC.LastItemId;
4789+
if (g.HoveredId == id)
4790+
g.HoveredIdUsingMouseWheel = true;
4791+
if (g.ActiveId == id)
4792+
g.ActiveIdUsingMouseWheel = true;
4793+
}
4794+
47774795
ImVec2 ImGui::GetItemRectMin()
47784796
{
47794797
ImGuiWindow* window = GetCurrentWindowRead();

imgui.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ Index of this file:
5959
// Version
6060
// (Integer encoded as XYYZZ for use in #if preprocessor conditionals. Work in progress versions typically starts at XYY99 then bounce up to XYY00, XYY01 etc. when release tagging happens)
6161
#define IMGUI_VERSION "1.80 WIP"
62-
#define IMGUI_VERSION_NUM 17908
62+
#define IMGUI_VERSION_NUM 17909
6363
#define IMGUI_CHECKVERSION() ImGui::DebugCheckVersionAndDataLayout(IMGUI_VERSION, sizeof(ImGuiIO), sizeof(ImGuiStyle), sizeof(ImVec2), sizeof(ImVec4), sizeof(ImDrawVert), sizeof(ImDrawIdx))
6464
#define IMGUI_HAS_TABLE
6565

imgui_internal.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -1275,9 +1275,11 @@ struct ImGuiContext
12751275
float WheelingWindowTimer;
12761276

12771277
// Item/widgets state and tracking information
1278-
ImGuiID HoveredId; // Hovered widget
1278+
ImGuiID HoveredId; // Hovered widget, filled during the frame
12791279
ImGuiID HoveredIdPreviousFrame;
12801280
bool HoveredIdAllowOverlap;
1281+
bool HoveredIdUsingMouseWheel; // Hovered widget will use mouse wheel. Blocks scrolling the underlying window.
1282+
bool HoveredIdPreviousFrameUsingMouseWheel;
12811283
bool HoveredIdDisabled; // At least one widget passed the rect test, but has been discarded by disabled flag or popup inhibit. May be true even if HoveredId == 0.
12821284
float HoveredIdTimer; // Measure contiguous hovering time
12831285
float HoveredIdNotActiveTimer; // Measure contiguous hovering time where the item has not been active
@@ -1290,6 +1292,7 @@ struct ImGuiContext
12901292
bool ActiveIdHasBeenPressedBefore; // Track whether the active id led to a press (this is to allow changing between PressOnClick and PressOnRelease without pressing twice). Used by range_select branch.
12911293
bool ActiveIdHasBeenEditedBefore; // Was the value associated to the widget Edited over the course of the Active state.
12921294
bool ActiveIdHasBeenEditedThisFrame;
1295+
bool ActiveIdUsingMouseWheel; // Active widget will want to read mouse wheel. Blocks scrolling the underlying window.
12931296
ImU32 ActiveIdUsingNavDirMask; // Active widget will want to read those nav move requests (e.g. can activate a button and move away from it)
12941297
ImU32 ActiveIdUsingNavInputMask; // Active widget will want to read those nav inputs.
12951298
ImU64 ActiveIdUsingKeyInputMask; // Active widget will want to read those key inputs. When we grow the ImGuiKey enum we'll need to either to order the enum to make useful keys come first, either redesign this into e.g. a small array.
@@ -1501,6 +1504,7 @@ struct ImGuiContext
15011504

15021505
HoveredId = HoveredIdPreviousFrame = 0;
15031506
HoveredIdAllowOverlap = false;
1507+
HoveredIdUsingMouseWheel = HoveredIdPreviousFrameUsingMouseWheel = false;
15041508
HoveredIdDisabled = false;
15051509
HoveredIdTimer = HoveredIdNotActiveTimer = 0.0f;
15061510
ActiveId = 0;
@@ -1512,6 +1516,7 @@ struct ImGuiContext
15121516
ActiveIdHasBeenPressedBefore = false;
15131517
ActiveIdHasBeenEditedBefore = false;
15141518
ActiveIdHasBeenEditedThisFrame = false;
1519+
ActiveIdUsingMouseWheel = false;
15151520
ActiveIdUsingNavDirMask = 0x00;
15161521
ActiveIdUsingNavInputMask = 0x00;
15171522
ActiveIdUsingKeyInputMask = 0x00;
@@ -2267,6 +2272,7 @@ namespace ImGui
22672272

22682273
// Inputs
22692274
// FIXME: Eventually we should aim to move e.g. IsActiveIdUsingKey() into IsKeyXXX functions.
2275+
IMGUI_API void SetItemUsingMouseWheel();
22702276
inline bool IsActiveIdUsingNavDir(ImGuiDir dir) { ImGuiContext& g = *GImGui; return (g.ActiveIdUsingNavDirMask & (1 << dir)) != 0; }
22712277
inline bool IsActiveIdUsingNavInput(ImGuiNavInput input) { ImGuiContext& g = *GImGui; return (g.ActiveIdUsingNavInputMask & (1 << input)) != 0; }
22722278
inline bool IsActiveIdUsingKey(ImGuiKey key) { ImGuiContext& g = *GImGui; IM_ASSERT(key < 64); return (g.ActiveIdUsingKeyInputMask & ((ImU64)1 << key)) != 0; }

0 commit comments

Comments
 (0)