text_array_object.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // SuperTux
  2. // Copyright (C) 2018 Nir <goproducti@gmail.com>
  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_ARRAY_OBJECT_HPP
  17. #define HEADER_SUPERTUX_OBJECT_TEXT_ARRAY_OBJECT_HPP
  18. #include <memory>
  19. #include "squirrel/exposed_object.hpp"
  20. #include "supertux/game_object.hpp"
  21. #include "scripting/text_array_object.hpp"
  22. #include "supertux/timer.hpp"
  23. #include "object/text_object.hpp"
  24. #include "object/text_array_item.hpp"
  25. typedef size_t ta_index;
  26. /** A text array object intended for narration */
  27. class TextArrayObject final : public GameObject,
  28. public ExposedObject<TextArrayObject, scripting::TextArrayObject>
  29. {
  30. public:
  31. TextArrayObject(const std::string& name = "");
  32. ~TextArrayObject() override = default;
  33. virtual void draw(DrawingContext& context) override;
  34. virtual void update(float dt_sec) override;
  35. virtual bool is_saveable() const override { return false; }
  36. static std::string class_name() { return "text-array"; }
  37. virtual std::string get_class_name() const override { return class_name(); }
  38. static std::string display_name() { return _("Text array"); }
  39. virtual std::string get_display_name() const override { return display_name(); }
  40. virtual const std::string get_icon_path() const override {
  41. return "images/engine/editor/textarray.png";
  42. }
  43. /////////// TextArray api related ///////////
  44. /** Empties the text array. */
  45. void clear();
  46. /** Adds a text with duration.
  47. @param: text the text itself (can be multiline & formatted).
  48. @param: duration (optional) the text display time in seconds, defaults to 3. */
  49. void add_text(const std::string& text, float duration = 3.0f);
  50. /** Sets the current text index.
  51. @param: index the index to set to. */
  52. void set_text_index(ta_index index);
  53. /** Sets the keep visible flag.
  54. This flag overrides all texts to be visible.
  55. @note: fade_transition overrides this
  56. @param: keep_visible true to enable keep_visible; false to disable the flag. */
  57. void set_keep_visible(bool keep_visible);
  58. /** Sets the fade transition flag.
  59. This flag overrides all texts to be visible and fading.
  60. @note: overrides keep_visible flag */
  61. void set_fade_transition(bool fade_transition);
  62. /** Sets fadetime for fade_transition.
  63. @param: fadetime the fade time.
  64. @note: does NOT override the TextArray::fade_in() method. */
  65. void set_fade_time(float fadetime);
  66. /** Sets the done flag as on. This disables the text array.
  67. @note: the text array is not cleared.
  68. @param: done true for on; false for off. */
  69. void set_done(bool done);
  70. /** Sets the auto flag on & starts the auto narration.
  71. @note: this starts the auto narration immediately!
  72. this is disabled once the user inputs a skip! */
  73. void set_auto(bool is_auto);
  74. /** Sets the current text to the next one.
  75. @note: if the text is the last on the array,
  76. the done flag is set, and the text array is disabled. */
  77. void next_text();
  78. /** Sets the current text to the previous.
  79. @note: if the current text is the first on the array,
  80. it stays that way. */
  81. void prev_text();
  82. /////////// TextArrayObject access ///////////
  83. /** Gets the text item at a certain index.
  84. @param: index the index of the text item to get.
  85. @return: pointer to the text array item; or nullptr if fails. */
  86. TextArrayItem* get_text_item(ta_index index) const;
  87. /** Gets the current text item.
  88. @return: pointer the current text array item; or nullptr if fails. */
  89. TextArrayItem* get_current_text_item() const;
  90. /** Gets the last text item.
  91. @return: pointer to the last text item; or nullptr if fails. */
  92. TextArrayItem* get_last_text_item() const;
  93. private:
  94. /** Overrides the properties of the text objects, according to the flags. */
  95. void override_properties();
  96. /** Resets the auto narration state and updates it if necessary. */
  97. void reset_automation();
  98. /** Handles user input requests (skipping, rewinding)
  99. @note: might change to manual mode (disables auto flag) */
  100. void handle_input_requests();
  101. /** Should fade transition logic apply
  102. @return: true if fadeTransition flag is on & the transition is valid;
  103. false otherwise. */
  104. bool should_fade() const;
  105. private:
  106. bool m_isDone;
  107. bool m_isAuto;
  108. bool m_keepVisible;
  109. bool m_fadeTransition;
  110. float m_fadetime;
  111. std::vector<std::unique_ptr<TextArrayItem> > m_texts;
  112. ta_index m_curTextIndex;
  113. ta_index m_lastTextIndex;
  114. Timer m_waiting;
  115. private:
  116. TextArrayObject(const TextArrayObject&) = delete;
  117. TextArrayObject& operator=(const TextArrayObject&) = delete;
  118. };
  119. #endif
  120. /* EOF */