Added ability to box select with long press

This commit is contained in:
Pax1601
2024-07-07 19:10:02 +02:00
parent fbc22676c5
commit 1dceb0b421
5 changed files with 52 additions and 24 deletions

View File

@@ -14,19 +14,21 @@ export var BoxSelect = Handler.extend({
this._forceBoxSelect = false; this._forceBoxSelect = false;
map.on("unload", this._destroy, this); map.on("unload", this._destroy, this);
document.addEventListener("mapForceBoxSelect", () => { document.addEventListener("mapForceBoxSelect", (e) => {
this._forceBoxSelect = true; this._forceBoxSelect = true;
const originalEvent = (e as CustomEvent).detail;
this._onMouseDown(originalEvent);
}); });
}, },
addHooks: function () { addHooks: function () {
DomEvent.on(this._container, "mousedown", this._onMouseDown, this); DomEvent.on(this._container, "mousedown", this._onMouseDown, this);
DomEvent.on(this._container, "touchstart", this._onMouseDown, this); DomEvent.on(this._container, "mapForceBoxSelect", this._onMouseDown, this);
}, },
removeHooks: function () { removeHooks: function () {
DomEvent.off(this._container, "mousedown", this._onMouseDown, this); DomEvent.off(this._container, "mousedown", this._onMouseDown, this);
DomEvent.off(this._container, "touchstart", this._onMouseDown, this); DomEvent.off(this._container, "mapForceBoxSelect", this._onMouseDown, this);
}, },
moved: function () { moved: function () {

View File

@@ -95,6 +95,8 @@ export class Map extends L.Map {
#theatre: string = ""; #theatre: string = "";
#waitingForDoubleClick: boolean = false; #waitingForDoubleClick: boolean = false;
#doubleClickTimer: number = 0; #doubleClickTimer: number = 0;
#longPressTimer: number = 0;
#dragging: boolean = false;
/** /**
* *
@@ -138,9 +140,12 @@ export class Map extends L.Map {
this.on("zoom", (e: any) => this.#onZoom(e)); this.on("zoom", (e: any) => this.#onZoom(e));
this.on("zoomend", (e: any) => this.#onZoomEnd(e)); this.on("zoomend", (e: any) => this.#onZoomEnd(e));
this.on("drag", (e: any) => this.centerOnUnit(null)); this.on("drag", (e: any) => this.centerOnUnit(null));
this.on("dragstart", (e: any) => this.#onDragStart());
this.on("dragend", (e: any) => this.#onDragEnd(null));
this.on("contextmenu", (e: any) => this.#onContextMenu(e)); this.on("contextmenu", (e: any) => this.#onContextMenu(e));
this.on("selectionstart", (e: any) => this.#onSelectionStart(e)); this.on("selectionstart", (e: any) => this.#onSelectionStart(e));
this.on("selectionend", (e: any) => this.#onSelectionEnd(e)); this.on("selectionend", (e: any) => this.#onSelectionEnd(e));
this.on("mouseup", (e: any) => this.#onMouseUp(e));
this.on("mousedown", (e: any) => this.#onMouseDown(e)); this.on("mousedown", (e: any) => this.#onMouseDown(e));
this.on("mousemove", (e: any) => this.#onMouseMove(e)); this.on("mousemove", (e: any) => this.#onMouseMove(e));
this.on("keydown", (e: any) => this.#onKeyDown(e)); this.on("keydown", (e: any) => this.#onKeyDown(e));
@@ -149,6 +154,9 @@ export class Map extends L.Map {
if (this.#slaveDCSCamera) this.#broadcastPosition(); if (this.#slaveDCSCamera) this.#broadcastPosition();
}); });
L.DomEvent.on(this.getContainer(), "touchstart", this.#onMouseDown, this);
L.DomEvent.on(this.getContainer(), "touchend", this.#onMouseUp, this);
/* Event listeners */ /* Event listeners */
document.addEventListener("hiddenTypesChanged", (ev: CustomEventInit) => { document.addEventListener("hiddenTypesChanged", (ev: CustomEventInit) => {
Object.values(getApp().getUnitsManager().getUnits()).forEach( Object.values(getApp().getUnitsManager().getUnits()).forEach(
@@ -735,7 +743,8 @@ export class Map extends L.Map {
/* Still waiting so no doubleclick; do the click action */ /* Still waiting so no doubleclick; do the click action */
if (this.#waitingForDoubleClick) { if (this.#waitingForDoubleClick) {
if (!this.#preventLeftClick) { if (!this.#preventLeftClick) {
this.hideAllContextMenus();
/* Execute the short click action */
if (this.#state === IDLE) { if (this.#state === IDLE) {
this.deselectAllCoalitionAreas(); this.deselectAllCoalitionAreas();
} else if (this.#state === SPAWN_UNIT) { } else if (this.#state === SPAWN_UNIT) {
@@ -791,6 +800,14 @@ export class Map extends L.Map {
#onContextMenu(e: any) {} #onContextMenu(e: any) {}
#onDragStart(e: any) {
this.#dragging = true;
}
#onDragEnd(e: any) {
this.#dragging = false;
}
#onSelectionStart(e: any) { #onSelectionStart(e: any) {
this.#selecting = true; this.#selecting = true;
} }
@@ -806,7 +823,19 @@ export class Map extends L.Map {
document.dispatchEvent(new CustomEvent("mapSelectionEnd")); document.dispatchEvent(new CustomEvent("mapSelectionEnd"));
} }
#onMouseDown(e: any) {} #onMouseUp(e: any) {
window.clearTimeout(this.#longPressTimer);
}
#onMouseDown(e: any) {
this.#longPressTimer = window.setTimeout(()=> {
if (!this.#dragging)
if (e.type === "touchstart")
document.dispatchEvent(new CustomEvent("mapForceBoxSelect", {detail: e}));
else
document.dispatchEvent(new CustomEvent("mapForceBoxSelect", {detail: e.originalEvent}));
}, 500);
}
#onMouseMove(e: any) { #onMouseMove(e: any) {
this.#lastMousePosition.x = e.originalEvent.x; this.#lastMousePosition.x = e.originalEvent.x;

View File

@@ -7,9 +7,7 @@ import {
faEllipsisV, faEllipsisV,
faCog, faCog,
faQuestionCircle, faQuestionCircle,
faPlusSquare, faPlusSquare
faBox,
faObjectGroup,
} from "@fortawesome/free-solid-svg-icons"; } from "@fortawesome/free-solid-svg-icons";
import { EventsConsumer } from "../../eventscontext"; import { EventsConsumer } from "../../eventscontext";
import { StateConsumer } from "../../statecontext"; import { StateConsumer } from "../../statecontext";
@@ -73,16 +71,6 @@ export function SideBar() {
icon={faPencil} icon={faPencil}
tooltip="Hide/show drawing menu" tooltip="Hide/show drawing menu"
></OlStateButton> ></OlStateButton>
<OlStateButton
onClick={() => {
document.dispatchEvent(
new CustomEvent("mapForceBoxSelect")
);
}}
checked={appState.mapBoxSelection}
icon={faObjectGroup}
tooltip="Enable box selection on the map"
></OlStateButton>
</div> </div>
</div> </div>
<div className="flex w-16 flex-wrap content-end justify-center p-4"> <div className="flex w-16 flex-wrap content-end justify-center p-4">

View File

@@ -117,6 +117,9 @@ export function UnitMouseControlBar(props: {}) {
}); });
} else { } else {
setActiveContextAction(null); setActiveContextAction(null);
getApp().getMap().setState(CONTEXT_ACTION, {
contextAction: null,
});
} }
} }
}} }}

