import { Component, Fragment, ReactNode } from "react"; import { useSettingWithEvent, IMixinSettingProps, Language } from "@Context/Setting"; import { AttrInput } from "@Input/AttrInput/AttrInput"; import { ObjectID } from "@Model/Model"; import { TogglesInput } from "@Input/TogglesInput/TogglesInput"; import { ObjectPicker } from "@Input/ObjectPicker/ObjectPicker"; import { AllI18nKeys, I18N } from "@Component/Localization/Localization"; import { Message } from "@Input/Message/Message"; import { ColorInput } from "@Input/ColorInput/ColorInput"; import { ComboInput, IDisplayItem } from "@Input/ComboInput/ComboInput"; import { IParameter, IParameterOption, IParameterOptionItem, IParameterValue, IParamValue, isObjectType } from "@Model/Parameter"; import "./Parameter.scss"; interface IParameterProps

{ option: IParameterOption

; value: IParameterValue

; key: ObjectID; change: (key: K, val: IParamValue) => any; i18n?: (key: string, language: Language) => string; renderKey?: Array; title?: AllI18nKeys; titleOption?: Record; isFirst?: boolean; } @useSettingWithEvent("language") class Parameter

extends Component & IMixinSettingProps> { private renderParameter (key: K, option: IParameterOptionItem, value: IParamValue): ReactNode { const language = this.props.setting?.language ?? "EN_US"; const indexKey = `${this.props.key}-${key}`; const type = option.type; let keyI18n: string, keyI18nOption: Record | undefined; // Custom I18N if (this.props.i18n) { keyI18n = "Panel.Info.Behavior.Details.Parameter.Key"; keyI18nOption = { key: this.props.i18n(option.name, language) }; } else { keyI18n = option.name; } if (type === "number") { return ?? 0} valueChange={(val) => { this.props.change(key, parseFloat(val) as IParamValue); }} />; } else if (type === "string") { return ?? ""} valueChange={(val) => { this.props.change(key, val as IParamValue); }} />; } else if (type === "boolean") { return ?? false} valueChange={(val) => { this.props.change(key, val as IParamValue); }} /> } else if (isObjectType(type)) { type IObjectParamValue = IParamValue<"G" | "R" | "LG" | "LR">; const typedValue = value as IObjectParamValue; return { typedValue.picker = obj as IObjectParamValue["picker"]; this.props.change(key, typedValue as IParamValue); }} cleanValue={() => { typedValue.picker = undefined as IObjectParamValue["picker"]; this.props.change(key, typedValue as IParamValue); }} /> } else if (type === "color") { return ?? false} valueChange={(val) => { this.props.change(key, val as IParamValue); }} /> } else if (type === "option") { let allOption: IDisplayItem[] = []; let focusKey: number = -1; if (option.allOption) { for (let i = 0; i < option.allOption.length; i++) { if (this.props.i18n) { allOption.push({ i18nOption: { key: this.props.i18n(option.allOption[i].name, language) }, i18n: "Panel.Info.Behavior.Details.Parameter.Key", key: option.allOption[i].key }) } else { allOption.push({ i18n: option.allOption[i].name, key: option.allOption[i].key }) } if (option.allOption[i].key === value) { focusKey = i; } } } return { this.props.change(key, val.key as IParamValue); }} /> } else if (type === "vec") { type IObjectParamValue = IParamValue<"vec">; const typedValue = value as IObjectParamValue; const i18nVal = I18N(this.props, keyI18n, keyI18nOption); return { typedValue[0] = parseFloat(val); this.props.change(key, typedValue as IParamValue); }} /> { typedValue[1] = parseFloat(val); this.props.change(key, typedValue as IParamValue); }} /> { typedValue[2] = parseFloat(val); this.props.change(key, typedValue as IParamValue); }} /> } else { return } } private renderAllParameter(key: Array) { return key.map((key) => { return this.renderParameter( key, this.props.option[key], this.props.value[key], ); }); } public render(): ReactNode { let allOptionKeys: Array; if (this.props.renderKey) { allOptionKeys = this.props.renderKey; } else { allOptionKeys = Object.getOwnPropertyNames(this.props.option); } return <> { allOptionKeys.length > 0 && this.props.title ? : null } { this.renderAllParameter(allOptionKeys) } } } export { Parameter }