File: TouchControlsOptionsSection.cpp

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 (144 lines) | stat: -rw-r--r-- 6,139 bytes parent folder | download | duplicates (2)
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
#include "TouchControlsOptionsSection.h"
#include "MenuResources.h"
#include "../HUD.h"
#include "../../PreferencesCache.h"
#include "../../LevelHandler.h"

#include "../../../nCine/I18n.h"

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

namespace Jazz2::UI::Menu
{
	TouchControlsOptionsSection::TouchControlsOptionsSection()
		: _isDirty(false), _selectedZone(SelectedZone::None), _lastPointerId(-1)
	{
	}

	TouchControlsOptionsSection::~TouchControlsOptionsSection()
	{
		if (_isDirty) {
			_isDirty = false;
			PreferencesCache::Save();
		}
	}

	void TouchControlsOptionsSection::OnUpdate(float timeMult)
	{
		if (_root->ActionHit(PlayerAction::Menu)) {
			_root->PlaySfx("MenuSelect"_s, 0.5f);
			_root->LeaveSection();
			return;
		}
	}

	void TouchControlsOptionsSection::OnDraw(Canvas* canvas)
	{
		Vector2i viewSize = canvas->ViewSize;
		Recti contentBounds = _root->GetContentBounds();
		Vector2f center = Vector2f(contentBounds.X + contentBounds.W * 0.5f, contentBounds.Y + contentBounds.H * 0.5f);
		float topLine = contentBounds.Y + 31.0f;

		_root->DrawElement(MenuDim, center.X, topLine - 2.0f, IMenuContainer::BackgroundLayer,
			Alignment::Top, Colorf::Black, Vector2f(680.0f, 200.0f), Vector4f(1.0f, 0.0f, -0.7f, 0.7f));
		_root->DrawElement(MenuLine, 0, center.X, topLine, IMenuContainer::MainLayer, Alignment::Center, Colorf::White, 1.6f);

		std::int32_t charOffset = 0;
		_root->DrawStringShadow(_("Touch Controls"), charOffset, center.X, topLine - 21.0f, IMenuContainer::FontLayer,
			Alignment::Center, Colorf(0.46f, 0.46f, 0.46f, 0.5f), 0.9f, 0.7f, 1.1f, 1.1f, 0.4f, 0.9f);

		// TRANSLATORS: Header in Options > Controls > Touch Controls section
		_root->DrawStringShadow(_("You can adjust position of the touch zones by drag and drop."), charOffset, center.X, topLine + 40.0f, IMenuContainer::FontLayer,
			Alignment::Top, Colorf(0.62f, 0.44f, 0.34f, 0.5f), 0.9f, 0.4f, 0.6f, 0.6f, 0.6f, 0.9f, 1.2f);

		float leftSize = HUD::DpadSize * LevelHandler::DefaultWidth * 0.45f;
		DrawOutlinedSolid(HUD::DpadLeft * LevelHandler::DefaultWidth + PreferencesCache::TouchLeftPadding.X, viewSize.Y - HUD::DpadBottom * LevelHandler::DefaultHeight + PreferencesCache::TouchLeftPadding.Y, IMenuContainer::MainLayer + 20, Alignment::BottomLeft, Vector2f(leftSize, leftSize));

		float rightSizeX = HUD::ButtonSize * LevelHandler::DefaultWidth * 1.6f;
		float rightSizeY = HUD::ButtonSize * LevelHandler::DefaultWidth * 0.8f;
		DrawOutlinedSolid(viewSize.X - PreferencesCache::TouchRightPadding.X, viewSize.Y - 0.04f * LevelHandler::DefaultHeight + PreferencesCache::TouchRightPadding.Y, IMenuContainer::MainLayer + 20, Alignment::BottomRight, Vector2f(rightSizeX, rightSizeY));
	}

