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 enum class RelativePosition { | |
18 BOTTOM = 0, | |
19 LEFT = 1, | |
20 TOP = 2, | |
21 RIGHT = 3, | |
22 }; | |
23 | |
24 // Returns the relative position of |test| with respect to |ref|. | |
25 // If |test| is below |ref|, then the return value is RelativePosition::BOTTOM. | |
26 // The precedence of relative position in order of highest to lowest is | |
27 // BOTTOM, LEFT, TOP, and finally RIGHT. | |
28 // In other words, if |test| is both below and to the left of |ref|, then the | |
29 // RelativePosition is BOTTOM. | |
30 RelativePosition RectRelativePosition(const gfx::Rect& ref, | |
31 const gfx::Rect& test) { | |
32 if (ref.bottom() <= test.y()) | |
33 return RelativePosition::BOTTOM; | |
34 if (test.right() <= ref.x()) | |
35 return RelativePosition::LEFT; | |
36 if (test.bottom() <= ref.y()) | |
37 return RelativePosition::TOP; | |
38 | |
39 return RelativePosition::RIGHT; | |
40 } | |
41 | |
42 gfx::Rect CoordinateRotateRectangle90(const gfx::Rect& rect) { | |
43 return gfx::Rect(rect.y(), -rect.x() - rect.width(), | |
44 rect.height(), rect.width()); | |
45 } | |
46 | |
47 gfx::Rect CoordinateRotateRectangle180(const gfx::Rect& rect) { | |
48 return gfx::Rect(-rect.x() - rect.width(), -rect.y() -rect.height(), | |
49 rect.width(), rect.height()); | |
50 } | |
51 | |
52 gfx::Rect CoordinateRotateRectangle270(const gfx::Rect& rect) { | |
53 return gfx::Rect(-rect.y() - rect.height(), rect.x(), | |
54 rect.height(), rect.width()); | |
55 } | |
56 | |
57 gfx::Rect CanonicalizeRelativePosition(const gfx::Rect& rect, | |
58 RelativePosition relative_position) { | |
59 switch (relative_position) { | |
60 case RelativePosition::LEFT: | |
61 return CoordinateRotateRectangle90(rect); | |
62 case RelativePosition::TOP: | |
63 return CoordinateRotateRectangle180(rect); | |
64 case RelativePosition::RIGHT: | |
65 return CoordinateRotateRectangle270(rect); | |
66 default: | |
67 return rect; | |
68 } | |
69 } | |
70 | |
17 bool InRange(int target, int lower_bound, int upper_bound) { | 71 bool InRange(int target, int lower_bound, int upper_bound) { |
18 return lower_bound <= target && target <= upper_bound; | 72 return lower_bound <= target && target <= upper_bound; |
19 } | 73 } |
20 | 74 |
21 // Scaled |unscaled_offset| to the same relative position on |unscaled_length| | 75 // Scaled |unscaled_offset| to the same relative position on |unscaled_length| |
22 // based off of |unscaled_length|'s |scale_factor|. | 76 // based off of |unscaled_length|'s |scale_factor|. |
23 int ScaleOffset(int unscaled_length, float scale_factor, int unscaled_offset) { | 77 int ScaleOffset(int unscaled_length, float scale_factor, int unscaled_offset) { |
24 float scaled_length = static_cast<float>(unscaled_length) / scale_factor; | 78 float scaled_length = static_cast<float>(unscaled_length) / scale_factor; |
25 float percent = | 79 float percent = |
26 static_cast<float>(unscaled_offset) / static_cast<float>(unscaled_length); | 80 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 { | 216 } else { |
163 DCHECK(InRange(parent_begin, current_begin, current_end)); | 217 DCHECK(InRange(parent_begin, current_begin, current_end)); |
164 placement.offset = ScaleOffset(current_end - current_begin, | 218 placement.offset = ScaleOffset(current_end - current_begin, |
165 current.device_scale_factor(), | 219 current.device_scale_factor(), |
166 current_begin); | 220 current_begin); |
167 } | 221 } |
168 | 222 |
169 return placement; | 223 return placement; |
170 } | 224 } |
171 | 225 |
226 // This function rotates the rectangles so that ref is always on top of rect, | |
227 // allowing the function to concentrate on comparing |ref|'s bottom corners and | |
228 // |rect|'s top corners when the rects don't overlap vertically. | |
229 int64_t SquaredDistanceBetweenRects(const gfx::Rect& ref, | |
230 const gfx::Rect& rect) { | |
231 if (ref.Intersects(rect)) | |
232 return 0; | |
233 | |
234 RelativePosition relative_position = RectRelativePosition(ref, rect); | |
235 gfx::Rect ref_work(CanonicalizeRelativePosition(ref, relative_position)); | |
236 gfx::Rect rect_work(CanonicalizeRelativePosition(rect, relative_position)); | |
oshima
2016/05/26 21:52:57
Just naming suggestion. how about:
Rotation degre
robliao
2016/05/26 22:28:47
I want to avoid using display::Rotation as that se
oshima
2016/05/26 22:54:47
I didn't mean to suggest display::Rotation. Sorry
| |
237 // Now that the ref is on top, we can concentrate ref bottom | |
238 // and rect top calculations. | |
239 if (rect_work.right() < ref_work.x()) | |
240 return (rect_work.top_right() - ref_work.bottom_left()).LengthSquared(); | |
241 else if (ref_work.right() < rect_work.x()) | |
242 return (rect_work.origin() - ref_work.bottom_right()).LengthSquared(); | |
243 | |
244 int distance = rect_work.y() - ref_work.bottom(); | |
245 return distance * distance; | |
246 } | |
247 | |
172 } // namespace win | 248 } // namespace win |
173 } // namespace display | 249 } // namespace display |
OLD | NEW |