Unified Messaging Subsystem
[supertux.git] / src / object / text_object.cpp
1 #include <config.h>
2
3 #include "text_object.hpp"
4
5 #include <iostream>
6 #include "resources.hpp"
7 #include "video/drawing_context.hpp"
8 #include "msg.hpp"
9
10 TextObject::TextObject()
11   : fading(0), fadetime(0), visible(false)
12 {
13   font = blue_text;
14   centered = false;
15 }
16
17 TextObject::~TextObject()
18 {
19 }
20
21 void
22 TextObject::set_font(const std::string& name)
23 {
24   if(name == "gold") {
25     font = gold_text;
26   } else if(name == "white") {
27     font = white_text;
28   } else if(name == "blue") {
29     font = blue_text;
30   } else if(name == "gray") {
31     font = gray_text;
32   } else if(name == "big") {
33     font = white_big_text;
34   } else if(name == "small") {
35     font = white_small_text;
36   } else {
37     msg_warning("Unknown font '" << name << "'.");
38   }
39 }
40
41 void
42 TextObject::set_text(const std::string& text)
43 {
44   this->text = text;
45 }
46
47 void
48 TextObject::fade_in(float fadetime)
49 {
50   this->fadetime = fadetime;
51   fading = fadetime;
52 }
53
54 void
55 TextObject::fade_out(float fadetime)
56 {
57   this->fadetime = fadetime;
58   fading = -fadetime;
59 }
60
61 void
62 TextObject::set_visible(bool visible)
63 {
64   this->visible = visible;
65   fading = 0;
66 }
67
68 void
69 TextObject::set_centered(bool centered)
70 {
71   this->centered = centered;
72 }
73
74 void
75 TextObject::draw(DrawingContext& context)
76 {
77   context.push_transform();
78   context.set_translation(Vector(0, 0));
79   if(fading > 0) {
80     context.set_alpha((fadetime-fading) / fadetime);
81   } else if(fading < 0) {
82     context.set_alpha(-fading / fadetime);
83   } else if(!visible) {
84     context.pop_transform();
85     return;
86   }
87
88   context.draw_filled_rect(Vector(125, 50), Vector(550, 120),
89       Color(0.6, 0.7, 0.8, 0.5), LAYER_GUI-50);
90   if (centered) {
91     context.draw_center_text(font, text, Vector(0, 50+35), LAYER_GUI-40);
92   }
93   else context.draw_text(font, text, Vector(125+35, 50+35), LEFT_ALLIGN, LAYER_GUI-40);
94
95   context.pop_transform();
96 }
97
98 void
99 TextObject::update(float elapsed_time)
100 {
101   if(fading > 0) {
102     fading -= elapsed_time;
103     if(fading <= 0) {
104       fading = 0;
105       visible = true;
106     }
107   } else if(fading < 0) {
108     fading += elapsed_time;
109     if(fading >= 0) {
110       fading = 0;
111       visible = false;
112     }
113   }
114 }
115