2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 /** @file script_text.hpp Everything to handle text which can be translated. */
10 #ifndef SCRIPT_TEXT_HPP
11 #define SCRIPT_TEXT_HPP
13 #include "script_object.hpp"
14 #include "../../core/alloc_type.hpp"
19 * Internal parent object of all Text-like objects.
22 class Text
: public ScriptObject
{
25 * Convert a ScriptText to a normal string.
29 virtual std::string
GetEncodedText() = 0;
32 * Convert a #ScriptText into a decoded normal string.
36 const std::string
GetDecodedText();
40 * Internally used class to create a raw text in a Text object.
43 class RawText
: public Text
{
45 RawText(const std::string
&text
);
47 std::string
GetEncodedText() override
{ return this->text
; }
49 const std::string text
;
53 * Class that handles all text related functions. You can define a language
54 * file in lang/english.txt, in the same format as OpenTTD does, including
55 * tags like {BLACK}, {STRING1} etc. The name given to this string is made
56 * available to you in ScriptText, for example: ScriptText.STR_NEWS, if your
57 * english.txt contains: STR_NEWS :{BLACK}Welcome {COMPANY}!
59 * In translation files like lang/dutch.txt you can then translate such
60 * strings, like: STR_NEWS :{BLACK}Hallo {COMPANY}!
61 * When the user has the dutch language selected, it will automatically use
62 * the translated string when available. The fallback language is always
63 * the english language.
65 * If you use parameters in your strings, you will have to define those
66 * parameters, for example like this:
67 * \code local text = ScriptText(ScriptText.STR_NEWS);
68 * text.AddParam(1); \endcode
69 * This will set the {COMPANY} to the name of Company 1. Alternatively you
70 * can directly give those arguments to the ScriptText constructor, like this:
71 * \code local text = ScriptText(ScriptText.STR_NEWS, 1); \endcode
75 class ScriptText
: public Text
, public ZeroedMemoryAllocator
{
77 static const int SCRIPT_TEXT_MAX_PARAMETERS
= 20; ///< The maximum amount of parameters you can give to one object.
81 * The constructor wrapper from Squirrel.
83 ScriptText(HSQUIRRELVM vm
);
86 * Generate a text from string. You can set parameters to the instance which
87 * can be required for the string.
88 * @param string The string of the text.
89 * @param ... Optional arguments for this string.
91 ScriptText(StringID string
, ...);
92 #endif /* DOXYGEN_API */
96 * Used for .param_N and [] set from Squirrel.
98 SQInteger
_set(HSQUIRRELVM vm
);
103 SQInteger
SetParam(HSQUIRRELVM vm
);
108 SQInteger
AddParam(HSQUIRRELVM vm
);
111 * Set the parameter to a value.
112 * @param parameter Which parameter to set.
113 * @param value The value of the parameter. Has to be string, integer or an instance of the class ScriptText.
115 void SetParam(int parameter
, Object value
);
118 * Add a value as parameter (appending it).
119 * @param value The value of the parameter. Has to be string, integer or an instance of the class ScriptText.
120 * @return The same object as on which this is called, so you can chain.
122 ScriptText
*AddParam(Object value
);
123 #endif /* DOXYGEN_API */
128 std::string
GetEncodedText() override
;
131 using ScriptTextRef
= ScriptObjectRef
<ScriptText
>;
132 using StringIDList
= std::vector
<StringID
>;
133 using Param
= std::variant
<SQInteger
, std::string
, ScriptTextRef
>;
141 ParamCheck(StringID owner
, int idx
, Param
*param
) : owner(owner
), idx(idx
), param(param
), used(false) {}
143 void Encode(std::back_insert_iterator
<std::string
> &output
);
146 using ParamList
= std::vector
<ParamCheck
>;
147 using ParamSpan
= std::span
<ParamCheck
>;
150 Param param
[SCRIPT_TEXT_MAX_PARAMETERS
];
154 * Internal function to recursively fill a list of parameters.
155 * The parameters are added as _GetEncodedText used to encode them
156 * before the addition of parameter validation.
157 * @param params The list of parameters to fill.
159 void _FillParamList(ParamList
¶ms
);
162 * Internal function for recursive calling this function over multiple
163 * instances, while writing in the same buffer.
164 * @param output The output to write the encoded text to.
165 * @param param_count The number of parameters that are in the string.
166 * @param seen_ids The list of seen StringID.
168 void _GetEncodedText(std::back_insert_iterator
<std::string
> &output
, int ¶m_count
, StringIDList
&seen_ids
, ParamSpan args
);
171 * Set a parameter, where the value is the first item on the stack.
173 SQInteger
_SetParam(int k
, HSQUIRRELVM vm
);
176 #endif /* SCRIPT_TEXT_HPP */