File: ScrollableMenuSection.h

package info (click to toggle)
jazz2-native 3.5.0-1
  • links: PTS, VCS
  • area: contrib
  • in suites:
  • size: 16,836 kB
  • sloc: cpp: 172,557; xml: 113; python: 36; makefile: 5; sh: 2
file content (353 lines) | stat: -rw-r--r-- 10,499 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#pragma once

#include "MenuSection.h"
#include "MenuResources.h"

#include "../../../nCine/Base/FrameTimer.h"

using namespace Jazz2::UI::Menu::Resources;

namespace Jazz2::UI::Menu
{
	/** @brief Simplifies creation of unified scrollable menu sections */
	template<class TItem>
	class ScrollableMenuSection : public MenuSection
	{
	public:
		ScrollableMenuSection();

		Recti GetClipRectangle(const Recti& contentBounds) override;

		void OnShow(IMenuContainer* root) override;
		void OnUpdate(float timeMult) override;
		void OnDrawClipped(Canvas* canvas) override;
		void OnTouchEvent(const TouchEvent& event, Vector2i viewSize) override;

	protected:
		/** @brief Generic item in @ref ScrollableMenuSection */
		struct ListViewItem {
			TItem Item;
			std::int32_t Y;
			std::int32_t Height;

			ListViewItem() { }
			ListViewItem(TItem item) : Item(item) { }
		};

#ifndef DOXYGEN_GENERATING_OUTPUT
		static constexpr std::int32_t ItemHeight = 40;
		static constexpr std::int32_t TopLine = 31;
		static constexpr std::int32_t BottomLine = 42;

		SmallVector<ListViewItem> _items;
		std::int32_t _selectedIndex;
		float _animation;
		float _transitionTime;
		std::int32_t _y;
		std::int32_t _height;
		std::int32_t _availableHeight;
		Vector2f _touchStart;
		Vector2f _touchLast;
		float _touchTime;
		float _touchSpeed;
		std::int8_t _touchDirection;
		bool _scrollable;
#endif

		/** @brief Makes currently selected item visible in the viewport */
		void EnsureVisibleSelected(std::int32_t offset = 0);
		/** @brief Called when the selected item should be executed */
		virtual void OnExecuteSelected() = 0;
		/** @brief Called when an item layout should be calculated */
		virtual void OnLayoutItem(Canvas* canvas, ListViewItem& item);
		/** @brief Called when an information about empty list should be drawn */
		virtual void OnDrawEmptyText(Canvas* canvas, std::int32_t& charOffset) { }
		/** @brief Called when an item should be drawn */
		virtual void OnDrawItem(Canvas* canvas, ListViewItem& item, std::int32_t& charOffset, bool isSelected) = 0;
		/** @brief Called when input should be handled */
		virtual void OnHandleInput();
		/** @brief Called when back button is pressed */
		virtual void OnBackPressed();
		/** @brief Called when selected item is changed */
		virtual void OnSelectionChanged(ListViewItem& item) { }
		/** @brief Called when a touch event is released */
		virtual void OnTouchUp(std::int32_t newIndex, Vector2i viewSize, Vector2i touchPos);
	};

	template<class TItem>
	ScrollableMenuSection<TItem>::ScrollableMenuSection()
		: _selectedIndex(0), _animation(0.0f), _transitionTime(0.0f), _y(0), _height(0), _availableHeight(0),
			_touchTime(0.0f), _touchSpeed(0.0f), _touchDirection(0), _scrollable(false)
	{
	}

	template<class TItem>
	Recti ScrollableMenuSection<TItem>::GetClipRectangle(const Recti& contentBounds)
	{
		return Recti(contentBounds.X, contentBounds.Y + TopLine - 1, contentBounds.W, contentBounds.H - TopLine - BottomLine + 2);
	}

	template<class TItem>
	void ScrollableMenuSection<TItem>::OnShow(IMenuContainer* root)
	{
		MenuSection::OnShow(root);

		_animation = 0.0f;
	}

