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