Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/binaries/data/mods/public/gui/session/selection_panels.js
Show First 20 Lines • Show All 622 Lines • ▼ Show 20 Lines | g_SelectionPanels.Research = { | ||||
"getItems": function(unitEntStates) | "getItems": function(unitEntStates) | ||||
{ | { | ||||
let ret = []; | let ret = []; | ||||
if (unitEntStates.length == 1) | if (unitEntStates.length == 1) | ||||
return !unitEntStates[0].production || !unitEntStates[0].production.technologies ? ret : | return !unitEntStates[0].production || !unitEntStates[0].production.technologies ? ret : | ||||
unitEntStates[0].production.technologies.map(tech => ({ | unitEntStates[0].production.technologies.map(tech => ({ | ||||
"tech": tech, | "tech": tech, | ||||
"techCostMultiplier": unitEntStates[0].production.techCostMultiplier, | "techCostMultiplier": unitEntStates[0].production.techCostMultiplier, | ||||
"researchFacilityId": unitEntStates[0].id | "researchFacilityId": unitEntStates[0].id, | ||||
"isUpgrading": !!unitEntStates[0].upgrade && unitEntStates[0].upgrade.isUpgrading | |||||
})); | })); | ||||
for (let state of unitEntStates) | let sortedEntStates = unitEntStates.sort((a, b) => | ||||
(!b.upgrade || !b.upgrade.isUpgrading) - (!a.upgrade || !a.upgrade.isUpgrading)); | |||||
for (let state of sortedEntStates) | |||||
{ | { | ||||
if (!state.production || !state.production.technologies) | if (!state.production || !state.production.technologies) | ||||
continue; | continue; | ||||
// Remove the techs we already have in ret (with the same name and techCostMultiplier) | // Remove the techs we already have in ret (with the same name and techCostMultiplier) | ||||
let filteredTechs = state.production.technologies.filter( | let filteredTechs = state.production.technologies.filter( | ||||
tech => tech != null && !ret.some( | tech => tech != null && !ret.some( | ||||
item => | item => | ||||
(item.tech == tech || | (item.tech == tech || | ||||
item.tech.pair && | item.tech.pair && | ||||
tech.pair && | tech.pair && | ||||
item.tech.bottom == tech.bottom && | item.tech.bottom == tech.bottom && | ||||
item.tech.top == tech.top) && | item.tech.top == tech.top) && | ||||
Object.keys(item.techCostMultiplier).every( | Object.keys(item.techCostMultiplier).every( | ||||
k => item.techCostMultiplier[k] == state.production.techCostMultiplier[k]) | k => item.techCostMultiplier[k] == state.production.techCostMultiplier[k]) | ||||
)); | )); | ||||
if (filteredTechs.length + ret.length <= this.getMaxNumberOfItems() && | if (filteredTechs.length + ret.length <= this.getMaxNumberOfItems() && | ||||
getNumberOfRightPanelButtons() <= this.getMaxNumberOfItems() * (filteredTechs.some(tech => !!tech.pair) ? 1 : 2)) | getNumberOfRightPanelButtons() <= this.getMaxNumberOfItems() * (filteredTechs.some(tech => !!tech.pair) ? 1 : 2)) | ||||
ret = ret.concat(filteredTechs.map(tech => ({ | ret = ret.concat(filteredTechs.map(tech => ({ | ||||
"tech": tech, | "tech": tech, | ||||
"techCostMultiplier": state.production.techCostMultiplier, | "techCostMultiplier": state.production.techCostMultiplier, | ||||
"researchFacilityId": state.id | "researchFacilityId": state.id, | ||||
"isUpgrading": !!state.upgrade && state.upgrade.isUpgrading | |||||
}))); | }))); | ||||
} | } | ||||
return ret; | return ret; | ||||
}, | }, | ||||
"hideItem": function(i, rowLength) // Called when no item is found | "hideItem": function(i, rowLength) // Called when no item is found | ||||
{ | { | ||||
Engine.GetGUIObjectByName("unitResearchButton[" + i + "]").hidden = true; | Engine.GetGUIObjectByName("unitResearchButton[" + i + "]").hidden = true; | ||||
// We also remove the paired tech and the pair symbol | // We also remove the paired tech and the pair symbol | ||||
▲ Show 20 Lines • Show All 129 Lines • ▼ Show 20 Lines | for (let tech of data.item.tech.pair ? [data.item.tech.bottom, data.item.tech.top] : [data.item.tech]) | ||||
else if (neededResources) | else if (neededResources) | ||||
{ | { | ||||
button.enabled = false; | button.enabled = false; | ||||
modifier += resourcesToAlphaMask(neededResources) + ":"; | modifier += resourcesToAlphaMask(neededResources) + ":"; | ||||
} | } | ||||
else | else | ||||
button.enabled = controlsPlayer(data.player); | button.enabled = controlsPlayer(data.player); | ||||
if (data.item.isUpgrading) | |||||
{ | |||||
button.enabled = false; | |||||
modifier += "color:0 0 0 127:grayscale:"; | |||||
button.tooltip += "\n" + coloredText(translate("Cannot research while upgrading."), "red"); | |||||
} | |||||
if (template.icon) | if (template.icon) | ||||
icon.sprite = modifier + "stretched:session/portraits/" + template.icon; | icon.sprite = modifier + "stretched:session/portraits/" + template.icon; | ||||
setPanelObjectPosition(button, position, data.rowLength); | setPanelObjectPosition(button, position, data.rowLength); | ||||
// Prepare to handle the top button (if any) | // Prepare to handle the top button (if any) | ||||
position -= data.rowLength; | position -= data.rowLength; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 198 Lines • ▼ Show 20 Lines | "setupButton": function(data) | ||||
} | } | ||||
else | else | ||||
{ | { | ||||
data.button.enabled = controlsPlayer(data.player); | data.button.enabled = controlsPlayer(data.player); | ||||
if (neededResources) | if (neededResources) | ||||
modifier = resourcesToAlphaMask(neededResources) + ":"; | modifier = resourcesToAlphaMask(neededResources) + ":"; | ||||
} | } | ||||
if (data.unitEntStates.every(state => state.upgrade && state.upgrade.isUpgrading)) | |||||
{ | |||||
data.button.enabled = false; | |||||
modifier = "color:0 0 0 127:grayscale:"; | |||||
data.button.tooltip += "\n" + coloredText(translate("Cannot train while upgrading."), "red"); | |||||
} | |||||
if (template.icon) | if (template.icon) | ||||
data.icon.sprite = modifier + "stretched:session/portraits/" + template.icon; | data.icon.sprite = modifier + "stretched:session/portraits/" + template.icon; | ||||
let index = data.i + getNumberOfRightPanelButtons(); | let index = data.i + getNumberOfRightPanelButtons(); | ||||
setPanelObjectPosition(data.button, index, data.rowLength); | setPanelObjectPosition(data.button, index, data.rowLength); | ||||
return true; | return true; | ||||
} | } | ||||
Show All 14 Lines | "getItems": function(unitEntStates) | ||||
return unitEntStates[0].upgrade && unitEntStates[0].upgrade.upgrades; | return unitEntStates[0].upgrade && unitEntStates[0].upgrade.upgrades; | ||||
}, | }, | ||||
"setupButton": function(data) | "setupButton": function(data) | ||||
{ | { | ||||
let template = GetTemplateData(data.item.entity); | let template = GetTemplateData(data.item.entity); | ||||
if (!template) | if (!template) | ||||
return false; | return false; | ||||
let progressOverlay = Engine.GetGUIObjectByName("unitUpgradeProgressSlider[" + data.i + "]"); | |||||
progressOverlay.hidden = true; | |||||
let technologyEnabled = true; | let technologyEnabled = true; | ||||
if (data.item.requiredTechnology) | if (data.item.requiredTechnology) | ||||
technologyEnabled = Engine.GuiInterfaceCall("IsTechnologyResearched", { | technologyEnabled = Engine.GuiInterfaceCall("IsTechnologyResearched", { | ||||
"tech": data.item.requiredTechnology, | "tech": data.item.requiredTechnology, | ||||
"player": data.player | "player": data.player | ||||
}); | }); | ||||
let limits = getEntityLimitAndCount(data.playerState, data.item.entity); | |||||
let upgradingEntStates = data.unitEntStates.filter(state => state.upgrade.template == data.item.entity); | |||||
let upgradableEntStates = data.unitEntStates.filter(state => | |||||
!state.upgrade.progress && | |||||
(!state.production || !state.production.queue || !state.production.queue.length)); | |||||
let neededResources = data.item.cost && Engine.GuiInterfaceCall("GetNeededResources", { | let neededResources = data.item.cost && Engine.GuiInterfaceCall("GetNeededResources", { | ||||
"cost": multiplyEntityCosts(data.item, data.unitEntStates.length), | "cost": multiplyEntityCosts(data.item, upgradableEntStates.length), | ||||
"player": data.player | "player": data.player | ||||
}); | }); | ||||
let limits = getEntityLimitAndCount(data.playerState, data.item.entity); | |||||
let progress = data.unitEntStates[0].upgrade.progress || 0; | |||||
let isUpgrading = data.unitEntStates[0].upgrade.template == data.item.entity; | |||||
let tooltip; | let tooltip; | ||||
if (!progress) | let modifier = ""; | ||||
if (!upgradingEntStates.length && upgradableEntStates.length) | |||||
{ | { | ||||
let tooltips = []; | let tooltips = []; | ||||
if (data.item.tooltip) | if (data.item.tooltip) | ||||
tooltips.push(sprintf(translate("Upgrade to %(name)s. %(tooltip)s"), { | tooltips.push(sprintf(translate("Upgrade to %(name)s. %(tooltip)s"), { | ||||
"name": template.name.generic, | "name": template.name.generic, | ||||
"tooltip": translate(data.item.tooltip) | "tooltip": translate(data.item.tooltip) | ||||
})); | })); | ||||
else | else | ||||
tooltips.push(sprintf(translate("Upgrade to %(name)s."), { | tooltips.push(sprintf(translate("Upgrade to %(name)s."), { | ||||
"name": template.name.generic | "name": template.name.generic | ||||
})); | })); | ||||
tooltips.push( | tooltips.push( | ||||
getEntityCostComponentsTooltipString(data.item, undefined, data.unitEntStates.length), | getEntityCostComponentsTooltipString(data.item, undefined, upgradableEntStates.length), | ||||
formatLimitString(limits.entLimit, limits.entCount, limits.entLimitChangers), | formatLimitString(limits.entLimit, limits.entCount, limits.entLimitChangers), | ||||
getRequiredTechnologyTooltip(technologyEnabled, data.item.requiredTechnology, GetSimState().players[data.player].civ), | getRequiredTechnologyTooltip(technologyEnabled, data.item.requiredTechnology, GetSimState().players[data.player].civ), | ||||
getNeededResourcesTooltip(neededResources), | getNeededResourcesTooltip(neededResources), | ||||
showTemplateViewerOnRightClickTooltip()); | showTemplateViewerOnRightClickTooltip()); | ||||
tooltip = tooltips.filter(tip => tip).join("\n"); | tooltip = tooltips.filter(tip => tip).join("\n"); | ||||
data.button.onPress = function() { upgradeEntity(data.item.entity); }; | data.button.onPress = function() { | ||||
upgradeEntity( | |||||
data.item.entity, | |||||
upgradableEntStates.map(state => state.id)); | |||||
}; | |||||
if (!technologyEnabled || limits.canBeAddedCount == 0 && | |||||
!upgradableEntStates.some(state => hasSameRestrictionCategory(data.item.entity, state.template))) | |||||
{ | |||||
data.button.enabled = false; | |||||
modifier = "color:0 0 0 127:grayscale:"; | |||||
} | |||||
else if (neededResources) | |||||
{ | |||||
data.button.enabled = false; | |||||
modifier = resourcesToAlphaMask(neededResources) + ":"; | |||||
} | |||||
data.countDisplay.caption = upgradableEntStates.length > 1 ? upgradableEntStates.length : ""; | |||||
} | } | ||||
else if (isUpgrading) | else if (upgradingEntStates.length) | ||||
{ | { | ||||
tooltip = translate("Cancel Upgrading"); | tooltip = translate("Cancel Upgrading"); | ||||
data.button.onPress = function() { cancelUpgradeEntity(); }; | data.button.onPress = function() { cancelUpgradeEntity(); }; | ||||
data.countDisplay.caption = upgradingEntStates.length > 1 ? upgradingEntStates.length : ""; | |||||
let progress = 0; | |||||
for (let state of upgradingEntStates) | |||||
progress = Math.max(progress, state.upgrade.progress || 1); | |||||
let progressOverlaySize = progressOverlay.size; | |||||
// TODO This is bad: we assume the progressOverlay is square | |||||
progressOverlaySize.top = progressOverlaySize.bottom + Math.round((1 - progress) * (progressOverlaySize.left - progressOverlaySize.right)); | |||||
progressOverlay.size = progressOverlaySize; | |||||
progressOverlay.hidden = false; | |||||
} | } | ||||
else | else | ||||
{ | { | ||||
tooltip = translate("Cannot upgrade when the entity is already upgrading."); | tooltip = coloredText(translatePlural( | ||||
"Cannot upgrade when the entity is training, researching or already upgrading.", | |||||
"Cannot upgrade when all entities are training, researching or already upgrading.", | |||||
data.unitEntStates.length), "red"); | |||||
data.button.onPress = function() {}; | data.button.onPress = function() {}; | ||||
data.button.enabled = false; | |||||
modifier = "color:0 0 0 127:grayscale:"; | |||||
} | } | ||||
data.button.enabled = controlsPlayer(data.player); | data.button.enabled = controlsPlayer(data.player); | ||||
data.button.tooltip = tooltip; | data.button.tooltip = tooltip; | ||||
data.button.onPressRight = function() { | data.button.onPressRight = function() { | ||||
showTemplateDetails(data.item.entity); | showTemplateDetails(data.item.entity); | ||||
}; | }; | ||||
let modifier = ""; | |||||
if (!isUpgrading) | |||||
if (progress || !technologyEnabled || limits.canBeAddedCount == 0 && | |||||
!hasSameRestrictionCategory(data.item.entity, data.unitEntStates[0].template)) | |||||
{ | |||||
data.button.enabled = false; | |||||
modifier = "color:0 0 0 127:grayscale:"; | |||||
} | |||||
else if (neededResources) | |||||
{ | |||||
data.button.enabled = false; | |||||
modifier = resourcesToAlphaMask(neededResources) + ":"; | |||||
} | |||||
data.icon.sprite = modifier + "stretched:session/" + | data.icon.sprite = modifier + "stretched:session/" + | ||||
(data.item.icon || "portraits/" + template.icon); | (data.item.icon || "portraits/" + template.icon); | ||||
data.countDisplay.caption = data.unitEntStates.length > 1 ? data.unitEntStates.length : ""; | |||||
let progressOverlay = Engine.GetGUIObjectByName("unitUpgradeProgressSlider[" + data.i + "]"); | |||||
if (isUpgrading) | |||||
{ | |||||
let size = progressOverlay.size; | |||||
size.top = size.left + Math.round(progress * (size.right - size.left)); | |||||
progressOverlay.size = size; | |||||
} | |||||
progressOverlay.hidden = !isUpgrading; | |||||
setPanelObjectPosition(data.button, data.i + getNumberOfRightPanelButtons(), data.rowLength); | setPanelObjectPosition(data.button, data.i + getNumberOfRightPanelButtons(), data.rowLength); | ||||
return true; | return true; | ||||
} | } | ||||
}; | }; | ||||
function initSelectionPanels() | function initSelectionPanels() | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 54 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator