1// SuperTux
2// Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17#include "object/floating_image.hpp"
18
19#include "sprite/sprite.hpp"
20#include "sprite/sprite_manager.hpp"
21#include "supertux/globals.hpp"
22
23FloatingImage::FloatingImage(const std::string& spritefile) :
24 sprite(SpriteManager::current()->create(spritefile)),
25 layer(LAYER_FOREGROUND1 + 1),
26 visible(false),
27 anchor(ANCHOR_MIDDLE),
28 pos(),
29 fading(0),
30 fadetime(0)
31{
32}
33
34FloatingImage::~FloatingImage()
35{
36}
37
38void
39FloatingImage::update(float dt_sec)
40{
41 if (fading > 0) {
42 fading -= dt_sec;
43 if (fading <= 0) {
44 fading = 0;
45 visible = true;
46 }
47 } else if (fading < 0) {
48 fading += dt_sec;
49 if (fading >= 0) {
50 fading = 0;
51 visible = false;
52 }
53 }
54}
55
56void
57FloatingImage::set_action(const std::string& action)
58{
59 sprite->set_action(action);
60}
61
62std::string
63FloatingImage::get_action()
64{
65 return sprite->get_action();
66}
67
68void
69FloatingImage::fade_in(float fadetime_)
70{
71 fadetime = fadetime_;
72 fading = fadetime_;
73}
74
75void
76FloatingImage::fade_out(float fadetime_)
77{
78 fadetime = fadetime_;
79 fading = -fadetime_;
80}
81
82void
83FloatingImage::draw(DrawingContext& context)
84{
85 context.push_transform();
86 context.set_translation(Vector(0, 0));
87
88 if (fading > 0) {
89 context.set_alpha((fadetime-fading) / fadetime);
90 } else if (fading < 0) {
91 context.set_alpha(-fading / fadetime);
92 } else if (!visible) {
93 context.pop_transform();
94 return;
95 }
96
97 Vector spos = pos + get_anchor_pos(Rectf(0, 0,
98 static_cast<float>(context.get_width()),
99 static_cast<float>(context.get_height())),
100 static_cast<float>(sprite->get_width()),
101 static_cast<float>(sprite->get_height()),
102 anchor);
103
104 sprite->draw(context.color(), spos, layer);
105
106 context.pop_transform();
107}
108
109/* EOF */
110