Move CUIRect class into its own files

This commit is contained in:
Robert Müller 2022-07-08 17:56:06 +02:00
parent 8828838f68
commit b50309dd5c
5 changed files with 273 additions and 260 deletions

View file

@ -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

View file

@ -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

View file

@ -3,6 +3,8 @@
#ifndef GAME_CLIENT_UI_H
#define GAME_CLIENT_UI_H
#include "ui_rect.h"
#include <engine/input.h>
#include <engine/textrender.h>
@ -10,101 +12,6 @@
#include <string>
#include <vector>
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;

168
src/game/client/ui_rect.cpp Normal file
View file

@ -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;
}

101
src/game/client/ui_rect.h Normal file
View file

@ -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