Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Just create one Undo object per tile edit #80

Open
Joncom opened this issue Jun 17, 2020 · 0 comments
Open

Just create one Undo object per tile edit #80

Joncom opened this issue Jun 17, 2020 · 0 comments

Comments

@Joncom
Copy link
Collaborator

Joncom commented Jun 17, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant