Home | History | Annotate | Download | only in mouse_cursor
      1 // Copyright (c) 2012 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 "ppapi/cpp/graphics_2d.h"
      6 #include "ppapi/cpp/image_data.h"
      7 #include "ppapi/cpp/input_event.h"
      8 #include "ppapi/cpp/instance.h"
      9 #include "ppapi/cpp/module.h"
     10 #include "ppapi/cpp/mouse_cursor.h"
     11 #include "ppapi/cpp/view.h"
     12 
     13 void FillRect(pp::ImageData* image, int left, int top, int width, int height,
     14               uint32_t color) {
     15   for (int y = std::max(0, top);
     16        y < std::min(image->size().height() - 1, top + height);
     17        y++) {
     18     for (int x = std::max(0, left);
     19          x < std::min(image->size().width() - 1, left + width);
     20          x++)
     21       *image->GetAddr32(pp::Point(x, y)) = color;
     22   }
     23 }
     24 
     25 class MyInstance : public pp::Instance {
     26  public:
     27   MyInstance(PP_Instance instance)
     28       : pp::Instance(instance), width_(0), height_(0) {
     29     RequestInputEvents(PP_INPUTEVENT_CLASS_MOUSE);
     30   }
     31 
     32   virtual ~MyInstance() {
     33   }
     34 
     35   virtual void DidChangeView(const pp::View& view) {
     36     width_ = view.GetRect().width();
     37     height_ = view.GetRect().height();
     38   }
     39 
     40   virtual bool HandleInputEvent(const pp::InputEvent& event) {
     41     switch (event.GetType()) {
     42       case PP_INPUTEVENT_TYPE_MOUSEDOWN:
     43         return true;
     44       case PP_INPUTEVENT_TYPE_MOUSEMOVE:
     45         HandleMove(pp::MouseInputEvent(event));
     46         return true;
     47       case PP_INPUTEVENT_TYPE_KEYDOWN:
     48         return true;
     49       default:
     50         return false;
     51     }
     52   }
     53 
     54   void HandleMove(const pp::MouseInputEvent& event) {
     55     pp::Point point = event.GetPosition();
     56     int segments = 3;
     57     if (point.y() < height_ / segments) {
     58       // Top part gets custom cursor of wait.
     59       pp::MouseCursor::SetCursor(this, PP_MOUSECURSOR_TYPE_WAIT);
     60     } else if (point.y() < (height_ / segments) * 2) {
     61       // Next part gets custom image cursor.
     62       pp::ImageData cursor(this, pp::ImageData::GetNativeImageDataFormat(),
     63                            pp::Size(32, 32), true);
     64       // Note that in real life you will need to handle the case where the
     65       // native format is different.
     66       FillRect(&cursor, 0, 0, 32, 32, 0x80000080);
     67       pp::MouseCursor::SetCursor(this, PP_MOUSECURSOR_TYPE_CUSTOM, cursor,
     68                                  pp::Point(16, 16));
     69     } else {
     70       // Next part gets no cursor.
     71       pp::MouseCursor::SetCursor(this, PP_MOUSECURSOR_TYPE_NONE);
     72     }
     73   }
     74 
     75 
     76  private:
     77   int width_;
     78   int height_;
     79 };
     80 
     81 class MyModule : public pp::Module {
     82  public:
     83   MyModule() : pp::Module() {}
     84   virtual ~MyModule() {}
     85 
     86   virtual pp::Instance* CreateInstance(PP_Instance instance) {
     87     return new MyInstance(instance);
     88   }
     89 };
     90 
     91 namespace pp {
     92 
     93 // Factory function for your specialization of the Module object.
     94 Module* CreateModule() {
     95   return new MyModule();
     96 }
     97 
     98 }  // namespace pp
     99