Skip to content

Commit a4a7e2e

Browse files
committed
Rotation required UI for strum app
1 parent 5d0bad0 commit a4a7e2e

2 files changed

Lines changed: 142 additions & 1 deletion

File tree

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
#include <MatrixOS.h>
2+
3+
bool RotationRequiredUI(bool up, bool down, bool left, bool right) {
4+
bool rotated = false;
5+
6+
EDirection current_orientation = MatrixOS::UserVar::rotation;
7+
8+
if(!up && !down && !left && !right)
9+
{
10+
MLOGW("RotationRequiredUI", "No direction is set to true, nothing to do");
11+
return false;
12+
}
13+
14+
if((up && current_orientation == UP) || (down && current_orientation == DOWN) || (left && current_orientation == LEFT) || (right && current_orientation == RIGHT))
15+
{
16+
MLOGD("RotationRequiredUI", "Already in correct orientation, nothing to do");
17+
return true;
18+
}
19+
20+
UI rotationRequiredUI("Rotation Required", Color(0x00FF00), true);
21+
22+
uint32_t start_time = MatrixOS::SYS::Millis();
23+
24+
UIButton brightnessBtn;
25+
brightnessBtn.SetName("Brightness");
26+
brightnessBtn.SetColor(Color(0xFFFFFF));
27+
brightnessBtn.SetSize(Dimension(2, 2));
28+
brightnessBtn.OnPress([&]() -> void { MatrixOS::LED::NextBrightness(); });
29+
rotationRequiredUI.AddUIComponent(brightnessBtn, Point(3, 3));
30+
31+
// Rotation control and canvas
32+
UIButton rotateUpBtn;
33+
rotateUpBtn.SetName("Rotate Up");
34+
rotateUpBtn.SetSize(Dimension(2, 1));
35+
EDirection actual_direction = current_orientation;
36+
if((actual_direction == UP && up) || (actual_direction == DOWN && down) || (actual_direction == LEFT && left) || (actual_direction == RIGHT && right))
37+
{
38+
rotateUpBtn.SetColorFunc([&]() -> Color { return ColorEffects::ColorBreathLowBound(Color(0x00FF00), 64, 2000, start_time); });
39+
rotateUpBtn.OnPress([&]() -> void {
40+
MatrixOS::SYS::Rotate(UP);
41+
rotated = true;
42+
rotationRequiredUI.Exit();
43+
});
44+
}
45+
else
46+
{
47+
rotateUpBtn.SetColor(Color(0x00FF00).Dim());
48+
}
49+
rotationRequiredUI.AddUIComponent(rotateUpBtn, Point(3, 2));
50+
51+
UIButton rotateRightBtn;
52+
rotateRightBtn.SetName("Rotate Right");
53+
rotateRightBtn.SetSize(Dimension(1, 2));
54+
actual_direction = (EDirection)((current_orientation + 90) % 360);
55+
if((actual_direction == UP && up) || (actual_direction == DOWN && down) || (actual_direction == LEFT && left) || (actual_direction == RIGHT && right))
56+
{
57+
rotateRightBtn.SetColorFunc([&]() -> Color { return ColorEffects::ColorBreathLowBound(Color(0x00FF00), 64, 2000, start_time); });
58+
rotateRightBtn.OnPress([&]() -> void {
59+
MatrixOS::SYS::Rotate(RIGHT);
60+
rotated = true;
61+
rotationRequiredUI.Exit();
62+
});
63+
}
64+
else
65+
{
66+
rotateRightBtn.SetColor(Color(0x00FF00).Dim());
67+
}
68+
rotationRequiredUI.AddUIComponent(rotateRightBtn, Point(5, 3));
69+
70+
UIButton rotateDownBtn;
71+
rotateDownBtn.SetName("Rotate Down");
72+
rotateDownBtn.SetSize(Dimension(2, 1));
73+
actual_direction = (EDirection)((current_orientation + 180) % 360);
74+
if((actual_direction == UP && up) || (actual_direction == DOWN && down) || (actual_direction == LEFT && left) || (actual_direction == RIGHT && right))
75+
{
76+
rotateDownBtn.SetColorFunc([&]() -> Color { return ColorEffects::ColorBreathLowBound(Color(0x00FF00), 64, 2000, start_time); });
77+
rotateDownBtn.OnPress([&]() -> void {
78+
MatrixOS::SYS::Rotate(DOWN);
79+
rotated = true;
80+
rotationRequiredUI.Exit();
81+
});
82+
}
83+
else
84+
{
85+
rotateDownBtn.SetColor(Color(0x00FF00).Dim());
86+
}
87+
rotationRequiredUI.AddUIComponent(rotateDownBtn, Point(3, 5));
88+
89+
UIButton rotateLeftBtn;
90+
rotateLeftBtn.SetName("Rotate Left");
91+
rotateLeftBtn.SetSize(Dimension(1, 2));
92+
actual_direction = (EDirection)((current_orientation + 270) % 360);
93+
if((actual_direction == UP && up) || (actual_direction == DOWN && down) || (actual_direction == LEFT && left) || (actual_direction == RIGHT && right))
94+
{
95+
rotateLeftBtn.SetColorFunc([&]() -> Color { return ColorEffects::ColorBreathLowBound(Color(0x00FF00), 64, 2000, start_time); });
96+
rotateLeftBtn.OnPress([&]() -> void {
97+
MatrixOS::SYS::Rotate(LEFT);
98+
rotated = true;
99+
rotationRequiredUI.Exit();
100+
});
101+
}
102+
else
103+
{
104+
rotateLeftBtn.SetColor(Color(0x00FF00).Dim());
105+
}
106+
rotationRequiredUI.AddUIComponent(rotateLeftBtn, Point(2, 3));
107+
108+
109+
// Second, set the key event handler to match the intended behavior
110+
rotationRequiredUI.SetKeyEventHandler([&](KeyEvent* keyEvent) -> bool {
111+
// If function key is hold down. Exit the application
112+
if (keyEvent->id == FUNCTION_KEY)
113+
{
114+
if (keyEvent->info.state == RELEASED)
115+
{
116+
rotationRequiredUI.Exit(); // Exit the UI
117+
}
118+
119+
return true; // Block UI from to do anything with FN, basically this function control the life cycle of the UI
120+
}
121+
return false; // Nothing happened. Let the UI handle the key event
122+
});
123+
124+
// // The UI object is now fully set up. Let the UI runtime to start and take over.
125+
rotationRequiredUI.Start();
126+
127+
return rotated;
128+
}