View File

@@ -1218,10 +1218,9 @@ export abstract class Unit extends CustomMarker {
} }
updatePathFromMarkers() { updatePathFromMarkers() {
var path: any = {}; var path: any = [];
this.#pathMarkers.forEach((marker) => { this.#pathMarkers.forEach((marker) => {
path[Object.keys(path).length.toString()] = marker.getLatLng(); path[Object.keys(path).length.toString()] = marker.getLatLng();
console.log(marker.getLatLng());
}); });
getApp().getServerManager().addDestination(this.ID, path); getApp().getServerManager().addDestination(this.ID, path);
} }
@@ -1818,8 +1817,12 @@ export abstract class Unit extends CustomMarker {
icon: pathIcon, icon: pathIcon,
draggable: true, draggable: true,
}).addTo(getApp().getMap()); }).addTo(getApp().getMap());
marker.on("dragstart", (event) => {
event.target.options["freeze"] = true;
})
marker.on("dragend", (event) => { marker.on("dragend", (event) => {
this.updatePathFromMarkers(); this.updatePathFromMarkers();
event.target.options["freeze"] = false;
}); });
this.#pathMarkers.push(marker); this.#pathMarkers.push(marker);
} }
@@ -1835,10 +1838,13 @@ export abstract class Unit extends CustomMarker {
/* Update the position of the existing markers (to avoid creating markers uselessly) */ /* Update the position of the existing markers (to avoid creating markers uselessly) */
for (let WP in this.#activePath) { for (let WP in this.#activePath) {
var destination = this.#activePath[WP]; var destination = this.#activePath[WP];
this.#pathMarkers[parseInt(WP)].setLatLng([ var frozen = this.#pathMarkers[parseInt(WP)].options["freeze"];
destination.lat, if (!this.#pathMarkers[parseInt(WP)].options["freeze"]) {
destination.lng, this.#pathMarkers[parseInt(WP)].setLatLng([
]); destination.lat,
destination.lng,
]);
}
points.push(new LatLng(destination.lat, destination.lng)); points.push(new LatLng(destination.lat, destination.lng));
this.#pathPolyline.setLatLngs(points); this.#pathPolyline.setLatLngs(points);
} }