diff options
author | ben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-12-10 23:24:27 +0000 |
---|---|---|
committer | ben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-12-10 23:24:27 +0000 |
commit | 3df1e15f05e97223a7acff7373f0b4fb5a39fc76 (patch) | |
tree | 69ac96a449de70f5fa47a1e9ec53930daa9661ec /mojo/examples/sample_app | |
parent | af7b5864f521efbd3c3758dc3d10f0a4a10c0080 (diff) | |
download | chromium_src-3df1e15f05e97223a7acff7373f0b4fb5a39fc76.zip chromium_src-3df1e15f05e97223a7acff7373f0b4fb5a39fc76.tar.gz chromium_src-3df1e15f05e97223a7acff7373f0b4fb5a39fc76.tar.bz2 |
Do something more interesting than printf when an event is received by sample_app.
R=abarth@chromium.org
http://crbug.com/324639
Review URL: https://codereview.chromium.org/108273008
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@239910 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'mojo/examples/sample_app')
-rw-r--r-- | mojo/examples/sample_app/DEPS | 4 | ||||
-rw-r--r-- | mojo/examples/sample_app/gles2_client_impl.cc | 62 | ||||
-rw-r--r-- | mojo/examples/sample_app/gles2_client_impl.h | 9 | ||||
-rw-r--r-- | mojo/examples/sample_app/native_viewport_client_impl.cc | 4 | ||||
-rw-r--r-- | mojo/examples/sample_app/spinning_cube.cc | 61 | ||||
-rw-r--r-- | mojo/examples/sample_app/spinning_cube.h | 9 |
6 files changed, 127 insertions, 22 deletions
diff --git a/mojo/examples/sample_app/DEPS b/mojo/examples/sample_app/DEPS new file mode 100644 index 0000000..49b9c28 --- /dev/null +++ b/mojo/examples/sample_app/DEPS @@ -0,0 +1,4 @@ +include_rules = [ + "+ui/events/event_constants.h", # pending enum support in mojom + "+ui/gfx", # TODO(beng): trim the size of this dep. +] diff --git a/mojo/examples/sample_app/gles2_client_impl.cc b/mojo/examples/sample_app/gles2_client_impl.cc index 2a87ca0..ea89cb7 100644 --- a/mojo/examples/sample_app/gles2_client_impl.cc +++ b/mojo/examples/sample_app/gles2_client_impl.cc @@ -8,9 +8,17 @@ #include <GLES2/gl2ext.h> #include "mojo/public/gles2/gles2.h" +#include "ui/events/event_constants.h" namespace mojo { namespace examples { +namespace { + +float CalculateDragDistance(const gfx::PointF& start, const Point& end) { + return hypot(start.x() - end.x(), start.y() - end.y()); +} + +} GLES2ClientImpl::GLES2ClientImpl(ScopedMessagePipeHandle pipe) : service_(pipe.Pass()) { @@ -21,29 +29,71 @@ GLES2ClientImpl::~GLES2ClientImpl() { service_->Destroy(); } +void GLES2ClientImpl::HandleInputEvent(const Event& event) { + switch (event.action()) { + case ui::ET_MOUSE_PRESSED: + case ui::ET_TOUCH_PRESSED: + timer_.Stop(); + capture_point_.SetPoint(event.location().x(), event.location().y()); + last_drag_point_ = capture_point_; + drag_start_time_ = base::Time::Now(); + break; + case ui::ET_MOUSE_DRAGGED: + case ui::ET_TOUCH_MOVED: + if (!timer_.IsRunning()) { + int direction = event.location().y() < last_drag_point_.y() || + event.location().x() > last_drag_point_.x() ? 1 : -1; + cube_.set_direction(direction); + cube_.UpdateForDragDistance( + CalculateDragDistance(last_drag_point_, event.location())); + cube_.Draw(); + MojoGLES2SwapBuffers(); + + last_drag_point_.SetPoint(event.location().x(), event.location().y()); + } + break; + case ui::ET_MOUSE_RELEASED: + case ui::ET_TOUCH_RELEASED: { + cube_.SetFlingMultiplier( + CalculateDragDistance(capture_point_, event.location()), + base::TimeDelta(base::Time::Now() - drag_start_time_).InSecondsF()); + + capture_point_ = last_drag_point_ = gfx::PointF(); + StartTimer(); + } + break; + default: + break; + } +} + void GLES2ClientImpl::DidCreateContext(uint64_t encoded, uint32_t width, uint32_t height) { MojoGLES2MakeCurrent(encoded); cube_.Init(width, height); - last_time_ = base::Time::Now(); - timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(16), - this, &GLES2ClientImpl::Draw); + StartTimer(); +} + +void GLES2ClientImpl::ContextLost() { + timer_.Stop(); } void GLES2ClientImpl::Draw() { base::Time now = base::Time::Now(); base::TimeDelta offset = now - last_time_; last_time_ = now; - cube_.Update(offset.InSecondsF()); + cube_.UpdateForTimeDelta(offset.InSecondsF()); cube_.Draw(); MojoGLES2SwapBuffers(); } -void GLES2ClientImpl::ContextLost() { - timer_.Stop(); +void GLES2ClientImpl::StartTimer() { + last_time_ = base::Time::Now(); + timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(16), + this, &GLES2ClientImpl::Draw); } } // namespace examples diff --git a/mojo/examples/sample_app/gles2_client_impl.h b/mojo/examples/sample_app/gles2_client_impl.h index 1d38a3c..300557a 100644 --- a/mojo/examples/sample_app/gles2_client_impl.h +++ b/mojo/examples/sample_app/gles2_client_impl.h @@ -5,10 +5,13 @@ #ifndef MOJO_EXAMPLES_SAMPLE_APP_GLES2_CLIENT_IMPL_H_ #define MOJO_EXAMPLES_SAMPLE_APP_GLES2_CLIENT_IMPL_H_ +#include "base/time/time.h" #include "base/timer/timer.h" #include "mojo/examples/sample_app/spinning_cube.h" #include "mojo/public/bindings/lib/remote_ptr.h" #include "mojom/gles2.h" +#include "mojom/native_viewport.h" +#include "ui/gfx/point_f.h" namespace mojo { namespace examples { @@ -18,6 +21,8 @@ class GLES2ClientImpl : public GLES2ClientStub { explicit GLES2ClientImpl(ScopedMessagePipeHandle pipe); virtual ~GLES2ClientImpl(); + void HandleInputEvent(const Event& event); + private: virtual void DidCreateContext(uint64_t encoded, uint32_t width, @@ -25,10 +30,14 @@ class GLES2ClientImpl : public GLES2ClientStub { virtual void ContextLost() MOJO_OVERRIDE; void Draw(); + void StartTimer(); base::Time last_time_; base::RepeatingTimer<GLES2ClientImpl> timer_; SpinningCube cube_; + gfx::PointF capture_point_; + gfx::PointF last_drag_point_; + base::Time drag_start_time_; RemotePtr<GLES2> service_; diff --git a/mojo/examples/sample_app/native_viewport_client_impl.cc b/mojo/examples/sample_app/native_viewport_client_impl.cc index 2fc2d98..17991da 100644 --- a/mojo/examples/sample_app/native_viewport_client_impl.cc +++ b/mojo/examples/sample_app/native_viewport_client_impl.cc @@ -41,8 +41,8 @@ void NativeViewportClientImpl::OnDestroyed() { void NativeViewportClientImpl::OnEvent(const Event& event) { if (!event.location().is_null()) { - LOG(INFO) << "Located Event @" - << event.location().x() << "," << event.location().y(); + gles2_client_->HandleInputEvent(event); + service_->AckEvent(event); } } diff --git a/mojo/examples/sample_app/spinning_cube.cc b/mojo/examples/sample_app/spinning_cube.cc index 3a09132..7983563 100644 --- a/mojo/examples/sample_app/spinning_cube.cc +++ b/mojo/examples/sample_app/spinning_cube.cc @@ -293,8 +293,16 @@ class ESMatrix { } }; +float RotationForTimeDelta(float delta_time) { + return delta_time * 40.0f; } +float RotationForDragDistance(float drag_distance) { + return drag_distance / 5; // Arbitrary damping. +} + +} // namespace + class SpinningCube::GLState { public: GLState(); @@ -330,7 +338,9 @@ SpinningCube::SpinningCube() : initialized_(false), width_(0), height_(0), - state_(new GLState()) { + state_(new GLState()), + fling_multiplier_(1.0f), + direction_(1) { state_->angle_ = 45.0f; } @@ -384,23 +394,33 @@ void SpinningCube::OnGLContextLost() { state_->OnGLContextLost(); } -void SpinningCube::Update(float delta_time) { - state_->angle_ += ( delta_time * 40.0f ); - if (state_->angle_ >= 360.0f ) +void SpinningCube::SetFlingMultiplier(float drag_distance, + float drag_time) { + fling_multiplier_ = RotationForDragDistance(drag_distance) / + RotationForTimeDelta(drag_time); + +} + +void SpinningCube::UpdateForTimeDelta(float delta_time) { + state_->angle_ += RotationForTimeDelta(delta_time) * fling_multiplier_; + if (state_->angle_ >= 360.0f) state_->angle_ -= 360.0f; - float aspect = static_cast<GLfloat>(width_) / static_cast<GLfloat>(height_); + // Arbitrary 50-step linear reduction in spin speed. + if (fling_multiplier_ > 1.0f) { + fling_multiplier_ = + std::max(1.0f, fling_multiplier_ - (fling_multiplier_ - 1.0f) / 50); + } - ESMatrix perspective; - perspective.LoadIdentity(); - perspective.Perspective(60.0f, aspect, 1.0f, 20.0f ); + Update(); +} - ESMatrix modelview; - modelview.LoadIdentity(); - modelview.Translate(0.0, 0.0, -2.0); - modelview.Rotate(state_->angle_, 1.0, 0.0, 1.0); +void SpinningCube::UpdateForDragDistance(float distance) { + state_->angle_ += RotationForDragDistance(distance); + if (state_->angle_ >= 360.0f ) + state_->angle_ -= 360.0f; - state_->mvp_matrix_.Multiply(&modelview, &perspective); + Update(); } void SpinningCube::Draw() { @@ -425,5 +445,20 @@ void SpinningCube::Draw() { 0); } +void SpinningCube::Update() { + float aspect = static_cast<GLfloat>(width_) / static_cast<GLfloat>(height_); + + ESMatrix perspective; + perspective.LoadIdentity(); + perspective.Perspective(60.0f, aspect, 1.0f, 20.0f ); + + ESMatrix modelview; + modelview.LoadIdentity(); + modelview.Translate(0.0, 0.0, -2.0); + modelview.Rotate(state_->angle_ * direction_, 1.0, 0.0, 1.0); + + state_->mvp_matrix_.Multiply(&modelview, &perspective); +} + } // namespace examples } // namespace mojo diff --git a/mojo/examples/sample_app/spinning_cube.h b/mojo/examples/sample_app/spinning_cube.h index c4a81ea..9cd78b8 100644 --- a/mojo/examples/sample_app/spinning_cube.h +++ b/mojo/examples/sample_app/spinning_cube.h @@ -18,7 +18,10 @@ class SpinningCube { ~SpinningCube(); void Init(uint32_t width, uint32_t height); - void Update(float delta_time); + void set_direction(int direction) { direction_ = direction; } + void SetFlingMultiplier(float drag_distance, float drag_time); + void UpdateForTimeDelta(float delta_time); + void UpdateForDragDistance(float distance); void Draw(); void OnGLContextLost(); @@ -26,10 +29,14 @@ class SpinningCube { private: class GLState; + void Update(); + bool initialized_; uint32_t width_; uint32_t height_; scoped_ptr<GLState> state_; + float fling_multiplier_; + int direction_; }; } // namespace examples |