Crazy Eddie's GUI System  0.8.5
CoordConverter.h
1 /***********************************************************************
2  created: Sun Sep 18 2005
3  author: Paul D Turner <paul@cegui.org.uk>
4 *************************************************************************/
5 /***************************************************************************
6  * Copyright (C) 2004 - 2006 Paul D Turner & The CEGUI Development Team
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining
9  * a copy of this software and associated documentation files (the
10  * "Software"), to deal in the Software without restriction, including
11  * without limitation the rights to use, copy, modify, merge, publish,
12  * distribute, sublicense, and/or sell copies of the Software, and to
13  * permit persons to whom the Software is furnished to do so, subject to
14  * the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be
17  * included in all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  * OTHER DEALINGS IN THE SOFTWARE.
26  ***************************************************************************/
27 #ifndef _CEGUICoordConverter_h_
28 #define _CEGUICoordConverter_h_
29 
30 #include "CEGUI/UDim.h"
31 #include "CEGUI/Vector.h"
32 #include "CEGUI/Size.h"
33 #include "CEGUI/Rect.h"
34 
35 // Start of CEGUI namespace section
36 namespace CEGUI
37 {
44 class CEGUIEXPORT CoordConverter
45 {
46 public:
62  inline static float alignToPixels(float x)
63  {
64  return (float)(int)(( x ) + (( x ) > 0.0f ? 0.5f : -0.5f));
65  }
66 
71  inline static float asAbsolute(const UDim& u, float base, bool pixelAlign = true)
72  {
73  return pixelAlign ? alignToPixels(base * u.d_scale + u.d_offset) : base * u.d_scale + u.d_offset;
74  }
75 
80  inline static float asRelative(const UDim& u, float base)
81  {
82  return (base != 0.0f) ? u.d_offset / base + u.d_scale : 0.0f;
83  }
84 
89  inline static Vector2f asAbsolute(const Vector2<UDim>& v, const Sizef& base, bool pixelAlign = true)
90  {
91  return Vector2f(asAbsolute(v.d_x, base.d_width, pixelAlign), asAbsolute(v.d_y, base.d_height, pixelAlign));
92  }
93 
98  inline static Vector2f asRelative(const Vector2<UDim>& v, const Sizef& base)
99  {
100  return Vector2f(asRelative(v.d_x, base.d_width), asRelative(v.d_y, base.d_height));
101  }
102 
107  inline static Sizef asAbsolute(const Size<UDim>& v, const Sizef& base, bool pixelAlign = true)
108  {
109  return Sizef(asAbsolute(v.d_width, base.d_width, pixelAlign), asAbsolute(v.d_height, base.d_height, pixelAlign));
110  }
111 
116  inline static Sizef asRelative(const Size<UDim>& v, const Sizef& base)
117  {
118  return Sizef(asRelative(v.d_width, base.d_width), asRelative(v.d_height, base.d_height));
119  }
120 
121  inline static Rectf asAbsolute(const URect& r, const Sizef& base, bool pixelAlign = true)
122  {
123  return Rectf(
124  asAbsolute(r.d_min.d_x, base.d_width, pixelAlign),
125  asAbsolute(r.d_min.d_y, base.d_height, pixelAlign),
126  asAbsolute(r.d_max.d_x, base.d_width, pixelAlign),
127  asAbsolute(r.d_max.d_y, base.d_height, pixelAlign)
128  );
129  }
130 
131  inline static Rectf asRelative(const URect& r, const Sizef& base)
132  {
133  return Rectf(
134  asRelative(r.d_min.d_x, base.d_width),
135  asRelative(r.d_min.d_y, base.d_height),
136  asRelative(r.d_max.d_x, base.d_width),
137  asRelative(r.d_max.d_y, base.d_height)
138  );
139  }
140 
156  static float screenToWindowX(const Window& window, const UDim& x);
157 
173  static float screenToWindowX(const Window& window, const float x);
174 
190  static float screenToWindowY(const Window& window, const UDim& y);
191 
207  static float screenToWindowY(const Window& window, const float y);
208 
224  static Vector2f screenToWindow(const Window& window, const UVector2& vec);
225 
241  static Vector2f screenToWindow(const Window& window, const Vector2f& vec);
242 
257  static Rectf screenToWindow(const Window& window, const URect& rect);
258 
273  static Rectf screenToWindow(const Window& window, const Rectf& rect);
274 
275 private:
277  CoordConverter();
278 
290  static float getBaseXValue(const Window& window);
291 
303  static float getBaseYValue(const Window& window);
304 
316  static Vector2f getBaseValue(const Window& window);
317 };
318 
319 } // End of CEGUI namespace section
320 
321 
322 #endif // end of guard _CEGUICoordConverter_h_
static float asRelative(const UDim &u, float base)
converts given UDim to relative value
Definition: CoordConverter.h:80
static float alignToPixels(float x)
Static method used to return a float value rounded to the nearest integer.
Definition: CoordConverter.h:62
static Vector2f asAbsolute(const Vector2< UDim > &v, const Sizef &base, bool pixelAlign=true)
converts given Vector2 to absolute Vector2f
Definition: CoordConverter.h:89
Main namespace for Crazy Eddie's GUI Library.
Definition: arch_overview.dox:1
static Sizef asAbsolute(const Size< UDim > &v, const Sizef &base, bool pixelAlign=true)
converts given Size to absolute Sizef
Definition: CoordConverter.h:107
static Sizef asRelative(const Size< UDim > &v, const Sizef &base)
converts given Size to relative Sizef
Definition: CoordConverter.h:116
Utility class that helps in converting various types of co-ordinate between absolute screen positions...
Definition: CoordConverter.h:44
Dimension that has both a relative 'scale' portion and and absolute 'offset' portion.
Definition: UDim.h:92
static Vector2f asRelative(const Vector2< UDim > &v, const Sizef &base)
converts given Vector2 to relative Vector2f
Definition: CoordConverter.h:98
static float asAbsolute(const UDim &u, float base, bool pixelAlign=true)
converts given UDim to absolute value
Definition: CoordConverter.h:71