2010-10-13 21:48:13 +02:00
|
|
|
const MAX_SELECTION_SIZE = 40; // Limits selection size and ensures that there will not be too many selection items in the GUI
|
|
|
|
|
2010-08-08 19:28:51 +02:00
|
|
|
function _setHighlight(ents, alpha)
|
2010-03-11 21:01:16 +01:00
|
|
|
{
|
2010-05-01 18:20:58 +02:00
|
|
|
if (ents.length)
|
2010-08-08 19:28:51 +02:00
|
|
|
Engine.GuiInterfaceCall("SetSelectionHighlight", { "entities":ents, "alpha":alpha });
|
2010-03-11 21:01:16 +01:00
|
|
|
}
|
2010-01-09 20:20:14 +01:00
|
|
|
|
2010-10-02 14:41:29 +02:00
|
|
|
function _setStatusBars(ents, enabled)
|
|
|
|
{
|
|
|
|
if (ents.length)
|
|
|
|
Engine.GuiInterfaceCall("SetStatusBars", { "entities":ents, "enabled":enabled });
|
|
|
|
}
|
|
|
|
|
2010-04-30 01:36:05 +02:00
|
|
|
function _setMotionOverlay(ents, enabled)
|
|
|
|
{
|
2010-05-01 18:20:58 +02:00
|
|
|
if (ents.length)
|
|
|
|
Engine.GuiInterfaceCall("SetMotionDebugOverlay", { "entities":ents, "enabled":enabled });
|
2010-04-30 01:36:05 +02:00
|
|
|
}
|
|
|
|
|
2010-08-10 03:25:24 +02:00
|
|
|
function _playSound(ent)
|
|
|
|
{
|
|
|
|
Engine.GuiInterfaceCall("PlaySound", { "name":"select", "entity":ent });
|
|
|
|
}
|
|
|
|
|
2010-09-25 17:22:41 +02:00
|
|
|
//-------------------------------- -------------------------------- --------------------------------
|
2010-10-13 21:48:13 +02:00
|
|
|
// EntityGroups class for managing grouped entities
|
2010-09-25 17:22:41 +02:00
|
|
|
//-------------------------------- -------------------------------- --------------------------------
|
2010-10-13 21:48:13 +02:00
|
|
|
function EntityGroups()
|
2010-09-25 17:22:41 +02:00
|
|
|
{
|
2010-10-13 21:48:13 +02:00
|
|
|
this.EntityGroup = function(name, templateName, ent)
|
|
|
|
{
|
|
|
|
this.name = name;
|
|
|
|
this.templateName = templateName;
|
|
|
|
this.ents = {ent : ent}; // the second element is stored as number
|
|
|
|
this.count = 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.EntityGroup.prototype.add = function(ent)
|
|
|
|
{
|
|
|
|
this.ents[ent] = ent;
|
|
|
|
this.count++;
|
|
|
|
};
|
2010-10-11 19:24:06 +02:00
|
|
|
|
2010-10-13 21:48:13 +02:00
|
|
|
this.EntityGroup.prototype.remove= function(ent)
|
|
|
|
{
|
|
|
|
delete this.ents[ent];
|
|
|
|
this.count--;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.groups = {};
|
|
|
|
this.ents = {};
|
2010-09-25 17:22:41 +02:00
|
|
|
}
|
|
|
|
|
2010-10-13 21:48:13 +02:00
|
|
|
EntityGroups.prototype.reset = function()
|
2010-09-25 17:22:41 +02:00
|
|
|
{
|
2010-10-13 21:48:13 +02:00
|
|
|
this.groups = {};
|
|
|
|
this.ents = {};
|
2010-10-11 19:24:06 +02:00
|
|
|
};
|
|
|
|
|
2010-10-13 21:48:13 +02:00
|
|
|
EntityGroups.prototype.add = function(ents)
|
2010-10-11 19:24:06 +02:00
|
|
|
{
|
2010-10-13 21:48:13 +02:00
|
|
|
for each (var ent in ents)
|
2010-09-25 17:22:41 +02:00
|
|
|
{
|
2010-10-13 21:48:13 +02:00
|
|
|
if (!this.ents[ent])
|
2010-09-25 17:22:41 +02:00
|
|
|
{
|
2010-10-13 21:48:13 +02:00
|
|
|
var entState = GetEntityState(ent);
|
2010-10-11 19:24:06 +02:00
|
|
|
var templateName = entState.template;
|
|
|
|
var template = GetTemplateData(templateName);
|
2010-10-13 21:48:13 +02:00
|
|
|
var name = template.name.specific || template.name.generic || "???";
|
|
|
|
|
|
|
|
if (this.groups[name])
|
|
|
|
this.groups[name].add(ent);
|
2010-09-25 17:22:41 +02:00
|
|
|
else
|
2010-10-13 21:48:13 +02:00
|
|
|
this.groups[name] = new this.EntityGroup(name, templateName, ent);
|
|
|
|
|
|
|
|
this.ents[ent] = name;
|
2010-09-25 17:22:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-10-13 21:48:13 +02:00
|
|
|
EntityGroups.prototype.removeEnt = function(ent)
|
2010-09-25 17:22:41 +02:00
|
|
|
{
|
2010-10-13 21:48:13 +02:00
|
|
|
var name = this.ents[ent];
|
|
|
|
|
|
|
|
// Remove the entity
|
|
|
|
delete this.ents[ent];
|
|
|
|
this.groups[name].remove(ent);
|
|
|
|
|
|
|
|
// Remove the entire group
|
|
|
|
if (this.groups[name].count == 0)
|
|
|
|
delete this.groups[name];
|
2010-09-25 17:22:41 +02:00
|
|
|
};
|
|
|
|
|
2010-10-13 22:16:03 +02:00
|
|
|
EntityGroups.prototype.getCount = function(name)
|
2010-09-25 17:22:41 +02:00
|
|
|
{
|
2010-10-13 21:48:13 +02:00
|
|
|
return this.groups[name].count;
|
2010-09-25 17:22:41 +02:00
|
|
|
};
|
|
|
|
|
2010-10-13 21:48:13 +02:00
|
|
|
EntityGroups.prototype.getTemplateNames = function()
|
2010-09-25 17:22:41 +02:00
|
|
|
{
|
2010-10-13 21:48:13 +02:00
|
|
|
var templateNames = [];
|
|
|
|
for each (var group in this.groups)
|
|
|
|
templateNames.push(group.templateName);
|
|
|
|
return templateNames;
|
2010-09-25 17:22:41 +02:00
|
|
|
};
|
|
|
|
|
2010-10-13 21:48:13 +02:00
|
|
|
EntityGroups.prototype.getEntsByName = function(name)
|
2010-09-25 17:22:41 +02:00
|
|
|
{
|
2010-10-13 21:48:13 +02:00
|
|
|
var ents = [];
|
|
|
|
for each (var ent in this.groups[name].ents)
|
|
|
|
ents.push(ent);
|
|
|
|
return ents;
|
2010-09-25 17:22:41 +02:00
|
|
|
};
|
|
|
|
|
2010-10-13 21:48:13 +02:00
|
|
|
// Gets all ents in every group except ones of the specified group
|
|
|
|
EntityGroups.prototype.getEntsByNameInverse = function(name)
|
2010-09-25 17:22:41 +02:00
|
|
|
{
|
|
|
|
var ents = [];
|
2010-10-13 21:48:13 +02:00
|
|
|
for each (var group in this.groups)
|
|
|
|
if (group.name != name)
|
|
|
|
for each (var ent in group.ents)
|
|
|
|
ents.push(ent);
|
2010-09-25 17:22:41 +02:00
|
|
|
return ents;
|
|
|
|
};
|
|
|
|
|
2010-06-26 20:10:03 +02:00
|
|
|
//-------------------------------- -------------------------------- --------------------------------
|
|
|
|
// EntitySelection class for managing the entity selection list and the primary selection
|
|
|
|
//-------------------------------- -------------------------------- --------------------------------
|
2010-03-11 21:01:16 +01:00
|
|
|
function EntitySelection()
|
2010-01-25 23:31:43 +01:00
|
|
|
{
|
2010-05-01 18:20:58 +02:00
|
|
|
// Private properties:
|
2010-06-26 20:10:03 +02:00
|
|
|
//--------------------------------
|
2010-05-09 15:56:06 +02:00
|
|
|
this.selected = {}; // { id:id, id:id, ... } for each selected entity ID 'id'
|
2010-06-26 20:10:03 +02:00
|
|
|
|
|
|
|
// { id:id, ... } for mouseover-highlighted entity IDs in these, the key is a string and the value is an int;
|
|
|
|
// we want to use the int form wherever possible since it's more efficient to send to the simulation code)
|
|
|
|
this.highlighted = {};
|
|
|
|
|
2010-04-30 01:36:05 +02:00
|
|
|
this.motionDebugOverlay = false;
|
2010-05-01 18:20:58 +02:00
|
|
|
|
|
|
|
// Public properties:
|
2010-06-26 20:10:03 +02:00
|
|
|
//--------------------------------
|
2010-05-01 18:20:58 +02:00
|
|
|
this.dirty = false; // set whenever the selection has changed
|
2010-10-13 21:48:13 +02:00
|
|
|
this.groups = new EntityGroups();
|
2010-01-25 23:31:43 +01:00
|
|
|
}
|
|
|
|
|
2010-10-13 21:48:13 +02:00
|
|
|
// Deselect everything but entities of the chosen type if the modifier is true otherwise deselect just the chosen entity
|
2010-09-25 17:22:41 +02:00
|
|
|
EntitySelection.prototype.makePrimarySelection = function(primaryTemplateName, modifierKey)
|
2010-07-19 23:23:12 +02:00
|
|
|
{
|
2010-07-31 00:19:43 +02:00
|
|
|
var selection = this.toList();
|
2010-10-13 21:48:13 +02:00
|
|
|
var ent;
|
2010-09-25 17:22:41 +02:00
|
|
|
|
2010-10-13 21:48:13 +02:00
|
|
|
// Find an ent of a unit of the same type
|
2010-09-25 17:22:41 +02:00
|
|
|
for (var i = 0; i < selection.length; i++)
|
2010-07-31 00:19:43 +02:00
|
|
|
{
|
2010-09-25 17:22:41 +02:00
|
|
|
var entState = GetEntityState(selection[i]);
|
|
|
|
if (!entState)
|
|
|
|
continue;
|
|
|
|
if (entState.template == primaryTemplateName)
|
2010-10-13 21:48:13 +02:00
|
|
|
ent = selection[i];
|
2010-07-31 00:19:43 +02:00
|
|
|
}
|
2010-09-25 17:22:41 +02:00
|
|
|
|
2010-10-13 21:48:13 +02:00
|
|
|
var primaryEntState = GetEntityState(ent);
|
2010-09-25 17:22:41 +02:00
|
|
|
if (!primaryEntState)
|
|
|
|
return;
|
2010-10-13 21:48:13 +02:00
|
|
|
|
2010-09-25 17:22:41 +02:00
|
|
|
var primaryTemplate = GetTemplateData(primaryTemplateName);
|
2010-10-13 21:48:13 +02:00
|
|
|
var primaryName = primaryTemplate.name.specific || primaryTemplate.name.generic || "???";
|
2010-09-25 17:22:41 +02:00
|
|
|
|
|
|
|
var ents = [];
|
|
|
|
if (modifierKey)
|
2010-10-13 21:48:13 +02:00
|
|
|
ents = this.groups.getEntsByNameInverse(primaryName);
|
2010-06-26 20:10:03 +02:00
|
|
|
else
|
2010-10-13 21:48:13 +02:00
|
|
|
ents = this.groups.getEntsByName(primaryName);
|
2010-09-25 17:22:41 +02:00
|
|
|
|
2010-07-31 00:19:43 +02:00
|
|
|
this.reset();
|
|
|
|
this.addList(ents);
|
|
|
|
}
|
2010-06-26 20:10:03 +02:00
|
|
|
|
2010-07-31 00:19:43 +02:00
|
|
|
// Get a list of the template names
|
|
|
|
EntitySelection.prototype.getTemplateNames = function()
|
2010-06-26 20:10:03 +02:00
|
|
|
{
|
2010-07-31 00:19:43 +02:00
|
|
|
var templateNames = [];
|
|
|
|
var ents = this.toList();
|
|
|
|
|
|
|
|
for each (var ent in ents)
|
|
|
|
{
|
2010-08-28 23:12:52 +02:00
|
|
|
var entState = GetEntityState(ent);
|
2010-07-31 00:19:43 +02:00
|
|
|
if (entState)
|
|
|
|
templateNames.push(entState.template);
|
|
|
|
}
|
|
|
|
return templateNames;
|
|
|
|
}
|
2010-06-26 20:10:03 +02:00
|
|
|
|
2010-07-04 03:03:14 +02:00
|
|
|
// Update the selection to take care of changes (like units that have been killed)
|
2010-08-01 18:25:24 +02:00
|
|
|
EntitySelection.prototype.update = function()
|
2010-07-04 03:03:14 +02:00
|
|
|
{
|
2010-07-31 00:19:43 +02:00
|
|
|
for each (var ent in this.selected)
|
2010-07-04 03:03:14 +02:00
|
|
|
{
|
2010-08-28 23:12:52 +02:00
|
|
|
var entState = GetEntityState(ent);
|
2010-10-13 21:48:13 +02:00
|
|
|
|
2010-10-05 02:08:05 +02:00
|
|
|
// Remove deleted units
|
2010-07-04 03:03:14 +02:00
|
|
|
if (!entState)
|
|
|
|
{
|
2010-10-13 21:48:13 +02:00
|
|
|
delete this.selected[ent];
|
|
|
|
this.groups.removeEnt(ent);
|
|
|
|
this.dirty = true;
|
2010-10-05 02:08:05 +02:00
|
|
|
continue;
|
|
|
|
}
|
2010-10-13 21:48:13 +02:00
|
|
|
|
2010-10-05 02:08:05 +02:00
|
|
|
// Remove non-visible units (e.g. moved back into fog-of-war)
|
|
|
|
if (entState.visibility == "hidden")
|
|
|
|
{
|
|
|
|
// Disable any highlighting of the disappeared unit
|
|
|
|
_setHighlight([ent], 0);
|
|
|
|
_setStatusBars([ent], false);
|
|
|
|
_setMotionOverlay([ent], false);
|
|
|
|
|
|
|
|
delete this.selected[ent];
|
2010-10-13 21:48:13 +02:00
|
|
|
this.dirty = true;
|
2010-10-05 02:08:05 +02:00
|
|
|
continue;
|
2010-07-04 03:03:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-03-11 21:01:16 +01:00
|
|
|
EntitySelection.prototype.addList = function(ents)
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
2010-10-13 21:48:13 +02:00
|
|
|
var selectionSize = this.toList().length;
|
|
|
|
var i = 1;
|
2010-03-11 21:01:16 +01:00
|
|
|
var added = [];
|
2010-10-13 21:48:13 +02:00
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
for each (var ent in ents)
|
|
|
|
{
|
2010-10-13 21:48:13 +02:00
|
|
|
if (!this.selected[ent] && (selectionSize + i) <= MAX_SELECTION_SIZE)
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
2010-03-11 21:01:16 +01:00
|
|
|
added.push(ent);
|
2010-05-09 15:56:06 +02:00
|
|
|
this.selected[ent] = ent;
|
2010-10-13 21:48:13 +02:00
|
|
|
i++;
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
|
|
|
}
|
2010-10-13 21:48:13 +02:00
|
|
|
|
2010-08-08 19:28:51 +02:00
|
|
|
_setHighlight(added, 1);
|
2010-10-02 14:41:29 +02:00
|
|
|
_setStatusBars(added, true);
|
2010-04-30 01:36:05 +02:00
|
|
|
_setMotionOverlay(added, this.motionDebugOverlay);
|
2010-08-10 03:25:24 +02:00
|
|
|
if (added.length)
|
|
|
|
_playSound(added[0]);
|
2010-10-13 21:48:13 +02:00
|
|
|
|
2010-10-11 19:24:06 +02:00
|
|
|
this.groups.add(this.toList()); // Create Selection Groups
|
2010-05-01 18:20:58 +02:00
|
|
|
this.dirty = true;
|
2010-03-11 21:01:16 +01:00
|
|
|
};
|
2010-01-09 20:20:14 +01:00
|
|
|
|
2010-11-07 21:42:52 +01:00
|
|
|
EntitySelection.prototype.removeList = function(ents)
|
|
|
|
{
|
|
|
|
var removed = [];
|
|
|
|
|
|
|
|
for each (var ent in ents)
|
|
|
|
{
|
|
|
|
if (this.selected[ent])
|
|
|
|
{
|
|
|
|
this.groups.removeEnt(ent);
|
|
|
|
removed.push(ent);
|
|
|
|
delete this.selected[ent];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_setHighlight(removed, 0);
|
|
|
|
_setStatusBars(removed, false);
|
|
|
|
_setMotionOverlay(removed, false);
|
|
|
|
|
|
|
|
this.dirty = true;
|
|
|
|
};
|
|
|
|
|
2010-03-11 21:01:16 +01:00
|
|
|
EntitySelection.prototype.reset = function()
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
2010-08-08 19:28:51 +02:00
|
|
|
_setHighlight(this.toList(), 0);
|
2010-10-02 14:41:29 +02:00
|
|
|
_setStatusBars(this.toList(), false);
|
2010-04-30 01:36:05 +02:00
|
|
|
_setMotionOverlay(this.toList(), false);
|
2010-03-11 21:01:16 +01:00
|
|
|
this.selected = {};
|
2010-10-11 19:24:06 +02:00
|
|
|
this.groups.reset();
|
2010-05-01 18:20:58 +02:00
|
|
|
this.dirty = true;
|
2010-03-11 21:01:16 +01:00
|
|
|
};
|
2010-01-09 20:20:14 +01:00
|
|
|
|
2010-03-11 21:01:16 +01:00
|
|
|
EntitySelection.prototype.toList = function()
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
|
|
|
var ents = [];
|
2010-05-09 15:56:06 +02:00
|
|
|
for each (var ent in this.selected)
|
|
|
|
ents.push(ent);
|
2010-01-09 20:20:14 +01:00
|
|
|
return ents;
|
2010-03-11 21:01:16 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
EntitySelection.prototype.setHighlightList = function(ents)
|
|
|
|
{
|
2010-10-02 14:41:29 +02:00
|
|
|
var highlighted = {};
|
|
|
|
for each (var ent in ents)
|
|
|
|
highlighted[ent] = ent;
|
|
|
|
|
2010-03-11 21:01:16 +01:00
|
|
|
var removed = [];
|
|
|
|
var added = [];
|
|
|
|
|
2010-10-02 14:41:29 +02:00
|
|
|
// Remove highlighting for the old units that are no longer highlighted
|
|
|
|
// (excluding ones that are actively selected too)
|
2010-05-09 15:56:06 +02:00
|
|
|
for each (var ent in this.highlighted)
|
2010-10-02 14:41:29 +02:00
|
|
|
if (!highlighted[ent] && !this.selected[ent])
|
|
|
|
removed.push(+ent);
|
2010-03-11 21:01:16 +01:00
|
|
|
|
2010-10-02 14:41:29 +02:00
|
|
|
// Add new highlighting for units that aren't already highlighted
|
2010-03-11 21:01:16 +01:00
|
|
|
for each (var ent in ents)
|
2010-10-02 14:41:29 +02:00
|
|
|
if (!this.highlighted[ent] && !this.selected[ent])
|
|
|
|
added.push(+ent);
|
2010-03-11 21:01:16 +01:00
|
|
|
|
2010-08-08 19:28:51 +02:00
|
|
|
_setHighlight(removed, 0);
|
2010-10-02 14:41:29 +02:00
|
|
|
_setStatusBars(removed, false);
|
2010-03-11 21:01:16 +01:00
|
|
|
|
2010-10-02 14:41:29 +02:00
|
|
|
_setHighlight(added, 0.5);
|
|
|
|
_setStatusBars(added, true);
|
2010-03-11 21:01:16 +01:00
|
|
|
|
2010-10-02 14:41:29 +02:00
|
|
|
// Store the new highlight list
|
|
|
|
this.highlighted = highlighted;
|
2010-03-11 21:01:16 +01:00
|
|
|
};
|
|
|
|
|
2010-04-30 01:36:05 +02:00
|
|
|
EntitySelection.prototype.SetMotionDebugOverlay = function(enabled)
|
|
|
|
{
|
|
|
|
this.motionDebugOverlay = enabled;
|
|
|
|
_setMotionOverlay(this.toList(), enabled);
|
|
|
|
};
|
2010-03-11 21:01:16 +01:00
|
|
|
|
|
|
|
var g_Selection = new EntitySelection();
|