Skip to content

Just create one Undo object per tile edit #80

@Joncom

Description

@Joncom

This property can be deleted:

oldData: null,

And so can these two methods:

beginEditing: function() {
this.oldData = ig.copy(this.data);
},
getOldTile: function( x, y ) {
var tx = Math.floor( x / this.tilesize );
var ty = Math.floor( y / this.tilesize );
if(
(tx >= 0 && tx < this.width) &&
(ty >= 0 && ty < this.height)
) {
return this.oldData[ty][tx];
}
else {
return 0;
}
},

And then patch weltmeister.js as follows:

diff --git a/lib/weltmeister/weltmeister.js b/lib/weltmeister/weltmeister.js
index 8c9bae9..7fd9d1c 100644
--- a/lib/weltmeister/weltmeister.js
+++ b/lib/weltmeister/weltmeister.js
@@ -717,14 +717,6 @@ wm.Weltmeister = ig.Class.extend({
                                        }
                                        else {
                                                this.undo.beginMapDraw();
-                                               this.activeLayer.beginEditing();
-                                               if( 
-                                                       this.activeLayer.linkWithCollision && 
-                                                       this.collisionLayer && 
-                                                       this.collisionLayer != this.activeLayer
-                                               ) {
-                                                       this.collisionLayer.beginEditing();
-                                               }
                                                this.setTileOnCurrentLayer();
                                        }
                                }
@@ -831,10 +823,12 @@ wm.Weltmeister = ig.Class.extend({
                                var mapy = y + by * this.activeLayer.tilesize;
                                
                                var newTile = brushRow[bx];
-                               var oldTile = this.activeLayer.getOldTile( mapx, mapy );
-                               
-                               this.activeLayer.setTile( mapx, mapy, newTile );
-                               this.undo.pushMapDraw( this.activeLayer, mapx, mapy, oldTile, newTile );
+                               var oldTile = this.activeLayer.getTile( mapx, mapy );
+
+                               if( newTile !== oldTile ) {
+                                       this.activeLayer.setTile( mapx, mapy, newTile );
+                                       this.undo.pushMapDraw( this.activeLayer, mapx, mapy, oldTile, newTile );
+                               }
                                
                                
                                if( 
@@ -845,8 +839,10 @@ wm.Weltmeister = ig.Class.extend({
                                        var collisionLayerTile = newTile > 0 ? this.collisionSolid : 0;
                                        
                                        var oldCollisionTile = this.collisionLayer.getOldTile(mapx, mapy);
-                                       this.collisionLayer.setTile( mapx, mapy, collisionLayerTile );
-                                       this.undo.pushMapDraw( this.collisionLayer, mapx, mapy, oldCollisionTile, collisionLayerTile );
+                                       if( collisionLayerTile !== oldCollisionTile ) {
+                                               this.collisionLayer.setTile( mapx, mapy, collisionLayerTile );
+                                               this.undo.pushMapDraw( this.collisionLayer, mapx, mapy, oldCollisionTile, collisionLayerTile );
+                                       }
                                }
                        }
                }

There is no need to keep a reference to oldData because the old data already gets passed into the undo and redo objects. Additionally, there is no need to make 10's of undo objects per single tile edit, which currently happens. The added if( newTile !== oldTile ) check ensures we only have one undo object per edit, which is necessary before we can delete the oldData array.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions