OLD | NEW |
1 // Copyright (c) 2011 The Native Client Authors. All rights reserved. | 1 // Copyright (c) 2011 The Native Client 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 "examples/mouselock/mouselock.h" | 5 #include "examples/mouselock/mouselock.h" |
6 | 6 |
7 #include <cmath> | 7 #include <cmath> |
8 #include <cstdlib> | 8 #include <cstdlib> |
9 #include <stdarg.h> | 9 #include <stdarg.h> |
10 #include <stdio.h> | 10 #include <stdio.h> |
11 #include <string.h> | 11 #include <string.h> |
12 | 12 |
| 13 #include <algorithm> |
| 14 |
13 // Indicate the direction of the mouse location relative to the center of the | 15 // Indicate the direction of the mouse location relative to the center of the |
14 // view. These values are used to determine which 2D quadrant the needle lies | 16 // view. These values are used to determine which 2D quadrant the needle lies |
15 // in. | 17 // in. |
16 typedef enum { | 18 typedef enum { |
17 kLeft = 0, | 19 kLeft = 0, |
18 kRight = 1, | 20 kRight = 1, |
19 kUp = 2, | 21 kUp = 2, |
20 kDown = 3 | 22 kDown = 3 |
21 } MouseDirection; | 23 } MouseDirection; |
22 | 24 |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 if (image == NULL) { | 212 if (image == NULL) { |
211 Log("DrawCenterSpot with NULL image"); | 213 Log("DrawCenterSpot with NULL image"); |
212 return; | 214 return; |
213 } | 215 } |
214 // Draw the center spot. The ROI is bounded by the size of the spot, plus | 216 // Draw the center spot. The ROI is bounded by the size of the spot, plus |
215 // one pixel. | 217 // one pixel. |
216 int center_x = image->size().width() / 2; | 218 int center_x = image->size().width() / 2; |
217 int center_y = image->size().height() / 2; | 219 int center_y = image->size().height() / 2; |
218 int region_of_interest_radius = kCentralSpotRadius + 1; | 220 int region_of_interest_radius = kCentralSpotRadius + 1; |
219 | 221 |
220 for (int y = center_y - region_of_interest_radius; | 222 pp::Point left_top(std::max(0, center_x - region_of_interest_radius), |
221 y < center_y + region_of_interest_radius; | 223 std::max(0, center_y - region_of_interest_radius)); |
222 ++y) { | 224 pp::Point right_bottom(std::min(image->size().width(), |
223 for (int x = center_x - region_of_interest_radius; | 225 center_x + region_of_interest_radius), |
224 x < center_x + region_of_interest_radius; | 226 std::min(image->size().height(), |
225 ++x) { | 227 center_y + region_of_interest_radius)); |
| 228 for (int y = left_top.y(); y < right_bottom.y(); ++y) { |
| 229 for (int x = left_top.x(); x < right_bottom.x(); ++x) { |
226 if (GetDistance(x, y, center_x, center_y) < kCentralSpotRadius) { | 230 if (GetDistance(x, y, center_x, center_y) < kCentralSpotRadius) { |
227 *image->GetAddr32(pp::Point(x, y)) = spot_color; | 231 *image->GetAddr32(pp::Point(x, y)) = spot_color; |
228 } | 232 } |
229 } | 233 } |
230 } | 234 } |
231 } | 235 } |
232 | 236 |
233 void MouseLockInstance::DrawNeedle(pp::ImageData* image, | 237 void MouseLockInstance::DrawNeedle(pp::ImageData* image, |
234 uint32_t needle_color) { | 238 uint32_t needle_color) { |
235 if (image == NULL) { | 239 if (image == NULL) { |
(...skipping 26 matching lines...) Expand all Loading... |
262 } else { | 266 } else { |
263 anchor_1.set_x(center_x + kCentralSpotRadius); | 267 anchor_1.set_x(center_x + kCentralSpotRadius); |
264 anchor_1.set_y(center_y); | 268 anchor_1.set_y(center_y); |
265 anchor_2.set_x(center_x - kCentralSpotRadius); | 269 anchor_2.set_x(center_x - kCentralSpotRadius); |
266 anchor_2.set_y(center_y); | 270 anchor_2.set_y(center_y); |
267 direction = (mouse_movement_.y() < 0) ? kUp : kDown; | 271 direction = (mouse_movement_.y() < 0) ? kUp : kDown; |
268 if (direction == kUp) | 272 if (direction == kUp) |
269 anchor_1.swap(anchor_2); | 273 anchor_1.swap(anchor_2); |
270 } | 274 } |
271 | 275 |
272 for (int y = center_y - abs_mouse_y; y < center_y + abs_mouse_y; ++y) { | 276 pp::Point left_top(std::max(0, center_x - abs_mouse_x), |
273 for (int x = center_x - abs_mouse_x; x < center_x + abs_mouse_x; ++x) { | 277 std::max(0, center_y - abs_mouse_y)); |
| 278 pp::Point right_bottom(std::min(image->size().width(), |
| 279 center_x + abs_mouse_x), |
| 280 std::min(image->size().height(), |
| 281 center_y + abs_mouse_y)); |
| 282 for (int y = left_top.y(); y < right_bottom.y(); ++y) { |
| 283 for (int x = left_top.x(); x < right_bottom.x(); ++x) { |
274 bool within_bound_1 = | 284 bool within_bound_1 = |
275 ((y - anchor_1.y()) * (vertex.x() - anchor_1.x())) > | 285 ((y - anchor_1.y()) * (vertex.x() - anchor_1.x())) > |
276 ((vertex.y() - anchor_1.y()) * (x - anchor_1.x())); | 286 ((vertex.y() - anchor_1.y()) * (x - anchor_1.x())); |
277 bool within_bound_2 = | 287 bool within_bound_2 = |
278 ((y - anchor_2.y()) * (vertex.x() - anchor_2.x())) < | 288 ((y - anchor_2.y()) * (vertex.x() - anchor_2.x())) < |
279 ((vertex.y() - anchor_2.y()) * (x - anchor_2.x())); | 289 ((vertex.y() - anchor_2.y()) * (x - anchor_2.x())); |
280 bool within_bound_3 = | 290 bool within_bound_3 = |
281 (direction == kUp && y < center_y) || | 291 (direction == kUp && y < center_y) || |
282 (direction == kDown && y > center_y) || | 292 (direction == kDown && y > center_y) || |
283 (direction == kLeft && x < center_x) || | 293 (direction == kLeft && x < center_x) || |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 | 330 |
321 namespace pp { | 331 namespace pp { |
322 | 332 |
323 // Factory function for your specialization of the Module object. | 333 // Factory function for your specialization of the Module object. |
324 Module* CreateModule() { | 334 Module* CreateModule() { |
325 return new MouseLockModule(); | 335 return new MouseLockModule(); |
326 } | 336 } |
327 | 337 |
328 } // namespace pp | 338 } // namespace pp |
329 | 339 |
OLD | NEW |