From 5e5b850ec187f8d92c747ca7789b6ab412312b91 Mon Sep 17 00:00:00 2001 From: Marco Cawthorne Date: Mon, 24 Feb 2025 17:22:07 -0800 Subject: [PATCH] vguiButton: Change behaviour under which a button acts as disabled. vguiWidget: add generic Enable, Disable accessor methods. --- src/vgui/Button.qc | 25 ++++++++++++++++++++----- src/vgui/Widget.h | 9 +++++++++ src/vgui/Widget.qc | 29 +++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 5 deletions(-) diff --git a/src/vgui/Button.qc b/src/vgui/Button.qc index 422794e1..fc94e8a2 100644 --- a/src/vgui/Button.qc +++ b/src/vgui/Button.qc @@ -57,6 +57,8 @@ public: /** Sets the command to execute when the button is pressed. */ nonvirtual void SetExec(string); + nonvirtual bool ButtonDisabled(void); + /* overrides */ virtual void Draw(void); virtual bool Input(float,float,float,float); @@ -76,6 +78,7 @@ private: void vguiButton::vguiButton(void) { + m_strExec = __NULL__; m_vecColor = UI_MAINCOLOR; m_flAlpha = 1.0f; m_vecIconColor = [1,1,1]; @@ -158,13 +161,26 @@ vguiButton::GetKeyEquivalent(void) return keynumtostring(m_keyEquivalent); } +bool +vguiButton::ButtonDisabled(void) +{ + if (STRING_SET(m_strExec)) { + return (false); + } + + if (tmpvguiButton1) { + return (false); + } + + return (true); +} void vguiButton::Draw(void) { vguiTheme theme = GetTheme(); - if (!tmpvguiButton1) { + if (IsEnabled() == false) { theme.DrawButton(m_parent.m_vecOrigin + m_vecOrigin, m_vecSize, VGUI_STATE_DISABLED); } else if (m_iFlags & BUTTON_DOWN) { theme.DrawButton(m_parent.m_vecOrigin + m_vecOrigin, m_vecSize, VGUI_STATE_ACTIVE); @@ -207,11 +223,9 @@ bool vguiButton::Input(float flEVType, float flKey, float flChar, float flDevID) { bool ret = false; - bool mouseHover = false; - - if (!tmpvguiButton1) { + if (IsEnabled() == false) { return (false); } @@ -254,8 +268,9 @@ vguiButton::Input(float flEVType, float flKey, float flChar, float flDevID) if (tmpvguiButton1) tmpvguiButton1(); - if (m_strExec) + if (STRING_SET(m_strExec)) { localcmd(sprintf("%s\n", m_strExec)); + } ret = true; } diff --git a/src/vgui/Widget.h b/src/vgui/Widget.h index 85ea58b3..7f5b57f6 100644 --- a/src/vgui/Widget.h +++ b/src/vgui/Widget.h @@ -64,6 +64,14 @@ public: virtual void NowVisible(void); virtual void NowHidden(void); + /** Enable the widget. */ + nonvirtual void Enable(void); + /** Disable the widget. */ + nonvirtual void Disable(void); + + virtual void NowEnabled(void); + virtual void NowDisabled(void); + /** Called in order to draw the widget. */ virtual void Draw(void); /** Called whenever the physical properties of the display change. */ @@ -87,5 +95,6 @@ private: vguiWidget m_children; int m_iFlags; bool m_bVisible; + bool m_bEnabled; vguiTheme m_theme; }; diff --git a/src/vgui/Widget.qc b/src/vgui/Widget.qc index 7f2ee283..bb957d6d 100644 --- a/src/vgui/Widget.qc +++ b/src/vgui/Widget.qc @@ -9,6 +9,7 @@ vguiWidget::vguiWidget(void) m_parent = __NULL__; m_iFlags = 0i; m_bVisible = true; + m_bEnabled = true; isVGUI = true; Spawned(); @@ -20,6 +21,34 @@ vguiWidget::SetTheme(vguiTheme theme) m_theme = theme; } +bool +vguiWidget::IsEnabled(void) +{ + return (m_bEnabled); +} + +void +vguiWidget::Enable(void) +{ + m_bEnabled = true; +} + +void +vguiWidget::Disable(void) +{ + m_bEnabled = false; +} + +void +vguiWidget::NowEnabled(void) +{ +} + +void +vguiWidget::NowDisabled(void) +{ +} + vguiTheme vguiWidget::GetTheme(void) {