OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "content/browser/renderer_host/input/synthetic_pinch_gesture.h" | 5 #include "content/browser/renderer_host/input/synthetic_pinch_gesture.h" |
6 | 6 |
7 #include <cmath> | 7 #include <cmath> |
8 | 8 |
9 #include "content/common/input/input_event.h" | 9 #include "content/common/input/input_event.h" |
10 #include "ui/events/latency_info.h" | 10 #include "ui/events/latency_info.h" |
11 | 11 |
12 namespace content { | 12 namespace content { |
13 namespace { | 13 namespace { |
14 | 14 |
15 // TODO(dominikg): Use touch slop to compute this value. | 15 // TODO(dominikg): Use touch slop to compute this value. |
16 const float kMinPointerDistance = 40.0f; | 16 const float kMinPointerDistance = 40.0f; |
17 | 17 |
18 } | 18 } |
19 | 19 |
20 SyntheticPinchGesture::SyntheticPinchGesture( | 20 SyntheticPinchGesture::SyntheticPinchGesture( |
21 const SyntheticPinchGestureParams& params) | 21 const SyntheticPinchGestureParams& params) |
22 : params_(params), started_(false) { | 22 : params_(params), |
23 current_y_0_(0), | |
bulach
2013/12/02 18:24:51
nit: wouldn't make any difference, but 0.0f would
Dominik Grewe
2013/12/02 18:50:04
Done.
| |
24 current_y_1_(0), | |
25 target_y_0_(0), | |
26 target_y_1_(0), | |
27 started_(false) { | |
23 DCHECK_GE(params_.total_num_pixels_covered, 0); | 28 DCHECK_GE(params_.total_num_pixels_covered, 0); |
24 | |
25 float inner_distance_to_anchor = kMinPointerDistance / 2.0f; | |
26 float outer_distance_to_anchor = | |
27 inner_distance_to_anchor + params_.total_num_pixels_covered / 2.0f; | |
28 | |
29 // Move pointers away from each other to zoom in | |
30 // or towards each other to zoom out. | |
31 if (params_.zoom_in) { | |
32 current_y_0_ = params_.anchor.y() - inner_distance_to_anchor; | |
33 current_y_1_ = params_.anchor.y() + inner_distance_to_anchor; | |
34 target_y_0_ = params_.anchor.y() - outer_distance_to_anchor; | |
35 target_y_1_ = params_.anchor.y() + outer_distance_to_anchor; | |
36 } else { | |
37 current_y_0_ = params_.anchor.y() - outer_distance_to_anchor; | |
38 current_y_1_ = params_.anchor.y() + outer_distance_to_anchor; | |
39 target_y_0_ = params_.anchor.y() - inner_distance_to_anchor; | |
40 target_y_1_ = params_.anchor.y() + inner_distance_to_anchor; | |
41 } | |
42 } | 29 } |
43 | 30 |
44 SyntheticPinchGesture::~SyntheticPinchGesture() {} | 31 SyntheticPinchGesture::~SyntheticPinchGesture() {} |
45 | 32 |
46 SyntheticGesture::Result SyntheticPinchGesture::ForwardInputEvents( | 33 SyntheticGesture::Result SyntheticPinchGesture::ForwardInputEvents( |
47 const base::TimeDelta& interval, SyntheticGestureTarget* target) { | 34 const base::TimeDelta& interval, SyntheticGestureTarget* target) { |
48 | 35 |
49 SyntheticGestureParams::GestureSourceType source = | 36 SyntheticGestureParams::GestureSourceType source = |
50 params_.gesture_source_type; | 37 params_.gesture_source_type; |
51 if (source == SyntheticGestureParams::DEFAULT_INPUT) | 38 if (source == SyntheticGestureParams::DEFAULT_INPUT) |
52 source = target->GetDefaultSyntheticGestureSourceType(); | 39 source = target->GetDefaultSyntheticGestureSourceType(); |
53 | 40 |
54 if (!target->SupportsSyntheticGestureSourceType(source)) | 41 if (!target->SupportsSyntheticGestureSourceType(source)) |
55 return SyntheticGesture::GESTURE_SOURCE_TYPE_NOT_SUPPORTED_BY_PLATFORM; | 42 return SyntheticGesture::GESTURE_SOURCE_TYPE_NOT_SUPPORTED_BY_PLATFORM; |
56 | 43 |
57 if (source == SyntheticGestureParams::TOUCH_INPUT) | 44 if (source == SyntheticGestureParams::TOUCH_INPUT) |
58 return ForwardTouchInputEvents(interval, target); | 45 return ForwardTouchInputEvents(interval, target); |
59 else | 46 else |
60 return SyntheticGesture::GESTURE_SOURCE_TYPE_NOT_IMPLEMENTED; | 47 return SyntheticGesture::GESTURE_SOURCE_TYPE_NOT_IMPLEMENTED; |
61 } | 48 } |
62 | 49 |
63 SyntheticGesture::Result SyntheticPinchGesture::ForwardTouchInputEvents( | 50 SyntheticGesture::Result SyntheticPinchGesture::ForwardTouchInputEvents( |
64 const base::TimeDelta& interval, SyntheticGestureTarget* target) { | 51 const base::TimeDelta& interval, SyntheticGestureTarget* target) { |
65 if (HasFinished()) | 52 if (!started_) { |
66 return SyntheticGesture::GESTURE_FINISHED; | 53 if (params_.total_num_pixels_covered == 0) |
54 return SyntheticGesture::GESTURE_FINISHED; | |
67 | 55 |
68 if (!started_) { | 56 SetupCoordinates(target); |
57 | |
69 touch_event_.PressPoint(params_.anchor.x(), current_y_0_); | 58 touch_event_.PressPoint(params_.anchor.x(), current_y_0_); |
70 touch_event_.PressPoint(params_.anchor.x(), current_y_1_); | 59 touch_event_.PressPoint(params_.anchor.x(), current_y_1_); |
71 ForwardTouchEvent(target); | 60 ForwardTouchEvent(target); |
72 started_ = true; | 61 started_ = true; |
73 } | 62 } |
74 | 63 |
75 // Compute the delta for the first pointer. The other one moves exactly | 64 // Compute the delta for the first pointer. The other one moves exactly |
76 // the same but in the opposite direction. | 65 // the same but in the opposite direction. |
77 float delta = GetDeltaForPointer0(interval); | 66 float delta = GetDeltaForPointer0(interval); |
78 current_y_0_ += delta; | 67 current_y_0_ += delta; |
(...skipping 15 matching lines...) Expand all Loading... | |
94 } | 83 } |
95 | 84 |
96 return SyntheticGesture::GESTURE_RUNNING; | 85 return SyntheticGesture::GESTURE_RUNNING; |
97 } | 86 } |
98 | 87 |
99 void SyntheticPinchGesture::ForwardTouchEvent(SyntheticGestureTarget* target) { | 88 void SyntheticPinchGesture::ForwardTouchEvent(SyntheticGestureTarget* target) { |
100 target->DispatchInputEventToPlatform( | 89 target->DispatchInputEventToPlatform( |
101 InputEvent(touch_event_, ui::LatencyInfo(), false)); | 90 InputEvent(touch_event_, ui::LatencyInfo(), false)); |
102 } | 91 } |
103 | 92 |
93 void SyntheticPinchGesture::SetupCoordinates(SyntheticGestureTarget* target) { | |
94 float inner_distance_to_anchor = 2 * target->GetTouchSlopInDips(); | |
95 float outer_distance_to_anchor = inner_distance_to_anchor + | |
96 params_.total_num_pixels_covered / 2.0f + | |
97 target->GetTouchSlopInDips(); | |
bulach
2013/12/02 18:24:51
nit: "target->GetTouchSlopInDips()" is going to ja
Dominik Grewe
2013/12/02 18:50:04
Done.
| |
98 | |
99 // Move pointers away from each other to zoom in | |
100 // or towards each other to zoom out. | |
101 if (params_.zoom_in) { | |
102 current_y_0_ = params_.anchor.y() - inner_distance_to_anchor; | |
103 current_y_1_ = params_.anchor.y() + inner_distance_to_anchor; | |
104 target_y_0_ = params_.anchor.y() - outer_distance_to_anchor; | |
105 target_y_1_ = params_.anchor.y() + outer_distance_to_anchor; | |
106 } else { | |
107 current_y_0_ = params_.anchor.y() - outer_distance_to_anchor; | |
108 current_y_1_ = params_.anchor.y() + outer_distance_to_anchor; | |
109 target_y_0_ = params_.anchor.y() - inner_distance_to_anchor; | |
110 target_y_1_ = params_.anchor.y() + inner_distance_to_anchor; | |
111 } | |
112 } | |
113 | |
104 float SyntheticPinchGesture::GetDeltaForPointer0( | 114 float SyntheticPinchGesture::GetDeltaForPointer0( |
105 const base::TimeDelta& interval) const { | 115 const base::TimeDelta& interval) const { |
106 float total_abs_delta = | 116 float total_abs_delta = |
107 params_.relative_pointer_speed_in_pixels_s * interval.InSecondsF(); | 117 params_.relative_pointer_speed_in_pixels_s * interval.InSecondsF(); |
108 | 118 |
109 // Make sure we're not moving too far in the final step. | 119 // Make sure we're not moving too far in the final step. |
110 total_abs_delta = | 120 total_abs_delta = |
111 std::min(total_abs_delta, ComputeAbsoluteRemainingDistance()); | 121 std::min(total_abs_delta, ComputeAbsoluteRemainingDistance()); |
112 | 122 |
113 float abs_delta_pointer_0 = total_abs_delta / 2; | 123 float abs_delta_pointer_0 = total_abs_delta / 2; |
114 return params_.zoom_in ? -abs_delta_pointer_0 : abs_delta_pointer_0; | 124 return params_.zoom_in ? -abs_delta_pointer_0 : abs_delta_pointer_0; |
115 } | 125 } |
116 | 126 |
117 float SyntheticPinchGesture::ComputeAbsoluteRemainingDistance() const { | 127 float SyntheticPinchGesture::ComputeAbsoluteRemainingDistance() const { |
118 float distance_0 = params_.zoom_in ? (current_y_0_ - target_y_0_) | 128 float distance_0 = params_.zoom_in ? (current_y_0_ - target_y_0_) |
119 : (target_y_0_ - current_y_0_); | 129 : (target_y_0_ - current_y_0_); |
120 DCHECK_GE(distance_0, 0); | 130 DCHECK_GE(distance_0, 0); |
121 | 131 |
122 // Both pointers move the same overall distance at the same speed. | 132 // Both pointers move the same overall distance at the same speed. |
123 return 2 * distance_0; | 133 return 2 * distance_0; |
124 } | 134 } |
125 | 135 |
126 bool SyntheticPinchGesture::HasFinished() const { | 136 bool SyntheticPinchGesture::HasFinished() const { |
127 return ComputeAbsoluteRemainingDistance() == 0; | 137 return ComputeAbsoluteRemainingDistance() == 0; |
128 } | 138 } |
129 | 139 |
130 } // namespace content | 140 } // namespace content |
OLD | NEW |