blob: 83d431dea38fb3ffae6109448af8c972d9257cea [file] [log] [blame]
Dominik Laskowski718f9602019-11-09 20:01:35 -08001/*
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
21namespace android::ui {
22
23enum class Rotation { Rotation0 = 0, Rotation90 = 1, Rotation180 = 2, Rotation270 = 3 };
24
25// Equivalent to Surface.java constants.
26constexpr auto ROTATION_0 = Rotation::Rotation0;
27constexpr auto ROTATION_90 = Rotation::Rotation90;
28constexpr auto ROTATION_180 = Rotation::Rotation180;
29constexpr auto ROTATION_270 = Rotation::Rotation270;
30
31constexpr auto toRotation(std::underlying_type_t<Rotation> rotation) {
32 return static_cast<Rotation>(rotation);
33}
34
35constexpr auto toRotationInt(Rotation rotation) {
36 return static_cast<std::underlying_type_t<Rotation>>(rotation);
37}
38
39constexpr Rotation operator+(Rotation lhs, Rotation rhs) {
40 constexpr auto N = toRotationInt(ROTATION_270) + 1;
41 return toRotation((toRotationInt(lhs) + toRotationInt(rhs)) % N);
42}
43
Marin Shalamanov6ad317c2020-07-29 23:34:07 +020044constexpr Rotation operator-(Rotation lhs, Rotation rhs) {
45 constexpr auto N = toRotationInt(ROTATION_270) + 1;
46 return toRotation((N + toRotationInt(lhs) - toRotationInt(rhs)) % N);
47}
48
49constexpr Rotation operator-(Rotation rotation) {
50 return ROTATION_0 - rotation;
51}
52
Dominik Laskowski718f9602019-11-09 20:01:35 -080053constexpr const char* toCString(Rotation rotation) {
54 switch (rotation) {
55 case ROTATION_0:
56 return "ROTATION_0";
57 case ROTATION_90:
58 return "ROTATION_90";
59 case ROTATION_180:
60 return "ROTATION_180";
61 case ROTATION_270:
62 return "ROTATION_270";
63 }
64}
65
66} // namespace android::ui