1 /*
2 * Copyright 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #pragma once
18
19 #include <type_traits>
20
21 namespace android::ui {
22
23 enum class Rotation {
24 Rotation0 = 0,
25 Rotation90 = 1,
26 Rotation180 = 2,
27 Rotation270 = 3,
28
29 ftl_last = Rotation270
30 };
31
32 // Equivalent to Surface.java constants.
33 constexpr auto ROTATION_0 = Rotation::Rotation0;
34 constexpr auto ROTATION_90 = Rotation::Rotation90;
35 constexpr auto ROTATION_180 = Rotation::Rotation180;
36 constexpr auto ROTATION_270 = Rotation::Rotation270;
37
toRotation(std::underlying_type_t<Rotation> rotation)38 constexpr auto toRotation(std::underlying_type_t<Rotation> rotation) {
39 return static_cast<Rotation>(rotation);
40 }
41
toRotationInt(Rotation rotation)42 constexpr auto toRotationInt(Rotation rotation) {
43 return static_cast<std::underlying_type_t<Rotation>>(rotation);
44 }
45
46 constexpr Rotation operator+(Rotation lhs, Rotation rhs) {
47 constexpr auto N = toRotationInt(ROTATION_270) + 1;
48 return toRotation((toRotationInt(lhs) + toRotationInt(rhs)) % N);
49 }
50
51 constexpr Rotation operator-(Rotation lhs, Rotation rhs) {
52 constexpr auto N = toRotationInt(ROTATION_270) + 1;
53 return toRotation((N + toRotationInt(lhs) - toRotationInt(rhs)) % N);
54 }
55
56 constexpr Rotation operator-(Rotation rotation) {
57 return ROTATION_0 - rotation;
58 }
59
toCString(Rotation rotation)60 constexpr const char* toCString(Rotation rotation) {
61 switch (rotation) {
62 case ROTATION_0:
63 return "ROTATION_0";
64 case ROTATION_90:
65 return "ROTATION_90";
66 case ROTATION_180:
67 return "ROTATION_180";
68 case ROTATION_270:
69 return "ROTATION_270";
70 }
71 }
72
73 } // namespace android::ui
74