text_object.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. #include "object/text_object.hpp"
  17. #include "editor/editor.hpp"
  18. #include "supertux/sector.hpp"
  19. #include "util/reader.hpp"
  20. #include "util/reader_mapping.hpp"
  21. #include "supertux/globals.hpp"
  22. #include "supertux/resources.hpp"
  23. #include "video/drawing_context.hpp"
  24. TextObject::TextObject(const std::string& name) :
  25. GameObject(name),
  26. ExposedObject<TextObject, scripting::TextObject>(this),
  27. m_font(Resources::normal_font),
  28. m_text(),
  29. m_wrapped_text(),
  30. m_fade_progress(0),
  31. m_fadetime(0),
  32. m_visible(false),
  33. m_centered(false),
  34. m_anchor(ANCHOR_MIDDLE),
  35. m_anchor_offset(0, 0),
  36. m_pos(0, 0),
  37. m_wrap_width(500.f),
  38. m_front_fill_color(0.6f, 0.7f, 0.8f, 0.5f),
  39. m_back_fill_color(0.2f, 0.3f, 0.4f, 0.8f),
  40. m_text_color(1.f, 1.f, 1.f, 1.f),
  41. m_roundness(16.f),
  42. m_growing_in(),
  43. m_growing_out(),
  44. m_fading_in(),
  45. m_fading_out(),
  46. m_grower(),
  47. m_fader()
  48. {
  49. }
  50. TextObject::~TextObject()
  51. {
  52. }
  53. void
  54. TextObject::set_font(const std::string& name)
  55. {
  56. if (name == "normal") {
  57. m_font = Resources::normal_font;
  58. } else if (name == "big") {
  59. m_font = Resources::big_font;
  60. } else if (name == "small") {
  61. m_font = Resources::small_font;
  62. } else {
  63. log_warning << "Unknown font '" << name << "'." << std::endl;
  64. m_font = Resources::normal_font;
  65. }
  66. wrap_text();
  67. }
  68. void
  69. TextObject::wrap_text()
  70. {
  71. m_wrapped_text.clear();
  72. std::string rest = m_text;
  73. do {
  74. std::string overflow;
  75. m_wrapped_text += m_font->wrap_to_width(rest, m_wrap_width, &overflow);
  76. if (!overflow.empty()) {
  77. m_wrapped_text += "\n";
  78. }
  79. rest = overflow;
  80. } while (!rest.empty());
  81. }
  82. void
  83. TextObject::set_text(const std::string& text)
  84. {
  85. m_text = text;
  86. wrap_text();
  87. }
  88. void
  89. TextObject::grow_in(float fadetime)
  90. {
  91. m_fadetime = fadetime;
  92. m_visible = true;
  93. m_fade_progress = 0;
  94. m_growing_in = true;
  95. m_grower = true;
  96. }
  97. void
  98. TextObject::grow_out(float fadetime)
  99. {
  100. m_fadetime = fadetime;
  101. m_fade_progress = 1;
  102. m_growing_out = true;
  103. }
  104. void
  105. TextObject::fade_in(float fadetime)
  106. {
  107. m_fadetime = fadetime;
  108. m_visible = true;
  109. m_fade_progress = 0;
  110. m_fading_in = true;
  111. m_fader = true;
  112. }
  113. void
  114. TextObject::fade_out(float fadetime)
  115. {
  116. m_fadetime = fadetime;
  117. m_fade_progress = 1;
  118. m_fading_out = true;
  119. }
  120. void
  121. TextObject::set_visible(bool visible)
  122. {
  123. if (visible)
  124. {
  125. fade_in(0);
  126. }
  127. else
  128. {
  129. fade_out(0);
  130. }
  131. }
  132. void
  133. TextObject::set_centered(bool centered)
  134. {
  135. m_centered = centered;
  136. }
  137. void
  138. TextObject::set_front_fill_color(Color frontfill)
  139. {
  140. m_front_fill_color = frontfill;
  141. }
  142. void
  143. TextObject::set_back_fill_color(Color backfill)
  144. {
  145. m_back_fill_color = backfill;
  146. }
  147. void
  148. TextObject::set_text_color(Color textcolor)
  149. {
  150. m_text_color = textcolor;
  151. }
  152. void
  153. TextObject::set_roundness(float roundness)
  154. {
  155. m_roundness = roundness;
  156. }
  157. void
  158. TextObject::draw(DrawingContext& context)
  159. {
  160. context.push_transform();
  161. context.set_translation(Vector(0, 0));
  162. context.transform().scale = 1.f;
  163. if (m_fader)
  164. context.set_alpha(m_fade_progress);
  165. if (!m_visible)
  166. {
  167. context.pop_transform();
  168. return;
  169. }
  170. float width = m_font->get_text_width(m_wrapped_text) + 20.0f;
  171. float height = m_font->get_text_height(m_wrapped_text) + 20.0f;
  172. Vector spos = m_pos + get_anchor_pos(context.get_rect(),
  173. width, height, m_anchor) + m_anchor_offset;
  174. Vector sizepos = spos + (Vector(width / 2.f, height / 2.f)) - (Vector(width / 2.f, height / 2.f) * (m_fade_progress));
  175. if (m_fade_progress > 0.f)
  176. {
  177. const Rectf draw_rect(m_grower ? sizepos : spos, Sizef(width, height) * (m_fader ? 1.f : m_fade_progress));
  178. context.color().draw_filled_rect(draw_rect.grown(4.0f), m_back_fill_color, m_roundness + 4.f, LAYER_GUI + 50);
  179. context.color().draw_filled_rect(draw_rect, m_front_fill_color, m_roundness, LAYER_GUI + 50);
  180. }
  181. if (m_fader || (m_grower && m_fade_progress >= 1.f))
  182. {
  183. context.color().draw_text(m_font, m_wrapped_text,
  184. spos + Vector(m_centered ? width / 2.f : 10.f, 10.f),
  185. m_centered ? ALIGN_CENTER : ALIGN_LEFT,
  186. LAYER_GUI + 60, m_text_color);
  187. }
  188. context.pop_transform();
  189. }
  190. void
  191. TextObject::update(float dt_sec)
  192. {
  193. if ((m_growing_in || m_fading_in) && m_fade_progress < 1.f)
  194. {
  195. m_fade_progress += dt_sec / m_fadetime;
  196. if (m_fade_progress >= 1.f)
  197. {
  198. m_fade_progress = 1.f;
  199. m_visible = true;
  200. m_growing_in = false;
  201. m_fading_in = false;
  202. }
  203. }
  204. else if ((m_growing_out || m_fading_out) && m_fade_progress > 0.f)
  205. {
  206. m_fade_progress -= dt_sec / m_fadetime;
  207. if (m_fade_progress <= 0.f)
  208. {
  209. m_fade_progress = 0.f;
  210. m_visible = false;
  211. m_growing_out = false;
  212. m_fading_out = false;
  213. m_grower = false;
  214. m_fader = false;
  215. }
  216. }
  217. }
  218. /* EOF */