A way to make a Multi-level Undo / Redo

Example requests & Code samples for GdPicture Toolkits.
Post Reply
User avatar
Loïc
Site Admin
Posts: 5881
Joined: Tue Oct 17, 2006 10:48 pm
Location: France
Contact:

A way to make a Multi-level Undo / Redo

Post by Loïc » Sat Nov 01, 2008 6:41 pm

Hi!

There are many ways to add Undo / Redo feature using GdPicture Toolkits.

I give one which is simple and fast to implement.

The idea is to make an array of cloned image. Each entry of this array contains a state of the image on which we are working:


Create a public array containing the handle of the cloned image and a value defining the current array position

Code: Select all

Private Const UNDO_LEVEL = 10 'You can change this value to have an other undo/redo level
Dim arImagesUndo(0 To UNDO_LEVEL - 1) As Long
Dim nUndoIDX As Long = -1 '-1 means that we haven't yet saved image state

Create a SaveGraphicState procedure which saves the current image state. Call this procedure when you want to allow the user to undo the image modifications

Code: Select all

Private Sub SaveGraphicState()
   Dim i As Integer
   
   nUndoIDX = nUndoIDX + 1
   If nUndoIDX > UNDO_LEVEL - 1 Then
      Imaging1.CloseImage (arImagesUndo(0))
      For i = 0 To UNDO_LEVEL - 2
          arImagesUndo(i) = arImagesUndo(i + 1)
          nUndoIDX = UNDO_LEVEL - 1
      Next i
   Else
      For i = nUndoIDX + 1 To UNDO_LEVEL - 1
         If arImagesUndo(i) <> 0 Then
            Imaging1.CloseImage (arImagesUndo(i))
            arImagesUndo(i) = 0
         End If
      Next i
   End If
   arImagesUndo(nUndoIDX) = Imaging1.CreateClonedImage(Imaging1.GetNativeImage)  
End Sub

Undo the to the previous saved state

Code: Select all

Private Sub Undo()
   nUndoIDX = nUndoIDX - 1
   Imaging1.ClearImage (White)
   Call Imaging1.DrawImage(arImagesUndo(nUndoIDX), 0, 0, Imaging1.GetWidth, Imaging1.GetHeight, InterpolationModeNearestNeighbor)
End Sub

Redo to the next saved state

Code: Select all

Private Sub Redo()
   nUndoIDX = nUndoIDX + 1
   Imaging1.ClearImage (White)
   Call Imaging1.DrawImage(arImagesUndo(nUndoIDX), 0, 0, Imaging1.GetWidth, Imaging1.GetHeight, InterpolationModeNearestNeighbor)
End Sub

You can find an application of this method into the following samples:

- FreehandDraw vb6 sample of GdPicture Pro Imaging SDK
- FreehandDraw vb.net sample of GdPicture Pro Imaging SDK


Let me know any suggestion or question.


Loïc

bdcsoho
Posts: 26
Joined: Mon Sep 29, 2008 11:14 pm

Re: A way to make a Multi-level Undo / Redo

Post by bdcsoho » Sun Nov 09, 2008 8:56 pm

Hello Loïc,

I was sick over the last week therefore I did not have access to my computer... :cry:

I will go and look into this sample, to see how I can use it for the benefit of the program I am composing,

Again, thank you for posting the sample.

Sven

User avatar
Loïc
Site Admin
Posts: 5881
Joined: Tue Oct 17, 2006 10:48 pm
Location: France
Contact:

Re: A way to make a Multi-level Undo / Redo

Post by Loïc » Mon Nov 10, 2008 1:12 pm

Hi Sven,

Let me know if I was not clear enough or if you encounter problems to include this method on your application.

Best regards,

Loïc

mercdave
Posts: 5
Joined: Tue Oct 14, 2008 6:03 pm

Add Undo feature using GdPicture Toolkits for Delphi

Post by mercdave » Tue Nov 11, 2008 1:29 pm

I am using GDPicture to scan drawings and save as pdf's. I want to give users the ability of dropping inspection symbols (tri-angle) on the drawing and save the amended version. I have been able to drop the required shape on the drawing but want to give the user the ability to undo and re-drop the shape. I am using Delphi 7 and the relevant samples are in VB. How do I do it in Delphi?

Thanks


Dave

User avatar
Loïc
Site Admin
Posts: 5881
Joined: Tue Oct 17, 2006 10:48 pm
Location: France
Contact:

Re: A way to make a Multi-level Undo / Redo

Post by Loïc » Tue Nov 11, 2008 3:13 pm

Dave, I can't give the same sample for all the programming languages.

The sample above can be converted in Delphi very easily.

Thanks for your comprehension.

Best regards,

Loïc

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest