0ad/binaries/data/mods/public/gui/session/developer_overlay/DeveloperOverlay.js
elexis 96a6a136b6 Rewrite developer overlay to use class syntax, one class per checkbox, a class for the EntityState overlay and TimeWarp debug feature, refs #5387.
Using 22 classes instead of 1 class (refs 2bcf4f678b/D1928) leverages
more benefit of the paradigm.
In particular it means the checkboxes can own the EntityState and
TimeWarp helpers and the DeveloperOverlay itself can remain independent.
Improve performance by 200 microseconds per turn by unsubscribing from
onSimulationUpdate when the developer overlay is not opened, refs
e3f43f6352 / D2378.
Move TimeWarp from input.js from 8ddef2fee0 to independent class using
hotkey release event from 78bc56f33e, refs #3194.

Differential Revision: https://code.wildfiregames.com/D2383
This was SVN commit r23081.
2019-10-19 00:26:34 +00:00

68 lines
1.8 KiB
JavaScript

/**
* This class stores the checkboxes that are part of the developer overlay.
* These checkboxes may own their own helper class instances, such as the EntityState or TimeWarp feature.
*/
class DeveloperOverlay
{
constructor(playerViewControl, selection)
{
this.devCommandsOverlay = Engine.GetGUIObjectByName("devCommandsOverlay");
this.devCommandsOverlay.onPress = this.toggle.bind(this);
this.checkBoxes = this.getCheckboxNames().map((name, i) =>
new DeveloperOverlayCheckbox(
new DeveloperOverlayCheckboxes.prototype[name](playerViewControl, selection),
i));
this.resize();
}
/**
* Mods may overwrite this to change the order.
*/
getCheckboxNames()
{
return Object.keys(DeveloperOverlayCheckboxes.prototype);
}
toggle()
{
if (g_IsNetworked && !g_GameAttributes.settings.CheatsEnabled)
return;
this.devCommandsOverlay.hidden = !this.devCommandsOverlay.hidden;
this.sendNotification();
this.checkBoxes.forEach(checkbox => {
checkbox.setHidden(this.devCommandsOverlay.hidden);
});
}
sendNotification()
{
let message = this.devCommandsOverlay.hidden ? this.CloseNotification : this.OpenNotification;
// Only players can send the simulation chat command
if (Engine.GetPlayerID() == -1)
g_Chat.submitChat(message);
else
Engine.PostNetworkCommand({
"type": "aichat",
"message": message,
"translateMessage": true,
"translateParameters": [],
"parameters": {}
});
}
resize()
{
let size = this.devCommandsOverlay.size;
size.bottom =
size.top +
this.checkBoxes.reduce((height, checkbox) => height + checkbox.getHeight(), 0);
this.devCommandsOverlay.size = size;
}
}
DeveloperOverlay.prototype.OpenNotification = markForTranslation("The Developer Overlay was opened.");
DeveloperOverlay.prototype.CloseNotification = markForTranslation("The Developer Overlay was closed.");