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+ }
0 commit comments