2005-07-27 17:21:27 +02:00
/ *
DESCRIPTION : Functions for "main game" session GUI .
NOTES :
* /
// ====================================================================
function initSession ( )
{
// ============================================= CONSTANTS =================================================
2005-09-10 19:30:57 +02:00
snConst = new Object ( ) ;
// Portraits (large and small).
snConst . Portrait = new Object ( ) ;
snConst . Portrait . Sml = new Object ( ) ;
2006-02-28 00:37:43 +01:00
snConst . Portrait . Sml . Width = 45 ;
2005-09-10 19:30:57 +02:00
snConst . Portrait . Sml . Height = snConst . Portrait . Sml . Width ;
snConst . Portrait . Lrg = new Object ( ) ;
snConst . Portrait . Lrg . Width = 64 ;
snConst . Portrait . Lrg . Height = snConst . Portrait . Lrg . Width ;
2005-07-27 17:21:27 +02:00
// Small icons (eg Movement Rate, Food).
2005-09-10 19:30:57 +02:00
snConst . MiniIcon = new Object ( ) ;
2006-02-28 00:37:43 +01:00
snConst . MiniIcon . Width = 30 ;
2005-09-10 19:30:57 +02:00
snConst . MiniIcon . Height = snConst . MiniIcon . Width ;
2005-07-27 17:21:27 +02:00
// ============================================= GLOBALS =================================================
2005-07-30 22:25:38 +02:00
// Define cell reference constants for icon sheets.
initCellReference ( ) ;
2005-07-27 17:21:27 +02:00
}
// ====================================================================
function initCellReference ( )
{
2006-02-28 00:37:43 +01:00
cellGroup = new Array ( ) ;
2006-03-15 00:01:55 +01:00
// Define categories of cell groups by checking their reference files in the same locations as the icon sheets.
addCellGroupCategory ( "art/textures/ui/session/icons/sheets/" ) ;
// (Note that we don't use this and we probably shouldn't, since the entities state which icon cell they should use; it makes it easier to remember which icon is used
// for which unit if we have this reference sheet, though.)
addCellGroupCategory ( "art/textures/ui/session/portraits/sheets/" ) ;
}
2006-02-28 00:37:43 +01:00
2006-03-15 00:01:55 +01:00
// ====================================================================
2006-02-28 00:37:43 +01:00
2006-03-15 00:01:55 +01:00
function addCellGroupCategory ( iconSheetPath )
{
// Get array list of all icon sheet reference files.
2006-03-21 23:30:29 +01:00
var iconSheets = buildFileList ( iconSheetPath , "*.txt" , true ) ;
2006-03-15 00:01:55 +01:00
// Alphabetically sort the array.
iconSheets . sort ( ) ;
// Seek through all icon sheets.
2006-03-21 23:30:29 +01:00
for ( var sheet = 0 ; sheet < iconSheets . length ; sheet ++ )
2006-03-15 00:01:55 +01:00
{
// Get the current icon sheet name.
2006-03-21 23:30:29 +01:00
var groupName = iconSheets [ sheet ] ;
2006-03-15 00:01:55 +01:00
// Remove path and extension information so we just have the group name.
groupName = groupName . replace ( iconSheetPath , "" ) ;
groupName = groupName . replace ( ".txt" , "" ) ;
groupName = toTitleCase ( groupName ) ;
2006-02-28 00:37:43 +01:00
2006-03-15 00:01:55 +01:00
// Get the elements from the current icon sheet.
2006-03-21 23:30:29 +01:00
var iconArray = readFileLines ( iconSheets [ sheet ] ) ;
2006-03-01 21:23:09 +01:00
2006-03-15 00:01:55 +01:00
// For each row in the icon sheet file,
2006-03-21 23:30:29 +01:00
for ( var row = 0 ; row < iconArray . length ; row ++ )
2006-03-15 00:01:55 +01:00
{
// Get the individual fields in the array as another array.
2006-03-21 23:30:29 +01:00
var iconElements = iconArray [ row ] . split ( "," ) ;
2006-03-15 00:01:55 +01:00
// Add this cell to the current group.
addCell ( groupName , iconElements [ 0 ] , iconElements [ 1 ] ) ;
}
}
2006-02-28 00:37:43 +01:00
}
// ====================================================================
function addCell ( group , cellID , cellName )
{
// Add a cell ID to a cell container group.
// If this array does not exist,
if ( ! cellGroup [ group ] )
{
// Create the container.
cellGroup [ group ] = new Array ( ) ;
cellGroup [ group ] . length = new Object ( 0 ) ;
}
cellGroup [ group ] [ cellID ] = new Array ( ) ;
cellGroup [ group ] [ cellID ] . id = new Object ( cellGroup [ group ] . length ) ;
cellGroup [ group ] [ cellID ] . name = new Object ( cellName ) ;
cellGroup [ group ] . length ++ ;
2005-07-27 17:21:27 +02:00
}
// ====================================================================
function setPortrait ( objectName , portraitString , portraitSuffix , portraitCell )
{
2006-03-21 23:30:29 +01:00
// Use this function as a shortcut to change a portrait object to a different portrait image.
2005-07-27 17:21:27 +02:00
2006-03-21 23:30:29 +01:00
// Accepts an object and specifies its default, rollover (lit) and disabled (gray) sprites.
// Sprite Format: "sn""portraitString""portraitSuffix"
// Sprite Format: "sn""portraitString""portraitSuffix""Over"
// Sprite Format: "sn""portraitString""portraitSuffix""Disabled"
// Note: Make sure the file follows this naming convention or bad things could happen.
2005-07-27 17:21:27 +02:00
2006-03-21 23:30:29 +01:00
// Get GUI object
setPortraitGUIObject = getGUIObjectByName ( objectName ) ;
2005-07-27 17:21:27 +02:00
2005-09-10 19:30:57 +02:00
// Report error if object not found.
if ( ! setPortraitGUIObject )
{
console . write ( "setPortrait(): Failed to find object " + objectName + "." ) ;
return 1 ;
}
2005-07-27 17:21:27 +02:00
// Set the three portraits.
if ( portraitSuffix && portraitSuffix != "" )
2006-02-28 00:37:43 +01:00
setPortraitGUIObject . sprite = "sn" + portraitString + portraitSuffix ;
2005-07-27 17:21:27 +02:00
else
2006-02-28 00:37:43 +01:00
setPortraitGUIObject . sprite = "sn" + portraitString ;
2005-07-27 17:21:27 +02:00
2006-02-28 00:37:43 +01:00
setPortraitGUIObject . sprite _over = setPortraitGUIObject . sprite + "Over" ;
setPortraitGUIObject . sprite _disabled = setPortraitGUIObject . sprite + "Disabled" ;
2005-07-27 17:21:27 +02:00
// If the source texture is a multi-frame image (icon sheet), specify correct cell.
if ( portraitCell && portraitCell != "" )
setPortraitGUIObject . cell _id = portraitCell ;
else
setPortraitGUIObject . cell _id = "" ;
2005-09-10 19:30:57 +02:00
return 0 ;
2005-07-27 17:21:27 +02:00
}
// ====================================================================
function flipGUI ( NewGUIType )
{
2005-07-30 22:25:38 +02:00
// Changes GUI to a different layout.
2005-07-27 17:21:27 +02:00
switch ( NewGUIType )
{
// Set which GUI to use.
2005-07-30 22:25:38 +02:00
case rb :
case lb :
case lt :
case rt :
GUIType = NewGUIType ;
2005-07-27 17:21:27 +02:00
break ;
default :
// If no type specified, toggle.
2005-07-30 22:25:38 +02:00
if ( GUIType == rb )
GUIType = lb ;
2005-07-27 17:21:27 +02:00
else
2005-07-30 22:25:38 +02:00
if ( GUIType == lb )
GUIType = lt ;
2005-07-27 17:21:27 +02:00
else
2005-07-30 22:25:38 +02:00
if ( GUIType == lt )
GUIType = rt ;
else
GUIType = rb ;
2005-07-27 17:21:27 +02:00
break ;
}
2005-07-30 22:25:38 +02:00
// Seek through all sizes created.
2006-03-21 23:30:29 +01:00
for ( var i = 0 ; i <= Crd . last ; i ++ )
2005-07-27 17:21:27 +02:00
{
2005-07-30 22:25:38 +02:00
// Set their sizes to the stored value.
getGUIObjectByName ( Crd [ i ] . name ) . size = Crd [ i ] . size [ GUIType ] ;
2005-07-27 17:21:27 +02:00
}
2005-07-30 22:25:38 +02:00
writeConsole ( "GUI flipped to size " + GUIType + "." ) ;
2005-07-27 17:21:27 +02:00
}
// ====================================================================
2005-09-05 05:21:20 +02:00
// Update-on-alteration trickery...
// We don't really want to update every single time we get a
// selection-changed or property-changed event; that could happen
// a lot. Instead, use this bunch of globals to cache any changes
// that happened between GUI updates.
// This boolean determines whether the selection has been changed.
var selectionChanged = false ;
// This boolean determines what the template of the selected object
// was when last we looked
var selectionTemplate = null ;
// This array holds the name of all properties that need to be updated
var selectionPropertiesChanged = new Array ( ) ;
// This array holds a list of all the objects we hold property-change
// watches on
var propertyWatches = new Array ( ) ;
// ====================================================================
// This function resets all the update variables, above
function resetUpdateVars ( )
{
if ( selectionChanged )
{
for ( watchedObject in propertyWatches )
propertyWatches [ watchedObject ] . unwatchAll ( selectionWatchHandler ) ; // Remove the handler
propertyWatches = new Array ( ) ;
2005-10-30 22:09:27 +01:00
if ( selection . length > 0 && selection [ 0 ] )
2005-09-05 05:21:20 +02:00
{
// Watch the object itself
selection [ 0 ] . watchAll ( selectionWatchHandler ) ;
propertyWatches . push ( selection [ 0 ] ) ;
// And every parent back up the tree (changes there will affect
// displayed properties via inheritance)
var parent = selection [ 0 ] . template
while ( parent )
{
parent . watchAll ( selectionWatchHandler ) ;
propertyWatches . push ( selection [ 0 ] ) ;
parent = parent . parent ;
}
}
}
selectionChanged = false ;
2005-10-30 22:09:27 +01:00
if ( selection . length > 0 && selection [ 0 ] )
2005-09-05 05:21:20 +02:00
{
selectionTemplate = selection [ 0 ] . template ;
}
else
selectionTemplate = null ;
selectionPropertiesChanged = new Array ( ) ;
}
// ====================================================================
// This function returns whether we should update a particular statistic
// in the GUI (e.g. "actions.attack") - this should happen if: the selection
// changed, the selection had its template altered (changing lots of stuff)
// or an assignment has been made to that stat or any property within that
// stat.
function shouldUpdateStat ( statname )
{
if ( selectionChanged || ( selectionTemplate != selection [ 0 ] . template ) )
2005-12-16 08:12:19 +01:00
{
2005-09-05 05:21:20 +02:00
return ( true ) ;
2005-12-16 08:12:19 +01:00
}
2005-09-05 05:21:20 +02:00
for ( var property in selectionPropertiesChanged )
{
// If property starts with statname
if ( selectionPropertiesChanged [ property ] . substring ( 0 , statname . length ) == statname )
2005-12-16 08:12:19 +01:00
{
2005-09-05 05:21:20 +02:00
return ( true ) ;
2005-12-16 08:12:19 +01:00
}
2005-09-05 05:21:20 +02:00
}
2005-12-16 08:12:19 +01:00
return ( false ) ;
2005-09-05 05:21:20 +02:00
}
// ====================================================================
// This function is a handler for the 'selectionChanged' event,
// it updates the selectionChanged flag
function selectionChangedHandler ( )
{
selectionChanged = true ;
}
// ====================================================================
// Register it.
addGlobalHandler ( "selectionChanged" , selectionChangedHandler ) ;
// ====================================================================
// This function is a handler for a watch event; it updates the
// selectionPropertiesChanged array.
function selectionWatchHandler ( propname , oldvalue , newvalue )
{
selectionPropertiesChanged . push ( propname ) ;
// This bit's important (watches allow the handler to change the value
// before it gets written; we don't want to affect things, so make sure
// the value we send back is the one that was going to be written)
return ( newvalue ) ;
}
// ====================================================================
function snRefresh ( )
{
// Updated each tick to refresh session controls if necessary.
// Don't process GUI when we're full-screen.
if ( getGUIObjectByName ( "sn" ) . hidden == false )
{
if ( ! selection . length ) // If no entity selected,
{
// Hide Status Orb
guiHide ( "snStatusPane" ) ;
// Hide Group Pane.
// guiHide ("snGroupPane");
getGlobal ( ) . MultipleEntitiesSelected = 0 ;
}
else // If at least one entity selected,
{
// Reveal Status Orb
guiUnHide ( "snStatusPane" ) ;
2005-12-17 01:24:43 +01:00
2005-09-05 05:21:20 +02:00
// (later, we need to base this on the selected unit's stats changing)
refreshStatusPane ( ) ;
// Check if a group of entities selected
if ( selection . length > 1 )
{
// If a group pane isn't already open, and we don't have the same set as last time,
if ( selection . length != getGlobal ( ) . MultipleEntitiesSelected )
{
// (later, we need to base this on the selection changing)
// refreshGroupPane();
getGlobal ( ) . MultipleEntitiesSelected = selection . length ;
}
}
else
{
getGlobal ( ) . MultipleEntitiesSelected = 0 ;
// Hide Group Pane.
// guiHide ("snGroupPane");
}
}
// Modify any resources given/taken
// (later, we need to base this on a resource-changing event).
// refreshResourcePool();
// Update Team Tray
// (later, we need to base this on the player creating a group).
// refreshTeamTray();
2006-01-08 14:39:05 +01:00
2005-10-30 02:06:04 +01:00
// Refresh handler that monitors whether property/selection has been updated.
resetUpdateVars ( ) ;
2005-09-05 05:21:20 +02:00
}
2006-01-08 14:39:05 +01:00
if ( selectionChanged && getGUIObjectByName ( "mn" ) . hidden == false )
{
// Update manual if it's open.
refreshManual ( ) ;
}
2006-06-10 05:05:16 +02:00
// Always refresh resources
refreshResources ( ) ;
2005-09-05 05:21:20 +02:00
}
2005-09-10 19:30:57 +02:00
// ====================================================================