-
Notifications
You must be signed in to change notification settings - Fork 4
/
CloudHistory.as
158 lines (109 loc) · 3.72 KB
/
CloudHistory.as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package
{
import flash.events.Event;
import flash.display.BitmapData;
import mx.events.ItemClickEvent;
import mx.collections.ArrayCollection;
// Cloud Engine's History Manager
public class CloudHistory
{
// TODO: 1 history stack per Cloud
public static var historyList:ArrayCollection = new ArrayCollection(); // Array of Actions
public static var historyIndex:uint = 0;
// binary data (bitmapData)
public static var historyDataList:Array = new Array(); // Array of Data
// save binary for this action?
public static var historySaveBinary:Boolean = false;
// Constructor
public function CloudHistory()
{
}
// Save before Action
public static function action(actionName:String, historyClient:ICloudHistoryClient, saveBmpData:Boolean):void
{
// DO CLEAN: If actions exist after the last added, delete them
var cleanIndex:uint = historyIndex+1;
while(cleanIndex < historyList.length)
{
historyList.removeItemAt(cleanIndex);
// todo: remove binary?
}
// DO ACTION
historySaveBinary = saveBmpData; // save binary as well?
var stateXML:XML = historyClient.Save(); // save state XML
var actionEntry:Object = {name: historyIndex + ") " + actionName + (saveBmpData?' (B)':''),
icon: Globals.g_assets.AddTagIcon,
client: historyClient,
state: stateXML,
saveBitmapData: saveBmpData};
historyList.addItem(actionEntry); // add action in list
historyIndex = historyList.length-1; // position index to action at the end of the list
}
// Undo Last Action
public static function undo():void
{
if(historyIndex < 1) // Dont Undo the Canvas creation
return;
historyIndex--;
var actionEntry:Object = historyList.getItemAt(historyIndex); // retreive last entry
if(actionEntry && actionEntry.client && actionEntry.state)
{
actionEntry.client.Load( actionEntry.state ); // Load XML state
if(actionEntry.saveBitmapData)
{
// Load Previous bitmapData entry
}
}
// TODO: If index is historyList.length-1, Save state here, to redo exactly to our current state
}
// Redo Next Action
public static function redo():void
{
if(historyIndex >= historyList.length-1) // Cannot Redo what hasnt happened yet
return;
historyIndex++;
var actionEntry:Object = historyList.getItemAt(historyIndex); // retreive entry
if(actionEntry && actionEntry.client && actionEntry.state)
{
actionEntry.client.Load( actionEntry.state ); // Load XML state
if(actionEntry.saveBitmapData)
{
// Load Current bitmapData entry
}
}
}
// Save binary data (ie: bitmapData), return path
public static function saveBitmapData(bmpData:BitmapData):int
{
if(!historySaveBinary) // Dont save binary for this action
return -1;
// Copy BmpData
var newBmpData:BitmapData = new BitmapData(bmpData.width, bmpData.height, true, 0);
newBmpData.draw(bmpData);
var index:int = historyDataList.push(newBmpData); // push data in array, returns next slot index
return index-1; // return index
}
// Load binary data (ie: bitmapData), returns Data
public static function loadBitmapData(index:int):BitmapData
{
if(index < 0)
return null;
if(index < historyDataList.length)
{
return historyDataList[index]; // return binary data at specified index
// TODO: delete?
}
return null;
}
// Clear History
public static function clear():void
{
historyList.removeAll();
historyIndex = 0;
while(historyDataList.length)
{
historyDataList.pop(); // remove data
}
}
}
}