changed worldmap format a bit to be more consistent with level format
[supertux.git] / src / object / text_object.cpp
1 #include <config.h>
2
3 #include "text_object.h"
4 #include "resources.h"
5 #include "video/drawing_context.h"
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 == "white") {
29     font = white_text;
30   } else if(name == "big") {
31     font = white_big_text;
32   } else if(name == "small") {
33     font = white_small_text;
34   } else {
35     std::cerr << "Unknown font '" << name << "'.\n";
36   }
37 }
38
39 void
40 TextObject::set_text(const std::string& text)
41 {
42   this->text = text;
43 }
44
45 void
46 TextObject::fade_in(float fadetime)
47 {
48   this->fadetime = fadetime;
49   fading = fadetime;
50 }
51
52 void
53 TextObject::fade_out(float fadetime)
54 {
55   this->fadetime = fadetime;
56   fading = -fadetime;
57 }
58
59 void
60 TextObject::set_visible(bool visible)
61 {
62   this->visible = visible;
63   fading = 0;
64 }
65
66 void
67 TextObject::draw(DrawingContext& context)
68 {
69   context.push_transform();
70   context.set_translation(Vector(0, 0));
71   if(fading > 0) {
72     context.set_alpha(static_cast<uint8_t> 
73         ((fadetime-fading) * 255.0 / fadetime));
74   } else if(fading < 0) {
75     context.set_alpha(static_cast<uint8_t> (-fading * 255.0 / fadetime));
76   } else if(!visible) {
77     context.pop_transform();
78     return;
79   }
80
81   context.draw_filled_rect(Vector(125, 50), Vector(550, 120),
82       Color(150, 180, 200, 125), LAYER_GUI-50);
83   context.draw_text(font, text, Vector(125+35, 50+35), LEFT_ALLIGN, LAYER_GUI-40);
84
85   context.pop_transform();
86 }
87
88 void
89 TextObject::update(float elapsed_time)
90 {
91   if(fading > 0) {
92     fading -= elapsed_time;
93     if(fading <= 0) {
94       fading = 0;
95       visible = true;
96     }
97   } else if(fading < 0) {
98     fading += elapsed_time;
99     if(fading >= 0) {
100       fading = 0;
101       visible = false;
102     }
103   }
104 }
105