2010-07-31 23:46:25 +02:00
|
|
|
// Panel types
|
|
|
|
const SELECTION = "Selection";
|
|
|
|
const QUEUE = "Queue";
|
2010-09-28 16:19:13 +02:00
|
|
|
const GARRISON = "Garrison";
|
|
|
|
const FORMATION = "Formation";
|
2010-07-31 23:46:25 +02:00
|
|
|
const TRAINING = "Training";
|
2012-04-20 19:21:04 +02:00
|
|
|
const RESEARCH = "Research";
|
2010-07-31 23:46:25 +02:00
|
|
|
const CONSTRUCTION = "Construction";
|
2010-08-01 07:09:30 +02:00
|
|
|
const COMMAND = "Command";
|
2011-06-18 00:13:39 +02:00
|
|
|
const STANCE = "Stance";
|
2010-07-31 23:46:25 +02:00
|
|
|
|
2010-10-04 00:42:15 +02:00
|
|
|
// Constants
|
|
|
|
const COMMANDS_PANEL_WIDTH = 228;
|
|
|
|
const UNIT_PANEL_BASE = -52; // QUEUE: The offset above the main panel (will often be negative)
|
|
|
|
const UNIT_PANEL_HEIGHT = 44; // QUEUE: The height needed for a row of buttons
|
2010-07-19 23:23:12 +02:00
|
|
|
|
2012-03-08 21:42:28 +01:00
|
|
|
// Trading constants
|
|
|
|
const TRADING_RESOURCES = ["food", "wood", "stone", "metal"];
|
|
|
|
|
2011-11-24 16:43:32 +01:00
|
|
|
// Barter constants
|
|
|
|
const BARTER_RESOURCE_AMOUNT_TO_SELL = 100;
|
|
|
|
const BARTER_BUNCH_MULTIPLIER = 5;
|
|
|
|
const BARTER_RESOURCES = ["food", "wood", "stone", "metal"];
|
|
|
|
const BARTER_ACTIONS = ["Sell", "Buy"];
|
|
|
|
|
2010-07-01 22:31:47 +02:00
|
|
|
// The number of currently visible buttons (used to optimise showing/hiding)
|
2012-04-20 19:21:04 +02:00
|
|
|
var g_unitPanelButtons = {"Selection": 0, "Queue": 0, "Formation": 0, "Garrison": 0, "Training": 0, "Research": 0, "Barter": 0, "Trading": 0, "Construction": 0, "Command": 0, "Stance": 0};
|
2010-07-01 22:31:47 +02:00
|
|
|
|
|
|
|
// Unit panels are panels with row(s) of buttons
|
2012-03-08 21:42:28 +01:00
|
|
|
var g_unitPanels = ["Selection", "Queue", "Formation", "Garrison", "Training", "Barter", "Trading", "Construction", "Research", "Stance", "Command"];
|
2011-11-24 16:43:32 +01:00
|
|
|
|
|
|
|
// Indexes of resources to sell and buy on barter panel
|
|
|
|
var g_barterSell = 0;
|
2010-07-01 22:31:47 +02:00
|
|
|
|
2010-09-25 17:22:41 +02:00
|
|
|
// Lay out a row of centered buttons (does not work inside a loop like the other function)
|
|
|
|
function layoutButtonRowCentered(rowNumber, guiName, startIndex, endIndex, width)
|
|
|
|
{
|
|
|
|
var buttonSideLength = getGUIObjectByName("unit"+guiName+"Button[0]").size.bottom;
|
|
|
|
var buttonSpacer = buttonSideLength+1;
|
|
|
|
var colNumber = 0;
|
|
|
|
|
|
|
|
// Collect buttons
|
|
|
|
var buttons = [];
|
|
|
|
var icons = [];
|
|
|
|
|
|
|
|
for (var i = startIndex; i < endIndex; i++)
|
|
|
|
{
|
|
|
|
var button = getGUIObjectByName("unit"+guiName+"Button["+i+"]");
|
|
|
|
var icon = getGUIObjectByName("unit"+guiName+"Icon["+i+"]");
|
|
|
|
|
|
|
|
if (button)
|
|
|
|
{
|
|
|
|
buttons.push(button);
|
|
|
|
icons.push(icon);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Location of middle button
|
|
|
|
var middleIndex = Math.ceil(buttons.length/2);
|
|
|
|
|
|
|
|
// Determine whether even or odd number of buttons
|
|
|
|
var center = (buttons.length/2 == Math.ceil(buttons.length/2))? Math.ceil(width/2) : Math.ceil(width/2+buttonSpacer/2);
|
|
|
|
|
|
|
|
// Left Side
|
|
|
|
for (var i = middleIndex-1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
if (buttons[i])
|
|
|
|
{
|
|
|
|
var icon = icons[i];
|
|
|
|
var size = buttons[i].size;
|
|
|
|
size.left = center - buttonSpacer*colNumber - buttonSideLength;
|
|
|
|
size.right = center - buttonSpacer*colNumber;
|
|
|
|
size.top = buttonSpacer*rowNumber;
|
|
|
|
size.bottom = buttonSpacer*rowNumber + buttonSideLength;
|
|
|
|
buttons[i].size = size;
|
|
|
|
colNumber++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Right Side
|
|
|
|
center += 1; // add spacing to center buttons
|
|
|
|
colNumber = 0; // reset to 0
|
|
|
|
|
|
|
|
for (var i = middleIndex; i < buttons.length; i++)
|
|
|
|
{
|
|
|
|
if (buttons[i])
|
|
|
|
{
|
|
|
|
var icon = icons[i];
|
|
|
|
var size = buttons[i].size;
|
|
|
|
size.left = center + buttonSpacer*colNumber;
|
|
|
|
size.right = center + buttonSpacer*colNumber + buttonSideLength;
|
|
|
|
size.top = buttonSpacer*rowNumber;
|
|
|
|
size.bottom = buttonSpacer*rowNumber + buttonSideLength;
|
|
|
|
buttons[i].size = size;
|
|
|
|
colNumber++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-03 18:51:20 +02:00
|
|
|
// Lay out button rows
|
|
|
|
function layoutButtonRow(rowNumber, guiName, buttonSideLength, buttonSpacer, startIndex, endIndex)
|
|
|
|
{
|
2010-07-06 01:04:38 +02:00
|
|
|
var colNumber = 0;
|
2010-07-03 18:51:20 +02:00
|
|
|
|
2010-07-20 01:01:58 +02:00
|
|
|
for (var i = startIndex; i < endIndex; i++)
|
2010-07-03 18:51:20 +02:00
|
|
|
{
|
|
|
|
var button = getGUIObjectByName("unit"+guiName+"Button["+i+"]");
|
2010-09-25 17:22:41 +02:00
|
|
|
|
|
|
|
if (button)
|
|
|
|
{
|
|
|
|
var size = button.size;
|
2011-09-06 20:15:27 +02:00
|
|
|
|
2010-09-25 17:22:41 +02:00
|
|
|
size.left = buttonSpacer*colNumber;
|
|
|
|
size.right = buttonSpacer*colNumber + buttonSideLength;
|
|
|
|
size.top = buttonSpacer*rowNumber;
|
|
|
|
size.bottom = buttonSpacer*rowNumber + buttonSideLength;
|
2011-09-06 20:15:27 +02:00
|
|
|
|
2010-09-25 17:22:41 +02:00
|
|
|
button.size = size;
|
|
|
|
colNumber++;
|
|
|
|
}
|
2010-07-03 18:51:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-01 22:31:47 +02:00
|
|
|
// Sets up "unit panels" - the panels with rows of icons (Helper function for updateUnitDisplay)
|
2010-07-31 23:46:25 +02:00
|
|
|
function setupUnitPanel(guiName, usedPanels, unitEntState, items, callback)
|
2010-07-01 22:31:47 +02:00
|
|
|
{
|
|
|
|
usedPanels[guiName] = 1;
|
2012-03-08 21:42:28 +01:00
|
|
|
|
2010-07-31 23:46:25 +02:00
|
|
|
var numberOfItems = items.length;
|
2010-10-02 00:18:03 +02:00
|
|
|
var selection = g_Selection.toList();
|
2010-10-24 00:43:15 +02:00
|
|
|
var garrisonGroups = new EntityGroups();
|
2010-07-31 23:46:25 +02:00
|
|
|
|
2011-09-06 20:15:27 +02:00
|
|
|
// Determine how many buttons there should be
|
|
|
|
switch (guiName)
|
2010-07-01 22:31:47 +02:00
|
|
|
{
|
2010-08-03 03:54:16 +02:00
|
|
|
case SELECTION:
|
2011-09-06 20:15:27 +02:00
|
|
|
if (numberOfItems > 16)
|
|
|
|
numberOfItems = 16;
|
2010-07-31 23:46:25 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case QUEUE:
|
2011-09-06 20:15:27 +02:00
|
|
|
if (numberOfItems > 16)
|
|
|
|
numberOfItems = 16;
|
2010-09-28 16:19:13 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GARRISON:
|
2011-09-06 20:15:27 +02:00
|
|
|
if (numberOfItems > 16)
|
|
|
|
numberOfItems = 16;
|
|
|
|
//Group garrisoned units based on class
|
|
|
|
garrisonGroups.add(unitEntState.garrisonHolder.entities);
|
2010-09-28 16:19:13 +02:00
|
|
|
break;
|
|
|
|
|
2011-06-18 00:13:39 +02:00
|
|
|
case STANCE:
|
2011-09-06 20:15:27 +02:00
|
|
|
if (numberOfItems > 5)
|
|
|
|
numberOfItems = 5;
|
2012-04-15 18:27:44 +02:00
|
|
|
break;
|
2012-04-15 05:44:57 +02:00
|
|
|
|
2010-09-28 16:19:13 +02:00
|
|
|
case FORMATION:
|
2011-09-06 20:15:27 +02:00
|
|
|
if (numberOfItems > 16)
|
|
|
|
numberOfItems = 16;
|
2010-07-31 23:46:25 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TRAINING:
|
2011-09-06 20:15:27 +02:00
|
|
|
if (numberOfItems > 24)
|
|
|
|
numberOfItems = 24;
|
2010-07-31 23:46:25 +02:00
|
|
|
break;
|
2012-04-20 19:21:04 +02:00
|
|
|
|
|
|
|
case RESEARCH:
|
|
|
|
if (numberOfItems > 8)
|
|
|
|
numberOfItems = 8;
|
|
|
|
break;
|
2010-08-17 15:37:46 +02:00
|
|
|
|
2010-07-31 23:46:25 +02:00
|
|
|
case CONSTRUCTION:
|
2011-09-06 20:15:27 +02:00
|
|
|
if (numberOfItems > 24)
|
|
|
|
numberOfItems = 24;
|
2010-07-31 23:46:25 +02:00
|
|
|
break;
|
2010-08-17 15:37:46 +02:00
|
|
|
|
2010-08-01 07:09:30 +02:00
|
|
|
case COMMAND:
|
2011-09-06 20:15:27 +02:00
|
|
|
if (numberOfItems > 6)
|
|
|
|
numberOfItems = 6;
|
2010-08-01 07:09:30 +02:00
|
|
|
break;
|
2010-07-31 23:46:25 +02:00
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2011-09-06 20:15:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make buttons
|
|
|
|
var i;
|
|
|
|
for (i = 0; i < numberOfItems; i++)
|
|
|
|
{
|
|
|
|
var item = items[i];
|
2012-04-20 19:21:04 +02:00
|
|
|
|
|
|
|
// If a tech has been researched it leaves an empty slot
|
|
|
|
if (guiName == RESEARCH && !item)
|
|
|
|
{
|
|
|
|
getGUIObjectByName("unit"+guiName+"Button["+i+"]").hidden = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the entity type and load the template for that type if necessary
|
|
|
|
var entType;
|
2011-09-06 20:15:27 +02:00
|
|
|
var template;
|
2012-04-20 19:21:04 +02:00
|
|
|
switch (guiName)
|
2011-09-06 20:15:27 +02:00
|
|
|
{
|
2012-04-20 19:21:04 +02:00
|
|
|
case QUEUE:
|
|
|
|
// The queue can hold both technologies and units so we need to use the correct code for
|
|
|
|
// loading the templates
|
|
|
|
if (item.unitTemplate)
|
|
|
|
{
|
|
|
|
entType = item.unitTemplate;
|
|
|
|
template = GetTemplateData(entType);
|
|
|
|
}
|
|
|
|
else if (item.technologyTemplate)
|
|
|
|
{
|
|
|
|
entType = item.technologyTemplate;
|
|
|
|
template = GetTechnologyData(entType);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!template)
|
|
|
|
continue; // ignore attempts to use invalid templates (an error should have been
|
|
|
|
// reported already)
|
|
|
|
break;
|
|
|
|
case RESEARCH:
|
|
|
|
entType = item;
|
|
|
|
template = GetTechnologyData(entType);
|
|
|
|
if (!template)
|
|
|
|
continue; // ignore attempts to use invalid templates (an error should have been
|
|
|
|
// reported already)
|
|
|
|
break;
|
|
|
|
case SELECTION:
|
|
|
|
case GARRISON:
|
|
|
|
case TRAINING:
|
|
|
|
case CONSTRUCTION:
|
|
|
|
entType = item;
|
|
|
|
template = GetTemplateData(entType);
|
|
|
|
if (!template)
|
|
|
|
continue; // ignore attempts to use invalid templates (an error should have been
|
|
|
|
// reported already)
|
|
|
|
break;
|
2011-09-06 20:15:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (guiName)
|
|
|
|
{
|
|
|
|
case SELECTION:
|
|
|
|
var name = getEntityName(template);
|
|
|
|
var tooltip = name;
|
|
|
|
var count = g_Selection.groups.getCount(item);
|
|
|
|
getGUIObjectByName("unit"+guiName+"Count["+i+"]").caption = (count > 1 ? count : "");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QUEUE:
|
|
|
|
var tooltip = getEntityName(template);
|
|
|
|
var progress = Math.round(item.progress*100) + "%";
|
|
|
|
getGUIObjectByName("unit"+guiName+"Count["+i+"]").caption = (item.count > 1 ? item.count : "");
|
|
|
|
|
|
|
|
if (i == 0)
|
|
|
|
{
|
|
|
|
getGUIObjectByName("queueProgress").caption = (item.progress ? progress : "");
|
|
|
|
var size = getGUIObjectByName("unit"+guiName+"ProgressSlider["+i+"]").size;
|
|
|
|
size.top = Math.round(item.progress*40);
|
|
|
|
getGUIObjectByName("unit"+guiName+"ProgressSlider["+i+"]").size = size;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GARRISON:
|
|
|
|
var name = getEntityName(template);
|
2012-03-21 16:18:31 +01:00
|
|
|
var tooltip = "Unload " + getEntityName(template) + "\nSingle-click to unload 1. Shift-click to unload all of this type.";
|
2011-09-06 20:15:27 +02:00
|
|
|
var count = garrisonGroups.getCount(item);
|
|
|
|
getGUIObjectByName("unit"+guiName+"Count["+i+"]").caption = (count > 1 ? count : "");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STANCE:
|
|
|
|
case FORMATION:
|
|
|
|
var tooltip = toTitleCase(item);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TRAINING:
|
|
|
|
var tooltip = getEntityNameWithGenericType(template);
|
2011-11-12 23:23:08 +01:00
|
|
|
|
2011-09-06 20:15:27 +02:00
|
|
|
if (template.tooltip)
|
|
|
|
tooltip += "\n[font=\"serif-13\"]" + template.tooltip + "[/font]";
|
|
|
|
|
2012-04-20 19:21:04 +02:00
|
|
|
var [batchSize, batchIncrement] = getTrainingBatchStatus(unitEntState.id, entType);
|
2011-11-12 23:23:08 +01:00
|
|
|
var trainNum = batchSize ? batchSize+batchIncrement : batchIncrement;
|
|
|
|
|
2011-09-06 20:15:27 +02:00
|
|
|
tooltip += "\n" + getEntityCost(template);
|
|
|
|
|
2011-11-12 23:23:08 +01:00
|
|
|
if (template.health)
|
|
|
|
tooltip += "\n[font=\"serif-bold-13\"]Health:[/font] " + template.health;
|
|
|
|
if (template.armour)
|
|
|
|
tooltip += "\n[font=\"serif-bold-13\"]Armour:[/font] " + damageTypesToText(template.armour);
|
|
|
|
if (template.attack)
|
|
|
|
tooltip += "\n" + getEntityAttack(template);
|
|
|
|
if (template.speed)
|
|
|
|
tooltip += "\n" + getEntitySpeed(template);
|
|
|
|
|
|
|
|
tooltip += "\n\n[font=\"serif-bold-13\"]Shift-click[/font][font=\"serif-13\"] to train " + trainNum + ".[/font]";
|
|
|
|
|
2011-09-06 20:15:27 +02:00
|
|
|
break;
|
2012-04-20 19:21:04 +02:00
|
|
|
|
|
|
|
case RESEARCH:
|
|
|
|
var tooltip = getEntityNameWithGenericType(template);
|
|
|
|
|
|
|
|
if (template.tooltip)
|
|
|
|
tooltip += "\n[font=\"serif-13\"]" + template.tooltip + "[/font]";
|
|
|
|
|
|
|
|
tooltip += "\n" + getEntityCost(template);
|
|
|
|
break;
|
2011-09-06 20:15:27 +02:00
|
|
|
|
|
|
|
case CONSTRUCTION:
|
|
|
|
var tooltip = getEntityNameWithGenericType(template);
|
|
|
|
if (template.tooltip)
|
2011-11-12 23:23:08 +01:00
|
|
|
tooltip += "\n[font=\"serif-13\"]" + template.tooltip + "[/font]";
|
2011-09-06 20:15:27 +02:00
|
|
|
|
|
|
|
tooltip += "\n" + getEntityCost(template);
|
2011-11-12 23:23:08 +01:00
|
|
|
|
|
|
|
tooltip += getPopulationBonus(template);
|
|
|
|
if (template.health)
|
|
|
|
tooltip += "\n[font=\"serif-bold-13\"]Health:[/font] " + template.health;
|
|
|
|
|
2011-09-06 20:15:27 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case COMMAND:
|
2011-12-10 08:07:04 +01:00
|
|
|
// here, "item" is an object with properties .name (command name), .tooltip and .icon (relative to session/icons/single)
|
|
|
|
if (item.name == "unload-all")
|
2011-09-06 20:15:27 +02:00
|
|
|
{
|
|
|
|
var count = unitEntState.garrisonHolder.entities.length;
|
|
|
|
getGUIObjectByName("unit"+guiName+"Count["+i+"]").caption = (count > 0 ? count : "");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
getGUIObjectByName("unit"+guiName+"Count["+i+"]").caption = "";
|
|
|
|
}
|
|
|
|
|
2011-12-10 08:07:04 +01:00
|
|
|
tooltip = (item.tooltip ? item.tooltip : toTitleCase(item.name));
|
2011-09-06 20:15:27 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2010-07-01 22:31:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Button
|
|
|
|
var button = getGUIObjectByName("unit"+guiName+"Button["+i+"]");
|
|
|
|
var icon = getGUIObjectByName("unit"+guiName+"Icon["+i+"]");
|
2012-04-09 20:58:03 +02:00
|
|
|
var selection = getGUIObjectByName("unit"+guiName+"Selection["+i+"]");
|
2010-07-01 22:31:47 +02:00
|
|
|
button.hidden = false;
|
|
|
|
button.tooltip = tooltip;
|
|
|
|
|
2011-09-06 20:15:27 +02:00
|
|
|
// Button Function (need nested functions to get the closure right)
|
|
|
|
button.onpress = (function(e){ return function() { callback(e) } })(item);
|
2010-07-01 22:31:47 +02:00
|
|
|
|
2010-08-01 18:25:24 +02:00
|
|
|
// Get icon image
|
2010-09-28 16:19:13 +02:00
|
|
|
if (guiName == "Formation")
|
|
|
|
{
|
2011-05-01 22:40:53 +02:00
|
|
|
var formationOk = Engine.GuiInterfaceCall("CanMoveEntsIntoFormation", {
|
|
|
|
"ents": g_Selection.toList(),
|
|
|
|
"formationName": item
|
|
|
|
});
|
|
|
|
|
2012-04-09 20:58:03 +02:00
|
|
|
var grayscale = "";
|
2011-05-01 22:40:53 +02:00
|
|
|
button.enabled = formationOk;
|
2012-03-21 17:45:02 +01:00
|
|
|
if (!formationOk)
|
|
|
|
{
|
2012-04-09 20:58:03 +02:00
|
|
|
grayscale = "grayscale:";
|
2012-03-21 17:45:02 +01:00
|
|
|
|
|
|
|
// Display a meaningful tooltip why the formation is disabled
|
|
|
|
var requirements = Engine.GuiInterfaceCall("GetFormationRequirements", {
|
|
|
|
"formationName": item
|
|
|
|
});
|
|
|
|
|
|
|
|
button.tooltip += " (disabled)";
|
|
|
|
if (requirements.count > 1)
|
|
|
|
button.tooltip += "\n" + requirements.count + " units required";
|
|
|
|
if (requirements.classesRequired)
|
|
|
|
{
|
|
|
|
button.tooltip += "\nOnly units of type";
|
|
|
|
for each (var classRequired in requirements.classesRequired)
|
|
|
|
{
|
|
|
|
button.tooltip += " " + classRequired;
|
|
|
|
}
|
|
|
|
button.tooltip += " allowed.";
|
|
|
|
}
|
|
|
|
}
|
2012-04-09 20:58:03 +02:00
|
|
|
|
|
|
|
var formationSelected = Engine.GuiInterfaceCall("IsFormationSelected", {
|
|
|
|
"ents": g_Selection.toList(),
|
|
|
|
"formationName": item
|
|
|
|
});
|
|
|
|
|
|
|
|
selection.hidden = !formationSelected;
|
|
|
|
icon.sprite = "stretched:"+grayscale+"session/icons/formations/"+item.replace(/\s+/,'').toLowerCase()+".png";
|
|
|
|
|
2012-03-21 17:45:02 +01:00
|
|
|
}
|
2011-06-18 00:13:39 +02:00
|
|
|
else if (guiName == "Stance")
|
|
|
|
{
|
2011-06-24 14:35:15 +02:00
|
|
|
var stanceSelected = Engine.GuiInterfaceCall("IsStanceSelected", {
|
2011-06-18 00:13:39 +02:00
|
|
|
"ents": g_Selection.toList(),
|
|
|
|
"stance": item
|
|
|
|
});
|
|
|
|
|
2012-04-09 20:58:03 +02:00
|
|
|
selection.hidden = !stanceSelected;
|
|
|
|
icon.sprite = "stretched:session/icons/stances/"+item+".png";
|
2011-06-18 00:13:39 +02:00
|
|
|
}
|
2010-09-28 16:19:13 +02:00
|
|
|
else if (guiName == "Command")
|
2010-08-01 07:09:30 +02:00
|
|
|
{
|
2012-04-10 01:13:45 +02:00
|
|
|
icon.sprite = "stretched:session/icons/" + item.icon;
|
2011-09-06 20:15:27 +02:00
|
|
|
|
2010-08-01 07:09:30 +02:00
|
|
|
}
|
2010-10-23 01:27:42 +02:00
|
|
|
else if (template.icon)
|
|
|
|
{
|
2012-04-20 19:21:04 +02:00
|
|
|
var grayscale = "";
|
|
|
|
button.enabled = true;
|
|
|
|
|
|
|
|
if (template.requiredTechnology && !Engine.GuiInterfaceCall("IsTechnologyResearched", template.requiredTechnology))
|
|
|
|
{
|
|
|
|
button.enabled = false;
|
|
|
|
var techName = getEntityName(GetTechnologyData(template.requiredTechnology));
|
|
|
|
button.tooltip += "\nRequires " + techName;
|
|
|
|
grayscale = "grayscale:";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (guiName == RESEARCH && !Engine.GuiInterfaceCall("CheckTechnologyRequirements", entType))
|
|
|
|
{
|
|
|
|
button.enabled = false;
|
|
|
|
button.tooltip += "\n" + GetTechnologyData(entType).requirementsTooltip;
|
|
|
|
grayscale = "grayscale:";
|
|
|
|
}
|
|
|
|
|
|
|
|
icon.sprite = "stretched:" + grayscale + "session/portraits/" + template.icon;
|
2010-10-23 01:27:42 +02:00
|
|
|
}
|
2010-07-01 22:31:47 +02:00
|
|
|
else
|
2010-08-01 07:09:30 +02:00
|
|
|
{
|
2010-10-23 01:27:42 +02:00
|
|
|
// TODO: we should require all entities to have icons, so this case never occurs
|
|
|
|
icon.sprite = "bkFillBlack";
|
2010-08-01 07:09:30 +02:00
|
|
|
}
|
2010-07-01 22:31:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Position the visible buttons (TODO: if there's lots, maybe they should be squeezed together to fit)
|
|
|
|
var numButtons = i;
|
2010-09-28 16:19:13 +02:00
|
|
|
|
|
|
|
var rowLength = 8;
|
2010-10-04 00:42:15 +02:00
|
|
|
if (guiName == "Selection")
|
2010-10-06 00:44:32 +02:00
|
|
|
rowLength = 4;
|
2010-10-04 00:42:15 +02:00
|
|
|
else if (guiName == "Formation" || guiName == "Garrison" || guiName == "Command")
|
2010-09-28 16:19:13 +02:00
|
|
|
rowLength = 4;
|
|
|
|
|
2010-08-01 02:33:20 +02:00
|
|
|
var numRows = Math.ceil(numButtons / rowLength);
|
2010-07-19 23:23:12 +02:00
|
|
|
var buttonSideLength = getGUIObjectByName("unit"+guiName+"Button[0]").size.bottom;
|
2010-08-01 07:09:30 +02:00
|
|
|
var buttonSpacer = buttonSideLength+1;
|
2010-07-07 15:28:37 +02:00
|
|
|
|
2010-07-19 23:23:12 +02:00
|
|
|
// Layout buttons
|
2010-09-28 16:19:13 +02:00
|
|
|
if (guiName == "Command")
|
2010-09-25 17:22:41 +02:00
|
|
|
{
|
2010-10-04 00:42:15 +02:00
|
|
|
layoutButtonRowCentered(0, guiName, 0, numButtons, COMMANDS_PANEL_WIDTH);
|
2010-09-25 17:22:41 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (var i = 0; i < numRows; i++)
|
|
|
|
layoutButtonRow(i, guiName, buttonSideLength, buttonSpacer, rowLength*i, rowLength*(i+1) );
|
|
|
|
}
|
2011-09-06 20:15:27 +02:00
|
|
|
|
2010-10-04 00:42:15 +02:00
|
|
|
// Resize Queue panel if needed
|
|
|
|
if (guiName == "Queue") // or garrison
|
|
|
|
{
|
|
|
|
var panel = getGUIObjectByName("unitQueuePanel");
|
|
|
|
var size = panel.size;
|
|
|
|
size.top = (UNIT_PANEL_BASE - ((numRows-1)*UNIT_PANEL_HEIGHT));
|
|
|
|
panel.size = size;
|
|
|
|
}
|
2010-07-19 23:23:12 +02:00
|
|
|
|
2010-07-01 22:31:47 +02:00
|
|
|
// Hide any buttons we're no longer using
|
|
|
|
for (i = numButtons; i < g_unitPanelButtons[guiName]; ++i)
|
|
|
|
getGUIObjectByName("unit"+guiName+"Button["+i+"]").hidden = true;
|
|
|
|
|
|
|
|
g_unitPanelButtons[guiName] = numButtons;
|
|
|
|
}
|
|
|
|
|
2012-03-08 21:42:28 +01:00
|
|
|
// Sets up "unit trading panel" - special case for setupUnitPanel
|
2012-04-06 17:07:04 +02:00
|
|
|
function setupUnitTradingPanel(unitEntState, selection)
|
2012-03-08 21:42:28 +01:00
|
|
|
{
|
|
|
|
for (var i = 0; i < TRADING_RESOURCES.length; i++)
|
|
|
|
{
|
|
|
|
var resource = TRADING_RESOURCES[i];
|
|
|
|
var button = getGUIObjectByName("unitTradingButton["+i+"]");
|
|
|
|
button.size = (i * 46) + " 0 " + ((i + 1) * 46) + " 46";
|
2012-04-06 17:07:04 +02:00
|
|
|
var selectTradingPreferredGoodsData = { "entities": selection, "preferredGoods": resource };
|
2012-03-08 21:42:28 +01:00
|
|
|
button.onpress = (function(e){ return function() { selectTradingPreferredGoods(e); } })(selectTradingPreferredGoodsData);
|
|
|
|
button.enabled = true;
|
|
|
|
button.tooltip = "Set " + resource + " as trading goods";
|
|
|
|
var icon = getGUIObjectByName("unitTradingIcon["+i+"]");
|
|
|
|
var preferredGoods = unitEntState.trader.preferredGoods;
|
2012-04-09 20:58:03 +02:00
|
|
|
var selected = getGUIObjectByName("unitTradingSelection["+i+"]");
|
|
|
|
selected.hidden = !(resource == preferredGoods);
|
2012-04-09 00:56:11 +02:00
|
|
|
var grayscale = (resource != preferredGoods) ? "grayscale:" : "";
|
2012-04-09 20:58:03 +02:00
|
|
|
icon.sprite = "stretched:"+grayscale+"session/icons/resources/" + resource + ".png";
|
2012-03-08 21:42:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-24 16:43:32 +01:00
|
|
|
// Sets up "unit barter panel" - special case for setupUnitPanel
|
|
|
|
function setupUnitBarterPanel(unitEntState)
|
|
|
|
{
|
|
|
|
// Amount of player's resource to exchange
|
|
|
|
var amountToSell = BARTER_RESOURCE_AMOUNT_TO_SELL;
|
|
|
|
if (Engine.HotkeyIsPressed("session.massbarter"))
|
|
|
|
amountToSell *= BARTER_BUNCH_MULTIPLIER;
|
|
|
|
// One pass for each resource
|
|
|
|
for (var i = 0; i < BARTER_RESOURCES.length; i++)
|
|
|
|
{
|
|
|
|
var resource = BARTER_RESOURCES[i];
|
|
|
|
// One pass for 'sell' row and another for 'buy'
|
|
|
|
for (var j = 0; j < 2; j++)
|
|
|
|
{
|
|
|
|
var action = BARTER_ACTIONS[j];
|
|
|
|
|
2012-04-09 20:58:03 +02:00
|
|
|
if (j == 0)
|
|
|
|
{
|
|
|
|
// Display the selection overlay
|
|
|
|
var selection = getGUIObjectByName("unitBarter" + action + "Selection["+i+"]");
|
|
|
|
selection.hidden = !(i == g_barterSell);
|
|
|
|
}
|
|
|
|
|
2012-04-09 00:56:11 +02:00
|
|
|
// We gray out the not selected icons in 'sell' row
|
|
|
|
var grayscale = (j == 0 && i != g_barterSell) ? "grayscale:" : "";
|
2011-11-24 16:43:32 +01:00
|
|
|
var icon = getGUIObjectByName("unitBarter" + action + "Icon["+i+"]");
|
|
|
|
|
|
|
|
var button = getGUIObjectByName("unitBarter" + action + "Button["+i+"]");
|
|
|
|
button.size = (i * 46) + " 0 " + ((i + 1) * 46) + " 46";
|
|
|
|
var amountToBuy;
|
2012-04-15 18:27:44 +02:00
|
|
|
// We don't display a button in 'buy' row if the same resource is selected in 'sell' row
|
2011-11-24 16:43:32 +01:00
|
|
|
if (j == 1 && i == g_barterSell)
|
|
|
|
{
|
2012-04-15 05:44:57 +02:00
|
|
|
button.hidden = true;
|
2011-11-24 16:43:32 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-04-15 05:44:57 +02:00
|
|
|
button.hidden = false;
|
2011-11-24 16:43:32 +01:00
|
|
|
button.tooltip = action + " " + resource;
|
2012-04-09 20:58:03 +02:00
|
|
|
icon.sprite = "stretched:"+grayscale+"session/icons/resources/" + resource + ".png";
|
2011-11-24 16:43:32 +01:00
|
|
|
var sellPrice = unitEntState.barterMarket.prices["sell"][BARTER_RESOURCES[g_barterSell]];
|
|
|
|
var buyPrice = unitEntState.barterMarket.prices["buy"][resource];
|
|
|
|
amountToBuy = "+" + Math.round(sellPrice / buyPrice * amountToSell);
|
|
|
|
}
|
|
|
|
var amount;
|
|
|
|
if (j == 0)
|
|
|
|
{
|
2012-04-08 09:27:56 +02:00
|
|
|
button.onpress = (function(i){ return function() { g_barterSell = i; } })(i);
|
2011-11-24 16:43:32 +01:00
|
|
|
amount = (i == g_barterSell) ? "-" + amountToSell : "";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-04-08 09:27:56 +02:00
|
|
|
var exchangeResourcesParameters = { "sell": BARTER_RESOURCES[g_barterSell], "buy": BARTER_RESOURCES[i], "amount": amountToSell };
|
|
|
|
button.onpress = (function(exchangeResourcesParameters){ return function() { exchangeResources(exchangeResourcesParameters); } })(exchangeResourcesParameters);
|
2011-11-24 16:43:32 +01:00
|
|
|
amount = amountToBuy;
|
|
|
|
}
|
|
|
|
getGUIObjectByName("unitBarter" + action + "Amount["+i+"]").caption = amount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-03 18:51:20 +02:00
|
|
|
// Updates right Unit Commands Panel - runs in the main session loop via updateSelectionDetails()
|
2010-09-28 16:19:13 +02:00
|
|
|
function updateUnitCommands(entState, supplementalDetailsPanel, commandsPanel, selection)
|
2010-07-01 22:31:47 +02:00
|
|
|
{
|
|
|
|
// Panels that are active
|
|
|
|
var usedPanels = {};
|
|
|
|
|
|
|
|
// If the selection is friendly units, add the command panels
|
|
|
|
var player = Engine.GetPlayerID();
|
|
|
|
if (entState.player == player || g_DevSettings.controlAll)
|
|
|
|
{
|
2010-09-28 16:19:13 +02:00
|
|
|
if (selection.length > 1)
|
|
|
|
setupUnitPanel("Selection", usedPanels, entState, g_Selection.groups.getTemplateNames(),
|
|
|
|
function (entType) { changePrimarySelectionGroup(entType); } );
|
|
|
|
|
2010-09-28 20:31:40 +02:00
|
|
|
var commands = getEntityCommandsList(entState);
|
|
|
|
if (commands.length)
|
|
|
|
setupUnitPanel("Command", usedPanels, entState, commands,
|
2011-12-10 08:07:04 +01:00
|
|
|
function (item) { performCommand(entState.id, item.name); } );
|
2010-09-28 16:19:13 +02:00
|
|
|
|
2010-10-24 00:43:15 +02:00
|
|
|
if (entState.garrisonHolder)
|
|
|
|
{
|
|
|
|
var groups = new EntityGroups();
|
|
|
|
groups.add(entState.garrisonHolder.entities);
|
|
|
|
setupUnitPanel("Garrison", usedPanels, entState, groups.getTemplateNames(),
|
2012-03-21 16:18:31 +01:00
|
|
|
function (item) { unload(entState.id, groups.getEntsByName(item)); } );
|
2010-10-24 00:43:15 +02:00
|
|
|
}
|
2010-10-13 23:40:56 +02:00
|
|
|
|
2010-09-28 16:19:13 +02:00
|
|
|
var formations = getEntityFormationsList(entState);
|
2012-03-18 22:16:30 +01:00
|
|
|
if (hasClass(entState, "Unit") && !hasClass(entState, "Animal") && !entState.garrisonHolder && formations.length)
|
2011-06-24 14:35:15 +02:00
|
|
|
{
|
2010-09-28 16:19:13 +02:00
|
|
|
setupUnitPanel("Formation", usedPanels, entState, formations,
|
|
|
|
function (item) { performFormation(entState.id, item); } );
|
2011-06-24 14:35:15 +02:00
|
|
|
}
|
2010-10-13 23:40:56 +02:00
|
|
|
|
2011-06-24 14:35:15 +02:00
|
|
|
// TODO: probably should load the stance list from a data file,
|
|
|
|
// and/or vary depending on what units are selected
|
2012-03-21 17:45:02 +01:00
|
|
|
var stances = ["violent", "aggressive", "passive", "defensive", "standground"];
|
2012-03-18 22:16:30 +01:00
|
|
|
if (hasClass(entState, "Unit") && !hasClass(entState, "Animal") && !entState.garrisonHolder && stances.length)
|
2011-06-24 14:35:15 +02:00
|
|
|
{
|
2011-06-18 00:13:39 +02:00
|
|
|
setupUnitPanel("Stance", usedPanels, entState, stances,
|
|
|
|
function (item) { performStance(entState.id, item); } );
|
2011-06-24 14:35:15 +02:00
|
|
|
}
|
2011-06-18 00:13:39 +02:00
|
|
|
|
2011-11-24 16:43:32 +01:00
|
|
|
getGUIObjectByName("unitBarterPanel").hidden = !entState.barterMarket;
|
|
|
|
if (entState.barterMarket)
|
|
|
|
{
|
|
|
|
usedPanels["Barter"] = 1;
|
|
|
|
setupUnitBarterPanel(entState);
|
|
|
|
}
|
|
|
|
|
2010-07-01 22:31:47 +02:00
|
|
|
if (entState.buildEntities && entState.buildEntities.length)
|
2010-09-28 20:31:40 +02:00
|
|
|
{
|
2010-07-31 23:46:25 +02:00
|
|
|
setupUnitPanel("Construction", usedPanels, entState, entState.buildEntities, startBuildingPlacement);
|
2010-09-28 20:31:40 +02:00
|
|
|
}
|
2012-04-20 19:21:04 +02:00
|
|
|
|
|
|
|
if (entState.production && entState.production.entities.length)
|
|
|
|
{
|
|
|
|
setupUnitPanel("Training", usedPanels, entState, entState.production.entities,
|
2012-04-21 02:21:01 +02:00
|
|
|
function (trainEntType) { addTrainingToQueue(selection, trainEntType); } );
|
2012-04-20 19:21:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (entState.production && entState.production.technologies.length)
|
2010-09-28 20:31:40 +02:00
|
|
|
{
|
2012-04-20 19:21:04 +02:00
|
|
|
setupUnitPanel("Research", usedPanels, entState, entState.production.technologies,
|
|
|
|
function (researchType) { addResearchToQueue(entState.id, researchType); } );
|
2010-09-28 20:31:40 +02:00
|
|
|
}
|
2010-07-01 22:31:47 +02:00
|
|
|
|
2012-04-20 19:21:04 +02:00
|
|
|
if (entState.production && entState.production.queue.length)
|
|
|
|
setupUnitPanel("Queue", usedPanels, entState, entState.production.queue,
|
|
|
|
function (item) { removeFromProductionQueue(entState.id, item.id); } );
|
2010-08-01 07:09:30 +02:00
|
|
|
|
2012-03-08 21:42:28 +01:00
|
|
|
if (entState.trader)
|
|
|
|
{
|
|
|
|
usedPanels["Trading"] = 1;
|
2012-04-06 17:07:04 +02:00
|
|
|
setupUnitTradingPanel(entState, selection);
|
2012-03-08 21:42:28 +01:00
|
|
|
}
|
2010-07-01 22:31:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Hides / unhides Unit Panels (panels should be grouped by type, not by order, but we will leave that for another time)
|
|
|
|
var offset = 0;
|
|
|
|
for each (var panelName in g_unitPanels)
|
|
|
|
{
|
|
|
|
var panel = getGUIObjectByName("unit" + panelName + "Panel");
|
|
|
|
if (usedPanels[panelName])
|
|
|
|
panel.hidden = false;
|
|
|
|
else
|
|
|
|
panel.hidden = true;
|
|
|
|
}
|
|
|
|
}
|
2010-10-25 01:40:09 +02:00
|
|
|
|
|
|
|
// Force hide commands panels
|
|
|
|
function hideUnitCommands()
|
|
|
|
{
|
|
|
|
for each (var panelName in g_unitPanels)
|
|
|
|
getGUIObjectByName("unit" + panelName + "Panel").hidden = true;
|
2011-05-01 22:40:53 +02:00
|
|
|
}
|