Move researched tech progress overlay to a separate file, use class notation, refs #5387.

Improves performance if no techs are researched from 200 to 50
microseconds per turn (from 500 to 300 microseconds for one tech etc).

Differential Revision: https://code.wildfiregames.com/D2386
This was SVN commit r23088.
This commit is contained in:
elexis 2019-10-21 23:05:04 +00:00
parent ba993d4610
commit 688a2a00a8
4 changed files with 112 additions and 58 deletions

View file

@ -0,0 +1,107 @@
/**
* This class is responsible for displaying the currently researched technologies in an overlay.
*/
class ResearchProgress
{
constructor(playerViewControl)
{
this.buttons = Engine.GetGUIObjectByName("researchStartedButtons").children;
this.buttonHandlers = this.buttons.map((button, i) => new ResearchProgressButton(i));
/**
* Top coordinate of the research list.
* Changes depending on the number of displayed counters.
*/
this.topOffset = 0;
let updater = this.updateResearchProgress.bind(this);
registerSimulationUpdateHandler(updater);
playerViewControl.registerViewedPlayerChangeHandler(updater);
}
setTopOffset(offset)
{
this.topOffset = offset;
}
updateResearchProgress()
{
let researchStarted = Engine.GuiInterfaceCall("GetStartedResearch", g_ViewedPlayer);
let i = 0;
for (let techName in researchStarted)
{
if (i == this.buttons.length)
break;
this.buttonHandlers[i++].onResearchedProgress(this.topOffset, techName, researchStarted[techName]);
}
while (i < this.buttons.length)
this.buttons[i++].hidden = true;
}
}
/**
* This is an individual button displaying a tech currently researched by the currently viewed player.
*/
class ResearchProgressButton
{
constructor(i)
{
this.button = Engine.GetGUIObjectByName("researchStartedButton[" + i + "]");
this.sprite = Engine.GetGUIObjectByName("researchStartedIcon[" + i + "]");
this.progress = Engine.GetGUIObjectByName("researchStartedProgressSlider[" + i + "]");
this.timeRemaining = Engine.GetGUIObjectByName("researchStartedTimeRemaining[" + i + "]");
this.buttonHeight = this.button.size.bottom - this.button.size.top;
this.buttonTop = this.Margin + (this.Margin + this.buttonHeight) * i;
this.progressHeight = this.progress.size.bottom - this.progress.size.top;
this.progressTop = this.progress.size.top;
this.button.onPress = this.onPress.bind(this);
}
onResearchedProgress(offset, techName, researchStatus)
{
this.researcher = researchStatus.researcher;
let template = GetTechnologyData(techName, g_Players[g_ViewedPlayer].civ);
this.sprite.sprite = "stretched:" + this.PortraitDirectory + template.icon;
let size = this.button.size;
size.top = offset + this.buttonTop;
size.bottom = size.top + this.buttonHeight;
this.button.size = size;
this.button.tooltip = getEntityNames(template);
this.button.hidden = false;
size = this.progress.size;
size.top = this.progressTop + this.progressHeight * researchStatus.progress;
this.progress.size = size;
this.timeRemaining.caption =
Engine.FormatMillisecondsIntoDateStringGMT(
researchStatus.timeRemaining,
translateWithContext("countdown format", this.CountdownFormat));
}
onPress()
{
selectAndMoveTo(this.researcher);
}
}
/**
* Distance between consecutive buttons.
*/
ResearchProgressButton.prototype.Margin = 4;
/**
* Directory containing all icons.
*/
ResearchProgressButton.prototype.PortraitDirectory = "session/portraits/";
/**
* This format is used when displaying the remaining time of the currently viewed techs in research.
*/
ResearchProgressButton.prototype.CountdownFormat = markForTranslationWithContext("countdown format", "m:ss");

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<object size="100%-50 40 100%-10 100%-200">
<object size="100%-50 40 100%-10 100%-200" name="researchStartedButtons">
<repeat count="10">
<object name="researchStartedButton[n]" hidden="true" style="iconButton" type="button" size="0 0 40 40" tooltip_style="sessionToolTipBottom">
<object name="researchStartedIcon[n]" ghost="true" type="image" size="3 3 37 37"/>

View file

