This repository has been archived on 2023-07-11. You can view files and clone it, but cannot push or open issues or pull requests.
MyGameFramework/lib/gwen/controls/gwen_slider.cpp
Gered c5cdddbeaa initial commit
current versions of all of my basic framework sources, build configurations/scripts, and supporting assets
2013-01-31 12:53:05 -05:00

104 lines
1.8 KiB
C++

/*
GWEN
Copyright (c) 2010 Facepunch Studios
See license in Gwen.h
*/
#include <math.h>
#include "gwen_slider.h"
using namespace Gwen;
using namespace Gwen::Controls;
using namespace Gwen::ControlsInternal;
GWEN_CONTROL_CONSTRUCTOR( SliderBar )
{
SetTarget( this );
RestrictToParent( true );
}
void SliderBar::Render( Skin::Base* skin )
{
skin->DrawSlideButton( this, IsDepressed(), IsHorizontal() );
}
GWEN_CONTROL_CONSTRUCTOR( Slider )
{
SetBounds( Gwen::Rect( 0, 0, 32, 128) );
m_SliderBar = new SliderBar( this );
m_SliderBar->onDragged.Add( this, &Slider::OnMoved );
m_fMin = 0.0f;
m_fMax = 1.0f;
m_bClampToNotches = false;
m_iNumNotches = 5;
m_fValue = 0.0f;
SetTabable( true );
}
void Slider::OnMoved( Controls::Base * /*control*/ )
{
SetValueInternal( CalculateValue() );
}
void Slider::Layout( Skin::Base* skin )
{
BaseClass::Layout( skin );
}
float Slider::CalculateValue()
{
return 0;
}
void Slider::SetFloatValue( float val, bool /*forceUpdate*/ )
{
if (val < m_fMin) val = m_fMin;
if (val > m_fMax) val = m_fMax;
// Normalize Value
val = (val - m_fMin) / (m_fMax - m_fMin);
SetValueInternal( val );
Redraw();
}
void Slider::SetValueInternal( float val )
{
if ( m_bClampToNotches )
{
val = floor( (val * (float)m_iNumNotches) + 0.5f );
val /= (float) m_iNumNotches;
}
if ( m_fValue != val )
{
m_fValue = val;
onValueChanged.Call( this );
}
UpdateBarFromValue();
}
float Slider::GetFloatValue()
{
return m_fMin + (m_fValue * (m_fMax - m_fMin));
}
void Slider::SetRange( float fMin, float fMax )
{
m_fMin = fMin;
m_fMax = fMax;
}
void Slider::RenderFocus( Gwen::Skin::Base* skin )
{
if ( Gwen::KeyboardFocus != this ) return;
if ( !IsTabable() ) return;
skin->DrawKeyboardHighlight( this, GetRenderBounds(), 0 );
}