summaryrefslogtreecommitdiff
path: root/libs/ui/include/ui/FloatRect.h
blob: 4c9c7b7ef183acf878c06ca433dd40a0fa205455 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
 * Copyright 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <math/HashCombine.h>
#include <ostream>

namespace android {

class FloatRect {
public:
    FloatRect() = default;
    constexpr FloatRect(float _left, float _top, float _right, float _bottom)
      : left(_left), top(_top), right(_right), bottom(_bottom) {}

    float getWidth() const { return right - left; }
    float getHeight() const { return bottom - top; }

    FloatRect intersect(const FloatRect& other) const {
        FloatRect intersection = {
            // Inline to avoid tromping on other min/max defines or adding a
            // dependency on STL
            (left > other.left) ? left : other.left,
            (top > other.top) ? top : other.top,
            (right < other.right) ? right : other.right,
            (bottom < other.bottom) ? bottom : other.bottom
        };
        if (intersection.getWidth() < 0 || intersection.getHeight() < 0) {
            return {0, 0, 0, 0};
        }
        return intersection;
    }

    float left = 0.0f;
    float top = 0.0f;
    float right = 0.0f;
    float bottom = 0.0f;

    constexpr bool isEmpty() const { return !(left < right && top < bottom); }
};

inline bool operator==(const FloatRect& a, const FloatRect& b) {
    return a.left == b.left && a.top == b.top && a.right == b.right && a.bottom == b.bottom;
}

static inline void PrintTo(const FloatRect& rect, ::std::ostream* os) {
    *os << "FloatRect(" << rect.left << ", " << rect.top << ", " << rect.right << ", "
        << rect.bottom << ")";
}

}  // namespace android

namespace std {

template <>
struct hash<android::FloatRect> {
    size_t operator()(const android::FloatRect& rect) const {
        return android::hashCombine(rect.left, rect.top, rect.right, rect.bottom);
    }
};
} // namespace std