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 "mojo/examples/sample_app/gles2_client_impl.h" | 5 #include "mojo/examples/sample_app/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 "mojo/public/c/gles2/gles2.h" | 12 #include "mojo/public/c/gles2/gles2.h" |
13 #include "ui/events/event_constants.h" | 13 #include "ui/events/event_constants.h" |
14 | 14 |
15 namespace mojo { | 15 namespace mojo { |
16 namespace examples { | 16 namespace examples { |
17 namespace { | 17 namespace { |
18 | 18 |
19 float CalculateDragDistance(const gfx::PointF& start, const Point& end) { | 19 float CalculateDragDistance(const gfx::PointF& start, const Point& end) { |
20 return hypot(start.x() - end.x(), start.y() - end.y()); | 20 return hypot(start.x() - end.x, start.y() - end.y); |
21 } | 21 } |
22 | 22 |
23 float GetRandomColor() { | 23 float GetRandomColor() { |
24 return static_cast<float>(rand()) / static_cast<float>(RAND_MAX); | 24 return static_cast<float>(rand()) / static_cast<float>(RAND_MAX); |
25 } | 25 } |
26 | 26 |
27 } | 27 } |
28 | 28 |
29 GLES2ClientImpl::GLES2ClientImpl(ScopedMessagePipeHandle pipe) | 29 GLES2ClientImpl::GLES2ClientImpl(ScopedMessagePipeHandle pipe) |
30 : getting_animation_frames_(false) { | 30 : getting_animation_frames_(false) { |
31 context_ = MojoGLES2CreateContext( | 31 context_ = MojoGLES2CreateContext( |
32 pipe.release().value(), | 32 pipe.release().value(), |
33 &ContextLostThunk, | 33 &ContextLostThunk, |
34 &DrawAnimationFrameThunk, | 34 &DrawAnimationFrameThunk, |
35 this); | 35 this); |
36 MojoGLES2MakeCurrent(context_); | 36 MojoGLES2MakeCurrent(context_); |
37 } | 37 } |
38 | 38 |
39 GLES2ClientImpl::~GLES2ClientImpl() { | 39 GLES2ClientImpl::~GLES2ClientImpl() { |
40 MojoGLES2DestroyContext(context_); | 40 MojoGLES2DestroyContext(context_); |
41 } | 41 } |
42 | 42 |
43 void GLES2ClientImpl::SetSize(const Size& size) { | 43 void GLES2ClientImpl::SetSize(const Size& size) { |
44 size_ = gfx::Size(size.width(), size.height()); | 44 size_ = gfx::Size(size.width, size.height); |
45 if (size_.IsEmpty()) | 45 if (size_.IsEmpty()) |
46 return; | 46 return; |
47 cube_.Init(size_.width(), size_.height()); | 47 cube_.Init(size_.width(), size_.height()); |
48 RequestAnimationFrames(); | 48 RequestAnimationFrames(); |
49 } | 49 } |
50 | 50 |
51 void GLES2ClientImpl::HandleInputEvent(const Event& event) { | 51 void GLES2ClientImpl::HandleInputEvent(const Event& event) { |
52 switch (event.action()) { | 52 switch (event.action) { |
53 case ui::ET_MOUSE_PRESSED: | 53 case ui::ET_MOUSE_PRESSED: |
54 case ui::ET_TOUCH_PRESSED: | 54 case ui::ET_TOUCH_PRESSED: |
55 CancelAnimationFrames(); | 55 CancelAnimationFrames(); |
56 capture_point_.SetPoint(event.location().x(), event.location().y()); | 56 capture_point_.SetPoint(event.location->x, event.location->y); |
57 last_drag_point_ = capture_point_; | 57 last_drag_point_ = capture_point_; |
58 drag_start_time_ = GetTimeTicksNow(); | 58 drag_start_time_ = GetTimeTicksNow(); |
59 break; | 59 break; |
60 case ui::ET_MOUSE_DRAGGED: | 60 case ui::ET_MOUSE_DRAGGED: |
61 case ui::ET_TOUCH_MOVED: | 61 case ui::ET_TOUCH_MOVED: |
62 if (!getting_animation_frames_) { | 62 if (!getting_animation_frames_) { |
63 int direction = event.location().y() < last_drag_point_.y() || | 63 int direction = event.location->y < last_drag_point_.y() || |
64 event.location().x() > last_drag_point_.x() ? 1 : -1; | 64 event.location->x > last_drag_point_.x() ? 1 : -1; |
65 cube_.set_direction(direction); | 65 cube_.set_direction(direction); |
66 cube_.UpdateForDragDistance( | 66 cube_.UpdateForDragDistance( |
67 CalculateDragDistance(last_drag_point_, event.location())); | 67 CalculateDragDistance(last_drag_point_, *event.location)); |
68 cube_.Draw(); | 68 cube_.Draw(); |
69 MojoGLES2SwapBuffers(); | 69 MojoGLES2SwapBuffers(); |
70 | 70 |
71 last_drag_point_.SetPoint(event.location().x(), event.location().y()); | 71 last_drag_point_.SetPoint(event.location->x, event.location->y); |
72 } | 72 } |
73 break; | 73 break; |
74 case ui::ET_MOUSE_RELEASED: | 74 case ui::ET_MOUSE_RELEASED: |
75 case ui::ET_TOUCH_RELEASED: { | 75 case ui::ET_TOUCH_RELEASED: { |
76 MojoTimeTicks offset = GetTimeTicksNow() - drag_start_time_; | 76 MojoTimeTicks offset = GetTimeTicksNow() - drag_start_time_; |
77 float delta = static_cast<float>(offset) / 1000000.; | 77 float delta = static_cast<float>(offset) / 1000000.; |
78 cube_.SetFlingMultiplier( | 78 cube_.SetFlingMultiplier( |
79 CalculateDragDistance(capture_point_, event.location()), | 79 CalculateDragDistance(capture_point_, *event.location), |
80 delta); | 80 delta); |
81 | 81 |
82 cube_.set_color(GetRandomColor(), GetRandomColor(), GetRandomColor()); | 82 cube_.set_color(GetRandomColor(), GetRandomColor(), GetRandomColor()); |
83 | 83 |
84 capture_point_ = last_drag_point_ = gfx::PointF(); | 84 capture_point_ = last_drag_point_ = gfx::PointF(); |
85 RequestAnimationFrames(); | 85 RequestAnimationFrames(); |
86 } | 86 } |
87 break; | 87 break; |
88 default: | 88 default: |
89 break; | 89 break; |
(...skipping 29 matching lines...) Expand all Loading... |
119 last_time_ = GetTimeTicksNow(); | 119 last_time_ = GetTimeTicksNow(); |
120 } | 120 } |
121 | 121 |
122 void GLES2ClientImpl::CancelAnimationFrames() { | 122 void GLES2ClientImpl::CancelAnimationFrames() { |
123 getting_animation_frames_ = false; | 123 getting_animation_frames_ = false; |
124 MojoGLES2CancelAnimationFrames(context_); | 124 MojoGLES2CancelAnimationFrames(context_); |
125 } | 125 } |
126 | 126 |
127 } // namespace examples | 127 } // namespace examples |
128 } // namespace mojo | 128 } // namespace mojo |
OLD | NEW |