8f70da822ca537079cf2eb4975b8d5b64d5f4b1e
[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 = normal_font;
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 == "normal") {
66     font = normal_font;
67   } else if(name == "big") {
68     font = big_font;
69   } else if(name == "small") {
70     font = small_font;
71   } else {
72     log_warning << "Unknown font '" << name << "'." << std::endl;
73     font = normal_font;
74   }
75 }
76
77 void
78 TextObject::set_text(const std::string& text)
79 {
80   this->text = text;
81 }
82
83 void
84 TextObject::fade_in(float fadetime)
85 {
86   this->fadetime = fadetime;
87   fading = fadetime;
88 }
89
90 void
91 TextObject::fade_out(float fadetime)
92 {
93   this->fadetime = fadetime;
94   fading = -fadetime;
95 }
96
97 void
98 TextObject::set_visible(bool visible)
99 {
100   this->visible = visible;
101   fading = 0;
102 }
103
104 void
105 TextObject::set_centered(bool centered)
106 {
107   this->centered = centered;
108 }
109
110 void
111 TextObject::draw(DrawingContext& context)
112 {
113   context.push_transform();
114   context.set_translation(Vector(0, 0));
115   if(fading > 0) {
116     context.set_alpha((fadetime-fading) / fadetime);
117   } else if(fading < 0) {
118     context.set_alpha(-fading / fadetime);
119   } else if(!visible) {
120     context.pop_transform();
121     return;
122   }
123
124   float width  = 500;
125   float height = 70;
126   Vector spos = pos + get_anchor_pos(Rect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT),
127       width, height, anchor);
128
129   context.draw_filled_rect(spos, Vector(width, height),
130       Color(0.6f, 0.7f, 0.8f, 0.5f), LAYER_GUI-50);
131   if (centered) {
132     context.draw_center_text(font, text, spos, LAYER_GUI-40, TextObject::default_color);
133   } else {
134     context.draw_text(font, text, spos + Vector(10, 10), ALIGN_LEFT, LAYER_GUI-40, TextObject::default_color);
135   }
136
137   context.pop_transform();
138 }
139
140 void
141 TextObject::update(float elapsed_time)
142 {
143   if(fading > 0) {
144     fading -= elapsed_time;
145     if(fading <= 0) {
146       fading = 0;
147       visible = true;
148     }
149   } else if(fading < 0) {
150     fading += elapsed_time;
151     if(fading >= 0) {
152       fading = 0;
153       visible = false;
154     }
155   }
156 }