Refactor CUIElements

This commit is contained in:
Jupeyy 2021-09-13 23:48:10 +02:00
parent 7f5ca0ffe1
commit 1e07a00c3b
6 changed files with 101 additions and 108 deletions

View file

@ -1188,8 +1188,8 @@ void CMenus::OnInit()
m_UIEx.Init(UI(), Kernel(), RenderTools(), m_aInputEvents, &m_NumInputEvents);
m_RefreshButton.Init(UI());
m_ConnectButton.Init(UI());
m_RefreshButton.Init(UI(), -1);
m_ConnectButton.Init(UI(), -1);
Console()->Chain("add_favorite", ConchainServerbrowserUpdate, this);
Console()->Chain("remove_favorite", ConchainServerbrowserUpdate, this);

View file

@ -130,12 +130,12 @@ class CMenus : public CComponent
Text.HMargin(pRect->h >= 20.0f ? 2.0f : 1.0f, &Text);
Text.HMargin((Text.h * FontFactor) / 2.0f, &Text);
if(UIElement.Size() != 3 || HintRequiresStringCheck || HintCanChangePositionOrSize)
if(!UIElement.AreRectsInit() || HintRequiresStringCheck || HintCanChangePositionOrSize || UIElement.Get(0)->m_UITextContainer == -1)
{
bool NeedsRecalc = UIElement.Size() != 3;
bool NeedsRecalc = !UIElement.AreRectsInit() || UIElement.Get(0)->m_UITextContainer == -1;
if(HintCanChangePositionOrSize)
{
if(UIElement.Size() == 3)
if(UIElement.AreRectsInit())
{
if(UIElement.Get(0)->m_X != pRect->x || UIElement.Get(0)->m_Y != pRect->y || UIElement.Get(0)->m_Width != pRect->w || UIElement.Get(0)->m_Y != pRect->h)
{
@ -146,7 +146,7 @@ class CMenus : public CComponent
const char *pText = NULL;
if(HintRequiresStringCheck)
{
if(UIElement.Size() == 3)
if(UIElement.AreRectsInit())
{
pText = GetTextLambda();
if(str_comp(UIElement.Get(0)->m_Text.c_str(), pText) != 0)
@ -157,10 +157,11 @@ class CMenus : public CComponent
}
if(NeedsRecalc)
{
if(UIElement.Size() > 0)
if(!UIElement.AreRectsInit())
{
UI()->ResetUIElement(UIElement);
UIElement.InitRects(3);
}
UI()->ResetUIElement(UIElement);
vec4 RealColor = Color;
for(int i = 0; i < 3; ++i)
@ -174,14 +175,13 @@ class CMenus : public CComponent
Color.a *= ButtonColorMulDefault();
Graphics()->SetColor(Color);
CUIElement::SUIElementRect NewRect;
CUIElement::SUIElementRect &NewRect = *UIElement.Get(i);
NewRect.m_UIRectQuadContainer = RenderTools()->CreateRoundRectQuadContainer(pRect->x, pRect->y, pRect->w, pRect->h, r, Corners);
NewRect.m_X = pRect->x;
NewRect.m_Y = pRect->y;
NewRect.m_Width = pRect->w;
NewRect.m_Height = pRect->h;
if(i == 0)
{
if(pText == NULL)
@ -189,12 +189,10 @@ class CMenus : public CComponent
NewRect.m_Text = pText;
UI()->DoLabel(NewRect, &Text, pText, Text.h * ms_FontmodHeight, 0, -1, AlignVertically);
}
UIElement.Add(NewRect);
}
Graphics()->SetColor(1, 1, 1, 1);
}
}
// render
size_t Index = 2;
if(UI()->ActiveItem() == pID)
@ -207,7 +205,6 @@ class CMenus : public CComponent
STextRenderColor ColorTextOutline(TextRender()->DefaultTextOutlineColor());
if(UIElement.Get(0)->m_UITextContainer != -1)
TextRender()->RenderTextContainer(UIElement.Get(0)->m_UITextContainer, &ColorText, &ColorTextOutline);
return UI()->DoButtonLogic(pID, Checked, pRect);
}

View file

@ -233,23 +233,11 @@ void CMenus::RenderServerbrowserServerList(CUIRect View)
CUIRect Row;
CUIRect SelectHitBox;
const int UIRectCount = 2 + (COL_VERSION + 1) * 3;
//initialize
if(pItem->m_pUIElement == NULL)
{
pItem->m_pUIElement = UI()->GetNewUIElement();
}
const int UIRectCount = 2 + (COL_VERSION + 1) * 3;
if(pItem->m_pUIElement->Size() != UIRectCount)
{
UI()->ResetUIElement(*pItem->m_pUIElement);
for(int UIElIndex = 0; UIElIndex < UIRectCount; ++UIElIndex)
{
CUIElement::SUIElementRect AddRect;
pItem->m_pUIElement->Add(AddRect);
}
pItem->m_pUIElement = UI()->GetNewUIElement(UIRectCount);
}
int Selected = str_comp(pItem->m_aAddress, g_Config.m_UiServerAddress) == 0; //selected_index==ItemIndex;

View file

@ -448,7 +448,7 @@ int CRenderTools::CreateRoundRectQuadContainer(float x, float y, float w, float
void CRenderTools::DrawUIElRect(CUIElement::SUIElementRect &ElUIRect, const CUIRect *pRect, ColorRGBA Color, int Corners, float Rounding)
{
bool NeedsRecreate = false;
if(ElUIRect.m_UIRectQuadContainer == -1 || ElUIRect.m_X != pRect->x || ElUIRect.m_Y != pRect->y || ElUIRect.m_Width != pRect->w || ElUIRect.m_Height != pRect->h)
if(ElUIRect.m_UIRectQuadContainer == -1 || ElUIRect.m_X != pRect->x || ElUIRect.m_Y != pRect->y || ElUIRect.m_Width != pRect->w || ElUIRect.m_Height != pRect->h || mem_comp(&ElUIRect.m_QuadColor, &Color, sizeof(Color)) != 0)
{
if(ElUIRect.m_UIRectQuadContainer != -1)
Graphics()->DeleteQuadContainer(ElUIRect.m_UIRectQuadContainer);
@ -460,6 +460,7 @@ void CRenderTools::DrawUIElRect(CUIElement::SUIElementRect &ElUIRect, const CUIR
ElUIRect.m_Y = pRect->y;
ElUIRect.m_Width = pRect->w;
ElUIRect.m_Height = pRect->h;
ElUIRect.m_QuadColor = Color;
Graphics()->SetColor(Color);
ElUIRect.m_UIRectQuadContainer = CreateRoundRectQuadContainer(pRect->x, pRect->y, pRect->w, pRect->h, Rounding, Corners);

View file

@ -8,17 +8,34 @@
#include <engine/shared/config.h>
#include <engine/textrender.h>
void CUIElement::Init(CUI *pUI)
void CUIElement::Init(CUI *pUI, int RequestedRectCount)
{
pUI->AddUIElement(this);
if(RequestedRectCount > 0)
m_UIRects.resize(RequestedRectCount);
}
CUIElement::SUIElementRect::SUIElementRect() :
m_UIRectQuadContainer(-1), m_UITextContainer(-1), m_X(-1), m_Y(-1), m_Width(-1), m_Height(-1)
void CUIElement::InitRects(int RequestedRectCount)
{
dbg_assert(m_UIRects.size() == 0, "UI rects can only be initialized once, create another ui element instead.");
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();
mem_zero(&m_Cursor, sizeof(m_Cursor));
mem_zero(&m_TextColor, sizeof(m_TextColor));
mem_zero(&m_TextOutlineColor, sizeof(m_TextOutlineColor));
m_TextColor.Set(-1, -1, -1, -1);
m_TextOutlineColor.Set(-1, -1, -1, -1);
m_QuadColor = ColorRGBA(-1, -1, -1, -1);
}
/********************************************************
@ -54,9 +71,9 @@ CUI::~CUI()
m_OwnUIElements.clear();
}
CUIElement *CUI::GetNewUIElement()
CUIElement *CUI::GetNewUIElement(int RequestedRectCount)
{
CUIElement *pNewEl = new CUIElement(this);
CUIElement *pNewEl = new CUIElement(this, RequestedRectCount);
m_OwnUIElements.push_back(pNewEl);
@ -78,9 +95,9 @@ void CUI::ResetUIElement(CUIElement &UIElement)
if(Rect.m_UITextContainer != -1)
TextRender()->DeleteTextContainer(Rect.m_UITextContainer);
Rect.m_UITextContainer = -1;
}
UIElement.Clear();
Rect.Reset();
}
}
void CUI::OnElementsReset()
@ -407,68 +424,45 @@ int CUI::DoPickerLogic(const void *pID, const CUIRect *pRect, float *pX, float *
return 1;
}
/*
int CUI::DoButton(const void *id, const char *text, int checked, const CUIRect *r, ui_draw_button_func draw_func, const void *extra)
{
// logic
int ret = 0;
int inside = ui_MouseInside(r);
static int button_used = 0;
if(ui_ActiveItem() == id)
{
if(!ui_MouseButton(button_used))
{
if(inside && checked >= 0)
ret = 1+button_used;
ui_SetActiveItem(0);
}
}
else if(ui_HotItem() == id)
{
if(ui_MouseButton(0))
{
ui_SetActiveItem(id);
button_used = 0;
}
if(ui_MouseButton(1))
{
ui_SetActiveItem(id);
button_used = 1;
}
}
if(inside)
ui_SetHotItem(id);
if(draw_func)
draw_func(id, text, checked, r, extra);
return ret;
}*/
void CUI::DoLabel(const CUIRect *r, const char *pText, float Size, int Align, float MaxWidth, int AlignVertically)
float CUI::DoTextLabel(float x, float y, float w, float h, const char *pText, float Size, int Align, float MaxWidth, int AlignVertically, bool StopAtEnd)
{
float AlignedSize = 0;
float MaxCharacterHeightInLine = 0;
float tw = TextRender()->TextWidth(0, Size, pText, -1, MaxWidth, &AlignedSize, &MaxCharacterHeightInLine);
float AlignmentVert = r->y + (r->h - AlignedSize) / 2.f;
int Flags = TEXTFLAG_RENDER | (StopAtEnd ? TEXTFLAG_STOP_AT_END : 0);
float AlignmentVert = y + (h - AlignedSize) / 2.f;
float AlignmentHori = 0;
if(AlignVertically == 0)
{
AlignmentVert = r->y + (r->h - AlignedSize) / 2.f - (AlignedSize - MaxCharacterHeightInLine) / 2.f;
AlignmentVert = y + (h - AlignedSize) / 2.f - (AlignedSize - MaxCharacterHeightInLine) / 2.f;
}
if(Align == 0)
{
TextRender()->Text(0, r->x + (r->w - tw) / 2.f, AlignmentVert, Size, pText, MaxWidth);
AlignmentHori = x + (w - tw) / 2.f;
}
else if(Align < 0)
{
TextRender()->Text(0, r->x, AlignmentVert, Size, pText, MaxWidth);
AlignmentHori = x;
}
else if(Align > 0)
{
TextRender()->Text(0, r->x + r->w - tw, AlignmentVert, Size, pText, MaxWidth);
AlignmentHori = x + w - tw;
}
CTextCursor Cursor;
TextRender()->SetCursor(&Cursor, AlignmentHori, AlignmentVert, Size, Flags);
Cursor.m_LineWidth = (float)MaxWidth;
TextRender()->TextEx(&Cursor, pText, -1);
return tw;
}
void CUI::DoLabel(const CUIRect *r, const char *pText, float Size, int Align, float MaxWidth, int AlignVertically)
{
DoTextLabel(r->x, r->y, r->w, r->h, pText, Size, Align, MaxWidth, AlignVertically);
}
void CUI::DoLabelScaled(const CUIRect *r, const char *pText, float Size, int Align, float MaxWidth, int AlignVertically)
@ -515,17 +509,21 @@ void CUI::DoLabel(CUIElement::SUIElementRect &RectEl, const CUIRect *pRect, cons
}
Cursor.m_LineWidth = MaxWidth;
RectEl.m_UITextContainer = TextRender()->CreateTextContainer(&Cursor, pText, StrLen);
RectEl.m_Cursor = Cursor;
RectEl.m_TextColor = TextRender()->GetTextColor();
RectEl.m_TextOutlineColor = TextRender()->GetTextOutlineColor();
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;
}
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)
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)
{
bool NeedsRecreate = false;
bool ColorChanged = RectEl.m_TextColor != TextRender()->GetTextColor() || RectEl.m_TextOutlineColor != TextRender()->GetTextOutlineColor();
if(RectEl.m_UITextContainer == -1 || RectEl.m_X != pRect->x || RectEl.m_Y != pRect->y || RectEl.m_Width != pRect->w || RectEl.m_Height != pRect->h || ColorChanged)
if(RectEl.m_UITextContainer == -1 || RectEl.m_X != x || RectEl.m_Y != y || RectEl.m_Width != w || RectEl.m_Height != h || ColorChanged)
{
NeedsRecreate = true;
}
@ -542,17 +540,16 @@ void CUI::DoLabelStreamed(CUIElement::SUIElementRect &RectEl, const CUIRect *pRe
NeedsRecreate = true;
}
}
if(NeedsRecreate)
{
if(RectEl.m_UITextContainer != -1)
TextRender()->DeleteTextContainer(RectEl.m_UITextContainer);
RectEl.m_UITextContainer = -1;
RectEl.m_X = pRect->x;
RectEl.m_Y = pRect->y;
RectEl.m_Width = pRect->w;
RectEl.m_Height = pRect->h;
RectEl.m_X = x;
RectEl.m_Y = y;
RectEl.m_Width = w;
RectEl.m_Height = h;
if(StrLen > 0)
RectEl.m_Text = std::string(pText, StrLen);
@ -561,11 +558,21 @@ void CUI::DoLabelStreamed(CUIElement::SUIElementRect &RectEl, const CUIRect *pRe
else
RectEl.m_Text.clear();
DoLabel(RectEl, pRect, pText, Size, Align, MaxWidth, AlignVertically, StopAtEnd, StrLen, pReadCursor);
CUIRect TmpRect;
TmpRect.x = x;
TmpRect.y = y;
TmpRect.w = w;
TmpRect.h = h;
DoLabel(RectEl, &TmpRect, pText, Size, Align, MaxWidth, AlignVertically, StopAtEnd, StrLen, pReadCursor);
}
STextRenderColor ColorText(TextRender()->DefaultTextColor());
STextRenderColor ColorTextOutline(TextRender()->DefaultTextOutlineColor());
STextRenderColor ColorText(RectEl.m_TextColor);
STextRenderColor ColorTextOutline(RectEl.m_TextOutlineColor);
if(RectEl.m_UITextContainer != -1)
TextRender()->RenderTextContainer(RectEl.m_UITextContainer, &ColorText, &ColorTextOutline);
}
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);
}

View file

@ -18,7 +18,7 @@ public:
/**
* 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
*/
@ -123,7 +123,7 @@ class CUIElement
{
friend class CUI;
CUIElement(CUI *pUI) { Init(pUI); }
CUIElement(CUI *pUI, int RequestedRectCount) { Init(pUI, RequestedRectCount); }
public:
struct SUIElementRect
@ -145,6 +145,10 @@ public:
STextRenderColor m_TextOutlineColor;
SUIElementRect();
ColorRGBA m_QuadColor;
void Reset();
};
protected:
@ -156,24 +160,19 @@ protected:
public:
CUIElement() = default;
void Init(CUI *pUI);
void Init(CUI *pUI, int RequestedRectCount);
SUIElementRect *Get(size_t Index)
{
return &m_UIRects[Index];
}
size_t Size()
bool AreRectsInit()
{
return m_UIRects.size();
return m_UIRects.size() != 0;
}
void Clear() { m_UIRects.clear(); }
void Add(SUIElementRect &ElRect)
{
m_UIRects.push_back(ElRect);
}
void InitRects(int RequestedRectCount);
};
class CUI
@ -210,7 +209,7 @@ public:
void ResetUIElement(CUIElement &UIElement);
CUIElement *GetNewUIElement();
CUIElement *GetNewUIElement(int RequestedRectCount);
void AddUIElement(CUIElement *pElement);
void OnElementsReset();
@ -273,11 +272,12 @@ public:
int DoButtonLogic(const void *pID, const char *pText /* TODO: Refactor: Remove */, int Checked, const CUIRect *pRect);
int DoPickerLogic(const void *pID, const CUIRect *pRect, float *pX, float *pY);
// TODO: Refactor: Remove this?
float DoTextLabel(float x, float y, float w, float h, const char *pText, float Size, int Align, float MaxWidth = -1, int AlignVertically = 1, bool StopAtEnd = false);
void DoLabel(const CUIRect *pRect, const char *pText, float Size, int Align, float MaxWidth = -1, int AlignVertically = 1);
void DoLabelScaled(const CUIRect *pRect, const char *pText, float Size, int Align, float MaxWidth = -1, int AlignVertically = 1);
void DoLabel(CUIElement::SUIElementRect &RectEl, const CUIRect *pRect, const char *pText, float Size, int Align, float MaxWidth = -1, int AlignVertically = 1, bool StopAtEnd = false, int StrLen = -1, class CTextCursor *pReadCursor = NULL);
void DoLabelStreamed(CUIElement::SUIElementRect &RectEl, float x, float y, float w, float h, const char *pText, float Size, int Align, float MaxWidth = -1, int AlignVertically = 1, bool StopAtEnd = false, int StrLen = -1, class CTextCursor *pReadCursor = NULL);
void DoLabelStreamed(CUIElement::SUIElementRect &RectEl, const CUIRect *pRect, const char *pText, float Size, int Align, float MaxWidth = -1, int AlignVertically = 1, bool StopAtEnd = false, int StrLen = -1, class CTextCursor *pReadCursor = NULL);
};