ebe3e32d7e617c3d2ac033ddcef88610bad2a277
[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   }
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 }