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

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: cc/ fixes Created 8 years, 1 month 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
« no previous file with comments | « ui/gfx/rect_base.h ('k') | ui/gfx/rect_f.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 if (IsEmpty()) {
189 if (IsEmpty()) 194 *this = rect;
190 return rect; 195 return;
196 }
191 if (rect.IsEmpty()) 197 if (rect.IsEmpty())
192 return *static_cast<const Class*>(this); 198 return;
193 199
194 Type rx = std::min(x(), rect.x()); 200 Type rx = std::min(x(), rect.x());
195 Type ry = std::min(y(), rect.y()); 201 Type ry = std::min(y(), rect.y());
196 Type rr = std::max(right(), rect.right()); 202 Type rr = std::max(right(), rect.right());
197 Type rb = std::max(bottom(), rect.bottom()); 203 Type rb = std::max(bottom(), rect.bottom());
198 204
199 return Class(rx, ry, rr - rx, rb - ry); 205 SetRect(rx, ry, rr - rx, rb - ry);
200 } 206 }
201 207
202 template<typename Class, 208 template<typename Class,
203 typename PointClass, 209 typename PointClass,
204 typename SizeClass, 210 typename SizeClass,
205 typename InsetsClass, 211 typename InsetsClass,
206 typename Type> 212 typename Type>
207 Class RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::Subtract( 213 void RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::Subtract(
208 const Class& rect) const { 214 const Class& rect) {
209 // boundary cases:
210 if (!Intersects(rect)) 215 if (!Intersects(rect))
211 return *static_cast<const Class*>(this); 216 return;
212 if (rect.Contains(*static_cast<const Class*>(this))) 217 if (rect.Contains(*static_cast<const Class*>(this))) {
213 return Class(); 218 SetRect(0, 0, 0, 0);
219 return;
220 }
214 221
215 Type rx = x(); 222 Type rx = x();
216 Type ry = y(); 223 Type ry = y();
217 Type rr = right(); 224 Type rr = right();
218 Type rb = bottom(); 225 Type rb = bottom();
219 226
220 if (rect.y() <= y() && rect.bottom() >= bottom()) { 227 if (rect.y() <= y() && rect.bottom() >= bottom()) {
221 // complete intersection in the y-direction 228 // complete intersection in the y-direction
222 if (rect.x() <= x()) { 229 if (rect.x() <= x()) {
223 rx = rect.right(); 230 rx = rect.right();
224 } else { 231 } else {
225 rr = rect.x(); 232 rr = rect.x();
226 } 233 }
227 } else if (rect.x() <= x() && rect.right() >= right()) { 234 } else if (rect.x() <= x() && rect.right() >= right()) {
228 // complete intersection in the x-direction 235 // complete intersection in the x-direction
229 if (rect.y() <= y()) { 236 if (rect.y() <= y()) {
230 ry = rect.bottom(); 237 ry = rect.bottom();
231 } else { 238 } else {
232 rb = rect.y(); 239 rb = rect.y();
233 } 240 }
234 } 241 }
235 return Class(rx, ry, rr - rx, rb - ry); 242 SetRect(rx, ry, rr - rx, rb - ry);
236 } 243 }
237 244
238 template<typename Class, 245 template<typename Class,
239 typename PointClass, 246 typename PointClass,
240 typename SizeClass, 247 typename SizeClass,
241 typename InsetsClass, 248 typename InsetsClass,
242 typename Type> 249 typename Type>
243 Class RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::AdjustToFit( 250 void RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::AdjustToFit(
244 const Class& rect) const { 251 const Class& rect) {
245 Type new_x = x(); 252 Type new_x = x();
246 Type new_y = y(); 253 Type new_y = y();
247 Type new_width = width(); 254 Type new_width = width();
248 Type new_height = height(); 255 Type new_height = height();
249 AdjustAlongAxis(rect.x(), rect.width(), &new_x, &new_width); 256 AdjustAlongAxis(rect.x(), rect.width(), &new_x, &new_width);
250 AdjustAlongAxis(rect.y(), rect.height(), &new_y, &new_height); 257 AdjustAlongAxis(rect.y(), rect.height(), &new_y, &new_height);
251 return Class(new_x, new_y, new_width, new_height); 258 SetRect(new_x, new_y, new_width, new_height);
252 } 259 }
253 260
254 template<typename Class, 261 template<typename Class,
255 typename PointClass, 262 typename PointClass,
256 typename SizeClass, 263 typename SizeClass,
257 typename InsetsClass, 264 typename InsetsClass,
258 typename Type> 265 typename Type>
259 PointClass RectBase<Class, PointClass, SizeClass, InsetsClass, Type>:: 266 PointClass RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::
260 CenterPoint() const { 267 CenterPoint() const {
261 return PointClass(x() + width() / 2, y() + height() / 2); 268 return PointClass(x() + width() / 2, y() + height() / 2);
262 } 269 }
263 270
264 template<typename Class, 271 template<typename Class,
265 typename PointClass, 272 typename PointClass,
266 typename SizeClass, 273 typename SizeClass,
267 typename InsetsClass, 274 typename InsetsClass,
268 typename Type> 275 typename Type>
269 Class RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::Center( 276 void RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::
270 const SizeClass& size) const { 277 ClampToCenteredSize(const SizeClass& size) {
271 Type new_width = std::min(width(), size.width()); 278 Type new_width = std::min(width(), size.width());
272 Type new_height = std::min(height(), size.height()); 279 Type new_height = std::min(height(), size.height());
273 Type new_x = x() + (width() - new_width) / 2; 280 Type new_x = x() + (width() - new_width) / 2;
274 Type new_y = y() + (height() - new_height) / 2; 281 Type new_y = y() + (height() - new_height) / 2;
275 return Class(new_x, new_y, new_width, new_height); 282 SetRect(new_x, new_y, new_width, new_height);
276 } 283 }
277 284
278 template<typename Class, 285 template<typename Class,
279 typename PointClass, 286 typename PointClass,
280 typename SizeClass, 287 typename SizeClass,
281 typename InsetsClass, 288 typename InsetsClass,
282 typename Type> 289 typename Type>
283 void RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::SplitVertically( 290 void RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::SplitVertically(
284 Class* left_half, Class* right_half) const { 291 Class* left_half, Class* right_half) const {
285 DCHECK(left_half); 292 DCHECK(left_half);
(...skipping 13 matching lines...) Expand all
299 typename Type> 306 typename Type>
300 bool RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::SharesEdgeWith( 307 bool RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::SharesEdgeWith(
301 const Class& rect) const { 308 const Class& rect) const {
302 return (y() == rect.y() && height() == rect.height() && 309 return (y() == rect.y() && height() == rect.height() &&
303 (x() == rect.right() || right() == rect.x())) || 310 (x() == rect.right() || right() == rect.x())) ||
304 (x() == rect.x() && width() == rect.width() && 311 (x() == rect.x() && width() == rect.width() &&
305 (y() == rect.bottom() || bottom() == rect.y())); 312 (y() == rect.bottom() || bottom() == rect.y()));
306 } 313 }
307 314
308 } // namespace gfx 315 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/rect_base.h ('k') | ui/gfx/rect_f.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698