Pepper_56_C++_interfaces
Pepper_56_C++_interfaces
 All Classes Namespaces Files Functions Typedefs Enumerations Macros Groups
rect.h
Go to the documentation of this file.
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 #ifndef PPAPI_CPP_RECT_H_
6 #define PPAPI_CPP_RECT_H_
7 
8 #include <stdint.h>
9 
10 #include "ppapi/c/pp_rect.h"
11 #include "ppapi/cpp/point.h"
12 #include "ppapi/cpp/size.h"
13 
16 
17 namespace pp {
18 
21 class Rect {
22  public:
23 
26  Rect() {
27  rect_.point.x = 0;
28  rect_.point.y = 0;
29  rect_.size.width = 0;
30  rect_.size.height = 0;
31  }
32 
38  Rect(const PP_Rect& rect) { // Implicit.
39  set_x(rect.point.x);
40  set_y(rect.point.y);
41  set_width(rect.size.width);
42  set_height(rect.size.height);
43  }
44 
51  Rect(int32_t w, int32_t h) {
52  set_x(0);
53  set_y(0);
54  set_width(w);
55  set_height(h);
56  }
57 
66  Rect(int32_t x, int32_t y, int32_t w, int32_t h) {
67  set_x(x);
68  set_y(y);
69  set_width(w);
70  set_height(h);
71  }
72 
78  explicit Rect(const Size& s) {
79  set_x(0);
80  set_y(0);
81  set_size(s);
82  }
83 
92  Rect(const Point& origin, const Size& size) {
93  set_point(origin);
94  set_size(size);
95  }
96 
98  ~Rect() {
99  }
100 
105  operator PP_Rect() const {
106  return rect_;
107  }
108 
112  const PP_Rect& pp_rect() const {
113  return rect_;
114  }
115 
119  PP_Rect& pp_rect() {
120  return rect_;
121  }
122 
123 
127  int32_t x() const {
128  return rect_.point.x;
129  }
130 
134  void set_x(int32_t in_x) {
135  rect_.point.x = in_x;
136  }
137 
141  int32_t y() const {
142  return rect_.point.y;
143  }
144 
148  void set_y(int32_t in_y) {
149  rect_.point.y = in_y;
150  }
151 
155  int32_t width() const {
156  return rect_.size.width;
157  }
158 
162  void set_width(int32_t w) {
163  if (w < 0) {
164  PP_DCHECK(w >= 0);
165  w = 0;
166  }
167  rect_.size.width = w;
168  }
169 
173  int32_t height() const {
174  return rect_.size.height;
175  }
176 
180  void set_height(int32_t h) {
181  if (h < 0) {
182  PP_DCHECK(h >= 0);
183  h = 0;
184  }
185  rect_.size.height = h;
186  }
187 
191  Point point() const {
192  return Point(rect_.point);
193  }
194 
199  void set_point(const Point& origin) {
200  rect_.point = origin;
201  }
202 
206  Size size() const {
207  return Size(rect_.size);
208  }
209 
214  void set_size(const Size& s) {
215  rect_.size.width = s.width();
216  rect_.size.height = s.height();
217  }
218 
226  int32_t right() const {
227  return x() + width();
228  }
229 
237  int32_t bottom() const {
238  return y() + height();
239  }
240 
247  void SetRect(int32_t x, int32_t y, int32_t w, int32_t h) {
248  set_x(x);
249  set_y(y);
250  set_width(w);
251  set_height(h);
252  }
253 
257  void SetRect(const PP_Rect& rect) {
258  rect_ = rect;
259  }
260 
268  void Inset(int32_t horizontal, int32_t vertical) {
269  Inset(horizontal, vertical, horizontal, vertical);
270  }
271 
283  void Inset(int32_t left, int32_t top, int32_t right, int32_t bottom);
284 
291  void Offset(int32_t horizontal, int32_t vertical);
292 
297  void Offset(const Point& point) {
298  Offset(point.x(), point.y());
299  }
300 
305  bool IsEmpty() const {
306  return rect_.size.width == 0 || rect_.size.height == 0;
307  }
308 
317  bool Contains(int32_t point_x, int32_t point_y) const;
318 
325  bool Contains(const Point& point) const {
326  return Contains(point.x(), point.y());
327  }
328 
334  bool Contains(const Rect& rect) const;
335 
342  bool Intersects(const Rect& rect) const;
343 
350  Rect Intersect(const Rect& rect) const;
351 
358  Rect Union(const Rect& rect) const;
359 
369  Rect Subtract(const Rect& rect) const;
370 
381  Rect AdjustToFit(const Rect& rect) const;
382 
386  Point CenterPoint() const;
387 
395  bool SharesEdgeWith(const Rect& rect) const;
396 
397  private:
398  PP_Rect rect_;
399 };
400 
403 class FloatRect {
404  public:
405 
409  rect_.point.x = 0.0f;
410  rect_.point.y = 0.0f;
411  rect_.size.width = 0.0f;
412  rect_.size.height = 0.0f;
413  }
414 
420  FloatRect(const PP_FloatRect& rect) { // Implicit.
421  set_x(rect.point.x);
422  set_y(rect.point.y);
423  set_width(rect.size.width);
424  set_height(rect.size.height);
425  }
426 
433  FloatRect(float w, float h) {
434  set_x(0);
435  set_y(0);
436  set_width(w);
437  set_height(h);
438  }
439 
448  FloatRect(float x, float y, float w, float h) {
449  set_x(x);
450  set_y(y);
451  set_width(w);
452  set_height(h);
453  }
454 
460  explicit FloatRect(const FloatSize& s) {
461  set_x(0);
462  set_y(0);
463  set_size(s);
464  }
465 
474  FloatRect(const FloatPoint& origin, const FloatSize& size) {
475  set_point(origin);
476  set_size(size);
477  }
478 
481  }
482 
487  operator PP_FloatRect() const {
488  return rect_;
489  }
490 
496  const PP_FloatRect& pp_float_rect() const {
497  return rect_;
498  }
499 
504  PP_FloatRect& pp_float_rect() {
505  return rect_;
506  }
507 
508 
512  float x() const {
513  return rect_.point.x;
514  }
515 
519  void set_x(float in_x) {
520  rect_.point.x = in_x;
521  }
522 
526  float y() const {
527  return rect_.point.y;
528  }
529 
533  void set_y(float in_y) {
534  rect_.point.y = in_y;
535  }
536 
540  float width() const {
541  return rect_.size.width;
542  }
543 
547  void set_width(float w) {
548  if (w < 0.0f) {
549  PP_DCHECK(w >= 0.0f);
550  w = 0.0f;
551  }
552  rect_.size.width = w;
553  }
554 
558  float height() const {
559  return rect_.size.height;
560  }
561 
565  void set_height(float h) {
566  if (h < 0.0f) {
567  PP_DCHECK(h >= 0.0f);
568  h = 0.0f;
569  }
570  rect_.size.height = h;
571  }
572 
576  FloatPoint point() const {
577  return FloatPoint(rect_.point);
578  }
579 
584  void set_point(const FloatPoint& origin) {
585  rect_.point = origin;
586  }
587 
592  return FloatSize(rect_.size);
593  }
594 
599  void set_size(const FloatSize& s) {
600  rect_.size.width = s.width();
601  rect_.size.height = s.height();
602  }
603 
611  float right() const {
612  return x() + width();
613  }
614 
622  float bottom() const {
623  return y() + height();
624  }
625 
632  void SetRect(float x, float y, float w, float h) {
633  set_x(x);
634  set_y(y);
635  set_width(w);
636  set_height(h);
637  }
638 
642  void SetRect(const PP_FloatRect& rect) {
643  rect_ = rect;
644  }
645 
653  void Inset(float horizontal, float vertical) {
654  Inset(horizontal, vertical, horizontal, vertical);
655  }
656 
668  void Inset(float left, float top, float right, float bottom);
669 
676  void Offset(float horizontal, float vertical);
677 
682  void Offset(const FloatPoint& point) {
683  Offset(point.x(), point.y());
684  }
685 
690  bool IsEmpty() const {
691  return rect_.size.width == 0.0f || rect_.size.height == 0.0f;
692  }
693 
702  bool Contains(float point_x, float point_y) const;
703 
710  bool Contains(const FloatPoint& point) const {
711  return Contains(point.x(), point.y());
712  }
713 
719  bool Contains(const FloatRect& rect) const;
720 
727  bool Intersects(const FloatRect& rect) const;
728 
735  FloatRect Intersect(const FloatRect& rect) const;
736 
743  FloatRect Union(const FloatRect& rect) const;
744 
754  FloatRect Subtract(const FloatRect& rect) const;
755 
766  FloatRect AdjustToFit(const FloatRect& rect) const;
767 
772  FloatPoint CenterPoint() const;
773 
781  bool SharesEdgeWith(const FloatRect& rect) const;
782 
783  private:
784  PP_FloatRect rect_;
785 };
786 
787 } // namespace pp
788 
796 inline bool operator==(const pp::Rect& lhs, const pp::Rect& rhs) {
797  return lhs.x() == rhs.x() &&
798  lhs.y() == rhs.y() &&
799  lhs.width() == rhs.width() &&
800  lhs.height() == rhs.height();
801 }
802 
810 inline bool operator!=(const pp::Rect& lhs, const pp::Rect& rhs) {
811  return !(lhs == rhs);
812 }
813 
823 inline bool operator==(const pp::FloatRect& lhs, const pp::FloatRect& rhs) {
824  return lhs.x() == rhs.x() &&
825  lhs.y() == rhs.y() &&
826  lhs.width() == rhs.width() &&
827  lhs.height() == rhs.height();
828 }
829 
838 inline bool operator!=(const pp::FloatRect& lhs, const pp::FloatRect& rhs) {
839  return !(lhs == rhs);
840 }
841 
842 #endif
843 
Definition: rect.h:403
float height() const
Definition: rect.h:558
bool Intersects(const FloatRect &rect) const
float x() const
Definition: rect.h:512
Rect(int32_t x, int32_t y, int32_t w, int32_t h)
Definition: rect.h:66
PP_Rect & pp_rect()
Definition: rect.h:119
Point CenterPoint() const
int32_t x() const
Definition: rect.h:127
float bottom() const
Definition: rect.h:622
float right() const
Definition: rect.h:611
void Inset(float horizontal, float vertical)
Definition: rect.h:653
float y() const
Definition: rect.h:526
Rect Union(const Rect &rect) const
void set_height(float h)
Definition: rect.h:565
Rect()
Definition: rect.h:26
Rect Subtract(const Rect &rect) const
void SetRect(int32_t x, int32_t y, int32_t w, int32_t h)
Definition: rect.h:247
FloatPoint CenterPoint() const
A size of an object based on width and height.
Definition: size.h:149
#define PP_DCHECK(a)
Definition: logging.h:16
A 2 dimensional point with 0,0 being the upper-left starting coordinate.
Definition: point.h:18
int32_t y() const
Definition: rect.h:141
Definition: rect.h:21
void SetRect(float x, float y, float w, float h)
Definition: rect.h:632
Rect(const Size &s)
Definition: rect.h:78
bool Intersects(const Rect &rect) const
int32_t y() const
Definition: point.h:86
bool IsEmpty() const
Definition: rect.h:305
FloatPoint point() const
Definition: rect.h:576
void Offset(const FloatPoint &point)
Definition: rect.h:682
Rect(const PP_Rect &rect)
Definition: rect.h:38
Rect AdjustToFit(const Rect &rect) const
FloatRect(const PP_FloatRect &rect)
Definition: rect.h:420
void set_y(float in_y)
Definition: rect.h:533
void set_size(const Size &s)
Definition: rect.h:214
void set_x(float in_x)
Definition: rect.h:519
void SetRect(const PP_FloatRect &rect)
Definition: rect.h:642
PP_FloatRect & pp_float_rect()
Definition: rect.h:504
void set_y(int32_t in_y)
Definition: rect.h:148
int height() const
Definition: size.h:96
void Offset(float horizontal, float vertical)
float width() const
Definition: rect.h:540
~Rect()
Destructor.
Definition: rect.h:98
float y() const
Definition: point.h:225
A size of an object based on width and height.
Definition: size.h:18
int32_t right() const
Definition: rect.h:226
Rect Intersect(const Rect &rect) const
int32_t height() const
Definition: rect.h:173
Point point() const
Definition: rect.h:191
FloatSize Floatsize() const
Definition: rect.h:591
Rect(const Point &origin, const Size &size)
Definition: rect.h:92
int32_t x() const
Definition: point.h:74
FloatRect(float x, float y, float w, float h)
Definition: rect.h:448
void set_x(int32_t in_x)
Definition: rect.h:134
FloatRect Intersect(const FloatRect &rect) const
void Offset(const Point &point)
Definition: rect.h:297
bool Contains(const Point &point) const
Definition: rect.h:325
float x() const
Definition: point.h:213
bool operator!=(const pp::Rect &lhs, const pp::Rect &rhs)
Definition: rect.h:810
Size size() const
Definition: rect.h:206
bool IsEmpty() const
Definition: rect.h:690
Definition: point.h:157
FloatRect(float w, float h)
Definition: rect.h:433
Rect(int32_t w, int32_t h)
Definition: rect.h:51
~FloatRect()
Destructor.
Definition: rect.h:480
int width() const
Definition: size.h:78
FloatRect()
Definition: rect.h:408
void Inset(int32_t horizontal, int32_t vertical)
Definition: rect.h:268
bool Contains(const FloatPoint &point) const
Definition: rect.h:710
void SetRect(const PP_Rect &rect)
Definition: rect.h:257
bool Contains(int32_t point_x, int32_t point_y) const
void set_point(const Point &origin)
Definition: rect.h:199
FloatRect AdjustToFit(const FloatRect &rect) const
bool operator==(const pp::Rect &lhs, const pp::Rect &rhs)
Definition: rect.h:796
float width() const
Definition: size.h:212
FloatRect Subtract(const FloatRect &rect) const
float height() const
Definition: size.h:230
FloatRect(const FloatSize &s)
Definition: rect.h:460
FloatRect(const FloatPoint &origin, const FloatSize &size)
Definition: rect.h:474
void set_point(const FloatPoint &origin)
Definition: rect.h:584
bool SharesEdgeWith(const FloatRect &rect) const
void set_width(int32_t w)
Definition: rect.h:162
bool SharesEdgeWith(const Rect &rect) const
const PP_Rect & pp_rect() const
Definition: rect.h:112
bool Contains(float point_x, float point_y) const
void set_width(float w)
Definition: rect.h:547
void set_size(const FloatSize &s)
Definition: rect.h:599
void Offset(int32_t horizontal, int32_t vertical)
int32_t width() const
Definition: rect.h:155
const PP_FloatRect & pp_float_rect() const
Definition: rect.h:496
FloatRect Union(const FloatRect &rect) const
int32_t bottom() const
Definition: rect.h:237
void set_height(int32_t h)
Definition: rect.h:180