1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/351564777): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <cmath>
#include "ppapi/c/ppb_console.h"
#include "ppapi/c/ppb_input_event.h"
#include "ppapi/cpp/graphics_2d.h"
#include "ppapi/cpp/image_data.h"
#include "ppapi/cpp/input_event.h"
#include "ppapi/cpp/instance.h"
#include "ppapi/cpp/logging.h"
#include "ppapi/cpp/module.h"
#include "ppapi/cpp/mouse_lock.h"
#include "ppapi/cpp/fullscreen.h"
#include "ppapi/cpp/rect.h"
#include "ppapi/cpp/var.h"
#include "ppapi/utility/completion_callback_factory.h"
class MyInstance : public pp::Instance, public pp::MouseLock {
public:
explicit MyInstance(PP_Instance instance)
: pp::Instance(instance),
pp::MouseLock(this),
width_(0),
height_(0),
mouse_locked_(false),
pending_paint_(false),
waiting_for_flush_completion_(false),
callback_factory_(this),
console_(NULL),
fullscreen_(this) {
}
virtual ~MyInstance() {}
virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) {
console_ = reinterpret_cast<const PPB_Console*>(
pp::Module::Get()->GetBrowserInterface(PPB_CONSOLE_INTERFACE));
if (!console_)
return false;
RequestInputEvents(PP_INPUTEVENT_CLASS_MOUSE |
PP_INPUTEVENT_CLASS_KEYBOARD);
return true;
}
virtual bool HandleInputEvent(const pp::InputEvent& event) {
switch (event.GetType()) {
case PP_INPUTEVENT_TYPE_MOUSEDOWN: {
pp::MouseInputEvent mouse_event(event);
if (mouse_event.GetButton() == PP_INPUTEVENT_MOUSEBUTTON_LEFT &&
!mouse_locked_) {
LockMouse(callback_factory_.NewCallback(&MyInstance::DidLockMouse));
}
return true;
}
case PP_INPUTEVENT_TYPE_MOUSEMOVE: {
pp::MouseInputEvent mouse_event(event);
mouse_movement_ = mouse_event.GetMovement();
static unsigned int i = 0;
Log(PP_LOGLEVEL_LOG, "[%d] movementX: %d; movementY: %d\n", i++,
mouse_movement_.x(), mouse_movement_.y());
Paint();
return true;
}
case PP_INPUTEVENT_TYPE_KEYDOWN: {
pp::KeyboardInputEvent key_event(event);
if (key_event.GetKeyCode() == 13) {
// Lock the mouse when the Enter key is pressed.
if (mouse_locked_)
UnlockMouse();
else
LockMouse(callback_factory_.NewCallback(&MyInstance::DidLockMouse));
return true;
} else if (key_event.GetKeyCode() == 70) {
// Enter Flash fullscreen mode when the 'f' key is pressed.
if (!fullscreen_.IsFullscreen())
fullscreen_.SetFullscreen(true);
return true;
}
return false;
}
default:
return false;
}
}
virtual void DidChangeView(const pp::Rect& position, const pp::Rect& clip) {
if (position.size().width() == width_ &&
position.size().height() == height_)
return; // We don't care about the position, only the size.
width_ = position.size().width();
height_ = position.size().height();
device_context_ = pp::Graphics2D(this, pp::Size(width_, height_), false);
if (!BindGraphics(device_context_))
return;
Paint();
}
virtual void MouseLockLost() {
if (mouse_locked_) {
mouse_locked_ = false;
Paint();
} else {
PP_NOTREACHED();
}
}
private:
void DidLockMouse(int32_t result) {
mouse_locked_ = result == PP_OK;
mouse_movement_.set_x(0);
mouse_movement_.set_y(0);
Paint();
}
void DidFlush(int32_t result) {
waiting_for_flush_completion_ = false;
if (pending_paint_) {
pending_paint_ = false;
Paint();
}
}
void Paint() {
if (waiting_for_flush_completion_) {
pending_paint_ = true;
return;
}
pp::ImageData image = PaintImage(width_, height_);
if (!image.is_null()) {
device_context_.ReplaceContents(&image);
waiting_for_flush_completion_ = true;
device_context_.Flush(
callback_factory_.NewCallback(&MyInstance::DidFlush));
}
}
pp::ImageData PaintImage(int width, int height) {
pp::ImageData image(this, PP_IMAGEDATAFORMAT_BGRA_PREMUL,
pp::Size(width, height), false);
if (image.is_null())
return image;
const static int kCenteralSpotRadius = 5;
const static uint32_t kBackgroundColor = 0xfff0f0f0;
const static uint32_t kLockedForegroundColor = 0xfff08080;
const static uint32_t kUnlockedForegroundColor = 0xff80f080;
int center_x = width / 2;
int center_y = height / 2;
pp::Point vertex(mouse_movement_.x() + center_x,
mouse_movement_.y() + center_y);
pp::Point anchor_1;
pp::Point anchor_2;
enum {
LEFT = 0,
RIGHT = 1,
UP = 2,
DOWN = 3
} direction = LEFT;
bool draw_needle = GetDistance(mouse_movement_.x(), mouse_movement_.y(),
0, 0) > kCenteralSpotRadius;
if (draw_needle) {
if (abs(mouse_movement_.x()) >= abs(mouse_movement_.y())) {
anchor_1.set_x(center_x);
anchor_1.set_y(center_y - kCenteralSpotRadius);
anchor_2.set_x(center_x);
anchor_2.set_y(center_y + kCenteralSpotRadius);
direction = (mouse_movement_.x() < 0) ? LEFT : RIGHT;
if (direction == LEFT)
anchor_1.swap(anchor_2);
} else {
anchor_1.set_x(center_x + kCenteralSpotRadius);
anchor_1.set_y(center_y);
anchor_2.set_x(center_x - kCenteralSpotRadius);
anchor_2.set_y(center_y);
direction = (mouse_movement_.y() < 0) ? UP : DOWN;
if (direction == UP)
anchor_1.swap(anchor_2);
}
}
uint32_t foreground_color = mouse_locked_ ? kLockedForegroundColor :
kUnlockedForegroundColor;
for (int y = 0; y < image.size().height(); ++y) {
for (int x = 0; x < image.size().width(); ++x) {
if (GetDistance(x, y, center_x, center_y) < kCenteralSpotRadius) {
*image.GetAddr32(pp::Point(x, y)) = foreground_color;
continue;
}
if (draw_needle) {
bool within_bound_1 =
((y - anchor_1.y()) * (vertex.x() - anchor_1.x())) >
((vertex.y() - anchor_1.y()) * (x - anchor_1.x()));
bool within_bound_2 =
((y - anchor_2.y()) * (vertex.x() - anchor_2.x())) <
((vertex.y() - anchor_2.y()) * (x - anchor_2.x()));
bool within_bound_3 =
(direction == UP && y < center_y) ||
(direction == DOWN && y > center_y) ||
(direction == LEFT && x < center_x) ||
(direction == RIGHT && x > center_x);
if (within_bound_1 && within_bound_2 && within_bound_3) {
*image.GetAddr32(pp::Point(x, y)) = foreground_color;
continue;
}
}
*image.GetAddr32(pp::Point(x, y)) = kBackgroundColor;
}
}
return image;
}
double GetDistance(int point_1_x, int point_1_y,
int point_2_x, int point_2_y) {
return sqrt(pow(static_cast<double>(point_1_x - point_2_x), 2) +
pow(static_cast<double>(point_1_y - point_2_y), 2));
}
void Log(PP_LogLevel level, const char* format, ...) {
va_list args;
va_start(args, format);
char buf[512];
vsnprintf(buf, sizeof(buf) - 1, format, args);
buf[sizeof(buf) - 1] = '\0';
va_end(args);
pp::Var value(buf);
console_->Log(pp_instance(), level, value.pp_var());
}
int width_;
int height_;
bool mouse_locked_;
pp::Point mouse_movement_;
bool pending_paint_;
bool waiting_for_flush_completion_;
pp::CompletionCallbackFactory<MyInstance> callback_factory_;
const PPB_Console* console_;
pp::Fullscreen fullscreen_;
pp::Graphics2D device_context_;
};
// This object is the global object representing this plugin library as long
// as it is loaded.
class MyModule : public pp::Module {
public:
MyModule() : pp::Module() {}
virtual ~MyModule() {}
// Override CreateInstance to create your customized Instance object.
virtual pp::Instance* CreateInstance(PP_Instance instance) {
return new MyInstance(instance);
}
};
namespace pp {
// Factory function for your specialization of the Module object.
Module* CreateModule() {
return new MyModule();
}
} // namespace pp
|