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