- renamed LEFT_ALLIGN to ALIGN_LEFT
[supertux.git] / src / object / text_object.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
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.
10 //
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.
15 //
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.
19
20 #include <config.h>
21
22 #include "text_object.hpp"
23
24 #include <iostream>
25 #include "resources.hpp"
26 #include "video/drawing_context.hpp"
27 #include "scripting/squirrel_util.hpp"
28 #include "log.hpp"
29
30 TextObject::TextObject(std::string name)
31   : fading(0), fadetime(0), visible(false)
32 {
33   this->name = name;
34   font = blue_text;
35   centered = false;
36 }
37
38 TextObject::~TextObject()
39 {
40 }
41
42 void
43 TextObject::expose(HSQUIRRELVM vm, SQInteger table_idx)
44 {
45   if (name.empty())
46     return;
47
48   Scripting::expose_object(vm, table_idx, dynamic_cast<Scripting::Text *>(this), name, false);
49 }
50
51 void
52 TextObject::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
53 {
54   if (name.empty())
55     return;
56
57   Scripting::unexpose_object(vm, table_idx, name);
58 }
59
60 void
61 TextObject::set_font(const std::string& name)
62 {
63   if(name == "gold") {
64     font = gold_text;
65   } else if(name == "white") {
66     font = white_text;
67   } else if(name == "blue") {
68     font = blue_text;
69   } else if(name == "gray") {
70     font = gray_text;
71   } else if(name == "big") {
72     font = white_big_text;
73   } else if(name == "small") {
74     font = white_small_text;
75   } else {
76     log_warning << "Unknown font '" << name << "'." << std::endl;
77   }
78 }
79
80 void
81 TextObject::set_text(const std::string& text)
82 {
83   this->text = text;
84 }
85
86 void
87 TextObject::fade_in(float fadetime)
88 {
89   this->fadetime = fadetime;
90   fading = fadetime;
91 }
92
93 void
94 TextObject::fade_out(float fadetime)
95 {
96   this->fadetime = fadetime;
97   fading = -fadetime;
98 }
99
100 void
101 TextObject::set_visible(bool visible)
102 {
103   this->visible = visible;
104   fading = 0;
105 }
106
107 void
108 TextObject::set_centered(bool centered)
109 {
110   this->centered = centered;
111 }
112
113 void
114 TextObject::draw(DrawingContext& context)
115 {
116   context.push_transform();
117   context.set_translation(Vector(0, 0));
118   if(fading > 0) {
119     context.set_alpha((fadetime-fading) / fadetime);
120   } else if(fading < 0) {
121     context.set_alpha(-fading / fadetime);
122   } else if(!visible) {
123     context.pop_transform();
124     return;
125   }
126
127   context.draw_filled_rect(Vector(125, 50), Vector(550, 120),
128       Color(0.6f, 0.7f, 0.8f, 0.5f), LAYER_GUI-50);
129   if (centered) {
130     context.draw_center_text(font, text, Vector(0, 50+35), LAYER_GUI-40);
131   } else {
132     context.draw_text(font, text, Vector(125+35, 50+35), ALIGN_LEFT, LAYER_GUI-40);
133   }
134
135   context.pop_transform();
136 }
137
138 void
139 TextObject::update(float elapsed_time)
140 {
141   if(fading > 0) {
142     fading -= elapsed_time;
143     if(fading <= 0) {
144       fading = 0;
145       visible = true;
146     }
147   } else if(fading < 0) {
148     fading += elapsed_time;
149     if(fading >= 0) {
150       fading = 0;
151       visible = false;
152     }
153   }
154 }