Skip to content

Commit 7f4d3a3

Browse files
committed
Split OptionsOptionInputField into two seperate classes for reuse in other mods
1 parent 684e65a commit 7f4d3a3

2 files changed

Lines changed: 49 additions & 25 deletions

File tree

src/gui/options/option-elements-inject.ts

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { GuiOption } from '../../mod-options'
1+
import type { GuiOption, InputFieldIsValidFunc } from '../../mod-options'
22

33
const optGet = (guiOption: GuiOption): unknown => {
44
return modmanager.options[guiOption.modId][guiOption.baseId]
@@ -268,72 +268,95 @@ sc.OPTION_GUIS[sc.OPTION_TYPES.CONTROLS].inject({
268268

269269
declare global {
270270
namespace modmanager.gui {
271-
interface OptionsOptionInputField extends ig.GuiElementBase {
272-
option: GuiOption
271+
interface InputFieldWrapper extends ig.GuiElementBase {
273272
inputField: nax.ccuilib.InputField
274273
isValidText?: sc.TextGui
275274
}
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
278283
}
279-
var OptionsOptionInputField: OptionsOptionInputFieldValidationConstructor
284+
var InputFieldWrapper: InputFieldWrapperConstructor
280285
}
281286
}
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) {
285289
this.parent()
286-
this.option = option
287-
if (option.type != 'INPUT_FIELD') throw new Error('how')
288290

289291
this.inputField = new nax.ccuilib.InputField(width - 30, 20)
290292

291-
const text = optGet(option) as string
292-
this.inputField.setText?.(text)
293+
this.inputField.setText?.(initialValue)
293294

294295
const revalidate = async (text: string) => {
295-
if (!option.isValid) throw new Error('how')
296+
if (!isValidFunc) throw new Error('how')
296297

297298
this.isValidText!.setText('\\i[lore-others]')
298-
const isValid = await option.isValid(text)
299+
const isValid = await isValidFunc(text)
299300

300301
if (this.inputField.getValueAsString() == text) {
301302
this.isValidText!.setText(isValid ? '\\i[quest-solve]' : '\\i[quest-elite]')
302303
return isValid
303304
} else return false
304305
}
305306

306-
if (option.isValid) {
307+
if (isValidFunc) {
307308
this.inputField.setPos(20 + 4, 0)
308309
this.isValidText = new sc.TextGui('')
309310
this.isValidText.setPos(3, 2)
310311
this.addChildGui(this.isValidText)
311-
revalidate(text)
312+
revalidate(initialValue)
312313
} else {
313314
this.inputField.setPos(12, 0)
314315
this.inputField.setSize(this.inputField.hook.size.x + 10, this.inputField.hook.size.y)
315316
}
316317

317318
this.inputField.onCharacterInput = str => {
318-
if (option.isValid) {
319+
if (isValidFunc) {
319320
revalidate(str).then(isValid => {
320-
if (isValid) optSet(option, str)
321+
if (isValid) setValueFunc(str)
321322
})
322323
} else {
323-
optSet(option, str)
324+
setValueFunc(str)
324325
}
325326
}
326327

327328
this.inputField.setAlign(ig.GUI_ALIGN.X_LEFT, ig.GUI_ALIGN.Y_CENTER)
328329

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+
}
333336
}
334337
this.setSize(width, this.inputField.hook.size.y)
335338

336339
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)
337360

338361
rowGroup.addFocusGui(this.inputField, 0, y)
339362
},

src/mod-options.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,15 @@ type BUTTON_GROUP = OptionChangeable & {
5252
}
5353
)
5454

55+
export type InputFieldIsValidFunc = (text: string) => boolean | Promise<boolean>
5556
interface INPUT_FIELD extends OptionChangeable {
5657
type: 'INPUT_FIELD'
5758
/** Initial option value */
5859
init: string
5960
/** Input field height */
6061
height?: number
6162
/** Validation function */
62-
isValid?: (text: string) => boolean | Promise<boolean>
63+
isValid?: InputFieldIsValidFunc
6364
}
6465

6566
interface ARRAY_SLIDER extends OptionChangeable {

0 commit comments

Comments
 (0)