2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "object/text_object.hpp"
19 #include "scripting/squirrel_util.hpp"
20 #include "supertux/globals.hpp"
21 #include "supertux/resources.hpp"
22 #include "video/drawing_context.hpp"
24 TextObject::TextObject(std::string name) :
31 anchor(ANCHOR_MIDDLE),
35 font = Resources::normal_font;
39 TextObject::~TextObject()
44 TextObject::expose(HSQUIRRELVM vm, SQInteger table_idx)
49 scripting::expose_object(vm, table_idx, dynamic_cast<scripting::Text *>(this), name, false);
53 TextObject::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
58 scripting::unexpose_object(vm, table_idx, name);
62 TextObject::set_font(const std::string& name)
64 if(name == "normal") {
65 font = Resources::normal_font;
66 } else if(name == "big") {
67 font = Resources::big_font;
68 } else if(name == "small") {
69 font = Resources::small_font;
71 log_warning << "Unknown font '" << name << "'." << std::endl;
72 font = Resources::normal_font;
77 TextObject::set_text(const std::string& text)
83 TextObject::fade_in(float fadetime)
85 this->fadetime = fadetime;
90 TextObject::fade_out(float fadetime)
92 this->fadetime = fadetime;
97 TextObject::set_visible(bool visible)
99 this->visible = visible;
104 TextObject::set_centered(bool centered)
106 this->centered = centered;
110 TextObject::draw(DrawingContext& context)
112 context.push_transform();
113 context.set_translation(Vector(0, 0));
115 context.set_alpha((fadetime-fading) / fadetime);
116 } else if(fading < 0) {
117 context.set_alpha(-fading / fadetime);
118 } else if(!visible) {
119 context.pop_transform();
125 Vector spos = pos + get_anchor_pos(Rectf(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT),
126 width, height, anchor);
128 context.draw_filled_rect(spos, Vector(width, height),
129 Color(0.6f, 0.7f, 0.8f, 0.5f), LAYER_GUI-50);
131 context.draw_center_text(font, text, spos, LAYER_GUI-40, TextObject::default_color);
133 context.draw_text(font, text, spos + Vector(10, 10), ALIGN_LEFT, LAYER_GUI-40, TextObject::default_color);
136 context.pop_transform();
140 TextObject::update(float elapsed_time)
143 fading -= elapsed_time;
148 } else if(fading < 0) {
149 fading += elapsed_time;