Polyphase Game Engine
Loading...
Searching...
No Matches
ScrollContainer.h
Go to the documentation of this file.
1#pragma once
2
4#include "AssetRef.h"
5
6class Quad;
7class Button;
8class Texture;
9
10enum class ScrollSizeMode : uint8_t
11{
12 FitWidth, // Content fills parent width, scrolls vertically
13 FitHeight, // Content fills parent height, scrolls horizontally
14 FitBoth, // Content fills both dimensions
15 None, // Content uses explicit size
16 Count
17};
18
19enum class ScrollbarMode : uint8_t
20{
21 Hidden, // Never show scrollbars
22 Auto, // Show only when needed
23 AlwaysVisible, // Always show
24 Count
25};
26
28{
29public:
30
32
33 virtual void Create() override;
34 virtual void GatherProperties(std::vector<Property>& props) override;
35 virtual void Tick(float deltaTime) override;
36 virtual void PreRender() override;
37
38 // Scroll Position
39 void SetScrollOffset(glm::vec2 offset);
40 glm::vec2 GetScrollOffset() const;
41 void SetScrollOffsetX(float x);
42 void SetScrollOffsetY(float y);
43 void ScrollToTop();
44 void ScrollToBottom();
45 void ScrollToLeft();
46 void ScrollToRight();
47
48 // Size Mode
49 void SetScrollSizeMode(ScrollSizeMode mode);
50 ScrollSizeMode GetScrollSizeMode() const;
51
52 // Scrollbar Configuration
53 void SetHorizontalScrollbarMode(ScrollbarMode mode);
54 ScrollbarMode GetHorizontalScrollbarMode() const;
55 void SetVerticalScrollbarMode(ScrollbarMode mode);
56 ScrollbarMode GetVerticalScrollbarMode() const;
57 void SetScrollbarWidth(float width);
58 float GetScrollbarWidth() const;
59
60 // Scroll Behavior
61 void SetScrollSpeed(float speed);
62 float GetScrollSpeed() const;
63 void SetMomentumEnabled(bool enabled);
64 bool IsMomentumEnabled() const;
65 void SetMomentumFriction(float friction);
66 float GetMomentumFriction() const;
67
68 // Query
69 bool CanScrollHorizontally() const;
70 bool CanScrollVertically() const;
71 glm::vec2 GetContentSize() const;
72 glm::vec2 GetMaxScrollOffset() const;
73 bool IsDragging() const;
74 bool IsScrolling() const;
75
76 // Child Input Priority
77 void SetChildInputPriority(bool priority);
78 bool GetChildInputPriority() const;
79
80 // Content Access
81 Widget* GetContentWidget();
82
83 // Explicit content override. Use this when the ScrollContainer's actual
84 // content child is marked transient (e.g. ListViewWidget's internal
85 // ArrayWidget) and would otherwise be skipped by the
86 // "first non-transient child" fallback. Setting nullptr restores the
87 // fallback behaviour. Caller owns the widget's lifecycle.
88 void SetContentWidget(Widget* widget);
89
90 // Visual Customization - Colors
91 void SetScrollbarColor(glm::vec4 color);
92 glm::vec4 GetScrollbarColor() const;
93 void SetScrollbarHoveredColor(glm::vec4 color);
94 glm::vec4 GetScrollbarHoveredColor() const;
95 void SetScrollbarTrackColor(glm::vec4 color);
96 glm::vec4 GetScrollbarTrackColor() const;
97
98 // Visual Customization - Textures
99 void SetScrollbarTexture(Texture* texture);
100 Texture* GetScrollbarTexture();
101 void SetTrackTexture(Texture* texture);
102 Texture* GetTrackTexture();
103
104 // Scroll Buttons
105 void SetShowScrollButtons(bool show);
106 bool GetShowScrollButtons() const;
107 void SetButtonSize(float size);
108 float GetButtonSize() const;
109 void SetUpButtonTexture(Texture* texture);
110 Texture* GetUpButtonTexture();
111 void SetDownButtonTexture(Texture* texture);
112 Texture* GetDownButtonTexture();
113 void SetLeftButtonTexture(Texture* texture);
114 Texture* GetLeftButtonTexture();
115 void SetRightButtonTexture(Texture* texture);
116 Texture* GetRightButtonTexture();
117 void SetButtonColor(glm::vec4 color);
118 glm::vec4 GetButtonColor() const;
119
120 // Access internal widgets
121 Quad* GetHScrollbar();
122 Quad* GetVScrollbar();
123 Quad* GetHTrack();
124 Quad* GetVTrack();
125 Button* GetUpButton();
126 Button* GetDownButton();
127 Button* GetLeftButton();
128 Button* GetRightButton();
129
130protected:
131
132 static bool HandlePropChange(Datum* datum, uint32_t index, const void* newValue);
133
134 void UpdateScrollbars();
135 void UpdateContentPosition();
136 void UpdateScrollButtons();
137 void ClampScrollOffset();
138 void HandleInput(float deltaTime);
139 void HandleMomentum(float deltaTime);
140 void HandleButtonInput();
141 bool ShouldShowHScrollbar() const;
142 bool ShouldShowVScrollbar() const;
143 bool IsPointerOverChildWidget(float x, float y) const;
144
145 // Scroll State
146 glm::vec2 mScrollOffset = glm::vec2(0.0f);
147 glm::vec2 mScrollVelocity = glm::vec2(0.0f);
148 glm::vec2 mDragStartOffset = glm::vec2(0.0f);
149 glm::vec2 mDragStartMouse = glm::vec2(0.0f);
150 glm::vec2 mLastMousePos = glm::vec2(0.0f);
151
152 // Size & Mode
156
157 // Scroll Behavior
158 float mScrollSpeed = 30.0f; // Pixels per scroll wheel notch
159 float mMomentumFriction = 5.0f; // Deceleration factor
160 bool mMomentumEnabled = true;
161
162 // Visual - Dimensions
163 float mScrollbarWidth = 8.0f;
164 float mButtonSize = 16.0f;
165 bool mShowScrollButtons = false;
166
167 // Visual - Colors
168 glm::vec4 mScrollbarColor = glm::vec4(0.5f, 0.5f, 0.5f, 0.8f);
169 glm::vec4 mScrollbarHoveredColor = glm::vec4(0.7f, 0.7f, 0.7f, 1.0f);
170 glm::vec4 mScrollbarTrackColor = glm::vec4(0.2f, 0.2f, 0.2f, 0.5f);
171 glm::vec4 mButtonColor = glm::vec4(0.4f, 0.4f, 0.4f, 1.0f);
172
173 // Visual - Textures
180
181 // State
182 bool mDragging = false;
183 bool mDraggingHScrollbar = false;
184 bool mDraggingVScrollbar = false;
185 bool mHScrollbarHovered = false;
186 bool mVScrollbarHovered = false;
187
188 // Input Behavior
189 bool mChildInputPriority = false; // If true, children receive input before scroll drag starts
190
191 // Cached content size (updated during PreRender)
192 glm::vec2 mCachedContentSize = glm::vec2(0.0f);
193
194 // Explicit content widget. When non-null, GetContentWidget() returns it
195 // instead of scanning children. Lets ListViewWidget point at its internal
196 // transient ArrayWidget without changing fallback behaviour for everyone
197 // else. Raw pointer matches the existing internal-widget convention; the
198 // ListViewWidget owns the lifecycle and clears it on teardown via the
199 // owning hierarchy.
200 Widget* mContentOverride = nullptr;
201
202 // Transient child widgets - Scrollbars
203 Quad* mHScrollbarTrack = nullptr;
204 Quad* mHScrollbarGrabber = nullptr;
205 Quad* mVScrollbarTrack = nullptr;
206 Quad* mVScrollbarGrabber = nullptr;
207
208 // Transient child widgets - Buttons
209 Button* mUpButton = nullptr;
210 Button* mDownButton = nullptr;
211 Button* mLeftButton = nullptr;
212 Button* mRightButton = nullptr;
213};
#define POLYPHASE_API
Definition PolyphaseAPI.h:31
ScrollSizeMode
Definition ScrollContainer.h:11
ScrollbarMode
Definition ScrollContainer.h:20
Definition AssetRef.h:18
Definition Button.h:21
Definition Datum.h:169
virtual void Create()
Definition Node.cpp:235
virtual void Tick(float deltaTime)
Definition Node.cpp:573
Definition Quad.h:20
Definition ScrollContainer.h:28
TextureRef mRightButtonTexture
Definition ScrollContainer.h:179
TextureRef mTrackTexture
Definition ScrollContainer.h:175
TextureRef mUpButtonTexture
Definition ScrollContainer.h:176
DECLARE_NODE(ScrollContainer, Widget)
TextureRef mDownButtonTexture
Definition ScrollContainer.h:177
TextureRef mLeftButtonTexture
Definition ScrollContainer.h:178
TextureRef mScrollbarTexture
Definition ScrollContainer.h:174
Definition Texture.h:10
Definition Widget.h:53
static bool HandlePropChange(Datum *datum, uint32_t index, const void *newValue)
Definition Widget.cpp:45
virtual void GatherProperties(std::vector< Property > &outProps) override
Definition Widget.cpp:115
virtual void PreRender()
Definition Widget.cpp:226