Portable Half-Life SDK. GoldSource and Xash3D. Crossplatform.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

308 lines
6.1 KiB

//========= Copyright (c) 1996-2002, Valve LLC, All rights reserved. ============
9 years ago
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================
#include "vgui_scrollbar2.h"
#include "vgui_slider2.h"
9 years ago
#include "vgui_loadtga.h"
#include <VGUI_IntChangeSignal.h>
#include <VGUI_Button.h>
#include <VGUI_ActionSignal.h>
#include <VGUI_LineBorder.h>
9 years ago
using namespace vgui;
namespace
{
class FooDefaultScrollBarIntChangeSignal : public IntChangeSignal
{
public:
FooDefaultScrollBarIntChangeSignal( ScrollBar2 *scrollBar )
9 years ago
{
_scrollBar = scrollBar;
9 years ago
}
virtual void intChanged( int value, Panel *panel )
9 years ago
{
_scrollBar->fireIntChangeSignal();
}
protected:
ScrollBar2 *_scrollBar;
9 years ago
};
class FooDefaultButtonSignal : public ActionSignal
{
public:
ScrollBar2 *_scrollBar;
int _buttonIndex;
FooDefaultButtonSignal( ScrollBar2 *scrollBar, int buttonIndex )
9 years ago
{
_scrollBar = scrollBar;
_buttonIndex = buttonIndex;
9 years ago
}
virtual void actionPerformed( Panel *panel )
9 years ago
{
_scrollBar->doButtonPressed( _buttonIndex );
9 years ago
}
};
}
//-----------------------------------------------------------------------------
// 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 )
9 years ago
{
Image *image = vgui_LoadTGA( filename );
if( image )
9 years ago
{
image->setColor( Color( 140, 140, 140, 0 ) );
setImage( image );
9 years ago
}
setBorder( &m_Border );
9 years ago
}
virtual void paintBackground()
{
int wide, tall;
getPaintSize( wide, tall );
9 years ago
// fill the background
drawSetColor( 0, 0, 0, 0 );
drawFilledRect( 0, 0, wide, tall );
9 years ago
}
};
//-----------------------------------------------------------------------------
// 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 )
9 years ago
{
_slider = null;
_button[0] = null;
_button[1] = null;
9 years ago
if( vertical )
9 years ago
{
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 );
9 years ago
}
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 );
9 years ago
}
setPaintBorderEnabled( true );
setPaintBackgroundEnabled( true );
setPaintEnabled( true );
setButtonPressedScrollValue( 15 );
9 years ago
validate();
}
9 years ago
void ScrollBar2::setSize( int wide, int tall )
9 years ago
{
Panel::setSize( wide, tall );
9 years ago
if( _slider == null )
9 years ago
{
return;
}
if( _button[0] == null )
9 years ago
{
return;
}
if( _button[1] == null )
9 years ago
{
return;
}
getPaintSize( wide, tall );
9 years ago
if( _slider->isVertical() )
9 years ago
{
_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 );
9 years ago
}
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);
9 years ago
}
}
void ScrollBar2::performLayout()
{
}
void ScrollBar2::setValue( int value )
9 years ago
{
_slider->setValue( value );
9 years ago
}
int ScrollBar2::getValue()
{
return _slider->getValue();
}
void ScrollBar2::addIntChangeSignal( IntChangeSignal *s )
9 years ago
{
_intChangeSignalDar.putElement( s );
_slider->addIntChangeSignal( new FooDefaultScrollBarIntChangeSignal( this ) );
9 years ago
}
void ScrollBar2::setRange( int min, int max )
9 years ago
{
_slider->setRange( min, max );
9 years ago
}
void ScrollBar2::fireIntChangeSignal()
{
for( int i = 0; i < _intChangeSignalDar.getCount(); i++ )
9 years ago
{
_intChangeSignalDar[i]->intChanged( _slider->getValue(), this );
9 years ago
}
}
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 )
9 years ago
{
if( _button[index] != null )
9 years ago
{
removeChild( _button[index] );
9 years ago
}
_button[index] = button;
addChild( _button[index] );
_button[index]->addActionSignal( new FooDefaultButtonSignal( this, index ) );
9 years ago
validate();
// _button[index]->setVisible( false );
9 years ago
}
Button *ScrollBar2::getButton( int index )
9 years ago
{
return _button[index];
}
// LEAK: new and old slider will leak
void ScrollBar2::setSlider( Slider2 *slider )
9 years ago
{
if( _slider != null )
9 years ago
{
removeChild( _slider );
9 years ago
}
_slider = slider;
addChild( _slider );
_slider->addIntChangeSignal( new FooDefaultScrollBarIntChangeSignal( this ) );
9 years ago
validate();
}
Slider2 *ScrollBar2::getSlider()
{
return _slider;
}
void ScrollBar2::doButtonPressed( int buttonIndex )
9 years ago
{
if( buttonIndex == 0 )
9 years ago
{
_slider->setValue( _slider->getValue() - _buttonPressedScrollValue );
9 years ago
}
else
{
_slider->setValue( _slider->getValue() + _buttonPressedScrollValue );
9 years ago
}
}
void ScrollBar2::setButtonPressedScrollValue( int value )
9 years ago
{
_buttonPressedScrollValue = value;
9 years ago
}
void ScrollBar2::setRangeWindow( int rangeWindow )
9 years ago
{
_slider->setRangeWindow( rangeWindow );
9 years ago
}
void ScrollBar2::setRangeWindowEnabled( bool state )
9 years ago
{
_slider->setRangeWindowEnabled( state );
9 years ago
}
void ScrollBar2::validate()
{
if( _slider != null )
9 years ago
{
int buttonOffset = 0;
9 years ago
for( int i = 0; i < 2; i++ )
9 years ago
{
if( _button[i] != null )
9 years ago
{
if( _button[i]->isVisible() )
9 years ago
{
if( _slider->isVertical() )
{
buttonOffset += _button[i]->getTall();
9 years ago
}
else
{
buttonOffset += _button[i]->getWide();
9 years ago
}
}
}
}
_slider->setButtonOffset( buttonOffset );
9 years ago
}
int wide, tall;
getSize( wide, tall );
setSize( wide, tall );
9 years ago
}