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