	template<class TItem>
	void ScrollableMenuSection<TItem>::OnUpdate(float timeMult)
	{
		if (_animation < 1.0f) {
			_animation = std::min(_animation + timeMult * 0.016f, 1.0f);
		}

		if (_touchSpeed > 0.0f) {
			if (_touchStart == Vector2f::Zero && _scrollable) {
				float y = _y + (_touchSpeed * (std::int32_t)_touchDirection * TouchKineticDivider * timeMult);
				if (y < (_availableHeight - _height) && _touchDirection < 0) {
					y = float(_availableHeight - _height);
					_touchDirection = 1;
					_touchSpeed *= TouchKineticDamping;
				} else if (y > 0.0f && _touchDirection > 0) {
					y = 0.0f;
					_touchDirection = -1;
					_touchSpeed *= TouchKineticDamping;
				}
				_y = std::int32_t(y);
			}

			_touchSpeed = std::max(_touchSpeed - TouchKineticFriction * TouchKineticDivider * timeMult, 0.0f);
		}

		OnHandleInput();

		_touchTime += timeMult;
	}

	template<class TItem>
	void ScrollableMenuSection<TItem>::OnLayoutItem(Canvas* canvas, ListViewItem& item)
	{
		item.Height = ItemHeight * 4 / 5;
	}

	template<class TItem>
	void ScrollableMenuSection<TItem>::OnHandleInput()
	{
		if (_root->ActionHit(PlayerAction::Menu)) {
			OnBackPressed();
		} else if (!_items.empty()) {
			if (_root->ActionHit(PlayerAction::Fire)) {
				OnExecuteSelected();
			} else if (_items.size() > 1) {
				if (_root->ActionHit(PlayerAction::Up)) {
					_root->PlaySfx("MenuSelect"_s, 0.5f);
					_animation = 0.0f;

					std::int32_t offset;
					if (_selectedIndex > 0) {
						_selectedIndex--;
						offset = -ItemHeight / 3;
					} else {
						_selectedIndex = (std::int32_t)(_items.size() - 1);
						offset = 0;
					}
					EnsureVisibleSelected(offset);
					OnSelectionChanged(_items[_selectedIndex]);
				} else if (_root->ActionHit(PlayerAction::Down)) {
					_root->PlaySfx("MenuSelect"_s, 0.5f);
					_animation = 0.0f;

					std::int32_t offset;
					if (_selectedIndex < _items.size() - 1) {
						_selectedIndex++;
						offset = ItemHeight / 3;
					} else {
						_selectedIndex = 0;
						offset = 0;
					}
					EnsureVisibleSelected(offset);
					OnSelectionChanged(_items[_selectedIndex]);
				}
			}
		}
	}

	template<class TItem>
	void ScrollableMenuSection<TItem>::OnBackPressed()
	{
		_root->PlaySfx("MenuSelect"_s, 0.5f);
		_root->LeaveSection();
	}

	template<class TItem>
	void ScrollableMenuSection<TItem>::OnDrawClipped(Canvas* canvas)
	{
		Vector2i viewSize = canvas->ViewSize;
		Recti contentBounds = _root->GetContentBounds();
		Recti clipRect = GetClipRectangle(contentBounds);
		std::int32_t charOffset = 0;

		if (_items.empty()) {
			_scrollable = false;
			OnDrawEmptyText(canvas, charOffset);
			return;
		}

		std::int32_t topLine = clipRect.Y + 1;
		std::int32_t bottomLine = clipRect.Y + clipRect.H - 1;
		_availableHeight = (bottomLine - topLine);

		if (_height == 0) {
			_height = ItemHeight * 2 / 3;

			for (std::int32_t i = 0; i < _items.size(); i++) {
				auto& item = _items[i];
				item.Y = _height + topLine + _y;
				OnLayoutItem(canvas, item);
				_height += item.Height;
			}
			_height -= ItemHeight / 2;

			if (_availableHeight - _height < 0) {
				_scrollable = true;
			}

			EnsureVisibleSelected();
		}

		if (_availableHeight - _height < 0) {
			_y = std::clamp(_y, _availableHeight - _height, 0);
			_scrollable = true;
		} else {
			_y = (_availableHeight - _height) / 2;
			_scrollable = false;
		}

		Vector2i center = Vector2i(viewSize.X / 2, topLine + ItemHeight / 2 + _y);

		for (std::int32_t i = 0; i < _items.size(); i++) {
			auto& item = _items[i];
			item.Y = center.Y;

			if (center.Y > topLine - ItemHeight && center.Y < bottomLine + ItemHeight) {
				OnDrawItem(canvas, item, charOffset, _selectedIndex == i);
			}

			center.Y += item.Height;
		}

		if (_items[0].Y < topLine + ItemHeight / 2) {
			_root->DrawElement(MenuGlow, 0, float(center.X), float(topLine), 900, Alignment::Center, Colorf(0.0f, 0.0f, 0.0f, 0.3f), 30.0f, 5.0f);
		}
		std::int32_t itemHeight = _items[_items.size() - 1].Height - ItemHeight * 4 / 5 + ItemHeight / 2;
		if (_items[_items.size() - 1].Y > bottomLine - itemHeight / 2) {
			_root->DrawElement(MenuGlow, 0, float(center.X), float(bottomLine), 900, Alignment::Center, Colorf(0.0f, 0.0f, 0.0f, 0.3f), 30.0f, 5.0f);
		}
	}

