Commonize create and save confirm method

pull/397/head
isaisstillalive 4 years ago
parent f1bcec65ad
commit 2722366d68

@ -109,9 +109,7 @@ export class HUDWaypoints extends BaseHUDPart {
// Catch mouse and key events
this.root.camera.downPreHandler.add(this.onMouseDown, this);
this.root.keyMapper
.getBinding(KEYMAPPINGS.navigation.createMarker)
.add(this.requestCreateMarker, this);
this.root.keyMapper.getBinding(KEYMAPPINGS.navigation.createMarker).add(this.requestSaveMarker, this);
/**
* Stores at how much opacity the markers should be rendered on the map.
@ -169,7 +167,7 @@ export class HUDWaypoints extends BaseHUDPart {
if (this.isWaypointDeletable(waypoint)) {
const editButton = makeDiv(element, null, ["editButton"]);
this.trackClicks(editButton, () => this.requestEditMarker(waypoint));
this.trackClicks(editButton, () => this.requestSaveMarker({ waypoint }));
}
if (!waypoint.label) {
@ -220,42 +218,61 @@ export class HUDWaypoints extends BaseHUDPart {
}
/**
* Requests to create a marker at the current camera position. If worldPos is set,
* Requests to save a marker at the current camera position. If worldPos is set,
* uses that position instead.
* @param {Vector=} worldPos Override the world pos, otherwise it is the camera position
* @param {object} param0
* @param {Vector=} param0.worldPos Override the world pos, otherwise it is the camera position
* @param {Waypoint=} param0.waypoint Waypoint to be edited. If omitted, create new
*/
requestCreateMarker(worldPos = null) {
requestSaveMarker({ worldPos = null, waypoint = null }) {
// Construct dialog with input field
const markerNameInput = new FormElementInput({
id: "markerName",
label: null,
placeholder: "",
defaultValue: waypoint ? waypoint.label : "",
validator: val =>
val.length > 0 && (val.length < MAX_LABEL_LENGTH || ShapeDefinition.isValidShortKey(val)),
});
const dialog = new DialogWithForm({
app: this.root.app,
title: T.dialogs.createMarker.title,
title: waypoint ? T.dialogs.createMarker.titleEdit : T.dialogs.createMarker.title,
desc: T.dialogs.createMarker.desc,
formElements: [markerNameInput],
buttons: waypoint ? ["delete:bad", "cancel", "ok:good"] : ["cancel", "ok:good"],
});
this.root.hud.parts.dialogs.internalShowDialog(dialog);
// Compute where to create the marker
const center = worldPos || this.root.camera.center;
dialog.buttonSignals.ok.add(() => {
// Show info that you can have only N markers in the demo,
// actually show this *after* entering the name so you want the
// standalone even more (I'm evil :P)
if (IS_DEMO && this.waypoints.length > 2) {
this.root.hud.parts.dialogs.showFeatureRestrictionInfo("", T.dialogs.markerDemoLimit.desc);
return;
}
// Edit marker
if (waypoint) {
dialog.buttonSignals.ok.add(() => {
// Actually rename the waypoint
this.renameWaypoint(waypoint, markerNameInput.getValue());
});
dialog.buttonSignals.delete.add(() => {
// Actually delete the waypoint
this.deleteWaypoint(waypoint);
});
} else {
// Compute where to create the marker
const center = worldPos || this.root.camera.center;
dialog.buttonSignals.ok.add(() => {
// Show info that you can have only N markers in the demo,
// actually show this *after* entering the name so you want the
// standalone even more (I'm evil :P)
if (IS_DEMO && this.waypoints.length > 2) {
this.root.hud.parts.dialogs.showFeatureRestrictionInfo(
"",
T.dialogs.markerDemoLimit.desc
);
return;
}
// Actually create the waypoint
this.addWaypoint(markerNameInput.getValue(), center);
});
// Actually create the waypoint
this.addWaypoint(markerNameInput.getValue(), center);
});
}
}
/**
@ -284,39 +301,6 @@ export class HUDWaypoints extends BaseHUDPart {
this.rerenderWaypointList();
}
/**
* Requests to edit a marker.
* @param {Waypoint} waypoint
*/
requestEditMarker(waypoint) {
// Construct dialog with input field
const markerNameInput = new FormElementInput({
id: "markerName",
label: null,
placeholder: "",
defaultValue: waypoint.label,
validator: val =>
val.length > 0 && (val.length < MAX_LABEL_LENGTH || ShapeDefinition.isValidShortKey(val)),
});
const dialog = new DialogWithForm({
app: this.root.app,
title: T.dialogs.createMarker.titleEdit,
desc: T.dialogs.createMarker.desc,
formElements: [markerNameInput],
buttons: ["delete:bad", "cancel", "ok:good"],
});
this.root.hud.parts.dialogs.internalShowDialog(dialog);
dialog.buttonSignals.ok.add(() => {
// Actually rename the waypoint
this.renameWaypoint(waypoint, markerNameInput.getValue());
});
dialog.buttonSignals.delete.add(() => {
// Actually delete the waypoint
this.deleteWaypoint(waypoint);
});
}
/**
* Renames a waypoint with the given label
* @param {Waypoint} waypoint
@ -440,7 +424,7 @@ export class HUDWaypoints extends BaseHUDPart {
} else if (button === enumMouseButton.right) {
if (this.isWaypointDeletable(waypoint)) {
this.root.soundProxy.playUiClick();
this.requestEditMarker(waypoint);
this.requestSaveMarker({ waypoint });
} else {
this.root.soundProxy.playUiError();
}
@ -452,7 +436,7 @@ export class HUDWaypoints extends BaseHUDPart {
if (button === enumMouseButton.right) {
if (this.root.camera.getIsMapOverlayActive()) {
const worldPos = this.root.camera.screenToWorld(pos);
this.requestCreateMarker(worldPos);
this.requestSaveMarker({ worldPos });
return STOP_PROPAGATION;
}
}

Loading…
Cancel
Save