text_object.hpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. #ifndef HEADER_SUPERTUX_OBJECT_TEXT_OBJECT_HPP
  17. #define HEADER_SUPERTUX_OBJECT_TEXT_OBJECT_HPP
  18. #include "editor/layer_object.hpp"
  19. #include "math/anchor_point.hpp"
  20. #include "video/color.hpp"
  21. #include "video/drawing_context.hpp"
  22. #include "video/font_ptr.hpp"
  23. class DrawingContext;
  24. class ReaderMapping;
  25. /**
  26. * A text object intended for scripts that want to tell a story.
  27. * @scripting
  28. * @summary A ""TextObject"" that was given a name can be controlled by scripts.
  29. * @instances A ""TextObject"" is instantiated by placing a definition inside a level.
  30. It can then be accessed by its name from a script or via ""sector.name"" from the console.
  31. */
  32. class TextObject final : public LayerObject
  33. {
  34. static Color default_color;
  35. public:
  36. static void register_class(ssq::VM& vm);
  37. public:
  38. TextObject(const std::string& name = "");
  39. ~TextObject() override;
  40. static std::string class_name() { return "textobject"; }
  41. virtual std::string get_class_name() const override { return class_name(); }
  42. virtual std::string get_exposed_class_name() const override { return "TextObject"; }
  43. static std::string display_name() { return _("Text"); }
  44. virtual std::string get_display_name() const override { return display_name(); }
  45. virtual GameObjectClasses get_class_types() const override { return GameObject::get_class_types().add(typeid(TextObject)); }
  46. virtual const std::string get_icon_path() const override { return "images/engine/editor/textarray.png"; }
  47. virtual void draw(DrawingContext& context) override;
  48. virtual void update(float dt_sec) override;
  49. virtual bool is_saveable() const override { return false; }
  50. /**
  51. * @scripting
  52. * @description Sets the text string to be displayed.
  53. * @param string $text
  54. */
  55. void set_text(const std::string& text);
  56. /**
  57. * @scripting
  58. * @description Returns the displayed text.
  59. */
  60. inline const std::string& get_text() const { return m_text; }
  61. /**
  62. * @scripting
  63. * @description Sets the font of the text to be displayed.
  64. * @param string $fontname Valid values are normal, big and small.
  65. */
  66. void set_font(const std::string& fontname);
  67. /**
  68. * @scripting
  69. * @description Fades in the specified text for the next ""fadetime"" seconds.
  70. * @param float $fadetime
  71. */
  72. void fade_in(float fadetime);
  73. /**
  74. * @scripting
  75. * @description Fades out the specified text for the next ""fadetime"" seconds.
  76. * @param float $fadetime
  77. */
  78. void fade_out(float fadetime);
  79. /**
  80. * @scripting
  81. * @description Grows in the specified text for the next ""fadetime"" seconds.
  82. * @param float $fadetime
  83. */
  84. void grow_in(float fadetime);
  85. /**
  86. * @scripting
  87. * @description Grows out the specified text for the next ""fadetime"" seconds.
  88. * @param float $fadetime
  89. */
  90. void grow_out(float fadetime);
  91. /**
  92. * @scripting
  93. * @description Shows or hides the text abruptly (drastic counterpart to ""fade_in()"" and ""fade_out()"").
  94. * @param bool $visible
  95. */
  96. void set_visible(bool visible);
  97. /**
  98. * @scripting
  99. * @description Returns ""true"" if the text is visible.
  100. */
  101. inline bool get_visible() const { return m_visible; }
  102. /**
  103. * @scripting
  104. * @description If ""centered"" is ""true"", the text will be centered on the screen. Otherwise, it will be left-aligned.
  105. * @param bool $centered
  106. */
  107. inline void set_centered(bool centered) { m_centered = centered; }
  108. /**
  109. * @scripting
  110. * @description Returns ""true"" if the text is centered.
  111. */
  112. inline bool get_centered() const { return m_centered; }
  113. /**
  114. * @scripting
  115. * @description Sets the offset of the text, relative to the anchor point.
  116. * @param float $x
  117. * @param float $y
  118. */
  119. inline void set_pos(float x, float y) { m_pos = Vector(x, y); }
  120. /**
  121. * @scripting
  122. * @description Returns the X offset of the text, relative to the anchor point.
  123. */
  124. inline float get_x() const { return m_pos.x; }
  125. /**
  126. * @scripting
  127. * @description Returns the Y offset of the text, relative to the anchor point.
  128. */
  129. inline float get_y() const { return m_pos.y; }
  130. #ifdef DOXYGEN_SCRIPTING
  131. /**
  132. * @scripting
  133. * @deprecated Use ""get_x()"" instead!
  134. * @description Returns the X offset of the text, relative to the anchor point.
  135. */
  136. float get_pos_x() const;
  137. /**
  138. * @scripting
  139. * @deprecated Use ""get_y()"" instead!
  140. * @description Returns the Y offset of the text, relative to the anchor point.
  141. */
  142. float get_pos_y() const;
  143. #endif
  144. /**
  145. * @scripting
  146. * @description Sets the anchor point of the text.
  147. * @param int $anchor One of the ""ANCHOR_*"" constants (see ${SRG_REF_AnchorPoints}).
  148. */
  149. inline void set_anchor_point(int anchor) { m_anchor = static_cast<AnchorPoint>(anchor); }
  150. /**
  151. * @scripting
  152. * @description Returns the current anchor point of the text (one of the ""ANCHOR_*"" constants; see ${SRG_REF_AnchorPoints}).
  153. */
  154. inline int get_anchor_point() const { return static_cast<int>(m_anchor); }
  155. /**
  156. * @scripting
  157. * @description Sets the anchor offset of the text.
  158. * @param float $x
  159. * @param float $y
  160. */
  161. inline void set_anchor_offset(float x, float y) { m_anchor_offset = Vector(x, y); }
  162. /**
  163. * @scripting
  164. * @description Gets the text wrap width of the text.
  165. */
  166. inline float get_wrap_width() const { return m_wrap_width; }
  167. /**
  168. * @scripting
  169. * @description Sets the text wrap width of the text.
  170. * @param float $width
  171. */
  172. inline void set_wrap_width(float width) { m_wrap_width = width; }
  173. /**
  174. * @scripting
  175. * @description Sets the front fill color of the text.
  176. * @param float $red
  177. * @param float $green
  178. * @param float $blue
  179. * @param float $alpha
  180. */
  181. void set_front_fill_color(float red, float green, float blue, float alpha);
  182. /**
  183. * @scripting
  184. * @description Sets the back fill color of the text.
  185. * @param float $red
  186. * @param float $green
  187. * @param float $blue
  188. * @param float $alpha
  189. */
  190. void set_back_fill_color(float red, float green, float blue, float alpha);
  191. /**
  192. * @scripting
  193. * @description Sets the text color.
  194. * @param float $red
  195. * @param float $green
  196. * @param float $blue
  197. * @param float $alpha
  198. */
  199. void set_text_color(float red, float green, float blue, float alpha);
  200. /**
  201. * @scripting
  202. * @description Sets the frame's roundness.
  203. * @param float $roundness
  204. */
  205. inline void set_roundness(float roundness) { m_roundness = roundness; }
  206. /**
  207. * @scripting
  208. * @description Returns the roundness of the text.
  209. */
  210. inline float get_roundness() const { return m_roundness; }
  211. inline void set_anchor_point(AnchorPoint anchor) { m_anchor = anchor; }
  212. inline void set_anchor_offset(const Vector& offset) { m_anchor_offset = offset; }
  213. inline void set_pos(const Vector& pos) { m_pos = pos; }
  214. inline const Vector& get_pos() const { return m_pos; }
  215. private:
  216. void wrap_text();
  217. private:
  218. FontPtr m_font;
  219. /**
  220. * @scripting
  221. * @description The displayed text.
  222. */
  223. std::string m_text;
  224. std::string m_wrapped_text;
  225. float m_fade_progress;
  226. float m_fadetime;
  227. /**
  228. * @scripting
  229. * @description Determines whether the text is visible.
  230. */
  231. bool m_visible;
  232. /**
  233. * @scripting
  234. * @description Determines whether the text is centered.
  235. */
  236. bool m_centered;
  237. AnchorPoint m_anchor;
  238. #ifdef DOXYGEN_SCRIPTING
  239. /**
  240. * @scripting
  241. * @description The current anchor point.
  242. */
  243. int m_anchor_point;
  244. #endif
  245. Vector m_anchor_offset;
  246. Vector m_pos;
  247. /**
  248. * @scripting
  249. * @description Determines the maximum wrap width of the text.
  250. */
  251. float m_wrap_width;
  252. Color m_front_fill_color;
  253. Color m_back_fill_color;
  254. Color m_text_color;
  255. /**
  256. * @scripting
  257. * @description Determines the roundness of the text frame.
  258. */
  259. float m_roundness;
  260. bool m_growing_in;
  261. bool m_growing_out;
  262. bool m_fading_in;
  263. bool m_fading_out;
  264. bool m_grower;
  265. bool m_fader;
  266. private:
  267. TextObject(const TextObject&) = delete;
  268. TextObject& operator=(const TextObject&) = delete;
  269. };
  270. #endif
  271. /* EOF */