-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScreenTint.js
283 lines (261 loc) · 8.73 KB
/
ScreenTint.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/** /*:
* @plugindesc Alternative way of changing screen tone.
* @author Caio Oliveira
*
* @help
* # Version 1.0.0
* # Source: https://github.com/caiofov/ScreenTint-RPGMaker-Plugin
*
* You can use variables (following RPG Maker's syntax
* - '\v[<variable_id>]') in all command arguments.
*
* > Change screen tone directly via command:
*
* tint tone <red> <green> <blue> <gray> <frames>
*
* - red (integer - between -255 and 255): red value
* - green (integer - between -255 and 255): green value
* - blue (integer - between -255 and 255): blue value
* - gray (integer - between 0 and 255): gray value
* - frames (integer): transition's duration (60 frames = 1 second)
*
*
* > Load saved tone:
* You can also save tones in the 'Saved Tones' field on the right
* side of this screen.
* To load them, you must use the following command:
*
* tint load <saved tone identifier>
*
* - saved tone identifier (string): saved tone's name
*
* Warning: It is case sensitive
*
* @param Show logs
* @type boolean
* @on Display
* @off Do not display
* @default false
* @desc Whether if to display or not the plugin logs (visible by pressing F8 when the game is running). Useful for debugging.
*
* @param Saved Tones
* @type struct<Tone>[]
* @desc Saved tones for loading in the game
* @default ["{\"Name\":\"Normal\", \"Red\":\"0\",\"Green\":\"0\",\"Blue\":\"0\",\"Gray\":\"0\",\"Frames\":\"60\"}", "{\"Name\":\"Dark\", \"Red\":\"-68\",\"Green\":\"-68\",\"Blue\":\"-68\",\"Gray\":\"0\",\"Frames\":\"60\"}", "{\"Name\":\"Sepia\", \"Red\":\"34\",\"Green\":\"-34\",\"Blue\":\"-68\",\"Gray\":\"170\",\"Frames\":\"60\"}", "{\"Name\":\"Sunset\", \"Red\":\"68\",\"Green\":\"-34\",\"Blue\":\"-34\",\"Gray\":\"0\",\"Frames\":\"60\"}", "{\"Name\":\"Night\", \"Red\":\"-68\",\"Green\":\"-68\",\"Blue\":\"0\",\"Gray\":\"68\",\"Frames\":\"60\"}"]
*
*
*/
/** /*:pt
* @plugindesc Maneira alternativa de alterar a tonalidade da tela.
* @author Caio Oliveira
*
* @help
* # Versão 1.0.0
* # Referência: https://github.com/caiofov/ScreenTint-RPGMaker-Plugin
*
* Você pode fazer o uso de variáveis (seguindo a sintaxe do RPG Maker -
* '\v[<id_da_variável>]') em todos argumentos dos comandos.
*
* > Mudar tonalidade diretamente por comando:
*
* tint tone <vermelho> <verde> <azul> <cinza> <quadros>
*
* - vermelho (inteiro - entre -255 e 255): valor vermelho
* - verde (inteiro - entre -255 e 255): valor verde
* - azul (inteiro - entre -255 e 255): valor azul
* - cinza (inteiro - entre 0 e 255): valor cinza
* - quadros (inteiro): duração da transição (60 quadros = 1 segundo)
*
*
* > Carregar uma tonalidade salva:
* Você também pode salvar suas tonalidades no campo "Tonalidades Salvas" do
* lado direito desta tela.
* Para carregá-las, deverá usar o seguinte comando:
*
* tint load <identificador da tonalidade salva>
*
* - identificador da tonalidade salva (string): nome da tonalidade salva
*
* Atenção: O comando diferencia letras maiúsculas e minúsculas
* @param Show logs
* @text Mostrar logs
* @type boolean
* @on Mostrar
* @off Não mostrar
* @default false
* @desc Mostrar ou não os logs do plugin (Visíveis quando pressiona F8 enquanto o jogo está executando). Útil para depuração.
*
* @param Saved Tones
* @text Tonalidade Salvas
* @type struct<Tone>[]
* @desc Salvar os tons para carregá-los mais facilmente
* @default ["{\"Name\":\"Normal\", \"Red\":\"0\",\"Green\":\"0\",\"Blue\":\"0\",\"Gray\":\"0\",\"Frames\":\"60\"}", "{\"Name\":\"Escuro\", \"Red\":\"-68\",\"Green\":\"-68\",\"Blue\":\"-68\",\"Gray\":\"0\",\"Frames\":\"60\"}", "{\"Name\":\"Sepia\", \"Red\":\"34\",\"Green\":\"-34\",\"Blue\":\"-68\",\"Gray\":\"170\",\"Frames\":\"60\"}", "{\"Name\":\"Por do sol\", \"Red\":\"68\",\"Green\":\"-34\",\"Blue\":\"-34\",\"Gray\":\"0\",\"Frames\":\"60\"}", "{\"Name\":\"Noite\", \"Red\":\"-68\",\"Green\":\"-68\",\"Blue\":\"0\",\"Gray\":\"68\",\"Frames\":\"60\"}"]
*
**/
/**
/*~struct~Tone:
* @param Name
* @type text
* @desc tone's identifier
*
* @param Red
* @type number
* @min -255
* @max 255
*
* @param Green
* @type number
* @min -255
* @max 255
*
* @param Blue
* @type number
* @min -255
* @max 255
*
* @param Gray
* @type number
* @min 0
* @max 255
*
* @param Frames
* @type number
* @desc Number of frames for the transition (1s -> 60 frames)
**/
/**
* Converts a text to a log pattern used on this plugin
* @param {string} message text to convert to plugin's error pattern
* @returns error message
*/
function log_message(message) {
return `[Screen Tint Plugin] - ${message}`
}
/**
* Converts a string to Number and validates if it is between `min` and `max`
* @param {string | Number} num number to convert
* @param {Number} min number's min value
* @param {Number} max number's max value (default: 255)
* @returns {Number} converted and validated number
*/
function to_color_number(num, min, max = 255) {
let converted = Number(parse_text(num))
if (converted > max || converted < min) {
throw RangeError(log_message(`Must be between ${min} and ${max}`))
}
return converted
}
/**
* Identifies whether if the text is referencing to a variable and gets its value.
* @param {string} text
* @returns {string | Number}
*/
function parse_text(text) {
let pattern = /\\v\[([0-9]+)\]/
let match = text.match(pattern)
if (match) {
return $gameVariables.value(Number(match[1]))
}
else {
return text
}
}
/**
* ScreenTone
* @param {string | Number} red
* @param {string | Number} green
* @param {string | Number} blue
* @param {string | Number} gray
* @param {string | Number} frames
*
* @type {{red:Number,green:Number,blue:Number,gray:Number,frames:Number}}
*/
function ScreenTone(red, green, blue, gray, frames) {
this.red = to_color_number(red, -255)
this.green = to_color_number(green, -255)
this.blue = to_color_number(blue, -255)
this.gray = to_color_number(gray, 0)
this.frames = parse_text(frames)
/**
* @returns {number[]} Tint data as array
*/
this.to_array = () => {
let a = [this.red, this.green, this.blue, this.gray]
return a
}
}
/**
* Parses the saved tone from plugin's parameters
* @param {string} tone_str Tone object as string
* @extends ScreenTone
* @type {{name:string}}
*/
function SavedTone(tone_str) {
let obj = JSON.parse(tone_str)
this.name = parse_text(obj.Name)
ScreenTone.prototype.constructor.call(this, obj.Red, obj.Green, obj.Blue, obj.Gray, obj.Frames)
}
/**
* Loads plugin's parameters
* @struct Parameters
* @type {{saved_tones:Array<SavedTone>, show_logs:boolean}}
*/
function PluginParameters() {
let params = PluginManager.parameters('ScreenTint')
this.saved_tones = JSON.parse(params["Saved Tones"]).map(tone => { return new SavedTone(tone) })
this.show_logs = JSON.parse(params["Show logs"])
/**
* Returns the saved tone which has the given name
* @param {String} name name to search
* @return {?SavedTone} tone found
*/
this.get_saved_tone = function (name) {
var tone_found = null
this.saved_tones.forEach(tone => {
console.log(tone.name.length, name.length)
if (tone.name === name) {
tone_found = tone
return
}
});
return tone_found
}
}
var PARAMS = new PluginParameters()
const plugin = Game_Interpreter.prototype.pluginCommand;
/**
* Displays a message on the console if 'show_logs' param is enable
* @param {string} message
*/
function log(message) {
if (PARAMS.show_logs) {
console.log(log_message(message))
}
}
Game_Interpreter.prototype.pluginCommand = function (command, args) {
plugin.call(this, command, args);
if (command === "tint") {
let tone
log(`Command: '${args[0]}' | Args: ${args.slice(1)}`)
switch (args[0]) {
case "tone":
if (args.length != 6) {
throw RangeError(log_message(`Invalid number of arguments (expected 5) for command 'tint'.\n
Current args: ${args.slice(1)}`))
}
tone = new ScreenTone(...args.slice(1))
break;
case "load":
let name = args.slice(1).join(" ")
tone = PARAMS.get_saved_tone(name)
if (!tone) {
throw RangeError(log_message(`Tone '${name}' can not be found.`))
}
break;
default:
throw TypeError(log_message(`Invalid command '${args[0]}'. Expected 'tone' or 'load'`))
}
let values = tone.to_array()
log(`Start tint | Values: ${values} | Frames: ${tone.frames}`)
$gameScreen.startTint(values, tone.frames)
}
};