mirror of
https://github.com/Pax1601/DCSOlympus.git
synced 2025-10-29 16:56:34 +00:00
(WiP) Unit spawn menu
This commit is contained in:
@@ -1,41 +1,63 @@
|
||||
import { LatLng } from "leaflet";
|
||||
import { getActiveCoalition, setActiveCoalition } from "..";
|
||||
import { ContextMenuOption } from "../@types/dom";
|
||||
import { ClickEvent } from "../map/map";
|
||||
import { spawnAircraft } from "../server/server";
|
||||
import { aircraftDatabase } from "../units/aircraftdatabase";
|
||||
import { Dropdown } from "./dropdown";
|
||||
|
||||
export interface SpawnOptions {
|
||||
role: string;
|
||||
type: string;
|
||||
latlng: LatLng;
|
||||
coalition: string;
|
||||
loadout: string | null;
|
||||
airbaseName: string | null;
|
||||
}
|
||||
|
||||
export class ContextMenu {
|
||||
#container: HTMLElement | null;
|
||||
#latlng: LatLng = new LatLng(0, 0);
|
||||
#aircraftRoleDropdown: Dropdown;
|
||||
#aircraftTypeDropdown: Dropdown;
|
||||
#aircraftLoadoutDropdown: Dropdown;
|
||||
//#unitsNumberDropdown: Dropdown;
|
||||
#spawnOptions: SpawnOptions = {role: "", type: "", latlng: this.#latlng, loadout: null, coalition: "blue", airbaseName: null};
|
||||
|
||||
constructor(id: string,) {
|
||||
this.#container = document.getElementById(id);
|
||||
this.#container?.querySelector("#switch")?.addEventListener('change', (e) => this.#onSwitch(e))
|
||||
this.#container?.querySelector("#context-menu-switch")?.addEventListener('change', (e) => this.#onSwitch(e));
|
||||
|
||||
this.#aircraftRoleDropdown = new Dropdown("role-options", (role: string) => this.#setAircraftRole(role), aircraftDatabase.getRoles());
|
||||
this.#aircraftTypeDropdown = new Dropdown("aircraft-options", (type: string) => this.#setAircraftType(type));
|
||||
this.#aircraftLoadoutDropdown = new Dropdown("loadout-options", (loadout: string) => this.#setAircraftLoadout(loadout));
|
||||
//this.#unitsNumberDropdown = new Dropdown("#units-options", this.#setAircraftType, [""]);
|
||||
|
||||
document.addEventListener("contextMenuShow", (e: any) => {
|
||||
this.#container?.querySelector("#aircraft-spawn-menu")?.classList.toggle("hide", e.detail.unitType !== "aircraft");
|
||||
})
|
||||
|
||||
document.addEventListener("contextMenuDeployAircraft", () => {
|
||||
this.hide();
|
||||
this.#spawnOptions.coalition = getActiveCoalition();
|
||||
if (this.#spawnOptions)
|
||||
spawnAircraft(this.#spawnOptions);
|
||||
})
|
||||
|
||||
this.hide();
|
||||
}
|
||||
|
||||
show(x: number, y: number, title: string, options: ContextMenuOption[], showCoalition: boolean) {
|
||||
show(x: number, y: number, latlng: LatLng) {
|
||||
this.#spawnOptions.latlng = latlng;
|
||||
this.#container?.classList.toggle("hide", false);
|
||||
|
||||
this.#container?.querySelector("#list")?.replaceChildren(...options.map((option: ContextMenuOption) => {
|
||||
var li = document.createElement("li");
|
||||
var button = document.createElement("button");
|
||||
button.textContent = option.tooltip;
|
||||
li.appendChild(button);
|
||||
button.addEventListener("click", (e: MouseEvent) => option.callback((e.target as HTMLButtonElement).innerText));
|
||||
return button;
|
||||
}));
|
||||
|
||||
this.#container?.querySelector("#switch")?.classList.toggle("hide", !showCoalition);
|
||||
|
||||
if (this.#container != null && options.length >= 1) {
|
||||
var titleDiv = this.#container.querySelector("#title");
|
||||
if (titleDiv)
|
||||
titleDiv.textContent = title;
|
||||
|
||||
if (x - this.#container.offsetWidth / 2 + this.#container.offsetWidth < window.innerWidth)
|
||||
this.#container.style.left = x - this.#container.offsetWidth / 2 + "px";
|
||||
if (this.#container != null) {
|
||||
if (x + this.#container.offsetWidth < window.innerWidth)
|
||||
this.#container.style.left = x + "px";
|
||||
else
|
||||
this.#container.style.left = window.innerWidth - this.#container.offsetWidth + "px";
|
||||
|
||||
if (y - 20 + this.#container.offsetHeight < window.innerHeight)
|
||||
this.#container.style.top = y - 20 + "px";
|
||||
if (y + this.#container.offsetHeight < window.innerHeight)
|
||||
this.#container.style.top = y + "px";
|
||||
else
|
||||
this.#container.style.top = window.innerHeight - this.#container.offsetHeight + "px";
|
||||
}
|
||||
@@ -47,10 +69,43 @@ export class ContextMenu {
|
||||
|
||||
#onSwitch(e: any) {
|
||||
if (this.#container != null) {
|
||||
if (e.currentTarget.checked)
|
||||
if (e.srcElement.checked)
|
||||
setActiveCoalition("red");
|
||||
else
|
||||
setActiveCoalition("blue");
|
||||
}
|
||||
}
|
||||
|
||||
#setAircraftRole(role: string)
|
||||
{
|
||||
if (this.#spawnOptions != null)
|
||||
{
|
||||
this.#spawnOptions.role = role;
|
||||
this.#aircraftTypeDropdown.setOptions(aircraftDatabase.getLabelsByRole(role));
|
||||
}
|
||||
}
|
||||
|
||||
#setAircraftType(type: string)
|
||||
{
|
||||
if (this.#spawnOptions != null)
|
||||
{
|
||||
this.#spawnOptions.type = type;
|
||||
this.#aircraftLoadoutDropdown.setOptions(aircraftDatabase.getLoadoutNamesByRole(type, this.#spawnOptions.role));
|
||||
}
|
||||
}
|
||||
|
||||
#setAircraftLoadout(loadoutName: string)
|
||||
{
|
||||
if (this.#spawnOptions != null)
|
||||
{
|
||||
var loadout = aircraftDatabase.getLoadoutsByName(this.#spawnOptions.type, loadoutName);
|
||||
if (loadout)
|
||||
this.#spawnOptions.loadout = loadout.code;
|
||||
}
|
||||
}
|
||||
|
||||
#setUnitsNumber(unitsNumber: string)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
32
client/src/controls/dropdown.ts
Normal file
32
client/src/controls/dropdown.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
export class Dropdown {
|
||||
#element: HTMLElement;
|
||||
#options: HTMLElement;
|
||||
#value: HTMLElement;
|
||||
#callback: CallableFunction;
|
||||
|
||||
constructor(ID: string, callback: CallableFunction, options: string[] | null = null)
|
||||
{
|
||||
this.#element = <HTMLElement>document.getElementById(ID);
|
||||
var element = this.#element;
|
||||
this.#options = <HTMLElement>this.#element.querySelector(".ol-select-options");
|
||||
this.#value = <HTMLElement>this.#element.querySelector(".ol-select-value");
|
||||
this.#callback = callback;
|
||||
if (options != null)
|
||||
this.setOptions(options);
|
||||
}
|
||||
|
||||
setOptions(options: string[])
|
||||
{
|
||||
this.#options.replaceChildren(...options.map((option: string) => {
|
||||
var div = document.createElement("div");
|
||||
var button = document.createElement("button");
|
||||
button.textContent = option;
|
||||
div.appendChild(button);
|
||||
button.addEventListener("click", (e: MouseEvent) => {
|
||||
this.#value.innerText = option;
|
||||
this.#callback(option);
|
||||
});
|
||||
return div;
|
||||
}));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user