Skip to content

Commit eb557c5

Browse files
feat(core): Retain parent reference in component
1 parent ded1d60 commit eb557c5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+632
-620
lines changed

source/ui/BonfireSettingsUi.ts

Lines changed: 89 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { coalesceArray, isNil } from "@oliversalzburg/js-utils/data/nil.js";
22
import type { SupportedLocale } from "../Engine.js";
3-
import type { KittenScientists } from "../KittenScientists.js";
43
import { BonfireSettings } from "../settings/BonfireSettings.js";
54
import type { SettingOptions } from "../settings/Settings.js";
65
import type { Building, StagedBuilding } from "../types/index.js";
@@ -13,26 +12,27 @@ import { SettingListItem, type SettingListItemOptions } from "./components/Setti
1312
import { SettingTriggerListItem } from "./components/SettingTriggerListItem.js";
1413
import { SettingsList } from "./components/SettingsList.js";
1514
import { SettingsPanel, type SettingsPanelOptions } from "./components/SettingsPanel.js";
15+
import type { UiComponent } from "./components/UiComponent.js";
1616

1717
export class BonfireSettingsUi extends SettingsPanel<BonfireSettings, SettingTriggerListItem> {
1818
constructor(
19-
host: KittenScientists,
19+
parent: UiComponent,
2020
settings: BonfireSettings,
2121
locale: SettingOptions<SupportedLocale>,
2222
options?: SettingsPanelOptions<SettingTriggerListItem> & SettingListItemOptions,
2323
) {
24-
const label = host.engine.i18n("ui.build");
24+
const label = parent.host.engine.i18n("ui.build");
2525
super(
26-
host,
26+
parent,
2727
settings,
28-
new SettingTriggerListItem(host, settings, locale, label, {
28+
new SettingTriggerListItem(parent, settings, locale, label, {
2929
onCheck: (isBatchProcess?: boolean) => {
30-
host.engine.imessage("status.auto.enable", [label]);
30+
parent.host.engine.imessage("status.auto.enable", [label]);
3131
this.refreshUi();
3232
options?.onCheck?.(isBatchProcess);
3333
},
3434
onUnCheck: (isBatchProcess?: boolean) => {
35-
host.engine.imessage("status.auto.disable", [label]);
35+
parent.host.engine.imessage("status.auto.disable", [label]);
3636
this.refreshUi();
3737
options?.onUnCheck?.(isBatchProcess);
3838
},
@@ -57,24 +57,24 @@ export class BonfireSettingsUi extends SettingsPanel<BonfireSettings, SettingTri
5757
},
5858
onRefreshTrigger: () => {
5959
const element = this.settingItem;
60-
element.triggerButton.element[0].title = host.engine.i18n("ui.trigger.section", [
60+
element.triggerButton.element[0].title = parent.host.engine.i18n("ui.trigger.section", [
6161
settings.trigger < 0
62-
? host.engine.i18n("ui.trigger.section.inactive")
63-
: host.renderPercentage(settings.trigger, locale.selected, true),
62+
? parent.host.engine.i18n("ui.trigger.section.inactive")
63+
: parent.host.renderPercentage(settings.trigger, locale.selected, true),
6464
]);
6565
},
6666
onSetTrigger: async () => {
6767
const value = await Dialog.prompt(
68-
host,
69-
host.engine.i18n("ui.trigger.prompt.percentage"),
70-
host.engine.i18n("ui.trigger.section.prompt", [
68+
parent,
69+
parent.host.engine.i18n("ui.trigger.prompt.percentage"),
70+
parent.host.engine.i18n("ui.trigger.section.prompt", [
7171
label,
7272
settings.trigger !== -1
73-
? host.renderPercentage(settings.trigger, locale.selected, true)
74-
: host.engine.i18n("ui.infinity"),
73+
? parent.host.renderPercentage(settings.trigger, locale.selected, true)
74+
: parent.host.engine.i18n("ui.infinity"),
7575
]),
76-
settings.trigger !== -1 ? host.renderPercentage(settings.trigger) : "",
77-
host.engine.i18n("ui.trigger.section.promptExplainer"),
76+
settings.trigger !== -1 ? parent.host.renderPercentage(settings.trigger) : "",
77+
parent.host.engine.i18n("ui.trigger.section.promptExplainer"),
7878
);
7979

8080
if (value === undefined) {
@@ -86,7 +86,7 @@ export class BonfireSettingsUi extends SettingsPanel<BonfireSettings, SettingTri
8686
return;
8787
}
8888

89-
settings.trigger = host.parsePercentage(value);
89+
settings.trigger = parent.host.parsePercentage(value);
9090
},
9191
}),
9292
);
@@ -95,15 +95,16 @@ export class BonfireSettingsUi extends SettingsPanel<BonfireSettings, SettingTri
9595
// We want the ability to use `this` in our callbacks, to construct more complex
9696
// usage scenarios where we need access to the entire UI section.
9797
this.addChildren([
98-
new SettingsList(host, {
98+
new SettingsList(parent, {
9999
children: coalesceArray(
100-
host.game.bld.buildingGroups.flatMap(buildingGroup => [
101-
new HeaderListItem(host, buildingGroup.title),
100+
parent.host.game.bld.buildingGroups.flatMap(buildingGroup => [
101+
new HeaderListItem(parent, buildingGroup.title),
102102
...buildingGroup.buildings.flatMap(building =>
103-
this._getBuildOptions(host, settings, locale, label, building),
103+
this._getBuildOptions(parent, settings, locale, label, building),
104104
),
105-
buildingGroup !== host.game.bld.buildingGroups[host.game.bld.buildingGroups.length - 1]
106-
? new Delimiter(host)
105+
buildingGroup !==
106+
parent.host.game.bld.buildingGroups[parent.host.game.bld.buildingGroups.length - 1]
107+
? new Delimiter(parent)
107108
: undefined,
108109
]),
109110
),
@@ -112,47 +113,78 @@ export class BonfireSettingsUi extends SettingsPanel<BonfireSettings, SettingTri
112113
this.refreshUi();
113114
},
114115
}),
115-
new SettingsList(host, {
116+
new SettingsList(parent, {
116117
children: [
117-
new HeaderListItem(host, host.engine.i18n("ui.additional")),
118-
new SettingListItem(host, settings.gatherCatnip, host.engine.i18n("option.catnip"), {
119-
onCheck: () => {
120-
host.engine.imessage("status.sub.enable", [host.engine.i18n("option.catnip")]);
121-
},
122-
onUnCheck: () => {
123-
host.engine.imessage("status.sub.disable", [host.engine.i18n("option.catnip")]);
118+
new HeaderListItem(parent, parent.host.engine.i18n("ui.additional")),
119+
new SettingListItem(
120+
parent,
121+
settings.gatherCatnip,
122+
parent.host.engine.i18n("option.catnip"),
123+
{
124+
onCheck: () => {
125+
parent.host.engine.imessage("status.sub.enable", [
126+
parent.host.engine.i18n("option.catnip"),
127+
]);
128+
},
129+
onUnCheck: () => {
130+
parent.host.engine.imessage("status.sub.disable", [
131+
parent.host.engine.i18n("option.catnip"),
132+
]);
133+
},
124134
},
125-
}),
135+
),
126136
new SettingListItem(
127-
host,
137+
parent,
128138
settings.turnOnSteamworks,
129-
host.engine.i18n("option.steamworks"),
139+
parent.host.engine.i18n("option.steamworks"),
130140
{
131141
onCheck: () => {
132-
host.engine.imessage("status.sub.enable", [host.engine.i18n("option.steamworks")]);
142+
parent.host.engine.imessage("status.sub.enable", [
143+
parent.host.engine.i18n("option.steamworks"),
144+
]);
133145
},
134146
onUnCheck: () => {
135-
host.engine.imessage("status.sub.disable", [host.engine.i18n("option.steamworks")]);
147+
parent.host.engine.imessage("status.sub.disable", [
148+
parent.host.engine.i18n("option.steamworks"),
149+
]);
136150
},
137151
},
138152
),
139-
new SettingListItem(host, settings.turnOnMagnetos, host.engine.i18n("option.magnetos"), {
140-
onCheck: () => {
141-
host.engine.imessage("status.sub.enable", [host.engine.i18n("option.magnetos")]);
142-
},
143-
onUnCheck: () => {
144-
host.engine.imessage("status.sub.disable", [host.engine.i18n("option.magnetos")]);
145-
},
146-
}),
147-
new SettingListItem(host, settings.turnOnReactors, host.engine.i18n("option.reactors"), {
148-
onCheck: () => {
149-
host.engine.imessage("status.sub.enable", [host.engine.i18n("option.reactors")]);
153+
new SettingListItem(
154+
parent,
155+
settings.turnOnMagnetos,
156+
parent.host.engine.i18n("option.magnetos"),
157+
{
158+
onCheck: () => {
159+
parent.host.engine.imessage("status.sub.enable", [
160+
parent.host.engine.i18n("option.magnetos"),
161+
]);
162+
},
163+
onUnCheck: () => {
164+
parent.host.engine.imessage("status.sub.disable", [
165+
parent.host.engine.i18n("option.magnetos"),
166+
]);
167+
},
150168
},
151-
onUnCheck: () => {
152-
host.engine.imessage("status.sub.disable", [host.engine.i18n("option.reactors")]);
169+
),
170+
new SettingListItem(
171+
parent,
172+
settings.turnOnReactors,
173+
parent.host.engine.i18n("option.reactors"),
174+
{
175+
onCheck: () => {
176+
parent.host.engine.imessage("status.sub.enable", [
177+
parent.host.engine.i18n("option.reactors"),
178+
]);
179+
},
180+
onUnCheck: () => {
181+
parent.host.engine.imessage("status.sub.disable", [
182+
parent.host.engine.i18n("option.reactors"),
183+
]);
184+
},
153185
},
154-
}),
155-
new BuildingUpgradeSettingsUi(host, settings.upgradeBuildings, locale, settings),
186+
),
187+
new BuildingUpgradeSettingsUi(parent, settings.upgradeBuildings, locale, settings),
156188
],
157189
hasDisableAll: false,
158190
hasEnableAll: false,
@@ -161,7 +193,7 @@ export class BonfireSettingsUi extends SettingsPanel<BonfireSettings, SettingTri
161193
}
162194

163195
private _getBuildOptions(
164-
host: KittenScientists,
196+
parent: UiComponent,
165197
settings: BonfireSettings,
166198
locale: SettingOptions<SupportedLocale>,
167199
sectionLabel: string,
@@ -171,13 +203,13 @@ export class BonfireSettingsUi extends SettingsPanel<BonfireSettings, SettingTri
171203
return [];
172204
}
173205

174-
const meta = host.game.bld.getBuildingExt(building).meta;
206+
const meta = parent.host.game.bld.getBuildingExt(building).meta;
175207
if (!isNil(meta.stages)) {
176208
const name = Object.values(settings.buildings).find(item => item.baseBuilding === building)
177209
?.building as StagedBuilding;
178210
return [
179211
BuildSectionTools.getBuildOption(
180-
host,
212+
parent,
181213
settings.buildings[building],
182214
locale,
183215
settings,
@@ -193,7 +225,7 @@ export class BonfireSettingsUi extends SettingsPanel<BonfireSettings, SettingTri
193225
},
194226
),
195227
BuildSectionTools.getBuildOption(
196-
host,
228+
parent,
197229
settings.buildings[name],
198230
locale,
199231
settings,
@@ -214,7 +246,7 @@ export class BonfireSettingsUi extends SettingsPanel<BonfireSettings, SettingTri
214246
if (!isNil(meta.label)) {
215247
return [
216248
BuildSectionTools.getBuildOption(
217-
host,
249+
parent,
218250
settings.buildings[building],
219251
locale,
220252
settings,

source/ui/BuildSectionTools.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import type { SupportedLocale } from "../Engine.js";
2-
import type { KittenScientists } from "../KittenScientists.js";
32
import type { SettingOptions, SettingTrigger, SettingTriggerMax } from "../settings/Settings.js";
43
import { Dialog } from "./components/Dialog.js";
54
import {
65
SettingMaxTriggerListItem,
76
type SettingMaxTriggerListItemOptions,
87
} from "./components/SettingMaxTriggerListItem.js";
8+
import type { UiComponent } from "./components/UiComponent.js";
99

1010
export const BuildSectionTools = {
1111
getBuildOption: (
12-
host: KittenScientists,
12+
parent: UiComponent,
1313
option: SettingTriggerMax,
1414
locale: SettingOptions<SupportedLocale>,
1515
sectionSetting: SettingTrigger,

source/ui/BuildingUpgradeSettingsUi.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { SupportedLocale } from "../Engine.js";
2-
import type { KittenScientists } from "../KittenScientists.js";
32
import type { BonfireSettings } from "../settings/BonfireSettings.js";
43
import type { BuildingUpgradeSettings } from "../settings/BuildingUpgradeSettings.js";
54
import type { SettingOptions } from "../settings/Settings.js";
@@ -8,18 +7,19 @@ import stylesLabelListItem from "./components/LabelListItem.module.css";
87
import { SettingListItem, type SettingListItemOptions } from "./components/SettingListItem.js";
98
import { SettingsList } from "./components/SettingsList.js";
109
import { SettingsPanel, type SettingsPanelOptions } from "./components/SettingsPanel.js";
10+
import type { UiComponent } from "./components/UiComponent.js";
1111

1212
export class BuildingUpgradeSettingsUi extends SettingsPanel<BuildingUpgradeSettings> {
1313
constructor(
14-
host: KittenScientists,
14+
parent: UiComponent,
1515
settings: BuildingUpgradeSettings,
1616
locale: SettingOptions<SupportedLocale>,
1717
sectionSetting: BonfireSettings,
1818
options?: SettingsPanelOptions<SettingListItem> & SettingListItemOptions,
1919
) {
2020
const label = host.engine.i18n("ui.upgrade.buildings");
2121
super(
22-
host,
22+
parent,
2323
settings,
2424
new SettingListItem(host, settings, label, {
2525
childrenHead: [new Container(host, { classes: [stylesLabelListItem.fillSpace] })],

source/ui/EmbassySettingsUi.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { isNil } from "@oliversalzburg/js-utils/data/nil.js";
22
import type { SupportedLocale } from "../Engine.js";
3-
import type { KittenScientists } from "../KittenScientists.js";
43
import type { EmbassySettings } from "../settings/EmbassySettings.js";
54
import type { SettingMax, SettingOptions } from "../settings/Settings.js";
65
import type { TradeSettings } from "../settings/TradeSettings.js";
@@ -11,18 +10,19 @@ import { SettingMaxListItem } from "./components/SettingMaxListItem.js";
1110
import { SettingTriggerListItem } from "./components/SettingTriggerListItem.js";
1211
import { SettingsList } from "./components/SettingsList.js";
1312
import { SettingsPanel, type SettingsPanelOptions } from "./components/SettingsPanel.js";
13+
import type { UiComponent } from "./components/UiComponent.js";
1414

1515
export class EmbassySettingsUi extends SettingsPanel<EmbassySettings, SettingTriggerListItem> {
1616
constructor(
17-
host: KittenScientists,
17+
parent: UiComponent,
1818
settings: EmbassySettings,
1919
locale: SettingOptions<SupportedLocale>,
2020
sectionSetting: TradeSettings,
2121
options?: SettingsPanelOptions<SettingTriggerListItem> & SettingListItemOptions,
2222
) {
23-
const label = host.engine.i18n("option.embassies");
23+
const label = parent.host.engine.i18n("option.embassies");
2424
super(
25-
host,
25+
parent,
2626
settings,
2727
new SettingTriggerListItem(host, settings, locale, label, {
2828
onCheck: (isBatchProcess?: boolean) => {
@@ -81,7 +81,7 @@ export class EmbassySettingsUi extends SettingsPanel<EmbassySettings, SettingTri
8181
}
8282

8383
private _makeEmbassySetting(
84-
host: KittenScientists,
84+
parent: UiComponent,
8585
option: SettingMax,
8686
locale: SupportedLocale,
8787
sectionSetting: EmbassySettings,

source/ui/EngineSettingsUi.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
import type { KittenScientists } from "../KittenScientists.js";
21
import type { EngineSettings } from "../settings/EngineSettings.js";
32
import { ucfirst } from "../tools/Format.js";
43
import { Container } from "./components/Container.js";
54
import stylesLabelListItem from "./components/LabelListItem.module.css";
65
import { SettingListItem } from "./components/SettingListItem.js";
6+
import type { UiComponent } from "./components/UiComponent.js";
77
import { ExpandoButton } from "./components/buttons/ExpandoButton.js";
88

99
export class EngineSettingsUi extends SettingListItem {
1010
readonly expando: ExpandoButton;
1111

12-
constructor(host: KittenScientists, settings: EngineSettings) {
13-
const label = ucfirst(host.engine.i18n("ui.engine"));
14-
super(host, settings, label, {
15-
childrenHead: [new Container(host, { classes: [stylesLabelListItem.fillSpace] })],
12+
constructor(parent: UiComponent, settings: EngineSettings) {
13+
const label = ucfirst(parent.host.engine.i18n("ui.engine"));
14+
super(parent, settings, label, {
15+
childrenHead: [new Container(parent, { classes: [stylesLabelListItem.fillSpace] })],
1616
onCheck: () => {
17-
host.engine.start(true);
17+
parent.host.engine.start(true);
1818
},
1919
onUnCheck: () => {
20-
host.engine.stop(true);
20+
parent.host.engine.stop(true);
2121
},
2222
});
2323

24-
this.expando = new ExpandoButton(host);
24+
this.expando = new ExpandoButton(parent);
2525
this.head.addChild(this.expando);
2626
}
2727
}

0 commit comments

Comments
 (0)