Changeset View
Standalone View
binaries/data/mods/public/gui/reference/structree/TrainerBox.js
- This file was added.
/** | |||||
* This code wraps the gui representing "trainer units" (a unit that can train other units) within the structree. | |||||
* | |||||
* An instance of this class is created for each child of the gui element named "trainers". | |||||
*/ | |||||
class TrainerBox extends EntityBox | |||||
{ | |||||
constructor(trainerIdx, Parser) | |||||
{ | |||||
super(trainerIdx, Parser); | |||||
this.gui = Engine.GetGUIObjectByName("trainer[" + this.guiIdx + "]"); | |||||
this.productionIconRow = Engine.GetGUIObjectByName("trainer[" + this.guiIdx + "]_row"); | |||||
elexis: (the less names one uses, the less indirection there will be and the less time it consumes to… | |||||
// Position production icons | |||||
horizontallySpaceObjects(this.productionIconRow.name, 4); | |||||
let rowHeight = this.constructor.ProdIconSize().rowHeight; | |||||
let rowSize = this.productionIconRow.size; | |||||
rowSize.top = -rowHeight; | |||||
this.productionIconRow.size = rowSize; | |||||
let size = this.gui.size; | |||||
// Adjust height to accommodate production row | |||||
size.bottom += rowHeight; | |||||
// We make the assumuption that all trainer boxes have the same height | |||||
let boxHeight = this.constructor.boxMargin / 2 + (size.bottom - size.top + this.constructor.boxMargin) * this.guiIdx; | |||||
size.top += boxHeight; | |||||
size.bottom += boxHeight; | |||||
// Make the box adjust automatically to column width | |||||
size.rright = 100; | |||||
size.right = -size.left; | |||||
this.gui.size = size; | |||||
} | |||||
draw(templateName, civCode) | |||||
{ | |||||
super.draw(templateName, civCode); | |||||
// Draw Production Icons | |||||
let prodCount = 0; | |||||
if (this.template.production) | |||||
for (let prodType in this.template.production) | |||||
for (let prod of this.template.production[prodType]) | |||||
{ | |||||
switch (prodType) | |||||
{ | |||||
case "units": | |||||
prod = this.Parser.getEntity(prod, civCode); | |||||
break; | |||||
case "techs": | |||||
prod = clone(this.Parser.getTechnology(prod, civCode)); | |||||
for (let res in this.template.techCostMultiplier) | |||||
if (prod.cost[res]) | |||||
prod.cost[res] *= this.template.techCostMultiplier[res]; | |||||
break; | |||||
Done Inline ActionsThis loop seems duplicated, I would need to run the code to see how the duplication could be minimized ideally. I remember the structree deduplication topic before - it doesnt have to be performed in this diff, and perhaps it already was partially performed using the prototype inheritance (class extends). elexis: This loop seems duplicated, I would need to run the code to see how the duplication could be… | |||||
default: | |||||
continue; | |||||
} | |||||
if (!this.drawProdIcon(prodCount, prod, civCode)) | |||||
break; | |||||
++prodCount; | |||||
} | |||||
if (this.template.upgrades) | |||||
for (let upgrade of this.template.upgrades) | |||||
{ | |||||
if (!this.drawProdIcon(prodCount, upgrade, civCode)) | |||||
break; | |||||
++prodCount; | |||||
} | |||||
hideRemaining(this.productionIconRow.name, prodCount); | |||||
// Centre the production icons with their row | |||||
let prodIconSize = this.constructor.ProdIconSize(); | |||||
let productionRowWidth = prodIconSize.rowWidth * prodCount + prodIconSize.hMargin; | |||||
let size = this.productionIconRow.size; | |||||
size.left = -productionRowWidth / 2; | |||||
this.productionIconRow.size = size; | |||||
// Return the box width | |||||
return Math.max(this.constructor.boxMinWidth, super.captionWidth(), productionRowWidth); | |||||
} | |||||
Done Inline Actionssuper.captionWidth() -> this.captionWidth() unless you really need to call the inherited function (for example when wanting to call super.captionWidth() from this.captionWidth(). This way for example mods can replace the captionWidth function with a custom function and its getting called, without having to overwrite the protoype property. (There could even be class1 inheriting class2 inheriting class3 and the topmost function would be called, not that it matters significantly) elexis: `super.captionWidth()` -> `this.captionWidth()` unless you really need to call the inherited… | |||||
/** | |||||
* Draws a single production icon (the small icons on the gray bars). | |||||
* | |||||
* This differs from the version used within the StructureBox class by not having a rowID argument. | |||||
* | |||||
* Returns True if successfully drawn, False if no space left to draw. | |||||
*/ | |||||
drawProdIcon(iconID, template, civCode) | |||||
{ | |||||
let prodEle = this.productionIconRow.children[iconID]; | |||||
if (prodEle === undefined) | |||||
{ | |||||
error("The trainers of the currently displayed civ have more production " + | |||||
"icons than can be supported by the current GUI layout"); | |||||
return false; | |||||
} | |||||
this.constructor.drawProdIcon(prodEle, template, civCode); | |||||
return true; | |||||
} | |||||
} |
(the less names one uses, the less indirection there will be and the less time it consumes to look up which code refers to which GUI object and which GUI object refers to which code)
perhaps