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