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