More refactoring of events

This commit is contained in:
Pax1601
2024-10-28 07:53:09 +01:00
parent 14c0a2f1e8
commit 7f5873b5b8
32 changed files with 1010 additions and 891 deletions

View File

@@ -1,3 +1,4 @@
import { UnitDeadEvent } from "../events";
import { Unit } from "./unit";
export class Group {
@@ -7,8 +8,8 @@ export class Group {
constructor(name: string) {
this.#name = name;
document.addEventListener("unitDeath", (e: any) => {
if (this.#members.includes(e.detail)) this.getLeader()?.onGroupChanged(e.detail);
UnitDeadEvent.on((unit) => {
if (this.#members.includes(unit)) this.getLeader()?.onGroupChanged(unit);
});
}

View File

@@ -71,6 +71,7 @@ import {
faXmarksLines,
} from "@fortawesome/free-solid-svg-icons";
import { Carrier } from "../mission/carrier";
import { ContactsUpdatedEvent, HiddenTypesChangedEvent, MapOptionsChangedEvent, UnitDeadEvent, UnitDeselectedEvent, UnitSelectedEvent } from "../events";
var pathIcon = new Icon({
iconUrl: "/vite/images/markers/marker-icon.png",
@@ -356,29 +357,20 @@ export abstract class Unit extends CustomMarker {
this.on("mouseup", (e) => this.#onMouseUp(e));
this.on("dblclick", (e) => this.#onDoubleClick(e));
this.on("mouseover", () => {
if (this.belongsToCommandedCoalition()) {
if (this.belongsToCommandedCoalition())
this.setHighlighted(true);
document.dispatchEvent(new CustomEvent("unitMouseover", { detail: this }));
}
});
this.on("mouseout", () => {
this.setHighlighted(false);
document.dispatchEvent(new CustomEvent("unitMouseout", { detail: this }));
});
getApp()
.getMap()
.on("zoomend", (e: any) => {
this.#onZoom(e);
});
this.on("mouseout", () => this.setHighlighted(false));
getApp().getMap().on("zoomend", (e: any) => this.#onZoom(e));
/* Deselect units if they are hidden */
document.addEventListener("hiddenTypesChanged", (ev: CustomEventInit) => {
HiddenTypesChangedEvent.on((hiddenTypes) => {
this.#updateMarker();
this.setSelected(this.getSelected() && !this.getHidden());
});
/* Update the marker when the options change */
document.addEventListener("mapOptionChanged", (ev: CustomEventInit) => {
MapOptionsChangedEvent.on(() => {
this.#updateMarker();
/* Circles don't like to be updated when the map is zooming */
@@ -559,7 +551,7 @@ export abstract class Unit extends CustomMarker {
break;
case DataIndexes.contacts:
this.#contacts = dataExtractor.extractContacts();
document.dispatchEvent(new CustomEvent("contactsUpdated", { detail: this }));
ContactsUpdatedEvent.dispatch();
break;
case DataIndexes.activePath:
this.#activePath = dataExtractor.extractActivePath();
@@ -599,9 +591,6 @@ export abstract class Unit extends CustomMarker {
this.setSelected(true);
}
}
/* If the unit is selected or if the view is centered on this unit, sent the update signal so that other elements like the UnitControlPanel can be updated. */
if (this.getSelected() || getApp().getMap().getCenteredOnUnit() === this) document.dispatchEvent(new CustomEvent("unitUpdated", { detail: this }));
}
/** Get unit data collated into an object
@@ -672,7 +661,7 @@ export abstract class Unit extends CustomMarker {
* @param newAlive (boolean) true = alive, false = dead
*/
setAlive(newAlive: boolean) {
if (newAlive != this.#alive) document.dispatchEvent(new CustomEvent("unitDeath", { detail: this }));
if (newAlive != this.#alive) UnitDeadEvent.dispatch(this)
this.#alive = newAlive;
}
@@ -709,11 +698,7 @@ export abstract class Unit extends CustomMarker {
this.getElement()?.querySelector(`.unit`)?.toggleAttribute("data-is-selected", selected);
/* Trigger events after all (de-)selecting has been done */
if (selected) {
document.dispatchEvent(new CustomEvent("unitSelection", { detail: this }));
} else {
document.dispatchEvent(new CustomEvent("unitDeselection", { detail: this }));
}
selected? UnitSelectedEvent.dispatch(this): UnitDeselectedEvent.dispatch(this);
}
}

View File

@@ -35,6 +35,14 @@ import { UnitDataFileExport } from "./importexport/unitdatafileexport";
import { UnitDataFileImport } from "./importexport/unitdatafileimport";
import { CoalitionCircle } from "../map/coalitionarea/coalitioncircle";
import { ContextActionSet } from "./contextactionset";
import {
CommandModeOptionsChangedEvent,
ContactsUpdatedEvent,
SelectedUnitsChangedEvent,
SelectionClearedEvent,
UnitDeselectedEvent,
UnitSelectedEvent,
} from "../events";
/** The UnitsManager handles the creation, update, and control of units. Data is strictly updated by the server ONLY. This means that any interaction from the user will always and only
* result in a command to the server, executed by means of a REST PUT request. Any subsequent change in data will be reflected only when the new data is sent back by the server. This strategy allows
@@ -55,17 +63,18 @@ export class UnitsManager {
this.#copiedUnits = [];
this.#units = {};
document.addEventListener("commandModeOptionsChanged", () => {
CommandModeOptionsChangedEvent.on(() => {
Object.values(this.#units).forEach((unit: Unit) => unit.updateVisibility());
});
document.addEventListener("contactsUpdated", (e) => {
ContactsUpdatedEvent.on(() => {
this.#requestDetectionUpdate = true;
});
UnitSelectedEvent.on((unit) => this.#onUnitDeselection(unit));
UnitDeselectedEvent.on((unit) => this.#onUnitSelection(unit));
document.addEventListener("copy", () => this.copy());
document.addEventListener("keyup", (event) => this.#onKeyUp(event));
document.addEventListener("paste", () => this.paste());
document.addEventListener("unitDeselection", (e) => this.#onUnitDeselection((e as CustomEvent).detail));
document.addEventListener("unitSelection", (e) => this.#onUnitSelection((e as CustomEvent).detail));
//this.#slowDeleteDialog = new Dialog("slow-delete-dialog");
}
@@ -1416,18 +1425,6 @@ export class UnitsManager {
if (spawnPoints <= getApp().getMissionManager().getAvailableSpawnPoints()) {
getApp().getMissionManager().setSpentSpawnPoints(spawnPoints);
spawnFunction();
document.dispatchEvent(
new CustomEvent("unitSpawned", {
detail: {
airbase: airbase,
category: category,
coalition: coalition,
country: country,
immediate: immediate,
unitSpawnTable: units,
},
})
);
return true;
} else {
//(getApp().getPopupsManager().get("infoPopup") as Popup).setText("Not enough spawn points available!");
@@ -1453,16 +1450,13 @@ export class UnitsManager {
/* Disable the firing of the selection event for a certain amount of time. This avoids firing many events if many units are selected */
if (!this.#selectionEventDisabled) {
window.setTimeout(() => {
document.dispatchEvent(
new CustomEvent("unitsSelection", {
detail: this.getSelectedUnits(),
})
);
SelectedUnitsChangedEvent.dispatch(this.getSelectedUnits());
let newContextActionSet = new ContextActionSet();
this.getSelectedUnits().forEach((unit) => unit.appendContextActions(newContextActionSet));
getApp().getMap().setContextAction(null);
getApp().getMap().setDefaultContextAction(newContextActionSet.getDefaultContextAction());
getApp().getMap().setContextActionSet(newContextActionSet);
getApp().setState(OlympusState.UNIT_CONTROL);
this.#selectionEventDisabled = false;
@@ -1472,24 +1466,19 @@ export class UnitsManager {
}
} else {
getApp().setState(OlympusState.IDLE);
document.dispatchEvent(new CustomEvent("clearSelection"));
SelectionClearedEvent.dispatch();
}
}
#onUnitDeselection(unit: Unit) {
if (this.getSelectedUnits().length == 0) {
if (getApp().getState() === OlympusState.UNIT_CONTROL)
getApp().setState(OlympusState.IDLE);
document.dispatchEvent(new CustomEvent("clearSelection"));
if (getApp().getState() === OlympusState.UNIT_CONTROL) getApp().setState(OlympusState.IDLE);
SelectionClearedEvent.dispatch();
} else {
/* Disable the firing of the selection event for a certain amount of time. This avoids firing many events if many units are selected */
if (!this.#deselectionEventDisabled) {
window.setTimeout(() => {
document.dispatchEvent(
new CustomEvent("unitsDeselection", {
detail: this.getSelectedUnits(),
})
);
SelectedUnitsChangedEvent.dispatch(this.getSelectedUnits());
this.#deselectionEventDisabled = false;
}, 100);
this.#deselectionEventDisabled = true;