Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(412)

Side by Side Diff: ppapi/examples/mouse_lock/mouse_lock.cc

Issue 7863003: Mouse lock implementation, including the renderer side and the Windows version of the browser side. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011 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 <cmath>
6 #include <stdarg.h>
7 #include <stdio.h>
8
9 #include "ppapi/c/dev/ppb_console_dev.h"
10 #include "ppapi/c/ppb_input_event.h"
11 #include "ppapi/cpp/completion_callback.h"
12 #include "ppapi/cpp/dev/mouse_lock_dev.h"
13 #include "ppapi/cpp/graphics_2d.h"
14 #include "ppapi/cpp/image_data.h"
15 #include "ppapi/cpp/input_event.h"
16 #include "ppapi/cpp/instance.h"
17 #include "ppapi/cpp/logging.h"
18 #include "ppapi/cpp/module.h"
19 #include "ppapi/cpp/rect.h"
20 #include "ppapi/cpp/var.h"
21
22 class MyInstance : public pp::Instance, public pp::MouseLock_Dev {
23 public:
24 explicit MyInstance(PP_Instance instance)
25 : pp::Instance(instance),
26 pp::MouseLock_Dev(this),
27 width_(0),
28 height_(0),
29 mouse_locked_(false),
30 pending_paint_(false),
31 waiting_for_flush_completion_(false),
32 callback_factory_(this),
33 console_(NULL) {
34 }
35 virtual ~MyInstance() {}
36
37 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) {
38 console_ = reinterpret_cast<const PPB_Console_Dev*>(
39 pp::Module::Get()->GetBrowserInterface(PPB_CONSOLE_DEV_INTERFACE));
40 if (!console_)
41 return false;
42
43 RequestInputEvents(PP_INPUTEVENT_CLASS_MOUSE);
44 return true;
45 }
46
47 virtual bool HandleInputEvent(const pp::InputEvent& event) {
48 switch (event.GetType()) {
49 case PP_INPUTEVENT_TYPE_MOUSEDOWN: {
50 pp::MouseInputEvent mouse_event(event);
51 if (mouse_event.GetButton() == PP_INPUTEVENT_MOUSEBUTTON_LEFT &&
52 !mouse_locked_) {
53 LockMouse(
54 callback_factory_.NewRequiredCallback(&MyInstance::DidLockMouse));
55 } else if (mouse_event.GetButton() == PP_INPUTEVENT_MOUSEBUTTON_RIGHT &&
56 mouse_locked_) {
57 UnlockMouse();
58 }
59 return true;
60 }
61 case PP_INPUTEVENT_TYPE_MOUSEMOVE: {
62 if (mouse_locked_) {
scheib 2011/09/09 22:00:06 I suggest also displaying movement when the mouse
yzshen1 2011/09/09 23:16:04 Done.
63 pp::MouseInputEvent mouse_event(event);
64 mouse_movement_ = mouse_event.GetMovement();
65 static unsigned int i = 0;
66 Log(PP_LOGLEVEL_LOG, "[%d] movementX: %d; movementY: %d\n", i++,
67 mouse_movement_.x(), mouse_movement_.y());
68
69 Paint();
70 }
71 return true;
72 }
73 default:
74 return false;
75 }
76 }
77
78 virtual void DidChangeView(const pp::Rect& position, const pp::Rect& clip) {
79 if (position.size().width() == width_ &&
80 position.size().height() == height_)
81 return; // We don't care about the position, only the size.
82
83 width_ = position.size().width();
84 height_ = position.size().height();
85
86 device_context_ = pp::Graphics2D(this, pp::Size(width_, height_), false);
87 if (!BindGraphics(device_context_))
88 return;
89
90 Paint();
91 }
92
93 virtual void MouseLockLost() {
94 if (mouse_locked_) {
95 mouse_locked_ = false;
96 Paint();
97 } else {
98 PP_NOTREACHED();
99 }
100 }
101
102 private:
103 void DidLockMouse(int32_t result) {
104 mouse_locked_ = result == PP_OK;
105 mouse_movement_.set_x(0);
106 mouse_movement_.set_y(0);
107 Paint();
108 }
109
110 void DidFlush(int32_t result) {
111 waiting_for_flush_completion_ = false;
112 if (pending_paint_) {
113 pending_paint_ = false;
114 Paint();
115 }
116 }
117
118 void Paint() {
119 if (waiting_for_flush_completion_) {
120 pending_paint_ = true;
121 return;
122 }
123
124 pp::ImageData image = PaintImage(width_, height_);
125 if (!image.is_null()) {
126 device_context_.ReplaceContents(&image);
127 waiting_for_flush_completion_ = true;
128 device_context_.Flush(
129 callback_factory_.NewRequiredCallback(&MyInstance::DidFlush));
130 }
131 }
132
133 pp::ImageData PaintImage(int width, int height) {
134 pp::ImageData image(this, PP_IMAGEDATAFORMAT_BGRA_PREMUL,
135 pp::Size(width, height), false);
136 if (image.is_null())
137 return image;
138
139 const static int kCenteralSpotRadius = 5;
140 const static uint32_t kBackgroundColor = 0xfff0f0f0;
141 const static uint32_t kForegroundColor = 0xfff08080;
142
143 int center_x = width / 2;
144 int center_y = height / 2;
145 pp::Point vertex(mouse_movement_.x() + center_x,
146 mouse_movement_.y() + center_y);
147 pp::Point anchor_1;
148 pp::Point anchor_2;
149 enum {
150 LEFT = 0,
151 RIGHT = 1,
152 UP = 2,
153 DOWN = 3
154 } direction;
155 bool draw_needle = mouse_locked_ &&
156 GetDistance(mouse_movement_.x(), mouse_movement_.y(),
157 0, 0) > kCenteralSpotRadius;
158 if (draw_needle) {
159 if (abs(mouse_movement_.x()) >= abs(mouse_movement_.y())) {
160 anchor_1.set_x(center_x);
161 anchor_1.set_y(center_y - kCenteralSpotRadius);
162 anchor_2.set_x(center_x);
163 anchor_2.set_y(center_y + kCenteralSpotRadius);
164 direction = (mouse_movement_.x() < 0) ? LEFT : RIGHT;
165 if (direction == LEFT)
166 anchor_1.swap(anchor_2);
167 } else {
168 anchor_1.set_x(center_x + kCenteralSpotRadius);
169 anchor_1.set_y(center_y);
170 anchor_2.set_x(center_x - kCenteralSpotRadius);
171 anchor_2.set_y(center_y);
172 direction = (mouse_movement_.y() < 0) ? UP : DOWN;
173 if (direction == UP)
174 anchor_1.swap(anchor_2);
175 }
176 }
177
178 for (int y = 0; y < image.size().height(); ++y) {
179 for (int x = 0; x < image.size().width(); ++x) {
180 if (mouse_locked_) {
181 if (GetDistance(x, y, center_x, center_y) < kCenteralSpotRadius) {
182 *image.GetAddr32(pp::Point(x, y)) = kForegroundColor;
183 continue;
184 }
185 if (draw_needle) {
186 bool within_bound_1 =
187 ((y - anchor_1.y()) * (vertex.x() - anchor_1.x())) >
188 ((vertex.y() - anchor_1.y()) * (x - anchor_1.x()));
189 bool within_bound_2 =
190 ((y - anchor_2.y()) * (vertex.x() - anchor_2.x())) <
191 ((vertex.y() - anchor_2.y()) * (x - anchor_2.x()));
192 bool within_bound_3 =
193 (direction == UP && y < center_y) ||
194 (direction == DOWN && y > center_y) ||
195 (direction == LEFT && x < center_x) ||
196 (direction == RIGHT && x > center_y);
197
198 if (within_bound_1 && within_bound_2 && within_bound_3) {
199 *image.GetAddr32(pp::Point(x, y)) = kForegroundColor;
200 continue;
201 }
202 }
203 }
204 *image.GetAddr32(pp::Point(x, y)) = kBackgroundColor;
205 }
206 }
207
208 return image;
209 }
210
211 double GetDistance(int point_1_x, int point_1_y,
212 int point_2_x, int point_2_y) {
213 return sqrt(pow(static_cast<double>(point_1_x - point_2_x), 2) +
214 pow(static_cast<double>(point_1_y - point_2_y), 2));
215 }
216
217 void Log(PP_LogLevel_Dev level, const char* format, ...) {
218 va_list args;
219 va_start(args, format);
220 char buf[512];
221 vsnprintf(buf, sizeof(buf) - 1, format, args);
222 buf[sizeof(buf) - 1] = '\0';
223 va_end(args);
224
225 pp::Var value(buf);
226 console_->Log(pp_instance(), level, value.pp_var());
227 }
228
229 int width_;
230 int height_;
231
232 bool mouse_locked_;
233 // Only valid when the mouse is locked.
234 pp::Point mouse_movement_;
235
236 bool pending_paint_;
237 bool waiting_for_flush_completion_;
238
239 pp::CompletionCallbackFactory<MyInstance> callback_factory_;
240
241 const PPB_Console_Dev* console_;
242
243 pp::Graphics2D device_context_;
244 };
245
246 // This object is the global object representing this plugin library as long
247 // as it is loaded.
248 class MyModule : public pp::Module {
249 public:
250 MyModule() : pp::Module() {}
251 virtual ~MyModule() {}
252
253 // Override CreateInstance to create your customized Instance object.
254 virtual pp::Instance* CreateInstance(PP_Instance instance) {
255 return new MyInstance(instance);
256 }
257 };
258
259 namespace pp {
260
261 // Factory function for your specialization of the Module object.
262 Module* CreateModule() {
263 return new MyModule();
264 }
265
266 } // namespace pp
267
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698