1
0
forked from 0ad/0ad

Production queue GUI interface.

Some refactoring of updateTab() function.
Disabling some unimplemented buttons.

This was SVN commit r6418.
This commit is contained in:
freenity 2008-10-11 20:46:40 +00:00
parent dba26306e0
commit bcfbf972ed
2 changed files with 507 additions and 363 deletions

View File

@ -12,6 +12,8 @@ function defineCommandButtons(command)
snStatusPaneCommand.list = new Object();
snStatusPaneCommand.button = new Object();
empty = false;
// Maximum number of buttons (either single or lists).
snStatusPaneCommand.tab.max = command.substring (command.lastIndexOf ("d")+1, command.lastIndexOf ("_")); // 8
// Maximum number of entries in a list.
@ -153,27 +155,32 @@ function updateTab (tab, type, cellSheet, attribute, attribute2, arrayCells)
// * pick: Usually a group that's the same as the tab name, so use this.
// * command: Tends to be "Command" (though not always), so defaults to this.
// * If no value is specified, it will use the above default for the type.
// attribute: * For production & pick: the attribute containing the list of items to display (eg selection[0].actions.create.list.research), which this entity must
// attribute: * For production & pick: the attribute containing the list of items to display
// (eg selection[0].actions.create.list.research), which this entity must
// have in order to update the tab with this list.
// * For command: the attribute that the entity must have in order to have this command.
// * If no value is specified, all entities have this attribute.
// attribute2: * For pick: The variable used to store the current item in the pick that has been selected -- placed in the tab (eg selection[0].actions.formation.curr)
// arrayCells: * Optional attribute. If true, assume that items cannot all be found in the same cellSheet; check in attribute.sheet for cell sheet name for each item.
// attribute2: * For pick: The variable used to store the current item in the pick that has been selected -- placed in the tab
// (eg selection[0].actions.formation.curr)
// arrayCells: * Optional attribute. If true, assume that items cannot all be found in the same cellSheet; check in attribute.sheet
// for cell sheet name for each item.
// Store string form of attribute for future reference.
attributeString = attribute;
attribute2String = attribute2;
// If either attribute is not a valid property, return false.
if ( (validProperty (attributeString)) && (validProperty (attribute2String)) )
if (! ((validProperty (attributeString)) && (validProperty (attribute2String))) )
{
return false;
}
// Use the value of the attribute from this point forwards.
attribute = eval (attribute);
attribute2 = eval (attribute2);
// String properties taken from entities can be a little confused about their variable type, so make sure they're considered strings.
attribute2 = String (attribute2);
//console.write ("1st: " + tabCounter + " " + tab + " " + type + " " + cellSheet + " " + attribute + " " + attribute2);
// Set default values.
if (cellSheet == "")
@ -194,103 +201,17 @@ function updateTab (tab, type, cellSheet, attribute, attribute2, arrayCells)
}
}
//console.write ("2nd: " + tabCounter + " " + tab + " " + type + " " + cellSheet + " " + attribute + " " + attribute2);
// Get tab.
var tabObject = getGUIObjectByName ("snStatusPaneCommand" + tabCounter + "_1");
// Enable tab.
guiUnHide (tabObject.name);
// Set tab portrait.
switch (tab)
{
case "selection":
case "garrison":
// Temporarily (until we have the tab textures) force a particular cell ID for selection and garrison so we can see the icon.
cellGroup[cellSheet][tab].id = 2;
// Use the horizontal tab. (Extends right.)
setPortrait ("snStatusPaneCommand" + tabCounter + "_1", "IconSheet", cellSheet + "Button_H", cellGroup[cellSheet][tab].id);
break;
default:
if (type == "pick" && cellSheet != "Tab") // Force the tab background for pick lists,
setPortrait ("snStatusPaneCommand" + tabCounter + "_1", "IconSheet", cellSheet + "TabButton", cellGroup[cellSheet][tab].id);
else
setPortrait ("snStatusPaneCommand" + tabCounter + "_1", "IconSheet", cellSheet + "Button", cellGroup[cellSheet][tab].id);
break;
}
setTabPortrait(type, tab, cellSheet, cellGroup, tabCounter);
//console.write ("3rd: " + "snStatusPaneCommand" + tabCounter + "_1" + "|" + "IconSheet" + "|" + cellSheet + "Button" + "|" + cellGroup[cellSheet][tab].id);
setTooltip(type, tab, cellSheet, cellGroup, tabObject);
switch (type)
{
case "command":
// Set tab tooltip.
var tooltip = cellGroup[cellSheet][tab].name;
tooltip += " " + cellSheet;
break;
case "pick":
// Set tab tooltip.
var tooltip = cellSheet;
tooltip += " List\nCurrent " + cellSheet + ": " + cellGroup[cellSheet][tab].name;
break;
default:
// Set tab tooltip.
var tooltip = cellGroup[cellSheet][tab].name;
tooltip += " " + cellSheet;
break;
}
tabObject.tooltip = tooltip;
switch (type)
{
case "command":
// An array of actions for the command "tabs" can be triggered by clicking the individual commands. We'll set them here.
switch(tab)
{
case "rally":
tabObject.onPress = function (event)
{
setCursor("cursor-rally");
}
break;
default:
tabObject.onPress = function (event)
{
}
break;
}
break;
default:
// Set tab function when user moves mouse over tab.
tabObject.onMouseEnter = function (event)
{
var currTab = this.name.substring (this.name.lastIndexOf ("d")+1, this.name.lastIndexOf ("_"));
// Seek through all list tabs. (Only the one being hovered should stay open.)
for (var i = 1; i < snStatusPaneCommand.split; i++)
{
// If we've found the current tab,
if (i == currTab)
{
// Click the tab button to toggle visibility of its list.
guiToggle ( "snStatusPaneCommandGroup" + currTab );
}
else // If it's another list tab,
{
// Ensure this tab is hidden.
guiHide ("snStatusPaneCommandGroup" + i);
}
}
}
// Set tab function when user clicks tab.
tabObject.onPress = function (event)
{
// Click the tab button to toggle visibility of its list.
guiToggle ( "snStatusPaneCommandGroup" + this.name.substring (this.name.lastIndexOf ("d")+1, this.name.lastIndexOf ("_")) );
}
break;
}
setEvents(type, tab, tabObject);
// Get group.
var groupObject = getGUIObjectByName ("snStatusPaneCommand" + "Group" + tabCounter);
@ -298,38 +219,28 @@ function updateTab (tab, type, cellSheet, attribute, attribute2, arrayCells)
// If the list hasn't been hidden (tab is open), and it should have list items (it's not merely a command),
if ( type != "command" && attribute != undefined )
{
// Reset array.
var listArray = [];
// Insert blank array element at location 0 (to buffer array so items are in 1..n range).
listArray[0] = "";
if (tab == "queue")
{
var newAttribute = groupProductionItems(attribute);
//ok, now split newAttribute into 2 arrays: 1 containing items, and another one containing nums.
// Extract item list into an array.
if (!attribute.length)
{ // If it's a list where each element is a value, (entity list)
for ( var i in attribute )
var nums = new Array();
attribute = new Array();
for (i=0; i<newAttribute.length; i++)
{
listArray[listArray.length] = i;
}
}
else
{ // If it's a list where each element is part of a numbered array, (array list)
for ( var i = 0; i < attribute.length; i++ )
{
listArray[listArray.length] = attribute[i];
// If cell sheet for each item is stored in attribute.sheet, transfer that across too.
if (arrayCells == true)
{
//console.write (attribute[i].sheet);
listArray[listArray.length].sheet = new Object(attribute[i].sheet);
//console.write (listArray[listArray.length].sheet);
}
attribute.push(newAttribute[i].item);
nums.push(newAttribute[i].num);
}
}
listArray = extractItemList(attribute, arrayCells);
// Populate the buttons in this tab's list.
for (var createLoop = 1; createLoop < snStatusPaneCommand.list.max; createLoop++)
{
// (Skip research list for the moment, since we don't have any portraits for techs. But we should remove the research condition later.)
// (Skip research list for the moment, since we don't have any portraits for techs.
// But we should remove the research condition later.)
if (createLoop < listArray.length && type != "command")
{
// Get name of current button.
@ -339,10 +250,14 @@ function updateTab (tab, type, cellSheet, attribute, attribute2, arrayCells)
{
case "pick":
// Set tooltip.
if (arrayCells == true) { // Check a .sheet property for each item in the list to know its cellsheet.
if (arrayCells == true)
{
// Check a .sheet property for each item in the list to know its cellsheet.
listObject.tooltip = cellGroup[listArray[createLoop].sheet][listArray[createLoop]].name;
}
else { // Assume each item uses the same cell sheet as the tab.
else
{
// Assume each item uses the same cell sheet as the tab.
if(cellGroup[cellSheet][listArray[createLoop]]) {
listObject.tooltip = cellGroup[cellSheet][listArray[createLoop]].name;
}
@ -353,8 +268,8 @@ function updateTab (tab, type, cellSheet, attribute, attribute2, arrayCells)
// Set portrait.
if(cellGroup[cellSheet][listArray[createLoop]]) {
setPortrait (listObject.name,
"IconSheet", cellSheet + "Button", cellGroup[cellSheet][listArray[createLoop]].id);
setPortrait (listObject.name, "IconSheet",
cellSheet + "Button", cellGroup[cellSheet][listArray[createLoop]].id);
}
// Store properties which we will need when doing the press event as a comma-delimited string.
@ -366,9 +281,9 @@ function updateTab (tab, type, cellSheet, attribute, attribute2, arrayCells)
// (Storing the properties in the hotkey is an evil hack, and we'll eventually need the hotkey, but since new properties cannot be
// attached to controls, there doesn't seem to be much choice ... Maybe add a .container property for situations such as this?)
listObject.hotkey = attribute2String + "," + listArray[createLoop] + "," + attributeString + "," + cellSheet.toLowerCase() + "," + tabCounter + ",";
// console.write (listObject.caption);
// Set item function.
listObject.onPress = function (event)
{
// Extract the properties from the comma-delimited string.
@ -408,104 +323,33 @@ function updateTab (tab, type, cellSheet, attribute, attribute2, arrayCells)
break;
default:
var itemName = "?";
//console.write(listArray[createLoop]);
switch (tab)
if (tab == "selection" || tab == "garrison" || tab == "research" || tab == "queue")
{
case "selection":
case "garrison":
case "research":
// Get name of item to display in list.
// (These already know the full name of the entity tag, so we don't prefix it.)
itemName = listArray[createLoop];
break;
default:
}
else
{
// Get name of item to display in list.
itemName = selection[0].traits.id.civ_code + "_" + listArray[createLoop];
}
setItemTooltip(tab, itemName, listObject, selection);
setItemFunction(tab, listObject, selection);
break;
}
//console.write ("4th: " + "snStatusPaneCommand" + tabCounter + "_1" + "|" + "IconSheet" + "|" + cellSheet + "Button" + "|" + createLoop + "|" + listArray[createLoop]);
switch (tab)
//Only for queue tab: write the number of queued similar items.
if (tab == "queue")
{
case "research":
// Store name of tech to display in list in this button's coordinate.
var tech = getTechnology(itemName, selection[0].player);
Crd[getCrd (listObject.name, true)].entity = tech;
if(!tech) {
listObject.tooltip = "Bad tech: " + itemName;
listObject.caption = "[font=trebuchet14b] " + parseInt(attribute[createLoop-1].progress * 100) + "%[/font]";
listObject.caption += "\n[font=trebuchet14b] (" + nums[createLoop-1] + ")[/font]";
}
else {
// Set tooltip.
listObject.tooltip = Crd[getCrd (listObject.name, true)].entity.generic + " (" + Crd[getCrd (listObject.name, true)].entity.specific + ")";
// Set portrait.
setPortrait (listObject.name,
Crd[getCrd (listObject.name, true)].entity.icon,
"Button",
Crd[getCrd (listObject.name, true)].entity.icon_cell);
}
break;
default:
// Store name of entity to display in list in this button's coordinate.
Crd[getCrd (listObject.name, true)].entity = new Object(itemName);
// Set tooltip.
var template = getEntityTemplate(itemName);
if (!template) break;
//If there are several units selected, don´t show this tooltip when moving the cursor over the selection tab.
if (tab != "selection")
else
{
listObject.tooltip = getProductionTooltip(template);
}
else if (tab == "selection")
{
listObject.tooltip = template.traits.id.specific;
}
// Set portrait.
setPortrait (listObject.name,
template.traits.id.icon,
"Button" + toTitleCase(selection[0].traits.id.civ_code),
template.traits.id.icon_cell);
break;
}
// Set item function.
listObject.onPress = function (event)
{
switch (tab)
{
case "train":
// TODO: Remove this item from the production queue if right-clicked.
issueCommand(selection, true, NMT_PRODUCE, PRODUCTION_TRAIN, ""+(Crd[getCrd (this.name, true)].entity));
break;
case "research":
// TODO: Remove this item from the production queue if right-clicked.
issueCommand(selection, true, NMT_PRODUCE, PRODUCTION_RESEARCH, ""+(Crd[getCrd (this.name, true)].entity.name));
break;
case "barter":
// Buy a quantity of this resource if left-clicked.
// Sell a quantity of this resource if right-clicked.
break;
case "structCiv":
case "structMil":
// Select building placement cursor.
tryConstruction( Crd[getCrd (this.name, true)].entity );
break;
case "garrison":
// Remove this item from the entity's garrison inventory.
break;
case "selection":
// Change the selection to this unit.
break;
default:
break;
}
}
break;
listObject.caption = "";
}
// Reveal portrait.
@ -522,15 +366,17 @@ function updateTab (tab, type, cellSheet, attribute, attribute2, arrayCells)
}
// Default the list to closed.
if (tab == "queue") {
groupObject.hidden = false;
}
else {
groupObject.hidden = true;
}
tabCounter++;
// Completed tab update successfully.
return true;
}
// Invalid property ... Cancel tab update.
return false;
}
@ -584,7 +430,7 @@ function refreshCommandButtons()
// Update research. (Tab button, persistent buttons, click them to do things.)
updateTab ("research", "production", "", "selection[0].actions.create.list.research", "");
//updateTab ("research", "production", "", "selection[0].actions.create.list.research", "");
// End of production and pick lists. Store end position.
var listCounter = tabCounter;
@ -592,10 +438,10 @@ function refreshCommandButtons()
// Commands begin from this point.
tabCounter = snStatusPaneCommand.split;
// Update commands. (Click "tab" button to do something; no list.)
updateTab ("patrol", "command", "", "selection[0].actions.patrol", "");
updateTab ("townbell", "command", "", "selection[0].actions.townBell", "");
//updateTab ("patrol", "command", "", "selection[0].actions.patrol", "");
//updateTab ("townbell", "command", "", "selection[0].actions.townBell", "");
updateTab ("rally", "command", "", "selection[0].actions.create.rally", "");
updateTab ("explore", "command", "", "selection[0].actions.explore", "");
//updateTab ("explore", "command", "", "selection[0].actions.explore", "");
updateTab ("retreat", "command", "", "selection[0].actions.retreat", "");
updateTab ("stop", "command", "", "selection[0].actions.stop", "");
@ -641,14 +487,39 @@ function refreshCommandButtons()
// If more than one entity is selected, list them.
if (selection.length > 1)
{
tempArray = [];
tempArray = new Array();
for ( var i = 0; i < selection.length; i++ )
tempArray[i] = selection[i].tag;
tempArray.length = i;
tempArray.push(selection[i].tag);
updateTab ("selection", "production", "Tab", "tempArray", "");
}
}
}
if (selection.length == 1)
{
if (selection[0].productionQueue && selection[0].productionQueue.length > 0)
{
tabCounter = snStatusPaneCommand.tab.max;
tempArray = new Array();
var queue = selection[0].productionQueue;
for(var i=0; i < queue.length; i++)
{
tempArray.push(queue.get(i));
}
updateTab("queue", "production", "Tab", "tempArray", "");
empty = false;
}
else
{
tabCounter = snStatusPaneCommand.tab.max;
var tabObject = getGUIObjectByName ("snStatusPaneCommand" + tabCounter + "_1");
guiHide(tabObject.name);
var groupObject = getGUIObjectByName ("snStatusPaneCommand" + "Group" + tabCounter);
guiHide(groupObject.name);
}
}
}
@ -696,3 +567,276 @@ function getProductionTooltip(template)
return tooltip;
}
// ===================================================================================
//entity is the name.
function removeFromProductionQueue(selection, entity)
{
var queue = selection[0].productionQueue;
for(var i=0;i<queue.length;i++)
{
if (entity == queue.get(i).name) {
queue.cancel(i);
return true;
}
}
}
// =====================================================================================
function groupProductionItems(l_attr)
{
var newAttribute = new Array();
for (var i=0; i<l_attr.length && newAttribute.length < 8; i++) //allow max 8 items
{
var result = new Object();
result.num = 1;
result.item = l_attr[i];
while (i < (l_attr.length-1) && result.item.name == l_attr[i+1].name)
{
i++;
result.num++;
}
newAttribute.push(result);
}
return newAttribute;
}
// =====================================================================================
function setTooltip(type, tab, cellSheet, cellGroup, tabObject)
{
switch (type)
{
case "command":
var tooltip = cellGroup[cellSheet][tab].name;
tooltip += " " + cellSheet;
break;
case "pick":
var tooltip = cellSheet;
tooltip += " List\nCurrent " + cellSheet + ": " + cellGroup[cellSheet][tab].name;
break;
default:
var tooltip = cellGroup[cellSheet][tab].name;
tooltip += " " + cellSheet;
break;
}
tabObject.tooltip = tooltip;
}
// =====================================================================================
function setEvents(type, tab, tabObject)
{
if (type == "command")
{
if (tab == "rally")
{
tabObject.onPress = function (event)
{
setCursor("cursor-rally");
}
}
else
{
tabObject.onPress = function (event)
{
}
}
}
else
{
tabObject.onMouseEnter = function (event)
{
var currTab = this.name.substring (this.name.lastIndexOf ("d")+1, this.name.lastIndexOf ("_"));
// Seek through all list tabs. (Only the one being hovered should stay open.)
for (var i = 1; i < snStatusPaneCommand.split; i++)
{
// If we've found the current tab,
if (i == currTab)
{
// Click the tab button to toggle visibility of its list.
guiToggle ( "snStatusPaneCommandGroup" + currTab );
}
else // If it's another list tab,
{
// Ensure this tab is hidden.
guiHide ("snStatusPaneCommandGroup" + i);
}
}
}
// Set tab function when user clicks tab.
tabObject.onPress = function (event)
{
// Click the tab button to toggle visibility of its list.
guiToggle ( "snStatusPaneCommandGroup" + this.name.substring (this.name.lastIndexOf ("d")+1, this.name.lastIndexOf ("_")) );
}
}
}
// =====================================================================================
function setTabPortrait(type, tab, cellSheet, cellGroup, tabCounter)
{
switch (tab)
{
case "selection":
case "garrison":
case "queue":
// Temporarily (until we have the tab textures) force a particular cell ID
cellGroup[cellSheet][tab].id = 2;
// Use the horizontal tab. (Extends right.)
setPortrait ("snStatusPaneCommand" + tabCounter + "_1", "IconSheet",
cellSheet + "Button_H", cellGroup[cellSheet][tab].id);
break;
default:
if (type == "pick" && cellSheet != "Tab")
{
setPortrait ("snStatusPaneCommand" + tabCounter + "_1", "IconSheet",
cellSheet + "TabButton", cellGroup[cellSheet][tab].id);
}
else
{
setPortrait ("snStatusPaneCommand" + tabCounter + "_1", "IconSheet",
cellSheet + "Button", cellGroup[cellSheet][tab].id);
}
break;
}
}
// =====================================================================================
function setItemTooltip(tab, itemName, listObject, selection)
{
switch (tab)
{
case "research":
// Store name of tech to display in list in this button's coordinate.
var tech = getTechnology(itemName, selection[0].player);
Crd[getCrd (listObject.name, true)].entity = tech;
if(!tech) {
listObject.tooltip = "Bad tech: " + itemName;
}
else {
// Set tooltip.
listObject.tooltip = Crd[getCrd (listObject.name, true)].entity.generic +
" (" + Crd[getCrd (listObject.name, true)].entity.specific + ")";
// Set portrait.
setPortrait (listObject.name,
Crd[getCrd (listObject.name, true)].entity.icon,
"Button",
Crd[getCrd (listObject.name, true)].entity.icon_cell);
}
break;
default:
// Store name of entity to display in list in this button's coordinate.
Crd[getCrd (listObject.name, true)].entity = new Object(itemName);
// Set tooltip.
var template = getEntityTemplate(itemName);
if (!template) break;
//If there are several units selected, don´t show this tooltip when moving the cursor over the selection tab.
if (tab != "selection")
{
listObject.tooltip = getProductionTooltip(template);
}
else
{
listObject.tooltip = template.traits.id.specific;
}
// Set portrait.
setPortrait (listObject.name,
template.traits.id.icon,
"Button" + toTitleCase(selection[0].traits.id.civ_code),
template.traits.id.icon_cell);
break;
}
}
// =====================================================================================
function setItemFunction(tab, listObject, selection)
{
listObject.onPress = function (event)
{
switch (tab)
{
case "train":
issueCommand(selection, true, NMT_PRODUCE, PRODUCTION_TRAIN, ""+(Crd[getCrd (this.name, true)].entity));
break;
case "research":
// TODO: Remove this item from the production queue if right-clicked.
issueCommand(selection, true, NMT_PRODUCE, PRODUCTION_RESEARCH, ""+(Crd[getCrd (this.name, true)].entity.name));
break;
case "barter":
// Buy a quantity of this resource if left-clicked.
// Sell a quantity of this resource if right-clicked.
break;
case "structCiv":
case "structMil":
// Select building placement cursor.
tryConstruction( Crd[getCrd (this.name, true)].entity );
break;
case "garrison":
// Remove this item from the entity's garrison inventory.
break;
case "selection":
// Change the selection to this unit.
break;
case "queue":
//Cancel this production item.
removeFromProductionQueue(selection, Crd[getCrd(this.name, true)].entity);
break;
default:
break;
}
}
}
// =====================================================================================
function extractItemList(attribute, arrayCells)
{
var listArray = new Array();
// Insert blank array element at location 0 (to buffer array so items are in 1..n range).
listArray.push("");
if (!attribute.length)
{ // If it's a list where each element is a value, (entity list)
for ( var i in attribute )
{
listArray.push(i);
}
}
else
{ // If it's a list where each element is part of a numbered array, (array list)
for ( var i = 0; i < attribute.length; i++ )
{
if (attribute[i].name) {
if (attribute[i].name != "special_settlement")
listArray.push(attribute[i].name);
} else {
if (attribute[i] != "special_settlement")
listArray.push(attribute[i]);
}
// If cell sheet for each item is stored in attribute.sheet, transfer that across too.
if (arrayCells == true)
{
listArray[listArray.length-1].sheet = new Object(attribute[i].name.sheet);
}
}
}
return listArray;
}