OSDN Git Service

open
[eliscolors/main.git] / ElisUndoManager.m
1 //  Copyright (c) 2009 Yanagi Asakura
2 //
3 //  This software is provided 'as-is', without any express or implied
4 //  warranty. In no event will the authors be held liable for any damages
5 //  arising from the use of this software.
6 //
7 //  Permission is granted to anyone to use this software for any purpose,
8 //  including commercial applications, and to alter it and redistribute it
9 //  freely, subject to the following restrictions:
10 //
11 //  1. The origin of this software must not be misrepresented; you must not
12 //  claim that you wrote the original software. If you use this software
13 //  in a product, an acknowledgment in the product documentation would be
14 //  appreciated but is not required.
15 //
16 //  2. Altered source versions must be plainly marked as such, and must not be
17 //  misrepresented as being the original software.
18 //
19 //  3. This notice may not be removed or altered from any source
20 //  distribution.
21
22 //
23 //  ElisUndoManager.m
24 //  Elis Colors
25 //
26 //  Created by 柳 on 09/09/24.
27 //  Copyright 2009 __MyCompanyName__. All rights reserved.
28 //
29
30 #import "ElisUndoManager.h"
31
32
33 @implementation ElisUndoManager
34
35 - (void)awakeFromNib
36 {
37     undoStack = [[NSMutableArray alloc] init];
38     index = 0;
39 }
40
41 - (void)pushOperation:(ElisLayer*)layer
42 {
43     NSMutableData* data = [[NSMutableData alloc] init];
44     NSKeyedArchiver* encoder = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
45     
46     [layer saveToEncoder:encoder];
47     [encoder finishEncoding];
48     
49 //    [undoStack addObject:layer];
50 //    [undoStack addObject:data];
51     [undoStack removeObjectsInRange:NSMakeRange(index, [undoStack count] - index)];
52     
53     if(index != 0)
54         if([data isEqualToData:[undoStack objectAtIndex:index-1]])
55             return;
56     
57     [undoStack insertObject:layer atIndex:index];
58     [undoStack insertObject:data atIndex:index+1];
59     index += 2;
60 }
61
62 - (ElisLayer*)popOperation
63 {
64     NSData* data = [undoStack objectAtIndex:index-1];
65     ElisLayer* layer = [undoStack objectAtIndex:index-2];
66     
67     NSKeyedUnarchiver* decoder = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];    
68     [layer loadFromDecoder:decoder];
69     [decoder finishDecoding];
70     
71     index -= 2;
72     
73     return layer;
74 }
75
76 - (void)redoOperation
77 {
78     index += 4;
79     [self popOperation];
80 }
81
82 - (BOOL)canUndo
83 {
84     return index != 0;
85     return [undoStack count] != 0;
86 }
87
88 - (BOOL)canRedo
89 {
90     return index+2 < [undoStack count];
91 }
92
93 @end