Chromium Code Reviews| 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() << "Invalid masked TouchEvent action."; | |
|
tdresser
2014/02/27 15:28:30
What does the word "masked" mean here?
jdduke (slow)
2014/02/27 17:40:29
Oops, relic of the |GetActionMasked()| call that u
| |
| 49 return ui::MotionEvent::ACTION_CANCEL; | |
| 50 } | |
| 51 | |
| 52 int GetActionIndexFrom(const WebTouchEvent& event) { | |
| 53 for (size_t i = 0; i < event.touchesLength; ++i) { | |
| 54 if (event.touches[i].state != WebTouchPoint::StateUndefined && | |
| 55 event.touches[i].state != WebTouchPoint::StateStationary) | |
| 56 return i; | |
| 57 } | |
| 58 return -1; | |
| 59 } | |
| 60 | |
| 61 } // namespace | |
| 62 | |
| 63 MotionEventWeb::MotionEventWeb(const WebTouchEvent& event) | |
| 64 : event_(event), | |
| 65 cached_action_(GetActionFrom(event)), | |
| 66 cached_action_index_(GetActionIndexFrom(event)) { | |
| 67 DCHECK_GT(GetPointerCount(), 0U); | |
| 68 } | |
| 69 | |
| 70 MotionEventWeb::~MotionEventWeb() {} | |
| 71 | |
| 72 MotionEventWeb::Action MotionEventWeb::GetAction() const { | |
| 73 return cached_action_; | |
| 74 } | |
| 75 | |
| 76 int MotionEventWeb::GetActionIndex() const { return cached_action_index_; } | |
| 77 | |
| 78 size_t MotionEventWeb::GetPointerCount() const { return event_.touchesLength; } | |
| 79 | |
| 80 int MotionEventWeb::GetPointerId(size_t pointer_index) const { | |
| 81 DCHECK_LT(pointer_index, GetPointerCount()); | |
| 82 return event_.touches[pointer_index].id; | |
| 83 } | |
| 84 | |
| 85 float MotionEventWeb::GetX(size_t pointer_index) const { | |
| 86 DCHECK_LT(pointer_index, GetPointerCount()); | |
| 87 return event_.touches[pointer_index].position.x; | |
| 88 } | |
| 89 | |
| 90 float MotionEventWeb::GetY(size_t pointer_index) const { | |
| 91 DCHECK_LT(pointer_index, GetPointerCount()); | |
| 92 return event_.touches[pointer_index].position.y; | |
| 93 } | |
| 94 | |
| 95 float MotionEventWeb::GetTouchMajor(size_t pointer_index) const { | |
| 96 DCHECK_LT(pointer_index, GetPointerCount()); | |
| 97 // TODO(jdduke): We should be a bit more careful about axes here. | |
| 98 return 2.f * std::max(event_.touches[pointer_index].radiusX, | |
| 99 event_.touches[pointer_index].radiusY); | |
| 100 } | |
| 101 | |
| 102 base::TimeTicks MotionEventWeb::GetEventTime() const { | |
| 103 return base::TimeTicks() + | |
| 104 base::TimeDelta::FromMicroseconds(event_.timeStampSeconds * | |
| 105 base::Time::kMicrosecondsPerSecond); | |
| 106 } | |
| 107 | |
| 108 size_t MotionEventWeb::GetHistorySize() const { return 0; } | |
| 109 | |
| 110 base::TimeTicks MotionEventWeb::GetHistoricalEventTime( | |
| 111 size_t historical_index) const { | |
| 112 NOTIMPLEMENTED(); | |
| 113 return base::TimeTicks(); | |
| 114 } | |
| 115 | |
| 116 float MotionEventWeb::GetHistoricalTouchMajor(size_t pointer_index, | |
| 117 size_t historical_index) const { | |
| 118 NOTIMPLEMENTED(); | |
| 119 return 0.f; | |
| 120 } | |
| 121 | |
| 122 float MotionEventWeb::GetHistoricalX(size_t pointer_index, | |
| 123 size_t historical_index) const { | |
| 124 NOTIMPLEMENTED(); | |
| 125 return 0.f; | |
| 126 } | |
| 127 | |
| 128 float MotionEventWeb::GetHistoricalY(size_t pointer_index, | |
| 129 size_t historical_index) const { | |
| 130 NOTIMPLEMENTED(); | |
| 131 return 0.f; | |
| 132 } | |
| 133 | |
| 134 scoped_ptr<ui::MotionEvent> MotionEventWeb::Clone() const { | |
| 135 return scoped_ptr<MotionEvent>(new MotionEventWeb(event_)); | |
| 136 } | |
| 137 | |
| 138 scoped_ptr<ui::MotionEvent> MotionEventWeb::Cancel() const { | |
| 139 WebTouchEvent cancel_event(event_); | |
| 140 | |
| 141 cancel_event.type = WebInputEvent::TouchCancel; | |
| 142 for (size_t i = 0; i < cancel_event.touchesLength; ++i) | |
| 143 cancel_event.touches[i].state = WebTouchPoint::StateCancelled; | |
| 144 | |
| 145 return scoped_ptr<MotionEvent>(new MotionEventWeb(cancel_event)); | |
| 146 } | |
| 147 | |
| 148 // StackAllocatedMotionEventWeb | |
|
tdresser
2014/02/27 15:28:30
This comment feels out of date.
| |
| 149 | |
| 150 } // namespace content | |
| OLD | NEW |