| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 <cmath> | 5 #include <cmath> |
| 6 #include <stdarg.h> | 6 #include <stdarg.h> |
| 7 #include <stdio.h> | 7 #include <stdio.h> |
| 8 | 8 |
| 9 #include "ppapi/c/dev/ppb_console_dev.h" | 9 #include "ppapi/c/dev/ppb_console_dev.h" |
| 10 #include "ppapi/c/dev/ppb_gamepad_dev.h" | 10 #include "ppapi/c/dev/ppb_gamepad_dev.h" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 callback_factory_(this), | 42 callback_factory_(this), |
| 43 gamepad_(NULL) { | 43 gamepad_(NULL) { |
| 44 } | 44 } |
| 45 virtual ~MyInstance() {} | 45 virtual ~MyInstance() {} |
| 46 | 46 |
| 47 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) { | 47 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) { |
| 48 gamepad_ = reinterpret_cast<const PPB_Gamepad_Dev*>( | 48 gamepad_ = reinterpret_cast<const PPB_Gamepad_Dev*>( |
| 49 pp::Module::Get()->GetBrowserInterface(PPB_GAMEPAD_DEV_INTERFACE)); | 49 pp::Module::Get()->GetBrowserInterface(PPB_GAMEPAD_DEV_INTERFACE)); |
| 50 if (!gamepad_) | 50 if (!gamepad_) |
| 51 return false; | 51 return false; |
| 52 | |
| 53 RequestInputEvents(PP_INPUTEVENT_CLASS_MOUSE | | |
| 54 PP_INPUTEVENT_CLASS_KEYBOARD); | |
| 55 return true; | 52 return true; |
| 56 } | 53 } |
| 57 | 54 |
| 58 virtual void DidChangeView(const pp::View& view) { | 55 virtual void DidChangeView(const pp::View& view) { |
| 59 pp::Rect rect = view.GetRect(); | 56 pp::Rect rect = view.GetRect(); |
| 60 if (rect.size().width() == width_ && | 57 if (rect.size().width() == width_ && |
| 61 rect.size().height() == height_) | 58 rect.size().height() == height_) |
| 62 return; // We don't care about the position, only the size. | 59 return; // We don't care about the position, only the size. |
| 63 | 60 |
| 64 width_ = rect.size().width(); | 61 width_ = rect.size().width(); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 85 } else { | 82 } else { |
| 86 printf("NullImage\n"); | 83 printf("NullImage\n"); |
| 87 } | 84 } |
| 88 } | 85 } |
| 89 | 86 |
| 90 pp::ImageData PaintImage(const pp::Size& size) { | 87 pp::ImageData PaintImage(const pp::Size& size) { |
| 91 pp::ImageData image(this, PP_IMAGEDATAFORMAT_BGRA_PREMUL, size, false); | 88 pp::ImageData image(this, PP_IMAGEDATAFORMAT_BGRA_PREMUL, size, false); |
| 92 if (image.is_null()) | 89 if (image.is_null()) |
| 93 return image; | 90 return image; |
| 94 | 91 |
| 95 PP_GamepadsData_Dev gamepad_data; | 92 PP_GamepadsSampleData_Dev gamepad_data; |
| 96 gamepad_->SampleGamepads(pp_instance(), &gamepad_data); | 93 gamepad_->Sample(pp_instance(), &gamepad_data); |
| 97 | 94 |
| 98 int width2 = size.width() / 2; | 95 if (gamepad_data.length > 1 && gamepad_data.items[0].connected) { |
| 99 int height2 = size.height() / 2; | 96 int width2 = size.width() / 2; |
| 100 // Draw 2 axes | 97 int height2 = size.height() / 2; |
| 101 for (size_t i = 0; i < gamepad_data.items[0].axes_length; i += 2) { | 98 // Draw 2 axes |
| 102 int x = static_cast<int>( | 99 for (size_t i = 0; i < gamepad_data.items[0].axes_length; i += 2) { |
| 103 gamepad_data.items[0].axes[i + 0] * width2 + width2); | 100 int x = static_cast<int>( |
| 104 int y = static_cast<int>( | 101 gamepad_data.items[0].axes[i + 0] * width2 + width2); |
| 105 gamepad_data.items[0].axes[i + 1] * height2 + height2); | 102 int y = static_cast<int>( |
| 106 uint32_t box_bgra = 0x80000000; // Alpha 50%. | 103 gamepad_data.items[0].axes[i + 1] * height2 + height2); |
| 107 FillRect(&image, x - 3, y - 3, 7, 7, box_bgra); | 104 uint32_t box_bgra = 0x80000000; // Alpha 50%. |
| 108 } | 105 FillRect(&image, x - 3, y - 3, 7, 7, box_bgra); |
| 106 } |
| 109 | 107 |
| 110 for (size_t i = 0; i < gamepad_data.items[0].buttons_length; ++i) { | 108 for (size_t i = 0; i < gamepad_data.items[0].buttons_length; ++i) { |
| 111 float button_val = gamepad_data.items[0].buttons[i]; | 109 float button_val = gamepad_data.items[0].buttons[i]; |
| 112 uint32_t colour = static_cast<uint32_t>((button_val * 192) + 63) << 24; | 110 uint32_t colour = static_cast<uint32_t>((button_val * 192) + 63) << 24; |
| 113 int x = i * 8 + 10; | 111 int x = i * 8 + 10; |
| 114 int y = 10; | 112 int y = 10; |
| 115 FillRect(&image, x - 3, y - 3, 7, 7, colour); | 113 FillRect(&image, x - 3, y - 3, 7, 7, colour); |
| 114 } |
| 116 } | 115 } |
| 117 return image; | 116 return image; |
| 118 } | 117 } |
| 119 | 118 |
| 120 int width_; | 119 int width_; |
| 121 int height_; | 120 int height_; |
| 122 | 121 |
| 123 pp::CompletionCallbackFactory<MyInstance> callback_factory_; | 122 pp::CompletionCallbackFactory<MyInstance> callback_factory_; |
| 124 | 123 |
| 125 const PPB_Gamepad_Dev* gamepad_; | 124 const PPB_Gamepad_Dev* gamepad_; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 142 | 141 |
| 143 namespace pp { | 142 namespace pp { |
| 144 | 143 |
| 145 // Factory function for your specialization of the Module object. | 144 // Factory function for your specialization of the Module object. |
| 146 Module* CreateModule() { | 145 Module* CreateModule() { |
| 147 return new MyModule(); | 146 return new MyModule(); |
| 148 } | 147 } |
| 149 | 148 |
| 150 } // namespace pp | 149 } // namespace pp |
| 151 | 150 |
| OLD | NEW |