@ -21,6 +21,7 @@ var g_ObjectivesDialog;
var g_PauseControl;
var g_PauseOverlay;
var g_PlayerViewControl;
var g_ResearchProgress;
var g_TradeDialog;
var g_TopPanel;
@ -156,12 +157,6 @@ var g_EntitySelectionChangeHandlers = new Set();
*/
var g_HotkeyChangeHandlers = new Set();
/**
* Top coordinate of the research list.
* Changes depending on the number of displayed counters.
*/
var g_ResearchListTop = 4;
/**
* List of additional entities to highlight.
*/
@ -288,6 +283,7 @@ function init(initData, hotloadData)
g_PauseControl = new PauseControl();
g_PauseOverlay = new PauseOverlay(g_PauseControl);
g_Menu = new Menu(g_PauseControl, g_PlayerViewControl, g_Chat);
g_ResearchProgress = new ResearchProgress(g_PlayerViewControl);
g_TradeDialog = new TradeDialog(g_PlayerViewControl);
g_TopPanel = new TopPanel(g_PlayerViewControl, g_DiplomacyDialog, g_TradeDialog, g_ObjectivesDialog, g_GameSpeedControl);
@ -730,7 +726,6 @@ function updateGUIObjects()
displayPanelEntities();
updateGroups();
updateResearchDisplay();
updateSelectionDetails();
updateBuildingPlacementPreview();
updateTimeNotifications();
@ -947,55 +942,6 @@ function selectAndMoveTo(ent)
Engine.CameraMoveTo(position.x, position.z);
}
function updateResearchDisplay()
{
let researchStarted = Engine.GuiInterfaceCall("GetStartedResearch", g_ViewedPlayer);
// Set up initial positioning.
let buttonSideLength = Engine.GetGUIObjectByName("researchStartedButton[0]").size.right;
for (let i = 0; i < 10; ++i)
{
let button = Engine.GetGUIObjectByName("researchStartedButton[" + i + "]");
let size = button.size;
size.top = g_ResearchListTop + (4 + buttonSideLength) * i;
size.bottom = size.top + buttonSideLength;
button.size = size;
}
let numButtons = 0;
for (let tech in researchStarted)
{
// Show at most 10 in-progress techs.
if (numButtons >= 10)
break;
let template = GetTechnologyData(tech, g_Players[g_ViewedPlayer].civ);
let button = Engine.GetGUIObjectByName("researchStartedButton[" + numButtons + "]");
button.hidden = false;
button.tooltip = getEntityNames(template);
button.onpress = (function(e) { return function() { selectAndMoveTo(e); }; })(researchStarted[tech].researcher);
let icon = "stretched:session/portraits/" + template.icon;
Engine.GetGUIObjectByName("researchStartedIcon[" + numButtons + "]").sprite = icon;
// Scale the progress indicator.
let size = Engine.GetGUIObjectByName("researchStartedProgressSlider[" + numButtons + "]").size;
// Buttons are assumed to be square, so left/right offsets can be used for top/bottom.
size.top = size.left + Math.round(researchStarted[tech].progress * (size.right - size.left));
Engine.GetGUIObjectByName("researchStartedProgressSlider[" + numButtons + "]").size = size;
Engine.GetGUIObjectByName("researchStartedTimeRemaining[" + numButtons + "]").caption =
Engine.FormatMillisecondsIntoDateStringGMT(researchStarted[tech].timeRemaining, translateWithContext("countdown format", "m:ss"));
++numButtons;
}
// Hide unused buttons.
for (let i = numButtons; i < 10; ++i)
Engine.GetGUIObjectByName("researchStartedButton[" + i + "]").hidden = true;
}
/**
* Toggles the display of status bars for all of the player's entities.
*
@ -1152,5 +1098,5 @@ function appendSessionCounters(counters)
if (simState.ceasefireActive && Engine.ConfigDB_GetValue("user", "gui.session.ceasefirecounter") === "true")
counters.push(timeToString(simState.ceasefireTimeRemaining));
g_ResearchListTop = 4 + 14 * counters.length;
g_ResearchProgress.setTopOffset(14 * counters.length);
}

View file

@ -87,6 +87,7 @@
<include directory="gui/session/diplomacy/"/>
<include directory="gui/session/objectives/"/>
<include file="gui/session/GameSpeedControl.xml"/>
<include file="gui/session/ResearchProgress.xml"/>
<include file="gui/session/TopPanel.xml"/>
<include file="gui/session/trade/TradeDialog.xml"/>
<include file="gui/session/tutorial_panel.xml"/>