1 /*
2  * This file is part of EasyRPG Player.
3  *
4  * EasyRPG Player 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  * EasyRPG Player 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 EasyRPG Player. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef EP_GAME_MESSAGE_H
19 #define EP_GAME_MESSAGE_H
20 
21 #include <vector>
22 #include <bitset>
23 #include <string>
24 #include <functional>
25 #include "string_view.h"
26 #include "pending_message.h"
27 
28 class Window_Message;
29 class AsyncOp;
30 
31 namespace Game_Message {
32 
33 	static const int MAX_LINE = 4;
34 
35 	/** Set the window used to display the text */
36 	void SetWindow(Window_Message* window);
37 
38 	Window_Message* GetWindow();
39 
40 	AsyncOp Update();
41 
42 	/** Contains the different lines of text. */
43 	extern std::vector<std::string> texts;
44 
45 	/**
46 	 * Determines the position of the message box respecting the player's map
47 	 * position and if obstructing is allowed.
48 	 *
49 	 * @return Best message box position
50 	 */
51 	int GetRealPosition();
52 
53 	/**
54 	 * Sets pending message text for the messaging system to display.
55 	 *
56 	 * @param pm - the pending message text
57 	 */
58 	void SetPendingMessage(PendingMessage&& pm);
59 
60 	/** Callback type for WordWrap function */
61 	using WordWrapCallback = const std::function<void(StringView line)>;
62 
63 	/**
64 	 * Breaks the line into lines, each of which is equal
65 	 * or less than a specified limit in pixels in the
66 	 * given font (except in cases when breaking by spaces
67 	 * can't produce a short line), and calls the callback
68 	 * for each resulting line.
69 	 *
70 	 * Font::Default() will be used to determine the word breaking.
71 	 * The caller is responsible for ensuring that Font::Default()
72 	 * either does not change between calling this function and
73 	 * displaying the results, or at least that the changed font
74 	 * has same metrics as the font used to calculate the line sizes.
75 	 *
76 	 * @param[in] line The line that will be broken into lines
77 	 * and added into the lines vector.
78 	 * @param[in] limit maximum size of each line after word-breaking.
79 	 * @param callback a function to be called for each word-wrapped line
80 	 */
81 	int WordWrap(StringView line, int limit, const WordWrapCallback& callback);
82 
83 	/**
84 	 * Return if it's legal to show a new message box.
85 	 *
86 	 * @param foreground true if this is in the foreground context, otherwise parallel context.
87 	 * @return true if we can show a message box.
88 	 */
89 	bool CanShowMessage(bool foreground);
90 
91 	/** @return true if there is message text pending */
92 	bool IsMessagePending();
93 	/** @return true if message window is running */
94 	bool IsMessageActive();
95 
96 	// EasyRPG extension allowing more recursive variables \v[\v[...]]
97 	static constexpr int easyrpg_default_max_recursion = 8;
98 	// RPG_RT only allows 1 level of recursion.
99 	static constexpr int rpg_rt_default_max_recursion = 1;
100 	// Which one we'll use by default.
101 	static constexpr int default_max_recursion = easyrpg_default_max_recursion;
102 
103 	/** Struct returned by parameter parsing methods */
104 	struct ParseParamResult {
105 		/** iterator to the next character after parsed content */
106 		const char* next = nullptr;
107 		/** value that was parsed */
108 		int value = 0;
109 	};
110 
111 	/** Parse a \v[] variable string
112 	 *
113 	 * @param iter start of utf8 string
114 	 * @param end end of utf8 string
115 	 * @param escape_char the escape character to use
116 	 * @param skip_prefix if true, assume prefix was already parsed and iter starts at the first left bracket.
117 	 * @param max_recursion How many times to allow recursive variable lookups.
118 	 *
119 	 * @return \refer ParseParamResult
120 	 */
121 	ParseParamResult ParseVariable(const char* iter, const char* end, uint32_t escape_char, bool skip_prefix = false, int max_recursion = default_max_recursion);
122 
123 	/** Parse a \c[] color string
124 	 *
125 	 * @param iter start of utf8 string
126 	 * @param end end of utf8 string
127 	 * @param escape_char the escape character to use
128 	 * @param skip_prefix if true, assume prefix was already parsed and iter starts at the first left bracket.
129 	 * @param max_recursion How many times to allow recursive variable lookups.
130 	 *
131 	 * @return \refer ParseParamResult
132 	 */
133 	ParseParamResult ParseColor(const char* iter, const char* end, uint32_t escape_char, bool skip_prefix = false, int max_recursion = default_max_recursion);
134 
135 	/** Parse a \s[] speed string
136 	 *
137 	 * @param iter start of utf8 string
138 	 * @param end end of utf8 string
139 	 * @param escape_char the escape character to use
140 	 * @param skip_prefix if true, assume prefix was already parsed and iter starts at the first left bracket.
141 	 * @param max_recursion How many times to allow recursive variable lookups.
142 	 *
143 	 * @return \refer ParseParamResult
144 	 */
145 	ParseParamResult ParseSpeed(const char* iter, const char* end, uint32_t escape_char, bool skip_prefix = false, int max_recursion = default_max_recursion);
146 
147 	/** Parse a \n[] actor name string
148 	 *
149 	 * @param iter start of utf8 string
150 	 * @param end end of utf8 string
151 	 * @param escape_char the escape character to use
152 	 * @param skip_prefix if true, assume prefix was already parsed and iter starts at the first left bracket.
153 	 * @param max_recursion How many times to allow recursive variable lookups.
154 	 *
155 	 * @return \refer ParseParamResult
156 	 */
157 	ParseParamResult ParseActor(const char* iter, const char* end, uint32_t escape_char, bool skip_prefix = false, int max_recursion = default_max_recursion);
158 }
159 
160 
161 #endif
162