root/content/browser/renderer_host/input/motion_event_web.cc

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. AllTouchPointsHaveState
  2. GetActionFrom
  3. GetActionIndexFrom
  4. cached_action_index_
  5. GetId
  6. GetAction
  7. GetActionIndex
  8. GetPointerCount
  9. GetPointerId
  10. GetX
  11. GetY
  12. GetTouchMajor
  13. GetPressure
  14. GetEventTime
  15. GetHistorySize
  16. GetHistoricalEventTime
  17. GetHistoricalTouchMajor
  18. GetHistoricalX
  19. GetHistoricalY
  20. Clone
  21. Cancel

// 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.

#include "content/browser/renderer_host/input/motion_event_web.h"

#include "base/logging.h"

using blink::WebInputEvent;
using blink::WebTouchEvent;
using blink::WebTouchPoint;

namespace content {
namespace {

bool AllTouchPointsHaveState(const WebTouchEvent& event,
                             WebTouchPoint::State state) {
  for (size_t i = 0; i < event.touchesLength; ++i) {
    if (event.touches[i].state != state)
      return false;
  }
  return true;
}

ui::MotionEvent::Action GetActionFrom(const WebTouchEvent& event) {
  // TODO(jdduke): Use WebTouchEventTraits.
  DCHECK(event.touchesLength);
  switch (event.type) {
    case WebInputEvent::TouchStart:
      if (AllTouchPointsHaveState(event, WebTouchPoint::StatePressed))
        return ui::MotionEvent::ACTION_DOWN;
      else
        return ui::MotionEvent::ACTION_POINTER_DOWN;
    case WebInputEvent::TouchEnd:
      if (AllTouchPointsHaveState(event, WebTouchPoint::StateReleased))
        return ui::MotionEvent::ACTION_UP;
      else
        return ui::MotionEvent::ACTION_POINTER_UP;
    case WebInputEvent::TouchCancel:
      DCHECK(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::GetTouchMajor(size_t pointer_index) const {
  DCHECK_LT(pointer_index, GetPointerCount());
  // TODO(jdduke): We should be a bit more careful about axes here.
  return 2.f * std::max(event_.touches[pointer_index].radiusX,
                        event_.touches[pointer_index].radiusY);
}

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);
}

size_t MotionEventWeb::GetHistorySize() const { return 0; }

base::TimeTicks MotionEventWeb::GetHistoricalEventTime(
    size_t historical_index) const {
  NOTIMPLEMENTED();
  return base::TimeTicks();
}

float MotionEventWeb::GetHistoricalTouchMajor(size_t pointer_index,
                                              size_t historical_index) const {
  NOTIMPLEMENTED();
  return 0.f;
}

float MotionEventWeb::GetHistoricalX(size_t pointer_index,
                                     size_t historical_index) const {
  NOTIMPLEMENTED();
  return 0.f;
}

float MotionEventWeb::GetHistoricalY(size_t pointer_index,
                                     size_t historical_index) const {
  NOTIMPLEMENTED();
  return 0.f;
}

scoped_ptr<ui::MotionEvent> MotionEventWeb::Clone() const {
  return scoped_ptr<MotionEvent>(new MotionEventWeb(event_));
}

scoped_ptr<ui::MotionEvent> MotionEventWeb::Cancel() const {
  WebTouchEvent cancel_event(event_);

  cancel_event.type = WebInputEvent::TouchCancel;
  for (size_t i = 0; i < cancel_event.touchesLength; ++i)
    cancel_event.touches[i].state = WebTouchPoint::StateCancelled;

  return scoped_ptr<MotionEvent>(new MotionEventWeb(cancel_event));
}

}  // namespace content

/* [<][>][^][v][top][bottom][index][help] */