OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/display/win/scaling_util.h" | 5 #include "ui/display/win/scaling_util.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "ui/gfx/geometry/rect.h" | 9 #include "ui/gfx/geometry/rect.h" |
10 #include "ui/gfx/geometry/safe_integer_conversions.h" | 10 #include "ui/gfx/geometry/safe_integer_conversions.h" |
11 #include "ui/gfx/geometry/size.h" | 11 #include "ui/gfx/geometry/size.h" |
12 #include "ui/gfx/geometry/vector2d.h" | 12 #include "ui/gfx/geometry/vector2d.h" |
13 #include "ui/gfx/range/range.h" | 13 #include "ui/gfx/range/range.h" |
14 | 14 |
15 namespace { | 15 namespace { |
16 | 16 |
17 // Represents the amount of rotation an object has about a coordinate plane. | |
18 enum class CoordinateRotation { | |
19 COORDINATE_ROTATE_0, | |
20 COORDINATE_ROTATE_90, | |
21 COORDINATE_ROTATE_180, | |
22 COORDINATE_ROTATE_270, | |
23 }; | |
24 | |
25 // Returns the CoordinateRotation necessary for |ref| and |other| so that |ref| | |
26 // is positioned on top of |other|. | |
27 CoordinateRotation ComputeCoordinateRotationRefTop(const gfx::Rect& ref, | |
28 const gfx::Rect& other) { | |
29 if (ref.bottom() <= other.y()) | |
30 return CoordinateRotation::COORDINATE_ROTATE_0; | |
31 if (other.right() <= ref.x()) | |
32 return CoordinateRotation::COORDINATE_ROTATE_90; | |
33 if (other.bottom() <= ref.y()) | |
34 return CoordinateRotation::COORDINATE_ROTATE_180; | |
35 | |
36 return CoordinateRotation::COORDINATE_ROTATE_270; | |
37 } | |
38 | |
39 gfx::Rect CoordinateRotateRectangle90(const gfx::Rect& rect) { | |
40 return gfx::Rect(rect.y(), -rect.x() - rect.width(), | |
41 rect.height(), rect.width()); | |
42 } | |
43 | |
44 gfx::Rect CoordinateRotateRectangle180(const gfx::Rect& rect) { | |
45 return gfx::Rect(-rect.x() - rect.width(), -rect.y() -rect.height(), | |
46 rect.width(), rect.height()); | |
47 } | |
48 | |
49 gfx::Rect CoordinateRotateRectangle270(const gfx::Rect& rect) { | |
50 return gfx::Rect(-rect.y() - rect.height(), rect.x(), | |
51 rect.height(), rect.width()); | |
52 } | |
53 | |
54 gfx::Rect CoordinateRotateRect(const gfx::Rect& rect, | |
55 CoordinateRotation rotation) { | |
56 switch (rotation) { | |
57 case CoordinateRotation::COORDINATE_ROTATE_90: | |
58 return CoordinateRotateRectangle90(rect); | |
59 case CoordinateRotation::COORDINATE_ROTATE_180: | |
60 return CoordinateRotateRectangle180(rect); | |
61 case CoordinateRotation::COORDINATE_ROTATE_270: | |
62 return CoordinateRotateRectangle270(rect); | |
63 default: | |
64 return rect; | |
65 } | |
66 } | |
67 | |
17 bool InRange(int target, int lower_bound, int upper_bound) { | 68 bool InRange(int target, int lower_bound, int upper_bound) { |
18 return lower_bound <= target && target <= upper_bound; | 69 return lower_bound <= target && target <= upper_bound; |
19 } | 70 } |
20 | 71 |
21 // Scaled |unscaled_offset| to the same relative position on |unscaled_length| | 72 // Scaled |unscaled_offset| to the same relative position on |unscaled_length| |
22 // based off of |unscaled_length|'s |scale_factor|. | 73 // based off of |unscaled_length|'s |scale_factor|. |
23 int ScaleOffset(int unscaled_length, float scale_factor, int unscaled_offset) { | 74 int ScaleOffset(int unscaled_length, float scale_factor, int unscaled_offset) { |
24 float scaled_length = static_cast<float>(unscaled_length) / scale_factor; | 75 float scaled_length = static_cast<float>(unscaled_length) / scale_factor; |
25 float percent = | 76 float percent = |
26 static_cast<float>(unscaled_offset) / static_cast<float>(unscaled_length); | 77 static_cast<float>(unscaled_offset) / static_cast<float>(unscaled_length); |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
162 } else { | 213 } else { |
163 DCHECK(InRange(parent_begin, current_begin, current_end)); | 214 DCHECK(InRange(parent_begin, current_begin, current_end)); |
164 placement.offset = ScaleOffset(current_end - current_begin, | 215 placement.offset = ScaleOffset(current_end - current_begin, |
165 current.device_scale_factor(), | 216 current.device_scale_factor(), |
166 current_begin); | 217 current_begin); |
167 } | 218 } |
168 | 219 |
169 return placement; | 220 return placement; |
170 } | 221 } |
171 | 222 |
223 // This function rotates the rectangles so that ref is always on top of rect, | |
oshima
2016/05/26 22:54:48
Since the commnd says ref is on top of rect, why d
robliao
2016/05/26 23:19:24
At this point ref is not necessarily on top of rec
oshima
2016/05/26 23:43:13
Ah ok. nvm.
| |
224 // allowing the function to concentrate on comparing |ref|'s bottom corners and | |
225 // |rect|'s top corners when the rects don't overlap vertically. | |
226 int64_t SquaredDistanceBetweenRects(const gfx::Rect& ref, | |
227 const gfx::Rect& rect) { | |
228 if (ref.Intersects(rect)) | |
229 return 0; | |
230 | |
231 CoordinateRotation degrees = ComputeCoordinateRotationRefTop(ref, rect); | |
232 gfx::Rect top_rect(CoordinateRotateRect(ref, degrees)); | |
233 gfx::Rect bottom_rect(CoordinateRotateRect(rect, degrees)); | |
234 if (bottom_rect.right() < top_rect.x()) | |
235 return (bottom_rect.top_right() - top_rect.bottom_left()).LengthSquared(); | |
236 else if (top_rect.right() < bottom_rect.x()) | |
237 return (bottom_rect.origin() - top_rect.bottom_right()).LengthSquared(); | |
238 | |
239 int distance = bottom_rect.y() - top_rect.bottom(); | |
240 return distance * distance; | |
241 } | |
242 | |
172 } // namespace win | 243 } // namespace win |
173 } // namespace display | 244 } // namespace display |
OLD | NEW |