|
1 | | -import { GuiOption } from '../../mod-options' |
| 1 | +import type { GuiOption, InputFieldIsValidFunc } from '../../mod-options' |
2 | 2 |
|
3 | 3 | const optGet = (guiOption: GuiOption): unknown => { |
4 | 4 | return modmanager.options[guiOption.modId][guiOption.baseId] |
@@ -268,72 +268,95 @@ sc.OPTION_GUIS[sc.OPTION_TYPES.CONTROLS].inject({ |
268 | 268 |
|
269 | 269 | declare global { |
270 | 270 | namespace modmanager.gui { |
271 | | - interface OptionsOptionInputField extends ig.GuiElementBase { |
272 | | - option: GuiOption |
| 271 | + interface InputFieldWrapper extends ig.GuiElementBase { |
273 | 272 | inputField: nax.ccuilib.InputField |
274 | 273 | isValidText?: sc.TextGui |
275 | 274 | } |
276 | | - interface OptionsOptionInputFieldValidationConstructor extends ImpactClass<OptionsOptionInputField> { |
277 | | - new (option: GuiOption, y: number, rowGroup: sc.RowButtonGroup, width: number): OptionsOptionInputField |
| 275 | + interface InputFieldWrapperConstructor extends ImpactClass<OptionsOptionInputField> { |
| 276 | + new ( |
| 277 | + initialValue: string, |
| 278 | + setValueFunc: (text: string) => void, |
| 279 | + width: number, |
| 280 | + isValid?: InputFieldIsValidFunc, |
| 281 | + description?: string |
| 282 | + ): OptionsOptionInputField |
278 | 283 | } |
279 | | - var OptionsOptionInputField: OptionsOptionInputFieldValidationConstructor |
| 284 | + var InputFieldWrapper: InputFieldWrapperConstructor |
280 | 285 | } |
281 | 286 | } |
282 | | - |
283 | | -modmanager.gui.OptionsOptionInputField = ig.GuiElementBase.extend({ |
284 | | - init(option, y, rowGroup, width) { |
| 287 | +modmanager.gui.InputFieldWrapper = ig.GuiElementBase.extend({ |
| 288 | + init(initialValue, setValueFunc, width, isValidFunc, description) { |
285 | 289 | this.parent() |
286 | | - this.option = option |
287 | | - if (option.type != 'INPUT_FIELD') throw new Error('how') |
288 | 290 |
|
289 | 291 | this.inputField = new nax.ccuilib.InputField(width - 30, 20) |
290 | 292 |
|
291 | | - const text = optGet(option) as string |
292 | | - this.inputField.setText?.(text) |
| 293 | + this.inputField.setText?.(initialValue) |
293 | 294 |
|
294 | 295 | const revalidate = async (text: string) => { |
295 | | - if (!option.isValid) throw new Error('how') |
| 296 | + if (!isValidFunc) throw new Error('how') |
296 | 297 |
|
297 | 298 | this.isValidText!.setText('\\i[lore-others]') |
298 | | - const isValid = await option.isValid(text) |
| 299 | + const isValid = await isValidFunc(text) |
299 | 300 |
|
300 | 301 | if (this.inputField.getValueAsString() == text) { |
301 | 302 | this.isValidText!.setText(isValid ? '\\i[quest-solve]' : '\\i[quest-elite]') |
302 | 303 | return isValid |
303 | 304 | } else return false |
304 | 305 | } |
305 | 306 |
|
306 | | - if (option.isValid) { |
| 307 | + if (isValidFunc) { |
307 | 308 | this.inputField.setPos(20 + 4, 0) |
308 | 309 | this.isValidText = new sc.TextGui('') |
309 | 310 | this.isValidText.setPos(3, 2) |
310 | 311 | this.addChildGui(this.isValidText) |
311 | | - revalidate(text) |
| 312 | + revalidate(initialValue) |
312 | 313 | } else { |
313 | 314 | this.inputField.setPos(12, 0) |
314 | 315 | this.inputField.setSize(this.inputField.hook.size.x + 10, this.inputField.hook.size.y) |
315 | 316 | } |
316 | 317 |
|
317 | 318 | this.inputField.onCharacterInput = str => { |
318 | | - if (option.isValid) { |
| 319 | + if (isValidFunc) { |
319 | 320 | revalidate(str).then(isValid => { |
320 | | - if (isValid) optSet(option, str) |
| 321 | + if (isValid) setValueFunc(str) |
321 | 322 | }) |
322 | 323 | } else { |
323 | | - optSet(option, str) |
| 324 | + setValueFunc(str) |
324 | 325 | } |
325 | 326 | } |
326 | 327 |
|
327 | 328 | this.inputField.setAlign(ig.GUI_ALIGN.X_LEFT, ig.GUI_ALIGN.Y_CENTER) |
328 | 329 |
|
329 | | - const backup = this.inputField.focusGained.bind(this.inputField) |
330 | | - this.inputField.focusGained = function (this: nax.ccuilib.InputField) { |
331 | | - backup() |
332 | | - sc.menu.setInfoText(option.description as string) |
| 330 | + if (description) { |
| 331 | + const backup = this.inputField.focusGained.bind(this.inputField) |
| 332 | + this.inputField.focusGained = function (this: nax.ccuilib.InputField) { |
| 333 | + backup() |
| 334 | + sc.menu.setInfoText(description) |
| 335 | + } |
333 | 336 | } |
334 | 337 | this.setSize(width, this.inputField.hook.size.y) |
335 | 338 |
|
336 | 339 | this.addChildGui(this.inputField) |
| 340 | + }, |
| 341 | +}) |
| 342 | + |
| 343 | +declare global { |
| 344 | + namespace modmanager.gui { |
| 345 | + interface OptionsOptionInputField extends modmanager.gui.InputFieldWrapper { |
| 346 | + option: GuiOption |
| 347 | + } |
| 348 | + interface OptionsOptionInputFieldConstructor extends ImpactClass<OptionsOptionInputField> { |
| 349 | + new (option: GuiOption, y: number, rowGroup: sc.RowButtonGroup, width: number): OptionsOptionInputField |
| 350 | + } |
| 351 | + var OptionsOptionInputField: OptionsOptionInputFieldConstructor |
| 352 | + } |
| 353 | +} |
| 354 | + |
| 355 | +modmanager.gui.OptionsOptionInputField = modmanager.gui.InputFieldWrapper.extend({ |
| 356 | + init(option, y, rowGroup, width) { |
| 357 | + this.option = option |
| 358 | + if (option.type != 'INPUT_FIELD') throw new Error('how') |
| 359 | + this.parent(optGet(option) as string, text => optSet(option, text), width, option.isValid, option.description) |
337 | 360 |
|
338 | 361 | rowGroup.addFocusGui(this.inputField, 0, y) |
339 | 362 | }, |
|
0 commit comments