1 Star 0 Fork 0

yoyojacky/CSMoE

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Button.cpp 30.33 KB
一键复制 编辑 原始数据 按行查看 历史
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
//========= Copyright ?1996-2005, Valve Corporation, All rights reserved. ============//
//
// Purpose: Basic button control
//
// $NoKeywords: $
//=============================================================================//
#include <stdio.h>
#include <tier1/KeyValues.h>
#include <tier1/utlsymbol.h>
#include <vgui/IBorder.h>
#include <vgui/IInputInternal.h>
#include <vgui/IScheme.h>
#include <vgui/ISurface.h>
#include <vgui/ISystem.h>
#include <vgui/IVGUI.h>
#include <vgui/MouseCode.h>
#include <vgui/KeyCode.h>
#include "Button.h"
#include "FocusNavGroup.h"
// memdbgon must be the last include file in a .cpp file!!!
#include <tier0/memdbgon.h>
using namespace vgui2;
// global list of all the names of all the sounds played by buttons
CUtlSymbolTable g_ButtonSoundNames;
DECLARE_BUILD_FACTORY_DEFAULT_TEXT( Button, Button );
//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
Button::Button(Panel *parent, const char *panelName, const char *text, Panel *pActionSignalTarget, const char *pCmd ) : Label(parent, panelName, text)
{
Init();
if ( pActionSignalTarget && pCmd )
{
AddActionSignalTarget( pActionSignalTarget );
SetCommand( pCmd );
}
}
//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
Button::Button(Panel *parent, const char *panelName, const wchar_t *wszText, Panel *pActionSignalTarget, const char *pCmd ) : Label(parent, panelName, wszText)
{
Init();
if ( pActionSignalTarget && pCmd )
{
AddActionSignalTarget( pActionSignalTarget );
SetCommand( pCmd );
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void Button::Init()
{
_buttonFlags.SetFlag( USE_CAPTURE_MOUSE | BUTTON_BORDER_ENABLED );
_mouseClickMask = 0;
_actionMessage = NULL;
_defaultBorder = NULL;
_depressedBorder = NULL;
_keyFocusBorder = NULL;
m_bSelectionStateSaved = false;
m_sArmedSoundName = UTL_INVAL_SYMBOL;
m_sDepressedSoundName = UTL_INVAL_SYMBOL;
m_sReleasedSoundName = UTL_INVAL_SYMBOL;
SetTextInset(6, 0);
SetMouseClickEnabled( MOUSE_LEFT, true );
SetButtonActivationType(ACTIVATE_ONPRESSEDANDRELEASED);
// labels have this off by default, but we need it on
SetPaintBackgroundEnabled( true );
// Modified to fit in CSO Style
SetContentAlignment(a_center);
_paint = true;
_imageBackground = false;
}
//-----------------------------------------------------------------------------
// Purpose: Destructor
//-----------------------------------------------------------------------------
Button::~Button()
{
if (_actionMessage)
{
_actionMessage->deleteThis();
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void Button::SetButtonActivationType(ActivationType_t activationType)
{
_activationType = activationType;
}
//-----------------------------------------------------------------------------
// Purpose: Set button border attribute enabled.
//-----------------------------------------------------------------------------
void Button::SetButtonBorderEnabled( bool state )
{
if ( state != _buttonFlags.IsFlagSet( BUTTON_BORDER_ENABLED ) )
{
_buttonFlags.SetFlag( BUTTON_BORDER_ENABLED, state );
InvalidateLayout(false);
}
}
//-----------------------------------------------------------------------------
// Purpose: Set button selected state.
//-----------------------------------------------------------------------------
void Button::SetSelected( bool state )
{
if ( _buttonFlags.IsFlagSet( SELECTED ) != state )
{
_buttonFlags.SetFlag( SELECTED, state );
RecalculateDepressedState();
InvalidateLayout(false);
}
}
void Button::SetBlink( bool state )
{
if ( _buttonFlags.IsFlagSet( BLINK ) != state )
{
_buttonFlags.SetFlag( BLINK, state );
RecalculateDepressedState();
InvalidateLayout(false);
}
}
//-----------------------------------------------------------------------------
// Purpose: Set button force depressed state.
//-----------------------------------------------------------------------------
void Button::ForceDepressed(bool state)
{
if ( _buttonFlags.IsFlagSet( FORCE_DEPRESSED ) != state )
{
_buttonFlags.SetFlag( FORCE_DEPRESSED, state );
RecalculateDepressedState();
InvalidateLayout(false);
}
}
//-----------------------------------------------------------------------------
// Purpose: Set button depressed state with respect to the force depressed state.
//-----------------------------------------------------------------------------
void Button::RecalculateDepressedState( void )
{
bool newState;
if (!IsEnabled())
{
newState = false;
}
else
{
newState = _buttonFlags.IsFlagSet( FORCE_DEPRESSED ) ? true : (_buttonFlags.IsFlagSet(ARMED) && _buttonFlags.IsFlagSet( SELECTED ) );
}
_buttonFlags.SetFlag( DEPRESSED, newState );
}
//-----------------------------------------------------------------------------
// Purpose: Sets whether or not the button captures all mouse input when depressed
// Defaults to true
// Should be set to false for things like menu items where there is a higher-level mouse capture
//-----------------------------------------------------------------------------
void Button::SetUseCaptureMouse( bool state )
{
_buttonFlags.SetFlag( USE_CAPTURE_MOUSE, state );
}
//-----------------------------------------------------------------------------
// Purpose: Check if mouse capture is enabled.
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
bool Button::IsUseCaptureMouseEnabled( void )
{
return _buttonFlags.IsFlagSet( USE_CAPTURE_MOUSE );
}
//-----------------------------------------------------------------------------
// Purpose: Set armed state.
//-----------------------------------------------------------------------------
void Button::SetArmed(bool state)
{
if ( _buttonFlags.IsFlagSet( ARMED ) != state )
{
_buttonFlags.SetFlag( ARMED, state );
RecalculateDepressedState();
InvalidateLayout(false);
// play any sounds specified
if (state && m_sArmedSoundName != UTL_INVAL_SYMBOL)
{
surface()->PlaySound(g_ButtonSoundNames.String(m_sArmedSoundName));
}
}
}
//-----------------------------------------------------------------------------
// Purpose: Check armed state
//-----------------------------------------------------------------------------
bool Button::IsArmed()
{
return _buttonFlags.IsFlagSet( ARMED );
}
KeyValues *Button::GetActionMessage()
{
return _actionMessage->MakeCopy();
}
void Button::PlayButtonReleasedSound()
{
// check for playing a transition sound
if ( m_sReleasedSoundName != UTL_INVAL_SYMBOL )
{
surface()->PlaySound( g_ButtonSoundNames.String( m_sReleasedSoundName ) );
}
}
//-----------------------------------------------------------------------------
// Purpose: Activate a button click.
//-----------------------------------------------------------------------------
void Button::DoClick()
{
SetSelected(true);
FireActionSignal();
PlayButtonReleasedSound();
SetSelected(false);
}
//-----------------------------------------------------------------------------
// Purpose: Check selected state
//-----------------------------------------------------------------------------
bool Button::IsSelected()
{
return _buttonFlags.IsFlagSet( SELECTED );
}
//-----------------------------------------------------------------------------
// Purpose: Check depressed state
//-----------------------------------------------------------------------------
bool Button::IsDepressed()
{
return _buttonFlags.IsFlagSet( DEPRESSED );
}
bool Button::IsBlinking( void )
{
return _buttonFlags.IsFlagSet( BLINK );
}
//-----------------------------------------------------------------------------
// Drawing focus box?
//-----------------------------------------------------------------------------
bool Button::IsDrawingFocusBox()
{
return _buttonFlags.IsFlagSet( DRAW_FOCUS_BOX );
}
void Button::DrawFocusBox( bool bEnable )
{
_buttonFlags.SetFlag( DRAW_FOCUS_BOX, bEnable );
}
//-----------------------------------------------------------------------------
// Purpose: Paint button on screen
//-----------------------------------------------------------------------------
void Button::Paint(void)
{
if ( !ShouldPaint() )
return;
BaseClass::Paint();
if ( HasFocus() && IsEnabled() && IsDrawingFocusBox() )
{
int x0, y0, x1, y1;
int wide, tall;
GetSize(wide, tall);
x0 = 3, y0 = 3, x1 = wide - 4 , y1 = tall - 2;
DrawFocusBorder(x0, y0, x1, y1);
}
}
void Button::PaintBackground(void)
{
if (_imageBackground)
{
int wide, tall;
GetSize(wide, tall);
const int iOffset = 0;
IImage **ppimage;
if (_buttonFlags.IsFlagSet(DEPRESSED))
ppimage = _depressedImage;
else if (_buttonFlags.IsFlagSet(ARMED))
ppimage = _armedImage;
else
ppimage = _defaultImage;
ppimage[0]->SetPos(0, 0);
ppimage[0]->SetSize(10, tall);
ppimage[0]->Paint();
ppimage[1]->SetPos(10, 0);
ppimage[1]->SetSize(wide - 20 - iOffset, tall);
ppimage[1]->Paint();
ppimage[2]->SetPos(wide - 10 - iOffset, 0);
ppimage[2]->SetSize(10 - iOffset, tall);
ppimage[2]->Paint();
return;
}
BaseClass::PaintBackground();
}
//-----------------------------------------------------------------------------
// Purpose: Perform graphical layout of button.
//-----------------------------------------------------------------------------
void Button::PerformLayout()
{
// reset our border
SetBorder( GetBorder(_buttonFlags.IsFlagSet( DEPRESSED ), _buttonFlags.IsFlagSet( ARMED ), _buttonFlags.IsFlagSet( SELECTED ), HasFocus() ) );
// set our color
SetFgColor(GetButtonFgColor());
SetBgColor(GetButtonBgColor());
BaseClass::PerformLayout();
}
//-----------------------------------------------------------------------------
// Purpose: Get button foreground color
// Output : Color
//-----------------------------------------------------------------------------
Color Button::GetButtonFgColor()
{
if ( !_buttonFlags.IsFlagSet( BLINK ) )
{
if (_buttonFlags.IsFlagSet( DEPRESSED ))
return _depressedFgColor;
if (_buttonFlags.IsFlagSet( ARMED ))
return _armedFgColor;
return _defaultFgColor;
}
Color cBlendedColor;
if (_buttonFlags.IsFlagSet( DEPRESSED ))
cBlendedColor = _depressedFgColor;
else if (_buttonFlags.IsFlagSet( ARMED ))
cBlendedColor = _armedFgColor;
else
cBlendedColor = _defaultFgColor;
float fBlink = ( sinf( system()->GetTimeMillis() * 0.01f ) + 1.0f ) * 0.5f;
if ( _buttonFlags.IsFlagSet( BLINK ) )
{
cBlendedColor[ 0 ] = (float)cBlendedColor[ 0 ] * fBlink + (float)_blinkFgColor[ 0 ] * ( 1.0f - fBlink );
cBlendedColor[ 1 ] = (float)cBlendedColor[ 1 ] * fBlink + (float)_blinkFgColor[ 1 ] * ( 1.0f - fBlink );
cBlendedColor[ 2 ] = (float)cBlendedColor[ 2 ] * fBlink + (float)_blinkFgColor[ 2 ] * ( 1.0f - fBlink );
cBlendedColor[ 3 ] = (float)cBlendedColor[ 3 ] * fBlink + (float)_blinkFgColor[ 3 ] * ( 1.0f - fBlink );
}
return cBlendedColor;
}
//-----------------------------------------------------------------------------
// Purpose: Get button background color
//-----------------------------------------------------------------------------
Color Button::GetButtonBgColor()
{
if (_buttonFlags.IsFlagSet( DEPRESSED ))
return _depressedBgColor;
if (_buttonFlags.IsFlagSet( ARMED ))
return _armedBgColor;
return _defaultBgColor;
}
//-----------------------------------------------------------------------------
// Purpose: Called when key focus is received
//-----------------------------------------------------------------------------
void Button::OnSetFocus()
{
InvalidateLayout(false);
BaseClass::OnSetFocus();
}
//-----------------------------------------------------------------------------
// Purpose: Respond when focus is killed
//-----------------------------------------------------------------------------
void Button::OnKillFocus()
{
InvalidateLayout(false);
BaseClass::OnKillFocus();
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void Button::ApplySchemeSettings(IScheme *pScheme)
{
BaseClass::ApplySchemeSettings(pScheme);
// get the borders we need
_defaultBorder = pScheme->GetBorder("ButtonBorder");
_depressedBorder = pScheme->GetBorder("ButtonDepressedBorder");
_keyFocusBorder = pScheme->GetBorder("ButtonKeyFocusBorder");
_defaultFgColor = GetSchemeColor("Button.TextColor", GetSchemeColor("BaseText", Color(255, 255, 255, 255), pScheme), pScheme);
_defaultBgColor = GetSchemeColor("Button.BgColor", GetSchemeColor("BgColor", Color(0, 0, 0, 255), pScheme), pScheme);
_armedFgColor = GetSchemeColor("Button.ArmedTextColor", GetSchemeColor("ButtonArmedFgColor", _defaultFgColor, pScheme), pScheme);
_armedBgColor = GetSchemeColor("Button.ArmedBgColor", GetSchemeColor("ButtonArmedBgColor", _defaultBgColor, pScheme), pScheme);
_depressedFgColor = GetSchemeColor("Button.DepressedTextColor", GetSchemeColor("ButtonDepressedFgColor", _defaultFgColor, pScheme), pScheme);
_depressedBgColor = GetSchemeColor("Button.DepressedBgColor", GetSchemeColor("ButtonDepressedBgColor", _defaultBgColor, pScheme), pScheme);
_keyboardFocusColor = GetSchemeColor("Button.FocusBorderColor", Color(0, 0, 0, 255), pScheme);
int xInset = 0, yInset = 0;
const char *resourceString = pScheme->GetResourceString("Button.TextInsetX");
if (resourceString[0])
xInset = atoi(resourceString);
resourceString = pScheme->GetResourceString("Button.TextInsetY");
if (resourceString[0])
yInset = atoi(resourceString);
SetTextInset(xInset, yInset);
const char *enableImage = pScheme->GetResourceString("Button/LeftC");
if (enableImage[0])
{
_imageBackground = true;
_depressedImage[0] = scheme()->GetImage(enableImage, true);
_depressedImage[1] = scheme()->GetImage(pScheme->GetResourceString("Button/CenterC"), true);
_depressedImage[2] = scheme()->GetImage(pScheme->GetResourceString("Button/RightC"), true);
_defaultImage[0] = scheme()->GetImage(pScheme->GetResourceString("Button/LeftN"), true);
_defaultImage[1] = scheme()->GetImage(pScheme->GetResourceString("Button/CenterN"), true);
_defaultImage[2] = scheme()->GetImage(pScheme->GetResourceString("Button/RightN"), true);
_armedImage[0] = scheme()->GetImage(pScheme->GetResourceString("Button/LeftO"), true);
_armedImage[1] = scheme()->GetImage(pScheme->GetResourceString("Button/CenterO"), true);
_armedImage[2] = scheme()->GetImage(pScheme->GetResourceString("Button/RightO"), true);
}
_blinkFgColor = GetSchemeColor("Button.BlinkColor", Color(255, 155, 0, 255), pScheme);
InvalidateLayout();
}
//-----------------------------------------------------------------------------
// Purpose: Set default button colors.
//-----------------------------------------------------------------------------
void Button::SetDefaultColor(Color fgColor, Color bgColor)
{
if (!(_defaultFgColor == fgColor && _defaultBgColor == bgColor))
{
_defaultFgColor = fgColor;
_defaultBgColor = bgColor;
InvalidateLayout(false);
}
}
//-----------------------------------------------------------------------------
// Purpose: Set armed button colors
//-----------------------------------------------------------------------------
void Button::SetArmedColor(Color fgColor, Color bgColor)
{
if (!(_armedFgColor == fgColor && _armedBgColor == bgColor))
{
_armedFgColor = fgColor;
_armedBgColor = bgColor;
InvalidateLayout(false);
}
}
//-----------------------------------------------------------------------------
// Purpose: Set depressed button colors
//-----------------------------------------------------------------------------
void Button::SetDepressedColor(Color fgColor, Color bgColor)
{
if (!(_depressedFgColor == fgColor && _depressedBgColor == bgColor))
{
_depressedFgColor = fgColor;
_depressedBgColor = bgColor;
InvalidateLayout(false);
}
}
//-----------------------------------------------------------------------------
// Purpose: Set blink button color
//-----------------------------------------------------------------------------
void Button::SetBlinkColor(Color fgColor)
{
if (!(_blinkFgColor == fgColor))
{
_blinkFgColor = fgColor;
InvalidateLayout(false);
}
}
//-----------------------------------------------------------------------------
// Purpose: Set default button border attributes.
//-----------------------------------------------------------------------------
void Button::SetDefaultBorder(IBorder *border)
{
_defaultBorder = border;
InvalidateLayout(false);
}
//-----------------------------------------------------------------------------
// Purpose: Set depressed button border attributes.
//-----------------------------------------------------------------------------
void Button::SetDepressedBorder(IBorder *border)
{
_depressedBorder = border;
InvalidateLayout(false);
}
//-----------------------------------------------------------------------------
// Purpose: Set key focus button border attributes.
//-----------------------------------------------------------------------------
void Button::SetKeyFocusBorder(IBorder *border)
{
_keyFocusBorder = border;
InvalidateLayout(false);
}
//-----------------------------------------------------------------------------
// Purpose: Get button border attributes.
//-----------------------------------------------------------------------------
IBorder *Button::GetBorder(bool depressed, bool armed, bool selected, bool keyfocus)
{
if ( _buttonFlags.IsFlagSet( BUTTON_BORDER_ENABLED ) )
{
// raised buttons with no armed state
if (depressed)
return _depressedBorder;
if (keyfocus)
return _keyFocusBorder;
if (IsEnabled() && _buttonFlags.IsFlagSet( DEFAULT_BUTTON ))
return _keyFocusBorder;
return _defaultBorder;
}
else
{
// flat buttons that raise
if (depressed)
return _depressedBorder;
if (armed)
return _defaultBorder;
}
return _defaultBorder;
}
//-----------------------------------------------------------------------------
// Purpose: sets this button to be the button that is accessed by default
// when the user hits ENTER or SPACE
//-----------------------------------------------------------------------------
void Button::SetAsCurrentDefaultButton(int state)
{
if ( _buttonFlags.IsFlagSet( DEFAULT_BUTTON ) != (bool)state )
{
_buttonFlags.SetFlag( DEFAULT_BUTTON, state );
if (state)
{
// post a message up notifying our nav group that we're now the default button
if (GetVParent())
{
KeyValues *msg = new KeyValues("CurrentDefaultButtonSet");
msg->SetPtr("button", this);
ivgui()->PostMessage(GetVParent(), msg, GetVPanel());
}
}
InvalidateLayout();
Repaint();
}
}
//-----------------------------------------------------------------------------
// Purpose: sets this button to be the button that is accessed by default
// when the user hits ENTER or SPACE
//-----------------------------------------------------------------------------
void Button::SetAsDefaultButton(int state)
{
if ( _buttonFlags.IsFlagSet( DEFAULT_BUTTON ) != (bool)state )
{
_buttonFlags.SetFlag( DEFAULT_BUTTON, state );
if (state)
{
// post a message up notifying our nav group that we're now the default button
if (GetVParent())
{
KeyValues *msg = new KeyValues("DefaultButtonSet");
msg->SetPtr("button", this);
ivgui()->PostMessage(GetVParent(), msg, GetVPanel());
}
}
InvalidateLayout();
Repaint();
}
}
//-----------------------------------------------------------------------------
// Purpose: sets rollover sound
//-----------------------------------------------------------------------------
void Button::SetArmedSound(const char *sound)
{
if (sound)
{
m_sArmedSoundName = g_ButtonSoundNames.AddString(sound);
}
else
{
m_sArmedSoundName = UTL_INVAL_SYMBOL;
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void Button::SetDepressedSound(const char *sound)
{
if (sound)
{
m_sDepressedSoundName = g_ButtonSoundNames.AddString(sound);
}
else
{
m_sDepressedSoundName = UTL_INVAL_SYMBOL;
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void Button::SetReleasedSound(const char *sound)
{
if (sound)
{
m_sReleasedSoundName = g_ButtonSoundNames.AddString(sound);
}
else
{
m_sReleasedSoundName = UTL_INVAL_SYMBOL;
}
}
//-----------------------------------------------------------------------------
// Purpose: Set button to be mouse clickable or not.
//-----------------------------------------------------------------------------
void Button::SetMouseClickEnabled(MouseCode code,bool state)
{
if(state)
{
//set bit to 1
_mouseClickMask|=1<<((int)(code+1));
}
else
{
//set bit to 0
_mouseClickMask&=~(1<<((int)(code+1)));
}
}
//-----------------------------------------------------------------------------
// Purpose: Check if button is mouse clickable
//-----------------------------------------------------------------------------
bool Button::IsMouseClickEnabled(MouseCode code)
{
if(_mouseClickMask&(1<<((int)(code+1))))
{
return true;
}
return false;
}
//-----------------------------------------------------------------------------
// Purpose: sets the command to send when the button is pressed
//-----------------------------------------------------------------------------
void Button::SetCommand( const char *command )
{
SetCommand(new KeyValues("Command", "command", command));
}
//-----------------------------------------------------------------------------
// Purpose: sets the message to send when the button is pressed
//-----------------------------------------------------------------------------
void Button::SetCommand( KeyValues *message )
{
// delete the old message
if (_actionMessage)
{
_actionMessage->deleteThis();
}
_actionMessage = message;
}
//-----------------------------------------------------------------------------
// Purpose: Peeks at the message to send when button is pressed
// Input : -
// Output : KeyValues
//-----------------------------------------------------------------------------
KeyValues *Button::GetCommand()
{
return _actionMessage;
}
//-----------------------------------------------------------------------------
// Purpose: Message targets that the button has been pressed
//-----------------------------------------------------------------------------
void Button::FireActionSignal()
{
// message-based action signal
if (_actionMessage)
{
// see if it's a url
if (!stricmp(_actionMessage->GetName(), "command")
&& !strnicmp(_actionMessage->GetString("command", ""), "url ", strlen("url "))
&& strstr(_actionMessage->GetString("command", ""), "://"))
{
// it's a command to launch a url, run it
system()->ShellExecute("open", _actionMessage->GetString("command", " ") + 4);
}
PostActionSignal(_actionMessage->MakeCopy());
}
}
//-----------------------------------------------------------------------------
// Purpose: gets info about the button
//-----------------------------------------------------------------------------
bool Button::RequestInfo(KeyValues *outputData)
{
if (!stricmp(outputData->GetName(), "CanBeDefaultButton"))
{
outputData->SetInt("result", CanBeDefaultButton() ? 1 : 0);
return true;
}
else if (!stricmp(outputData->GetName(), "GetState"))
{
outputData->SetInt("state", IsSelected());
return true;
}
else if ( !stricmp( outputData->GetName(), "GetCommand" ))
{
if ( _actionMessage )
{
outputData->SetString( "command", _actionMessage->GetString( "command", "" ) );
}
else
{
outputData->SetString( "command", "" );
}
return true;
}
return BaseClass::RequestInfo(outputData);
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
bool Button::CanBeDefaultButton(void)
{
return true;
}
//-----------------------------------------------------------------------------
// Purpose: Get control settings for editing
//-----------------------------------------------------------------------------
void Button::GetSettings( KeyValues *outResourceData )
{
BaseClass::GetSettings(outResourceData);
if (_actionMessage)
{
outResourceData->SetString("command", _actionMessage->GetString("command", ""));
}
outResourceData->SetInt("default", _buttonFlags.IsFlagSet( DEFAULT_BUTTON ) );
if ( m_bSelectionStateSaved )
{
outResourceData->SetInt( "selected", IsSelected() );
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void Button::ApplySettings( KeyValues *inResourceData )
{
BaseClass::ApplySettings(inResourceData);
const char *cmd = inResourceData->GetString("command", "");
if (*cmd)
{
// add in the command
SetCommand(cmd);
}
// set default button state
int defaultButton = inResourceData->GetInt("default");
if (defaultButton && CanBeDefaultButton())
{
SetAsDefaultButton(true);
}
// saved selection state
int iSelected = inResourceData->GetInt( "selected", -1 );
if ( iSelected != -1 )
{
SetSelected( iSelected != 0 );
m_bSelectionStateSaved = true;
}
}
//-----------------------------------------------------------------------------
// Purpose: Describes editing details
//-----------------------------------------------------------------------------
const char *Button::GetDescription( void )
{
static char buf[1024];
Q_snprintf(buf, sizeof(buf), "%s, string command, int default", BaseClass::GetDescription());
return buf;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void Button::OnSetState(int state)
{
SetSelected((bool)state);
Repaint();
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void Button::OnCursorEntered()
{
if (IsEnabled())
{
SetArmed(true);
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void Button::OnCursorExited()
{
if ( !_buttonFlags.IsFlagSet( BUTTON_KEY_DOWN ) )
{
SetArmed(false);
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void Button::OnMousePressed(MouseCode code)
{
if (!IsEnabled())
return;
if (!IsMouseClickEnabled(code))
return;
if (_activationType == ACTIVATE_ONPRESSED)
{
if ( IsKeyBoardInputEnabled() )
{
RequestFocus();
}
DoClick();
return;
}
// play activation sound
if (m_sDepressedSoundName != UTL_INVAL_SYMBOL)
{
surface()->PlaySound(g_ButtonSoundNames.String(m_sDepressedSoundName));
}
if (IsUseCaptureMouseEnabled() && _activationType == ACTIVATE_ONPRESSEDANDRELEASED)
{
{
if ( IsKeyBoardInputEnabled() )
{
RequestFocus();
}
SetSelected(true);
Repaint();
}
// lock mouse input to going to this button
input()->SetMouseCapture(GetVPanel());
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void Button::OnMouseDoublePressed(MouseCode code)
{
OnMousePressed(code);
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void Button::OnMouseReleased(MouseCode code)
{
// ensure mouse capture gets released
if (IsUseCaptureMouseEnabled())
{
input()->SetMouseCapture(NULL);
}
if (_activationType == ACTIVATE_ONPRESSED)
return;
if (!IsMouseClickEnabled(code))
return;
if (!IsSelected() && _activationType == ACTIVATE_ONPRESSEDANDRELEASED)
return;
// it has to be both enabled and (mouse over the button or using a key) to fire
if ( IsEnabled() && ( GetVPanel() == input()->GetMouseOver() || _buttonFlags.IsFlagSet( BUTTON_KEY_DOWN ) ) )
{
DoClick();
}
else
{
SetSelected(false);
}
// make sure the button gets unselected
Repaint();
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void Button::OnKeyCodePressed(KeyCode code)
{
if (code == KEY_SPACE || code == KEY_ENTER)
{
SetArmed(true);
_buttonFlags.SetFlag( BUTTON_KEY_DOWN );
OnMousePressed(MOUSE_LEFT);
if (IsUseCaptureMouseEnabled()) // undo the mouse capture since its a fake mouse click!
{
input()->SetMouseCapture(NULL);
}
}
else
{
_buttonFlags.ClearFlag( BUTTON_KEY_DOWN );
BaseClass::OnKeyCodePressed(code);
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void Button::OnKeyCodeReleased(KeyCode code)
{
if (_buttonFlags.IsFlagSet( BUTTON_KEY_DOWN ) && (code == KEY_SPACE || code == KEY_ENTER))
{
SetArmed(true);
OnMouseReleased(MOUSE_LEFT);
}
else
{
BaseClass::OnKeyCodeReleased(code);
}
_buttonFlags.ClearFlag( BUTTON_KEY_DOWN );
SetArmed(false);
}
//-----------------------------------------------------------------------------
// Purpose: Override this to draw different focus border
//-----------------------------------------------------------------------------
void Button::DrawFocusBorder(int tx0, int ty0, int tx1, int ty1)
{
surface()->DrawSetColor(_keyboardFocusColor);
DrawDashedLine(tx0, ty0, tx1, ty0+1, 1, 1); // top
DrawDashedLine(tx0, ty0, tx0+1, ty1, 1, 1); // left
DrawDashedLine(tx0, ty1-1, tx1, ty1, 1, 1); // bottom
DrawDashedLine(tx1-1, ty0, tx1, ty1, 1, 1); // right
}
//-----------------------------------------------------------------------------
// Purpose: Size the object to its button and text. - only works from in ApplySchemeSettings or PerformLayout()
//-----------------------------------------------------------------------------
void Button::SizeToContents()
{
int wide, tall;
GetContentSize(wide, tall);
SetSize(wide + Label::Content, tall + Label::Content);
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/yoyojacky/CSMoE.git
git@gitee.com:yoyojacky/CSMoE.git
yoyojacky
CSMoE
CSMoE
master

搜索帮助