ef591eeb24804b0ac5ced7e3db04ac6eb0d2d056
[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()
31   : fading(0), fadetime(0), visible(false)
32 {
33   font = blue_text;
34   centered = false;
35 }
36
37 TextObject::~TextObject()
38 {
39 }
40
41 void
42 TextObject::expose(HSQUIRRELVM vm, SQInteger table_idx)
43 {
44   Scripting::Text* interface = static_cast<Scripting::Text*> (this);
45   Scripting::expose_object(vm, table_idx, interface, "Text", false);
46 }
47
48 void
49 TextObject::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
50 {
51   Scripting::unexpose_object(vm, table_idx, "Text");
52 }
53
54 void
55 TextObject::set_font(const std::string& name)
56 {
57   if(name == "gold") {
58     font = gold_text;
59   } else if(name == "white") {
60     font = white_text;
61   } else if(name == "blue") {
62     font = blue_text;
63   } else if(name == "gray") {
64     font = gray_text;
65   } else if(name == "big") {
66     font = white_big_text;
67   } else if(name == "small") {
68     font = white_small_text;
69   } else {
70     log_warning << "Unknown font '" << name << "'." << std::endl;
71   }
72 }
73
74 void
75 TextObject::set_text(const std::string& text)
76 {
77   this->text = text;
78 }
79
80 void
81 TextObject::fade_in(float fadetime)
82 {
83   this->fadetime = fadetime;
84   fading = fadetime;
85 }
86
87 void
88 TextObject::fade_out(float fadetime)
89 {
90   this->fadetime = fadetime;
91   fading = -fadetime;
92 }
93
94 void
95 TextObject::set_visible(bool visible)
96 {
97   this->visible = visible;
98   fading = 0;
99 }
100
101 void
102 TextObject::set_centered(bool centered)
103 {
104   this->centered = centered;
105 }
106
107 void
108 TextObject::draw(DrawingContext& context)
109 {
110   context.push_transform();
111   context.set_translation(Vector(0, 0));
112   if(fading > 0) {
113     context.set_alpha((fadetime-fading) / fadetime);
114   } else if(fading < 0) {
115     context.set_alpha(-fading / fadetime);
116   } else if(!visible) {
117     context.pop_transform();
118     return;
119   }
120
121   context.draw_filled_rect(Vector(125, 50), Vector(550, 120),
122       Color(0.6, 0.7, 0.8, 0.5), LAYER_GUI-50);
123   if (centered) {
124     context.draw_center_text(font, text, Vector(0, 50+35), LAYER_GUI-40);
125   }
126   else context.draw_text(font, text, Vector(125+35, 50+35), LEFT_ALLIGN, LAYER_GUI-40);
127
128   context.pop_transform();
129 }
130
131 void
132 TextObject::update(float elapsed_time)
133 {
134   if(fading > 0) {
135     fading -= elapsed_time;
136     if(fading <= 0) {
137       fading = 0;
138       visible = true;
139     }
140   } else if(fading < 0) {
141     fading += elapsed_time;
142     if(fading >= 0) {
143       fading = 0;
144       visible = false;
145     }
146   }
147 }
148