source: GTP/trunk/App/Demos/Geom/OgreStuff/include/CEGUI/CEGUIUDim.h @ 1812

Revision 1812, 7.2 KB checked in by gumbau, 18 years ago (diff)
Line 
1/************************************************************************
2    filename:   CEGUIUDim.h
3    created:    Tue May 31 2005
4    author:     Paul D Turner <paul@cegui.org.uk>
5*************************************************************************/
6/*************************************************************************
7    Crazy Eddie's GUI System (http://www.cegui.org.uk)
8    Copyright (C)2004 - 2005 Paul D Turner (paul@cegui.org.uk)
9 
10    This library is free software; you can redistribute it and/or
11    modify it under the terms of the GNU Lesser General Public
12    License as published by the Free Software Foundation; either
13    version 2.1 of the License, or (at your option) any later version.
14 
15    This library is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    Lesser General Public License for more details.
19 
20    You should have received a copy of the GNU Lesser General Public
21    License along with this library; if not, write to the Free Software
22    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23*************************************************************************/
24#ifndef _CEGUIUDim_h_
25#define _CEGUIUDim_h_
26
27#include "CEGUIRect.h"
28#include "CEGUIVector.h"
29
30// some macros to aid in the creation of UDims
31#define cegui_absdim(x)     UDim(0,(x))
32#define cegui_reldim(x)     UDim((x),0)
33
34
35// Start of CEGUI namespace section
36namespace CEGUI
37{
38    /*!
39    \brief
40        Class representing a unified dimension; that is a dimension that has both
41        a relative 'scale' portion and and absolute 'offset' portion.
42    */
43    class CEGUIEXPORT UDim
44    {
45    public:
46        UDim() {}
47        UDim(float scale, float offset) : d_scale(scale), d_offset(offset) {}
48        ~UDim() {}
49
50        float asAbsolute(float base) const    { return PixelAligned(base * d_scale) + d_offset; }
51        float asRelative(float base) const    { return (base != 0.0f) ? d_offset / base + d_scale : 0.0f; }
52
53        UDim operator+(const UDim& other) const     { return UDim(d_scale + other.d_scale, d_offset + other.d_offset); }
54        UDim operator-(const UDim& other) const     { return UDim(d_scale - other.d_scale, d_offset - other.d_offset); }
55        UDim operator/(const UDim& other) const     { return UDim(d_scale / other.d_scale, d_offset / other.d_offset); }
56        UDim operator*(const UDim& other) const     { return UDim(d_scale * other.d_scale, d_offset * other.d_offset); }
57
58        const UDim& operator+=(const UDim& other)   { d_scale += other.d_scale; d_offset += other.d_offset; return *this; }
59        const UDim& operator-=(const UDim& other)   { d_scale -= other.d_scale; d_offset -= other.d_offset; return *this; }
60        const UDim& operator/=(const UDim& other)   { d_scale /= other.d_scale; d_offset /= other.d_offset; return *this; }
61        const UDim& operator*=(const UDim& other)   { d_scale *= other.d_scale; d_offset *= other.d_offset; return *this; }
62
63        bool operator==(const UDim& other) const    { return d_scale == other.d_scale && d_offset == other.d_offset; }
64        bool operator!=(const UDim& other) const    { return !operator==(other); }
65
66        float d_scale, d_offset;
67    };
68
69    /*!
70    \brief
71        Two dimensional vector class built using unified dimensions (UDims).
72        The UVector2 class is used for representing both positions and sizes.
73    */
74    class CEGUIEXPORT UVector2
75    {
76    public:
77        UVector2() {}
78        UVector2(const UDim& x, const UDim& y) : d_x(x), d_y(y) {}
79        ~UVector2() {}
80
81        Vector2 asAbsolute(const Size& base) const    { return Vector2(d_x.asAbsolute(base.d_width), d_y.asAbsolute(base.d_height)); }
82        Vector2 asRelative(const Size& base) const    { return Vector2(d_x.asRelative(base.d_width), d_y.asRelative(base.d_height)); }
83
84        UVector2 operator+(const UVector2& other) const     { return UVector2(d_x + other.d_x, d_y + other.d_y); }
85        UVector2 operator-(const UVector2& other) const     { return UVector2(d_x - other.d_x, d_y - other.d_y); }
86        UVector2 operator/(const UVector2& other) const     { return UVector2(d_x / other.d_x, d_y / other.d_y); }
87        UVector2 operator*(const UVector2& other) const     { return UVector2(d_x * other.d_x, d_y * other.d_y); }
88
89        const UVector2& operator+=(const UVector2& other)   { d_x += other.d_x; d_y += other.d_y; return *this; }
90        const UVector2& operator-=(const UVector2& other)   { d_x -= other.d_x; d_y -= other.d_y; return *this; }
91        const UVector2& operator/=(const UVector2& other)   { d_x /= other.d_x; d_y /= other.d_y; return *this; }
92        const UVector2& operator*=(const UVector2& other)   { d_x *= other.d_x; d_y *= other.d_y; return *this; }
93       
94        bool operator==(const UVector2& other) const    { return d_x == other.d_x && d_y == other.d_y; }
95        bool operator!=(const UVector2& other) const    { return !operator==(other); }
96
97        UDim d_x, d_y;
98    };
99
100    /*!
101    \brief
102        Area rectangle class built using unified dimensions (UDims).
103    */
104    class CEGUIEXPORT URect
105    {
106    public:
107        URect() {}
108       
109        URect(const UVector2& min, const UVector2& max) : d_min(min), d_max(max) {}
110       
111        URect(const UDim& left, const UDim& top, const UDim& right, const UDim& bottom)
112        {
113            d_min.d_x = left;
114            d_min.d_y = top;
115            d_max.d_x = right;
116            d_max.d_y = bottom;
117        }
118       
119        ~URect() {}
120   
121        Rect asAbsolute(const Size& base) const
122        {
123            return Rect(
124                    d_min.d_x.asAbsolute(base.d_width),
125                    d_min.d_y.asAbsolute(base.d_height),
126                    d_max.d_x.asAbsolute(base.d_width),
127                    d_max.d_y.asAbsolute(base.d_height)
128                );
129        }
130
131        Rect asRelative(const Size& base) const
132        {
133            return Rect(
134                    d_min.d_x.asRelative(base.d_width),
135                    d_min.d_y.asRelative(base.d_height),
136                    d_max.d_x.asRelative(base.d_width),
137                    d_max.d_y.asRelative(base.d_height)
138                );
139        }
140
141        const UVector2& getPosition() const     { return d_min; }
142        UVector2 getSize() const                { return d_max - d_min; }
143        UDim getWidth() const                   { return d_max.d_x - d_min.d_x; }
144        UDim getHeight() const                  { return d_max.d_y - d_min.d_y; }
145
146        void setPosition(const UVector2& pos)
147        {
148            UVector2 sz(d_max - d_min);
149            d_min = pos;
150            d_max = d_min + sz;
151        }
152
153        void setSize(const UVector2& sz)
154        {
155            d_max = d_min + sz;
156        }
157
158        void setWidth(const UDim& w)        { d_max.d_x = d_min.d_x + w; }
159        void setHeight(const UDim& h)       { d_max.d_y = d_min.d_y + h; }
160
161        void offset(const UVector2& sz)
162        {
163            d_min += sz;
164            d_max += sz;
165        }
166       
167        UVector2 d_min, d_max;
168    };
169
170} // End of  CEGUI namespace section
171
172
173#endif  // end of guard _CEGUIUDim_h_
Note: See TracBrowser for help on using the repository browser.