From b50309dd5cf2614f69232f3bc0f634388464d956 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Fri, 8 Jul 2022 17:56:06 +0200 Subject: [PATCH] Move `CUIRect` class into its own files --- CMakeLists.txt | 2 + src/game/client/ui.cpp | 165 ----------------------------------- src/game/client/ui.h | 97 +-------------------- src/game/client/ui_rect.cpp | 168 ++++++++++++++++++++++++++++++++++++ src/game/client/ui_rect.h | 101 ++++++++++++++++++++++ 5 files changed, 273 insertions(+), 260 deletions(-) create mode 100644 src/game/client/ui_rect.cpp create mode 100644 src/game/client/ui_rect.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 97b8728c3..39709e37f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2103,6 +2103,8 @@ if(CLIENT) ui.h ui_ex.cpp ui_ex.h + ui_rect.cpp + ui_rect.h ) set_src(GAME_EDITOR GLOB src/game/editor auto_map.cpp diff --git a/src/game/client/ui.cpp b/src/game/client/ui.cpp index 1866223cf..d7ce4d4a7 100644 --- a/src/game/client/ui.cpp +++ b/src/game/client/ui.cpp @@ -297,171 +297,6 @@ void CUI::UpdateClipping() } } -void CUIRect::HSplitMid(CUIRect *pTop, CUIRect *pBottom, float Spacing) const -{ - CUIRect r = *this; - const float Cut = r.h / 2; - const float HalfSpacing = Spacing / 2; - - if(pTop) - { - pTop->x = r.x; - pTop->y = r.y; - pTop->w = r.w; - pTop->h = Cut - HalfSpacing; - } - - if(pBottom) - { - pBottom->x = r.x; - pBottom->y = r.y + Cut + HalfSpacing; - pBottom->w = r.w; - pBottom->h = r.h - Cut - HalfSpacing; - } -} - -void CUIRect::HSplitTop(float Cut, CUIRect *pTop, CUIRect *pBottom) const -{ - CUIRect r = *this; - - if(pTop) - { - pTop->x = r.x; - pTop->y = r.y; - pTop->w = r.w; - pTop->h = Cut; - } - - if(pBottom) - { - pBottom->x = r.x; - pBottom->y = r.y + Cut; - pBottom->w = r.w; - pBottom->h = r.h - Cut; - } -} - -void CUIRect::HSplitBottom(float Cut, CUIRect *pTop, CUIRect *pBottom) const -{ - CUIRect r = *this; - - if(pTop) - { - pTop->x = r.x; - pTop->y = r.y; - pTop->w = r.w; - pTop->h = r.h - Cut; - } - - if(pBottom) - { - pBottom->x = r.x; - pBottom->y = r.y + r.h - Cut; - pBottom->w = r.w; - pBottom->h = Cut; - } -} - -void CUIRect::VSplitMid(CUIRect *pLeft, CUIRect *pRight, float Spacing) const -{ - CUIRect r = *this; - const float Cut = r.w / 2; - const float HalfSpacing = Spacing / 2; - - if(pLeft) - { - pLeft->x = r.x; - pLeft->y = r.y; - pLeft->w = Cut - HalfSpacing; - pLeft->h = r.h; - } - - if(pRight) - { - pRight->x = r.x + Cut + HalfSpacing; - pRight->y = r.y; - pRight->w = r.w - Cut - HalfSpacing; - pRight->h = r.h; - } -} - -void CUIRect::VSplitLeft(float Cut, CUIRect *pLeft, CUIRect *pRight) const -{ - CUIRect r = *this; - - if(pLeft) - { - pLeft->x = r.x; - pLeft->y = r.y; - pLeft->w = Cut; - pLeft->h = r.h; - } - - if(pRight) - { - pRight->x = r.x + Cut; - pRight->y = r.y; - pRight->w = r.w - Cut; - pRight->h = r.h; - } -} - -void CUIRect::VSplitRight(float Cut, CUIRect *pLeft, CUIRect *pRight) const -{ - CUIRect r = *this; - - if(pLeft) - { - pLeft->x = r.x; - pLeft->y = r.y; - pLeft->w = r.w - Cut; - pLeft->h = r.h; - } - - if(pRight) - { - pRight->x = r.x + r.w - Cut; - pRight->y = r.y; - pRight->w = Cut; - pRight->h = r.h; - } -} - -void CUIRect::Margin(float Cut, CUIRect *pOtherRect) const -{ - CUIRect r = *this; - - pOtherRect->x = r.x + Cut; - pOtherRect->y = r.y + Cut; - pOtherRect->w = r.w - 2 * Cut; - pOtherRect->h = r.h - 2 * Cut; -} - -void CUIRect::VMargin(float Cut, CUIRect *pOtherRect) const -{ - CUIRect r = *this; - - pOtherRect->x = r.x + Cut; - pOtherRect->y = r.y; - pOtherRect->w = r.w - 2 * Cut; - pOtherRect->h = r.h; -} - -void CUIRect::HMargin(float Cut, CUIRect *pOtherRect) const -{ - CUIRect r = *this; - - pOtherRect->x = r.x; - pOtherRect->y = r.y + Cut; - pOtherRect->w = r.w; - pOtherRect->h = r.h - 2 * Cut; -} - -bool CUIRect::Inside(float x_, float y_) const -{ - return x_ >= this->x && x_ < this->x + this->w && y_ >= this->y && y_ < this->y + this->h; -} - int CUI::DoButtonLogic(const void *pID, int Checked, const CUIRect *pRect) { // logic diff --git a/src/game/client/ui.h b/src/game/client/ui.h index 4be2a42f1..cbd5916ff 100644 --- a/src/game/client/ui.h +++ b/src/game/client/ui.h @@ -3,6 +3,8 @@ #ifndef GAME_CLIENT_UI_H #define GAME_CLIENT_UI_H +#include "ui_rect.h" + #include #include @@ -10,101 +12,6 @@ #include #include -class CUIRect -{ -public: - float x, y, w, h; - - /** - * Splits 2 CUIRect inside *this* CUIRect horizontally. You can pass null pointers. - * - * @param pTop This rect will end up taking the top half of this CUIRect. - * @param pBottom This rect will end up taking the bottom half of this CUIRect. - * @param Spacing Total size of margin between split rects. - */ - void HSplitMid(CUIRect *pTop, CUIRect *pBottom, float Spacing = 0.0f) const; - /** - * Splits 2 CUIRect inside *this* CUIRect. - * - * The cut parameter determines the height of the top rect, so it allows more customization than HSplitMid. - * - * This method doesn't check if Cut is bigger than *this* rect height. - * - * @param Cut The height of the pTop rect. - * @param pTop The rect that ends up at the top with a height equal to Cut. - * @param pBottom The rect that ends up at the bottom with a height equal to *this* rect minus the Cut. - */ - void HSplitTop(float Cut, CUIRect *pTop, CUIRect *pBottom) const; - /** - * Splits 2 CUIRect inside *this* CUIRect. - * - * The cut parameter determines the height of the bottom rect, so it allows more customization than HSplitMid. - * - * This method doesn't check if Cut is bigger than *this* rect height. - * - * @param Cut The height of the pBottom rect. - * @param pTop The rect that ends up at the top with a height equal to *this* CUIRect height minus Cut. - * @param pBottom The rect that ends up at the bottom with a height equal to Cut. - */ - void HSplitBottom(float Cut, CUIRect *pTop, CUIRect *pBottom) const; - /** - * Splits 2 CUIRect inside *this* CUIRect vertically. You can pass null pointers. - * - * @param pLeft This rect will take up the left half of *this* CUIRect. - * @param pRight This rect will take up the right half of *this* CUIRect. - * @param Spacing Total size of margin between split rects. - */ - void VSplitMid(CUIRect *pLeft, CUIRect *pRight, float Spacing = 0.0f) const; - /** - * Splits 2 CUIRect inside *this* CUIRect. - * - * The cut parameter determines the width of the left rect, so it allows more customization than VSplitMid. - * - * This method doesn't check if Cut is bigger than *this* rect width. - * - * @param Cut The width of the pLeft rect. - * @param pLeft The rect that ends up at the left with a width equal to Cut. - * @param pRight The rect that ends up at the right with a width equal to *this* rect minus the Cut. - */ - void VSplitLeft(float Cut, CUIRect *pLeft, CUIRect *pRight) const; - /** - * Splits 2 CUIRect inside *this* CUIRect. - * - * The cut parameter determines the width of the right rect, so it allows more customization than VSplitMid. - * - * This method doesn't check if Cut is bigger than *this* rect width. - * - * @param Cut The width of the pRight rect. - * @param pLeft The rect that ends up at the left with a width equal to *this* CUIRect width minus Cut. - * @param pRight The rect that ends up at the right with a width equal to Cut. - */ - void VSplitRight(float Cut, CUIRect *pLeft, CUIRect *pRight) const; - - /** - * Places pOtherRect inside *this* CUIRect with Cut as the margin. - * - * @param Cut The margin. - * @param pOtherRect The CUIRect to place inside *this* CUIRect. - */ - void Margin(float Cut, CUIRect *pOtherRect) const; - /** - * Places pOtherRect inside *this* CUIRect applying Cut as the margin only on the vertical axis. - * - * @param Cut The margin. - * @param pOtherRect The CUIRect to place inside *this* CUIRect - */ - void VMargin(float Cut, CUIRect *pOtherRect) const; - /** - * Places pOtherRect inside *this* CUIRect applying Cut as the margin only on the horizontal axis. - * - * @param Cut The margin. - * @param pOtherRect The CUIRect to place inside *this* CUIRect - */ - void HMargin(float Cut, CUIRect *pOtherRect) const; - - bool Inside(float x_, float y_) const; -}; - struct SUIAnimator { bool m_Active; diff --git a/src/game/client/ui_rect.cpp b/src/game/client/ui_rect.cpp new file mode 100644 index 000000000..b9e143067 --- /dev/null +++ b/src/game/client/ui_rect.cpp @@ -0,0 +1,168 @@ +/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */ +/* If you are missing that file, acquire a complete release at teeworlds.com. */ +#include "ui_rect.h" + +void CUIRect::HSplitMid(CUIRect *pTop, CUIRect *pBottom, float Spacing) const +{ + CUIRect r = *this; + const float Cut = r.h / 2; + const float HalfSpacing = Spacing / 2; + + if(pTop) + { + pTop->x = r.x; + pTop->y = r.y; + pTop->w = r.w; + pTop->h = Cut - HalfSpacing; + } + + if(pBottom) + { + pBottom->x = r.x; + pBottom->y = r.y + Cut + HalfSpacing; + pBottom->w = r.w; + pBottom->h = r.h - Cut - HalfSpacing; + } +} + +void CUIRect::HSplitTop(float Cut, CUIRect *pTop, CUIRect *pBottom) const +{ + CUIRect r = *this; + + if(pTop) + { + pTop->x = r.x; + pTop->y = r.y; + pTop->w = r.w; + pTop->h = Cut; + } + + if(pBottom) + { + pBottom->x = r.x; + pBottom->y = r.y + Cut; + pBottom->w = r.w; + pBottom->h = r.h - Cut; + } +} + +void CUIRect::HSplitBottom(float Cut, CUIRect *pTop, CUIRect *pBottom) const +{ + CUIRect r = *this; + + if(pTop) + { + pTop->x = r.x; + pTop->y = r.y; + pTop->w = r.w; + pTop->h = r.h - Cut; + } + + if(pBottom) + { + pBottom->x = r.x; + pBottom->y = r.y + r.h - Cut; + pBottom->w = r.w; + pBottom->h = Cut; + } +} + +void CUIRect::VSplitMid(CUIRect *pLeft, CUIRect *pRight, float Spacing) const +{ + CUIRect r = *this; + const float Cut = r.w / 2; + const float HalfSpacing = Spacing / 2; + + if(pLeft) + { + pLeft->x = r.x; + pLeft->y = r.y; + pLeft->w = Cut - HalfSpacing; + pLeft->h = r.h; + } + + if(pRight) + { + pRight->x = r.x + Cut + HalfSpacing; + pRight->y = r.y; + pRight->w = r.w - Cut - HalfSpacing; + pRight->h = r.h; + } +} + +void CUIRect::VSplitLeft(float Cut, CUIRect *pLeft, CUIRect *pRight) const +{ + CUIRect r = *this; + + if(pLeft) + { + pLeft->x = r.x; + pLeft->y = r.y; + pLeft->w = Cut; + pLeft->h = r.h; + } + + if(pRight) + { + pRight->x = r.x + Cut; + pRight->y = r.y; + pRight->w = r.w - Cut; + pRight->h = r.h; + } +} + +void CUIRect::VSplitRight(float Cut, CUIRect *pLeft, CUIRect *pRight) const +{ + CUIRect r = *this; + + if(pLeft) + { + pLeft->x = r.x; + pLeft->y = r.y; + pLeft->w = r.w - Cut; + pLeft->h = r.h; + } + + if(pRight) + { + pRight->x = r.x + r.w - Cut; + pRight->y = r.y; + pRight->w = Cut; + pRight->h = r.h; + } +} + +void CUIRect::Margin(float Cut, CUIRect *pOtherRect) const +{ + CUIRect r = *this; + + pOtherRect->x = r.x + Cut; + pOtherRect->y = r.y + Cut; + pOtherRect->w = r.w - 2 * Cut; + pOtherRect->h = r.h - 2 * Cut; +} + +void CUIRect::VMargin(float Cut, CUIRect *pOtherRect) const +{ + CUIRect r = *this; + + pOtherRect->x = r.x + Cut; + pOtherRect->y = r.y; + pOtherRect->w = r.w - 2 * Cut; + pOtherRect->h = r.h; +} + +void CUIRect::HMargin(float Cut, CUIRect *pOtherRect) const +{ + CUIRect r = *this; + + pOtherRect->x = r.x; + pOtherRect->y = r.y + Cut; + pOtherRect->w = r.w; + pOtherRect->h = r.h - 2 * Cut; +} + +bool CUIRect::Inside(float x_, float y_) const +{ + return x_ >= this->x && x_ < this->x + this->w && y_ >= this->y && y_ < this->y + this->h; +} diff --git a/src/game/client/ui_rect.h b/src/game/client/ui_rect.h new file mode 100644 index 000000000..bbb886e5c --- /dev/null +++ b/src/game/client/ui_rect.h @@ -0,0 +1,101 @@ +/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */ +/* If you are missing that file, acquire a complete release at teeworlds.com. */ +#ifndef GAME_CLIENT_UI_RECT_H +#define GAME_CLIENT_UI_RECT_H + +class CUIRect +{ +public: + float x, y, w, h; + + /** + * Splits 2 CUIRect inside *this* CUIRect horizontally. You can pass null pointers. + * + * @param pTop This rect will end up taking the top half of this CUIRect. + * @param pBottom This rect will end up taking the bottom half of this CUIRect. + * @param Spacing Total size of margin between split rects. + */ + void HSplitMid(CUIRect *pTop, CUIRect *pBottom, float Spacing = 0.0f) const; + /** + * Splits 2 CUIRect inside *this* CUIRect. + * + * The cut parameter determines the height of the top rect, so it allows more customization than HSplitMid. + * + * This method doesn't check if Cut is bigger than *this* rect height. + * + * @param Cut The height of the pTop rect. + * @param pTop The rect that ends up at the top with a height equal to Cut. + * @param pBottom The rect that ends up at the bottom with a height equal to *this* rect minus the Cut. + */ + void HSplitTop(float Cut, CUIRect *pTop, CUIRect *pBottom) const; + /** + * Splits 2 CUIRect inside *this* CUIRect. + * + * The cut parameter determines the height of the bottom rect, so it allows more customization than HSplitMid. + * + * This method doesn't check if Cut is bigger than *this* rect height. + * + * @param Cut The height of the pBottom rect. + * @param pTop The rect that ends up at the top with a height equal to *this* CUIRect height minus Cut. + * @param pBottom The rect that ends up at the bottom with a height equal to Cut. + */ + void HSplitBottom(float Cut, CUIRect *pTop, CUIRect *pBottom) const; + /** + * Splits 2 CUIRect inside *this* CUIRect vertically. You can pass null pointers. + * + * @param pLeft This rect will take up the left half of *this* CUIRect. + * @param pRight This rect will take up the right half of *this* CUIRect. + * @param Spacing Total size of margin between split rects. + */ + void VSplitMid(CUIRect *pLeft, CUIRect *pRight, float Spacing = 0.0f) const; + /** + * Splits 2 CUIRect inside *this* CUIRect. + * + * The cut parameter determines the width of the left rect, so it allows more customization than VSplitMid. + * + * This method doesn't check if Cut is bigger than *this* rect width. + * + * @param Cut The width of the pLeft rect. + * @param pLeft The rect that ends up at the left with a width equal to Cut. + * @param pRight The rect that ends up at the right with a width equal to *this* rect minus the Cut. + */ + void VSplitLeft(float Cut, CUIRect *pLeft, CUIRect *pRight) const; + /** + * Splits 2 CUIRect inside *this* CUIRect. + * + * The cut parameter determines the width of the right rect, so it allows more customization than VSplitMid. + * + * This method doesn't check if Cut is bigger than *this* rect width. + * + * @param Cut The width of the pRight rect. + * @param pLeft The rect that ends up at the left with a width equal to *this* CUIRect width minus Cut. + * @param pRight The rect that ends up at the right with a width equal to Cut. + */ + void VSplitRight(float Cut, CUIRect *pLeft, CUIRect *pRight) const; + + /** + * Places pOtherRect inside *this* CUIRect with Cut as the margin. + * + * @param Cut The margin. + * @param pOtherRect The CUIRect to place inside *this* CUIRect. + */ + void Margin(float Cut, CUIRect *pOtherRect) const; + /** + * Places pOtherRect inside *this* CUIRect applying Cut as the margin only on the vertical axis. + * + * @param Cut The margin. + * @param pOtherRect The CUIRect to place inside *this* CUIRect + */ + void VMargin(float Cut, CUIRect *pOtherRect) const; + /** + * Places pOtherRect inside *this* CUIRect applying Cut as the margin only on the horizontal axis. + * + * @param Cut The margin. + * @param pOtherRect The CUIRect to place inside *this* CUIRect + */ + void HMargin(float Cut, CUIRect *pOtherRect) const; + + bool Inside(float x_, float y_) const; +}; + +#endif