Show textarea.cpp syntax highlighted
/***************************************************************************
textarea.cpp - Area object with text
-------------------
begin : Tue Jan 7 2003
copyright : (C) 2003 by Mattias Jansson
email : mattias@realityrift.com
***************************************************************************
The contents of this file are subject to the Mozilla Public License Version
1.1 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
for the specific language governing rights and limitations under the
License.
The Original Code is the NeoEngine, NeoWTK, textarea.cpp
The Initial Developer of the Original Code is Mattias Jansson.
Portions created by Mattias Jansson are Copyright (C) 2002
Reality Rift Studios. All Rights Reserved.
***************************************************************************/
#include "textarea.h"
#include "attribute.h"
#include <neoengine/font.h>
#include <neoengine/logstream.h>
#ifdef HAVE_NEOCHUNKIO
# include <neochunkio/stdstring.h>
# include <neochunkio/color.h>
# include <neochunkio/bool.h>
using namespace NeoChunkIO;
#endif
#include <string>
using namespace std;
using namespace NeoEngine;
namespace NeoWTK
{
TextArea::TextArea( Object *pkParent, TextArea *pkObject ) :
Area( pkParent, pkObject )
{
if( !pkObject )
{
m_kFontColor = Color::WHITE;
m_bPassword = false;
m_kPadding.Set( 0, 1 );
}
else
{
m_pkFont = pkObject->m_pkFont;
m_kPadding = pkObject->m_kPadding;
m_kFontColor = pkObject->m_kFontColor;
m_strText = pkObject->m_strText;
m_bPassword = pkObject->m_bPassword;
}
}
TextArea::~TextArea()
{
}
void TextArea::SetAttribute( const AttributeBase &rkAttribute )
{
if( rkAttribute.m_strName == "padding" )
{
const Attribute< Coord > *pkData = dynamic_cast< const Attribute< Coord >* >( &rkAttribute );
if( pkData )
{
SetTextPadding( pkData->m_kData );
return;
}
}
else if( rkAttribute.m_strName == "password" )
{
const Attribute< bool > *pkData = dynamic_cast< const Attribute< bool >* >( &rkAttribute );
if( pkData )
{
m_bPassword = pkData->m_kData;
return;
}
}
else if( rkAttribute.m_strName == "text" )
{
const Attribute< string > *pkData = dynamic_cast< const Attribute< string >* >( &rkAttribute );
if( pkData )
{
SetText( pkData->m_kData );
return;
}
}
Area::SetAttribute( rkAttribute );
}
void TextArea::SetText( const std::string &rstrText )
{
m_strText = rstrText;
}
const std::string &TextArea::GetText()
{
return m_strText;
}
void TextArea::SetFont( FontPtr pkFont )
{
m_pkFont = pkFont;
}
FontPtr TextArea::GetFont()
{
return m_pkFont;
}
void TextArea::SetFontColor( const Color &rkColor )
{
m_kFontColor = rkColor;
}
const Color &TextArea::GetFontColor()
{
return m_kFontColor;
}
void TextArea::SetTextPadding( const Coord &rkPadding )
{
m_kPadding = rkPadding;
}
const Coord &TextArea::GetTextPadding()
{
return m_kPadding;
}
bool TextArea::Render( Frustum *pkFrustum, bool bForce )
{
//Render background
if( !Area::Render( pkFrustum, bForce ) )
return false;
Coord kPos( GetWorldPosition() );
if( m_pkFont && m_strText.length() )
{
Color kOldColor = m_pkFont->GetColor();
m_pkFont->SetColor( m_kFontColor );
if( !m_bPassword )
m_pkFont->Printf( kPos.x + m_kPadding.x, kPos.y + m_kPadding.y, m_strText.c_str() );
else
m_pkFont->Printf( kPos.x + m_kPadding.x, kPos.y + m_kPadding.y, string( m_strText.length(), '*' ).c_str() );
m_pkFont->SetColor( kOldColor );
}
return true;
}
Object *TextArea::Duplicate( Object *pkParent, Object *pkObject )
{
if( !pkObject )
pkObject = new TextArea( pkParent, this );
else
{
TextArea *pkArea = dynamic_cast< TextArea* >( pkObject );
pkArea->SetFont( m_pkFont );
pkArea->SetText( m_strText );
pkArea->SetTextPadding( m_kPadding );
pkArea->SetFontColor( m_kFontColor );
}
return Area::Duplicate( pkParent, pkObject );
}
#ifdef HAVE_NEOCHUNKIO
int TextAreaChunk::ParseData( unsigned int uiFlags, FileManager *pkFileManager )
{
Chunk *pkChunk = 0;
int iRet = 0;
if( !m_pkObject )
m_pkObject = new TextArea( 0, 0 );
TextArea *pkArea = dynamic_cast< TextArea* >( m_pkObject );
if( ( iRet = AreaChunk::ParseData( uiFlags, pkFileManager ) ) <= 0 )
return iRet;
if( ( pkChunk = FindChunk( "padding", NeoWTK::ChunkType::COORD, -1 ) ) )
pkArea->SetTextPadding( dynamic_cast< CoordChunk* >( pkChunk )->m_kCoord );
if( ( pkChunk = FindChunk( "text", NeoChunkIO::ChunkType::STRING, -1 ) ) )
pkArea->SetText( dynamic_cast< StringChunk* >( pkChunk )->m_strData );
if( ( pkChunk = FindChunk( "textcolor", NeoChunkIO::ChunkType::COLOR, -1 ) ) )
pkArea->SetFontColor( dynamic_cast< ColorChunk* >( pkChunk )->m_kData );
if( ( pkChunk = FindChunk( "font", NeoChunkIO::ChunkType::STRING, -1 ) ) )
{
string strFont = dynamic_cast< StringChunk* >( pkChunk )->m_strData;
FontPtr pkFont = Core::Get()->GetFontManager()->FindFont( strFont );
if( pkFont )
pkArea->SetFont( pkFont );
else
neolog << LogLevel( WARNING ) << "[TextAreaChunk::ParseData] unable to locate font [" << strFont << "]" << endl;
}
if( ( pkChunk = FindChunk( "password", NeoChunkIO::ChunkType::BOOL, -1 ) ) )
pkArea->m_bPassword = dynamic_cast< BoolChunk* >( pkChunk )->m_bData;
return 1;
}
#endif
}; /*! namespace NeoWTK */
See more files for this project here