Explicitly add python-numpy dependency to install-build-deps.
[chromium-blink-merge.git] / ui / events / gestures / motion_event_aura.cc
blob11f8ba14c73a48a5d1f713f23055736cf01e29af
1 // Copyright 2014 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.
5 // MSVC++ requires this to be set before any other includes to get M_PI.
6 #define _USE_MATH_DEFINES
8 #include "ui/events/gestures/motion_event_aura.h"
10 #include <cmath>
12 #include "base/logging.h"
13 #include "ui/events/gesture_detection/gesture_configuration.h"
15 namespace ui {
16 namespace {
18 PointerProperties GetPointerPropertiesFromTouchEvent(const TouchEvent& touch) {
19 PointerProperties pointer_properties;
20 pointer_properties.x = touch.x();
21 pointer_properties.y = touch.y();
22 pointer_properties.raw_x = touch.root_location_f().x();
23 pointer_properties.raw_y = touch.root_location_f().y();
24 pointer_properties.id = touch.touch_id();
25 pointer_properties.pressure = touch.force();
26 pointer_properties.source_device_id = touch.source_device_id();
28 float radius_x = touch.radius_x();
29 float radius_y = touch.radius_y();
30 float rotation_angle_rad = touch.rotation_angle() * M_PI / 180.f;
31 DCHECK_GE(radius_x, 0) << "Unexpected x-radius < 0";
32 DCHECK_GE(radius_y, 0) << "Unexpected y-radius < 0";
33 DCHECK(0 <= rotation_angle_rad && rotation_angle_rad <= M_PI_2)
34 << "Unexpected touch rotation angle";
36 if (radius_x > radius_y) {
37 // The case radius_x == radius_y is omitted from here on purpose: for
38 // circles, we want to pass the angle (which could be any value in such
39 // cases but always seem to be set to zero) unchanged.
40 pointer_properties.touch_major = 2.f * radius_x;
41 pointer_properties.touch_minor = 2.f * radius_y;
42 pointer_properties.orientation = rotation_angle_rad - M_PI_2;
43 } else {
44 pointer_properties.touch_major = 2.f * radius_y;
45 pointer_properties.touch_minor = 2.f * radius_x;
46 pointer_properties.orientation = rotation_angle_rad;
49 if (!pointer_properties.touch_major) {
50 pointer_properties.touch_major =
51 2.f * GestureConfiguration::GetInstance()->default_radius();
52 pointer_properties.touch_minor =
53 2.f * GestureConfiguration::GetInstance()->default_radius();
54 pointer_properties.orientation = 0;
57 // TODO(jdduke): Plumb tool type from the platform, crbug.com/404128.
58 pointer_properties.tool_type = MotionEvent::TOOL_TYPE_UNKNOWN;
60 return pointer_properties;
63 } // namespace
65 MotionEventAura::MotionEventAura() {
66 set_action_index(-1);
69 MotionEventAura::~MotionEventAura() {
72 void MotionEventAura::OnTouch(const TouchEvent& touch) {
73 switch (touch.type()) {
74 case ET_TOUCH_PRESSED:
75 AddTouch(touch);
76 break;
77 case ET_TOUCH_RELEASED:
78 case ET_TOUCH_CANCELLED:
79 // Removing these touch points needs to be postponed until after the
80 // MotionEvent has been dispatched. This cleanup occurs in
81 // CleanupRemovedTouchPoints.
82 UpdateTouch(touch);
83 break;
84 case ET_TOUCH_MOVED:
85 UpdateTouch(touch);
86 break;
87 default:
88 NOTREACHED();
89 break;
92 UpdateCachedAction(touch);
93 set_flags(touch.flags());
94 set_event_time(touch.time_stamp() + base::TimeTicks());
97 int MotionEventAura::GetId() const {
98 return GetPointerId(0);
101 void MotionEventAura::CleanupRemovedTouchPoints(const TouchEvent& event) {
102 if (event.type() != ET_TOUCH_RELEASED &&
103 event.type() != ET_TOUCH_CANCELLED) {
104 return;
107 DCHECK(GetPointerCount());
108 int index_to_delete = GetIndexFromId(event.touch_id());
109 set_action_index(0);
110 pointer(index_to_delete) = pointer(GetPointerCount() - 1);
111 PopPointer();
114 int MotionEventAura::GetSourceDeviceId(size_t pointer_index) const {
115 DCHECK_LT(pointer_index, GetPointerCount());
116 return pointer(pointer_index).source_device_id;
119 void MotionEventAura::AddTouch(const TouchEvent& touch) {
120 if (GetPointerCount() == MotionEvent::MAX_TOUCH_POINT_COUNT)
121 return;
123 PushPointer(GetPointerPropertiesFromTouchEvent(touch));
126 void MotionEventAura::UpdateTouch(const TouchEvent& touch) {
127 pointer(GetIndexFromId(touch.touch_id())) =
128 GetPointerPropertiesFromTouchEvent(touch);
131 void MotionEventAura::UpdateCachedAction(const TouchEvent& touch) {
132 DCHECK(GetPointerCount());
133 switch (touch.type()) {
134 case ET_TOUCH_PRESSED:
135 if (GetPointerCount() == 1) {
136 set_action(ACTION_DOWN);
137 } else {
138 set_action(ACTION_POINTER_DOWN);
139 set_action_index(GetIndexFromId(touch.touch_id()));
141 break;
142 case ET_TOUCH_RELEASED:
143 if (GetPointerCount() == 1) {
144 set_action(ACTION_UP);
145 } else {
146 set_action(ACTION_POINTER_UP);
147 set_action_index(GetIndexFromId(touch.touch_id()));
149 break;
150 case ET_TOUCH_CANCELLED:
151 set_action(ACTION_CANCEL);
152 break;
153 case ET_TOUCH_MOVED:
154 set_action(ACTION_MOVE);
155 break;
156 default:
157 NOTREACHED();
158 break;
162 int MotionEventAura::GetIndexFromId(int id) const {
163 int index = FindPointerIndexOfId(id);
164 DCHECK_GE(index, 0);
165 DCHECK_LT(index, static_cast<int>(GetPointerCount()));
166 return index;
169 } // namespace ui