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

Side by Side Diff: chrome/browser/android/vr_shell/vr_input_manager.cc

Issue 2380323003: Refactorings to vr controller code. (Closed)
Patch Set: Fix BUILD.gn Created 4 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
« no previous file with comments | « chrome/browser/android/vr_shell/vr_input_manager.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "base/android/scoped_java_ref.h" 5 #include "base/android/scoped_java_ref.h"
6 #include "base/task_runner_util.h"
6 #include "chrome/browser/android/vr_shell/vr_input_manager.h" 7 #include "chrome/browser/android/vr_shell/vr_input_manager.h"
8 #include "content/public/browser/browser_thread.h"
7 9
8 using blink::WebGestureEvent; 10 using blink::WebGestureEvent;
9 using blink::WebMouseEvent; 11 using blink::WebMouseEvent;
10 using blink::WebInputEvent; 12 using blink::WebInputEvent;
11 13
12 namespace vr_shell { 14 namespace vr_shell {
13 15
14 VrInputManager::VrInputManager(content::WebContents* web_contents) 16 VrInputManager::VrInputManager(content::WebContents* web_contents)
15 : web_contents_(web_contents) { 17 : web_contents_(web_contents) {
16 dpi_scale_ = 1; 18 dpi_scale_ = 1;
17 } 19 }
18 20
19 VrInputManager::~VrInputManager() {} 21 VrInputManager::~VrInputManager() {}
20 22
23 void VrInputManager::ProcessUpdatedGesture(VrGesture gesture) {
24 if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) {
25 content::BrowserThread::PostTask(
26 content::BrowserThread::UI, FROM_HERE,
27 base::Bind(&VrInputManager::SendGesture, this, gesture));
28 } else {
29 SendGesture(gesture);
30 }
31 }
32
33 void VrInputManager::SendGesture(VrGesture gesture) {
34 int64_t event_time = gesture.start_time;
35 int64_t event_time_milliseconds = static_cast<int64_t>(event_time / 1000000);
36
37 if (gesture.type == WebInputEvent::GestureScrollBegin ||
38 gesture.type == WebInputEvent::GestureScrollUpdate ||
39 gesture.type == WebInputEvent::GestureScrollEnd) {
40 SendScrollEvent(event_time_milliseconds, 0.0f, 0.0f,
41 gesture.details.scroll.delta.x,
42 gesture.details.scroll.delta.y, gesture.type);
43 } else if (gesture.type == WebInputEvent::GestureTap) {
44 SendClickEvent(event_time_milliseconds, gesture.details.buttons.pos.x,
45 gesture.details.buttons.pos.y);
46 } else if (gesture.type == WebInputEvent::MouseMove) {
47 SendMouseMoveEvent(event_time_milliseconds, gesture.details.move.delta.x,
48 gesture.details.move.delta.y, gesture.details.move.type);
49 }
50 }
51
21 void VrInputManager::ScrollBegin(int64_t time_ms, 52 void VrInputManager::ScrollBegin(int64_t time_ms,
22 float x, 53 float x,
23 float y, 54 float y,
24 float hintx, 55 float hintx,
25 float hinty, 56 float hinty,
26 bool target_viewport) { 57 bool target_viewport) {
27 WebGestureEvent event = 58 WebGestureEvent event =
28 MakeGestureEvent(WebInputEvent::GestureScrollBegin, time_ms, x, y); 59 MakeGestureEvent(WebInputEvent::GestureScrollBegin, time_ms, x, y);
29 event.data.scrollBegin.deltaXHint = hintx / dpi_scale_; 60 event.data.scrollBegin.deltaXHint = hintx / dpi_scale_;
30 event.data.scrollBegin.deltaYHint = hinty / dpi_scale_; 61 event.data.scrollBegin.deltaYHint = hinty / dpi_scale_;
31 event.data.scrollBegin.targetViewport = target_viewport; 62 event.data.scrollBegin.targetViewport = target_viewport;
32 63
33 SendGestureEvent(event); 64 ForwardGestureEvent(event);
34 } 65 }
35 66
36 void VrInputManager::ScrollEnd(int64_t time_ms) { 67 void VrInputManager::ScrollEnd(int64_t time_ms) {
37 WebGestureEvent event = 68 WebGestureEvent event =
38 MakeGestureEvent(WebInputEvent::GestureScrollEnd, time_ms, 0, 0); 69 MakeGestureEvent(WebInputEvent::GestureScrollEnd, time_ms, 0, 0);
39 SendGestureEvent(event); 70 ForwardGestureEvent(event);
40 } 71 }
41 72
42 void VrInputManager::ScrollBy(int64_t time_ms, 73 void VrInputManager::ScrollBy(int64_t time_ms,
43 float x, 74 float x,
44 float y, 75 float y,
45 float dx, 76 float dx,
46 float dy) { 77 float dy) {
47 WebGestureEvent event = 78 WebGestureEvent event =
48 MakeGestureEvent(WebInputEvent::GestureScrollUpdate, time_ms, x, y); 79 MakeGestureEvent(WebInputEvent::GestureScrollUpdate, time_ms, x, y);
49 event.data.scrollUpdate.deltaX = -dx / dpi_scale_; 80 event.data.scrollUpdate.deltaX = -dx / dpi_scale_;
50 event.data.scrollUpdate.deltaY = -dy / dpi_scale_; 81 event.data.scrollUpdate.deltaY = -dy / dpi_scale_;
51 82
52 SendGestureEvent(event); 83 ForwardGestureEvent(event);
53 } 84 }
54 85
55 void VrInputManager::SendScrollEvent(int64_t time_ms, 86 void VrInputManager::SendScrollEvent(int64_t time_ms,
56 float x, 87 float x,
57 float y, 88 float y,
58 float dx, 89 float dx,
59 float dy, 90 float dy,
60 int type) { 91 int type) {
61 float hinty = -dy; 92 float hinty = -dy;
62 float hintx = -dx; 93 float hintx = -dx;
63 bool target_viewport = false; 94 bool target_viewport = false;
64 switch (type) { 95 switch (type) {
65 case WebInputEvent::GestureScrollBegin: { 96 case WebInputEvent::GestureScrollBegin: {
66 WebGestureEvent event_start = 97 WebGestureEvent event_start =
67 MakeGestureEvent(WebInputEvent::GestureScrollBegin, time_ms, x, y); 98 MakeGestureEvent(WebInputEvent::GestureScrollBegin, time_ms, x, y);
68 event_start.data.scrollBegin.deltaXHint = hintx / dpi_scale_; 99 event_start.data.scrollBegin.deltaXHint = hintx / dpi_scale_;
69 event_start.data.scrollBegin.deltaYHint = hinty / dpi_scale_; 100 event_start.data.scrollBegin.deltaYHint = hinty / dpi_scale_;
70 event_start.data.scrollBegin.targetViewport = target_viewport; 101 event_start.data.scrollBegin.targetViewport = target_viewport;
71 SendGestureEvent(event_start); 102 ForwardGestureEvent(event_start);
72 break; 103 break;
73 } 104 }
74 case WebInputEvent::GestureScrollUpdate: { 105 case WebInputEvent::GestureScrollUpdate: {
75 WebGestureEvent event = 106 WebGestureEvent event =
76 MakeGestureEvent(WebInputEvent::GestureScrollUpdate, time_ms, x, y); 107 MakeGestureEvent(WebInputEvent::GestureScrollUpdate, time_ms, x, y);
77 event.data.scrollUpdate.deltaX = -dx / dpi_scale_; 108 event.data.scrollUpdate.deltaX = -dx / dpi_scale_;
78 event.data.scrollUpdate.deltaY = -dy / dpi_scale_; 109 event.data.scrollUpdate.deltaY = -dy / dpi_scale_;
79 SendGestureEvent(event); 110 ForwardGestureEvent(event);
80 break; 111 break;
81 } 112 }
82 case WebInputEvent::GestureScrollEnd: { 113 case WebInputEvent::GestureScrollEnd: {
83 WebGestureEvent event_end = 114 WebGestureEvent event_end =
84 MakeGestureEvent(WebInputEvent::GestureScrollEnd, time_ms, 0, 0); 115 MakeGestureEvent(WebInputEvent::GestureScrollEnd, time_ms, 0, 0);
85 SendGestureEvent(event_end); 116 ForwardGestureEvent(event_end);
86 break; 117 break;
87 } 118 }
88 } 119 }
89 } 120 }
90 121
91 void VrInputManager::SendMouseMoveEvent(int64_t time_ms, 122 void VrInputManager::SendMouseMoveEvent(int64_t time_ms,
92 float x, 123 float x,
93 float y, 124 float y,
94 int type) { 125 int type) {
95 WebMouseEvent result; 126 WebMouseEvent result;
96 127
97 result.type = WebInputEvent::MouseMove; 128 result.type = WebInputEvent::MouseMove;
98 result.pointerType = blink::WebPointerProperties::PointerType::Mouse; 129 result.pointerType = blink::WebPointerProperties::PointerType::Mouse;
99 result.x = x / dpi_scale_; 130 result.x = x / dpi_scale_;
100 result.y = y / dpi_scale_; 131 result.y = y / dpi_scale_;
101 result.windowX = x / dpi_scale_; 132 result.windowX = x / dpi_scale_;
102 result.windowY = y / dpi_scale_; 133 result.windowY = y / dpi_scale_;
103 result.timeStampSeconds = time_ms / 1000.0; 134 result.timeStampSeconds = time_ms / 1000.0;
104 result.clickCount = 1; 135 result.clickCount = 1;
105 result.modifiers = 0; 136 result.modifiers = 0;
106 137
107 if (type == 1) { 138 if (type == 1) {
108 result.type = WebInputEvent::MouseEnter; 139 result.type = WebInputEvent::MouseEnter;
109 } else if (type == 2) { 140 } else if (type == 2) {
110 result.type = WebInputEvent::MouseLeave; 141 result.type = WebInputEvent::MouseLeave;
111 } 142 }
112 result.button = WebMouseEvent::Button::NoButton; 143 result.button = WebMouseEvent::Button::NoButton;
113 144
114 SendMouseEvent(result); 145 ForwardMouseEvent(result);
115 } 146 }
116 147
117 void VrInputManager::SendClickEvent(int64_t time_ms, float x, float y) { 148 void VrInputManager::SendClickEvent(int64_t time_ms, float x, float y) {
118 WebGestureEvent tap_down_event = 149 WebGestureEvent tap_down_event =
119 MakeGestureEvent(WebInputEvent::GestureTapDown, time_ms, x, y); 150 MakeGestureEvent(WebInputEvent::GestureTapDown, time_ms, x, y);
120 tap_down_event.data.tap.tapCount = 1; 151 tap_down_event.data.tap.tapCount = 1;
121 SendGestureEvent(tap_down_event); 152 ForwardGestureEvent(tap_down_event);
122 153
123 WebGestureEvent tap_event = 154 WebGestureEvent tap_event =
124 MakeGestureEvent(WebInputEvent::GestureTap, time_ms, x, y); 155 MakeGestureEvent(WebInputEvent::GestureTap, time_ms, x, y);
125 tap_event.data.tap.tapCount = 1; 156 tap_event.data.tap.tapCount = 1;
126 SendGestureEvent(tap_event); 157 ForwardGestureEvent(tap_event);
127 } 158 }
128 159
129 void VrInputManager::PinchBegin(int64_t time_ms, float x, float y) { 160 void VrInputManager::PinchBegin(int64_t time_ms, float x, float y) {
130 WebGestureEvent event = 161 WebGestureEvent event =
131 MakeGestureEvent(WebInputEvent::GesturePinchBegin, time_ms, x, y); 162 MakeGestureEvent(WebInputEvent::GesturePinchBegin, time_ms, x, y);
132 SendGestureEvent(event); 163 ForwardGestureEvent(event);
133 } 164 }
134 165
135 void VrInputManager::PinchEnd(int64_t time_ms) { 166 void VrInputManager::PinchEnd(int64_t time_ms) {
136 WebGestureEvent event = 167 WebGestureEvent event =
137 MakeGestureEvent(WebInputEvent::GesturePinchEnd, time_ms, 0, 0); 168 MakeGestureEvent(WebInputEvent::GesturePinchEnd, time_ms, 0, 0);
138 SendGestureEvent(event); 169 ForwardGestureEvent(event);
139 } 170 }
140 171
141 void VrInputManager::PinchBy(int64_t time_ms, 172 void VrInputManager::PinchBy(int64_t time_ms,
142 float anchor_x, 173 float anchor_x,
143 float anchor_y, 174 float anchor_y,
144 float delta) { 175 float delta) {
145 WebGestureEvent event = MakeGestureEvent(WebInputEvent::GesturePinchUpdate, 176 WebGestureEvent event = MakeGestureEvent(WebInputEvent::GesturePinchUpdate,
146 time_ms, anchor_x, anchor_y); 177 time_ms, anchor_x, anchor_y);
147 event.data.pinchUpdate.scale = delta; 178 event.data.pinchUpdate.scale = delta;
148 179
149 SendGestureEvent(event); 180 ForwardGestureEvent(event);
150 } 181 }
151 182
152 void VrInputManager::SendPinchEvent(int64_t time_ms, 183 void VrInputManager::SendPinchEvent(int64_t time_ms,
153 float x, 184 float x,
154 float y, 185 float y,
155 float dz, 186 float dz,
156 int type) { 187 int type) {
157 switch (type) { 188 switch (type) {
158 case WebInputEvent::GesturePinchBegin: { 189 case WebInputEvent::GesturePinchBegin: {
159 WebGestureEvent event_start = 190 WebGestureEvent event_start =
160 MakeGestureEvent(WebInputEvent::GesturePinchBegin, time_ms, x, y); 191 MakeGestureEvent(WebInputEvent::GesturePinchBegin, time_ms, x, y);
161 SendGestureEvent(event_start); 192 ForwardGestureEvent(event_start);
162 break; 193 break;
163 } 194 }
164 case WebInputEvent::GesturePinchUpdate: { 195 case WebInputEvent::GesturePinchUpdate: {
165 WebGestureEvent event = 196 WebGestureEvent event =
166 MakeGestureEvent(WebInputEvent::GesturePinchUpdate, time_ms, x, y); 197 MakeGestureEvent(WebInputEvent::GesturePinchUpdate, time_ms, x, y);
167 event.data.pinchUpdate.scale = dz; 198 event.data.pinchUpdate.scale = dz;
168 199
169 SendGestureEvent(event); 200 ForwardGestureEvent(event);
170 break; 201 break;
171 } 202 }
172 case WebInputEvent::GesturePinchEnd: { 203 case WebInputEvent::GesturePinchEnd: {
173 WebGestureEvent event_end = 204 WebGestureEvent event_end =
174 MakeGestureEvent(WebInputEvent::GesturePinchEnd, time_ms, 0, 0); 205 MakeGestureEvent(WebInputEvent::GesturePinchEnd, time_ms, 0, 0);
175 SendGestureEvent(event_end); 206 ForwardGestureEvent(event_end);
176 break; 207 break;
177 } 208 }
178 } 209 }
179 } 210 }
180 211
181 WebGestureEvent VrInputManager::MakeGestureEvent(WebInputEvent::Type type, 212 WebGestureEvent VrInputManager::MakeGestureEvent(WebInputEvent::Type type,
182 int64_t time_ms, 213 int64_t time_ms,
183 float x, 214 float x,
184 float y) const { 215 float y) const {
185 WebGestureEvent result; 216 WebGestureEvent result;
186 217
187 result.type = type; 218 result.type = type;
188 result.x = x / dpi_scale_; 219 result.x = x / dpi_scale_;
189 result.y = y / dpi_scale_; 220 result.y = y / dpi_scale_;
190 result.timeStampSeconds = time_ms / 1000.0; 221 result.timeStampSeconds = time_ms / 1000.0;
191 result.sourceDevice = blink::WebGestureDeviceTouchscreen; 222 result.sourceDevice = blink::WebGestureDeviceTouchscreen;
192 223
193 return result; 224 return result;
194 } 225 }
195 226
196 void VrInputManager::SendGestureEvent(const blink::WebGestureEvent& event) { 227 void VrInputManager::ForwardGestureEvent(const blink::WebGestureEvent& event) {
197 content::RenderWidgetHost* rwh = 228 content::RenderWidgetHost* rwh =
198 web_contents_->GetRenderWidgetHostView()->GetRenderWidgetHost(); 229 web_contents_->GetRenderWidgetHostView()->GetRenderWidgetHost();
199 if (rwh) 230 if (rwh)
200 rwh->ForwardGestureEvent(event); 231 rwh->ForwardGestureEvent(event);
201 } 232 }
202 233
203 void VrInputManager::SendMouseEvent(const blink::WebMouseEvent& event) { 234 void VrInputManager::ForwardMouseEvent(const blink::WebMouseEvent& event) {
204 content::RenderWidgetHost* rwh = 235 content::RenderWidgetHost* rwh =
205 web_contents_->GetRenderWidgetHostView()->GetRenderWidgetHost(); 236 web_contents_->GetRenderWidgetHostView()->GetRenderWidgetHost();
206 if (rwh) 237 if (rwh)
207 rwh->ForwardMouseEvent(event); 238 rwh->ForwardMouseEvent(event);
208 } 239 }
209 240
210 } // namespace vr_shell 241 } // namespace vr_shell
OLDNEW
« no previous file with comments | « chrome/browser/android/vr_shell/vr_input_manager.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698