/
Phileas_TextWrap.js
208 lines (188 loc) · 7.1 KB
/
Phileas_TextWrap.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//=============================================================================
// Phileas_TextWrap.js
//=============================================================================
// [Update History]
// 2023.July.02 Ver1.0.0 First Release
// 2023.July.03 Ver1.0.1 Fixed TAA bug
// 2023.July.03 Ver1.0.2 TAA_BookMenu standard padding
/*:
* @target MZ
* @plugindesc Automatic text wrapping.
* @author Phileas
*
* @command getWrappedText
* @text Writes the wrapped text to a variable.
* @desc Inserts line breaks into the given text so that the width of the text does not exceed the specified one.
*
* @arg text
* @text Text
* @type string
* @desc Input non-empty text
* @default String
*
* @arg fontName
* @text Font name
* @type combo
* @option GameFont
* @option Arial
* @option Courier New
* @option SimHei
* @option Heiti TC
* @option Dotum
* @option AppleGothic
* @desc Text font.
* @default GameFont
*
* @arg fontSize
* @text Font size
* @type number
* @desc Input a positive number.
* @default 26
*
* @arg maxWidth
* @text Max text width
* @type number
* @desc Input a positive number.
* @default 700
*
* @arg variableId
* @text A variable id
* @type variable
* @desc The text will be written to this variable.
* @default 1
*
*
* @help
* The plugin adds a line break to the text in such a way that the text does not go beyond the borders of the window.
* The plugin works automatically for message windows and the TAA_BookMenu plugin.
*
* Plugin Command: getWrappedText
* This command will allow you to write the processed text to a variable.
*
* Feature of the plugin: the text color setting (\C[x]) is saved when the line is moved. Even if the new line is displayed in another window (which often happens when transferring text in the message window).
*
* You can always write to the author if you need support for other windows. Or if you need other features or even plugins.
* Boosty: https://boosty.to/phileas
* RPG Maker Web: https://forums.rpgmakerweb.com/index.php?members/phileas.176075/
* RPG Maker Union: https://rpgmakerunion.ru/id/phileas
* Email: olek.olegovich gmail.com
* Telegram: olekolegovich
*
* [License]
* This plugin is released under MIT license.
* http://opensource.org/licenses/mit-license.php
*/
(function() {
//--------MY CODE:
PluginManager.registerCommand("Phileas_TextWrap", "getWrappedText", getWrappedTextByCommand);
function getColor(word) {
for (let i = word.length - 2; i > -1; --i) {
if (word[i] == "\\" && word[i + 1] == "C" && word[i + 2] == "[") {
let tag = "\\C[";
let j = i + 3;
while (word[j] != ']' && j < word.length) {
tag += word[j];
++j;
}
if (j == word.length) {
return "";
}
if (i > 0 && word[i - 1] == "\\") {
tag = "\\" + tag;
}
return tag + "]"
}
}
return "";
}
Window_Base.prototype.phileasGetTextWidth = function(text, x, y, width) {
const textState = this.createTextState(text, x, y, width);
this.processAllText(textState);
return textState.outputWidth;
};
function getWrappedText(text, maxWidth, mainWindow) {
let wrapWindow = new Window_Base(new Rectangle(mainWindow.x, mainWindow.y, maxWidth, mainWindow.height));
wrapWindow.contents.fontFace = mainWindow.contents.fontFace;
wrapWindow.contents.fontSize = mainWindow.contents.fontSize;
rect = wrapWindow.baseTextRect();
let result = "";
let word = "";
let line = "";
let lastIndex = 0;
let currentColor = "";
let nFlag = false;
if (text[text.length - 1] != " ") {
text += " ";
}
for (let i = 0; i < text.length; ++i) {
if (text[i] == "\\" && i + 1 < text.length && text[i] == "n") {
nFlag = true;
}
else if (text[i] != " ") {
continue;
}
word = text.substring(lastIndex, i + 1);
let newColor = getColor(word);
if (newColor != "") {
currentColor = newColor;
}
line += word;
let currentWidth = wrapWindow.phileasGetTextWidth(line, rect.x, rect.y, rect.width);
//console.log('parsed: ', [line, currentWidth]);
if (currentWidth > maxWidth) {
result += "\n";
currentWidth = wrapWindow.textWidth(wrapWindow.convertEscapeCharacters(word));
line = word = currentColor + word;
}
result += word;
lastIndex = i + 1;
if (nFlag) {
result += "\n";
line = word = currentColor;
++lastIndex;
++i;
nFlag = false;
}
}
return result;
}
function getWrappedTextByCommand(params) {
let text = params['text'];
let fontName = params['fontName'];
let fontSize = Number(params['fontSize']);
let maxWidth = Number(params['maxWidth']);
let variableId = Number(params['variableId']);
if (text == undefined || text == "" || fontName == undefined || fontName == ""
|| fontSize < 1 || maxWidth < 1 || variableId < 1) {
console.log('Wrapping failed for these parameters: ', {'text':text, 'fontName':fontName, 'fontSize':fontSize, 'maxWidth':maxWidth, 'variableId':variableId});
return;
}
let wrapWindow = new Window_Base(new Rectangle(0, 0, maxWidth, Graphics.boxHeight));
wrapWindow.contents.fontFace = fontName;
wrapWindow.contents.fontSize = fontSize;
let wrappedText = getWrappedText(text, maxWidth, wrapWindow);
$gameVariables.setValue(variableId, wrappedText);
}
Window_Message.prototype.phileasGetWindowMessageMargin = function() {
const faceExists = $gameMessage.faceName() !== "";
const spacing = 30;
return faceExists ? ImageManager.faceWidth + spacing : 4;
}
//--------CHANGED CORE:
Origin_startMessage = Window_Message.prototype.startMessage;
Window_Message.prototype.startMessage = function() {
let text = $gameMessage.allText();
let maxWidth = this.width - this.phileasGetWindowMessageMargin();
let wrappedText = getWrappedText(text, maxWidth, this);
$gameMessage._texts.length = 0;
$gameMessage._texts = [wrappedText];
Origin_startMessage.call(this);
};
if (typeof Window_BookText != "undefined") {
Origin_TaaPreparePrintableObjects = Window_BookText.prototype.preparePrintableObjects;
Window_BookText.prototype.preparePrintableObjects = function(text) {
let wrappedText = getWrappedText(text, this.windowWidth() - this.standardPadding(), this);
Origin_TaaPreparePrintableObjects.call(this, wrappedText);
}
}
}());