4
Copyright (C) 2005-2009 Julien Jorge, Sebastien Angibaud
6
This program is free software; you can redistribute it and/or modify it
7
under the terms of the GNU General Public License as published by the
8
Free Software Foundation; either version 2 of the License, or (at your
9
option) any later version.
11
This program is distributed in the hope that it will be useful, but WITHOUT
12
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16
You should have received a copy of the GNU General Public License along
17
with this program; if not, write to the Free Software Foundation, Inc.,
18
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
contact: plee-the-bear@gamned.org
22
Please add the tag [PTB] in the subject of your mails.
26
* \brief Implementation of the ptb::log_layer class.
27
* \author Julien Jorge
29
#include "ptb/layer/log_layer.hpp"
31
#include "engine/level_globals.hpp"
33
#include <claw/logger.hpp>
34
#include <claw/string_algorithm.hpp>
36
/*----------------------------------------------------------------------------*/
40
ptb::log_layer::log_layer()
41
: m_visible(false), m_lines(15), m_append(false)
44
} // log_layer::log_layer()
46
/*----------------------------------------------------------------------------*/
50
ptb::log_layer::~log_layer()
52
claw::logger.remove(this);
54
for (unsigned int i=0; i!=m_lines.size(); ++i)
56
} // log_layer::~log_layer()
58
/*----------------------------------------------------------------------------*/
60
* \brief Initialize the layer.
62
void ptb::log_layer::build()
64
bear::visual::font font =
65
get_level_globals().get_font( "font/fixed_white-7x12.fnt" );
67
claw::math::coordinate_2d<unsigned int> pos( font->get_size() );
69
for (unsigned int i=0; i!=m_lines.size(); ++i)
71
m_lines[i] = new bear::gui::static_text( NULL, font );
72
m_lines[i]->set_auto_size( true );
73
m_lines[i]->set_position( pos );
74
pos.y += font->get_size().y;
77
claw::logger.merge(this);
78
} // log_layer::build()
80
/*----------------------------------------------------------------------------*/
82
* \brief Render the layer on a screen.
83
* \param e (out) The scene elements.
85
void ptb::log_layer::render( scene_element_list& e ) const
88
for (unsigned int i=0; i!=m_lines.size(); ++i)
89
m_lines[i]->render(e);
90
} // log_layer::render()
92
/*----------------------------------------------------------------------------*/
94
* \brief Inform the layer that a keyboard key has been pressed.
95
* \param key The value of the pressed key.
97
bool ptb::log_layer::key_pressed( const bear::input::key_info& key )
101
if ( key.is_function(6) )
102
m_visible = !m_visible;
107
} // log_layer::key_pressed()
109
/*----------------------------------------------------------------------------*/
111
* \brief Write a string in the stream.
112
* \param str The string to write.
114
void ptb::log_layer::write( const std::string& str )
116
std::list<std::string> lines;
118
std::list<std::string>::const_iterator it;
120
claw::text::split(lines, str, '\n');
122
if ( lines.size() > m_lines.size() )
131
m_lines[i]->set_text(*it);
135
else if ( !lines.empty() )
137
unsigned int keep = m_lines.size() - lines.size();
142
unsigned int j = m_lines.size() - keep;
144
for (i=0; i!=keep; ++i, ++j)
145
m_lines[i]->set_text( m_lines[j]->get_text() );
151
m_lines[i-1]->set_text( m_lines[i-1]->get_text() + *it );
155
for ( ; it!=lines.end(); ++it, ++i )
156
m_lines[i]->set_text(*it);
159
if ( str.size() > 0 )
160
m_append = ( str[str.size() - 1] != '\n' );
161
} // log_layer::flush()