roo_display
API Documentation for roo_display
Loading...
Searching...
No Matches
orientation.h
Go to the documentation of this file.
1#pragma once
2
3#include <assert.h>
4#include <inttypes.h>
5
6#include "roo_logging.h"
7
8namespace roo_display {
9
10/// Represents the orientation of a display device.
11///
12/// A display has a natural physical placement defining absolute up/down/left/
13/// right. `Orientation` maps logical (x, y) coordinates to those directions.
14/// Examples:
15/// - RightDown: x increases left-to-right, y increases top-to-bottom (default).
16/// - UpRight: x increases bottom-to-top, y increases left-to-right.
17///
18/// Internally, orientation is stored as a byte and can be passed by value.
19/// There are 8 possible values, which can be viewed as:
20/// - 4 rotations (90-degree steps) with optional mirror flip
21/// - 3 binary parameters: x/y swapped, horizontal direction, vertical
22/// direction.
23///
24/// Helper methods are provided for common transformations.
26 public:
27 /// Horizontal direction of the display.
29
30 /// Vertical direction of the display.
32
41
42 [[deprecated("Use `Orientation::kLeftToRight` instead.")]]
44 [[deprecated("Use `Orientation::kRightToLeft` instead.")]]
46 [[deprecated("Use `Orientation::kTopToBottom` instead.")]]
48 [[deprecated("Use `Orientation::kBottomToTop` instead.")]]
50
51 constexpr Orientation() : orientation_(0) {}
52
53 /// Return the default orientation (RightDown).
54 static constexpr Orientation Default() { return RightDown(); }
55
56 /// Return a specific orientation value.
57 ///
58 /// Each static factory describes how logical x/y axes map to physical
59 /// directions, using the naming convention {x-direction}{y-direction}.
60 /// For example, RightDown means x increases to the right and y increases
61 /// downward.
62 static constexpr Orientation RightDown() { return Orientation(0); }
63 /// Return orientation where x increases downward and y increases to the
64 /// right.
65 static constexpr Orientation DownRight() { return Orientation(1); }
66 /// Return orientation where x increases to the left and y increases downward.
67 static constexpr Orientation LeftDown() { return Orientation(2); }
68 /// Return orientation where x increases downward and y increases to the left.
69 static constexpr Orientation DownLeft() { return Orientation(3); }
70 /// Return orientation where x increases to the right and y increases upward.
71 static constexpr Orientation RightUp() { return Orientation(4); }
72 /// Return orientation where x increases upward and y increases to the right.
73 static constexpr Orientation UpRight() { return Orientation(5); }
74 /// Return orientation where x increases to the left and y increases upward.
75 static constexpr Orientation LeftUp() { return Orientation(6); }
76 /// Return orientation where x increases upward and y increases to the left.
77 static constexpr Orientation UpLeft() { return Orientation(7); }
78
79 /// Return the default orientation rotated by `count` * 90 degrees.
80 ///
81 /// @param count Number of 90-degree clockwise steps (negative allowed).
82 static Orientation RotatedByCount(int count) {
83 return Default().rotateClockwise(count);
84 }
85
86 /// Swap the mapping between x/y and horizontal/vertical directions.
87 Orientation swapXY() const { return Orientation(orientation_ ^ 1); }
88
89 /// Return whether x maps to the vertical direction.
90 bool isXYswapped() const { return (orientation_ & 1) == 1; }
91
92 /// Return the horizontal direction.
94 return (orientation_ & 2) == 0 ? kLeftToRight : kRightToLeft;
95 }
96
97 /// Return whether horizontal direction is left-to-right.
98 bool isLeftToRight() const {
100 }
101
102 /// Return whether horizontal direction is right-to-left.
103 bool isRightToLeft() const {
105 }
106
107 /// Flip the horizontal direction.
108 Orientation flipHorizontally() const { return Orientation(orientation_ ^ 2); }
109
110 /// Return the vertical direction.
112 return (orientation_ & 4) == 0 ? kTopToBottom : kBottomToTop;
113 }
114
115 /// Return whether vertical direction is top-to-bottom.
116 bool isTopToBottom() const { return getVerticalDirection() == kTopToBottom; }
117
118 /// Return whether vertical direction is bottom-to-top.
119 bool isBottomToTop() const { return getVerticalDirection() == kBottomToTop; }
120
121 /// Flip the vertical direction.
122 Orientation flipVertically() const { return Orientation(orientation_ ^ 4); }
123
124 /// Return whether text would appear mirrored in this orientation.
125 bool isMirrored() const {
126 static const bool mirrored[8] = {0, 1, 1, 0, 1, 0, 0, 1};
127 return mirrored[orientation_];
128 }
129
130 /// Rotate 90 degrees clockwise.
132 static const uint8_t successors[8] = {3, 2, 7, 6, 1, 0, 5, 4};
133 return Orientation(successors[orientation_]);
134 }
135
136 /// Rotate 90 degrees counter-clockwise.
138
139 /// Rotate 180 degrees.
141 // Combination of vertical and horizontal flip.
142 return Orientation(orientation_ ^ 0x06);
143 }
144
145 /// Rotate clockwise by `turns` * 90 degrees.
147 switch (turns & 3) {
148 case 0:
149 return *this;
150 case 1:
151 return rotateRight();
152 case 2:
153 return rotateUpsideDown();
154 case 3:
155 return rotateLeft();
156 default:
157 assert(false);
158 return *this;
159 }
160 }
161
162 /// Rotate counter-clockwise by `turns` * 90 degrees.
166
167 /// Return how many right rotations reach this orientation from default.
168 int getRotationCount() const {
169 const uint8_t counts[] = {0, 1, 3, 2};
170 return counts[orientation_ >> 1];
171 }
172
173 /// Flip along the x axis (horizontal or vertical depending on swap).
176 }
177
178 /// Flip along the y axis (vertical or horizontal depending on swap).
181 }
182
183 /// Return a common name for this orientation.
184 const char* asString() const {
185 static const char* names[] = {"RightDown", "DownRight", "LeftDown",
186 "DownLeft", "RightUp", "UpRight",
187 "LeftUp", "UpLeft"};
188 return names[orientation_];
189 }
190
191 private:
192 friend bool operator==(Orientation a, Orientation b);
193 friend bool operator!=(Orientation a, Orientation b);
194
195 constexpr Orientation(uint8_t orientation) : orientation_(orientation) {}
196
197 uint8_t orientation_;
198};
199
201 return a.orientation_ == b.orientation_;
202}
203
205 return a.orientation_ != b.orientation_;
206}
207
208} // namespace roo_display
Represents the orientation of a display device.
Definition orientation.h:25
static constexpr Orientation UpLeft()
Return orientation where x increases upward and y increases to the left.
Definition orientation.h:77
Orientation rotateClockwise(int turns) const
Rotate clockwise by turns * 90 degrees.
bool isMirrored() const
Return whether text would appear mirrored in this orientation.
static constexpr VerticalDirection BOTTOM_TO_TOP
Definition orientation.h:49
static constexpr Orientation RightDown()
Return a specific orientation value.
Definition orientation.h:62
int getRotationCount() const
Return how many right rotations reach this orientation from default.
friend bool operator==(Orientation a, Orientation b)
VerticalDirection getVerticalDirection() const
Return the vertical direction.
Orientation rotateRight() const
Rotate 90 degrees clockwise.
static constexpr Orientation UpRight()
Return orientation where x increases upward and y increases to the right.
Definition orientation.h:73
Orientation swapXY() const
Swap the mapping between x/y and horizontal/vertical directions.
Definition orientation.h:87
static constexpr Orientation DownLeft()
Return orientation where x increases downward and y increases to the left.
Definition orientation.h:69
static constexpr Orientation LeftUp()
Return orientation where x increases to the left and y increases upward.
Definition orientation.h:75
HorizontalDirection
Horizontal direction of the display.
Definition orientation.h:28
static constexpr VerticalDirection TOP_TO_BOTTOM
Definition orientation.h:47
bool isRightToLeft() const
Return whether horizontal direction is right-to-left.
bool isLeftToRight() const
Return whether horizontal direction is left-to-right.
Definition orientation.h:98
Orientation rotateUpsideDown() const
Rotate 180 degrees.
Orientation flipHorizontally() const
Flip the horizontal direction.
Orientation rotateLeft() const
Rotate 90 degrees counter-clockwise.
bool isTopToBottom() const
Return whether vertical direction is top-to-bottom.
static Orientation RotatedByCount(int count)
Return the default orientation rotated by count * 90 degrees.
Definition orientation.h:82
static constexpr HorizontalDirection kRightToLeft
Definition orientation.h:35
static constexpr Orientation RightUp()
Return orientation where x increases to the right and y increases upward.
Definition orientation.h:71
static constexpr HorizontalDirection LEFT_TO_RIGHT
Definition orientation.h:43
static constexpr HorizontalDirection RIGHT_TO_LEFT
Definition orientation.h:45
const char * asString() const
Return a common name for this orientation.
Orientation flipVertically() const
Flip the vertical direction.
friend bool operator!=(Orientation a, Orientation b)
static constexpr Orientation LeftDown()
Return orientation where x increases to the left and y increases downward.
Definition orientation.h:67
Orientation flipX() const
Flip along the x axis (horizontal or vertical depending on swap).
Orientation rotateCounterClockwise(int turns) const
Rotate counter-clockwise by turns * 90 degrees.
Orientation flipY() const
Flip along the y axis (vertical or horizontal depending on swap).
static constexpr VerticalDirection kBottomToTop
Definition orientation.h:39
VerticalDirection
Vertical direction of the display.
Definition orientation.h:31
static constexpr VerticalDirection kTopToBottom
Definition orientation.h:37
static constexpr Orientation DownRight()
Return orientation where x increases downward and y increases to the right.
Definition orientation.h:65
static constexpr Orientation Default()
Return the default orientation (RightDown).
Definition orientation.h:54
static constexpr HorizontalDirection kLeftToRight
Definition orientation.h:33
bool isBottomToTop() const
Return whether vertical direction is bottom-to-top.
bool isXYswapped() const
Return whether x maps to the vertical direction.
Definition orientation.h:90
HorizontalDirection getHorizontalDirection() const
Return the horizontal direction.
Definition orientation.h:93
Defines 140 opaque HTML named colors.
constexpr bool operator==(const Color &a, const Color &b)
Equality operator for colors.
Definition color.h:99
constexpr bool operator!=(const Color &a, const Color &b)
Inequality operator for colors.
Definition color.h:104