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";
|
|
|
|
const CONSTRUCTION = "Construction";
|
2010-08-01 07:09:30 +02:00
|
|
|
const COMMAND = "Command";
|
2010-07-31 23:46:25 +02:00
|
|
|
|
2010-07-31 00:19:43 +02:00
|
|
|
// Constants used by the Queue or Garrison panel
|
2010-09-28 16:19:13 +02:00
|
|
|
//const UNIT_PANEL_BASE = -48; // The offset above the main panel (will often be negative)
|
|
|
|
//const UNIT_PANEL_HEIGHT = 41; // The height needed for a row of buttons
|
2010-07-19 23:23:12 +02:00
|
|
|
|
2010-07-01 22:31:47 +02:00
|
|
|
// The number of currently visible buttons (used to optimise showing/hiding)
|
2010-09-28 16:19:13 +02:00
|
|
|
var g_unitPanelButtons = {"Selection": 0, "Queue": 0, "Formation": 0, "Garrison": 0, "Training": 0, "Construction": 0, "Command": 0};
|
2010-07-01 22:31:47 +02:00
|
|
|
|
|
|
|
// Unit panels are panels with row(s) of buttons
|
2010-09-28 16:19:13 +02:00
|
|
|
var g_unitPanels = ["Selection", "Queue", "Formation", "Garrison", "Training", "Construction", "Research", "Stance", "Command"];
|
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;
|
|
|
|
|
|
|
|
size.left = buttonSpacer*colNumber;
|
|
|
|
size.right = buttonSpacer*colNumber + buttonSideLength;
|
|
|
|
size.top = buttonSpacer*rowNumber;
|
|
|
|
size.bottom = buttonSpacer*rowNumber + buttonSideLength;
|
|
|
|
|
|
|
|
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;
|
2010-07-31 00:19:43 +02:00
|
|
|
var selection = g_Selection.toList();
|
2010-07-31 23:46:25 +02:00
|
|
|
var numberOfItems = items.length;
|
2010-09-25 17:22:41 +02:00
|
|
|
|
2010-09-28 16:19:13 +02:00
|
|
|
if (guiName == "Selection" || guiName == "Formation" || guiName == "Garrison")
|
|
|
|
{
|
|
|
|
if (numberOfItems > 15)
|
|
|
|
numberOfItems = 15;
|
|
|
|
}
|
|
|
|
else if (guiName == "Queue")
|
|
|
|
{
|
|
|
|
if (numberOfItems > 16)
|
|
|
|
numberOfItems = 16;
|
|
|
|
}
|
|
|
|
else if ( guiName == "Command")
|
|
|
|
{
|
|
|
|
if (numberOfItems > 4)
|
|
|
|
numberOfItems = 4;
|
|
|
|
}
|
2010-09-25 17:22:41 +02:00
|
|
|
else if (numberOfItems > 18)
|
2010-09-28 16:19:13 +02:00
|
|
|
{
|
|
|
|
numberOfItems = 18;
|
|
|
|
}
|
2010-07-31 23:46:25 +02:00
|
|
|
|
|
|
|
var i;
|
|
|
|
for (i = 0; i < numberOfItems; i++)
|
2010-07-01 22:31:47 +02:00
|
|
|
{
|
2010-07-31 23:46:25 +02:00
|
|
|
var item = items[i];
|
2010-08-03 03:54:16 +02:00
|
|
|
var entType = ((guiName == "Queue")? item.template : item);
|
|
|
|
var template;
|
2010-09-28 16:19:13 +02:00
|
|
|
if (guiName != "Formation" && guiName != "Command")
|
2010-07-01 22:31:47 +02:00
|
|
|
{
|
2010-08-28 23:12:52 +02:00
|
|
|
template = GetTemplateData(entType);
|
2010-08-01 18:25:24 +02:00
|
|
|
if (!template)
|
|
|
|
continue; // ignore attempts to use invalid templates (an error should have been reported already)
|
2010-08-03 03:54:16 +02:00
|
|
|
}
|
2010-08-17 15:37:46 +02:00
|
|
|
|
2010-08-03 03:54:16 +02:00
|
|
|
switch (guiName)
|
|
|
|
{
|
|
|
|
case SELECTION:
|
2010-09-28 16:19:13 +02:00
|
|
|
var tooltip = getEntityName(template);
|
2010-09-25 17:22:41 +02:00
|
|
|
var count = g_Selection.groups.getCount(item);
|
|
|
|
getGUIObjectByName("unit"+guiName+"Count["+i+"]").caption = (count > 1 ? count : "");
|
2010-07-31 23:46:25 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case QUEUE:
|
2010-08-01 18:25:24 +02:00
|
|
|
var tooltip = getEntityName(template);
|
2010-07-01 22:31:47 +02:00
|
|
|
var progress = Math.round(item.progress*100) + "%";
|
|
|
|
getGUIObjectByName("unit"+guiName+"Count["+i+"]").caption = (item.count > 1 ? item.count : "");
|
2010-09-28 16:19:13 +02:00
|
|
|
|
|
|
|
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 tooltip = getEntityName(template);
|
|
|
|
var count = g_Selection.groups.getCount(item);
|
|
|
|
getGUIObjectByName("unit"+guiName+"Count["+i+"]").caption = (count > 1 ? count : "");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FORMATION:
|
|
|
|
var tooltip = toTitleCase(item);
|
2010-07-31 23:46:25 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TRAINING:
|
2010-08-17 02:55:02 +02:00
|
|
|
var tooltip = getEntityNameWithGenericType(template);
|
|
|
|
if (template.tooltip)
|
|
|
|
tooltip += "\n[font=\"serif-13\"]" + template.tooltip + "[/font]";
|
|
|
|
|
|
|
|
tooltip += "\n" + getEntityCost(template);
|
|
|
|
|
2010-08-03 03:54:16 +02:00
|
|
|
var [batchSize, batchIncrement] = getTrainingQueueBatchStatus(unitEntState.id, entType);
|
2010-07-31 23:46:25 +02:00
|
|
|
if (batchSize)
|
2010-07-01 22:31:47 +02:00
|
|
|
{
|
2010-07-31 23:46:25 +02:00
|
|
|
tooltip += "\n[font=\"serif-13\"]Training [font=\"serif-bold-13\"]" + batchSize + "[font=\"serif-13\"] units; " +
|
|
|
|
"Shift-click to train [font=\"serif-bold-13\"]"+ (batchSize+batchIncrement) + "[font=\"serif-13\"] units[/font]";
|
2010-07-01 22:31:47 +02:00
|
|
|
}
|
2010-07-31 23:46:25 +02:00
|
|
|
break;
|
2010-08-17 15:37:46 +02:00
|
|
|
|
2010-07-31 23:46:25 +02:00
|
|
|
case CONSTRUCTION:
|
2010-08-17 02:55:02 +02:00
|
|
|
var tooltip = getEntityNameWithGenericType(template);
|
|
|
|
if (template.tooltip)
|
|
|
|
tooltip += "\n[font=\"serif-13\"]" + template.tooltip + "[/font]";
|
2010-08-17 15:37:46 +02:00
|
|
|
|
2010-08-17 02:55:02 +02:00
|
|
|
tooltip += "\n" + getEntityCost(template);
|
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:
|
2010-08-01 18:25:24 +02:00
|
|
|
tooltip = toTitleCase(item);
|
2010-08-01 07:09:30 +02:00
|
|
|
break;
|
2010-07-31 23:46:25 +02:00
|
|
|
|
|
|
|
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+"]");
|
|
|
|
button.hidden = false;
|
|
|
|
button.tooltip = tooltip;
|
|
|
|
|
2010-07-31 00:19:43 +02:00
|
|
|
// Button Function
|
2010-09-25 17:22:41 +02:00
|
|
|
button.onpress = (function(e) { return function() { callback(e) } })(item); // (need nested functions to get the closure right)
|
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")
|
|
|
|
{
|
|
|
|
icon.cell_id = getFormationCellId(item);
|
|
|
|
icon.enabled = false;
|
|
|
|
if (!icon.enabled)
|
|
|
|
icon.sprite = "formation_disabled";
|
|
|
|
}
|
|
|
|
else if (guiName == "Command")
|
2010-08-01 07:09:30 +02:00
|
|
|
{
|
2010-08-03 03:54:16 +02:00
|
|
|
//icon.cell_id = i;
|
|
|
|
icon.cell_id = getCommandCellId(item);
|
2010-08-01 07:09:30 +02:00
|
|
|
}
|
2010-07-01 22:31:47 +02:00
|
|
|
else
|
2010-08-01 07:09:30 +02:00
|
|
|
{
|
|
|
|
icon.sprite = template.icon_sheet;
|
|
|
|
icon.cell_id = ((typeof template.icon_cell == "undefined")? 0 : template.icon_cell);
|
|
|
|
}
|
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;
|
|
|
|
if (guiName == "Selection" || guiName == "Formation" || guiName == "Garrison")
|
|
|
|
rowLength = 5;
|
|
|
|
else if (guiName == "Command")
|
|
|
|
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-09-28 16:19:13 +02:00
|
|
|
/*
|
2010-07-19 23:23:12 +02:00
|
|
|
// Resize Queue panel if needed
|
|
|
|
if (guiName == "Queue") // or garrison
|
2010-07-03 18:51:20 +02:00
|
|
|
{
|
2010-07-19 23:23:12 +02:00
|
|
|
var panel = getGUIObjectByName("unitQueuePanel");
|
|
|
|
var size = panel.size;
|
2010-07-31 00:19:43 +02:00
|
|
|
size.top = (UNIT_PANEL_BASE - ((numRows-1)*UNIT_PANEL_HEIGHT));
|
2010-07-19 23:23:12 +02:00
|
|
|
panel.size = size;
|
2010-07-03 18:51:20 +02:00
|
|
|
}
|
2010-09-28 16:19:13 +02:00
|
|
|
*/
|
2010-07-03 18:51:20 +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-09-28 16:19:13 +02:00
|
|
|
layoutButtonRowCentered(0, guiName, 0, numButtons, 222);
|
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) );
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
if (entState.attack) // TODO - this should be based on some AI properties
|
|
|
|
{
|
|
|
|
//usedPanels["Stance"] = 1;
|
|
|
|
//usedPanels["Formation"] = 1;
|
|
|
|
// (These are disabled since they're not implemented yet)
|
|
|
|
}
|
|
|
|
else // TODO - this should be based on various other things
|
|
|
|
{
|
|
|
|
//usedPanels["Research"] = 1;
|
|
|
|
}
|
|
|
|
|
2010-09-28 16:19:13 +02:00
|
|
|
if (selection.length > 1)
|
|
|
|
setupUnitPanel("Selection", usedPanels, entState, g_Selection.groups.getTemplateNames(),
|
|
|
|
function (entType) { changePrimarySelectionGroup(entType); } );
|
|
|
|
|
|
|
|
if (entState.training && entState.training.queue.length)
|
|
|
|
setupUnitPanel("Queue", usedPanels, entState, entState.training.queue,
|
|
|
|
function (item) { removeFromTrainingQueue(entState.id, item.id); } );
|
|
|
|
|
|
|
|
/*
|
|
|
|
if (selection.length > 1)
|
|
|
|
setupUnitPanel("Garrison", usedPanels, entState, g_Selection.groups.getTemplateNames(),
|
|
|
|
function (entType) { changePrimarySelectionGroup(entType); } );
|
|
|
|
*/
|
|
|
|
|
|
|
|
var formations = getEntityFormationsList(entState);
|
|
|
|
if (formations.length)
|
|
|
|
setupUnitPanel("Formation", usedPanels, entState, formations,
|
|
|
|
function (item) { performFormation(entState.id, item); } );
|
|
|
|
|
2010-07-01 22:31:47 +02:00
|
|
|
if (entState.buildEntities && entState.buildEntities.length)
|
2010-07-31 23:46:25 +02:00
|
|
|
setupUnitPanel("Construction", usedPanels, entState, entState.buildEntities, startBuildingPlacement);
|
2010-07-01 22:31:47 +02:00
|
|
|
|
|
|
|
if (entState.training && entState.training.entities.length)
|
2010-07-31 23:46:25 +02:00
|
|
|
setupUnitPanel("Training", usedPanels, entState, entState.training.entities,
|
2010-07-01 22:31:47 +02:00
|
|
|
function (trainEntType) { addToTrainingQueue(entState.id, trainEntType); } );
|
|
|
|
|
2010-08-06 02:29:58 +02:00
|
|
|
var commands = getEntityCommandsList(entState);
|
2010-08-01 07:09:30 +02:00
|
|
|
if (commands.length)
|
|
|
|
setupUnitPanel("Command", usedPanels, entState, commands,
|
|
|
|
function (item) { performCommand(entState.id, item); } );
|
|
|
|
|
2010-09-28 16:19:13 +02:00
|
|
|
supplementalDetailsPanel.hidden = false;
|
2010-07-01 22:31:47 +02:00
|
|
|
commandsPanel.hidden = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-09-25 17:22:41 +02:00
|
|
|
getGUIObjectByName("stamina").hidden = true;
|
2010-09-28 16:19:13 +02:00
|
|
|
supplementalDetailsPanel.hidden = true;
|
2010-07-01 22:31:47 +02:00
|
|
|
commandsPanel.hidden = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|