Applications/Strum/Strum.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,15 @@
44
#include "StrumDurationModifier.h"
55
#include "StrumBar.h"
66

7+
#include "RotationRequiredUI.h"
8+
79
void Strum::Setup() {
810

11+
if(RotationRequiredUI(true, true, false, false) == false)
12+
{
13+
Exit();
14+
}
15+
916
// Load From NVS
1017
if (nvsVersion == (uint32_t)STRUM_APP_VERSION)
1118
{
@@ -119,7 +126,13 @@ void Strum::ActionMenu() {
119126
UIButton systemSettingBtn;
120127
systemSettingBtn.SetName("System Setting");
121128
systemSettingBtn.SetColor(Color(0xFFFFFF));
122-
systemSettingBtn.OnPress([&]() -> void { MatrixOS::SYS::OpenSetting(); });
129+
systemSettingBtn.OnPress([&]() -> void {
130+
MatrixOS::SYS::OpenSetting();
131+
if(RotationRequiredUI(true, true, false, false) == false)
132+
{
133+
Exit();
134+
}
135+
});
123136
actionMenu.AddUIComponent(systemSettingBtn, Point(7, 7));
124137

125138
actionMenu.SetKeyEventHandler([&](KeyEvent* keyEvent) -> bool {

0 commit comments

Comments
 (0)