Dominik Laskowski | 718f960 | 2019-11-09 20:01:35 -0800 | [diff] [blame] | 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 { Rotation0 = 0, Rotation90 = 1, Rotation180 = 2, Rotation270 = 3 }; |
| 24 | |
| 25 | // Equivalent to Surface.java constants. |
| 26 | constexpr auto ROTATION_0 = Rotation::Rotation0; |
| 27 | constexpr auto ROTATION_90 = Rotation::Rotation90; |
| 28 | constexpr auto ROTATION_180 = Rotation::Rotation180; |
| 29 | constexpr auto ROTATION_270 = Rotation::Rotation270; |
| 30 | |
| 31 | constexpr auto toRotation(std::underlying_type_t<Rotation> rotation) { |
| 32 | return static_cast<Rotation>(rotation); |
| 33 | } |
| 34 | |
| 35 | constexpr auto toRotationInt(Rotation rotation) { |
| 36 | return static_cast<std::underlying_type_t<Rotation>>(rotation); |
| 37 | } |
| 38 | |
| 39 | constexpr 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 Shalamanov | 6ad317c | 2020-07-29 23:34:07 +0200 | [diff] [blame] | 44 | constexpr 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 | |
| 49 | constexpr Rotation operator-(Rotation rotation) { |
| 50 | return ROTATION_0 - rotation; |
| 51 | } |
| 52 | |
Dominik Laskowski | 718f960 | 2019-11-09 20:01:35 -0800 | [diff] [blame] | 53 | constexpr 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 |