1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2025-06-13 13:04:03 +00:00

Share querySignalValue

(Also fixing a typo)
This commit is contained in:
Git-Nivrak 2020-10-01 12:23:06 +03:00
parent a438c2fdf9
commit 6a2bdb22ab
2 changed files with 30 additions and 94 deletions

View File

@ -31,77 +31,7 @@ export class HUDEditConstantSignal extends BaseHUDPart {
const signalComp = entity.components.ConstantSignal; const signalComp = entity.components.ConstantSignal;
if (signalComp) { if (signalComp) {
if (button === enumMouseButton.left) { if (button === enumMouseButton.left) {
if (!entity.components.ConstantSignal) { this.root.systemMgr.systems.constantSignal.querySignalValue(entity, true)
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);
return STOP_PROPAGATION; return STOP_PROPAGATION;
} else if (button === enumMouseButton.right) { } else if (button === enumMouseButton.right) {
this.root.logic.tryDeleteBuilding(entity); this.root.logic.tryDeleteBuilding(entity);

View File

@ -18,7 +18,7 @@ export class ConstantSignalSystem extends GameSystemWithFilter {
constructor(root) { constructor(root) {
super(root, [ConstantSignalComponent]); super(root, [ConstantSignalComponent]);
this.root.signals.entityManuallyPlaced.add(this.querySigalValue, this); this.root.signals.entityManuallyPlaced.add(this.querySignalValue, this);
} }
update() { update() {
@ -34,20 +34,23 @@ export class ConstantSignalSystem extends GameSystemWithFilter {
/** /**
* Asks the entity to enter a valid signal code * Asks the entity to enter a valid signal code
* @param {Entity} entity * @param {Entity} entity
* @param {Boolean} isEditing
*/ */
querySigalValue(entity) { querySignalValue(entity, isEditing=false) {
if (!entity.components.ConstantSignal) { if (!entity.components.ConstantSignal) {
return; return;
} }
// Ok, query, but also save the uid because it could get stale // Ok, query, but also save the uid because it could get stale
const uid = entity.uid; const uid = entity.uid;
// Get the ConstantSignal component
const signalComp = entity.components.ConstantSignal;
const signalValueInput = new FormElementInput({ const signalValueInput = new FormElementInput({
id: "signalValue", id: "signalValue",
label: fillInLinkIntoTranslation(T.dialogs.editSignal.descShortKey, THIRDPARTY_URLS.shapeViewer), label: fillInLinkIntoTranslation(T.dialogs.editSignal.descShortKey, THIRDPARTY_URLS.shapeViewer),
placeholder: "", placeholder: "",
defaultValue: "", defaultValue: signalComp.signal ? signalComp.signal.getAsCopyableKey() : "",
validator: val => this.parseSignalCode(val), validator: val => this.parseSignalCode(val),
}); });
@ -103,26 +106,29 @@ export class ConstantSignalSystem extends GameSystemWithFilter {
dialog.valueChosen.add(closeHandler); dialog.valueChosen.add(closeHandler);
// When cancelled, destroy the entity again // When cancelled, destroy the entity again
dialog.buttonSignals.cancel.add(() => { if (!isEditing) {
if (!this.root || !this.root.entityMgr) { dialog.buttonSignals.cancel.add(() => {
// Game got stopped if (!this.root || !this.root.entityMgr) {
return; // Game got stopped
} return;
}
const entityRef = this.root.entityMgr.findByUid(uid, false);
if (!entityRef) { const entityRef = this.root.entityMgr.findByUid(uid, false);
// outdated if (!entityRef) {
return; // outdated
} return;
}
const constantComp = entityRef.components.ConstantSignal;
if (!constantComp) { const constantComp = entityRef.components.ConstantSignal;
// no longer interesting if (!constantComp) {
return; // no longer interesting
} return;
}
this.root.logic.tryDeleteBuilding(entityRef);
}); this.root.logic.tryDeleteBuilding(entityRef);
});
}
} }
/** /**