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_label.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

115 lines
2.1 KiB
C++

/*
GWEN
Copyright (c) 2010 Facepunch Studios
See license in Gwen.h
*/
#include "../gwen.h"
#include "gwen_label.h"
#include "../gwen_utility.h"
using namespace Gwen;
using namespace Gwen::Controls;
GWEN_CONTROL_CONSTRUCTOR( Label )
{
m_CreatedFont = NULL;
m_Text = new ControlsInternal::Text( this );
m_Text->SetFont( GetSkin()->GetDefaultFont() );
SetMouseInputEnabled( false );
SetBounds( 0, 0, 100, 10 );
SetAlignment( Gwen::Pos::Left | Gwen::Pos::Top );
}
void Label::PreDelete( Gwen::Skin::Base* skin )
{
if ( m_CreatedFont )
{
skin->ReleaseFont( m_CreatedFont );
delete m_CreatedFont;
m_CreatedFont = NULL;
SetFont( NULL );
}
}
void Label::PostLayout( Skin::Base* /*skin*/ )
{
m_Text->Position( m_iAlign );
}
void Label::SetAlignment( int iAlign )
{
if ( m_iAlign == iAlign ) return;
m_iAlign = iAlign;
Invalidate();
}
int Label::GetAlignment()
{
return m_iAlign;
}
void Label::SetText( const TextObject& str, bool bDoEvents )
{
if ( m_Text->GetText() == str.Get() ) return;
m_Text->SetString( str );
Redraw();
if ( bDoEvents )
OnTextChanged();
}
void Label::SizeToContents()
{
m_Text->SetPos( m_Padding.left, m_Padding.top );
m_Text->RefreshSize();
SetSize( m_Text->Width() + m_Padding.left + m_Padding.right, m_Text->Height() + m_Padding.top + m_Padding.bottom );
}
Gwen::Rect Label::GetCharacterPosition( int iChar )
{
Gwen::Rect p = m_Text->GetCharacterPosition( iChar );
p.x += m_Text->X();
p.y += m_Text->Y();
return p;
}
void Label::OnBoundsChanged( Gwen::Rect oldChildBounds )
{
BaseClass::OnBoundsChanged( oldChildBounds );
if ( m_Text->Wrap() )
{
m_Text->RefreshSize();
Invalidate();
}
}
void Label::SetFont( Gwen::String strFacename, int iSize, bool bBold )
{
if ( m_CreatedFont )
{
GetSkin()->ReleaseFont( m_CreatedFont );
delete m_CreatedFont;
m_CreatedFont = NULL;
SetFont( NULL );
}
m_CreatedFont = new Gwen::Font();
Debug::AssertCheck( m_CreatedFont != NULL, "Couldn't Create Font!" );
m_CreatedFont->bold = bBold;
m_CreatedFont->facename = strFacename;
m_CreatedFont->size = iSize;
SetFont( m_CreatedFont );
m_Text->RefreshSize();
}