wg_rect.h
来自「一个小巧的嵌入式图形系统wGUI, 可以用VC编译」· C头文件 代码 · 共 197 行
H
197 行
// wg_rect.h//// CRect class interface////// Copyright (c) 2002 Rob Wiskow// rob-dev@boxedchaos.com//// This library is free software; you can redistribute it and/or// modify it under the terms of the GNU Lesser General Public// License as published by the Free Software Foundation; either// version 2.1 of the License, or (at your option) any later version.//// This library is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU// Lesser General Public License for more details.//// You should have received a copy of the GNU Lesser General Public// License along with this library; if not, write to the Free Software// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA//#ifndef _WG_RECT_H_#define _WG_RECT_H_#include "wg_point.h"#include "SDL.h"#include <stdlib.h>#include <math.h>namespace wGui{//! A representation of a rectangleclass CRect{public: //! The default constructor will initialize all 4 corners to (0, 0) CRect() : m_Left(0), m_Right(0), m_Top(0), m_Bottom(0) { } //! \param left The left edge of the rectangle //! \param top The top edge of the rectangle //! \param right The right edge of the rectangle //! \param bottom The bottom edge of the rectangle CRect(const int left, const int top, const int right, const int bottom) : m_Left(left), m_Right(right), m_Top(top), m_Bottom(bottom) { } // constructor //! Create a CRect using a pair of CPoints that represent the Top-Left, and Bottom-Right corners //! \param p1 Top left corner //! \param p2 Bottom right corner CRect(const CPoint& p1, const CPoint& p2) : m_Left(p1.XPos()), m_Right(p2.XPos()), m_Top(p1.YPos()), m_Bottom(p2.YPos()) { } // constructor //! Copy constructor //! \param r A CRect that thie new CRect will be copied from CRect(const CRect& r) : m_Left(r.m_Left), m_Right(r.m_Right), m_Top(r.m_Top), m_Bottom(r.m_Bottom) { } // constructor //! Standard Destructor virtual ~CRect() { } //! Set the Top poisition //! \param top The new Top coordinate void SetTop(const int top) { m_Top = top; } //! Set the Left poisition //! \param left The new Left coordinate void SetLeft(const int left) { m_Left = left; } //! Set the Right poisition //! \param right The new Right coordinate void SetRight(const int right) { m_Right = right; } //! Set the Bottom poisition //! \param bottom The new Bottom coordinate void SetBottom(const int bottom) { m_Bottom = bottom; } //! \return The Top position int Top(void) const { return m_Top; } //! \return The Left position int Left(void) const { return m_Left; } //! \return The Right position int Right(void) const { return m_Right; } //! \return The Bottom position int Bottom(void) const { return m_Bottom; } //! \return A point representing the Top Left corner of the CRect CPoint TopLeft(void) const { return CPoint(m_Left, m_Top); } //! \return A point representing the Top Right corner of the CRect CPoint TopRight(void) const { return CPoint(m_Right, m_Top); } //! \return A point representing the Bottom Left corner of the CRect CPoint BottomLeft(void) const { return CPoint(m_Left, m_Bottom); } //! \return A point representing the Bottom Right corner of the CRect CPoint BottomRight(void) const { return CPoint(m_Right, m_Bottom); } //! \return A point representing the center of the CRect CPoint Center(void) const { return CPoint((m_Left + m_Right) / 2, (m_Top + m_Bottom) / 2); } //! \return A point representing the CenterLeft point of the CRect CPoint CenterLeft(void) const { return CPoint( m_Left, (m_Top + m_Bottom) / 2); } //! \return A point representing the CenterTop point of the CRect CPoint CenterTop(void) const { return CPoint( (m_Left + m_Right) / 2, m_Top ); } //! \return A point representing the Bottom Left corner of the CRect CPoint CenterBottom(void) const { return CPoint( (m_Left + m_Right) / 2, m_Bottom ); } //! \return A point representing the Bottom Right corner of the CRect CPoint CenterRight(void) const { return CPoint( m_Right, (m_Top + m_Bottom) / 2); } //! Converts the CRect into a SDL style rect //! \return An SDL_Rect of the same size SDL_Rect SDLRect(void) const; //! \return The width (along the X axis) of the CRect int Width(void) const { return abs(m_Right - m_Left + 1); } //! \return The height (along the Y axis) of the CRect int Height(void) const { return abs(m_Bottom - m_Top + 1); } //! Assignment operator will copy the values of the other rect CRect& operator=(const CRect& r); // assignment operator //! Addition operator to add a CPoint, will offset the CRect //! \param p A point to offset the CRect by CRect operator+(const CPoint& p) const; //! Subtraction operator to subtract a CPoint, will offset the CRect //! \param p A point to offset the CRect by CRect operator-(const CPoint& p) const; //! Grow will increase (or decrease) all of the dimensions by the given amount. //! This means that for a rect 20 wide by 10 tall, Grow(1) will increase the size to 22 wide, 12 tall. //! (each side is moved out by 1) //! \param iGrowAmount The amount to grow the CRect's dimensions by, negative values can be used to shrink the rect //! \return A reference to the object CRect& Grow(int iGrowAmount); //! Move will move the rect by a offset specified //! \param iOffsetX how many pixel to move on X axis ( + or - ) //! \param iOffsetY how many pixel to move on Y axis ( + or - ) //! \return A reference to the object CRect& Move(int iOffsetX, int iOffsetY); //! Tests to see if the two CRects overlap //! \param r The other CRect to test with //! \return true if the CRects overlap bool Overlaps(const CRect& r); //! Clips the CRect to fit in another CRect //! \param r The CRect to clip to void ClipTo(const CRect& r); enum ERelativePosition { RELPOS_ABOVE = 1, //!< The point is above the top of the CRect RELPOS_BELOW = 2, //!< The point is below the bottom of the CRect RELPOS_LEFT = 4, //!< The point is to the left of the CRect RELPOS_RIGHT = 8, //!< The point is to the right of the CRect RELPOS_INSIDE = 16 //!< The point lies within the bounds of the CRect }; //! \brief The HitTest will test to see where a point is in relation to the rect //! \param p The point to test against the CRect //! \return The appropriate values of the ERelativePosition enum are ORed together unsigned int HitTest(const CPoint& p) const;protected: //! X position of the left border int m_Left; //! X position of the right border int m_Right; //! Y position of the top border int m_Top; //! Y position of the bottom border int m_Bottom;};}#endif // _WG_RECT_H_
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?