Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(149)

Side by Side Diff: ui/gfx/rect_base_impl.h

Issue 11110004: Make gfx::Rect class operations consistently mutate the class they are called on. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/gfx/rect_base.h" 5 #include "ui/gfx/rect_base.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/stringprintf.h" 8 #include "base/stringprintf.h"
9 9
10 // This file provides the implementation for RectBaese template and 10 // This file provides the implementation for RectBaese template and
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
158 const Class& rect) const { 158 const Class& rect) const {
159 return !(rect.x() >= right() || rect.right() <= x() || 159 return !(rect.x() >= right() || rect.right() <= x() ||
160 rect.y() >= bottom() || rect.bottom() <= y()); 160 rect.y() >= bottom() || rect.bottom() <= y());
161 } 161 }
162 162
163 template<typename Class, 163 template<typename Class,
164 typename PointClass, 164 typename PointClass,
165 typename SizeClass, 165 typename SizeClass,
166 typename InsetsClass, 166 typename InsetsClass,
167 typename Type> 167 typename Type>
168 Class RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::Intersect( 168 void RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::Intersect(
169 const Class& rect) const { 169 const Class& rect) {
170 if (IsEmpty() || rect.IsEmpty()) {
171 SetRect(0, 0, 0, 0);
172 return;
173 }
174
170 Type rx = std::max(x(), rect.x()); 175 Type rx = std::max(x(), rect.x());
171 Type ry = std::max(y(), rect.y()); 176 Type ry = std::max(y(), rect.y());
172 Type rr = std::min(right(), rect.right()); 177 Type rr = std::min(right(), rect.right());
173 Type rb = std::min(bottom(), rect.bottom()); 178 Type rb = std::min(bottom(), rect.bottom());
174 179
175 if (rx >= rr || ry >= rb) 180 if (rx >= rr || ry >= rb)
176 rx = ry = rr = rb = 0; // non-intersecting 181 rx = ry = rr = rb = 0; // non-intersecting
177 182
178 return Class(rx, ry, rr - rx, rb - ry); 183 SetRect(rx, ry, rr - rx, rb - ry);
179 } 184 }
180 185
181 template<typename Class, 186 template<typename Class,
182 typename PointClass, 187 typename PointClass,
183 typename SizeClass, 188 typename SizeClass,
184 typename InsetsClass, 189 typename InsetsClass,
185 typename Type> 190 typename Type>
186 Class RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::Union( 191 void RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::Union(
187 const Class& rect) const { 192 const Class& rect) {
188 // special case empty rects... 193 // special case empty rects...
sky 2012/10/12 16:26:38 Remove this comment, it depends clarify anything.
danakj 2012/10/19 20:33:53 Done.
189 if (IsEmpty()) 194 if (IsEmpty()) {
190 return rect; 195 SetRect(rect.x(), rect.y(), rect.width(), rect.height());
sky 2012/10/12 16:26:38 this = rect ?
danakj 2012/10/19 20:33:53 Done.
196 return;
197 }
191 if (rect.IsEmpty()) 198 if (rect.IsEmpty())
192 return *static_cast<const Class*>(this); 199 return;
193 200
194 Type rx = std::min(x(), rect.x()); 201 Type rx = std::min(x(), rect.x());
195 Type ry = std::min(y(), rect.y()); 202 Type ry = std::min(y(), rect.y());
196 Type rr = std::max(right(), rect.right()); 203 Type rr = std::max(right(), rect.right());
197 Type rb = std::max(bottom(), rect.bottom()); 204 Type rb = std::max(bottom(), rect.bottom());
198 205
199 return Class(rx, ry, rr - rx, rb - ry); 206 SetRect(rx, ry, rr - rx, rb - ry);
200 } 207 }
201 208
202 template<typename Class, 209 template<typename Class,
203 typename PointClass, 210 typename PointClass,
204 typename SizeClass, 211 typename SizeClass,
205 typename InsetsClass, 212 typename InsetsClass,
206 typename Type> 213 typename Type>
207 Class RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::Subtract( 214 void RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::Subtract(
208 const Class& rect) const { 215 const Class& rect) {
209 // boundary cases: 216 // boundary cases:
210 if (!Intersects(rect)) 217 if (!Intersects(rect))
211 return *static_cast<const Class*>(this); 218 return;
212 if (rect.Contains(*static_cast<const Class*>(this))) 219 if (rect.Contains(*static_cast<const Class*>(this))) {
213 return Class(); 220 SetRect(0, 0, 0, 0);
221 return;
222 }
214 223
215 Type rx = x(); 224 Type rx = x();
216 Type ry = y(); 225 Type ry = y();
217 Type rr = right(); 226 Type rr = right();
218 Type rb = bottom(); 227 Type rb = bottom();
219 228
220 if (rect.y() <= y() && rect.bottom() >= bottom()) { 229 if (rect.y() <= y() && rect.bottom() >= bottom()) {
221 // complete intersection in the y-direction 230 // complete intersection in the y-direction
222 if (rect.x() <= x()) { 231 if (rect.x() <= x()) {
223 rx = rect.right(); 232 rx = rect.right();
224 } else { 233 } else {
225 rr = rect.x(); 234 rr = rect.x();
226 } 235 }
227 } else if (rect.x() <= x() && rect.right() >= right()) { 236 } else if (rect.x() <= x() && rect.right() >= right()) {
228 // complete intersection in the x-direction 237 // complete intersection in the x-direction
229 if (rect.y() <= y()) { 238 if (rect.y() <= y()) {
230 ry = rect.bottom(); 239 ry = rect.bottom();
231 } else { 240 } else {
232 rb = rect.y(); 241 rb = rect.y();
233 } 242 }
234 } 243 }
235 return Class(rx, ry, rr - rx, rb - ry); 244 SetRect(rx, ry, rr - rx, rb - ry);
236 } 245 }
237 246
238 template<typename Class, 247 template<typename Class,
239 typename PointClass, 248 typename PointClass,
240 typename SizeClass, 249 typename SizeClass,
241 typename InsetsClass, 250 typename InsetsClass,
242 typename Type> 251 typename Type>
243 Class RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::AdjustToFit( 252 void RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::AdjustToFit(
244 const Class& rect) const { 253 const Class& rect) {
245 Type new_x = x(); 254 Type new_x = x();
246 Type new_y = y(); 255 Type new_y = y();
247 Type new_width = width(); 256 Type new_width = width();
248 Type new_height = height(); 257 Type new_height = height();
249 AdjustAlongAxis(rect.x(), rect.width(), &new_x, &new_width); 258 AdjustAlongAxis(rect.x(), rect.width(), &new_x, &new_width);
250 AdjustAlongAxis(rect.y(), rect.height(), &new_y, &new_height); 259 AdjustAlongAxis(rect.y(), rect.height(), &new_y, &new_height);
251 return Class(new_x, new_y, new_width, new_height); 260 SetRect(new_x, new_y, new_width, new_height);
252 } 261 }
253 262
254 template<typename Class, 263 template<typename Class,
255 typename PointClass, 264 typename PointClass,
256 typename SizeClass, 265 typename SizeClass,
257 typename InsetsClass, 266 typename InsetsClass,
258 typename Type> 267 typename Type>
259 PointClass RectBase<Class, PointClass, SizeClass, InsetsClass, Type>:: 268 PointClass RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::
260 CenterPoint() const { 269 CenterPoint() const {
261 return PointClass(x() + width() / 2, y() + height() / 2); 270 return PointClass(x() + width() / 2, y() + height() / 2);
262 } 271 }
263 272
264 template<typename Class, 273 template<typename Class,
265 typename PointClass, 274 typename PointClass,
266 typename SizeClass, 275 typename SizeClass,
267 typename InsetsClass, 276 typename InsetsClass,
268 typename Type> 277 typename Type>
269 Class RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::Center( 278 void RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::
270 const SizeClass& size) const { 279 ClampToCenteredSize(const SizeClass& size) {
271 Type new_width = std::min(width(), size.width()); 280 Type new_width = std::min(width(), size.width());
272 Type new_height = std::min(height(), size.height()); 281 Type new_height = std::min(height(), size.height());
273 Type new_x = x() + (width() - new_width) / 2; 282 Type new_x = x() + (width() - new_width) / 2;
274 Type new_y = y() + (height() - new_height) / 2; 283 Type new_y = y() + (height() - new_height) / 2;
275 return Class(new_x, new_y, new_width, new_height); 284 SetRect(new_x, new_y, new_width, new_height);
276 } 285 }
277 286
278 template<typename Class, 287 template<typename Class,
279 typename PointClass, 288 typename PointClass,
280 typename SizeClass, 289 typename SizeClass,
281 typename InsetsClass, 290 typename InsetsClass,
282 typename Type> 291 typename Type>
283 void RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::SplitVertically( 292 void RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::SplitVertically(
284 Class* left_half, Class* right_half) const { 293 Class* left_half, Class* right_half) const {
285 DCHECK(left_half); 294 DCHECK(left_half);
(...skipping 13 matching lines...) Expand all
299 typename Type> 308 typename Type>
300 bool RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::SharesEdgeWith( 309 bool RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::SharesEdgeWith(
301 const Class& rect) const { 310 const Class& rect) const {
302 return (y() == rect.y() && height() == rect.height() && 311 return (y() == rect.y() && height() == rect.height() &&
303 (x() == rect.right() || right() == rect.x())) || 312 (x() == rect.right() || right() == rect.x())) ||
304 (x() == rect.x() && width() == rect.width() && 313 (x() == rect.x() && width() == rect.width() &&
305 (y() == rect.bottom() || bottom() == rect.y())); 314 (y() == rect.bottom() || bottom() == rect.y()));
306 } 315 }
307 316
308 } // namespace gfx 317 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698