OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "content/browser/renderer_host/input/motion_event_web.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 |
| 9 using blink::WebInputEvent; |
| 10 using blink::WebPoint; |
| 11 using blink::WebTouchEvent; |
| 12 using blink::WebTouchPoint; |
| 13 |
| 14 namespace content { |
| 15 namespace { |
| 16 |
| 17 bool AllTouchPointsHaveState(const WebTouchEvent& event, |
| 18 WebTouchPoint::State state) { |
| 19 for (size_t i = 0; i < event.touchesLength; ++i) { |
| 20 if (event.touches[i].state != state) |
| 21 return false; |
| 22 } |
| 23 return true; |
| 24 } |
| 25 |
| 26 ui::MotionEvent::Action GetActionFrom(const WebTouchEvent& event) { |
| 27 // TODO(jdduke): Use WebTouchEventTraits. |
| 28 DCHECK(event.touchesLength); |
| 29 switch (event.type) { |
| 30 case WebInputEvent::TouchStart: |
| 31 if (AllTouchPointsHaveState(event, WebTouchPoint::StatePressed)) |
| 32 return ui::MotionEvent::ACTION_DOWN; |
| 33 else |
| 34 return ui::MotionEvent::ACTION_POINTER_DOWN; |
| 35 case WebInputEvent::TouchEnd: |
| 36 if (AllTouchPointsHaveState(event, WebTouchPoint::StateReleased)) |
| 37 return ui::MotionEvent::ACTION_UP; |
| 38 else |
| 39 return ui::MotionEvent::ACTION_POINTER_UP; |
| 40 case WebInputEvent::TouchCancel: |
| 41 DCHECK(AllTouchPointsHaveState(event, WebTouchPoint::StateCancelled)); |
| 42 return ui::MotionEvent::ACTION_CANCEL; |
| 43 case WebInputEvent::TouchMove: |
| 44 return ui::MotionEvent::ACTION_MOVE; |
| 45 default: |
| 46 break; |
| 47 }; |
| 48 NOTREACHED() |
| 49 << "Unable to derive a valid MotionEvent::Action from the WebTouchEvent."; |
| 50 return ui::MotionEvent::ACTION_CANCEL; |
| 51 } |
| 52 |
| 53 int GetActionIndexFrom(const WebTouchEvent& event) { |
| 54 for (size_t i = 0; i < event.touchesLength; ++i) { |
| 55 if (event.touches[i].state != WebTouchPoint::StateUndefined && |
| 56 event.touches[i].state != WebTouchPoint::StateStationary) |
| 57 return i; |
| 58 } |
| 59 return -1; |
| 60 } |
| 61 |
| 62 } // namespace |
| 63 |
| 64 MotionEventWeb::MotionEventWeb(const WebTouchEvent& event) |
| 65 : event_(event), |
| 66 cached_action_(GetActionFrom(event)), |
| 67 cached_action_index_(GetActionIndexFrom(event)) { |
| 68 DCHECK_GT(GetPointerCount(), 0U); |
| 69 } |
| 70 |
| 71 MotionEventWeb::~MotionEventWeb() {} |
| 72 |
| 73 MotionEventWeb::Action MotionEventWeb::GetAction() const { |
| 74 return cached_action_; |
| 75 } |
| 76 |
| 77 int MotionEventWeb::GetActionIndex() const { return cached_action_index_; } |
| 78 |
| 79 size_t MotionEventWeb::GetPointerCount() const { return event_.touchesLength; } |
| 80 |
| 81 int MotionEventWeb::GetPointerId(size_t pointer_index) const { |
| 82 DCHECK_LT(pointer_index, GetPointerCount()); |
| 83 return event_.touches[pointer_index].id; |
| 84 } |
| 85 |
| 86 float MotionEventWeb::GetX(size_t pointer_index) const { |
| 87 DCHECK_LT(pointer_index, GetPointerCount()); |
| 88 return event_.touches[pointer_index].position.x; |
| 89 } |
| 90 |
| 91 float MotionEventWeb::GetY(size_t pointer_index) const { |
| 92 DCHECK_LT(pointer_index, GetPointerCount()); |
| 93 return event_.touches[pointer_index].position.y; |
| 94 } |
| 95 |
| 96 float MotionEventWeb::GetTouchMajor(size_t pointer_index) const { |
| 97 DCHECK_LT(pointer_index, GetPointerCount()); |
| 98 // TODO(jdduke): We should be a bit more careful about axes here. |
| 99 return 2.f * std::max(event_.touches[pointer_index].radiusX, |
| 100 event_.touches[pointer_index].radiusY); |
| 101 } |
| 102 |
| 103 base::TimeTicks MotionEventWeb::GetEventTime() const { |
| 104 return base::TimeTicks() + |
| 105 base::TimeDelta::FromMicroseconds(event_.timeStampSeconds * |
| 106 base::Time::kMicrosecondsPerSecond); |
| 107 } |
| 108 |
| 109 size_t MotionEventWeb::GetHistorySize() const { return 0; } |
| 110 |
| 111 base::TimeTicks MotionEventWeb::GetHistoricalEventTime( |
| 112 size_t historical_index) const { |
| 113 NOTIMPLEMENTED(); |
| 114 return base::TimeTicks(); |
| 115 } |
| 116 |
| 117 float MotionEventWeb::GetHistoricalTouchMajor(size_t pointer_index, |
| 118 size_t historical_index) const { |
| 119 NOTIMPLEMENTED(); |
| 120 return 0.f; |
| 121 } |
| 122 |
| 123 float MotionEventWeb::GetHistoricalX(size_t pointer_index, |
| 124 size_t historical_index) const { |
| 125 NOTIMPLEMENTED(); |
| 126 return 0.f; |
| 127 } |
| 128 |
| 129 float MotionEventWeb::GetHistoricalY(size_t pointer_index, |
| 130 size_t historical_index) const { |
| 131 NOTIMPLEMENTED(); |
| 132 return 0.f; |
| 133 } |
| 134 |
| 135 scoped_ptr<ui::MotionEvent> MotionEventWeb::Clone() const { |
| 136 return scoped_ptr<MotionEvent>(new MotionEventWeb(event_)); |
| 137 } |
| 138 |
| 139 scoped_ptr<ui::MotionEvent> MotionEventWeb::Cancel() const { |
| 140 WebTouchEvent cancel_event(event_); |
| 141 |
| 142 cancel_event.type = WebInputEvent::TouchCancel; |
| 143 for (size_t i = 0; i < cancel_event.touchesLength; ++i) |
| 144 cancel_event.touches[i].state = WebTouchPoint::StateCancelled; |
| 145 |
| 146 return scoped_ptr<MotionEvent>(new MotionEventWeb(cancel_event)); |
| 147 } |
| 148 |
| 149 } // namespace content |
OLD | NEW |