Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "ui/gfx/win/rect_util.h" | |
| 6 | |
| 7 #include "ui/gfx/geometry/rect.h" | |
| 8 #include "ui/gfx/geometry/safe_integer_conversions.h" | |
| 9 #include "ui/gfx/geometry/size.h" | |
| 10 #include "ui/gfx/geometry/vector2d.h" | |
| 11 | |
| 12 namespace { | |
| 13 | |
| 14 enum class RelativePosition { | |
| 15 BOTTOM = 0, | |
| 16 LEFT = 1, | |
| 17 TOP = 2, | |
| 18 RIGHT = 3, | |
| 19 }; | |
| 20 | |
| 21 gfx::Rect CoordinateRotateRectangle90(const gfx::Rect& rect) { | |
| 22 return gfx::Rect(rect.y(), -rect.x() - rect.width(), | |
| 23 rect.height(), rect.width()); | |
| 24 } | |
| 25 | |
| 26 gfx::Rect CoordinateRotateRectangle180(const gfx::Rect& rect) { | |
| 27 return gfx::Rect(-rect.x() - rect.width(), -rect.y() -rect.height(), | |
| 28 rect.width(), rect.height()); | |
| 29 } | |
| 30 | |
| 31 gfx::Rect CoordinateRotateRectangle270(const gfx::Rect& rect) { | |
| 32 return gfx::Rect(-rect.y() - rect.height(), rect.x(), | |
| 33 rect.height(), rect.width()); | |
| 34 } | |
| 35 | |
| 36 gfx::Rect CoordinateReflectRectangleYAxis(const gfx::Rect& rect) { | |
| 37 return gfx::Rect(-rect.x() - rect.width(), rect.y(), | |
| 38 rect.width(), rect.height()); | |
| 39 } | |
| 40 | |
| 41 gfx::Rect CanonicalizeRelativePosition(gfx::Rect rect, | |
| 42 RelativePosition relative_position) { | |
| 43 switch (relative_position) { | |
| 44 case RelativePosition::LEFT: | |
| 45 rect = CoordinateRotateRectangle90(rect); | |
|
oshima
2016/02/11 22:59:58
nit: simply return instead of assigning to rect. s
robliao
2016/02/12 01:52:53
Done.
| |
| 46 break; | |
| 47 case RelativePosition::TOP: | |
| 48 rect = CoordinateRotateRectangle180(rect); | |
| 49 break; | |
| 50 case RelativePosition::RIGHT: | |
| 51 rect = CoordinateRotateRectangle270(rect); | |
| 52 break; | |
| 53 } | |
|
oshima
2016/02/11 22:59:58
indent. same for other places.
robliao
2016/02/12 01:52:53
Done.
| |
| 54 return rect; | |
| 55 } | |
| 56 | |
| 57 gfx::Rect CanonicalizeTouchingEdge(gfx::Rect rect, | |
| 58 gfx::win::RectEdge edge) { | |
| 59 switch (edge) { | |
| 60 case gfx::win::RectEdge::LEFT: | |
| 61 rect = CoordinateRotateRectangle90(rect); | |
| 62 break; | |
| 63 case gfx::win::RectEdge::TOP: | |
| 64 rect = CoordinateRotateRectangle180(rect); | |
| 65 // Helps prefer left and top alignment. | |
| 66 rect = CoordinateReflectRectangleYAxis(rect); | |
| 67 break; | |
| 68 case gfx::win::RectEdge::RIGHT: | |
| 69 rect = CoordinateRotateRectangle270(rect); | |
| 70 // Helps prefer left and top alignment. | |
| 71 rect = CoordinateReflectRectangleYAxis(rect); | |
| 72 break; | |
| 73 } | |
| 74 return rect; | |
| 75 } | |
| 76 | |
| 77 // Inverse of CanonicalizeTouchingEdge. | |
| 78 gfx::Rect RevertToOriginalEdge(gfx::Rect rect, gfx::win::RectEdge edge) { | |
| 79 switch (edge) { | |
| 80 case gfx::win::RectEdge::LEFT: | |
| 81 rect = CoordinateRotateRectangle270(rect); | |
| 82 break; | |
| 83 case gfx::win::RectEdge::TOP: | |
| 84 rect = CoordinateReflectRectangleYAxis(rect); | |
| 85 rect = CoordinateRotateRectangle180(rect); | |
| 86 break; | |
| 87 case gfx::win::RectEdge::RIGHT: | |
| 88 rect = CoordinateReflectRectangleYAxis(rect); | |
| 89 rect = CoordinateRotateRectangle90(rect); | |
| 90 break; | |
| 91 } | |
| 92 return rect; | |
| 93 } | |
| 94 | |
| 95 bool InRange(int target, int lower_bound, int upper_bound) { | |
| 96 return lower_bound <= target && target <= upper_bound; | |
| 97 } | |
| 98 | |
| 99 // Scales |val| within |old_min| and |old_max| to |new_min| and |new_max|. | |
| 100 int ScaleValue(int new_min, int new_max, int old_min, int old_max, int val) { | |
| 101 int old_delta = old_max - old_min; | |
| 102 if (old_delta == 0) { | |
| 103 DCHECK(new_min == new_max); | |
|
oshima
2016/02/11 22:59:58
DCHECK_EQ
robliao
2016/02/12 01:52:53
Done.
| |
| 104 return new_min; | |
| 105 } | |
| 106 float percent = (float)(val - old_min) / (float)old_delta; | |
|
oshima
2016/02/11 22:59:58
static_cast
robliao
2016/02/12 01:52:53
Done.
| |
| 107 return new_min + gfx::ToFlooredInt(percent * (float)(new_max - new_min)); | |
| 108 } | |
| 109 | |
| 110 RelativePosition RectRelativePosition(gfx::Rect ref, gfx::Rect test) { | |
| 111 int i = 0; | |
| 112 for (; i <= 3; ++i) { | |
| 113 if (ref.bottom() <= test.y()) { | |
| 114 break; | |
| 115 } else { | |
| 116 ref = CoordinateRotateRectangle90(ref); | |
| 117 test = CoordinateRotateRectangle90(test); | |
| 118 } | |
| 119 } | |
| 120 switch (i) { | |
| 121 case 0: | |
| 122 return RelativePosition::BOTTOM; | |
| 123 case 1: | |
| 124 return RelativePosition::LEFT; | |
| 125 case 2: | |
| 126 return RelativePosition::TOP; | |
| 127 default: | |
| 128 DCHECK(i == 3); | |
| 129 return RelativePosition::RIGHT; | |
| 130 } | |
| 131 } | |
| 132 | |
| 133 } // namespace | |
| 134 | |
| 135 namespace gfx { | |
| 136 namespace win { | |
| 137 | |
| 138 RectEdge FindTouchingRectEdge(gfx::Rect ref, gfx::Rect test) { | |
| 139 for (int i = 0; i <= 3; ++i) { | |
| 140 if (ref.bottom() == test.y() && | |
| 141 (InRange(test.x(), ref.x(), ref.right()) || | |
| 142 InRange(test.right(), ref.x(), ref.right()) || | |
| 143 InRange(ref.x(), test.x(), test.right()) || | |
| 144 InRange(ref.right(), test.x(), test.right()))) { | |
|
oshima
2016/02/11 22:59:58
this indent looks odd to me.
I believe this is te
robliao
2016/02/12 01:52:53
The parens certainly make it look odd, but I think
oshima
2016/02/12 23:18:26
It looks to me that this is more like
if (Stateme
robliao
2016/02/13 01:27:49
Applied the formatting.
We're in agreement that I
oshima
2016/02/16 06:03:26
Acknowledged.
| |
| 145 switch (i) { | |
| 146 case 0: | |
| 147 return RectEdge::BOTTOM; | |
| 148 case 1: | |
| 149 return RectEdge::LEFT; | |
| 150 case 2: | |
| 151 return RectEdge::TOP; | |
| 152 case 3: | |
| 153 return RectEdge::RIGHT; | |
| 154 } | |
| 155 } else { | |
| 156 ref = CoordinateRotateRectangle90(ref); | |
| 157 test = CoordinateRotateRectangle90(test); | |
| 158 } | |
| 159 } | |
| 160 return RectEdge::NONE; | |
| 161 } | |
| 162 | |
| 163 // Writing code that specifically deals with scaling each rect is tedious and | |
| 164 // error prone. Instead, this function transforms the positions of the | |
| 165 // two rects so that |ref_scaled_rect| is always on top of |ref_original_rect|, | |
| 166 // calculates the scaled and positioned target rect, and then reverses the | |
| 167 // transforms. As a result, the position logic can be written in terms of the | |
| 168 // bottom of the |ref_original_rect| instead of the bottom, left, top, and | |
| 169 // right. | |
| 170 gfx::Rect ScaleAndPositionRect(gfx::Rect ref_scaled_rect, | |
| 171 gfx::Rect ref_original_rect, | |
| 172 gfx::Rect target_rect, | |
| 173 float target_scale_factor) { | |
| 174 RectEdge orig_edge = FindTouchingRectEdge(ref_original_rect, target_rect); | |
| 175 | |
| 176 // Scale size only since the scaled origin location will be determined below. | |
| 177 gfx::Rect scaled_rect = gfx::ScaleToEnclosingRect(target_rect, | |
| 178 1.0f / target_scale_factor); | |
| 179 scaled_rect.set_origin(gfx::Point(0, 0)); | |
| 180 | |
| 181 if (orig_edge == RectEdge::NONE) { | |
| 182 // Currently, we expect all rectangles to touch each other. | |
| 183 NOTREACHED(); | |
| 184 scaled_rect.set_origin(target_rect.origin()); | |
| 185 } | |
| 186 | |
| 187 // Transform rectangles so we can simply deal with bottom edge sharing. | |
| 188 ref_scaled_rect = CanonicalizeTouchingEdge(ref_scaled_rect, orig_edge); | |
| 189 ref_original_rect = CanonicalizeTouchingEdge(ref_original_rect, orig_edge); | |
| 190 target_rect = CanonicalizeTouchingEdge(target_rect, orig_edge); | |
| 191 scaled_rect = CanonicalizeTouchingEdge(scaled_rect, orig_edge); | |
| 192 | |
| 193 // Position the rect. | |
| 194 scaled_rect.set_y(ref_scaled_rect.bottom()); | |
| 195 int x; | |
| 196 if (target_rect.right() == ref_original_rect.right()) { | |
| 197 // Maintain right alignment. If the rectangle was left-aligned, the next | |
| 198 // case will catch that. | |
| 199 x = ref_scaled_rect.right() - scaled_rect.width(); | |
| 200 } else if (InRange(target_rect.x(), | |
| 201 ref_original_rect.x(), | |
| 202 ref_original_rect.right())) { | |
| 203 // Position using the left point relative to the reference rect. | |
| 204 x = ScaleValue(ref_scaled_rect.x(), ref_scaled_rect.right(), | |
| 205 ref_original_rect.x(), ref_original_rect.right(), | |
| 206 target_rect.x()); | |
| 207 } else if (InRange(target_rect.right(), | |
| 208 ref_original_rect.x(), | |
| 209 ref_original_rect.right())) { | |
| 210 // Position using the right point relative to the reference rect. | |
| 211 x = ScaleValue(ref_scaled_rect.x(), ref_scaled_rect.right(), | |
| 212 ref_original_rect.x(), ref_original_rect.right(), | |
| 213 target_rect.right()) - scaled_rect.width(); | |
| 214 } else if (InRange(ref_scaled_rect.x(), | |
| 215 target_rect.x(), | |
| 216 target_rect.right())) { | |
| 217 // Position relative to the left point of the reference rect. | |
| 218 int offset = ScaleValue(0, scaled_rect.width(), | |
| 219 0, target_rect.width(), | |
| 220 ref_original_rect.x() - target_rect.x()); | |
| 221 x = ref_scaled_rect.x() - offset; | |
| 222 } else { | |
| 223 // Position relative to the right point of the reference rect. | |
| 224 int offset = ScaleValue(0, scaled_rect.width(), | |
| 225 0, target_rect.width(), | |
| 226 ref_original_rect.right() - target_rect.x()); | |
| 227 x = ref_scaled_rect.x() - offset - scaled_rect.width(); | |
| 228 } | |
| 229 scaled_rect.set_x(x); | |
| 230 DCHECK( | |
| 231 FindTouchingRectEdge(ref_scaled_rect, scaled_rect) == RectEdge::BOTTOM); | |
| 232 | |
| 233 // Reverse the transformation. | |
| 234 return RevertToOriginalEdge(scaled_rect, orig_edge); | |
| 235 } | |
| 236 | |
| 237 // This function takes the same approach as ScaleAndPositionRect, transforming | |
| 238 // the two input rects so that the relative positions are always the same. | |
| 239 int64_t SquaredDistanceBetweenRects(gfx::Rect ref, gfx::Rect rect) { | |
| 240 if (ref.Intersects(rect)) | |
| 241 return 0; | |
| 242 | |
| 243 RelativePosition relative_position = RectRelativePosition(ref, rect); | |
| 244 ref = CanonicalizeRelativePosition(ref, relative_position); | |
| 245 rect = CanonicalizeRelativePosition(rect, relative_position); | |
| 246 // Now that the ref is on top, we can concentrate ref bottom | |
| 247 // and rect top calculations. | |
| 248 if (rect.right() < ref.x()) | |
| 249 return (rect.top_right() - ref.bottom_left()).LengthSquared(); | |
| 250 else if (ref.right() < rect.x()) | |
| 251 return (rect.origin() - ref.bottom_right()).LengthSquared(); | |
| 252 | |
| 253 int distance = rect.y() - ref.bottom(); | |
| 254 return distance * distance; | |
| 255 } | |
| 256 | |
| 257 } // namespace win | |
| 258 } // namespace gfx | |
| OLD | NEW |