2010-11-20 10:37:14 +00:00
|
|
|
/* (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. */
|
2015-08-17 12:10:08 +00:00
|
|
|
#include <base/math.h>
|
2020-09-26 19:41:58 +00:00
|
|
|
#include <base/system.h>
|
2008-08-14 17:19:13 +00:00
|
|
|
|
2020-09-26 19:41:58 +00:00
|
|
|
#include "ui.h"
|
2010-05-29 07:25:38 +00:00
|
|
|
#include <engine/graphics.h>
|
2020-09-26 19:41:58 +00:00
|
|
|
#include <engine/shared/config.h>
|
2010-05-29 07:25:38 +00:00
|
|
|
#include <engine/textrender.h>
|
2022-03-11 16:34:48 +00:00
|
|
|
#include <limits>
|
2007-05-22 15:03:32 +00:00
|
|
|
|
2021-09-13 21:48:10 +00:00
|
|
|
void CUIElement::Init(CUI *pUI, int RequestedRectCount)
|
2020-10-12 10:29:47 +00:00
|
|
|
{
|
|
|
|
pUI->AddUIElement(this);
|
2021-09-13 21:48:10 +00:00
|
|
|
if(RequestedRectCount > 0)
|
|
|
|
m_UIRects.resize(RequestedRectCount);
|
2020-10-12 10:29:47 +00:00
|
|
|
}
|
|
|
|
|
2021-09-13 21:48:10 +00:00
|
|
|
void CUIElement::InitRects(int RequestedRectCount)
|
2020-11-25 12:05:53 +00:00
|
|
|
{
|
2022-01-22 12:54:25 +00:00
|
|
|
dbg_assert(m_UIRects.empty(), "UI rects can only be initialized once, create another ui element instead.");
|
2021-09-13 21:48:10 +00:00
|
|
|
m_UIRects.resize(RequestedRectCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
CUIElement::SUIElementRect::SUIElementRect() { Reset(); }
|
|
|
|
|
|
|
|
void CUIElement::SUIElementRect::Reset()
|
|
|
|
{
|
|
|
|
m_UIRectQuadContainer = -1;
|
|
|
|
m_UITextContainer = -1;
|
|
|
|
m_X = -1;
|
|
|
|
m_Y = -1;
|
|
|
|
m_Width = -1;
|
|
|
|
m_Height = -1;
|
|
|
|
m_Text.clear();
|
2020-11-25 12:05:53 +00:00
|
|
|
mem_zero(&m_Cursor, sizeof(m_Cursor));
|
2021-09-13 21:48:10 +00:00
|
|
|
m_TextColor.Set(-1, -1, -1, -1);
|
|
|
|
m_TextOutlineColor.Set(-1, -1, -1, -1);
|
|
|
|
m_QuadColor = ColorRGBA(-1, -1, -1, -1);
|
2020-11-25 12:05:53 +00:00
|
|
|
}
|
|
|
|
|
2007-05-22 15:03:32 +00:00
|
|
|
/********************************************************
|
2011-04-13 18:37:12 +00:00
|
|
|
UI
|
2007-05-22 15:03:32 +00:00
|
|
|
*********************************************************/
|
2009-10-27 14:38:53 +00:00
|
|
|
|
2021-12-03 18:49:22 +00:00
|
|
|
float CUI::ms_FontmodHeight = 0.8f;
|
|
|
|
|
2021-12-03 19:17:43 +00:00
|
|
|
void CUI::Init(class IGraphics *pGraphics, class ITextRender *pTextRender)
|
|
|
|
{
|
|
|
|
m_pGraphics = pGraphics;
|
|
|
|
m_pTextRender = pTextRender;
|
|
|
|
}
|
|
|
|
|
2009-10-27 14:38:53 +00:00
|
|
|
CUI::CUI()
|
|
|
|
{
|
|
|
|
m_pHotItem = 0;
|
|
|
|
m_pActiveItem = 0;
|
|
|
|
m_pLastActiveItem = 0;
|
2021-11-22 17:26:13 +00:00
|
|
|
m_pBecomingHotItem = 0;
|
2022-04-07 07:46:02 +00:00
|
|
|
m_pActiveTooltipItem = 0;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2009-10-27 14:38:53 +00:00
|
|
|
m_MouseX = 0;
|
|
|
|
m_MouseY = 0;
|
|
|
|
m_MouseWorldX = 0;
|
|
|
|
m_MouseWorldY = 0;
|
|
|
|
m_MouseButtons = 0;
|
2011-04-13 18:37:12 +00:00
|
|
|
m_LastMouseButtons = 0;
|
|
|
|
|
2009-10-27 14:38:53 +00:00
|
|
|
m_Screen.x = 0;
|
|
|
|
m_Screen.y = 0;
|
|
|
|
m_Screen.w = 848.0f;
|
|
|
|
m_Screen.h = 480.0f;
|
|
|
|
}
|
|
|
|
|
2020-10-12 10:29:47 +00:00
|
|
|
CUI::~CUI()
|
|
|
|
{
|
|
|
|
for(CUIElement *&pEl : m_OwnUIElements)
|
|
|
|
{
|
|
|
|
delete pEl;
|
|
|
|
}
|
|
|
|
m_OwnUIElements.clear();
|
|
|
|
}
|
|
|
|
|
2021-09-13 21:48:10 +00:00
|
|
|
CUIElement *CUI::GetNewUIElement(int RequestedRectCount)
|
2020-10-12 10:29:47 +00:00
|
|
|
{
|
2021-09-13 21:48:10 +00:00
|
|
|
CUIElement *pNewEl = new CUIElement(this, RequestedRectCount);
|
2020-10-12 10:29:47 +00:00
|
|
|
|
|
|
|
m_OwnUIElements.push_back(pNewEl);
|
|
|
|
|
|
|
|
return pNewEl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CUI::AddUIElement(CUIElement *pElement)
|
|
|
|
{
|
|
|
|
m_UIElements.push_back(pElement);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CUI::ResetUIElement(CUIElement &UIElement)
|
|
|
|
{
|
|
|
|
for(CUIElement::SUIElementRect &Rect : UIElement.m_UIRects)
|
|
|
|
{
|
|
|
|
if(Rect.m_UIRectQuadContainer != -1)
|
|
|
|
Graphics()->DeleteQuadContainer(Rect.m_UIRectQuadContainer);
|
|
|
|
Rect.m_UIRectQuadContainer = -1;
|
|
|
|
if(Rect.m_UITextContainer != -1)
|
|
|
|
TextRender()->DeleteTextContainer(Rect.m_UITextContainer);
|
|
|
|
Rect.m_UITextContainer = -1;
|
|
|
|
|
2021-09-13 21:48:10 +00:00
|
|
|
Rect.Reset();
|
|
|
|
}
|
2020-10-12 10:29:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CUI::OnElementsReset()
|
|
|
|
{
|
|
|
|
for(CUIElement *pEl : m_UIElements)
|
|
|
|
{
|
|
|
|
ResetUIElement(*pEl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CUI::OnWindowResize()
|
|
|
|
{
|
|
|
|
OnElementsReset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CUI::OnLanguageChange()
|
|
|
|
{
|
|
|
|
OnElementsReset();
|
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
int CUI::Update(float Mx, float My, float Mwx, float Mwy, int Buttons)
|
2007-05-22 15:03:32 +00:00
|
|
|
{
|
2020-12-14 00:51:31 +00:00
|
|
|
m_MouseDeltaX = Mx - m_MouseX;
|
|
|
|
m_MouseDeltaY = My - m_MouseY;
|
2011-04-13 18:37:12 +00:00
|
|
|
m_MouseX = Mx;
|
|
|
|
m_MouseY = My;
|
|
|
|
m_MouseWorldX = Mwx;
|
|
|
|
m_MouseWorldY = Mwy;
|
|
|
|
m_LastMouseButtons = m_MouseButtons;
|
|
|
|
m_MouseButtons = Buttons;
|
2021-11-22 17:26:13 +00:00
|
|
|
m_pHotItem = m_pBecomingHotItem;
|
2011-04-13 18:37:12 +00:00
|
|
|
if(m_pActiveItem)
|
|
|
|
m_pHotItem = m_pActiveItem;
|
2021-11-22 17:26:13 +00:00
|
|
|
m_pBecomingHotItem = 0;
|
2011-04-13 18:37:12 +00:00
|
|
|
return 0;
|
2007-05-22 15:03:32 +00:00
|
|
|
}
|
2009-10-27 14:38:53 +00:00
|
|
|
|
2021-11-26 20:55:31 +00:00
|
|
|
bool CUI::MouseInside(const CUIRect *pRect) const
|
2007-05-22 15:03:32 +00:00
|
|
|
{
|
2021-11-26 20:55:31 +00:00
|
|
|
return pRect->Inside(m_MouseX, m_MouseY);
|
2007-05-22 15:03:32 +00:00
|
|
|
}
|
|
|
|
|
2021-10-23 11:48:21 +00:00
|
|
|
void CUI::ConvertMouseMove(float *x, float *y) const
|
|
|
|
{
|
|
|
|
float Fac = (float)(g_Config.m_UiMousesens) / g_Config.m_InpMousesens;
|
|
|
|
*x = *x * Fac;
|
|
|
|
*y = *y * Fac;
|
|
|
|
}
|
|
|
|
|
2021-11-26 20:55:31 +00:00
|
|
|
float CUI::ButtonColorMul(const void *pID)
|
|
|
|
{
|
|
|
|
if(ActiveItem() == pID)
|
|
|
|
return ButtonColorMulActive();
|
|
|
|
else if(HotItem() == pID)
|
|
|
|
return ButtonColorMulHot();
|
|
|
|
return ButtonColorMulDefault();
|
|
|
|
}
|
|
|
|
|
2009-10-27 14:38:53 +00:00
|
|
|
CUIRect *CUI::Screen()
|
2007-05-22 15:03:32 +00:00
|
|
|
{
|
2011-04-13 18:37:12 +00:00
|
|
|
float Aspect = Graphics()->ScreenAspect();
|
|
|
|
float w, h;
|
2007-05-22 15:03:32 +00:00
|
|
|
|
2011-04-13 18:37:12 +00:00
|
|
|
h = 600;
|
2020-09-26 19:41:58 +00:00
|
|
|
w = Aspect * h;
|
2007-05-22 15:03:32 +00:00
|
|
|
|
2011-04-13 18:37:12 +00:00
|
|
|
m_Screen.w = w;
|
|
|
|
m_Screen.h = h;
|
2007-05-27 18:14:24 +00:00
|
|
|
|
2011-04-13 18:37:12 +00:00
|
|
|
return &m_Screen;
|
2007-05-22 15:03:32 +00:00
|
|
|
}
|
|
|
|
|
2021-09-22 19:48:55 +00:00
|
|
|
void CUI::MapScreen()
|
|
|
|
{
|
|
|
|
const CUIRect *pScreen = Screen();
|
|
|
|
Graphics()->MapScreen(pScreen->x, pScreen->y, pScreen->w, pScreen->h);
|
|
|
|
}
|
|
|
|
|
2012-01-10 22:13:19 +00:00
|
|
|
float CUI::PixelSize()
|
|
|
|
{
|
2020-09-26 19:41:58 +00:00
|
|
|
return Screen()->w / Graphics()->ScreenWidth();
|
2012-01-10 22:13:19 +00:00
|
|
|
}
|
|
|
|
|
2009-10-27 14:38:53 +00:00
|
|
|
void CUI::SetScale(float s)
|
2007-10-29 18:40:04 +00:00
|
|
|
{
|
2020-09-26 19:41:58 +00:00
|
|
|
g_Config.m_UiScale = (int)(s * 100.0f);
|
2007-10-29 18:40:04 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 21:26:26 +00:00
|
|
|
float CUI::Scale() const
|
2007-10-29 18:40:04 +00:00
|
|
|
{
|
2020-09-26 19:41:58 +00:00
|
|
|
return g_Config.m_UiScale / 100.0f;
|
2010-12-14 00:20:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
float CUIRect::Scale() const
|
|
|
|
{
|
2020-09-26 19:41:58 +00:00
|
|
|
return g_Config.m_UiScale / 100.0f;
|
2010-12-14 00:20:47 +00:00
|
|
|
}
|
2007-10-29 18:40:04 +00:00
|
|
|
|
2022-01-22 13:12:59 +00:00
|
|
|
void CUI::ClipEnable(const CUIRect *pRect)
|
2007-10-29 18:40:04 +00:00
|
|
|
{
|
2022-05-13 18:20:04 +00:00
|
|
|
if(IsClipped())
|
|
|
|
{
|
|
|
|
const CUIRect *pOldRect = ClipArea();
|
|
|
|
CUIRect Intersection;
|
|
|
|
Intersection.x = std::max(pRect->x, pOldRect->x);
|
|
|
|
Intersection.y = std::max(pRect->y, pOldRect->y);
|
|
|
|
Intersection.w = std::min(pRect->x + pRect->w, pOldRect->x + pOldRect->w) - pRect->x;
|
|
|
|
Intersection.h = std::min(pRect->y + pRect->h, pOldRect->y + pOldRect->h) - pRect->y;
|
|
|
|
m_Clips.push_back(Intersection);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_Clips.push_back(*pRect);
|
|
|
|
}
|
|
|
|
UpdateClipping();
|
2007-10-29 18:40:04 +00:00
|
|
|
}
|
|
|
|
|
2009-10-27 14:38:53 +00:00
|
|
|
void CUI::ClipDisable()
|
2007-10-29 18:40:04 +00:00
|
|
|
{
|
2022-05-13 18:20:04 +00:00
|
|
|
dbg_assert(IsClipped(), "no clip region");
|
|
|
|
m_Clips.pop_back();
|
|
|
|
UpdateClipping();
|
|
|
|
}
|
|
|
|
|
|
|
|
const CUIRect *CUI::ClipArea() const
|
|
|
|
{
|
|
|
|
dbg_assert(IsClipped(), "no clip region");
|
|
|
|
return &m_Clips.back();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CUI::UpdateClipping()
|
|
|
|
{
|
|
|
|
if(IsClipped())
|
|
|
|
{
|
|
|
|
const CUIRect *pRect = ClipArea();
|
|
|
|
const float XScale = Graphics()->ScreenWidth() / Screen()->w;
|
|
|
|
const float YScale = Graphics()->ScreenHeight() / Screen()->h;
|
|
|
|
Graphics()->ClipEnable((int)(pRect->x * XScale), (int)(pRect->y * YScale), (int)(pRect->w * XScale), (int)(pRect->h * YScale));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Graphics()->ClipDisable();
|
|
|
|
}
|
2007-10-29 18:40:04 +00:00
|
|
|
}
|
|
|
|
|
2022-01-26 19:19:25 +00:00
|
|
|
void CUIRect::HSplitMid(CUIRect *pTop, CUIRect *pBottom, float Spacing) const
|
2011-03-26 15:19:37 +00:00
|
|
|
{
|
|
|
|
CUIRect r = *this;
|
2022-01-26 19:19:25 +00:00
|
|
|
const float Cut = r.h / 2;
|
|
|
|
const float HalfSpacing = Spacing / 2;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
|
|
|
if(pTop)
|
|
|
|
{
|
|
|
|
pTop->x = r.x;
|
|
|
|
pTop->y = r.y;
|
|
|
|
pTop->w = r.w;
|
2022-01-26 19:19:25 +00:00
|
|
|
pTop->h = Cut - HalfSpacing;
|
2011-04-13 18:37:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(pBottom)
|
|
|
|
{
|
|
|
|
pBottom->x = r.x;
|
2022-01-26 19:19:25 +00:00
|
|
|
pBottom->y = r.y + Cut + HalfSpacing;
|
2011-04-13 18:37:12 +00:00
|
|
|
pBottom->w = r.w;
|
2022-01-26 19:19:25 +00:00
|
|
|
pBottom->h = r.h - Cut - HalfSpacing;
|
2011-04-13 18:37:12 +00:00
|
|
|
}
|
2011-03-26 15:19:37 +00:00
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
void CUIRect::HSplitTop(float Cut, CUIRect *pTop, CUIRect *pBottom) const
|
2007-10-29 18:40:04 +00:00
|
|
|
{
|
2011-04-13 18:37:12 +00:00
|
|
|
CUIRect r = *this;
|
|
|
|
Cut *= Scale();
|
|
|
|
|
2018-03-13 20:59:07 +00:00
|
|
|
if(pTop)
|
2011-04-13 18:37:12 +00:00
|
|
|
{
|
|
|
|
pTop->x = r.x;
|
|
|
|
pTop->y = r.y;
|
|
|
|
pTop->w = r.w;
|
|
|
|
pTop->h = Cut;
|
|
|
|
}
|
|
|
|
|
2018-03-13 20:59:07 +00:00
|
|
|
if(pBottom)
|
2011-04-13 18:37:12 +00:00
|
|
|
{
|
|
|
|
pBottom->x = r.x;
|
|
|
|
pBottom->y = r.y + Cut;
|
|
|
|
pBottom->w = r.w;
|
|
|
|
pBottom->h = r.h - Cut;
|
|
|
|
}
|
2007-10-29 18:40:04 +00:00
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
void CUIRect::HSplitBottom(float Cut, CUIRect *pTop, CUIRect *pBottom) const
|
2007-10-29 18:40:04 +00:00
|
|
|
{
|
2011-04-13 18:37:12 +00:00
|
|
|
CUIRect r = *this;
|
|
|
|
Cut *= Scale();
|
|
|
|
|
2018-03-13 20:59:07 +00:00
|
|
|
if(pTop)
|
2011-04-13 18:37:12 +00:00
|
|
|
{
|
|
|
|
pTop->x = r.x;
|
|
|
|
pTop->y = r.y;
|
|
|
|
pTop->w = r.w;
|
|
|
|
pTop->h = r.h - Cut;
|
|
|
|
}
|
|
|
|
|
2018-03-13 20:59:07 +00:00
|
|
|
if(pBottom)
|
2011-04-13 18:37:12 +00:00
|
|
|
{
|
|
|
|
pBottom->x = r.x;
|
|
|
|
pBottom->y = r.y + r.h - Cut;
|
|
|
|
pBottom->w = r.w;
|
|
|
|
pBottom->h = Cut;
|
|
|
|
}
|
2007-10-29 18:40:04 +00:00
|
|
|
}
|
|
|
|
|
2022-01-26 19:19:25 +00:00
|
|
|
void CUIRect::VSplitMid(CUIRect *pLeft, CUIRect *pRight, float Spacing) const
|
2007-10-29 18:40:04 +00:00
|
|
|
{
|
2011-04-13 18:37:12 +00:00
|
|
|
CUIRect r = *this;
|
2022-01-26 19:19:25 +00:00
|
|
|
const float Cut = r.w / 2;
|
|
|
|
const float HalfSpacing = Spacing / 2;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2018-03-13 20:59:07 +00:00
|
|
|
if(pLeft)
|
2011-04-13 18:37:12 +00:00
|
|
|
{
|
|
|
|
pLeft->x = r.x;
|
|
|
|
pLeft->y = r.y;
|
2022-01-26 19:19:25 +00:00
|
|
|
pLeft->w = Cut - HalfSpacing;
|
2011-04-13 18:37:12 +00:00
|
|
|
pLeft->h = r.h;
|
|
|
|
}
|
|
|
|
|
2018-03-13 20:59:07 +00:00
|
|
|
if(pRight)
|
2011-04-13 18:37:12 +00:00
|
|
|
{
|
2022-01-26 19:19:25 +00:00
|
|
|
pRight->x = r.x + Cut + HalfSpacing;
|
2011-04-13 18:37:12 +00:00
|
|
|
pRight->y = r.y;
|
2022-01-26 19:19:25 +00:00
|
|
|
pRight->w = r.w - Cut - HalfSpacing;
|
2011-04-13 18:37:12 +00:00
|
|
|
pRight->h = r.h;
|
|
|
|
}
|
2008-01-12 12:27:55 +00:00
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
void CUIRect::VSplitLeft(float Cut, CUIRect *pLeft, CUIRect *pRight) const
|
2008-01-12 12:27:55 +00:00
|
|
|
{
|
2011-04-13 18:37:12 +00:00
|
|
|
CUIRect r = *this;
|
|
|
|
Cut *= Scale();
|
|
|
|
|
2018-03-13 20:59:07 +00:00
|
|
|
if(pLeft)
|
2011-04-13 18:37:12 +00:00
|
|
|
{
|
|
|
|
pLeft->x = r.x;
|
|
|
|
pLeft->y = r.y;
|
|
|
|
pLeft->w = Cut;
|
|
|
|
pLeft->h = r.h;
|
|
|
|
}
|
|
|
|
|
2018-03-13 20:59:07 +00:00
|
|
|
if(pRight)
|
2011-04-13 18:37:12 +00:00
|
|
|
{
|
|
|
|
pRight->x = r.x + Cut;
|
|
|
|
pRight->y = r.y;
|
|
|
|
pRight->w = r.w - Cut;
|
|
|
|
pRight->h = r.h;
|
|
|
|
}
|
2007-10-29 18:40:04 +00:00
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
void CUIRect::VSplitRight(float Cut, CUIRect *pLeft, CUIRect *pRight) const
|
2007-10-29 18:40:04 +00:00
|
|
|
{
|
2011-04-13 18:37:12 +00:00
|
|
|
CUIRect r = *this;
|
|
|
|
Cut *= Scale();
|
|
|
|
|
2018-03-13 20:59:07 +00:00
|
|
|
if(pLeft)
|
2011-04-13 18:37:12 +00:00
|
|
|
{
|
|
|
|
pLeft->x = r.x;
|
|
|
|
pLeft->y = r.y;
|
|
|
|
pLeft->w = r.w - Cut;
|
|
|
|
pLeft->h = r.h;
|
|
|
|
}
|
|
|
|
|
2018-03-13 20:59:07 +00:00
|
|
|
if(pRight)
|
2011-04-13 18:37:12 +00:00
|
|
|
{
|
|
|
|
pRight->x = r.x + r.w - Cut;
|
|
|
|
pRight->y = r.y;
|
|
|
|
pRight->w = Cut;
|
|
|
|
pRight->h = r.h;
|
|
|
|
}
|
2008-01-12 12:27:55 +00:00
|
|
|
}
|
2007-10-29 18:40:04 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
void CUIRect::Margin(float Cut, CUIRect *pOtherRect) const
|
2008-01-12 12:27:55 +00:00
|
|
|
{
|
2011-04-13 18:37:12 +00:00
|
|
|
CUIRect r = *this;
|
2010-05-29 07:25:38 +00:00
|
|
|
Cut *= Scale();
|
2008-01-12 12:27:55 +00:00
|
|
|
|
2011-04-13 18:37:12 +00:00
|
|
|
pOtherRect->x = r.x + Cut;
|
|
|
|
pOtherRect->y = r.y + Cut;
|
2020-09-26 19:41:58 +00:00
|
|
|
pOtherRect->w = r.w - 2 * Cut;
|
|
|
|
pOtherRect->h = r.h - 2 * Cut;
|
2008-01-12 12:27:55 +00:00
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
void CUIRect::VMargin(float Cut, CUIRect *pOtherRect) const
|
2008-01-12 12:27:55 +00:00
|
|
|
{
|
2011-04-13 18:37:12 +00:00
|
|
|
CUIRect r = *this;
|
2010-05-29 07:25:38 +00:00
|
|
|
Cut *= Scale();
|
2008-01-12 12:27:55 +00:00
|
|
|
|
2011-04-13 18:37:12 +00:00
|
|
|
pOtherRect->x = r.x + Cut;
|
|
|
|
pOtherRect->y = r.y;
|
2020-09-26 19:41:58 +00:00
|
|
|
pOtherRect->w = r.w - 2 * Cut;
|
2011-04-13 18:37:12 +00:00
|
|
|
pOtherRect->h = r.h;
|
2007-10-29 18:40:04 +00:00
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
void CUIRect::HMargin(float Cut, CUIRect *pOtherRect) const
|
2007-10-29 18:40:04 +00:00
|
|
|
{
|
2011-04-13 18:37:12 +00:00
|
|
|
CUIRect r = *this;
|
2010-05-29 07:25:38 +00:00
|
|
|
Cut *= Scale();
|
2007-10-29 18:40:04 +00:00
|
|
|
|
2011-04-13 18:37:12 +00:00
|
|
|
pOtherRect->x = r.x;
|
|
|
|
pOtherRect->y = r.y + Cut;
|
|
|
|
pOtherRect->w = r.w;
|
2020-09-26 19:41:58 +00:00
|
|
|
pOtherRect->h = r.h - 2 * Cut;
|
2008-01-12 12:27:55 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 00:16:05 +00:00
|
|
|
bool CUIRect::Inside(float x_, float y_) const
|
2021-11-26 20:55:31 +00:00
|
|
|
{
|
2022-03-24 00:16:05 +00:00
|
|
|
return x_ >= this->x && x_ < this->x + this->w && y_ >= this->y && y_ < this->y + this->h;
|
2021-11-26 20:55:31 +00:00
|
|
|
}
|
|
|
|
|
2009-10-27 14:38:53 +00:00
|
|
|
int CUI::DoButtonLogic(const void *pID, const char *pText, int Checked, const CUIRect *pRect)
|
2020-10-12 10:29:47 +00:00
|
|
|
{
|
|
|
|
return DoButtonLogic(pID, Checked, pRect);
|
|
|
|
}
|
|
|
|
|
|
|
|
int CUI::DoButtonLogic(const void *pID, int Checked, const CUIRect *pRect)
|
2008-01-12 12:27:55 +00:00
|
|
|
{
|
2011-04-13 18:37:12 +00:00
|
|
|
// logic
|
|
|
|
int ReturnValue = 0;
|
2022-05-13 18:45:57 +00:00
|
|
|
const bool Inside = MouseHovered(pRect);
|
2022-05-13 18:28:57 +00:00
|
|
|
static int s_ButtonUsed = 0;
|
2009-10-27 14:38:53 +00:00
|
|
|
|
|
|
|
if(ActiveItem() == pID)
|
|
|
|
{
|
2022-05-13 18:28:57 +00:00
|
|
|
if(!MouseButton(s_ButtonUsed))
|
2009-10-27 14:38:53 +00:00
|
|
|
{
|
|
|
|
if(Inside && Checked >= 0)
|
2022-05-13 18:28:57 +00:00
|
|
|
ReturnValue = 1 + s_ButtonUsed;
|
2009-10-27 14:38:53 +00:00
|
|
|
SetActiveItem(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(HotItem() == pID)
|
|
|
|
{
|
2017-08-30 20:36:39 +00:00
|
|
|
for(int i = 0; i < 3; ++i)
|
2009-10-27 14:38:53 +00:00
|
|
|
{
|
2017-08-30 20:36:39 +00:00
|
|
|
if(MouseButton(i))
|
|
|
|
{
|
|
|
|
SetActiveItem(pID);
|
2022-05-13 18:28:57 +00:00
|
|
|
s_ButtonUsed = i;
|
2017-08-30 20:36:39 +00:00
|
|
|
}
|
2009-10-27 14:38:53 +00:00
|
|
|
}
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2009-10-27 14:38:53 +00:00
|
|
|
if(Inside)
|
|
|
|
SetHotItem(pID);
|
|
|
|
|
2011-04-13 18:37:12 +00:00
|
|
|
return ReturnValue;
|
2009-10-27 14:38:53 +00:00
|
|
|
}
|
2015-08-17 12:10:08 +00:00
|
|
|
|
|
|
|
int CUI::DoPickerLogic(const void *pID, const CUIRect *pRect, float *pX, float *pY)
|
|
|
|
{
|
2022-05-13 18:27:03 +00:00
|
|
|
const bool Inside = MouseInside(pRect);
|
2015-08-17 12:10:08 +00:00
|
|
|
|
2020-12-14 00:51:31 +00:00
|
|
|
if(Inside)
|
2015-08-17 12:10:08 +00:00
|
|
|
SetHotItem(pID);
|
|
|
|
|
2020-12-14 00:51:31 +00:00
|
|
|
if(HotItem() == pID && MouseButtonClicked(0))
|
|
|
|
SetActiveItem(pID);
|
|
|
|
|
|
|
|
if(ActiveItem() == pID && !MouseButton(0))
|
|
|
|
SetActiveItem(0);
|
|
|
|
|
|
|
|
if(ActiveItem() != pID)
|
2015-08-17 12:10:08 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if(pX)
|
|
|
|
*pX = clamp(m_MouseX - pRect->x, 0.0f, pRect->w) / Scale();
|
|
|
|
if(pY)
|
|
|
|
*pY = clamp(m_MouseY - pRect->y, 0.0f, pRect->h) / Scale();
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-03-11 16:34:48 +00:00
|
|
|
float CUI::DoTextLabel(float x, float y, float w, float h, const char *pText, float Size, int Align, const SLabelProperties &LabelProps)
|
2008-01-12 12:27:55 +00:00
|
|
|
{
|
2020-10-06 10:25:10 +00:00
|
|
|
float AlignedSize = 0;
|
|
|
|
float MaxCharacterHeightInLine = 0;
|
2022-03-11 16:34:48 +00:00
|
|
|
float tw = std::numeric_limits<float>::max();
|
|
|
|
float MaxTextWidth = LabelProps.m_MaxWidth != -1 ? LabelProps.m_MaxWidth : w;
|
|
|
|
tw = TextRender()->TextWidth(0, Size, pText, -1, LabelProps.m_MaxWidth, &AlignedSize, &MaxCharacterHeightInLine);
|
|
|
|
while(tw > MaxTextWidth + 0.001f)
|
|
|
|
{
|
|
|
|
if(!LabelProps.m_EnableWidthCheck)
|
|
|
|
break;
|
|
|
|
if(Size < 4.0f)
|
|
|
|
break;
|
|
|
|
Size -= 1.0f;
|
|
|
|
tw = TextRender()->TextWidth(0, Size, pText, -1, LabelProps.m_MaxWidth, &AlignedSize, &MaxCharacterHeightInLine);
|
|
|
|
}
|
2021-09-13 21:48:10 +00:00
|
|
|
|
2022-03-11 16:34:48 +00:00
|
|
|
int Flags = TEXTFLAG_RENDER | (LabelProps.m_StopAtEnd ? TEXTFLAG_STOP_AT_END : 0);
|
2021-09-13 21:48:10 +00:00
|
|
|
|
|
|
|
float AlignmentVert = y + (h - AlignedSize) / 2.f;
|
|
|
|
float AlignmentHori = 0;
|
2022-03-11 16:34:48 +00:00
|
|
|
if(LabelProps.m_AlignVertically == 0)
|
2020-10-06 10:25:10 +00:00
|
|
|
{
|
2021-09-13 21:48:10 +00:00
|
|
|
AlignmentVert = y + (h - AlignedSize) / 2.f - (AlignedSize - MaxCharacterHeightInLine) / 2.f;
|
2020-10-06 10:25:10 +00:00
|
|
|
}
|
2022-01-21 15:20:15 +00:00
|
|
|
// if(Align == 0)
|
|
|
|
if(Align & TEXTALIGN_CENTER)
|
2011-04-13 18:37:12 +00:00
|
|
|
{
|
2021-09-13 21:48:10 +00:00
|
|
|
AlignmentHori = x + (w - tw) / 2.f;
|
2008-01-12 12:27:55 +00:00
|
|
|
}
|
2022-01-21 15:20:15 +00:00
|
|
|
// else if(Align < 0)
|
|
|
|
else if(Align & TEXTALIGN_LEFT)
|
2018-03-21 14:54:51 +00:00
|
|
|
{
|
2021-09-13 21:48:10 +00:00
|
|
|
AlignmentHori = x;
|
2018-03-21 14:54:51 +00:00
|
|
|
}
|
2022-01-21 15:20:15 +00:00
|
|
|
// else if(Align > 0)
|
|
|
|
else if(Align & TEXTALIGN_RIGHT)
|
2008-01-12 12:27:55 +00:00
|
|
|
{
|
2021-09-13 21:48:10 +00:00
|
|
|
AlignmentHori = x + w - tw;
|
2008-01-12 12:27:55 +00:00
|
|
|
}
|
2021-09-13 21:48:10 +00:00
|
|
|
|
|
|
|
CTextCursor Cursor;
|
|
|
|
TextRender()->SetCursor(&Cursor, AlignmentHori, AlignmentVert, Size, Flags);
|
2022-03-11 16:34:48 +00:00
|
|
|
Cursor.m_LineWidth = (float)LabelProps.m_MaxWidth;
|
|
|
|
if(LabelProps.m_pSelCursor)
|
2021-09-16 14:50:17 +00:00
|
|
|
{
|
2022-03-11 16:34:48 +00:00
|
|
|
Cursor.m_CursorMode = LabelProps.m_pSelCursor->m_CursorMode;
|
|
|
|
Cursor.m_CursorCharacter = LabelProps.m_pSelCursor->m_CursorCharacter;
|
|
|
|
Cursor.m_CalculateSelectionMode = LabelProps.m_pSelCursor->m_CalculateSelectionMode;
|
|
|
|
Cursor.m_PressMouseX = LabelProps.m_pSelCursor->m_PressMouseX;
|
|
|
|
Cursor.m_PressMouseY = LabelProps.m_pSelCursor->m_PressMouseY;
|
|
|
|
Cursor.m_ReleaseMouseX = LabelProps.m_pSelCursor->m_ReleaseMouseX;
|
|
|
|
Cursor.m_ReleaseMouseY = LabelProps.m_pSelCursor->m_ReleaseMouseY;
|
2021-09-16 14:50:17 +00:00
|
|
|
|
2022-03-11 16:34:48 +00:00
|
|
|
Cursor.m_SelectionStart = LabelProps.m_pSelCursor->m_SelectionStart;
|
|
|
|
Cursor.m_SelectionEnd = LabelProps.m_pSelCursor->m_SelectionEnd;
|
2021-09-16 14:50:17 +00:00
|
|
|
}
|
2021-09-13 21:48:10 +00:00
|
|
|
|
|
|
|
TextRender()->TextEx(&Cursor, pText, -1);
|
|
|
|
|
2022-03-11 16:34:48 +00:00
|
|
|
if(LabelProps.m_pSelCursor)
|
2021-09-16 14:50:17 +00:00
|
|
|
{
|
2022-03-11 16:34:48 +00:00
|
|
|
*LabelProps.m_pSelCursor = Cursor;
|
2021-09-16 14:50:17 +00:00
|
|
|
}
|
|
|
|
|
2021-09-13 21:48:10 +00:00
|
|
|
return tw;
|
|
|
|
}
|
|
|
|
|
2022-03-11 16:34:48 +00:00
|
|
|
void CUI::DoLabel(const CUIRect *pRect, const char *pText, float Size, int Align, const SLabelProperties &LabelProps)
|
2021-09-13 21:48:10 +00:00
|
|
|
{
|
2022-03-11 16:34:48 +00:00
|
|
|
DoTextLabel(pRect->x, pRect->y, pRect->w, pRect->h, pText, Size, Align, LabelProps);
|
2007-10-29 18:40:04 +00:00
|
|
|
}
|
2010-12-14 00:20:47 +00:00
|
|
|
|
2022-03-11 16:34:48 +00:00
|
|
|
void CUI::DoLabelScaled(const CUIRect *pRect, const char *pText, float Size, int Align, const SLabelProperties &LabelProps)
|
2010-12-14 00:20:47 +00:00
|
|
|
{
|
2022-03-11 16:34:48 +00:00
|
|
|
DoLabel(pRect, pText, Size * Scale(), Align, LabelProps);
|
2014-01-15 23:46:13 +00:00
|
|
|
}
|
2020-10-12 10:29:47 +00:00
|
|
|
|
2022-03-11 16:34:48 +00:00
|
|
|
void CUI::DoLabel(CUIElement::SUIElementRect &RectEl, const CUIRect *pRect, const char *pText, float Size, int Align, const SLabelProperties &LabelProps, int StrLen, CTextCursor *pReadCursor)
|
2020-10-12 10:29:47 +00:00
|
|
|
{
|
|
|
|
float AlignedSize = 0;
|
|
|
|
float MaxCharacterHeightInLine = 0;
|
2022-03-11 16:34:48 +00:00
|
|
|
float tw = std::numeric_limits<float>::max();
|
|
|
|
float MaxTextWidth = LabelProps.m_MaxWidth != -1 ? LabelProps.m_MaxWidth : pRect->w;
|
|
|
|
tw = TextRender()->TextWidth(0, Size, pText, -1, LabelProps.m_MaxWidth, &AlignedSize, &MaxCharacterHeightInLine);
|
|
|
|
while(tw > MaxTextWidth + 0.001f)
|
|
|
|
{
|
|
|
|
if(!LabelProps.m_EnableWidthCheck)
|
|
|
|
break;
|
|
|
|
if(Size < 4.0f)
|
|
|
|
break;
|
|
|
|
Size -= 1.0f;
|
|
|
|
tw = TextRender()->TextWidth(0, Size, pText, -1, LabelProps.m_MaxWidth, &AlignedSize, &MaxCharacterHeightInLine);
|
|
|
|
}
|
2020-10-12 10:29:47 +00:00
|
|
|
float AlignmentVert = pRect->y + (pRect->h - AlignedSize) / 2.f;
|
|
|
|
float AlignmentHori = 0;
|
|
|
|
|
|
|
|
CTextCursor Cursor;
|
|
|
|
|
2022-03-11 16:34:48 +00:00
|
|
|
int Flags = TEXTFLAG_RENDER | (LabelProps.m_StopAtEnd ? TEXTFLAG_STOP_AT_END : 0);
|
2020-10-12 10:29:47 +00:00
|
|
|
|
2022-03-11 16:34:48 +00:00
|
|
|
if(LabelProps.m_AlignVertically == 0)
|
2020-10-12 10:29:47 +00:00
|
|
|
{
|
|
|
|
AlignmentVert = pRect->y + (pRect->h - AlignedSize) / 2.f - (AlignedSize - MaxCharacterHeightInLine) / 2.f;
|
|
|
|
}
|
2022-01-21 15:20:15 +00:00
|
|
|
// if(Align == 0)
|
|
|
|
if(Align & TEXTALIGN_CENTER)
|
2020-10-12 10:29:47 +00:00
|
|
|
{
|
|
|
|
AlignmentHori = pRect->x + (pRect->w - tw) / 2.f;
|
|
|
|
}
|
2022-01-21 15:20:15 +00:00
|
|
|
// else if(Align < 0)
|
|
|
|
else if(Align & TEXTALIGN_LEFT)
|
2020-10-12 10:29:47 +00:00
|
|
|
{
|
|
|
|
AlignmentHori = pRect->x;
|
|
|
|
}
|
2022-01-21 15:20:15 +00:00
|
|
|
// else if(Align > 0)
|
|
|
|
else if(Align & TEXTALIGN_RIGHT)
|
2020-10-12 10:29:47 +00:00
|
|
|
{
|
|
|
|
AlignmentHori = pRect->x + pRect->w - tw;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(pReadCursor)
|
|
|
|
{
|
|
|
|
Cursor = *pReadCursor;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TextRender()->SetCursor(&Cursor, AlignmentHori, AlignmentVert, Size, Flags);
|
|
|
|
}
|
2022-03-11 16:34:48 +00:00
|
|
|
Cursor.m_LineWidth = LabelProps.m_MaxWidth;
|
2020-10-12 10:29:47 +00:00
|
|
|
|
2020-11-08 18:41:16 +00:00
|
|
|
RectEl.m_TextColor = TextRender()->GetTextColor();
|
|
|
|
RectEl.m_TextOutlineColor = TextRender()->GetTextOutlineColor();
|
2021-09-13 21:48:10 +00:00
|
|
|
TextRender()->TextColor(TextRender()->DefaultTextColor());
|
|
|
|
TextRender()->TextOutlineColor(TextRender()->DefaultTextOutlineColor());
|
|
|
|
RectEl.m_UITextContainer = TextRender()->CreateTextContainer(&Cursor, pText, StrLen);
|
|
|
|
TextRender()->TextColor(RectEl.m_TextColor);
|
|
|
|
TextRender()->TextOutlineColor(RectEl.m_TextOutlineColor);
|
|
|
|
RectEl.m_Cursor = Cursor;
|
2020-10-12 10:29:47 +00:00
|
|
|
}
|
|
|
|
|
2021-09-13 21:48:10 +00:00
|
|
|
void CUI::DoLabelStreamed(CUIElement::SUIElementRect &RectEl, float x, float y, float w, float h, const char *pText, float Size, int Align, float MaxWidth, int AlignVertically, bool StopAtEnd, int StrLen, CTextCursor *pReadCursor)
|
2020-10-12 10:29:47 +00:00
|
|
|
{
|
|
|
|
bool NeedsRecreate = false;
|
2020-11-08 18:41:16 +00:00
|
|
|
bool ColorChanged = RectEl.m_TextColor != TextRender()->GetTextColor() || RectEl.m_TextOutlineColor != TextRender()->GetTextOutlineColor();
|
2021-09-13 21:48:10 +00:00
|
|
|
if(RectEl.m_UITextContainer == -1 || RectEl.m_X != x || RectEl.m_Y != y || RectEl.m_Width != w || RectEl.m_Height != h || ColorChanged)
|
2020-10-12 10:29:47 +00:00
|
|
|
{
|
|
|
|
NeedsRecreate = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(StrLen <= -1)
|
|
|
|
{
|
|
|
|
if(str_comp(RectEl.m_Text.c_str(), pText) != 0)
|
|
|
|
NeedsRecreate = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(StrLen != (int)RectEl.m_Text.size() || str_comp_num(RectEl.m_Text.c_str(), pText, StrLen) != 0)
|
|
|
|
NeedsRecreate = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(NeedsRecreate)
|
|
|
|
{
|
|
|
|
if(RectEl.m_UITextContainer != -1)
|
|
|
|
TextRender()->DeleteTextContainer(RectEl.m_UITextContainer);
|
|
|
|
RectEl.m_UITextContainer = -1;
|
|
|
|
|
2021-09-13 21:48:10 +00:00
|
|
|
RectEl.m_X = x;
|
|
|
|
RectEl.m_Y = y;
|
|
|
|
RectEl.m_Width = w;
|
|
|
|
RectEl.m_Height = h;
|
2020-10-12 10:29:47 +00:00
|
|
|
|
|
|
|
if(StrLen > 0)
|
|
|
|
RectEl.m_Text = std::string(pText, StrLen);
|
|
|
|
else if(StrLen < 0)
|
|
|
|
RectEl.m_Text = pText;
|
|
|
|
else
|
|
|
|
RectEl.m_Text.clear();
|
|
|
|
|
2021-09-13 21:48:10 +00:00
|
|
|
CUIRect TmpRect;
|
|
|
|
TmpRect.x = x;
|
|
|
|
TmpRect.y = y;
|
|
|
|
TmpRect.w = w;
|
|
|
|
TmpRect.h = h;
|
2022-03-11 16:34:48 +00:00
|
|
|
|
|
|
|
SLabelProperties Props;
|
|
|
|
Props.m_MaxWidth = MaxWidth;
|
|
|
|
Props.m_AlignVertically = AlignVertically;
|
|
|
|
Props.m_StopAtEnd = StopAtEnd;
|
|
|
|
DoLabel(RectEl, &TmpRect, pText, Size, Align, Props, StrLen, pReadCursor);
|
2020-10-12 10:29:47 +00:00
|
|
|
}
|
|
|
|
|
2021-09-13 21:48:10 +00:00
|
|
|
STextRenderColor ColorText(RectEl.m_TextColor);
|
|
|
|
STextRenderColor ColorTextOutline(RectEl.m_TextOutlineColor);
|
2020-10-12 10:29:47 +00:00
|
|
|
if(RectEl.m_UITextContainer != -1)
|
|
|
|
TextRender()->RenderTextContainer(RectEl.m_UITextContainer, &ColorText, &ColorTextOutline);
|
|
|
|
}
|
2021-09-13 21:48:10 +00:00
|
|
|
|
|
|
|
void CUI::DoLabelStreamed(CUIElement::SUIElementRect &RectEl, const CUIRect *pRect, const char *pText, float Size, int Align, float MaxWidth, int AlignVertically, bool StopAtEnd, int StrLen, CTextCursor *pReadCursor)
|
|
|
|
{
|
|
|
|
DoLabelStreamed(RectEl, pRect->x, pRect->y, pRect->w, pRect->h, pText, Size, Align, MaxWidth, AlignVertically, StopAtEnd, StrLen, pReadCursor);
|
|
|
|
}
|