Skip to content

Commit

Permalink
refactor: add Window class
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmagic2020 committed Jun 21, 2024
1 parent a365a5f commit 6846746
Show file tree
Hide file tree
Showing 3 changed files with 213 additions and 38 deletions.
150 changes: 128 additions & 22 deletions src/ui/generic/windows/graph.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import {
Bindable,
WindowTemplate,
label,
listview,
read,
window
} from "openrct2-flexui"
import { Bindable, WindowParams, label, listview, read } from "openrct2-flexui"
import StatisticalAnalysis from "../../../utils/statistical_analysis"
import { boxPlot } from "../graphs/box_plot"
import { baseData } from "../../../data/main"
import { language } from "../../../languages/lang"
import Window from "./window"

interface GraphWindowParams {
id: string
Expand All @@ -18,19 +12,15 @@ interface GraphWindowParams {
boxChartRange: Bindable<number>
}

class GraphWindow {
private _window: WindowTemplate
private static readonly openWindows: {
class GraphWindow extends Window<WindowParams> {
private static _openWindows: {
[id: string]: GraphWindow
} = {}

private _id: string

constructor(params: GraphWindowParams) {
if (typeof GraphWindow.openWindows[params.id] !== "undefined") {
this._window = GraphWindow.openWindows[params.id]._window
return
}
GraphWindow.openWindows[params.id] = this
this._window = window({
super({
title: params.title,
width: 800,
height: 200,
Expand Down Expand Up @@ -105,16 +95,132 @@ class GraphWindow {
})
],
onClose: () => {
delete GraphWindow.openWindows[params.id]
delete GraphWindow._openWindows[params.id]
}
})
this._id = params.id
if (GraphWindow._openWindows.hasOwnProperty(params.id)) {
return GraphWindow._openWindows[params.id]
}
GraphWindow._openWindows[params.id] = this

baseData.global.colour_scheme.primary.store.subscribe((colour) => {
if (typeof this._windowParams.colours !== "undefined") {
this._windowParams.colours[0] = colour
this.redefine()
}
})
baseData.global.colour_scheme.secondary.store.subscribe((colour) => {
if (typeof this._windowParams.colours !== "undefined") {
this._windowParams.colours[1] = colour
this.redefine()
}
})
}

static show(params: GraphWindowParams): GraphWindow {
const window = new GraphWindow(params)
window._window.open()
return window
override close() {
super.close()
delete GraphWindow._openWindows[this._id]
}
}

// class GraphWindow {
// private _window: WindowTemplate
// private static readonly openWindows: {
// [id: string]: GraphWindow
// } = {}

// constructor(params: GraphWindowParams) {
// if (typeof GraphWindow.openWindows[params.id] !== "undefined") {
// this._window = GraphWindow.openWindows[params.id]._window
// return
// }
// GraphWindow.openWindows[params.id] = this
// this._window = window({
// title: params.title,
// width: 800,
// height: 200,
// colours: [
// baseData.global.colour_scheme.primary.store.get(),
// baseData.global.colour_scheme.secondary.store.get()
// ],
// content: [
// listview({
// height: 26,
// columns: [
// {
// header: language.ui.generic.statistical_analysis.min
// },
// {
// header: language.ui.generic.statistical_analysis.q1
// },
// {
// header: language.ui.generic.statistical_analysis.median
// },
// {
// header: language.ui.generic.statistical_analysis.q3
// },
// {
// header: language.ui.generic.statistical_analysis.max
// },
// {
// header: language.ui.generic.statistical_analysis.mean
// },
// {
// header: language.ui.generic.statistical_analysis.mode
// },
// {
// header: language.ui.generic.statistical_analysis.range
// },
// {
// header: language.ui.generic.statistical_analysis.variance
// },
// {
// header:
// language.ui.generic.statistical_analysis.standard_deviation,
// width: 120
// }
// ],
// items: [
// [
// read(params.statistics).min,
// read(params.statistics).q1,
// read(params.statistics).median,
// read(params.statistics).q3,
// read(params.statistics).max,
// read(params.statistics).mean,
// read(params.statistics).mode,
// read(params.statistics).range,
// read(params.statistics).variance,
// read(params.statistics).standardDeviation
// ].map((item) => item.toString())
// ],
// scrollbars: "none"
// }),
// label({
// text: language.ui.generic.statistical_analysis.box_plot
// }),
// boxPlot({
// q1: read(params.statistics).q1,
// q3: read(params.statistics).q3,
// median: read(params.statistics).median,
// whiskerLow: read(params.statistics).min,
// whiskerHigh: read(params.statistics).max,
// range: params.boxChartRange,
// background: baseData.global.colour_scheme.secondary.store.get()
// })
// ],
// onClose: () => {
// delete GraphWindow.openWindows[params.id]
// }
// })
// }

// static show(params: GraphWindowParams): GraphWindow {
// const window = new GraphWindow(params)
// window._window.open()
// return window
// }
// }

export default GraphWindow
69 changes: 69 additions & 0 deletions src/ui/generic/windows/window.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import {
TabWindowParams,
WindowParams,
WindowTemplate,
tabwindow,
window
} from "openrct2-flexui"

class Window<T extends WindowParams | TabWindowParams> {
protected _windowParams: T
protected _windowTemplate: WindowTemplate
protected _isOpen: boolean

private isTabWindowParams(
params: WindowParams | TabWindowParams
): params is TabWindowParams {
return (params as TabWindowParams).tabs !== undefined
}

constructor(windowParams: T) {
this._windowParams = windowParams
if (this.isTabWindowParams(windowParams)) {
this._windowTemplate = tabwindow(windowParams)
} else {
this._windowTemplate = window(windowParams)
}
this._isOpen = false
}

get windowParams(): T {
return this._windowParams
}

set windowParams(windowParams: T) {
this._windowParams = windowParams
}

open() {
if (!this._isOpen) {
this._windowTemplate.open()
this._isOpen = true
} else {
this._windowTemplate.focus()
}
}

close() {
if (this._isOpen) {
this._windowTemplate.close()
this._isOpen = false
}
}

redefine() {
this.close()
if (this.isTabWindowParams(this._windowParams)) {
this._windowTemplate = tabwindow(this._windowParams)
} else {
this._windowTemplate = window(this._windowParams)
}
this.open()
}

setProperties(properties: Partial<WindowParams>) {
this._windowParams = { ...this._windowParams, ...properties }
}
}

export default Window
32 changes: 16 additions & 16 deletions src/ui/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,15 +291,15 @@ function getWindowParams(): WindowParams {
return guest.mass
})
)
GraphWindow.show({
new GraphWindow({
id: "guest_weight",
title: context.formatString(
language.ui.generic.advanced_statistics.title,
language.ui.main.label.guest_weight_ave
),
statistics: statisticalAnalsysis,
boxChartRange: statisticalAnalsysis.max
})
}).open()
}
})
]
Expand Down Expand Up @@ -333,15 +333,15 @@ function getWindowParams(): WindowParams {
return guest.cash
})
)
GraphWindow.show({
new GraphWindow({
id: "guest_wealth",
title: context.formatString(
language.ui.generic.advanced_statistics.title,
language.ui.main.label.guest_wealth_ave
),
statistics: statisticalAnalsysis,
boxChartRange: statisticalAnalsysis.max
})
}).open()
}
})
]
Expand Down Expand Up @@ -415,15 +415,15 @@ function getWindowParams(): WindowParams {
return guest.happiness
})
)
GraphWindow.show({
new GraphWindow({
id: "guest_happiness",
title: context.formatString(
language.ui.generic.advanced_statistics.title,
language.ui.main.label.guest_happiness_ave
),
statistics: statisticalAnalsysis,
boxChartRange: GuestData.MAX_HAPPINESS
})
}).open()
}
})
]
Expand Down Expand Up @@ -497,15 +497,15 @@ function getWindowParams(): WindowParams {
return guest.energy
})
)
GraphWindow.show({
new GraphWindow({
id: "guest_energy",
title: context.formatString(
language.ui.generic.advanced_statistics.title,
language.ui.main.label.guest_energy_ave
),
statistics: statisticalAnalsysis,
boxChartRange: GuestData.MAX_ENERGY
})
}).open()
}
})
]
Expand Down Expand Up @@ -579,15 +579,15 @@ function getWindowParams(): WindowParams {
return guest.nausea
})
)
GraphWindow.show({
new GraphWindow({
id: "guest_nausea",
title: context.formatString(
language.ui.generic.advanced_statistics.title,
language.ui.main.label.guest_nausea_ave
),
statistics: statisticalAnalsysis,
boxChartRange: GuestData.MAX_NAUSEA
})
}).open()
}
})
]
Expand Down Expand Up @@ -660,15 +660,15 @@ function getWindowParams(): WindowParams {
return guest.hunger
})
)
GraphWindow.show({
new GraphWindow({
id: "guest_hunger",
title: context.formatString(
language.ui.generic.advanced_statistics.title,
language.ui.main.label.guest_hunger_ave
),
statistics: statisticalAnalsysis,
boxChartRange: GuestData.MAX_HUNGER
})
}).open()
}
})
]
Expand Down Expand Up @@ -741,15 +741,15 @@ function getWindowParams(): WindowParams {
return guest.thirst
})
)
GraphWindow.show({
new GraphWindow({
id: "guest_thirst",
title: context.formatString(
language.ui.generic.advanced_statistics.title,
language.ui.main.label.guest_thirst_ave
),
statistics: statisticalAnalsysis,
boxChartRange: GuestData.MAX_THIRST
})
}).open()
}
})
]
Expand Down Expand Up @@ -823,15 +823,15 @@ function getWindowParams(): WindowParams {
return guest.toilet
})
)
GraphWindow.show({
new GraphWindow({
id: "guest_toilet",
title: context.formatString(
language.ui.generic.advanced_statistics.title,
language.ui.main.label.guest_toilet_ave
),
statistics: statisticalAnalsysis,
boxChartRange: GuestData.MAX_TOILET
})
}).open()
}
})
]
Expand Down

0 comments on commit 6846746

Please sign in to comment.