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

Side by Side Diff: examples/mouselock/mouselock.h

Issue 8510041: Add a mouselock example. (Closed) Base URL: http://nativeclient-sdk.googlecode.com/svn/trunk/src/
Patch Set: '' Created 9 years, 1 month 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
« no previous file with comments | « examples/mouselock/build.scons ('k') | examples/mouselock/mouselock.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Property Changes:
Added: svn:eol-style
+ LF
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
7 #include "ppapi/c/ppb_fullscreen.h"
8 #include "ppapi/c/ppb_input_event.h"
9 #include "ppapi/cpp/completion_callback.h"
10 #include "ppapi/cpp/fullscreen.h"
11 #include "ppapi/cpp/mouse_lock.h"
12 #include "ppapi/cpp/graphics_2d.h"
13 #include "ppapi/cpp/image_data.h"
14 #include "ppapi/cpp/input_event.h"
15 #include "ppapi/cpp/instance.h"
16 #include "ppapi/cpp/module.h"
17 #include "ppapi/cpp/rect.h"
18 #include "ppapi/cpp/var.h"
19
20 namespace mouselock {
21
22 class MouseLockInstance : public pp::Instance, public pp::MouseLock {
23 public:
24 explicit MouseLockInstance(PP_Instance instance)
25 : pp::Instance(instance),
26 pp::MouseLock(this),
27 width_(0),
28 height_(0),
29 mouse_locked_(false),
30 waiting_for_flush_completion_(false),
31 callback_factory_(this),
32 fullscreen_(this),
33 is_context_bound_(false),
34 background_scanline_(NULL) {
35 }
36 virtual ~MouseLockInstance();
37
38 // Called by the browser when the NaCl module is loaded and all ready to go.
39 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]);
40
41 // Called by the browser to handle incoming input events.
42 virtual bool HandleInputEvent(const pp::InputEvent& event);
43
44 // Called whenever the in-browser window changes size.
45 virtual void DidChangeView(const pp::Rect& position, const pp::Rect& clip);
46
47 // Called by the browser when mouselock is lost. This happens when the NaCl
48 // module exits fullscreen mode.
49 virtual void MouseLockLost();
50
51 private:
52 // Return the Cartesian distance between two points.
53 double GetDistance(int point_1_x, int point_1_y,
54 int point_2_x, int point_2_y) {
55 return sqrt(pow(static_cast<double>(point_1_x - point_2_x), 2) +
56 pow(static_cast<double>(point_1_y - point_2_y), 2));
57 }
58
59 // Called when mouse lock has been acquired. Used as a callback to
60 // pp::MouseLock.LockMouse().
61 void DidLockMouse(int32_t result);
62
63 // Called when the 2D context has been flushed to the browser window. Used
64 // as a callback to pp::Graphics2D.Flush().
65 void DidFlush(int32_t result);
66
67 // Creates a new paint buffer, paints it then flush it to the 2D context. If
68 // a flush is pending, this does nothing.
69 void Paint();
70
71 // Create a new pp::ImageData and paint the graphics that represent the mouse
72 // movement in it. Return the new pp::ImageData.
73 pp::ImageData PaintImage(int width, int height);
74
75 // Fill the image with the backgroud color.
76 void ClearToBackground(pp::ImageData* image);
77
78 // Draw a spot in |spot_color| in the center of the image. The radius of the
79 // spot is defined by a constant value in mouselock.cc
80 void DrawCenterSpot(pp::ImageData* image, uint32_t spot_color);
81
82 // Draw the needle when the mouse is outside of the central spot.
83 void DrawNeedle(pp::ImageData* image, uint32_t needle_color);
84
85 // Print the printf-style format to the "console" via PostMessage.
86 void Log(const char* format, ...);
87
88 int width_;
89 int height_;
90
91 bool mouse_locked_;
92 pp::Point mouse_movement_;
93 bool waiting_for_flush_completion_;
94 pp::CompletionCallbackFactory<MouseLockInstance> callback_factory_;
95
96 pp::Fullscreen fullscreen_;
97 pp::Graphics2D device_context_;
98 bool is_context_bound_;
99 uint32_t* background_scanline_;
100 };
101
102 } // namespace mouselock
OLDNEW
« no previous file with comments | « examples/mouselock/build.scons ('k') | examples/mouselock/mouselock.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698