2
0
mirror of https://github.com/FWGS/hlsdk-xash3d synced 2024-11-21 17:36:18 +01:00
hlsdk-xash3d/game_shared/vgui_scrollbar2.cpp

308 lines
6.1 KiB
C++
Raw Permalink Normal View History

//========= Copyright (c) 1996-2002, Valve LLC, All rights reserved. ============
2016-06-04 15:24:23 +02:00
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================
#include "vgui_scrollbar2.h"
#include "vgui_slider2.h"
2016-06-04 15:24:23 +02:00
#include "vgui_loadtga.h"
#include <VGUI_IntChangeSignal.h>
#include <VGUI_Button.h>
#include <VGUI_ActionSignal.h>
#include <VGUI_LineBorder.h>
2016-06-04 15:24:23 +02:00
using namespace vgui;
namespace
{
class FooDefaultScrollBarIntChangeSignal : public IntChangeSignal
{
public:
FooDefaultScrollBarIntChangeSignal( ScrollBar2 *scrollBar )
2016-06-04 15:24:23 +02:00
{
_scrollBar = scrollBar;
2016-06-04 15:24:23 +02:00
}
virtual void intChanged( int value, Panel *panel )
2016-06-04 15:24:23 +02:00
{
_scrollBar->fireIntChangeSignal();
}
protected:
ScrollBar2 *_scrollBar;
2016-06-04 15:24:23 +02:00
};
class FooDefaultButtonSignal : public ActionSignal
{
public:
ScrollBar2 *_scrollBar;
int _buttonIndex;
FooDefaultButtonSignal( ScrollBar2 *scrollBar, int buttonIndex )
2016-06-04 15:24:23 +02:00
{
_scrollBar = scrollBar;
_buttonIndex = buttonIndex;
2016-06-04 15:24:23 +02:00
}
virtual void actionPerformed( Panel *panel )
2016-06-04 15:24:23 +02:00
{
_scrollBar->doButtonPressed( _buttonIndex );
2016-06-04 15:24:23 +02:00
}
};
}
//-----------------------------------------------------------------------------
// Purpose: Default scrollbar button
// Draws in new scoreboard style
//-----------------------------------------------------------------------------
class ScrollBarButton : public Button
{
private:
LineBorder m_Border;
public:
ScrollBarButton( const char *filename, int x, int y, int wide, int tall ) : m_Border( Color( 60, 60, 60, 0 ) ), Button( "", x, y, wide, tall )
2016-06-04 15:24:23 +02:00
{
Image *image = vgui_LoadTGA( filename );
if( image )
2016-06-04 15:24:23 +02:00
{
image->setColor( Color( 140, 140, 140, 0 ) );
setImage( image );
2016-06-04 15:24:23 +02:00
}
setBorder( &m_Border );
2016-06-04 15:24:23 +02:00
}
virtual void paintBackground()
{
int wide, tall;
getPaintSize( wide, tall );
2016-06-04 15:24:23 +02:00
// fill the background
drawSetColor( 0, 0, 0, 0 );
drawFilledRect( 0, 0, wide, tall );
2016-06-04 15:24:23 +02:00
}
};
//-----------------------------------------------------------------------------
// Purpose: Constructor
// Input : x -
// y -
// wide -
// tall -
// vertical -
//-----------------------------------------------------------------------------
ScrollBar2::ScrollBar2( int x, int y, int wide, int tall, bool vertical ) : Panel( x, y, wide, tall )
2016-06-04 15:24:23 +02:00
{
_slider = null;
_button[0] = null;
_button[1] = null;
2016-06-04 15:24:23 +02:00
if( vertical )
2016-06-04 15:24:23 +02:00
{
setSlider( new Slider2( 0, wide - 1, wide, ( tall - ( wide * 2 ) ) + 2, true ) );
setButton( new ScrollBarButton( "gfx/vgui/arrowup.tga", 0, 0, wide, wide ), 0 );
setButton( new ScrollBarButton( "gfx/vgui/arrowdown.tga", 0, tall - wide, wide, wide ), 1 );
2016-06-04 15:24:23 +02:00
}
else
{
// untested code
setSlider( new Slider2( tall, 0, wide - ( tall * 2 ), tall, false ) );
setButton( new ScrollBarButton( "gfx/vgui/320_arrowlt.tga", 0, 0, tall + 1, tall + 1 ), 0 );
setButton( new ScrollBarButton( "gfx/vgui/320_arrowrt.tga", wide - tall, 0, tall + 1, tall + 1 ) , 1 );
2016-06-04 15:24:23 +02:00
}
setPaintBorderEnabled( true );
setPaintBackgroundEnabled( true );
setPaintEnabled( true );
setButtonPressedScrollValue( 15 );
2016-06-04 15:24:23 +02:00
validate();
}
2016-06-04 15:24:23 +02:00
void ScrollBar2::setSize( int wide, int tall )
2016-06-04 15:24:23 +02:00
{
Panel::setSize( wide, tall );
2016-06-04 15:24:23 +02:00
if( _slider == null )
2016-06-04 15:24:23 +02:00
{
return;
}
if( _button[0] == null )
2016-06-04 15:24:23 +02:00
{
return;
}
if( _button[1] == null )
2016-06-04 15:24:23 +02:00
{
return;
}
getPaintSize( wide, tall );
2016-06-04 15:24:23 +02:00
if( _slider->isVertical() )
2016-06-04 15:24:23 +02:00
{
_slider->setBounds( 0, wide, wide, tall - ( wide * 2 ) );
// _slider->setBounds( 0, 0, wide, tall );
_button[0]->setBounds( 0, 0, wide, wide );
_button[1]->setBounds( 0, tall - wide, wide, wide );
2016-06-04 15:24:23 +02:00
}
else
{
_slider->setBounds( tall, 0, wide - ( tall * 2 ), tall );
// _slider->setBounds( 0, 0, wide, tall );
_button[0]->setBounds( 0, 0, tall, tall );
_button[1]->setBounds( ( wide - tall ), 0, tall, tall);
2016-06-04 15:24:23 +02:00
}
}
void ScrollBar2::performLayout()
{
}
void ScrollBar2::setValue( int value )
2016-06-04 15:24:23 +02:00
{
_slider->setValue( value );
2016-06-04 15:24:23 +02:00
}
int ScrollBar2::getValue()
{
return _slider->getValue();
}
void ScrollBar2::addIntChangeSignal( IntChangeSignal *s )
2016-06-04 15:24:23 +02:00
{
_intChangeSignalDar.putElement( s );
_slider->addIntChangeSignal( new FooDefaultScrollBarIntChangeSignal( this ) );
2016-06-04 15:24:23 +02:00
}
void ScrollBar2::setRange( int min, int max )
2016-06-04 15:24:23 +02:00
{
_slider->setRange( min, max );
2016-06-04 15:24:23 +02:00
}
void ScrollBar2::fireIntChangeSignal()
{
for( int i = 0; i < _intChangeSignalDar.getCount(); i++ )
2016-06-04 15:24:23 +02:00
{
_intChangeSignalDar[i]->intChanged( _slider->getValue(), this );
2016-06-04 15:24:23 +02:00
}
}
bool ScrollBar2::isVertical()
{
return _slider->isVertical();
}
bool ScrollBar2::hasFullRange()
{
return _slider->hasFullRange();
}
// LEAK: new and old slider will leak
void ScrollBar2::setButton( Button *button, int index )
2016-06-04 15:24:23 +02:00
{
if( _button[index] != null )
2016-06-04 15:24:23 +02:00
{
removeChild( _button[index] );
2016-06-04 15:24:23 +02:00
}
_button[index] = button;
addChild( _button[index] );
_button[index]->addActionSignal( new FooDefaultButtonSignal( this, index ) );
2016-06-04 15:24:23 +02:00
validate();
// _button[index]->setVisible( false );
2016-06-04 15:24:23 +02:00
}
Button *ScrollBar2::getButton( int index )
2016-06-04 15:24:23 +02:00
{
return _button[index];
}
// LEAK: new and old slider will leak
void ScrollBar2::setSlider( Slider2 *slider )
2016-06-04 15:24:23 +02:00
{
if( _slider != null )
2016-06-04 15:24:23 +02:00
{
removeChild( _slider );
2016-06-04 15:24:23 +02:00
}
_slider = slider;
addChild( _slider );
_slider->addIntChangeSignal( new FooDefaultScrollBarIntChangeSignal( this ) );
2016-06-04 15:24:23 +02:00
validate();
}
Slider2 *ScrollBar2::getSlider()
{
return _slider;
}
void ScrollBar2::doButtonPressed( int buttonIndex )
2016-06-04 15:24:23 +02:00
{
if( buttonIndex == 0 )
2016-06-04 15:24:23 +02:00
{
_slider->setValue( _slider->getValue() - _buttonPressedScrollValue );
2016-06-04 15:24:23 +02:00
}
else
{
_slider->setValue( _slider->getValue() + _buttonPressedScrollValue );
2016-06-04 15:24:23 +02:00
}
}
void ScrollBar2::setButtonPressedScrollValue( int value )
2016-06-04 15:24:23 +02:00
{
_buttonPressedScrollValue = value;
2016-06-04 15:24:23 +02:00
}
void ScrollBar2::setRangeWindow( int rangeWindow )
2016-06-04 15:24:23 +02:00
{
_slider->setRangeWindow( rangeWindow );
2016-06-04 15:24:23 +02:00
}
void ScrollBar2::setRangeWindowEnabled( bool state )
2016-06-04 15:24:23 +02:00
{
_slider->setRangeWindowEnabled( state );
2016-06-04 15:24:23 +02:00
}
void ScrollBar2::validate()
{
if( _slider != null )
2016-06-04 15:24:23 +02:00
{
int buttonOffset = 0;
2016-06-04 15:24:23 +02:00
for( int i = 0; i < 2; i++ )
2016-06-04 15:24:23 +02:00
{
if( _button[i] != null )
2016-06-04 15:24:23 +02:00
{
if( _button[i]->isVisible() )
2016-06-04 15:24:23 +02:00
{
if( _slider->isVertical() )
{
buttonOffset += _button[i]->getTall();
2016-06-04 15:24:23 +02:00
}
else
{
buttonOffset += _button[i]->getWide();
2016-06-04 15:24:23 +02:00
}
}
}
}
_slider->setButtonOffset( buttonOffset );
2016-06-04 15:24:23 +02:00
}
int wide, tall;
getSize( wide, tall );
setSize( wide, tall );
2016-06-04 15:24:23 +02:00
}