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 #include "content/browser/renderer_host/input/motion_event_web.h"
7 #include "base/logging.h"
8 #include "content/common/input/web_touch_event_traits.h"
10 using blink::WebInputEvent
;
11 using blink::WebTouchEvent
;
12 using blink::WebTouchPoint
;
17 ui::MotionEvent::Action
GetActionFrom(const WebTouchEvent
& event
) {
18 DCHECK(event
.touchesLength
);
20 case WebInputEvent::TouchStart
:
21 if (WebTouchEventTraits::AllTouchPointsHaveState(
22 event
, WebTouchPoint::StatePressed
))
23 return ui::MotionEvent::ACTION_DOWN
;
25 return ui::MotionEvent::ACTION_POINTER_DOWN
;
26 case WebInputEvent::TouchEnd
:
27 if (WebTouchEventTraits::AllTouchPointsHaveState(
28 event
, WebTouchPoint::StateReleased
))
29 return ui::MotionEvent::ACTION_UP
;
31 return ui::MotionEvent::ACTION_POINTER_UP
;
32 case WebInputEvent::TouchCancel
:
33 DCHECK(WebTouchEventTraits::AllTouchPointsHaveState(
34 event
, WebTouchPoint::StateCancelled
));
35 return ui::MotionEvent::ACTION_CANCEL
;
36 case WebInputEvent::TouchMove
:
37 return ui::MotionEvent::ACTION_MOVE
;
42 << "Unable to derive a valid MotionEvent::Action from the WebTouchEvent.";
43 return ui::MotionEvent::ACTION_CANCEL
;
46 int GetActionIndexFrom(const WebTouchEvent
& event
) {
47 for (size_t i
= 0; i
< event
.touchesLength
; ++i
) {
48 if (event
.touches
[i
].state
!= WebTouchPoint::StateUndefined
&&
49 event
.touches
[i
].state
!= WebTouchPoint::StateStationary
)
57 MotionEventWeb::MotionEventWeb(const WebTouchEvent
& event
)
59 cached_action_(GetActionFrom(event
)),
60 cached_action_index_(GetActionIndexFrom(event
)) {
61 DCHECK_GT(GetPointerCount(), 0U);
64 MotionEventWeb::~MotionEventWeb() {}
66 int MotionEventWeb::GetId() const {
70 MotionEventWeb::Action
MotionEventWeb::GetAction() const {
71 return cached_action_
;
74 int MotionEventWeb::GetActionIndex() const { return cached_action_index_
; }
76 size_t MotionEventWeb::GetPointerCount() const { return event_
.touchesLength
; }
78 int MotionEventWeb::GetPointerId(size_t pointer_index
) const {
79 DCHECK_LT(pointer_index
, GetPointerCount());
80 return event_
.touches
[pointer_index
].id
;
83 float MotionEventWeb::GetX(size_t pointer_index
) const {
84 DCHECK_LT(pointer_index
, GetPointerCount());
85 return event_
.touches
[pointer_index
].position
.x
;
88 float MotionEventWeb::GetY(size_t pointer_index
) const {
89 DCHECK_LT(pointer_index
, GetPointerCount());
90 return event_
.touches
[pointer_index
].position
.y
;
93 float MotionEventWeb::GetTouchMajor(size_t pointer_index
) const {
94 DCHECK_LT(pointer_index
, GetPointerCount());
95 // TODO(jdduke): We should be a bit more careful about axes here.
96 return 2.f
* std::max(event_
.touches
[pointer_index
].radiusX
,
97 event_
.touches
[pointer_index
].radiusY
);
100 float MotionEventWeb::GetPressure(size_t pointer_index
) const {
104 base::TimeTicks
MotionEventWeb::GetEventTime() const {
105 return base::TimeTicks() +
106 base::TimeDelta::FromMicroseconds(event_
.timeStampSeconds
*
107 base::Time::kMicrosecondsPerSecond
);
110 size_t MotionEventWeb::GetHistorySize() const { return 0; }
112 base::TimeTicks
MotionEventWeb::GetHistoricalEventTime(
113 size_t historical_index
) const {
115 return base::TimeTicks();
118 float MotionEventWeb::GetHistoricalTouchMajor(size_t pointer_index
,
119 size_t historical_index
) const {
124 float MotionEventWeb::GetHistoricalX(size_t pointer_index
,
125 size_t historical_index
) const {
130 float MotionEventWeb::GetHistoricalY(size_t pointer_index
,
131 size_t historical_index
) const {
136 scoped_ptr
<ui::MotionEvent
> MotionEventWeb::Clone() const {
137 return scoped_ptr
<MotionEvent
>(new MotionEventWeb(event_
));
140 scoped_ptr
<ui::MotionEvent
> MotionEventWeb::Cancel() const {
141 WebTouchEvent
cancel_event(event_
);
142 WebTouchEventTraits::ResetTypeAndTouchStates(
143 blink::WebInputEvent::TouchCancel
,
144 // TODO(rbyers): Shouldn't we use a fresh timestamp?
145 event_
.timeStampSeconds
,
147 return scoped_ptr
<MotionEvent
>(new MotionEventWeb(cancel_event
));
150 } // namespace content