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