1 /*---------------------------------------------------------------------------* 2 Project: NintendoWare 3 File: ut_Rect.h 4 5 Copyright (C)2009-2011 Nintendo/HAL Laboratory, Inc. All rights reserved. 6 7 These coded instructions, statements, and computer programs contain proprietary 8 information of Nintendo and/or its licensed developers and are protected by 9 national and international copyright laws. They may not be disclosed to third 10 parties or copied or duplicated in any form, in whole or in part, without the 11 prior written consent of Nintendo. 12 13 The content herein is highly confidential and should be handled accordingly. 14 15 $Revision: $ 16 *---------------------------------------------------------------------------*/ 17 18 #ifndef NW_UT_RECT_H_ 19 #define NW_UT_RECT_H_ 20 21 #include <nw/types.h> 22 //#include <nw/math/arithmetic.h> 23 24 namespace nw { 25 namespace ut { 26 27 28 //-------------------------------------------------------------------------- 29 //! @brief 矩形を表すクラスです。 30 //--------------------------------------------------------------------------- 31 struct Rect 32 { 33 public: 34 /* ------------------------------------------------------------------------ 35 変数 36 ------------------------------------------------------------------------ */ 37 f32 left; //!< 矩形の左座標です。 38 f32 top; //!< 矩形の上座標です。 39 f32 right; //!< 矩形の右座標です。 40 f32 bottom; //!< 矩形の下座標です。 41 42 43 /* ------------------------------------------------------------------------ 44 関数 45 ------------------------------------------------------------------------ */ 46 //---------------------------------------- 47 //! @name コンストラクタ/デストラクタ 48 //@{ 49 50 //-------------------------------------------------------------------------- 51 //! @brief デフォルトコンストラクタです。 52 //--------------------------------------------------------------------------- RectRect53 Rect() 54 : left(0), 55 top(0), 56 right(0), 57 bottom(0) 58 { 59 } 60 61 //-------------------------------------------------------------------------- 62 //! @brief コンストラクタです。 63 //! 64 //! @param[in] l 矩形の左座標です。 65 //! @param[in] t 矩形の上座標です。 66 //! @param[in] r 矩形の右座標です。 67 //! @param[in] b 矩形の下座標です。 68 //--------------------------------------------------------------------------- RectRect69 Rect(f32 l, f32 t, f32 r, f32 b) 70 : left(l), 71 top(t), 72 right(r), 73 bottom(b) 74 { 75 } 76 77 //-------------------------------------------------------------------------- 78 //! @brief コピーコンストラクタです。 79 //--------------------------------------------------------------------------- RectRect80 Rect(const Rect& v) 81 : left(v.left), 82 top(v.top), 83 right(v.right), 84 bottom(v.bottom) 85 { 86 } 87 88 //-------------------------------------------------------------------------- 89 //! @brief デストラクタです。 90 //--------------------------------------------------------------------------- ~RectRect91 ~Rect() {} 92 93 //@} 94 95 //---------------------------------------- 96 //! @name 設定/取得 97 //@{ 98 99 //-------------------------------------------------------------------------- 100 //! @brief 矩形の幅を取得します。 101 //! 102 //! @return 矩形の幅です。 103 //--------------------------------------------------------------------------- GetWidthRect104 f32 GetWidth() const { return right - left; } 105 106 //-------------------------------------------------------------------------- 107 //! @brief 矩形の高さを取得します。 108 //! 109 //! @return 矩形の高さです。 110 //--------------------------------------------------------------------------- GetHeightRect111 f32 GetHeight() const { return bottom - top; } 112 113 //-------------------------------------------------------------------------- 114 //! @brief 矩形の基点となるX座標を取得します。 115 //! 116 //! @return 矩形の基点となるX座標です。 117 //--------------------------------------------------------------------------- GetXRect118 f32 GetX() const { return left; } 119 120 //-------------------------------------------------------------------------- 121 //! @brief 矩形の基点となるY座標を取得します。 122 //! 123 //! @return 矩形の基点となるY座標です。 124 //--------------------------------------------------------------------------- GetYRect125 f32 GetY() const { return top; } 126 127 //-------------------------------------------------------------------------- 128 //! @brief 矩形の幅を設定します。 129 //! 130 //! @param[in] width 矩形の幅です。 131 //--------------------------------------------------------------------------- SetWidthRect132 void SetWidth(f32 width) { right = left + width; } 133 134 //-------------------------------------------------------------------------- 135 //! @brief 矩形の高さを設定します。 136 //! 137 //! @param[in] height 矩形の高さです。 138 //--------------------------------------------------------------------------- SetHeightRect139 void SetHeight(f32 height) { bottom = top + height; } 140 141 //-------------------------------------------------------------------------- 142 //! @brief 矩形の左上座標を移動します。 143 //! 144 //! @param[in] x 矩形の左上座標の x 座標です。 145 //! @param[in] y 矩形の左上座標の y 座標です。 146 //--------------------------------------------------------------------------- MoveToRect147 void MoveTo(f32 x, f32 y) 148 { 149 right = x + GetWidth(); 150 left = x; 151 bottom = y + GetHeight(); 152 top = y; 153 } 154 155 //-------------------------------------------------------------------------- 156 //! @brief 矩形を並行移動します。 157 //! 158 //! @param[in] dx x 方向へ矩形を移動する移動量です。 159 //! @param[in] dy y 方向へ矩形を移動する移動量です。 160 //--------------------------------------------------------------------------- MoveRect161 void Move(f32 dx, f32 dy) 162 { 163 left += dx; 164 right += dx; 165 top += dy; 166 bottom += dy; 167 } 168 169 //-------------------------------------------------------------------------- 170 //! @brief 原点とサイズを設定します。 171 //! 172 //! @param[in] x 矩形の基点となるY座標です。 173 //! @param[in] y 矩形の基点となるX座標です。 174 //! @param[in] width 矩形の幅です。 175 //! @param[in] height 矩形の高さです。 176 //--------------------------------------------------------------------------- SetOriginAndSizeRect177 void SetOriginAndSize(f32 x, f32 y, f32 width, f32 height ) 178 { 179 left = x; 180 right = x + width; 181 top = y; 182 bottom = y + height; 183 } 184 185 //@} 186 187 //-------------------------------------------------------------------------- 188 //! @brief 矩形情報を正常な値に正規化します。 189 //--------------------------------------------------------------------------- NormalizeRect190 void Normalize() 191 { 192 const f32 l = left; 193 const f32 t = top; 194 const f32 r = right; 195 const f32 b = bottom; 196 197 left = (r - l) >= 0 ? l : r; 198 right = (r - l) >= 0 ? r : l; 199 top = (b - t) >= 0 ? t : b; 200 bottom = (b - t) >= 0 ? b : t; 201 } 202 }; 203 204 205 206 } /* namespace ut */ 207 } /* namespace nw */ 208 209 #endif // NW_UT_RECT_H_ 210