	template<class TItem>
	void ScrollableMenuSection<TItem>::OnTouchEvent(const TouchEvent& event, Vector2i viewSize)
	{
		switch (event.type) {
			case TouchEventType::Down: {
				std::int32_t pointerIndex = event.findPointerIndex(event.actionIndex);
				if (pointerIndex != -1) {
					std::int32_t y = std::int32_t(event.pointers[pointerIndex].y * float(viewSize.Y));
					if (y < 80) {
						OnBackPressed();
						return;
					}

					_touchStart = Vector2f(event.pointers[pointerIndex].x * viewSize.X, float(y));
					_touchLast = _touchStart;
					_touchTime = 0.0f;
				}
				break;
			}
			case TouchEventType::Move: {
				if (_touchStart != Vector2f::Zero) {
					std::int32_t pointerIndex = event.findPointerIndex(event.actionIndex);
					if (pointerIndex != -1) {
						Vector2f touchMove = Vector2f(event.pointers[pointerIndex].x * viewSize.X, event.pointers[pointerIndex].y * viewSize.Y);
						if (_scrollable) {
							float delta = touchMove.Y - _touchLast.Y;
							if (delta != 0.0f) {
								_y += std::int32_t(delta);
								if (delta < -0.1f && _touchDirection >= 0) {
									_touchDirection = -1;
									_touchSpeed = 0.0f;
								} else if (delta > 0.1f && _touchDirection <= 0) {
									_touchDirection = 1;
									_touchSpeed = 0.0f;
								}
								_touchSpeed = (0.8f * _touchSpeed) + (0.2f * std::abs(delta) / TouchKineticDivider);
							}
						}
						_touchLast = touchMove;
					}
				}
				break;
			}
			case TouchEventType::Up: {
				bool alreadyMoved = (_touchStart == Vector2f::Zero || (_touchStart - _touchLast).SqrLength() > 100 || _touchTime > FrameTimer::FramesPerSecond);
				_touchStart = Vector2f::Zero;
				if (alreadyMoved) {
					return;
				}

				for (std::int32_t i = 0; i < _items.size(); i++) {
					if (std::abs(_touchLast.Y - _items[i].Y) < 22) {
						OnTouchUp(i, viewSize, _touchLast.As<std::int32_t>());
						break;
					}
				}
				break;
			}
		}
	}

	template<class TItem>
	void ScrollableMenuSection<TItem>::OnTouchUp(std::int32_t newIndex, Vector2i viewSize, Vector2i touchPos)
	{
		std::int32_t halfW = viewSize.X / 2;
		if (std::abs(touchPos.X - halfW) < 150) {
			if (_selectedIndex == newIndex) {
				OnExecuteSelected();
			} else {
				_root->PlaySfx("MenuSelect"_s, 0.5f);
				_animation = 0.0f;
				_selectedIndex = newIndex;
				EnsureVisibleSelected();
				OnSelectionChanged(_items[_selectedIndex]);
			}
		}
	}

	template<class TItem>
	void ScrollableMenuSection<TItem>::EnsureVisibleSelected(std::int32_t offset)
	{
		if (!_scrollable) {
			return;
		}

		Recti contentBounds = _root->GetContentBounds();
		Recti clipRect = GetClipRectangle(contentBounds);
		std::int32_t topLine = clipRect.Y + 1;
		std::int32_t bottomLine = clipRect.Y + clipRect.H - 1;
		std::int32_t y = _items[_selectedIndex].Y + offset;

		if (y < topLine + ItemHeight / 2) {
			// Scroll up
			_y += (topLine + ItemHeight / 2 - y);
			return;
		}
		
		std::int32_t itemHeight = _items[_selectedIndex].Height - ItemHeight * 4 / 5 + ItemHeight / 2;
		if (y > bottomLine - itemHeight) {
			// Scroll down
			_y += (bottomLine - itemHeight - y);
			return;
		}
	}
}