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