Ticket #2179: selectionCache.diff

File selectionCache.diff, 5.0 KB (added by mimo, 10 years ago)

Simplify the patch keeping only the small part which had nearly all the time saving

  • binaries/data/mods/public/gui/session/input.js

     
    16311631    if (!selection.length)
    16321632        return;
    16331633   
    1634     var trainableEnts = getAllTrainableEntities(selection);
     1634    var trainableEnts = getAllTrainableEntitiesFromSelection();
    16351635   
    16361636    // Check if the position is valid
    16371637    if (!trainableEnts.length || trainableEnts.length <= position)
  • binaries/data/mods/public/gui/session/selection.js

     
    217217 */
    218218EntitySelection.prototype.update = function()
    219219{
     220    var changed = false;
    220221    this.checkRenamedEntities();
    221222    for each (var ent in this.selected)
    222223    {
     
    227228        {
    228229            delete this.selected[ent];
    229230            this.groups.removeEnt(ent);
    230             this.dirty = true;
     231            changed = true;
    231232            continue;
    232233        }
    233234
     
    241242
    242243            delete this.selected[ent];
    243244            this.groups.removeEnt(ent);
    244             this.dirty = true;
     245            changed = true;
    245246            continue;
    246247        }
    247248    }
     249    if (changed)
     250        this.onChange();
    248251};
    249252
    250253/**
     
    318321    }
    319322
    320323    this.groups.add(this.toList()); // Create Selection Groups
    321     this.dirty = true;
     324    this.onChange();
    322325};
    323326
    324327EntitySelection.prototype.removeList = function(ents)
     
    339342    _setStatusBars(removed, false);
    340343    _setMotionOverlay(removed, false);
    341344
    342     this.dirty = true;
     345    this.onChange();
    343346};
    344347
    345348EntitySelection.prototype.reset = function()
     
    349352    _setMotionOverlay(this.toList(), false);
    350353    this.selected = {};
    351354    this.groups.reset();
    352     this.dirty = true;
     355    this.onChange();
    353356};
    354357
    355358EntitySelection.prototype.rebuildSelection = function(renamed)
     
    407410    _setMotionOverlay(this.toList(), enabled);
    408411};
    409412
    410 var g_Selection = new EntitySelection();
     413EntitySelection.prototype.onChange = function()
     414{
     415    this.dirty = true;
     416    if (this == g_Selection)
     417        onSelectionChange();
     418}
    411419
     420
    412421/**
    413422 * EntityGroupsContainer class for managing grouped entities
    414423 */
     
    483492}
    484493
    485494var g_Groups = new EntityGroupsContainer();
     495
     496/**
     497 * Cache some quantities which depends only on g_Selection
     498 */
     499
     500var g_Selection = new EntitySelection();
     501
     502var g_canMoveIntoFormation = {};
     503var g_allBuildableEntities = undefined;
     504var g_allTrainableEntities = undefined;
     505
     506// Reset cached quantities which depend on selection
     507function onSelectionChange()
     508{
     509    g_canMoveIntoFormation = {};
     510    g_allBuildableEntities = undefined;
     511    g_allTrainableEntities = undefined;
     512}
  • binaries/data/mods/public/gui/session/session.js

     
    327327    // simulation state and the current selection).
    328328    if (g_Selection.dirty)
    329329    {
     330        g_Selection.dirty = false;
     331
    330332        onSimulationUpdate();
    331333
    332334        // Display rally points for selected buildings
     
    427429 */
    428430function onSimulationUpdate()
    429431{
    430     g_Selection.dirty = false;
    431432    g_EntityStates = {};
    432433    g_TemplateData = {};
    433434    g_TechnologyData = {};
  • binaries/data/mods/public/gui/session/unit_commands.js

     
    572572        // Get icon image
    573573        if (guiName == FORMATION)
    574574        {
    575             var formationOk = Engine.GuiInterfaceCall("CanMoveEntsIntoFormation", {
    576                 "ents": g_Selection.toList(),
    577                 "formationName": item
    578             });
    579 
     575            var formationOk = canMoveSelectionIntoFormation(item);
    580576            var grayscale = "";
    581577            button.enabled = formationOk;
    582578            if (!formationOk)
     
    12721268    return trainableEnts;
    12731269}
    12741270
     1271function getAllTrainableEntitiesFromSelection()
     1272{
     1273    if (!g_allTrainableEntities)
     1274        g_allTrainableEntities = getAllTrainableEntities(g_Selection.toList());
     1275
     1276    return g_allTrainableEntities;
     1277}
     1278
    12751279// Get all of the available entities which can be built by the selected entities
    12761280function getAllBuildableEntities(selection)
    12771281{
     
    12881292    removeDupes(buildableEnts);
    12891293    return buildableEnts;
    12901294}
     1295
     1296function getAllBuildableEntitiesFromSelection()
     1297{
     1298    if (!g_allBuildableEntities)
     1299        g_allBuildableEntities = getAllBuildableEntities(g_Selection.toList());
     1300
     1301    return g_allBuildableEntities;
     1302}
     1303
     1304// Check if the selection can move into formation, and cache the result
     1305function canMoveSelectionIntoFormation(formationName)
     1306{
     1307    if (!(formationName in g_canMoveIntoFormation))
     1308    {
     1309        g_canMoveIntoFormation[formationName] = Engine.GuiInterfaceCall("CanMoveEntsIntoFormation", {
     1310            "ents": g_Selection.toList(),
     1311            "formationName": formationName
     1312        });
     1313    }
     1314    return g_canMoveIntoFormation[formationName];
     1315}