| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "examples/spinning_cube/gles2_client_impl.h" | 5 #include "examples/spinning_cube/gles2_client_impl.h" |
| 6 | 6 |
| 7 #include <GLES2/gl2.h> | 7 #include <GLES2/gl2.h> |
| 8 #include <GLES2/gl2ext.h> | 8 #include <GLES2/gl2ext.h> |
| 9 #include <math.h> | 9 #include <math.h> |
| 10 #include <stdlib.h> | 10 #include <stdlib.h> |
| 11 | 11 |
| 12 #include "gpu/command_buffer/client/gles2_interface.h" | 12 #include "gpu/command_buffer/client/gles2_interface.h" |
| 13 #include "mojo/public/c/gles2/gles2.h" | 13 #include "mojo/public/c/gles2/gles2.h" |
| 14 #include "mojo/public/cpp/environment/environment.h" | 14 #include "mojo/public/cpp/environment/environment.h" |
| 15 #include "mojo/public/cpp/utility/run_loop.h" | 15 #include "mojo/public/cpp/utility/run_loop.h" |
| 16 | 16 |
| 17 namespace examples { | 17 namespace examples { |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 float CalculateDragDistance(const mojo::Point& start, const mojo::Point& end) { | 20 float CalculateDragDistance(const mojo::PointF& start, |
| 21 return hypot(static_cast<float>(start.x - end.x), | 21 const mojo::PointF& end) { |
| 22 static_cast<float>(start.y - end.y)); | 22 return hypot(start.x - end.x, start.y - end.y); |
| 23 } | 23 } |
| 24 | 24 |
| 25 float GetRandomColor() { | 25 float GetRandomColor() { |
| 26 return static_cast<float>(rand()) / static_cast<float>(RAND_MAX); | 26 return static_cast<float>(rand()) / static_cast<float>(RAND_MAX); |
| 27 } | 27 } |
| 28 | 28 |
| 29 } | 29 } // namespace |
| 30 | 30 |
| 31 GLES2ClientImpl::GLES2ClientImpl(mojo::ContextProviderPtr context_provider) | 31 GLES2ClientImpl::GLES2ClientImpl(mojo::ContextProviderPtr context_provider) |
| 32 : last_time_(mojo::GetTimeTicksNow()), | 32 : last_time_(mojo::GetTimeTicksNow()), |
| 33 waiting_to_draw_(false), | 33 waiting_to_draw_(false), |
| 34 context_provider_(context_provider.Pass()), | 34 context_provider_(context_provider.Pass()), |
| 35 context_(nullptr) { | 35 context_(nullptr) { |
| 36 context_provider_->Create(nullptr, | 36 context_provider_->Create(nullptr, |
| 37 [this](mojo::CommandBufferPtr command_buffer) { | 37 [this](mojo::CommandBufferPtr command_buffer) { |
| 38 ContextCreated(command_buffer.Pass()); | 38 ContextCreated(command_buffer.Pass()); |
| 39 }); | 39 }); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 50 return; | 50 return; |
| 51 static_cast<gpu::gles2::GLES2Interface*>( | 51 static_cast<gpu::gles2::GLES2Interface*>( |
| 52 MojoGLES2GetGLES2Interface(context_))->ResizeCHROMIUM(size_.width, | 52 MojoGLES2GetGLES2Interface(context_))->ResizeCHROMIUM(size_.width, |
| 53 size_.height, | 53 size_.height, |
| 54 1); | 54 1); |
| 55 WantToDraw(); | 55 WantToDraw(); |
| 56 } | 56 } |
| 57 | 57 |
| 58 void GLES2ClientImpl::HandleInputEvent(const mojo::Event& event) { | 58 void GLES2ClientImpl::HandleInputEvent(const mojo::Event& event) { |
| 59 switch (event.action) { | 59 switch (event.action) { |
| 60 case mojo::EVENT_TYPE_MOUSE_PRESSED: | 60 case mojo::EVENT_TYPE_POINTER_DOWN: |
| 61 case mojo::EVENT_TYPE_TOUCH_PRESSED: | 61 if (event.flags & mojo::EVENT_FLAGS_RIGHT_MOUSE_BUTTON) |
| 62 if (event.flags & mojo::EVENT_FLAGS_RIGHT_MOUSE_BUTTON) | 62 break; |
| 63 capture_point_.x = event.pointer_data->x; |
| 64 capture_point_.y = event.pointer_data->y; |
| 65 last_drag_point_ = capture_point_; |
| 66 drag_start_time_ = mojo::GetTimeTicksNow(); |
| 63 break; | 67 break; |
| 64 capture_point_ = *event.location_data->in_view_location; | 68 case mojo::EVENT_TYPE_POINTER_MOVE: { |
| 65 last_drag_point_ = capture_point_; | 69 if (event.flags & mojo::EVENT_FLAGS_LEFT_MOUSE_BUTTON) |
| 66 drag_start_time_ = mojo::GetTimeTicksNow(); | 70 break; |
| 67 break; | 71 mojo::PointF event_location; |
| 68 case mojo::EVENT_TYPE_MOUSE_DRAGGED: | 72 event_location.x = event.pointer_data->x; |
| 69 case mojo::EVENT_TYPE_TOUCH_MOVED: { | 73 event_location.y = event.pointer_data->y; |
| 70 if (event.flags & mojo::EVENT_FLAGS_RIGHT_MOUSE_BUTTON) | 74 int direction = (event_location.y < last_drag_point_.y || |
| 75 event_location.x > last_drag_point_.x) |
| 76 ? 1 |
| 77 : -1; |
| 78 cube_.set_direction(direction); |
| 79 cube_.UpdateForDragDistance( |
| 80 CalculateDragDistance(last_drag_point_, event_location)); |
| 81 WantToDraw(); |
| 82 |
| 83 last_drag_point_ = event_location; |
| 71 break; | 84 break; |
| 72 int direction = | |
| 73 (event.location_data->in_view_location->y < last_drag_point_.y || | |
| 74 event.location_data->in_view_location->x > last_drag_point_.x) | |
| 75 ? 1 : -1; | |
| 76 cube_.set_direction(direction); | |
| 77 cube_.UpdateForDragDistance(CalculateDragDistance( | |
| 78 last_drag_point_, *event.location_data->in_view_location)); | |
| 79 WantToDraw(); | |
| 80 | |
| 81 last_drag_point_ = *event.location_data->in_view_location; | |
| 82 break; | |
| 83 } | 85 } |
| 84 case mojo::EVENT_TYPE_MOUSE_RELEASED: | 86 case mojo::EVENT_TYPE_POINTER_UP: { |
| 85 case mojo::EVENT_TYPE_TOUCH_RELEASED: { | |
| 86 if (event.flags & mojo::EVENT_FLAGS_RIGHT_MOUSE_BUTTON) { | 87 if (event.flags & mojo::EVENT_FLAGS_RIGHT_MOUSE_BUTTON) { |
| 87 cube_.set_color(GetRandomColor(), GetRandomColor(), GetRandomColor()); | 88 cube_.set_color(GetRandomColor(), GetRandomColor(), GetRandomColor()); |
| 88 break; | 89 break; |
| 89 } | 90 } |
| 91 mojo::PointF event_location; |
| 92 event_location.x = event.pointer_data->x; |
| 93 event_location.y = event.pointer_data->y; |
| 90 MojoTimeTicks offset = mojo::GetTimeTicksNow() - drag_start_time_; | 94 MojoTimeTicks offset = mojo::GetTimeTicksNow() - drag_start_time_; |
| 91 float delta = static_cast<float>(offset) / 1000000.; | 95 float delta = static_cast<float>(offset) / 1000000.; |
| 92 cube_.SetFlingMultiplier(CalculateDragDistance( | 96 cube_.SetFlingMultiplier( |
| 93 capture_point_, *event.location_data->in_view_location), delta); | 97 CalculateDragDistance(capture_point_, event_location), delta); |
| 94 | 98 |
| 95 capture_point_ = last_drag_point_ = mojo::Point(); | 99 capture_point_ = last_drag_point_ = mojo::PointF(); |
| 96 WantToDraw(); | 100 WantToDraw(); |
| 97 break; | 101 break; |
| 98 } | 102 } |
| 99 default: | 103 default: |
| 100 break; | 104 break; |
| 101 } | 105 } |
| 102 } | 106 } |
| 103 | 107 |
| 104 void GLES2ClientImpl::ContextCreated(mojo::CommandBufferPtr command_buffer) { | 108 void GLES2ClientImpl::ContextCreated(mojo::CommandBufferPtr command_buffer) { |
| 105 context_ = MojoGLES2CreateContext( | 109 context_ = MojoGLES2CreateContext( |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 float delta = static_cast<float>(offset) / 1000000.; | 145 float delta = static_cast<float>(offset) / 1000000.; |
| 142 last_time_ = now; | 146 last_time_ = now; |
| 143 cube_.UpdateForTimeDelta(delta); | 147 cube_.UpdateForTimeDelta(delta); |
| 144 cube_.Draw(); | 148 cube_.Draw(); |
| 145 | 149 |
| 146 MojoGLES2SwapBuffers(); | 150 MojoGLES2SwapBuffers(); |
| 147 WantToDraw(); | 151 WantToDraw(); |
| 148 } | 152 } |
| 149 | 153 |
| 150 } // namespace examples | 154 } // namespace examples |
| OLD | NEW |