ddnet/src/game/client/ui.h

336 lines
9.9 KiB
C
Raw Normal View History

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. */
2010-05-29 07:25:38 +00:00
#ifndef GAME_CLIENT_UI_H
#define GAME_CLIENT_UI_H
#include "ui_rect.h"
#include <engine/input.h>
2020-10-12 10:29:47 +00:00
#include <engine/textrender.h>
2022-05-18 16:00:05 +00:00
#include <chrono>
2020-10-12 10:29:47 +00:00
#include <string>
#include <vector>
2022-08-10 21:00:25 +00:00
class IClient;
class IGraphics;
class IKernel;
2020-10-26 01:10:55 +00:00
struct SUIAnimator
{
bool m_Active;
bool m_ScaleLabel;
2020-10-26 03:10:58 +00:00
bool m_RepositionLabel;
2022-05-18 16:00:05 +00:00
std::chrono::nanoseconds m_Time;
2020-10-26 01:10:55 +00:00
float m_Value;
float m_XOffset;
float m_YOffset;
float m_WOffset;
float m_HOffset;
2020-10-26 01:10:55 +00:00
};
class IScrollbarScale
{
public:
virtual float ToRelative(int AbsoluteValue, int Min, int Max) const = 0;
virtual int ToAbsolute(float RelativeValue, int Min, int Max) const = 0;
};
class CLinearScrollbarScale : public IScrollbarScale
{
public:
float ToRelative(int AbsoluteValue, int Min, int Max) const override
{
return (AbsoluteValue - Min) / (float)(Max - Min);
}
int ToAbsolute(float RelativeValue, int Min, int Max) const override
{
return round_to_int(RelativeValue * (Max - Min) + Min + 0.1f);
}
};
class CLogarithmicScrollbarScale : public IScrollbarScale
{
private:
int m_MinAdjustment;
public:
CLogarithmicScrollbarScale(int MinAdjustment)
{
m_MinAdjustment = maximum(MinAdjustment, 1); // must be at least 1 to support Min == 0 with logarithm
}
float ToRelative(int AbsoluteValue, int Min, int Max) const override
{
if(Min < m_MinAdjustment)
{
AbsoluteValue += m_MinAdjustment;
Min += m_MinAdjustment;
Max += m_MinAdjustment;
}
return (log(AbsoluteValue) - log(Min)) / (float)(log(Max) - log(Min));
}
int ToAbsolute(float RelativeValue, int Min, int Max) const override
{
int ResultAdjustment = 0;
if(Min < m_MinAdjustment)
{
Min += m_MinAdjustment;
Max += m_MinAdjustment;
ResultAdjustment = -m_MinAdjustment;
}
return round_to_int(exp(RelativeValue * (log(Max) - log(Min)) + log(Min))) + ResultAdjustment;
}
};
struct SUIExEditBoxProperties
{
bool m_SelectText = false;
const char *m_pEmptyText = "";
};
2020-10-12 10:29:47 +00:00
class CUI;
class CUIElement
{
friend class CUI;
CUI *m_pUI;
2021-09-13 21:48:10 +00:00
CUIElement(CUI *pUI, int RequestedRectCount) { Init(pUI, RequestedRectCount); }
2020-10-12 10:29:47 +00:00
public:
struct SUIElementRect
{
CUIElement *m_pParent;
2020-10-12 10:29:47 +00:00
public:
int m_UIRectQuadContainer;
int m_UITextContainer;
float m_X;
float m_Y;
float m_Width;
float m_Height;
std::string m_Text;
CTextCursor m_Cursor;
ColorRGBA m_TextColor;
ColorRGBA m_TextOutlineColor;
2020-11-08 18:41:16 +00:00
2020-11-25 12:05:53 +00:00
SUIElementRect();
2021-09-13 21:48:10 +00:00
ColorRGBA m_QuadColor;
void Reset();
void Draw(const CUIRect *pRect, ColorRGBA Color, int Corners, float Rounding);
2020-10-12 10:29:47 +00:00
};
protected:
CUI *UI() const { return m_pUI; }
std::vector<SUIElementRect> m_vUIRects;
2020-10-12 10:29:47 +00:00
// used for marquees or other user implemented things
2021-06-23 05:05:49 +00:00
int64_t m_ElementTime;
2020-10-12 10:29:47 +00:00
public:
CUIElement() = default;
2021-09-13 21:48:10 +00:00
void Init(CUI *pUI, int RequestedRectCount);
2020-10-12 10:29:47 +00:00
SUIElementRect *Get(size_t Index)
{
return &m_vUIRects[Index];
2020-10-12 10:29:47 +00:00
}
2021-09-13 21:48:10 +00:00
bool AreRectsInit()
2020-10-12 10:29:47 +00:00
{
return !m_vUIRects.empty();
2020-10-12 10:29:47 +00:00
}
2021-09-13 21:48:10 +00:00
void InitRects(int RequestedRectCount);
2020-10-12 10:29:47 +00:00
};
2022-03-11 16:34:48 +00:00
struct SLabelProperties
{
float m_MaxWidth = -1;
int m_AlignVertically = 1;
bool m_StopAtEnd = false;
class CTextCursor *m_pSelCursor = nullptr;
bool m_EnableWidthCheck = true;
};
2022-07-16 13:32:06 +00:00
class CButtonContainer
{
};
2009-10-27 14:38:53 +00:00
class CUI
2008-01-12 12:27:55 +00:00
{
bool m_Enabled;
2009-10-27 14:38:53 +00:00
const void *m_pHotItem;
const void *m_pActiveItem;
const void *m_pLastActiveItem;
const void *m_pBecomingHotItem;
2022-04-07 07:46:02 +00:00
const void *m_pActiveTooltipItem;
bool m_ActiveItemValid = false;
2010-05-29 07:25:38 +00:00
float m_MouseX, m_MouseY; // in gui space
2020-12-14 00:51:31 +00:00
float m_MouseDeltaX, m_MouseDeltaY; // in gui space
2010-05-29 07:25:38 +00:00
float m_MouseWorldX, m_MouseWorldY; // in world space
2009-10-27 14:38:53 +00:00
unsigned m_MouseButtons;
unsigned m_LastMouseButtons;
bool m_MouseSlow = false;
IInput::CEvent *m_pInputEventsArray;
int *m_pInputEventCount;
bool m_MouseIsPress = false;
bool m_HasSelection = false;
int m_MousePressX = 0;
int m_MousePressY = 0;
int m_MouseCurX = 0;
int m_MouseCurY = 0;
int m_CurSelStart = 0;
int m_CurSelEnd = 0;
const void *m_pSelItem = nullptr;
int m_CurCursor = 0;
2009-10-27 14:38:53 +00:00
CUIRect m_Screen;
2022-05-13 18:20:04 +00:00
std::vector<CUIRect> m_vClips;
2022-05-13 18:20:04 +00:00
void UpdateClipping();
2022-08-10 21:00:25 +00:00
IClient *m_pClient;
IGraphics *m_pGraphics;
IInput *m_pInput;
ITextRender *m_pTextRender;
std::vector<CUIElement *> m_vpOwnUIElements; // ui elements maintained by CUI class
std::vector<CUIElement *> m_vpUIElements;
2020-10-12 10:29:47 +00:00
2009-10-27 14:38:53 +00:00
public:
static const CLinearScrollbarScale ms_LinearScrollbarScale;
static const CLogarithmicScrollbarScale ms_LogarithmicScrollbarScale;
2021-12-03 18:49:22 +00:00
static float ms_FontmodHeight;
void Init(IKernel *pKernel);
void InitInputs(IInput::CEvent *pInputEventsArray, int *pInputEventCount);
2022-08-10 21:00:25 +00:00
IClient *Client() const { return m_pClient; }
IGraphics *Graphics() const { return m_pGraphics; }
IInput *Input() const { return m_pInput; }
ITextRender *TextRender() const { return m_pTextRender; }
2008-01-12 12:27:55 +00:00
2009-10-27 14:38:53 +00:00
CUI();
2020-10-12 10:29:47 +00:00
~CUI();
void ResetUIElement(CUIElement &UIElement);
2021-09-13 21:48:10 +00:00
CUIElement *GetNewUIElement(int RequestedRectCount);
2020-10-12 10:29:47 +00:00
void AddUIElement(CUIElement *pElement);
void OnElementsReset();
void OnWindowResize();
void OnLanguageChange();
2007-05-22 15:03:32 +00:00
void SetEnabled(bool Enabled) { m_Enabled = Enabled; }
bool Enabled() const { return m_Enabled; }
void Update(float MouseX, float MouseY, float MouseWorldX, float MouseWorldY);
2007-05-22 15:03:32 +00:00
2020-12-14 00:51:31 +00:00
float MouseDeltaX() const { return m_MouseDeltaX; }
float MouseDeltaY() const { return m_MouseDeltaY; }
2009-10-27 14:38:53 +00:00
float MouseX() const { return m_MouseX; }
float MouseY() const { return m_MouseY; }
float MouseWorldX() const { return m_MouseWorldX; }
float MouseWorldY() const { return m_MouseWorldY; }
int MouseButton(int Index) const { return (m_MouseButtons >> Index) & 1; }
int MouseButtonClicked(int Index) const { return MouseButton(Index) && !((m_LastMouseButtons >> Index) & 1); }
int MouseButtonReleased(int Index) const { return ((m_LastMouseButtons >> Index) & 1) && !MouseButton(Index); }
2008-01-12 12:27:55 +00:00
void SetHotItem(const void *pID) { m_pBecomingHotItem = pID; }
void SetActiveItem(const void *pID)
{
m_ActiveItemValid = true;
m_pActiveItem = pID;
if(pID)
m_pLastActiveItem = pID;
}
bool CheckActiveItem(const void *pID)
{
if(m_pActiveItem == pID)
{
m_ActiveItemValid = true;
return true;
}
return false;
}
2022-04-07 07:46:02 +00:00
void SetActiveTooltipItem(const void *pID) { m_pActiveTooltipItem = pID; }
void ClearLastActiveItem() { m_pLastActiveItem = nullptr; }
2009-10-27 14:38:53 +00:00
const void *HotItem() const { return m_pHotItem; }
const void *NextHotItem() const { return m_pBecomingHotItem; }
2009-10-27 14:38:53 +00:00
const void *ActiveItem() const { return m_pActiveItem; }
2022-04-07 07:46:02 +00:00
const void *ActiveTooltipItem() const { return m_pActiveTooltipItem; }
2009-10-27 14:38:53 +00:00
const void *LastActiveItem() const { return m_pLastActiveItem; }
2008-01-12 12:27:55 +00:00
void StartCheck() { m_ActiveItemValid = false; }
void FinishCheck()
{
if(!m_ActiveItemValid && m_pActiveItem != nullptr)
{
SetActiveItem(nullptr);
m_pHotItem = nullptr;
m_pBecomingHotItem = nullptr;
}
}
2021-11-26 20:55:31 +00:00
bool MouseInside(const CUIRect *pRect) const;
bool MouseInsideClip() const { return !IsClipped() || MouseInside(ClipArea()); }
bool MouseHovered(const CUIRect *pRect) const { return MouseInside(pRect) && MouseInsideClip(); }
void ConvertMouseMove(float *pX, float *pY, IInput::ECursorType CursorType) const;
void ResetMouseSlow() { m_MouseSlow = false; }
2008-01-12 12:27:55 +00:00
2021-11-26 20:55:31 +00:00
float ButtonColorMulActive() { return 0.5f; }
float ButtonColorMulHot() { return 1.5f; }
float ButtonColorMulDefault() { return 1.0f; }
float ButtonColorMul(const void *pID);
2009-10-27 14:38:53 +00:00
CUIRect *Screen();
2021-09-22 19:48:55 +00:00
void MapScreen();
float PixelSize();
2022-05-13 18:20:04 +00:00
2010-05-29 07:25:38 +00:00
void ClipEnable(const CUIRect *pRect);
2009-10-27 14:38:53 +00:00
void ClipDisable();
2022-05-13 18:20:04 +00:00
const CUIRect *ClipArea() const;
inline bool IsClipped() const { return !m_vClips.empty(); }
2020-10-12 10:29:47 +00:00
int DoButtonLogic(const void *pID, int Checked, const CUIRect *pRect);
int DoPickerLogic(const void *pID, const CUIRect *pRect, float *pX, float *pY);
2022-08-10 21:00:25 +00:00
void DoSmoothScrollLogic(float *pScrollOffset, float *pScrollOffsetChange, float ViewPortSize, float TotalSize, float ScrollSpeed = 10.0f);
2022-03-11 16:34:48 +00:00
float DoTextLabel(float x, float y, float w, float h, const char *pText, float Size, int Align, const SLabelProperties &LabelProps = {});
void DoLabel(const CUIRect *pRect, const char *pText, float Size, int Align, const SLabelProperties &LabelProps = {});
2020-10-12 10:29:47 +00:00
void DoLabel(CUIElement::SUIElementRect &RectEl, const CUIRect *pRect, const char *pText, float Size, int Align, const SLabelProperties &LabelProps, int StrLen = -1, class CTextCursor *pReadCursor = nullptr);
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 = nullptr);
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 = nullptr);
bool DoEditBox(const void *pID, const CUIRect *pRect, char *pStr, unsigned StrSize, float FontSize, float *pOffset, bool Hidden = false, int Corners = IGraphics::CORNER_ALL, const SUIExEditBoxProperties &Properties = {});
bool DoClearableEditBox(const void *pID, const void *pClearID, const CUIRect *pRect, char *pStr, unsigned StrSize, float FontSize, float *pOffset, bool Hidden = false, int Corners = IGraphics::CORNER_ALL, const SUIExEditBoxProperties &Properties = {});
enum
{
SCROLLBAR_OPTION_INFINITE = 1,
SCROLLBAR_OPTION_NOCLAMPVALUE = 2,
};
float DoScrollbarV(const void *pID, const CUIRect *pRect, float Current);
float DoScrollbarH(const void *pID, const CUIRect *pRect, float Current, const ColorRGBA *pColorInner = nullptr);
void DoScrollbarOption(const void *pID, int *pOption, const CUIRect *pRect, const char *pStr, int Min, int Max, const IScrollbarScale *pScale = &ms_LinearScrollbarScale, unsigned Flags = 0u);
void DoScrollbarOptionLabeled(const void *pID, int *pOption, const CUIRect *pRect, const char *pStr, const char **ppLabels, int NumLabels, const IScrollbarScale *pScale = &ms_LinearScrollbarScale);
2009-10-27 14:38:53 +00:00
};
2007-05-22 15:03:32 +00:00
#endif