blob: c4dd55ba349ff796ec23fd39efb89752400dff3b [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
Mathias Agopian35801ce2009-05-26 17:44:57 -07002 * Copyright (C) 2009 The Android Open Source Project
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003 *
Mathias Agopian35801ce2009-05-26 17:44:57 -07004 * 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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08007 *
Mathias Agopian35801ce2009-05-26 17:44:57 -07008 * 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.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080015 */
16
Jamie Gennis59332802012-05-07 13:49:17 -070017#include <system/graphics.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080018#include <ui/Rect.h>
19
20namespace android {
21
Dianne Hackborn9147d112010-07-09 11:44:11 -070022static inline int32_t min(int32_t a, int32_t b) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080023 return (a<b) ? a : b;
24}
25
Dianne Hackborn9147d112010-07-09 11:44:11 -070026static inline int32_t max(int32_t a, int32_t b) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027 return (a>b) ? a : b;
28}
29
30void Rect::makeInvalid() {
31 left = 0;
32 top = 0;
33 right = -1;
34 bottom = -1;
35}
36
37bool Rect::operator < (const Rect& rhs) const
38{
39 if (top<rhs.top) {
40 return true;
41 } else if (top == rhs.top) {
42 if (left < rhs.left) {
43 return true;
44 } else if (left == rhs.left) {
45 if (bottom<rhs.bottom) {
46 return true;
47 } else if (bottom == rhs.bottom) {
48 if (right<rhs.right) {
49 return true;
50 }
51 }
52 }
53 }
54 return false;
55}
56
Dianne Hackborn9147d112010-07-09 11:44:11 -070057Rect& Rect::offsetTo(int32_t x, int32_t y)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058{
59 right -= left - x;
60 bottom -= top - y;
61 left = x;
62 top = y;
63 return *this;
64}
65
Dianne Hackborn9147d112010-07-09 11:44:11 -070066Rect& Rect::offsetBy(int32_t x, int32_t y)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067{
68 left += x;
69 top += y;
70 right+= x;
71 bottom+=y;
72 return *this;
73}
74
Mathias Agopian35801ce2009-05-26 17:44:57 -070075const Rect Rect::operator + (const Point& rhs) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080076{
Mathias Agopian35801ce2009-05-26 17:44:57 -070077 const Rect result(left+rhs.x, top+rhs.y, right+rhs.x, bottom+rhs.y);
78 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080079}
80
Mathias Agopian35801ce2009-05-26 17:44:57 -070081const Rect Rect::operator - (const Point& rhs) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080082{
Mathias Agopian35801ce2009-05-26 17:44:57 -070083 const Rect result(left-rhs.x, top-rhs.y, right-rhs.x, bottom-rhs.y);
84 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080085}
86
87bool Rect::intersect(const Rect& with, Rect* result) const
88{
89 result->left = max(left, with.left);
90 result->top = max(top, with.top);
91 result->right = min(right, with.right);
92 result->bottom = min(bottom, with.bottom);
93 return !(result->isEmpty());
94}
95
Jamie Gennisf15a83f2012-05-10 20:43:55 -070096Rect Rect::transform(uint32_t xform, int32_t width, int32_t height) const {
Jamie Gennis59332802012-05-07 13:49:17 -070097 Rect result(*this);
98 if (xform & HAL_TRANSFORM_FLIP_H) {
99 result = Rect(width - result.right, result.top,
100 width - result.left, result.bottom);
101 }
102 if (xform & HAL_TRANSFORM_FLIP_V) {
103 result = Rect(result.left, height - result.bottom,
104 result.right, height - result.top);
105 }
106 if (xform & HAL_TRANSFORM_ROT_90) {
107 int left = height - result.bottom;
108 int top = result.left;
109 int right = height - result.top;
110 int bottom = result.right;
111 result = Rect(left, top, right, bottom);
112 }
113 return result;
114}
115
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116}; // namespace android