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

Side by Side Diff: ui/views/rect_based_targeting_utils.cc

Issue 22891016: Add support for rect-based event targeting in views (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: comments addressed, moved fuzzing utility functions to new file Created 7 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2013 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/views/rect_based_targeting_utils.h"
6
7 namespace views {
8
9 namespace {
10
11 // The positive distance from |pos| to the nearest endpoint of the interval
12 // [start, end] is returned if |pos| lies within the interval, otherwise
13 // 0 is returned.
14 int DistanceToInterval(int pos, int start, int end) {
15 if (pos < start)
16 return start - pos;
17 if (pos > end)
18 return pos - end;
19 return 0;
20 }
21
22 } // namespace
23
24 bool UsePointBasedTargeting(const gfx::Rect& rect) {
25 return rect.width() == 1 && rect.height() == 1;
26 }
27
28 float PercentCoveredBy(const gfx::Rect& rect_1, const gfx::Rect& rect_2) {
29 gfx::Rect intersection(rect_1);
30 intersection.Intersect(rect_2);
31 int intersection_area = intersection.size().GetArea();
32 int rect_1_area = rect_1.size().GetArea();
33 return rect_1_area ? (float)intersection_area / (float)rect_1_area : 0;
sky 2013/10/08 20:01:12 use c++ casts, eg static_cast<float> ...
tdanderson 2013/10/09 21:42:06 Done.
34 }
35
36 int DistanceSquaredFromCenterLineToPoint(const gfx::Point& point,
37 const gfx::Rect& rect) {
38 gfx::Point center_point = rect.CenterPoint();
39 int dx = center_point.x() - point.x();
40 int dy = center_point.y() - point.y();
41
42 if (rect.width() > rect.height()) {
43 dx = DistanceToInterval(point.x(),
44 rect.x() + (rect.height() / 2),
45 rect.right() - (rect.height() / 2));
46 } else {
47 dy = DistanceToInterval(point.y(),
48 rect.y() + (rect.width() / 2),
49 rect.bottom() - (rect.width() / 2));
50 }
51 return (dx * dx) + (dy * dy);
52 }
53
54 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698