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