1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// MSVC++ requires this to be set before any other includes to get M_PI.
#define _USE_MATH_DEFINES
#include "content/browser/renderer_host/input/motion_event_web.h"
#include <cmath>
#include "base/logging.h"
#include "content/browser/renderer_host/input/web_input_event_util.h"
#include "content/common/input/web_touch_event_traits.h"
using blink::WebInputEvent;
using blink::WebTouchEvent;
using blink::WebTouchPoint;
namespace content {
namespace {
ui::MotionEvent::Action GetActionFrom(const WebTouchEvent& event) {
DCHECK(event.touchesLength);
switch (event.type) {
case WebInputEvent::TouchStart:
if (WebTouchEventTraits::AllTouchPointsHaveState(
event, WebTouchPoint::StatePressed))
return ui::MotionEvent::ACTION_DOWN;
else
return ui::MotionEvent::ACTION_POINTER_DOWN;
case WebInputEvent::TouchEnd:
if (WebTouchEventTraits::AllTouchPointsHaveState(
event, WebTouchPoint::StateReleased))
return ui::MotionEvent::ACTION_UP;
else
return ui::MotionEvent::ACTION_POINTER_UP;
case WebInputEvent::TouchCancel:
DCHECK(WebTouchEventTraits::AllTouchPointsHaveState(
event, WebTouchPoint::StateCancelled));
return ui::MotionEvent::ACTION_CANCEL;
case WebInputEvent::TouchMove:
return ui::MotionEvent::ACTION_MOVE;
default:
break;
};
NOTREACHED()
<< "Unable to derive a valid MotionEvent::Action from the WebTouchEvent.";
return ui::MotionEvent::ACTION_CANCEL;
}
int GetActionIndexFrom(const WebTouchEvent& event) {
for (size_t i = 0; i < event.touchesLength; ++i) {
if (event.touches[i].state != WebTouchPoint::StateUndefined &&
event.touches[i].state != WebTouchPoint::StateStationary)
return i;
}
return -1;
}
} // namespace
MotionEventWeb::MotionEventWeb(const WebTouchEvent& event)
: event_(event),
cached_action_(GetActionFrom(event)),
cached_action_index_(GetActionIndexFrom(event)) {
DCHECK_GT(GetPointerCount(), 0U);
}
MotionEventWeb::~MotionEventWeb() {}
int MotionEventWeb::GetId() const {
return 0;
}
MotionEventWeb::Action MotionEventWeb::GetAction() const {
return cached_action_;
}
int MotionEventWeb::GetActionIndex() const { return cached_action_index_; }
size_t MotionEventWeb::GetPointerCount() const { return event_.touchesLength; }
int MotionEventWeb::GetPointerId(size_t pointer_index) const {
DCHECK_LT(pointer_index, GetPointerCount());
return event_.touches[pointer_index].id;
}
float MotionEventWeb::GetX(size_t pointer_index) const {
DCHECK_LT(pointer_index, GetPointerCount());
return event_.touches[pointer_index].position.x;
}
float MotionEventWeb::GetY(size_t pointer_index) const {
DCHECK_LT(pointer_index, GetPointerCount());
return event_.touches[pointer_index].position.y;
}
float MotionEventWeb::GetRawX(size_t pointer_index) const {
DCHECK_LT(pointer_index, GetPointerCount());
return event_.touches[pointer_index].screenPosition.x;
}
float MotionEventWeb::GetRawY(size_t pointer_index) const {
DCHECK_LT(pointer_index, GetPointerCount());
return event_.touches[pointer_index].screenPosition.y;
}
float MotionEventWeb::GetTouchMajor(size_t pointer_index) const {
DCHECK_LT(pointer_index, GetPointerCount());
return 2.f * std::max(event_.touches[pointer_index].radiusX,
event_.touches[pointer_index].radiusY);
}
float MotionEventWeb::GetTouchMinor(size_t pointer_index) const {
DCHECK_LT(pointer_index, GetPointerCount());
return 2.f * std::min(event_.touches[pointer_index].radiusX,
event_.touches[pointer_index].radiusY);
}
float MotionEventWeb::GetOrientation(size_t pointer_index) const {
DCHECK_LT(pointer_index, GetPointerCount());
float rotation_angle_rad = event_.touches[pointer_index].rotationAngle
* M_PI / 180.f;
DCHECK(0 <= rotation_angle_rad && rotation_angle_rad <= M_PI_2)
<< "Unexpected touch rotation angle";
if (event_.touches[pointer_index].radiusX
> event_.touches[pointer_index].radiusY) {
// The case radiusX == radiusY is omitted from here on purpose: for circles,
// we want to pass the angle (which could be any value in such cases but
// always seem to be set to zero) unchanged.
rotation_angle_rad -= (float) M_PI_2;
}
return rotation_angle_rad;
}
float MotionEventWeb::GetPressure(size_t pointer_index) const {
return 0.f;
}
base::TimeTicks MotionEventWeb::GetEventTime() const {
return base::TimeTicks() +
base::TimeDelta::FromMicroseconds(event_.timeStampSeconds *
base::Time::kMicrosecondsPerSecond);
}
ui::MotionEvent::ToolType MotionEventWeb::GetToolType(
size_t pointer_index) const {
// TODO(jdduke): Plumb tool type from the platform event, crbug.com/404128.
DCHECK_LT(pointer_index, GetPointerCount());
return TOOL_TYPE_UNKNOWN;
}
int MotionEventWeb::GetButtonState() const {
return 0;
}
int MotionEventWeb::GetFlags() const {
return WebEventModifiersToEventFlags(event_.modifiers);
}
} // namespace content
|