4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #include "text_object.hpp"
25 #include "resources.hpp"
27 #include "video/drawing_context.hpp"
28 #include "scripting/squirrel_util.hpp"
31 TextObject::TextObject(std::string name)
32 : fading(0), fadetime(0), visible(false), anchor(ANCHOR_MIDDLE),
40 TextObject::~TextObject()
45 TextObject::expose(HSQUIRRELVM vm, SQInteger table_idx)
50 Scripting::expose_object(vm, table_idx, dynamic_cast<Scripting::Text *>(this), name, false);
54 TextObject::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
59 Scripting::unexpose_object(vm, table_idx, name);
63 TextObject::set_font(const std::string& name)
65 if(name == "normal") {
67 } else if(name == "big") {
69 } else if(name == "small") {
72 log_warning << "Unknown font '" << name << "'." << std::endl;
78 TextObject::set_text(const std::string& text)
84 TextObject::fade_in(float fadetime)
86 this->fadetime = fadetime;
91 TextObject::fade_out(float fadetime)
93 this->fadetime = fadetime;
98 TextObject::set_visible(bool visible)
100 this->visible = visible;
105 TextObject::set_centered(bool centered)
107 this->centered = centered;
111 TextObject::draw(DrawingContext& context)
113 context.push_transform();
114 context.set_translation(Vector(0, 0));
116 context.set_alpha((fadetime-fading) / fadetime);
117 } else if(fading < 0) {
118 context.set_alpha(-fading / fadetime);
119 } else if(!visible) {
120 context.pop_transform();
126 Vector spos = pos + get_anchor_pos(Rect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT),
127 width, height, anchor);
129 context.draw_filled_rect(spos, Vector(width, height),
130 Color(0.6f, 0.7f, 0.8f, 0.5f), LAYER_GUI-50);
132 context.draw_center_text(font, text, spos, LAYER_GUI-40, TextObject::default_color);
134 context.draw_text(font, text, spos + Vector(10, 10), ALIGN_LEFT, LAYER_GUI-40, TextObject::default_color);
137 context.pop_transform();
141 TextObject::update(float elapsed_time)
144 fading -= elapsed_time;
149 } else if(fading < 0) {
150 fading += elapsed_time;