OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/android/scoped_java_ref.h" |
| 6 #include "chrome/browser/android/vr_shell/vr_input_manager.h" |
| 7 |
| 8 using blink::WebGestureEvent; |
| 9 using blink::WebMouseEvent; |
| 10 using blink::WebInputEvent; |
| 11 |
| 12 namespace vr_shell { |
| 13 |
| 14 VrInputManager::VrInputManager(content::WebContents* web_contents) |
| 15 : web_contents_(web_contents) { |
| 16 dpi_scale_ = 1; |
| 17 } |
| 18 |
| 19 VrInputManager::~VrInputManager() {} |
| 20 |
| 21 void VrInputManager::ScrollBegin(int64_t time_ms, |
| 22 float x, |
| 23 float y, |
| 24 float hintx, |
| 25 float hinty, |
| 26 bool target_viewport) { |
| 27 WebGestureEvent event = |
| 28 MakeGestureEvent(WebInputEvent::GestureScrollBegin, time_ms, x, y); |
| 29 event.data.scrollBegin.deltaXHint = hintx / dpi_scale_; |
| 30 event.data.scrollBegin.deltaYHint = hinty / dpi_scale_; |
| 31 event.data.scrollBegin.targetViewport = target_viewport; |
| 32 |
| 33 SendGestureEvent(event); |
| 34 } |
| 35 |
| 36 void VrInputManager::ScrollEnd(int64_t time_ms) { |
| 37 WebGestureEvent event = |
| 38 MakeGestureEvent(WebInputEvent::GestureScrollEnd, time_ms, 0, 0); |
| 39 SendGestureEvent(event); |
| 40 } |
| 41 |
| 42 void VrInputManager::ScrollBy(int64_t time_ms, |
| 43 float x, |
| 44 float y, |
| 45 float dx, |
| 46 float dy) { |
| 47 WebGestureEvent event = |
| 48 MakeGestureEvent(WebInputEvent::GestureScrollUpdate, time_ms, x, y); |
| 49 event.data.scrollUpdate.deltaX = -dx / dpi_scale_; |
| 50 event.data.scrollUpdate.deltaY = -dy / dpi_scale_; |
| 51 |
| 52 SendGestureEvent(event); |
| 53 } |
| 54 |
| 55 void VrInputManager::SendScrollEvent(int64_t time_ms, |
| 56 float x, |
| 57 float y, |
| 58 float dx, |
| 59 float dy, |
| 60 int type) { |
| 61 float hinty = -dy; |
| 62 float hintx = -dx; |
| 63 bool target_viewport = false; |
| 64 switch (type) { |
| 65 case WebInputEvent::GestureScrollBegin: { |
| 66 WebGestureEvent event_start = |
| 67 MakeGestureEvent(WebInputEvent::GestureScrollBegin, time_ms, x, y); |
| 68 event_start.data.scrollBegin.deltaXHint = hintx / dpi_scale_; |
| 69 event_start.data.scrollBegin.deltaYHint = hinty / dpi_scale_; |
| 70 event_start.data.scrollBegin.targetViewport = target_viewport; |
| 71 SendGestureEvent(event_start); |
| 72 break; |
| 73 } |
| 74 case WebInputEvent::GestureScrollUpdate: { |
| 75 WebGestureEvent event = |
| 76 MakeGestureEvent(WebInputEvent::GestureScrollUpdate, time_ms, x, y); |
| 77 event.data.scrollUpdate.deltaX = -dx / dpi_scale_; |
| 78 event.data.scrollUpdate.deltaY = -dy / dpi_scale_; |
| 79 SendGestureEvent(event); |
| 80 break; |
| 81 } |
| 82 case WebInputEvent::GestureScrollEnd: { |
| 83 WebGestureEvent event_end = |
| 84 MakeGestureEvent(WebInputEvent::GestureScrollEnd, time_ms, 0, 0); |
| 85 SendGestureEvent(event_end); |
| 86 break; |
| 87 } |
| 88 } |
| 89 } |
| 90 |
| 91 void VrInputManager::SendMouseMoveEvent(int64_t time_ms, |
| 92 float x, |
| 93 float y, |
| 94 int type) { |
| 95 WebMouseEvent result; |
| 96 |
| 97 result.type = WebInputEvent::MouseMove; |
| 98 result.pointerType = blink::WebPointerProperties::PointerType::Mouse; |
| 99 result.x = x / dpi_scale_; |
| 100 result.y = y / dpi_scale_; |
| 101 result.windowX = x / dpi_scale_; |
| 102 result.windowY = y / dpi_scale_; |
| 103 result.timeStampSeconds = time_ms / 1000.0; |
| 104 result.clickCount = 1; |
| 105 result.modifiers = 0; |
| 106 |
| 107 if (type == 1) { |
| 108 result.type = WebInputEvent::MouseEnter; |
| 109 } else if (type == 2) { |
| 110 result.type = WebInputEvent::MouseLeave; |
| 111 } |
| 112 result.button = WebMouseEvent::Button::NoButton; |
| 113 |
| 114 SendMouseEvent(result); |
| 115 } |
| 116 |
| 117 void VrInputManager::SendClickEvent(int64_t time_ms, float x, float y) { |
| 118 WebGestureEvent tap_down_event = |
| 119 MakeGestureEvent(WebInputEvent::GestureTapDown, time_ms, x, y); |
| 120 tap_down_event.data.tap.tapCount = 1; |
| 121 SendGestureEvent(tap_down_event); |
| 122 |
| 123 WebGestureEvent tap_event = |
| 124 MakeGestureEvent(WebInputEvent::GestureTap, time_ms, x, y); |
| 125 tap_event.data.tap.tapCount = 1; |
| 126 SendGestureEvent(tap_event); |
| 127 } |
| 128 |
| 129 void VrInputManager::PinchBegin(int64_t time_ms, float x, float y) { |
| 130 WebGestureEvent event = |
| 131 MakeGestureEvent(WebInputEvent::GesturePinchBegin, time_ms, x, y); |
| 132 SendGestureEvent(event); |
| 133 } |
| 134 |
| 135 void VrInputManager::PinchEnd(int64_t time_ms) { |
| 136 WebGestureEvent event = |
| 137 MakeGestureEvent(WebInputEvent::GesturePinchEnd, time_ms, 0, 0); |
| 138 SendGestureEvent(event); |
| 139 } |
| 140 |
| 141 void VrInputManager::PinchBy(int64_t time_ms, |
| 142 float anchor_x, |
| 143 float anchor_y, |
| 144 float delta) { |
| 145 WebGestureEvent event = MakeGestureEvent(WebInputEvent::GesturePinchUpdate, |
| 146 time_ms, anchor_x, anchor_y); |
| 147 event.data.pinchUpdate.scale = delta; |
| 148 |
| 149 SendGestureEvent(event); |
| 150 } |
| 151 |
| 152 void VrInputManager::SendPinchEvent(int64_t time_ms, |
| 153 float x, |
| 154 float y, |
| 155 float dz, |
| 156 int type) { |
| 157 switch (type) { |
| 158 case WebInputEvent::GesturePinchBegin: { |
| 159 WebGestureEvent event_start = |
| 160 MakeGestureEvent(WebInputEvent::GesturePinchBegin, time_ms, x, y); |
| 161 SendGestureEvent(event_start); |
| 162 break; |
| 163 } |
| 164 case WebInputEvent::GesturePinchUpdate: { |
| 165 WebGestureEvent event = |
| 166 MakeGestureEvent(WebInputEvent::GesturePinchUpdate, time_ms, x, y); |
| 167 event.data.pinchUpdate.scale = dz; |
| 168 |
| 169 SendGestureEvent(event); |
| 170 break; |
| 171 } |
| 172 case WebInputEvent::GesturePinchEnd: { |
| 173 WebGestureEvent event_end = |
| 174 MakeGestureEvent(WebInputEvent::GesturePinchEnd, time_ms, 0, 0); |
| 175 SendGestureEvent(event_end); |
| 176 break; |
| 177 } |
| 178 } |
| 179 } |
| 180 |
| 181 WebGestureEvent VrInputManager::MakeGestureEvent(WebInputEvent::Type type, |
| 182 int64_t time_ms, |
| 183 float x, |
| 184 float y) const { |
| 185 WebGestureEvent result; |
| 186 |
| 187 result.type = type; |
| 188 result.x = x / dpi_scale_; |
| 189 result.y = y / dpi_scale_; |
| 190 result.timeStampSeconds = time_ms / 1000.0; |
| 191 result.sourceDevice = blink::WebGestureDeviceTouchscreen; |
| 192 |
| 193 return result; |
| 194 } |
| 195 |
| 196 void VrInputManager::SendGestureEvent(const blink::WebGestureEvent& event) { |
| 197 content::RenderWidgetHost* rwh = |
| 198 web_contents_->GetRenderWidgetHostView()->GetRenderWidgetHost(); |
| 199 if (rwh) |
| 200 rwh->ForwardGestureEvent(event); |
| 201 } |
| 202 |
| 203 void VrInputManager::SendMouseEvent(const blink::WebMouseEvent& event) { |
| 204 content::RenderWidgetHost* rwh = |
| 205 web_contents_->GetRenderWidgetHostView()->GetRenderWidgetHost(); |
| 206 if (rwh) |
| 207 rwh->ForwardMouseEvent(event); |
| 208 } |
| 209 |
| 210 } // namespace vr_shell |
OLD | NEW |