Commonize create and save confirm method

This commit is contained in:
isaisstillalive 2020-07-07 09:14:34 +09:00
parent f1bcec65ad
commit 2722366d68
1 changed files with 40 additions and 56 deletions

View File

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