More work on context actions

This commit is contained in:
Davide Passoni
2024-06-27 17:35:10 +02:00
parent 222a296b4f
commit 8cb8f7e108
4 changed files with 33 additions and 16 deletions

View File

@@ -99,7 +99,7 @@ export class Map extends L.Map {
#bradcastPositionXmlHttp: XMLHttpRequest | null = null;
#cameraZoomRatio: number = 1.0;
#contextAction: null | ContextAction = null;
#contextAction: null | ContextAction = null;
/**
*
@@ -324,7 +324,7 @@ export class Map extends L.Map {
}
/* State machine */
setState(state: string, options?: { spawnRequestTable?: SpawnRequestTable, contextAction?: ContextAction }) {
setState(state: string, options?: { spawnRequestTable?: SpawnRequestTable, contextAction?: ContextAction | null }) {
this.#state = state;
/* Operations to perform if you are NOT in a state */
@@ -333,7 +333,9 @@ export class Map extends L.Map {
}
/* Operations to perform if you ARE in a state */
if (this.#state === SPAWN_UNIT) {
if (this.#state === IDLE) {
getApp().getUnitsManager().deselectAllUnits();
} else if (this.#state === SPAWN_UNIT) {
this.#spawnRequestTable = options?.spawnRequestTable ?? null;
this.#spawnCursor?.removeFrom(this);
this.#spawnCursor = new TemporaryUnitMarker(new L.LatLng(0, 0), this.#spawnRequestTable?.unit.unitType ?? "unknown", this.#spawnRequestTable?.coalition ?? 'blue');
@@ -648,6 +650,10 @@ export class Map extends L.Map {
//}
}
executeContextAction(targetUnit: Unit | null, targetPosition: L.LatLng | null) {
this.#contextAction?.executeCallback(targetUnit, targetPosition);
}
/* Event handlers */
#onClick(e: any) {
if (!this.#preventLeftClick) {
@@ -678,6 +684,8 @@ export class Map extends L.Map {
else {
this.deselectAllCoalitionAreas();
}
} else if (this.#state === CONTEXT_ACTION) {
this.executeContextAction(null, e.latlng);
}
else {
this.setState(IDLE);
@@ -687,7 +695,7 @@ export class Map extends L.Map {
}
#onDoubleClick(e: any) {
this.setState(IDLE);
}
#onContextMenu(e: any) {
@@ -729,10 +737,6 @@ export class Map extends L.Map {
this.#computeDestinationRotation = false;
}
}
else if (this.#state === CONTEXT_ACTION) {
if (this.#contextAction)
this.#contextAction.executeCallback(null, e.latlng);
}
else {
this.setState(IDLE);
}

View File

@@ -111,6 +111,8 @@ export class ServerManager {
callback(res);
};
xmlHttp.send(JSON.stringify(request));
console.log(`Sending PUT request:`);
console.log(request);
}
getConfig(callback: CallableFunction) {

View File

@@ -1,13 +1,11 @@
import React, { useEffect, useState } from 'react';
import React, { useState } from 'react';
import { Unit } from '../../unit/unit';
import { ContextActionSet } from '../../unit/contextactionset';
import { OlStateButton } from '../components/olstatebutton';
import { faAccessibleIcon } from '@fortawesome/free-brands-svg-icons';
import { faCamera } from '@fortawesome/free-solid-svg-icons';
import { getApp } from '../../olympusapp';
import { ContextAction } from '../../unit/contextaction';
import { CONTEXT_ACTION } from '../../constants/constants';
import { FaInfoCircle, FaQuestionCircle } from 'react-icons/fa';
import { FaInfoCircle } from 'react-icons/fa';
export function UnitMouseControlBar(props: {
@@ -38,6 +36,12 @@ export function UnitMouseControlBar(props: {
updateData();
})
/* Deselect the context action when exiting state */
document.addEventListener("mapStateChanged", (ev) => {
setOpen(ev.detail === CONTEXT_ACTION);
})
/* Update the current values of the shown data */
function updateData() {
var newContextActionSet = new ContextActionSet();
@@ -52,7 +56,7 @@ export function UnitMouseControlBar(props: {
return <> {
open && <>
<div className='flex gap-2 rounded-md absolute top-20 left-[50%] translate-x-[-50%] bg-gray-200 dark:bg-olympus-900 z-ui-1 p-2'>
<div className='flex gap-2 rounded-md absolute top-20 left-[50%] translate-x-[-50%] bg-gray-200 dark:bg-olympus-900 z-ui-2 p-2'>
{
Object.values(contextActionsSet.getContextActions()).map((contextAction) => {
return <OlStateButton checked={contextAction === activeContextAction} icon={contextAction.getIcon()} tooltip={contextAction.getLabel()} onClick={() => {
@@ -60,8 +64,13 @@ export function UnitMouseControlBar(props: {
setActiveContextAction(null);
contextAction.executeCallback(null, null);
} else {
setActiveContextAction(contextAction);
getApp().getMap().setState(CONTEXT_ACTION, { contextAction: contextAction });
if (activeContextAction != contextAction) {
setActiveContextAction(contextAction);
getApp().getMap().setState(CONTEXT_ACTION, { contextAction: contextAction });
} else {
setActiveContextAction(null);
getApp().getMap().setState(CONTEXT_ACTION, { contextAction: null });
}
}
}} />
})

View File

@@ -5,7 +5,7 @@ import { CustomMarker } from '../map/markers/custommarker';
import { SVGInjector } from '@tanem/svg-injector';
import { UnitDatabase } from './databases/unitdatabase';
import { TargetMarker } from '../map/markers/targetmarker';
import { DLINK, DataIndexes, GAME_MASTER, IDLE, IRST, MOVE_UNIT, OPTIC, RADAR, ROEs, RWR, VISUAL, emissionsCountermeasures, reactionsToThreat, states, GROUPING_ZOOM_TRANSITION, MAX_SHOTS_SCATTER, SHOTS_SCATTER_DEGREES, GROUND_UNIT_AIR_DEFENCE_REGEX } from '../constants/constants';
import { DLINK, DataIndexes, GAME_MASTER, IDLE, IRST, MOVE_UNIT, OPTIC, RADAR, ROEs, RWR, VISUAL, emissionsCountermeasures, reactionsToThreat, states, GROUPING_ZOOM_TRANSITION, MAX_SHOTS_SCATTER, SHOTS_SCATTER_DEGREES, GROUND_UNIT_AIR_DEFENCE_REGEX, CONTEXT_ACTION } from '../constants/constants';
import { DataExtractor } from '../server/dataextractor';
import { groundUnitDatabase } from './databases/groundunitdatabase';
import { navyUnitDatabase } from './databases/navyunitdatabase';
@@ -1065,6 +1065,8 @@ export abstract class Unit extends CustomMarker {
getApp().getUnitsManager().deselectAllUnits();
this.setSelected(!this.getSelected());
} else if (getApp().getMap().getState() === CONTEXT_ACTION) {
getApp().getMap().executeContextAction(this, null);
}
}