	void TouchControlsOptionsSection::OnTouchEvent(const nCine::TouchEvent& event, Vector2i viewSize)
	{
		switch(event.type) {
			case TouchEventType::Down: {
				std::int32_t pointerIndex = event.findPointerIndex(event.actionIndex);
				if (pointerIndex != -1) {
					float x = event.pointers[pointerIndex].x;
					float y = event.pointers[pointerIndex].y * (float)viewSize.Y;

					if (y < 80.0f) {
						_root->PlaySfx("MenuSelect"_s, 0.5f);
						_root->LeaveSection();
						return;
					}

					if (y > 120.0f) {
						if (x < 0.4f) {
							_selectedZone = SelectedZone::Left;
						} else if (x > 0.6f) {
							_selectedZone = SelectedZone::Right;
						}
						_lastPos = Vector2f(event.pointers[pointerIndex].x, event.pointers[pointerIndex].y);
						_lastPointerId = event.actionIndex;
					}
				}
				break;
			}
			case TouchEventType::Move: {
				if (event.actionIndex == _lastPointerId) {
					std::int32_t pointerIndex = event.findPointerIndex(event.actionIndex);
					if (pointerIndex != -1) {
						Vector2f newPos = Vector2f(event.pointers[pointerIndex].x, event.pointers[pointerIndex].y);
						Vector2f diff = (newPos - _lastPos) * Vector2f(static_cast<float>(viewSize.X), static_cast<float>(viewSize.Y));
						_lastPos = newPos;

						switch (_selectedZone) {
							case SelectedZone::Left:
								PreferencesCache::TouchLeftPadding.X = std::round(std::clamp(PreferencesCache::TouchLeftPadding.X + diff.X, -130.0f, 200.0f));
								PreferencesCache::TouchLeftPadding.Y = std::round(std::clamp(PreferencesCache::TouchLeftPadding.Y + diff.Y, -100.0f, 140.0f));
								break;
							case SelectedZone::Right:
								PreferencesCache::TouchRightPadding.X = std::round(std::clamp(PreferencesCache::TouchRightPadding.X - diff.X, -180.0f, 170.0f));
								PreferencesCache::TouchRightPadding.Y = std::round(std::clamp(PreferencesCache::TouchRightPadding.Y + diff.Y, -140.0f, 100.0f));
								break;
						}

						_isDirty = true;
					}
				}
				break;
			}
			case TouchEventType::Up: {
				_selectedZone = SelectedZone::None;
				_lastPointerId = -1;
				break;
			}
			case TouchEventType::PointerUp: {
				if (event.actionIndex == _lastPointerId) {
					_selectedZone = SelectedZone::None;
					_lastPointerId = -1;
				}
				break;
			}
		}
	}

	void TouchControlsOptionsSection::DrawOutlinedSolid(float x, float y, std::uint16_t z, Alignment align, Vector2f size)
	{
		_root->DrawSolid(x, y, z, align, size, Colorf(1.0f, 1.0f, 1.0f, 0.5f));

		if ((align & Alignment::Right) == Alignment::Right) {
			_root->DrawSolid(x + 2.0f, y - size.Y, z + 1, align, Vector2f(size.X + 4.0f, 2.0f), Colorf(0.0f, 0.0f, 0.0f, 1.0f));
			_root->DrawSolid(x + 2.0f, y + 1.0f, z + 1, align, Vector2f(size.X + 4.0f, 2.0f), Colorf(0.0f, 0.0f, 0.0f, 1.0f));
			_root->DrawSolid(x + 2.0f, y, z + 1, align, Vector2f(2.0f, size.Y), Colorf(0.0f, 0.0f, 0.0f, 1.0f));
			_root->DrawSolid(x - size.X, y, z + 1, align, Vector2f(2.0f, size.Y), Colorf(0.0f, 0.0f, 0.0f, 1.0f));
		} else {
			_root->DrawSolid(x - 2.0f, y - size.Y, z + 1, align, Vector2f(size.X + 4.0f, 2.0f), Colorf(0.0f, 0.0f, 0.0f, 1.0f));
			_root->DrawSolid(x - 2.0f, y + 1.0f, z + 1, align, Vector2f(size.X + 4.0f, 2.0f), Colorf(0.0f, 0.0f, 0.0f, 1.0f));
			_root->DrawSolid(x - 2.0f, y, z + 1, align, Vector2f(2.0f, size.Y), Colorf(0.0f, 0.0f, 0.0f, 1.0f));
			_root->DrawSolid(x + size.X, y, z + 1, align, Vector2f(2.0f, size.Y), Colorf(0.0f, 0.0f, 0.0f, 1.0f));
		}
	}
}