Support appending and global selection via the 'find idle' keys.

This was SVN commit r12664.
This commit is contained in:
Deiz 2012-09-12 21:37:04 +00:00
parent 958fce0438
commit 41e5f88aae
2 changed files with 64 additions and 21 deletions

View File

@ -1716,30 +1716,57 @@ function setCameraFollow(entity)
var lastIdleUnit = 0;
var currIdleClass = 0;
var lastIdleType = undefined;
function resetIdleUnit()
{
lastIdleUnit = 0;
currIdleClass = 0;
lastIdleType = undefined;
}
function findIdleUnit(classes)
{
// Cycle through idling classes before giving up
for (var i = 0; i <= classes.length; ++i)
var append = Engine.HotkeyIsPressed("selection.add");
var selectall = Engine.HotkeyIsPressed("selection.offscreen");
// Reset the last idle unit, etc., if the selection type has changed.
var type = classes.join();
if (selectall || type != lastIdleType)
resetIdleUnit();
lastIdleType = type;
// If selectall is true, there is no limit and it's necessary to iterate
// over all of the classes, resetting only when the first match is found.
var matched = false;
for (var i = 0; i < classes.length; ++i)
{
var data = { prevUnit: lastIdleUnit, idleClass: classes[currIdleClass] };
var newIdleUnit = Engine.GuiInterfaceCall("FindIdleUnit", data);
var data = { idleClass: classes[currIdleClass], prevUnit: lastIdleUnit, limit: 1 };
if (append)
data.excludeUnits = g_Selection.toList();
if (selectall)
data = { idleClass: classes[currIdleClass] };
// Check if we have new valid entity
if (newIdleUnit && newIdleUnit != lastIdleUnit)
var idleUnits = Engine.GuiInterfaceCall("FindIdleUnits", data);
if (idleUnits.length && idleUnits[0] != lastIdleUnit)
{
lastIdleUnit = newIdleUnit;
g_Selection.reset()
g_Selection.addList([lastIdleUnit]);
Engine.CameraFollow(lastIdleUnit);
lastIdleUnit = idleUnits[0];
if (!append && (!selectall || selectall && !matched))
g_Selection.reset()
return;
if (selectall)
g_Selection.addList(idleUnits);
else
{
g_Selection.addList([lastIdleUnit]);
Engine.CameraFollow(lastIdleUnit);
return;
}
matched = true;
}
lastIdleUnit = 0;

View File

@ -1501,22 +1501,38 @@ function isIdleUnit(ent, idleClass)
return (cmpUnitAI && cmpIdentity && cmpUnitAI.IsIdle() && !cmpUnitAI.IsGarrisoned() && idleClass && cmpIdentity.HasClass(idleClass));
}
GuiInterface.prototype.FindIdleUnit = function(player, data)
GuiInterface.prototype.FindIdleUnits = function(player, data)
{
var rangeMan = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager);
var playerEntities = rangeMan.GetEntitiesByPlayer(player);
var playerEntities = rangeMan.GetEntitiesByPlayer(player).filter( function(e) {
var cmpUnitAI = Engine.QueryInterface(e, IID_UnitAI);
if (cmpUnitAI)
return true;
return false;
});
// Find the first matching entity that is after the previous selection,
// so that we cycle around in a predictable order
for each (var ent in playerEntities)
var idleUnits = [];
var noFilter = (data.prevUnit == undefined && data.excludeUnits == undefined);
for (var j = 0; j < playerEntities.length; ++j)
{
if (ent > data.prevUnit && isIdleUnit(ent, data.idleClass))
return ent;
var ent = playerEntities[j];
if (!isIdleUnit(ent, data.idleClass))
continue;
if (noFilter || ((data.prevUnit == undefined || ent > data.prevUnit) &&
(data.excludeUnits == undefined || data.excludeUnits.indexOf(ent) == -1)))
{
idleUnits.push(ent);
playerEntities.splice(j--, 1);
}
if (data.limit && idleUnits.length >= data.limit)
break;
}
// No idle entities left in the class
return 0;
};
return idleUnits;
}
GuiInterface.prototype.GetTradingRouteGain = function(player, data)
{
@ -1648,7 +1664,7 @@ var exposedFunctions = {
"SetWallPlacementPreview": 1,
"GetFoundationSnapData": 1,
"PlaySound": 1,
"FindIdleUnit": 1,
"FindIdleUnits": 1,
"GetTradingRouteGain": 1,
"GetTradingDetails": 1,
"CanAttack": 1,