UNPKG

@kitten-science/kitten-scientists

Version:

Add-on for the wonderful incremental browser game: https://kittensgame.com/web/

157 lines 8.32 kB
import { coalesceArray, isNil } from "@oliversalzburg/js-utils/data/nil.js"; import { BonfireSettings } from "../settings/BonfireSettings.js"; import { cl } from "../tools/Log.js"; import { BuildingUpgradeSettingsUi } from "./BuildingUpgradeSettingsUi.js"; import { BuildSectionTools } from "./BuildSectionTools.js"; import { Delimiter } from "./components/Delimiter.js"; import { Dialog } from "./components/Dialog.js"; import { HeaderListItem } from "./components/HeaderListItem.js"; import { SettingListItem } from "./components/SettingListItem.js"; import { SettingsList } from "./components/SettingsList.js"; import { SettingsPanel } from "./components/SettingsPanel.js"; import { SettingTriggerListItem } from "./components/SettingTriggerListItem.js"; export class BonfireSettingsUi extends SettingsPanel { constructor(parent, settings, locale) { console.debug(...cl(`Constructing ${BonfireSettingsUi.name}`)); const label = parent.host.engine.i18n("ui.build"); super(parent, settings, new SettingTriggerListItem(parent, settings, locale, label, { onCheck: (_isBatchProcess) => { parent.host.engine.imessage("status.auto.enable", [label]); }, onRefreshTrigger: () => { this.settingItem.triggerButton.element[0].title = parent.host.engine.i18n("ui.trigger.section", [ settings.trigger < 0 ? parent.host.engine.i18n("ui.trigger.section.inactive") : parent.host.renderPercentage(settings.trigger, locale.selected, true), ]); }, onSetTrigger: async () => { const value = await Dialog.prompt(parent, parent.host.engine.i18n("ui.trigger.prompt.percentage"), parent.host.engine.i18n("ui.trigger.section.prompt", [ label, settings.trigger !== -1 ? parent.host.renderPercentage(settings.trigger, locale.selected, true) : parent.host.engine.i18n("ui.infinity"), ]), settings.trigger !== -1 ? parent.host.renderPercentage(settings.trigger) : "", parent.host.engine.i18n("ui.trigger.section.promptExplainer")); if (value === undefined) { return; } if (value === "" || value.startsWith("-")) { settings.trigger = -1; return; } settings.trigger = parent.host.parsePercentage(value); }, onUnCheck: (_isBatchProcess) => { parent.host.engine.imessage("status.auto.disable", [label]); }, renderLabelTrigger: false, }), { onRefreshRequest: () => { this.settingItem.triggerButton.inactive = !settings.enabled || settings.trigger < 0; this.settingItem.triggerButton.ineffective = settings.enabled && settings.trigger < 0 && Object.values(settings.buildings).some(_ => _.enabled && 0 < _.max && _.trigger < 0); this.expando.ineffective = settings.enabled && Object.values(settings.buildings).some(_ => _.enabled && (0 === _.max || (_.trigger < 0 && settings.trigger < 0))); }, }); // Post-super() child insertion, so we can use this._getBuildOptions(). // We want the ability to use `this` in our callbacks, to construct more complex // usage scenarios where we need access to the entire UI section. this.addChildrenContent([ new SettingsList(this, { onReset: () => { this.setting.load({ buildings: new BonfireSettings().buildings }); }, }).addChildren(coalesceArray(this.host.game.bld.buildingGroups.flatMap(buildingGroup => [ new HeaderListItem(this, buildingGroup.title), ...buildingGroup.buildings.flatMap(building => this._getBuildOptions(this, settings, locale, label, building)), buildingGroup !== this.host.game.bld.buildingGroups[this.host.game.bld.buildingGroups.length - 1] ? new Delimiter(this) : undefined, ]))), new SettingsList(this, { hasDisableAll: false, hasEnableAll: false, }).addChildren([ new HeaderListItem(this, this.host.engine.i18n("ui.additional")), new SettingListItem(this, settings.gatherCatnip, this.host.engine.i18n("option.catnip"), { onCheck: () => { this.host.engine.imessage("status.sub.enable", [ this.host.engine.i18n("option.catnip"), ]); }, onUnCheck: () => { this.host.engine.imessage("status.sub.disable", [ this.host.engine.i18n("option.catnip"), ]); }, }), new SettingListItem(this, settings.turnOnSteamworks, this.host.engine.i18n("option.steamworks"), { onCheck: () => { this.host.engine.imessage("status.sub.enable", [ this.host.engine.i18n("option.steamworks"), ]); }, onUnCheck: () => { this.host.engine.imessage("status.sub.disable", [ this.host.engine.i18n("option.steamworks"), ]); }, }), new SettingListItem(this, settings.turnOnMagnetos, this.host.engine.i18n("option.magnetos"), { onCheck: () => { this.host.engine.imessage("status.sub.enable", [ this.host.engine.i18n("option.magnetos"), ]); }, onUnCheck: () => { this.host.engine.imessage("status.sub.disable", [ this.host.engine.i18n("option.magnetos"), ]); }, }), new SettingListItem(this, settings.turnOnReactors, this.host.engine.i18n("option.reactors"), { onCheck: () => { this.host.engine.imessage("status.sub.enable", [ this.host.engine.i18n("option.reactors"), ]); }, onUnCheck: () => { this.host.engine.imessage("status.sub.disable", [ this.host.engine.i18n("option.reactors"), ]); }, }), new BuildingUpgradeSettingsUi(this, settings.upgradeBuildings, locale, settings), ]), ]); } _getBuildOptions(parent, settings, locale, sectionLabel, building) { if (building === "unicornPasture" || isNil(settings.buildings[building])) { return []; } const meta = parent.host.game.bld.getBuildingExt(building).meta; if (!isNil(meta.stages)) { const name = Object.values(settings.buildings).find(item => item.baseBuilding === building) ?.building; return [ BuildSectionTools.getBuildOption(parent, settings.buildings[building], locale, settings, meta.stages[0].label, sectionLabel, { renderLabelTrigger: false }), BuildSectionTools.getBuildOption(parent, settings.buildings[name], locale, settings, meta.stages[1].label, sectionLabel, { renderLabelTrigger: false, upgradeIndicator: true, }), ]; } if (!isNil(meta.label)) { return [ BuildSectionTools.getBuildOption(parent, settings.buildings[building], locale, settings, meta.label, sectionLabel, { renderLabelTrigger: false }), ]; } return []; } } //# sourceMappingURL=BonfireSettingsUi.js.map