0ad/binaries/data/mods/public/gui/session/PanelEntity.js
elexis b859195064 Decouple panel entities code from session code and use class notation, refs #5387, #3000, #1902, #1802, d7d1d8ccb5.
Change the logic to only insert/delete buttonhandlers on ownershipchange
and update only the entitystate dependent part on simulation update.

Differential Revision: https://code.wildfiregames.com/D2387
This was SVN commit r23089.
2019-10-22 17:53:22 +00:00

92 lines
2.5 KiB
JavaScript

/**
* This class sets up a shortcut to a specific entity in the GUI panel.
* The button shows the portrait a tooltip with state information and a health bar.
* Clicking the button selects and jumps to to the entity.
*/
class PanelEntity
{
constructor(selection, entityID, buttonID, orderKey)
{
this.selection = selection;
this.hitpoints = undefined;
/**
* Public property
*/
this.entityID = entityID;
/**
* Public property
*/
this.orderKey = orderKey;
this.overlayName = "panelEntityHitOverlay[" + buttonID + "]";
this.panelEntityHealthBar = Engine.GetGUIObjectByName("panelEntityHealthBar[" + buttonID + "]");
this.panelEntButton = Engine.GetGUIObjectByName("panelEntityButton[" + buttonID + "]");
this.panelEntButton.onPress = this.onPress.bind(this);
this.panelEntButton.onDoublePress = this.onDoublePress.bind(this);
this.panelEntButton.hidden = false;
let entityState = GetEntityState(entityID);
let template = GetTemplateData(entityState.template);
this.nameTooltip = setStringTags(template.name.specific, this.NameTags) + "\n";
Engine.GetGUIObjectByName("panelEntityImage[" + buttonID + "]").sprite =
"stretched:" + this.PortraitDirectory + template.icon;
}
destroy()
{
this.panelEntButton.hidden = true;
stopColorFade(this.overlayName);
}
update(i, reposition)
{
// TODO: Instead of instant position changes, animate button movement.
if (reposition)
setPanelObjectPosition(this.panelEntButton, i, Infinity);
let entityState = GetEntityState(this.entityID);
if (this.hitpoints != entityState.hitpoints)
{
let size = this.panelEntityHealthBar.size;
size.rright = 100 * entityState.hitpoints / entityState.maxHitpoints;
this.panelEntityHealthBar.size = size;
}
this.panelEntButton.tooltip =
this.nameTooltip +
this.Tooltips.map(tooltip => tooltip(entityState)).filter(tip => tip).join("\n");
if (this.hitpoints > entityState.hitpoints)
startColorFade(this.overlayName, 100, 0, colorFade_attackUnit, true, smoothColorFadeRestart_attackUnit);
this.hitpoints = entityState.hitpoints;
}
onPress()
{
if (!Engine.HotkeyIsPressed("selection.add"))
this.selection.reset();
this.selection.addList([this.entityID]);
}
onDoublePress()
{
this.selection.selectAndMoveTo(getEntityOrHolder(this.entityID));
}
}
PanelEntity.prototype.NameTags = { "font": "sans-bold-16" };
PanelEntity.prototype.PortraitDirectory = "session/portraits/";
PanelEntity.prototype.Tooltips = [
getCurrentHealthTooltip,
getAttackTooltip,
getArmorTooltip,
getEntityTooltip,
getAurasTooltip
];