~hikiko/nux/arb-srgba-shader

« back to all changes in this revision

Viewing changes to NuxCore/Rect.cpp

  • Committer: Neil Jagdish Patel
  • Date: 2010-09-01 19:25:37 UTC
  • Revision ID: neil.patel@canonical.com-20100901192537-mfz7rm6q262pewg6
Import and build NuxCore

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "NKernel.h"
 
2
#include "Point.h"
 
3
#include "Size.h"
 
4
#include "Rect.h"
 
5
//#include "Utility.h"
 
6
 
 
7
//#pragma warning(disable: 4018)
 
8
template <class T> static const T& Min (const T& a, const T& b){ return (a<b) ? a : b;}
 
9
template <class T> static const T& Max (const T& a, const T& b){ return (a>b) ? a : b;}
 
10
 
 
11
NAMESPACE_BEGIN
 
12
 
 
13
 
 
14
//----------------------------------------------------------------------------
 
15
Rect::Rect()
 
16
{
 
17
    x = 0;
 
18
    y = 0;
 
19
    width = 0;
 
20
    height = 0;
 
21
 
 
22
}
 
23
 
 
24
//----------------------------------------------------------------------------
 
25
Rect::Rect(t_int32 x_, t_int32 y_, t_int32 width_, t_int32 height_)
 
26
{
 
27
    x = x_;
 
28
    y = y_;
 
29
    width = Max<int>(0, int(width_));
 
30
    height = Max<int>(0, int(height_));
 
31
}
 
32
 
 
33
//----------------------------------------------------------------------------
 
34
Rect::~Rect()
 
35
{
 
36
 
 
37
}
 
38
 
 
39
//----------------------------------------------------------------------------
 
40
Rect::Rect(const Rect& r)
 
41
{
 
42
    x = r.x;
 
43
    y = r.y;
 
44
    width = r.width;
 
45
    height = r.height;
 
46
}
 
47
 
 
48
//----------------------------------------------------------------------------
 
49
Rect& Rect::operator = (const Rect& r)
 
50
{
 
51
    if(&r == this)
 
52
        return *this;
 
53
    x = r.x;
 
54
    y = r.y;
 
55
    width = r.width;
 
56
    height = r.height;
 
57
    return *this;
 
58
}
 
59
 
 
60
//----------------------------------------------------------------------------
 
61
t_bool Rect::operator == (const Rect& r) const
 
62
{
 
63
    if((x == r.x) && (y == r.y) && (width == r.width) && (height == r.height))
 
64
    {
 
65
        return true;
 
66
    }
 
67
    return false;
 
68
}
 
69
 
 
70
//----------------------------------------------------------------------------
 
71
t_bool Rect::operator != (const Rect& r) const
 
72
{
 
73
    if((x == r.x) && (y == r.y) && (width == r.width) && (height == r.height))
 
74
    {
 
75
        return false;
 
76
    }
 
77
    return true;
 
78
}
 
79
 
 
80
void Rect::Set(t_int32 px, t_int32 py, t_int32 w, t_int32 h)
 
81
{
 
82
    x = px; y = py; width = w; height = h;
 
83
}
 
84
 
 
85
void Rect::SetPosition(t_int32 px, t_int32 py)
 
86
{
 
87
    x = px; y = py;
 
88
}
 
89
 
 
90
void Rect::SetSize(t_int32 w, t_int32 h)
 
91
{
 
92
    width = w; height = h;
 
93
}
 
94
 
 
95
//----------------------------------------------------------------------------
 
96
t_bool Rect::IsInside(const Point& p) const
 
97
{
 
98
    return ((x <= p.x) && (x + width > p.x) &&
 
99
            (y <= p.y) && (y + height > p.y));
 
100
}
 
101
 
 
102
t_bool Rect::IsPointInside(int x_, int y_) const
 
103
{
 
104
    return ((x <= x_) && (x + width > x_) &&
 
105
        (y <= y_) && (y + height > y_));
 
106
}
 
107
 
 
108
//----------------------------------------------------------------------------
 
109
Rect Rect::Intersect(const Rect& r) const
 
110
{
 
111
    // Get the corner points.
 
112
    const Point& ul1 = Point(x, y);
 
113
    const Point& ul2 = Point(r.x, r.y);
 
114
    int x = Max<int>(ul1.x, ul2.x);
 
115
    int y = Max<int>(ul1.y, ul2.y);
 
116
    int w = Min<int>(ul1.x + width,  ul2.x + r.width) - x;
 
117
    int h = Min<int>(ul1.y + height, ul2.y + r.height) - y;
 
118
    
 
119
    if(w < 0 || h < 0)
 
120
    {
 
121
        // No intersection
 
122
        return Rect ();
 
123
    }
 
124
    return Rect(x, y, w, h);
 
125
}
 
126
 
 
127
//----------------------------------------------------------------------------
 
128
// expand the width by factor_x and the height by factor_y 
 
129
void Rect::Expand(t_int32 dx, t_int32 dy)
 
130
{
 
131
    if(!IsNull()) 
 
132
    {
 
133
        x -= dx;
 
134
        y -= dy;
 
135
        width  += 2*dx;
 
136
        height += 2*dy;
 
137
    }
 
138
}
 
139
 
 
140
//----------------------------------------------------------------------------
 
141
// expand the width by factor_x and the height by factor_y 
 
142
Rect Rect::GetExpand(t_int32 dx, t_int32 dy) const
 
143
{
 
144
    Rect r = Rect(x - dx, y - dy, width + 2*dx, height + 2*dy);
 
145
    if(r.IsNull())
 
146
    {
 
147
        return Rect(0, 0, 0, 0);
 
148
    }
 
149
    return r;
 
150
}
 
151
 
 
152
NAMESPACE_END
 
153