decal.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // SuperTux - Decal
  2. // Copyright (C) 2008 Christoph Sommer <christoph.sommer@2008.expires.deltadevelopment.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. #include "object/decal.hpp"
  17. #include "scripting/decal.hpp"
  18. #include "supertux/flip_level_transformer.hpp"
  19. #include "sprite/sprite_manager.hpp"
  20. #include "util/reader.hpp"
  21. #include "util/reader_mapping.hpp"
  22. Decal::Decal(const ReaderMapping& reader) :
  23. MovingSprite(reader, "images/decal/explanations/billboard-bigtux.png", LAYER_OBJECTS, COLGROUP_DISABLED),
  24. ExposedObject<Decal, scripting::Decal>(this),
  25. m_default_action("default"),
  26. m_solid(),
  27. m_fade_sprite(m_sprite.get()->clone()),
  28. m_fade_timer(),
  29. m_sprite_timer(),
  30. m_visible(true)
  31. {
  32. m_layer = reader_get_layer(reader, LAYER_OBJECTS);
  33. reader.get("solid", m_solid, false);
  34. if (m_solid)
  35. set_group(COLGROUP_STATIC);
  36. if (reader.get("action", m_default_action))
  37. set_action(m_default_action, -1);
  38. }
  39. ObjectSettings
  40. Decal::get_settings()
  41. {
  42. ObjectSettings result = MovingSprite::get_settings();
  43. result.add_bool(_("Solid"), &m_solid, "solid", false);
  44. result.add_text(_("Action"), &m_default_action, "action", "default");
  45. result.reorder({"z-pos", "sprite", "x", "y"});
  46. return result;
  47. }
  48. Decal::~Decal()
  49. {
  50. }
  51. void
  52. Decal::draw(DrawingContext& context)
  53. {
  54. if (m_visible || m_fade_timer.started())
  55. m_sprite->draw(context.color(), get_pos(), m_layer, m_flip);
  56. if (m_visible && m_sprite_timer.started())
  57. m_fade_sprite->draw(context.color(), get_pos(), m_layer, m_flip);
  58. }
  59. void
  60. Decal::fade_in(float fade_time)
  61. {
  62. if (m_visible)
  63. return;
  64. m_visible = true;
  65. m_fade_timer.start(fade_time);
  66. }
  67. void
  68. Decal::fade_out(float fade_time)
  69. {
  70. if (!m_visible)
  71. return;
  72. m_visible = false;
  73. m_fade_timer.start(fade_time);
  74. }
  75. void
  76. Decal::fade_sprite(const std::string& new_sprite, float fade_time)
  77. {
  78. m_fade_sprite = SpriteManager::current()->create(new_sprite);
  79. m_sprite.swap(m_fade_sprite);
  80. // From now on flip_sprite == the old one
  81. m_sprite.get()->set_alpha(0);
  82. m_sprite_timer.start(fade_time);
  83. }
  84. void
  85. Decal::on_flip(float height)
  86. {
  87. MovingObject::on_flip(height);
  88. FlipLevelTransformer::transform_flip(m_flip);
  89. }
  90. void
  91. Decal::update(float)
  92. {
  93. if (m_sprite_timer.started())
  94. {
  95. if (m_sprite_timer.check())
  96. {
  97. m_sprite.get()->set_alpha(1.0f);
  98. m_fade_sprite.get()->set_alpha(0.0f);
  99. m_sprite_timer.stop();
  100. }
  101. else
  102. {
  103. // Square root makes the background stay at fairly constant color/transparency
  104. float new_alpha = sqrtf(m_sprite_timer.get_progress());
  105. float old_alpha = sqrtf(m_sprite_timer.get_timeleft() / m_sprite_timer.get_period());
  106. m_sprite.get()->set_alpha(new_alpha);
  107. m_fade_sprite.get()->set_alpha(old_alpha);
  108. }
  109. }
  110. if (m_fade_timer.started())
  111. {
  112. if (m_fade_timer.check())
  113. {
  114. if(m_visible)
  115. m_sprite.get()->set_alpha(1.0f);
  116. else
  117. m_sprite.get()->set_alpha(0.0f);
  118. m_fade_timer.stop();
  119. }
  120. else
  121. {
  122. float alpha;
  123. if (m_visible) alpha = m_fade_timer.get_progress();
  124. else alpha = m_fade_timer.get_timeleft() / m_fade_timer.get_period();
  125. m_sprite.get()->set_alpha(alpha);
  126. }
  127. }
  128. }
  129. /* EOF */