MyGUI  3.2.1
MyGUI_CoordConverter.h
Go to the documentation of this file.
00001 /*
00002  * This source file is part of MyGUI. For the latest info, see http://mygui.info/
00003  * Distributed under the MIT License
00004  * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
00005  */
00006 
00007 #ifndef __MYGUI_COORD_CONVERTER_H__
00008 #define __MYGUI_COORD_CONVERTER_H__
00009 
00010 #include "MyGUI_Prerequest.h"
00011 #include "MyGUI_Types.h"
00012 
00013 namespace MyGUI
00014 {
00015 
00016     class MYGUI_EXPORT CoordConverter
00017     {
00018     public:
00020         static FloatRect convertTextureCoord(const IntCoord& _coord, const IntSize& _textureSize)
00021         {
00022             if (!_textureSize.width || !_textureSize.height) return FloatRect();
00023             return FloatRect(
00024                 (float)_coord.left / (float)_textureSize.width,
00025                 (float)_coord.top / (float)_textureSize.height,
00026                 (float)_coord.right() / (float)_textureSize.width,
00027                 (float)_coord.bottom() / (float)_textureSize.height);
00028         }
00029 
00030         /* Convert from relative to pixel coordinates.
00031             @param _coord relative coordinates.
00032         */
00033         static IntCoord convertFromRelative(const FloatCoord& _coord, const IntSize& _view)
00034         {
00035             return IntCoord(int(_coord.left * _view.width), int(_coord.top * _view.height), int(_coord.width * _view.width), int(_coord.height * _view.height));
00036         }
00037 
00038         /* Convert from relative to pixel coordinates.
00039             @param _coord relative coordinates.
00040         */
00041         static IntSize convertFromRelative(const FloatSize& _size, const IntSize& _view)
00042         {
00043             return IntSize(int(_size.width * _view.width), int(_size.height * _view.height));
00044         }
00045 
00046         /* Convert from relative to pixel coordinates.
00047             @param _coord relative coordinates.
00048         */
00049         static IntPoint convertFromRelative(const FloatPoint& _point, const IntSize& _view)
00050         {
00051             return IntPoint(int(_point.left * _view.width), int(_point.top * _view.height));
00052         }
00053 
00054         /* Convert from pixel to relative coordinates.
00055             @param _coord pixel coordinates.
00056         */
00057         static FloatCoord convertToRelative(const IntCoord& _coord, const IntSize& _view)
00058         {
00059             return FloatCoord(_coord.left / (float)_view.width, _coord.top / (float)_view.height, _coord.width / (float)_view.width, _coord.height / (float)_view.height);
00060         }
00061 
00062         static FloatSize convertToRelative(const IntSize& _size, const IntSize& _view)
00063         {
00064             return FloatSize(_size.width / (float)_view.width, _size.height / (float)_view.height);
00065         }
00066 
00067         static FloatPoint convertToRelative(const IntPoint& _point, const IntSize& _view)
00068         {
00069             return FloatPoint(_point.left / (float)_view.width, _point.top / (float)_view.height);
00070         }
00071     };
00072 
00073 } // namespace MyGUI
00074 
00075 #endif // __MYGUI_COORD_CONVERTER_H__