krzysztofzablocki / LineDrawing

Beatiful and fast smooth line drawing algorithm for iOS - as seen in Foldify.
http://twitter.com/merowing_
Other
1.29k stars 213 forks source link

How can i add undo redo functions? #6

Closed yatanadam closed 11 years ago

yatanadam commented 11 years ago

Hi guy. How can i add undo redo functions? can you help ?

postmechanical commented 11 years ago

You need to learn how to use NSUndoManager. When the pan gesture ends you need to get an image from the scene and push it on the undo stack. On undo you need to pop the undo stack and set the image back as the scene's image. You'll need to declare undo and redo methods to handle this.

Von meinem iPhone gesendet

Am Oct 19, 2012 um 6:24 AM schrieb yatanadam notifications@github.com:

Hi guy. How can i add undo redo functions? can you help ?

— Reply to this email directly or view it on GitHub.

yatanadam commented 11 years ago

In fact i don't want to go on image by image. I think it isn't good way for ram. I need a method that works line by line. Thanks for answer alondon.

postmechanical commented 11 years ago

Then you'll need to save off all the paths as they are drawn, pushing sets of those onto the undo stack, and when undoing or redoing then regenerate the scene buffer contents from the desired set of paths. Note that is a far more significant refactoring of the scene code than the image based approach. To save memory you could write out image buffer contents to files and push paths/urls onto the undo stack.

On Oct 19, 2012, at 8:47 AM, yatanadam notifications@github.com wrote:

In fact i don't want to go on image by image. I think it isn't good way for ram. I need a method that works line by line. Thanks for answer alondon.

— Reply to this email directly or view it on GitHub.

yatanadam commented 11 years ago

Thanks for answer Alondon.I will try your method too. but now i wanna know where is my mistake. there is my code that what i tried.

-(void)undoButtonClicked{ [finishedLines removeLastObject]; [velocities removeAllObjects]; [circlesPoints removeAllObjects]; [renderTexture clear:1.0f g:1.0f b:1.0f a:1.0f]; delete=FALSE;

for (int i=0;i< [finishedLines count]; i++) {
    if ([finishedLines objectAtIndex:i]== [NSNumber numberWithInt:1]) {
        finishingLine=TRUE;
        connectingLine=false;
        i++;

    }
    else{
        finishingLine=false;
        connectingLine =false;
    }
 points=[finishedLines objectAtIndex:i];        
    NSLog(@"afterdelete points cunt:%d",[points count]);

ccColor4F color = {0, 0, 0, 1};
[renderTexture begin];

NSMutableArray *smoothedPoints = [self calculateSmoothLinePoints];
if (smoothedPoints) {
    [self drawLines:smoothedPoints withColor:color];
}
[renderTexture end];
}

AND

in calculatesmooth method{

if([points count]>2)

 if(delete){
      [finishedLines addobject:points]

}

}

This code doesnt work well for me.

krzysztofzablocki commented 11 years ago

The whole point of using render texture was to make it high performance and keep steady fps, that's why @alondon answer is the right one ( with pusing and poping state ). Other way would be to keep all the lines from start and whenever user presses undo remove last line and redraw whole image.

yatanadam commented 11 years ago

Like you said Krzysztofzablocki, i tried keep all lines from start and redraw them. But i could't. there is my code above your comment. Can you check it and tell the mistakes ? Thanks

miralem-cebic commented 11 years ago

can somebody send me a code snippet for this issue?