0ad/binaries/data/mods/public/gui/session/PanelEntityManager.js
Dunedan 93ce94655d
Use @stylistic/brace-style for eslint
Up to now `eslint-plugin-brace-rules` was used to enforce a common brace
style for JavaScript code. This plugin was however updated the last time
over 9 years ago and will be incompatible with ESLint v10, as that
[removes `context.getSourceCode()`][1], the plugin relies on.

To keep the eslint config working with ESLint v10, this replaces
`eslint-plugin-brace-rules` with the [`@stylistic/brace-style`][2] rule
from `@stylistic/eslint-plugin`, a package we already use.

While `@stylistic/brace-style` doesn't offer an option to format braces
in exactly the same way as before, the "allman" style seems to be the
one closest to the existing code.

[1]: https://eslint.org/blog/2025/11/eslint-v10.0.0-alpha.0-released/#removed-deprecated-rule-context-members
[2]: https://eslint.style/rules/brace-style
2026-01-12 21:33:52 +01:00

93 lines
2.4 KiB
JavaScript

/**
* This class creates a new buttonhandler per entity owned by the currently viewed player,
* or any player if the observer view is selected.
* The buttons are animated when the according entities are attacked, hence they have to keep a state.
*/
class PanelEntityManager
{
constructor(playerViewControl, selection, entityOrder)
{
this.panelEntityButtons = Engine.GetGUIObjectByName("panelEntityButtons").children;
this.selection = selection;
this.entityOrder = entityOrder;
// One handler per panelEntity owned, sorted according to given order, limited to buttoncount
this.handlers = [];
const updater = this.update.bind(this);
registerSimulationUpdateHandler(updater);
playerViewControl.registerViewedPlayerChangeHandler(updater);
}
update()
{
// Obtain entity IDs to display
const entityIDs =
g_ViewedPlayer == -1 ?
g_SimState.players.reduce((ents, pState) => ents.concat(pState.panelEntities), []) :
g_SimState.players[g_ViewedPlayer].panelEntities;
let reposition = false;
// Delete handlers for entities not owned anymore
for (let i = 0; i < this.handlers.length;)
if (entityIDs.indexOf(this.handlers[i].entityID) == -1)
{
this.handlers[i].destroy();
this.handlers.splice(i, 1);
reposition = true;
}
else
++i;
// Construct new handlers
for (const entityID of entityIDs)
if (this.handlers.every(handler => entityID != handler.entityID))
if (this.insertIfRelevant(entityID))
reposition = true;
for (let i = 0; i < this.handlers.length; ++i)
this.handlers[i].update(i, reposition);
}
insertIfRelevant(entityID)
{
const entityState = GetEntityState(entityID);
const orderKey = this.entityOrder.findIndex(entClass =>
entityState.identity.classes.indexOf(entClass) != -1);
// Sort depending on given order
const insertPos = this.handlers.reduce(
(pos, handler) =>
{
if (handler.orderKey <= orderKey)
++pos;
return pos;
},
0);
// Don't insert past the button limit
if (insertPos >= this.panelEntityButtons.length)
return false;
// Delete last handler if the limit is reached
if (this.handlers.length == this.panelEntityButtons.length)
{
this.handlers[this.handlers.length - 1].destroy();
this.handlers.splice(this.handlers.length - 1);
}
this.handlers.splice(
insertPos,
0,
new PanelEntity(
this.selection,
entityID,
this.panelEntityButtons.findIndex(button => button.hidden),
orderKey));
return true;
}
}