2
0
mirror of https://github.com/FWGS/hlsdk-xash3d synced 2024-11-22 01:47:45 +01:00
hlsdk-xash3d/game_shared/vgui_slider2.cpp

440 lines
8.0 KiB
C++
Raw Normal View History

//========= Copyright (c) 1996-2002, Valve LLC, All rights reserved. ============
2016-06-04 15:24:23 +02:00
//
// Purpose: New version of the slider bar
//
// $NoKeywords: $
//=============================================================================
#include "vgui_slider2.h"
2016-06-04 15:24:23 +02:00
#include <VGUI_InputSignal.h>
#include <VGUI_App.h>
#include <VGUI_IntChangeSignal.h>
#include <VGUI_MouseCode.h>
2016-06-04 15:24:23 +02:00
using namespace vgui;
namespace
{
class FooDefaultSliderSignal : public InputSignal
{
Slider2 *_slider;
2016-06-04 15:24:23 +02:00
public:
FooDefaultSliderSignal( Slider2 *slider )
2016-06-04 15:24:23 +02:00
{
_slider = slider;
2016-06-04 15:24:23 +02:00
}
void cursorMoved( int x, int y, Panel *panel )
2016-06-04 15:24:23 +02:00
{
_slider->privateCursorMoved( x, y, panel );
2016-06-04 15:24:23 +02:00
}
void cursorEntered( Panel *panel ) {}
void cursorExited( Panel *panel ) {}
void mouseDoublePressed( MouseCode code, Panel *panel ) {}
void mousePressed( MouseCode code, Panel *panel )
2016-06-04 15:24:23 +02:00
{
_slider->privateMousePressed( code, panel );
2016-06-04 15:24:23 +02:00
}
void mouseReleased( MouseCode code, Panel *panel )
2016-06-04 15:24:23 +02:00
{
_slider->privateMouseReleased( code, panel );
2016-06-04 15:24:23 +02:00
}
void mouseWheeled( int delta, Panel *panel ) {}
void keyPressed( KeyCode code, Panel *panel ) {}
void keyTyped( KeyCode code, Panel *panel ) {}
void keyReleased( KeyCode code, Panel *panel ) {}
void keyFocusTicked( Panel *panel ) {}
2016-06-04 15:24:23 +02:00
};
}
Slider2::Slider2( int x, int y, int wide, int tall, bool vertical ) : Panel( x, y, wide, tall )
2016-06-04 15:24:23 +02:00
{
_vertical = vertical;
_dragging = false;
_value = 0;
_range[0] = 0;
_range[1] = 299;
_rangeWindow = 0;
_rangeWindowEnabled = false;
_buttonOffset = 0;
2016-06-04 15:24:23 +02:00
recomputeNobPosFromValue();
addInputSignal( new FooDefaultSliderSignal( this ) );
2016-06-04 15:24:23 +02:00
}
void Slider2::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
recomputeNobPosFromValue();
}
bool Slider2::isVertical()
{
return _vertical;
}
void Slider2::setValue( int value )
2016-06-04 15:24:23 +02:00
{
int oldValue = _value;
2016-06-04 15:24:23 +02:00
if( value < _range[0] )
2016-06-04 15:24:23 +02:00
{
value = _range[0];
2016-06-04 15:24:23 +02:00
}
if( value > _range[1] )
2016-06-04 15:24:23 +02:00
{
value=_range[1];
}
_value = value;
2016-06-04 15:24:23 +02:00
recomputeNobPosFromValue();
if( _value != oldValue )
2016-06-04 15:24:23 +02:00
{
fireIntChangeSignal();
}
}
int Slider2::getValue()
{
return _value;
}
void Slider2::recomputeNobPosFromValue()
{
int wide, tall;
2016-06-04 15:24:23 +02:00
getPaintSize( wide, tall );
2016-06-04 15:24:23 +02:00
float fwide = (float)wide;
float ftall = (float)tall;
float frange = (float)(_range[1] - _range[0]);
float fvalue = (float)(_value - _range[0]);
float fper = fvalue / frange;
float frangewindow = (float)_rangeWindow;
2016-06-04 15:24:23 +02:00
if( frangewindow < 0 )
2016-06-04 15:24:23 +02:00
{
frangewindow = 0;
2016-06-04 15:24:23 +02:00
}
if( !_rangeWindowEnabled )
2016-06-04 15:24:23 +02:00
{
frangewindow = frange;
2016-06-04 15:24:23 +02:00
}
if( frangewindow > 0 )
2016-06-04 15:24:23 +02:00
{
if( _vertical )
2016-06-04 15:24:23 +02:00
{
float fnobsize=frangewindow;
float freepixels = ftall - fnobsize;
float firstpixel = freepixels * fper;
_nobPos[0] = (int)( firstpixel );
_nobPos[1] = (int)( firstpixel + fnobsize );
2016-06-04 15:24:23 +02:00
if( _nobPos[1] > tall )
2016-06-04 15:24:23 +02:00
{
_nobPos[0] = tall - ((int)fnobsize);
_nobPos[1] = tall;
2016-06-04 15:24:23 +02:00
}
}
else
{
float fnobsize = frangewindow;
2016-06-04 15:24:23 +02:00
float freepixels = fwide - fnobsize;
float firstpixel = freepixels * fper;
_nobPos[0]=(int)( firstpixel );
_nobPos[1]=(int)( firstpixel + fnobsize );
if( _nobPos[1] > wide )
2016-06-04 15:24:23 +02:00
{
_nobPos[0] = wide - ((int)fnobsize);
_nobPos[1] = wide;
2016-06-04 15:24:23 +02:00
}
}
}
repaint();
}
void Slider2::recomputeValueFromNobPos()
{
int wide,tall;
getPaintSize( wide, tall );
float fwide = (float)wide;
float ftall = (float)tall;
float frange = (float)(_range[1] - _range[0]);
float fvalue = (float)(_value - _range[0]);
float fnob = (float)_nobPos[0];
float frangewindow = (float)(_rangeWindow);
2016-06-04 15:24:23 +02:00
if( frangewindow < 0 )
2016-06-04 15:24:23 +02:00
{
frangewindow = 0;
2016-06-04 15:24:23 +02:00
}
if( !_rangeWindowEnabled )
2016-06-04 15:24:23 +02:00
{
frangewindow = frange;
2016-06-04 15:24:23 +02:00
}
if( frangewindow > 0 )
2016-06-04 15:24:23 +02:00
{
if( _vertical )
2016-06-04 15:24:23 +02:00
{
float fnobsize = frangewindow;
fvalue = frange * ( fnob / ( ftall - fnobsize ) );
2016-06-04 15:24:23 +02:00
}
else
{
float fnobsize = frangewindow;
fvalue = frange * ( fnob / ( fwide - fnobsize ) );
2016-06-04 15:24:23 +02:00
}
}
// Take care of rounding issues.
_value = (int)( fvalue+_range[0] + 0.5 );
2016-06-04 15:24:23 +02:00
// Clamp final result
_value = ( _value < _range[1] ) ? _value : _range[1];
}
bool Slider2::hasFullRange()
{
int wide, tall;
getPaintSize( wide, tall );
2016-06-04 15:24:23 +02:00
float fwide = (float)wide;
float ftall = (float)tall;
float frange = (float)( _range[1] - _range[0] );
float frangewindow = (float)( _rangeWindow );
2016-06-04 15:24:23 +02:00
if( frangewindow < 0 )
2016-06-04 15:24:23 +02:00
{
frangewindow = 0;
2016-06-04 15:24:23 +02:00
}
if( !_rangeWindowEnabled )
2016-06-04 15:24:23 +02:00
{
frangewindow = frange;
2016-06-04 15:24:23 +02:00
}
if( frangewindow > 0 )
2016-06-04 15:24:23 +02:00
{
if( _vertical )
2016-06-04 15:24:23 +02:00
{
if( frangewindow <= ( ftall + _buttonOffset ) )
{
return true;
}
}
else
{
if( frangewindow <= ( fwide + _buttonOffset ) )
{
return true;
}
}
}
return false;
}
void Slider2::addIntChangeSignal( IntChangeSignal *s )
2016-06-04 15:24:23 +02:00
{
_intChangeSignalDar.putElement(s);
}
void Slider2::fireIntChangeSignal()
{
for( int i = 0; i < _intChangeSignalDar.getCount(); i++ )
2016-06-04 15:24:23 +02:00
{
_intChangeSignalDar[i]->intChanged( getValue(), this );
2016-06-04 15:24:23 +02:00
}
}
void Slider2::paintBackground()
{
int wide, tall;
getPaintSize( wide, tall );
2016-06-04 15:24:23 +02:00
if( _vertical )
2016-06-04 15:24:23 +02:00
{
// background behind slider
drawSetColor( 40, 40, 40, 0 );
drawFilledRect( 0, 0, wide, tall );
2016-06-04 15:24:23 +02:00
// slider front
drawSetColor( 0, 0, 0, 0 );
drawFilledRect( 0, _nobPos[0], wide, _nobPos[1] );
2016-06-04 15:24:23 +02:00
// slider border
drawSetColor( 60, 60, 60, 0 );
drawFilledRect( 0, _nobPos[0], wide, _nobPos[0] + 1 ); // top
drawFilledRect( 0, _nobPos[1], wide, _nobPos[1] + 1 ); // bottom
drawFilledRect( 0, _nobPos[0] + 1, 1, _nobPos[1] ); // left
drawFilledRect( wide - 1, _nobPos[0] + 1, wide, _nobPos[1] ); // right
2016-06-04 15:24:23 +02:00
}
else
{
//!! doesn't work
drawSetColor( Scheme::sc_secondary3 );
drawFilledRect( 0, 0, wide, tall );
2016-06-04 15:24:23 +02:00
drawSetColor( Scheme::sc_black );
drawOutlinedRect( 0, 0, wide, tall );
2016-06-04 15:24:23 +02:00
drawSetColor( Scheme::sc_primary2 );
drawFilledRect( _nobPos[0], 0, _nobPos[1], tall );
2016-06-04 15:24:23 +02:00
drawSetColor( Scheme::sc_black );
drawOutlinedRect( _nobPos[0], 0, _nobPos[1], tall );
2016-06-04 15:24:23 +02:00
}
}
void Slider2::setRange( int min, int max )
2016-06-04 15:24:23 +02:00
{
if( max < min )
2016-06-04 15:24:23 +02:00
{
max =min;
2016-06-04 15:24:23 +02:00
}
if( min > max )
2016-06-04 15:24:23 +02:00
{
min = max;
2016-06-04 15:24:23 +02:00
}
_range[0] = min;
_range[1] = max;
2016-06-04 15:24:23 +02:00
}
void Slider2::getRange( int &min, int &max )
2016-06-04 15:24:23 +02:00
{
min = _range[0];
max = _range[1];
2016-06-04 15:24:23 +02:00
}
void Slider2::privateCursorMoved( int x, int y, Panel *panel )
2016-06-04 15:24:23 +02:00
{
if( !_dragging )
2016-06-04 15:24:23 +02:00
{
return;
}
getApp()->getCursorPos( x, y );
screenToLocal( x, y );
2016-06-04 15:24:23 +02:00
int wide, tall;
getPaintSize( wide, tall);
2016-06-04 15:24:23 +02:00
if( _vertical )
2016-06-04 15:24:23 +02:00
{
_nobPos[0] = _nobDragStartPos[0] + ( y - _dragStartPos[1] );
_nobPos[1] = _nobDragStartPos[1] + ( y - _dragStartPos[1] );
2016-06-04 15:24:23 +02:00
if( _nobPos[1] > tall )
2016-06-04 15:24:23 +02:00
{
_nobPos[0] = tall - ( _nobPos[1] - _nobPos[0] );
_nobPos[1] = tall;
2016-06-04 15:24:23 +02:00
}
if( _nobPos[0] < 0 )
2016-06-04 15:24:23 +02:00
{
_nobPos[1] = _nobPos[1] - _nobPos[0];
_nobPos[0] = 0;
2016-06-04 15:24:23 +02:00
}
}
else
{
_nobPos[0] = _nobDragStartPos[0] + ( x - _dragStartPos[0] );
_nobPos[1] = _nobDragStartPos[1] + ( x - _dragStartPos[0] );
2016-06-04 15:24:23 +02:00
if( _nobPos[1] > wide )
2016-06-04 15:24:23 +02:00
{
_nobPos[0] = wide - ( _nobPos[1] - _nobPos[0] );
_nobPos[1] = wide;
2016-06-04 15:24:23 +02:00
}
if( _nobPos[0] < 0 )
2016-06-04 15:24:23 +02:00
{
_nobPos[1] = _nobPos[1] - _nobPos[0];
_nobPos[0] = 0;
2016-06-04 15:24:23 +02:00
}
}
recomputeValueFromNobPos();
repaint();
fireIntChangeSignal();
}
void Slider2::privateMousePressed( MouseCode code, Panel *panel )
2016-06-04 15:24:23 +02:00
{
int x,y;
getApp()->getCursorPos( x, y );
screenToLocal( x, y );
2016-06-04 15:24:23 +02:00
if( _vertical )
2016-06-04 15:24:23 +02:00
{
if( ( y >= _nobPos[0] ) && ( y < _nobPos[1] ) )
2016-06-04 15:24:23 +02:00
{
_dragging = true;
getApp()->setMouseCapture( this );
_nobDragStartPos[0] = _nobPos[0];
_nobDragStartPos[1] = _nobPos[1];
_dragStartPos[0] = x;
_dragStartPos[1] = y;
2016-06-04 15:24:23 +02:00
}
}
else
{
if( ( x >= _nobPos[0] ) && ( x < _nobPos[1] ) )
2016-06-04 15:24:23 +02:00
{
_dragging = true;
getApp()->setMouseCapture( this );
_nobDragStartPos[0] = _nobPos[0];
_nobDragStartPos[1] = _nobPos[1];
_dragStartPos[0] = x;
_dragStartPos[1] = y;
2016-06-04 15:24:23 +02:00
}
}
}
void Slider2::privateMouseReleased( MouseCode code, Panel *panel )
2016-06-04 15:24:23 +02:00
{
_dragging = false;
getApp()->setMouseCapture( null );
2016-06-04 15:24:23 +02:00
}
void Slider2::getNobPos( int &min, int &max )
2016-06-04 15:24:23 +02:00
{
min = _nobPos[0];
max = _nobPos[1];
2016-06-04 15:24:23 +02:00
}
void Slider2::setRangeWindow( int rangeWindow )
2016-06-04 15:24:23 +02:00
{
_rangeWindow=rangeWindow;
}
void Slider2::setRangeWindowEnabled( bool state )
2016-06-04 15:24:23 +02:00
{
_rangeWindowEnabled=state;
}
void Slider2::setButtonOffset( int buttonOffset )
2016-06-04 15:24:23 +02:00
{
_buttonOffset=buttonOffset;
}