2019-08-31 19:18:15 -07:00
/*
2022-07-07 09:10:14 -07:00
* Copyright (c) 2016-2022 Vera Visions LLC.
2019-08-31 19:18:15 -07:00
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2022-07-07 09:10:14 -07:00
*/
2019-08-03 10:40:34 -07:00
enumflags
{
BUTTON_HOVER,
BUTTON_DOWN,
BUTTON_LASTACTIVE
};
2023-04-03 11:13:55 -07:00
/* TODO: MOVE THESE INTO VGUIButton! COMPILER NEEDS TO BE FIXED FIRST HOWEVER. */
.void(void) tmpVGUIButton1;
class VGUIButton:VGUIControl
2019-08-03 10:40:34 -07:00
{
2022-12-30 00:15:10 -08:00
public:
void VGUIButton(void);
/** Sets the title of the button. */
nonvirtual void SetTitle(string);
/** Sets the icon of the button. */
nonvirtual void SetIcon(string);
/** Sets the color of the button text. */
nonvirtual void SetColor(vector);
/** Sets the color of the icon on the button. */
nonvirtual void SetIconColor(vector);
2023-04-03 11:13:55 -07:00
/** Gets the title of the button. */
nonvirtual string GetTitle(void);
2022-12-30 00:15:10 -08:00
/** Sets the function to call when the button is pressed. */
nonvirtual void SetFunc(void(void));
2023-04-03 11:13:55 -07:00
2022-12-30 00:15:10 -08:00
/** Sets the command to execute when the button is pressed. */
nonvirtual void SetExec(string);
/* overrides */
virtual void Draw(void);
virtual bool Input(float,float,float,float);
2023-02-14 12:54:06 -08:00
virtual void Spawned(void);
2022-10-14 13:08:47 -07:00
private:
2022-05-20 16:35:36 -07:00
vector m_vecIMGSize;
2019-08-03 10:40:34 -07:00
vector m_vecColor;
2022-05-20 16:35:36 -07:00
vector m_vecIconColor;
2019-08-03 10:40:34 -07:00
float m_flAlpha;
string m_strTitle;
string m_strTitleActive;
string m_strIcon;
2021-05-16 14:08:08 -07:00
string m_strExec;
2019-08-03 10:40:34 -07:00
};
2020-03-25 09:42:36 -07:00
void
2022-12-30 00:15:10 -08:00
VGUIButton::VGUIButton(void)
2019-08-03 10:40:34 -07:00
{
m_vecColor = UI_MAINCOLOR;
m_flAlpha = 1.0f;
2022-05-20 16:35:36 -07:00
m_vecIconColor = [1,1,1];
2022-12-30 18:01:47 -08:00
}
void
VGUIButton::Spawned(void)
{
SetSize([96,24]);
2019-08-03 10:40:34 -07:00
}
2022-05-13 14:10:07 -07:00
void
2022-12-30 00:15:10 -08:00
VGUIButton::SetColor(vector vecColor)
2022-05-13 14:10:07 -07:00
{
m_vecColor = vecColor;
}
2022-05-20 16:35:36 -07:00
void
2022-12-30 00:15:10 -08:00
VGUIButton::SetIconColor(vector vecColor)
2022-05-20 16:35:36 -07:00
{
m_vecIconColor = vecColor;
};
2020-03-25 09:42:36 -07:00
void
2022-12-30 00:15:10 -08:00
VGUIButton::SetTitle(string strName)
2019-08-03 10:40:34 -07:00
{
2022-10-14 13:08:47 -07:00
vector newsize = [0.0f, 0.0f, 0.0f];
2020-03-25 09:42:36 -07:00
2019-08-03 10:40:34 -07:00
m_strTitle = strName;
2020-03-25 09:42:36 -07:00
m_strTitleActive = sprintf("^3%s", m_strTitle);
2023-04-03 11:13:55 -07:00
//m_strTitle = sprintf("%s%s", Font_RGBtoHex(UI_MAINCOLOR), strName);
2020-03-03 13:47:44 -08:00
drawfont = g_fntDefault.iID;
2020-03-25 09:42:36 -07:00
2021-11-03 13:54:43 -07:00
newsize[0] = stringwidth(m_strTitle, TRUE, [g_fntDefault.iScaleX, g_fntDefault.iScaleY]) + 16;
2020-03-25 09:42:36 -07:00
newsize[1] = 24;
SetSize(newsize);
2019-08-03 10:40:34 -07:00
}
2023-04-03 11:13:55 -07:00
string
VGUIButton::GetTitle(void)
{
return m_strTitle;
}
2020-03-25 09:42:36 -07:00
void
2022-12-30 00:15:10 -08:00
VGUIButton::SetIcon(string strName)
2019-08-03 10:40:34 -07:00
{
m_strIcon = strName;
2022-05-20 16:35:36 -07:00
m_vecIMGSize = drawgetimagesize(strName);
if (vlen(GetSize()) < vlen(m_vecIMGSize)) {
SetSize(m_vecIMGSize + [4,4]);
}
2019-08-03 10:40:34 -07:00
}
2020-03-25 09:42:36 -07:00
void
2023-04-03 11:13:55 -07:00
VGUIButton::SetFunc(void(void) func)
2019-08-03 10:40:34 -07:00
{
2023-04-03 11:13:55 -07:00
//#warning "SetFunc in VGUIButton is DEPRECATED. Compiler IS generating JUNK in the old way. Please override VGUIButton and define a OnMouseDown/Up method instead"
//m_vFunc = vFunc;
tmpVGUIButton1 = func;
2019-08-03 10:40:34 -07:00
}
2021-05-16 14:08:08 -07:00
void
2022-12-30 00:15:10 -08:00
VGUIButton::SetExec(string exe)
2021-05-16 14:08:08 -07:00
{
m_strExec = exe;
}
2023-04-03 11:13:55 -07:00
2021-05-16 14:08:08 -07:00
void
2022-12-30 00:15:10 -08:00
VGUIButton::Draw(void)
2019-08-03 10:40:34 -07:00
{
2019-08-10 14:37:56 -07:00
#ifndef CLASSIC_VGUI
2020-03-25 09:42:36 -07:00
drawfill(m_parent.m_vecOrigin + m_vecOrigin, m_vecSize, m_vecColor, m_flAlpha);
2019-08-10 14:37:56 -07:00
2020-03-25 09:42:36 -07:00
if (m_iFlags & BUTTON_DOWN) {
drawfill(m_parent.m_vecOrigin + m_vecOrigin + [0, m_vecSize[1] - 1], [m_vecSize[0], 1], [1,1,1], 0.5f);
drawfill(m_parent.m_vecOrigin + m_vecOrigin, [m_vecSize[0], 1], [0,0,0], 0.5f);
drawfill(m_parent.m_vecOrigin + m_vecOrigin + [0, 1], [1, m_vecSize[1] - 2], [0,0,0], 0.5f);
drawfill(m_parent.m_vecOrigin + m_vecOrigin + [m_vecSize[0] - 1, 1], [1, m_vecSize[1] - 2], [1,1,1], 0.5f);
2019-08-03 10:40:34 -07:00
} else {
2020-03-25 09:42:36 -07:00
drawfill(m_parent.m_vecOrigin + m_vecOrigin + [0, m_vecSize[1] - 1], [m_vecSize[0], 1], [0,0,0], 0.5f);
drawfill(m_parent.m_vecOrigin + m_vecOrigin, [m_vecSize[0], 1], [1,1,1], 0.5f);
drawfill(m_parent.m_vecOrigin + m_vecOrigin + [0, 1], [1, m_vecSize[1] - 2], [1,1,1], 0.5f);
drawfill(m_parent.m_vecOrigin + m_vecOrigin + [m_vecSize[0] - 1, 1], [1, m_vecSize[1] - 2], [0,0,0], 0.5f);
2019-08-03 10:40:34 -07:00
}
2023-04-03 11:13:55 -07:00
if (m_strTitle) {
if (m_iFlags & BUTTON_HOVER) {
Font_DrawText(m_parent.m_vecOrigin + m_vecOrigin + [8, 8], m_strTitle, g_fntDefault);
} else {
Font_DrawText(m_parent.m_vecOrigin + m_vecOrigin + [8, 8], m_strTitle, g_fntDefault);
}
}
if (m_strIcon) {
if (m_iFlags & BUTTON_DOWN)
drawpic(m_parent.m_vecOrigin + m_vecOrigin + [2,2], m_strIcon, m_vecIMGSize, m_vecIconColor * 0.25, 1.0f, 0);
else
drawpic(m_parent.m_vecOrigin + m_vecOrigin + [2,2], m_strIcon, m_vecIMGSize, m_vecIconColor, 1.0f, 0);
}
2019-08-10 14:37:56 -07:00
#else
2020-03-25 09:42:36 -07:00
if (m_iFlags & BUTTON_DOWN) {
drawfill(m_parent.m_vecOrigin + m_vecOrigin, m_vecSize, m_vecColor, 0.25f);
2019-08-10 14:37:56 -07:00
}
2020-03-25 09:42:36 -07:00
drawfill(m_parent.m_vecOrigin + m_vecOrigin + [0, m_vecSize[1] - 1], [m_vecSize[0], 1], m_vecColor, 1.0f);
drawfill(m_parent.m_vecOrigin + m_vecOrigin, [m_vecSize[0], 1], m_vecColor, 1.0f);
drawfill(m_parent.m_vecOrigin + m_vecOrigin + [0, 1], [1, m_vecSize[1] - 2], m_vecColor, 1.0f);
drawfill(m_parent.m_vecOrigin + m_vecOrigin + [m_vecSize[0] - 1, 1], [1, m_vecSize[1] - 2], m_vecColor, 1.0f);
2023-04-03 11:13:55 -07:00
2019-08-10 14:37:56 -07:00
2020-03-25 09:42:36 -07:00
if (m_strTitle) {
if (m_iFlags & BUTTON_LASTACTIVE) {
Font_DrawText(m_parent.m_vecOrigin + m_vecOrigin + [8, 8], m_strTitleActive, g_fntDefault);
2019-08-03 10:40:34 -07:00
} else {
2020-03-25 09:42:36 -07:00
Font_DrawText(m_parent.m_vecOrigin + m_vecOrigin + [8, 8], m_strTitle, g_fntDefault);
2019-08-03 10:40:34 -07:00
}
}
2020-03-25 09:42:36 -07:00
if (m_strIcon) {
2022-05-20 16:35:36 -07:00
if (m_iFlags & BUTTON_DOWN)
drawpic(m_parent.m_vecOrigin + m_vecOrigin + [2,2], m_strIcon, m_vecIMGSize, m_vecIconColor * 0.25, 1.0f, 0);
else
drawpic(m_parent.m_vecOrigin + m_vecOrigin + [2,2], m_strIcon, m_vecIMGSize, m_vecIconColor, 1.0f, 0);
2019-08-03 10:40:34 -07:00
}
2023-04-03 11:13:55 -07:00
#endif
2019-08-03 10:40:34 -07:00
}
2022-05-24 13:04:48 -07:00
bool
2022-12-30 00:15:10 -08:00
VGUIButton::Input(float flEVType, float flKey, float flChar, float flDevID)
2019-08-03 10:40:34 -07:00
{
2022-05-24 13:04:48 -07:00
bool ret = false;
2023-04-03 11:13:55 -07:00
bool mouseHover = false;
if (Util_MouseAbove(g_vecMousePos, m_parent.m_vecOrigin + m_vecOrigin, m_vecSize)) {
mouseHover = true;
}
if (mouseHover == true && HasFlag(BUTTON_HOVER) == false) {
FlagAdd(BUTTON_HOVER);
OnMouseEntered();
} else if (HasFlag(BUTTON_HOVER)) {
FlagRemove(BUTTON_HOVER);
OnMouseExited();
}
2020-03-25 09:42:36 -07:00
if (flEVType == IE_KEYDOWN) {
if (flKey == K_MOUSE1) {
FlagRemove(BUTTON_LASTACTIVE);
2023-04-03 11:13:55 -07:00
if (mouseHover) {
if (tmpVGUIButton1)
tmpVGUIButton1();
OnMouseDown();
2020-03-25 09:42:36 -07:00
FlagAdd(BUTTON_DOWN);
FlagAdd(BUTTON_LASTACTIVE);
2022-05-24 13:04:48 -07:00
ret = true;
2019-08-03 10:40:34 -07:00
}
}
2020-03-25 09:42:36 -07:00
} else if (flEVType == IE_KEYUP) {
if (flKey == K_MOUSE1) {
2023-04-03 11:13:55 -07:00
if (m_iFlags & BUTTON_DOWN && mouseHover) {
OnMouseUp();
2021-05-16 14:08:08 -07:00
if (m_strExec)
localcmd(sprintf("%s\n", m_strExec));
2022-05-24 13:04:48 -07:00
ret = true;
2019-08-03 10:40:34 -07:00
}
2020-03-25 09:42:36 -07:00
FlagRemove(BUTTON_DOWN);
2019-08-03 10:40:34 -07:00
}
}
2022-05-24 13:04:48 -07:00
return (ret);
2019-08-03 10:40:34 -07:00
}