Changeset View
Standalone View
binaries/data/mods/public/gui/session/selection.js
Show First 20 Lines • Show All 285 Lines • ▼ Show 20 Lines | EntitySelection.prototype.addList = function(ents, quiet, force = false) | ||||
// If someone else's player is the sole selected unit, don't allow adding to the selection | // If someone else's player is the sole selected unit, don't allow adding to the selection | ||||
let firstEntState = selection.length == 1 && GetEntityState(selection[0]); | let firstEntState = selection.length == 1 && GetEntityState(selection[0]); | ||||
if (firstEntState && firstEntState.player != g_ViewedPlayer && !force) | if (firstEntState && firstEntState.player != g_ViewedPlayer && !force) | ||||
return; | return; | ||||
let i = 1; | let i = 1; | ||||
let added = []; | let added = []; | ||||
ents = this.addBattalionMembers(ents); | |||||
Stan: It seems a bit dirty to overwrite the function param. | |||||
for (let ent of ents) | for (let ent of ents) | ||||
{ | { | ||||
if (selection.length + i > g_MaxSelectionSize) | if (selection.length + i > g_MaxSelectionSize) | ||||
break; | break; | ||||
if (this.selected[ent]) | if (this.selected[ent]) | ||||
continue; | continue; | ||||
Show All 26 Lines | EntitySelection.prototype.addList = function(ents, quiet, force = false) | ||||
} | } | ||||
this.groups.add(this.toList()); // Create Selection Groups | this.groups.add(this.toList()); // Create Selection Groups | ||||
this.onChange(); | this.onChange(); | ||||
}; | }; | ||||
EntitySelection.prototype.removeList = function(ents) | EntitySelection.prototype.removeList = function(ents) | ||||
{ | { | ||||
var removed = []; | let removed = []; | ||||
ents = this.addBattalionMembers(ents); | |||||
Done Inline ActionsI'd name the argument 'treatFormationsAsOne' or something like that. You can leave the explanation for the orderOne case. wraitii: I'd name the argument 'treatFormationsAsOne' or something like that. You can leave the… | |||||
Done Inline ActionsYeah I meant to ask for that too, because it might be confused with the orderone feature [ALT] keyword. Stan: Yeah I meant to ask for that too, because it might be confused with the orderone feature [ALT]… | |||||
for (let ent of ents) | for (let ent of ents) | ||||
if (this.selected[ent]) | if (this.selected[ent]) | ||||
{ | { | ||||
this.groups.removeEnt(ent); | this.groups.removeEnt(ent); | ||||
removed.push(ent); | removed.push(ent); | ||||
delete this.selected[ent]; | delete this.selected[ent]; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 50 Lines • ▼ Show 20 Lines | EntitySelection.prototype.find = function(condition) | ||||
for (let ent in this.selected) | for (let ent in this.selected) | ||||
if (condition(ent)) | if (condition(ent)) | ||||
return +ent; | return +ent; | ||||
return null; | return null; | ||||
}; | }; | ||||
EntitySelection.prototype.setHighlightList = function(ents) | EntitySelection.prototype.setHighlightList = function(ents) | ||||
{ | { | ||||
var highlighted = {}; | let highlighted = {}; | ||||
Done Inline ActionsForgot this one :) Stan: Forgot this one :) | |||||
Done Inline ActionsNot really, ents is used twice here in this function. ;) Freagarach: Not really, `ents` is used twice here in this function. ;) | |||||
ents = this.addBattalionMembers(ents); | |||||
for (let ent of ents) | for (let ent of ents) | ||||
highlighted[ent] = ent; | highlighted[ent] = ent; | ||||
var removed = []; | let removed = []; | ||||
var added = []; | let added = []; | ||||
// Remove highlighting for the old units that are no longer highlighted | // Remove highlighting for the old units that are no longer highlighted | ||||
// (excluding ones that are actively selected too) | // (excluding ones that are actively selected too) | ||||
for (let ent in this.highlighted) | for (let ent in this.highlighted) | ||||
if (!highlighted[ent] && !this.selected[ent]) | if (!highlighted[ent] && !this.selected[ent]) | ||||
removed.push(+ent); | removed.push(+ent); | ||||
// Add new highlighting for units that aren't already highlighted | // Add new highlighting for units that aren't already highlighted | ||||
Show All 29 Lines | EntitySelection.prototype.selectAndMoveTo = function(entityID) | ||||
let entState = GetEntityState(entityID); | let entState = GetEntityState(entityID); | ||||
if (!entState || !entState.position) | if (!entState || !entState.position) | ||||
return; | return; | ||||
this.reset(); | this.reset(); | ||||
this.addList([entityID]); | this.addList([entityID]); | ||||
Engine.CameraMoveTo(entState.position.x, entState.position.z); | Engine.CameraMoveTo(entState.position.x, entState.position.z); | ||||
}; | |||||
/** | |||||
* Adds the battalion members of a selected entities to the selection. | |||||
* @param {number[]} entities - The entity IDs of selected entities. | |||||
* @return {number[]} - Some more entity IDs if part of a battalion was selected. | |||||
*/ | |||||
EntitySelection.prototype.addBattalionMembers = function(entities) | |||||
{ | |||||
if (entities.length) | |||||
{ | |||||
let battalions = Engine.GuiInterfaceCall("GetBattalions", { "players": undefined }); | |||||
for (let battalion of battalions) | |||||
if (entities.some(entity => battalion.entities.indexOf(entity) != -1)) | |||||
entities = entities.concat(battalion.entities.filter(entity => entities.indexOf(entity) == -1)); | |||||
} | } | ||||
return entities; | |||||
}; | |||||
/** | /** | ||||
* Cache some quantities which depends only on selection | * Cache some quantities which depends only on selection | ||||
*/ | */ | ||||
var g_Selection = new EntitySelection(); | var g_Selection = new EntitySelection(); | ||||
g_Selection.isSelection = true; | g_Selection.isSelection = true; | ||||
var g_canMoveIntoFormation = {}; | var g_canMoveIntoFormation = {}; | ||||
Done Inline ActionsMove below conditions Silier: Move below conditions | |||||
Done Inline ActionsCan entities be null? Should we check for it? Stan: Can entities be null? Should we check for it? | |||||
Done Inline ActionsThe rest doesn't check either, so I don't think we should check for it. (This errors out nicely when called with nullish value.) Freagarach: The rest doesn't check either, so I don't think we should check for it. (This errors out nicely… | |||||
var g_allBuildableEntities; | var g_allBuildableEntities; | ||||
var g_allTrainableEntities; | var g_allTrainableEntities; | ||||
Done Inline ActionsDo you really need that check ? Stan: Do you really need that check ? | |||||
Done Inline ActionsNot sure, I'll test. I thought it might save some performance perhaps, when not selecting a grouped entity. Freagarach: Not sure, I'll test. I thought it might save some performance perhaps, when not selecting a… | |||||
Done Inline ActionsApparently we do, otherwise hovering doesn't work. Freagarach: Apparently we do, otherwise hovering doesn't work. | |||||
Done Inline ActionsYou could inline here Silier: You could inline here | |||||
Done Inline ActionsI wonder if you couldn't save some cycles by filtering then checking the length and if more than 0 assign ? Would avoid the some. Stan: I wonder if you couldn't save some cycles by filtering then checking the length and if more… | |||||
Done Inline ActionsIt is again hovering that holds this off. Not sure why though,,, Freagarach: It is again hovering that holds this off. Not sure why though,,, | |||||
// Reset cached quantities | // Reset cached quantities | ||||
function onSelectionChange() | function onSelectionChange() | ||||
{ | { | ||||
g_canMoveIntoFormation = {}; | g_canMoveIntoFormation = {}; | ||||
g_allBuildableEntities = undefined; | g_allBuildableEntities = undefined; | ||||
Done Inline ActionsI measured somewhere that concat was really slow and that it's better to have a for loop with .push, but not sure it's worth the hassle. Stan: I measured somewhere that concat was really slow and that it's better to have a for loop with . | |||||
Done Inline ActionsAh, since this is called often in a large selection, it might indeed be worth the hassle. It may even be worth using a Set. Freagarach: Ah, since this is called often in a large selection, it might indeed be worth the hassle. It… | |||||
g_allTrainableEntities = undefined; | g_allTrainableEntities = undefined; | ||||
} | } | ||||
/** | /** | ||||
* EntityGroupsContainer class for managing grouped entities | * EntityGroupsContainer class for managing grouped entities | ||||
*/ | */ | ||||
function EntityGroupsContainer() | function EntityGroupsContainer() | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 59 Lines • Show Last 20 Lines |
It seems a bit dirty to overwrite the function param.