diff --git a/packages/editor/src/UI/editors/TrainStopEditor.ts b/packages/editor/src/UI/editors/TrainStopEditor.ts new file mode 100644 index 00000000..fbaa780e --- /dev/null +++ b/packages/editor/src/UI/editors/TrainStopEditor.ts @@ -0,0 +1,61 @@ +import { Entity } from '../../core/Entity' +import { Checkbox } from '../controls/Checkbox' +import { Textbox } from '../controls/Textbox' +import { Editor } from './Editor' + +/** Train Stop Editor */ +export class TrainStopEditor extends Editor { + public constructor(entity: Entity) { + super(402, 171, entity) + + this.addLabel(140, 46, 'Station Name:') + // The length is arbitrary, but the Textbox doesn't work right without it + const stationTextBox: Textbox = new Textbox(250, entity.station, 100) + stationTextBox.position.set(140, 65) + this.addChild(stationTextBox) + + const isLimitDefined: boolean = this.m_Entity.manualTrainsLimit !== undefined + const limitCheckBox: Checkbox = new Checkbox(isLimitDefined, 'Enable train limit') + limitCheckBox.position.set(140, 97) + this.addChild(limitCheckBox) + + const trainsLimitString: string = this.m_Entity.manualTrainsLimit?.toString() + const limitTextbox: Textbox = new Textbox(30, trainsLimitString, 3, '0123456789') + limitTextbox.position.set(275, 95) + this.addChild(limitTextbox) + + stationTextBox.on('changed', () => { + this.m_Entity.station = stationTextBox.text + }) + + limitCheckBox.on('changed', () => { + if (limitCheckBox.checked) { + this.m_Entity.manualTrainsLimit = 0 + limitTextbox.text = '0' + } else { + this.m_Entity.manualTrainsLimit = undefined + limitTextbox.text = '' + } + }) + + limitTextbox.on('changed', () => { + let limit: number = parseInt(limitTextbox.text) + if (isNaN(limit)) { + limit = undefined + } + + this.m_Entity.manualTrainsLimit = limit + limitCheckBox.checked = limit !== undefined && limit >= 0 + }) + + this.onEntityChange('station', () => { + stationTextBox.text = this.m_Entity.station + }) + + this.onEntityChange('manualTrainsLimit', () => { + const limit = this.m_Entity.manualTrainsLimit + limitTextbox.text = limit === undefined ? '' : `${limit}` + limitCheckBox.checked = limit !== undefined && limit >= 0 + }) + } +} diff --git a/packages/editor/src/UI/editors/factory.ts b/packages/editor/src/UI/editors/factory.ts index e31d1e2e..34f75b02 100644 --- a/packages/editor/src/UI/editors/factory.ts +++ b/packages/editor/src/UI/editors/factory.ts @@ -7,6 +7,7 @@ import { MiningEditor } from './MiningEditor' import { SplitterEditor } from './SplitterEditor' import { ChestEditor } from './ChestEditor' import { TempEditor } from './TempEditor' +import { TrainStopEditor } from './TrainStopEditor' /** * Factory Function for creating Editor based on Entity Number @@ -72,6 +73,10 @@ export function createEditor(entity: Entity): Editor { case 'rocket_silo': editor = new TempEditor(entity) break + // Train stop + case 'train_stop': + editor = new TrainStopEditor(entity) + break default: { return undefined } diff --git a/packages/editor/src/core/Entity.ts b/packages/editor/src/core/Entity.ts index 97ec3780..ec9be887 100644 --- a/packages/editor/src/core/Entity.ts +++ b/packages/editor/src/core/Entity.ts @@ -497,6 +497,34 @@ export class Entity extends EventEmitter { return this.m_rawEntity.color } + /** Entity Train Stop Station name */ + public get station(): string { + return this.m_rawEntity.station + } + + public set station(station: string) { + if (this.m_rawEntity.station === station) return + + this.m_BP.history + .updateValue(this.m_rawEntity, ['station'], station, 'Change station name') + .onDone(() => this.emit('station')) + .commit() + } + + /** Entity Train Stop Trains Limit */ + public get manualTrainsLimit(): number | undefined { + return this.m_rawEntity.manual_trains_limit + } + + public set manualTrainsLimit(limit: number | undefined) { + if (this.m_rawEntity.manual_trains_limit === limit) return + + this.m_BP.history + .updateValue(this.m_rawEntity, ['manual_trains_limit'], limit, 'Change trains limit') + .onDone(() => this.emit('manualTrainsLimit')) + .commit() + } + public get operator(): string { if (this.name === 'decider_combinator') { const cb = this.m_rawEntity.control_behavior