diff --git a/src/js/game/hud/parts/edit_constant_signal.js b/src/js/game/hud/parts/edit_constant_signal.js index 27658f17..0c56e1a5 100644 --- a/src/js/game/hud/parts/edit_constant_signal.js +++ b/src/js/game/hud/parts/edit_constant_signal.js @@ -31,77 +31,7 @@ export class HUDEditConstantSignal extends BaseHUDPart { const signalComp = entity.components.ConstantSignal; if (signalComp) { if (button === enumMouseButton.left) { - if (!entity.components.ConstantSignal) { - return; - } - - // Ok, query, but also save the uid because it could get stale - const uid = entity.uid; - - const signalValueInput = new FormElementInput({ - id: "signalValue", - label: fillInLinkIntoTranslation( - T.dialogs.editSignal.descShortKey, - THIRDPARTY_URLS.shapeViewer - ), - placeholder: "", - defaultValue: signalComp.signal ? signalComp.signal.getAsCopyableKey() : "", - validator: val => this.root.systemMgr.systems.constantSignal.parseSignalCode(val), - }); - - const itemInput = new FormElementItemChooser({ - id: "signalItem", - label: null, - items: [ - BOOL_FALSE_SINGLETON, - BOOL_TRUE_SINGLETON, - ...Object.values(COLOR_ITEM_SINGLETONS), - this.root.shapeDefinitionMgr.getShapeItemFromShortKey(blueprintShape), - ], - }); - - const dialog = new DialogWithForm({ - app: this.root.app, - title: T.dialogs.editSignal.title, - desc: T.dialogs.editSignal.descItems, - formElements: [itemInput, signalValueInput], - buttons: ["cancel:bad:escape", "ok:good:enter"], - closeButton: false, - }); - this.root.hud.parts.dialogs.internalShowDialog(dialog); - - // When confirmed, set the signal - const closeHandler = () => { - if (!this.root || !this.root.entityMgr) { - // Game got stopped - return; - } - - const entityRef = this.root.entityMgr.findByUid(uid, false); - if (!entityRef) { - // outdated - return; - } - - const constantComp = entityRef.components.ConstantSignal; - if (!constantComp) { - // no longer interesting - return; - } - - if (itemInput.chosenItem) { - console.log(itemInput.chosenItem); - constantComp.signal = itemInput.chosenItem; - } else { - constantComp.signal = this.root.systemMgr.systems.constantSignal.parseSignalCode( - signalValueInput.getValue() - ); - } - }; - - dialog.buttonSignals.ok.add(closeHandler); - dialog.valueChosen.add(closeHandler); - + this.root.systemMgr.systems.constantSignal.querySignalValue(entity, true) return STOP_PROPAGATION; } else if (button === enumMouseButton.right) { this.root.logic.tryDeleteBuilding(entity); diff --git a/src/js/game/systems/constant_signal.js b/src/js/game/systems/constant_signal.js index 3af98460..4975a784 100644 --- a/src/js/game/systems/constant_signal.js +++ b/src/js/game/systems/constant_signal.js @@ -18,7 +18,7 @@ export class ConstantSignalSystem extends GameSystemWithFilter { constructor(root) { super(root, [ConstantSignalComponent]); - this.root.signals.entityManuallyPlaced.add(this.querySigalValue, this); + this.root.signals.entityManuallyPlaced.add(this.querySignalValue, this); } update() { @@ -34,20 +34,23 @@ export class ConstantSignalSystem extends GameSystemWithFilter { /** * Asks the entity to enter a valid signal code * @param {Entity} entity + * @param {Boolean} isEditing */ - querySigalValue(entity) { + querySignalValue(entity, isEditing=false) { if (!entity.components.ConstantSignal) { return; } // Ok, query, but also save the uid because it could get stale const uid = entity.uid; + // Get the ConstantSignal component + const signalComp = entity.components.ConstantSignal; const signalValueInput = new FormElementInput({ id: "signalValue", label: fillInLinkIntoTranslation(T.dialogs.editSignal.descShortKey, THIRDPARTY_URLS.shapeViewer), placeholder: "", - defaultValue: "", + defaultValue: signalComp.signal ? signalComp.signal.getAsCopyableKey() : "", validator: val => this.parseSignalCode(val), }); @@ -103,26 +106,29 @@ export class ConstantSignalSystem extends GameSystemWithFilter { dialog.valueChosen.add(closeHandler); // When cancelled, destroy the entity again - dialog.buttonSignals.cancel.add(() => { - if (!this.root || !this.root.entityMgr) { - // Game got stopped - return; - } - - const entityRef = this.root.entityMgr.findByUid(uid, false); - if (!entityRef) { - // outdated - return; - } - - const constantComp = entityRef.components.ConstantSignal; - if (!constantComp) { - // no longer interesting - return; - } - - this.root.logic.tryDeleteBuilding(entityRef); - }); + if (!isEditing) { + dialog.buttonSignals.cancel.add(() => { + if (!this.root || !this.root.entityMgr) { + // Game got stopped + return; + } + + const entityRef = this.root.entityMgr.findByUid(uid, false); + if (!entityRef) { + // outdated + return; + } + + const constantComp = entityRef.components.ConstantSignal; + if (!constantComp) { + // no longer interesting + return; + } + + this.root.logic.tryDeleteBuilding(entityRef); + }); + } + } /**