1 module editorEvents;
2 
3 import std.stdio;
4 import std.utf : toUTF8, toUTF32;
5 
6 import editor;
7 import serializer;
8 import types;
9 import sdlang;
10 
11 public import pixelperfectengine.concrete.eventchainsystem;
12 import pixelperfectengine.concrete.elements;
13 
14 import sdlang.token : Value;
15 
16 public static DummyWindow dwtarget;
17 public static WindowSerializer wserializer;
18 public static Editor editorTarget;
19 
20 public class PlacementEvent : UndoableEvent{
21 	private WindowElement element;
22 	private string type;
23 	private string name;
24 	private Tag backup;
25 	public this(WindowElement element, string type, string name){
26 		this.element = element;
27 		this.type = type;
28 		this.name = name;
29 	}
30 	public void redo(){
31 		try{
32 			wserializer.addElement(type, name, element.getPosition);
33 			dwtarget.addElement(element);
34 			editorTarget.elements[name] = ElementInfo(element, name, type);
35 		}catch(Exception e){
36 			writeln(e);
37 		}
38 		editorTarget.updateElementList;
39 	}
40 	public void undo(){
41 		backup = wserializer.removeElement(name);
42 		dwtarget.removeElement(element);
43 		editorTarget.elements.remove(name);
44 		editorTarget.updateElementList;
45 	}
46 }
47 
48 public class DeleteEvent : UndoableEvent{
49 	private Tag backup;
50 	ElementInfo eleminfo;
51 	public this(ElementInfo eleminfo){
52 		this.eleminfo = eleminfo;
53 	}
54 	public void redo(){
55 		backup = wserializer.removeElement(eleminfo.name);
56 		dwtarget.removeElement(eleminfo.element);
57 		editorTarget.elements.remove(eleminfo.name);
58 		editorTarget.updateElementList;
59 	}
60 	public void undo(){
61 		try{
62 			wserializer.addElement(backup);
63 			dwtarget.addElement(eleminfo.element);
64 			editorTarget.elements[eleminfo.name] = eleminfo;
65 		}catch(Exception e){
66 			writeln(e);
67 		}
68 		editorTarget.updateElementList;
69 	}
70 }
71 
72 public class AttributeEditEvent : UndoableEvent{
73 	private Value[] oldVal, newVal;
74 	private string attributeName, targetName;
75 	public this(Value[] newVal, string attributeName, string targetName){
76 		this.newVal = newVal;
77 		this.attributeName = attributeName;
78 		this.targetName = targetName;
79 	}
80 	public void redo(){
81 		oldVal = wserializer.editValue(targetName, attributeName, newVal);
82 	}
83 	public void undo(){
84 		wserializer.editValue(targetName, attributeName, oldVal);
85 	}
86 }
87 
88 public class TextEditEvent : AttributeEditEvent{
89 	private dstring oldText, newText;
90 	public this(dstring newText, string targetName){
91 		this.newText = newText;
92 		this.oldText = editorTarget.elements[targetName].element.getText.text;
93 		super([Value(toUTF8(newText))], "text", targetName);
94 	}
95 	public override void redo(){
96 		super.redo;
97 		editorTarget.elements[targetName].element.setText(newText);
98 	}
99 	public override void undo(){
100 		super.undo;
101 		editorTarget.elements[targetName].element.setText(oldText);
102 	}
103 }
104 
105 public class SourceEditEvent : AttributeEditEvent{
106 	//private string oldText, newText;
107 	public this(string newText, string targetName){
108 		super([Value(newText)], "source", targetName);
109 	}
110 }
111 
112 public class PositionEditEvent : AttributeEditEvent{
113 	private Coordinate oldPos, newPos;
114 	public this(Coordinate newPos, string targetName){
115 		this.newPos = newPos;
116 		this.oldPos = editorTarget.elements[targetName].element.getPosition;
117 		super([Value(newPos.left), Value(newPos.top), Value(newPos.right), Value(newPos.bottom)], "position", targetName);
118 	}
119 	public override void redo(){
120 		super.redo;
121 		editorTarget.elements[targetName].element.setPosition(newPos);
122 		dwtarget.draw;
123 	}
124 	public override void undo(){
125 		super.undo;
126 		editorTarget.elements[targetName].element.setPosition(newPos);
127 		dwtarget.draw;
128 	}
129 }
130 
131 public class WindowAttributeEditEvent : UndoableEvent{
132 	private Value[] oldVal, newVal;
133 	private string attributeName;
134 	public this(string attributeName, Value[] newVal){
135 		this.attributeName = attributeName;
136 		this.newVal = newVal;
137 	}
138 	public void redo(){
139 		oldVal = wserializer.editWindowValue(attributeName, newVal);
140 	}
141 	public void undo(){
142 		wserializer.editWindowValue(attributeName, oldVal);
143 	}
144 }
145 
146 public class WindowRetitleEvent : WindowAttributeEditEvent{
147 	private dstring oldTitle, newTitle;
148 	public this(dstring title){
149 		newTitle = title;
150 		super("title", [Value(toUTF8(title))]);
151 		oldTitle = dwtarget.getTitle().text;
152 	}
153 	public override void redo(){
154 		dwtarget.setTitle(newTitle);
155 		super.redo;
156 	}
157 	public override void undo(){
158 		dwtarget.setTitle(oldTitle);
159 		super.undo;
160 	}
161 }
162 
163 public class WindowRenameEvent : UndoableEvent{
164 	private string oldName, newName;
165 	public this(string newName){
166 		this.newName = newName;
167 	}
168 	public void redo(){
169 		oldName = wserializer.renameWindow(newName);
170 	}
171 	public void undo(){
172 		wserializer.renameWindow(oldName);
173 	}
174 }
175 
176 public class WindowWidthChangeEvent : WindowAttributeEditEvent{
177 	private int oldWidth, newWidth;
178 	public this(int newWidth){
179 		this.newWidth = newWidth;
180 		super("size:x", [Value(newWidth)]);
181 		oldWidth = dwtarget.getPosition.width;
182 	}
183 	public override void redo(){
184 		dwtarget.setWidth(newWidth);
185 		super.redo;
186 	}
187 	public override void undo(){
188 		dwtarget.setWidth(oldWidth);
189 		super.undo;
190 	}
191 }
192 
193 public class WindowHeightChangeEvent : WindowAttributeEditEvent{
194 	private int oldHeight, newHeight;
195 	public this(int newHeight){
196 		this.newHeight = newHeight;
197 		super("size:y", [Value(newHeight)]);
198 		oldHeight = dwtarget.getPosition.height;
199 	}
200 	public override void redo(){
201 		dwtarget.setHeight(newHeight);
202 		super.redo;
203 	}
204 	public override void undo(){
205 		dwtarget.setHeight(oldHeight);
206 		super.undo;
207 	}
208 }
209 
210 public class RenameEvent : UndoableEvent {
211 	private string oldName, newName;
212 	private ElementInfo eleminfo, backup;
213 	public this(string oldName, string newName){
214 		this.oldName = oldName;
215 		this.newName = newName;
216 		eleminfo = editorTarget.elements[oldName];
217 		backup = eleminfo;
218 		eleminfo.name = newName;
219 	}
220 	public void redo(){
221 		wserializer.renameElement(oldName, newName);
222 		editorTarget.elements[newName] = eleminfo;
223 		editorTarget.elements.remove(oldName);
224 		editorTarget.updateElementList;
225 	}
226 	public void undo(){
227 		wserializer.renameElement(newName, oldName);
228 		editorTarget.elements[oldName] = backup;
229 		editorTarget.elements.remove(newName);
230 		editorTarget.updateElementList;
231 	}
232 }
233 
234 public class MoveElemEvent : UndoableEvent {
235 	private Box oldPos, newPos;
236 	private string target;
237 	public this(Box newPos, Box oldPos, string target){
238 		this.newPos = newPos;
239 		this.oldPos = oldPos;
240 		this.target = target;
241 	}
242 	public this(Box newPos, string target) {
243 		this.newPos = newPos;
244 		this.oldPos = editorTarget.elements[target].element.getPosition();
245 		this.target = target;
246 	}
247 	public void redo() {
248 		wserializer.editValue(target, "position", [Value(newPos.left), Value(newPos.top), Value(newPos.right), 
249 				Value(newPos.bottom)]);
250 		//oldPos = editorTarget.elements[target].position;
251 		editorTarget.elements[target].element.setPosition(newPos);
252 		dwtarget.draw();
253 	}
254 	public void undo() {
255 		wserializer.editValue(target, "position", [Value(oldPos.left), Value(oldPos.top), Value(oldPos.right), 
256 				Value(oldPos.bottom)]);
257 		editorTarget.elements[target].element.setPosition(oldPos);
258 		dwtarget.draw();
259 	}
260 }
261 
262 public class ListViewHeaderEditEvent : UndoableEvent {
263 	private Tag oldHeader;
264 	private Tag[] newHeader;
265 	private ListViewHeader oldHeader0, newHeader0;
266 	private string target;
267 	public this(Tag[] newHeader, ListViewHeader newHeader0, string target) {
268 		this.newHeader = newHeader;
269 		this.newHeader0 = newHeader0;
270 		this.target = target;
271 	}
272 	public void redo() {
273 		oldHeader = wserializer.getTag(target, "header");
274 		Tag t = new Tag(null, "header", oldHeader.values, null, newHeader);
275 		wserializer.replaceTag(target, "header", t);
276 		ListView lw = cast(ListView)editorTarget.elements[target].element;
277 		lw.setHeader(newHeader0, null);
278 		dwtarget.draw();
279 	}
280 	public void undo() {
281 		wserializer.replaceTag(target, "header", oldHeader);
282 		ListView lw = cast(ListView)editorTarget.elements[target].element;
283 		lw.setHeader(oldHeader0, null);
284 		dwtarget.draw();
285 	}
286 }