Array Grid Undo / Redo

Started by Ryan Timothy B, Fri 08/01/2010 23:38:07

Previous topic - Next topic

Ryan Timothy B

I have a little question for you guys.  I've got a grid dimension of 128x128 (using a struct), which would look like this  tilesX[0..127].Y[0..127] which hold the tile sprite number.

It's actually a level editor very similar to Starcraft's editor.  If I were to make an Undo feature, how would you guys do it?



I'd probably do something like this while the mouse is held down, and run this when a tile is changed:
Code: ags

function add_to_undo_array(int old_tile, int x, int y) {
  old_tileX[x].Y[y]=old_tile;  //having the same size as the map  old_tileX[0..127].Y[0..127]
  changed_amount++;
}


Then when the mouse is released:

Code: ags

int undo1[];
int undo1x[];
int undo1y[];

function create_array() {
  undo1 = new int[changed_amount];
  undo1x = new int[changed_amount];
  undo1y = new int[changed_amount];
  int ix;
  while (ix<gamex) {
    int iy;
    while (iy<gamey) {
      if (old_tileX[ix].Y[iy]!=null) {
        undo1[amount]=old_tileX[ix].Y[iy];
        undo1x[amount]=ix;
        undo1y[amount]=iy;
        amount++;
        old_tileX[ix].Y[iy]=null;
      }
      iy++;
    }
    ix++;
  }
}


Then when undo is pressed you take them from the newly created array.  You could also have multiple arrays for 5 or so undo levels.

btw: above code isn't tested or anything, I wrote it just as an example to show what I believe would be the best method in AGS.  So the question is..... Is this the best way?  Or would there be other ways?

RickJ

I'm not sure what you mean  by "while the mouse is held down".  Presumably you are dragging tiles into place.  Personally I would make a function that handles the  mouse_up event which would write
an entry into an edit journal.  Then I would have Do and Undo functions that take a journal index as their argument.   So the mouse up handler would write the command to the end of the journal and then execute Do() on that journal entry.

Code: ags

struct journal {
     int Command;  // necessary if there is more than one editor command
     int TileX;
     int TileY;
     int NewTile;
     int OldTile;
}
journal EditJournal[MAX_EDITS_PER_SESSION];



Ryan Timothy B

#2
Sorry RickJ, I assumed Level editor exactly like Starcraft's was self explanatory.

It's a grid of tiles, 128 tiles horizontal, by 128 tiles vertical.  When you click, or click and hold, tile sprite graphics are changed to accommodate the type of terrain you placed down.  Nothing to do with dragging, only replacing.
edit: Other example could be, imagine a wide brush in a paint program.


The tile placement and everything is complete, and I was just about to add an undo feature, but decided asking here could teach me something if my method was inferior to someone else's.

RickJ

Your method is simliar to what I suggested. The slight variation I suggested allows you to implement redo as well as undo.

SMF spam blocked by CleanTalk