@@ -55,7 +55,7 @@ public class Overworld
5555 private Texture2D docksIcons ;
5656 private Texture2D mapObjectsIcons ;
5757 private RenderTarget2D mapTexture ;
58- private byte [ ] overworldMap ;
58+ private byte [ ] overworldMap { get => targetMaps [ owMapCurrentTarget ] ; }
5959 private List < ( int id , OwEncounterGroup group ) > domains ;
6060 private List < ( OverworldTeleportIndex location , SCCoords coord ) > docks ;
6161 private List < ( MapObject mapobject , SCCoords coord ) > mapObjects ;
@@ -65,20 +65,32 @@ public class Overworld
6565 private GraphicsDevice graphicsDevice ;
6666 private SpriteBatch spriteBatch ;
6767 private List < SmartBrush > smartBrushes ;
68+ private int owMapCurrentTarget ;
69+ private int owMapBackSteps ;
70+ private int owMapForwardSteps ;
71+ private int owMapUndoDepth ;
72+ private bool currentlyDrawing ;
73+ private List < byte [ ] > targetMaps ;
74+ private List < List < ( int id , OwEncounterGroup group ) > > targetDomains ;
6875
6976 public bool UpdatePlacedMapObjects { get ; set ; }
7077 public bool UpdatePlacedDocks { get ; set ; }
7178 public bool UpdatePlacedRequiredTiles { get ; set ; }
7279 public bool UnsavedChanges { get ; set ; }
73- public Overworld ( Texture2D _tileset , SpriteFont _font , Texture2D _domaingroups , Texture2D _docks , Texture2D _mapobjects , GraphicsDevice _graphicsDevice , SpriteBatch _spriteBatch )
80+ public Overworld ( Texture2D _tileset , SpriteFont _font , Texture2D _domaingroups , Texture2D _docks , Texture2D _mapobjects , GraphicsDevice _graphicsDevice , SpriteBatch _spriteBatch , int _undodepth )
7481 {
7582 graphicsDevice = _graphicsDevice ;
7683 spriteBatch = _spriteBatch ;
7784 tileSet = _tileset ;
78- overworldMap = new byte [ 256 * 256 ] ;
85+ owMapCurrentTarget = 0 ;
86+ owMapBackSteps = 0 ;
87+ owMapForwardSteps = 0 ;
88+ currentlyDrawing = false ;
7989 domainGroupIcons = _domaingroups ;
8090 docksIcons = _docks ;
8191 mapObjectsIcons = _mapobjects ;
92+ owMapUndoDepth = _undodepth ;
93+ targetMaps = Enumerable . Range ( 0 , owMapUndoDepth + 1 ) . Select ( i => new byte [ 256 * 256 ] ) . ToList ( ) ;
8294 docks = new ( ) ;
8395 mapObjects = new ( ) ;
8496 mapTexture = new ( graphicsDevice , 4096 , 4096 , true , SurfaceFormat . Color , DepthFormat . Depth24 , 0 , RenderTargetUsage . PreserveContents ) ;
@@ -94,6 +106,57 @@ public Overworld(Texture2D _tileset, SpriteFont _font, Texture2D _domaingroups,
94106 UpdatePlacedRequiredTiles = true ;
95107 UnsavedChanges = false ;
96108 }
109+ private void CreateBackup ( )
110+ {
111+ int nextTarget = owMapCurrentTarget < owMapUndoDepth ? owMapCurrentTarget + 1 : 0 ;
112+ if ( owMapBackSteps < owMapUndoDepth )
113+ {
114+ owMapBackSteps ++ ;
115+ }
116+
117+ owMapForwardSteps = 0 ;
118+ targetMaps [ owMapCurrentTarget ] . CopyTo ( targetMaps [ nextTarget ] , 0 ) ;
119+ owMapCurrentTarget = nextTarget ;
120+ }
121+ public void Undo ( )
122+ {
123+ if ( owMapBackSteps == 0 )
124+ {
125+ return ;
126+ }
127+
128+ int previousTarget = owMapCurrentTarget > 0 ? owMapCurrentTarget - 1 : owMapUndoDepth ;
129+ owMapBackSteps -- ;
130+ if ( owMapForwardSteps < owMapUndoDepth )
131+ {
132+ owMapForwardSteps ++ ;
133+ }
134+
135+ owMapCurrentTarget = previousTarget ;
136+ GenerateInitialMap ( ) ;
137+ UpdatePlacedMapObjects = true ;
138+ UpdatePlacedDocks = true ;
139+ UpdatePlacedRequiredTiles = true ;
140+ }
141+ public void Redo ( )
142+ {
143+ if ( owMapForwardSteps == 0 )
144+ {
145+ return ;
146+ }
147+ owMapForwardSteps -- ;
148+ if ( owMapBackSteps < owMapUndoDepth )
149+ {
150+ owMapBackSteps ++ ;
151+ }
152+
153+ int nextTarget = owMapCurrentTarget < owMapUndoDepth ? owMapCurrentTarget + 1 : 0 ;
154+ owMapCurrentTarget = nextTarget ;
155+ GenerateInitialMap ( ) ;
156+ UpdatePlacedMapObjects = true ;
157+ UpdatePlacedDocks = true ;
158+ UpdatePlacedRequiredTiles = true ;
159+ }
97160 private void DefineSmarthBrushes ( )
98161 {
99162 smartBrushes = new ( )
@@ -110,7 +173,7 @@ private void DefineSmarthBrushes()
110173 }
111174 public void LoadData ( OwMapExchangeData mapdata )
112175 {
113- overworldMap = mapdata . DecodeMap ( ) ;
176+ mapdata . DecodeMap ( ) . CopyTo ( overworldMap , 0 ) ;
114177 domains = mapdata . DomainUpdates . Select ( d => ( ( int ) d . To , OwDataGroup . OwDomainsGroup [ d . From ] ) ) . ToList ( ) ;
115178 for ( int i = 0 ; i < 64 ; i ++ )
116179 {
@@ -139,9 +202,7 @@ public void LoadData(OwMapExchangeData mapdata)
139202 }
140203 public void ProcessTasks ( OwMapExchangeData mapdata , List < EditorTask > tasks )
141204 {
142- List < EditorTasks > managedTasks = new ( ) { EditorTasks . OverworldLoadMap , EditorTasks . OverworldBlueMap } ;
143-
144- var validtask = tasks . Where ( t => managedTasks . Contains ( t . Type ) ) . ToList ( ) ;
205+ var validtask = tasks . ToList ( ) ;
145206
146207 foreach ( var task in validtask )
147208 {
@@ -160,7 +221,16 @@ public void ProcessTasks(OwMapExchangeData mapdata, List<EditorTask> tasks)
160221 UpdatePlacedDocks = true ;
161222 UpdatePlacedRequiredTiles = true ;
162223 tasks . Remove ( task ) ;
163-
224+ }
225+ else if ( task . Type == EditorTasks . PaintingUndo )
226+ {
227+ Undo ( ) ;
228+ tasks . Remove ( task ) ;
229+ }
230+ else if ( task . Type == EditorTasks . PaintingRedo )
231+ {
232+ Redo ( ) ;
233+ tasks . Remove ( task ) ;
164234 }
165235 }
166236 }
@@ -284,6 +354,11 @@ public void UpdateTile(GraphicsDevice graphicsDevice, SpriteBatch spriteBatch, M
284354 {
285355 if ( ( ! mouse . LeftDown && ! mouse . LeftClick ) || ( ( tool . Tool != ToolAction . Pencil && tool . Tool != ToolAction . Brush ) ) )
286356 {
357+ if ( ! mouse . LeftDown && currentlyDrawing && ( tool . Tool == ToolAction . Pencil || tool . Tool == ToolAction . Brush ) )
358+ {
359+ currentlyDrawing = false ;
360+ }
361+
287362 return ;
288363 }
289364
@@ -300,6 +375,12 @@ public void UpdateTile(GraphicsDevice graphicsDevice, SpriteBatch spriteBatch, M
300375 return ;
301376 }
302377
378+ if ( currentlyDrawing == false )
379+ {
380+ CreateBackup ( ) ;
381+ currentlyDrawing = true ;
382+ }
383+
303384 int minsize = 0 ;
304385 int maxsize = 0 ;
305386
@@ -346,6 +427,11 @@ public void PlaceTemplate(GraphicsDevice graphicsDevice, SpriteBatch spriteBatch
346427 {
347428 if ( ( ! mouse . LeftDown && ! mouse . LeftClick ) || ( ( tool . Tool != ToolAction . Templates ) ) )
348429 {
430+ if ( ! mouse . LeftDown && currentlyDrawing && tool . Tool == ToolAction . Templates )
431+ {
432+ currentlyDrawing = false ;
433+ }
434+
349435 return ;
350436 }
351437
@@ -362,6 +448,12 @@ public void PlaceTemplate(GraphicsDevice graphicsDevice, SpriteBatch spriteBatch
362448 return ;
363449 }
364450
451+ if ( currentlyDrawing == false )
452+ {
453+ CreateBackup ( ) ;
454+ currentlyDrawing = true ;
455+ }
456+
365457 int sizex = tool . Template . GetLength ( 1 ) ;
366458 int sizey = tool . Template . GetLength ( 0 ) ;
367459
0 commit comments