1 module addTiles;
2 
3 import PixelPerfectEngine.concrete.window;
4 import PixelPerfectEngine.graphics.common;
5 import PixelPerfectEngine.extbmp.extbmp;
6 import PixelPerfectEngine.map.mapload;
7 import PixelPerfectEngine.graphics.layers;
8 import PixelPerfectEngine.graphics.paletteMan;
9 
10 public class LayerTypeException : Exception{
11 	public this(string msg, string file = __FILE__, size_t line =  __LINE__, Throwable next = null) {
12 		super(msg, file, line, next);
13 	}
14 }
15 
16 public class AddTiles : Window{
17 	public enum AcceptedBMPType{
18 		ALL,
19 		Indexed4Bit,
20 		Indexed8Bit,
21 		Indexed16Bit,
22 		DirectColor32Bit,
23 	}
24 	private struct AddedBitmaps{
25 		wchar ID;
26 		string name;
27 		string type;
28 		string srcID;
29 		string palette;
30 	}
31 	private AddedBitmaps[] abList;
32 	ListBox listBox_Added;
33 	ListBox listBox_Available;
34 	Button button_Add;
35 	Button button_Remove;
36 	Button button_Finish;
37 	Button button_ABI;
38 	Label label1;
39 	Label label2;
40 	Label label3;
41 	TextBox textBox_Name;
42 	TextBox textBox_ID;
43 	Label label4;
44 	ExtendibleBitmap source;
45 	ExtendibleMap document;
46 	AcceptedBMPType acceptedTypes;
47 	int x, y, layerNum;
48 	ITileLayer target;
49 	PaletteManager palman;
50 
51 	this(ExtendibleBitmap source, ExtendibleMap document, AcceptedBMPType acceptedTypes, int x, int y, int layerNum, ITileLayer target, PaletteManager palman){
52 		super(Coordinate(0, 0, 485, 340), "Add tiles"d);
53 		this.palman = palman;
54 		this.target = target;
55 		this.source = source;
56 		this.document = document;
57 		this.acceptedTypes = acceptedTypes;
58 		this.x = x;
59 		this.y = y;
60 		listBox_Added = new ListBox("listBox_Added",Coordinate(5, 20, 245, 225), null, new ListBoxHeader(["ID"d, "Name"d, "Type"d, "SrcID"d, "Pal"d], [32, 128, 48, 200, 80]), 16);
61 		addElement(listBox_Added, EventProperties.MOUSE);
62 		listBox_Available = new ListBox("listBox_Available",Coordinate(250, 20, 480, 225), null, new ListBoxHeader(["ID"d, "Type"d], [200, 48]), 16);
63 		addElement(listBox_Available, EventProperties.MOUSE);
64 		button_Add = new Button("Add"d, "button_Add", Coordinate(335, 230, 405, 250));
65 		addElement(button_Add, EventProperties.MOUSE);
66 		button_Add.onMouseLClickRel = &button_Add_mouseClick;
67 		button_Remove = new Button("Remove"d, "button_Remove", Coordinate(410, 230, 480, 250));
68 		addElement(button_Remove, EventProperties.MOUSE);
69 		button_Remove.onMouseLClickRel = &button_Remove_mouseClick;
70 		button_Finish = new Button("Finish"d, "button_Finish", Coordinate(410, 255, 480, 275));
71 		addElement(button_Finish, EventProperties.MOUSE);
72 		button_Finish.onMouseLClickRel = &button_Finish_mouseClick;
73 		button_ABI = new Button("Add by intrinsic"d, "button_ABI", Coordinate(250, 255, 405, 275));
74 		addElement(button_ABI, EventProperties.MOUSE);
75 		button_ABI.onMouseLClickRel = &button_ABI_mouseClick;
76 		label1 = new Label("Name:"d, "label1", Coordinate(5, 232, 66, 248));
77 		addElement(label1, EventProperties.MOUSE);
78 		label2 = new Label("ID:"d, "label2", Coordinate(5, 257, 68, 273));
79 		addElement(label2, EventProperties.MOUSE);
80 		label3 = new Label("Intrinsic: foretag####aftertag"d, "label3", Coordinate(5, 282, 400, 300));
81 		addElement(label3, EventProperties.MOUSE);
82 		textBox_Name = new TextBox("NULL"d, "textBox_Name", Coordinate(55, 230, 330, 250));
83 		addElement(textBox_Name, EventProperties.MOUSE);
84 		textBox_ID = new TextBox("0x0000"d, "textBox_ID", Coordinate(55, 255, 150, 275));
85 		addElement(textBox_ID, EventProperties.MOUSE);
86 		label4 = new Label("Start with \"[h]\" to use hex numbering system."d, "label4", Coordinate(5, 307, 479, 323));
87 		addElement(label4, EventProperties.MOUSE);
88 		getAvailableBitmaps;
89 	}
90 
91 	private void button_Remove_mouseClick(Event e){
92 		import std.algorithm;
93 		abList.remove(e.value);
94 		listBox_Added.removeLine(e.value);
95 	}
96 	private void button_Add_mouseClick(Event e){
97 		import std.conv;
98 		foreach(x; abList){
99 			if(x.srcID == to!string(listBox_Available.readLine(listBox_Available.selection).getText(0))){
100 				return;
101 			}
102 		}
103 		AddedBitmaps ab;
104 		// parse ID
105 		try{
106 			int x = to!int(textBox_ID.getText);
107 			ab.ID = to!wchar(x);
108 		}catch(Exception e){
109 
110 		}
111 		// get name
112 		try{
113 			ab.name = to!string(textBox_Name.getText);
114 		}catch(Exception e){
115 
116 		}
117 		ab.srcID = to!string(listBox_Available.readLine(listBox_Available.selection).getText(0));
118 		ab.palette = source.getPaletteMode(ab.name);
119 		abList ~= ab;
120 		listBox_Added.addLine(new ListBoxItem([textBox_ID.getText, to!dstring(ab.name), to!dstring(ab.type), to!dstring(ab.srcID)]));
121 	}
122 	private void button_Finish_mouseClick(Event e){
123 		foreach(a; abList){
124 			document.addTileToTileSource(layerNum, a.ID, a.name, a.srcID, source.dir);
125 		}
126 		if(target.classinfo == typeid(TileLayer)){
127 			TileLayer t = cast(TileLayer)target;
128 			foreach(a; abList){
129 				ABitmap b;
130 				switch(a.type){
131 					case "4bit":
132 						b = new Bitmap4Bit(cast(ubyte[])source.getBitmap(a.srcID),x,y,palman.addPalette(source.dir~":"~a.palette,cast(Color[])source.getPalette(a.palette)));
133 						break;
134 					case "8bit":
135 						b = new Bitmap8Bit(source.get8bitBitmap(a.srcID),x,y,palman.addPalette(source.dir~":"~a.palette,cast(Color[])source.getPalette(a.palette)));
136 						break;
137 					case "16bit":
138 						b = new Bitmap16Bit(source.get16bitBitmap(a.srcID),x,y);
139 						break;
140 					case "32bit":
141 						b = new Bitmap32Bit(cast(Color[])(source.getBitmap(a.srcID)),x,y);
142 						break;
143 					default:
144 						break;
145 				}
146 				t.addTile(b,a.ID);
147 			}
148 		}else if(target.classinfo == typeid(TransformableTileLayer!Bitmap8Bit)){
149 
150 		}else if(target.classinfo == typeid(TransformableTileLayer!Bitmap16Bit)){
151 
152 		}else if(target.classinfo == typeid(TransformableTileLayer!Bitmap32Bit)){
153 
154 		}
155 
156 		close;
157 	}
158 	private void button_ABI_mouseClick(Event e){
159 
160 	}
161 	private bool parseIntrinsic(wstring s){
162 		string foretag, aftertag;
163 		int numfieldStart = -1, numfieldEnd = -1;
164 		bool hex, numfieldParsed;
165 		if(s.length > 3){
166 			int i;
167 			if(s[0..3] == "[h]"){
168 				hex = true;
169 				i = 3;
170 			}
171 			for( ; i < s.length ; i++){
172 				if(s[i] == '#'){
173 					if(numfieldStart == -1){
174 						numfieldStart = i;
175 					}
176 					numfieldEnd = i;
177 					if(numfieldParsed){
178 						return false;
179 					}
180 				}else{
181 					if(numfieldParsed){
182 						aftertag ~= s[i];
183 					}else{
184 						if(numfieldEnd != -1){
185 							aftertag ~= s[i];
186 							numfieldParsed = true;
187 						}else{
188 							foretag ~= s[i];
189 						}
190 					}
191 				}
192 			}
193 		}
194 		return true;
195 	}
196 	private void getAvailableBitmaps(){
197 		import std.conv;
198 		ListBoxItem[] items;
199 		for(int i ; i > source.bitmapID.length ; i++){
200 			switch(acceptedTypes){
201 				case AcceptedBMPType.Indexed4Bit:
202 					if(x == source.getXsize(i) && y == source.getYsize(i) && source.bitdepth[i] == "4bit"){
203 						items ~= new ListBoxItem([to!dstring(source.bitmapID[i]), to!dstring(source.bitdepth[i])]);
204 					}
205 					break;
206 				case AcceptedBMPType.Indexed8Bit:
207 					if(x == source.getXsize(i) && y == source.getYsize(i) && source.bitdepth[i] == "8bit"){
208 						items ~= new ListBoxItem([to!dstring(source.bitmapID[i]), to!dstring(source.bitdepth[i])]);
209 					}
210 					break;
211 				case AcceptedBMPType.Indexed16Bit:
212 					if(x == source.getXsize(i) && y == source.getYsize(i) && source.bitdepth[i] == "16bit"){
213 						items ~= new ListBoxItem([to!dstring(source.bitmapID[i]), to!dstring(source.bitdepth[i])]);
214 					}
215 					break;
216 				case AcceptedBMPType.DirectColor32Bit:
217 					if(x == source.getXsize(i) && y == source.getYsize(i) && source.bitdepth[i] == "32bit"){
218 						items ~= new ListBoxItem([to!dstring(source.bitmapID[i]), to!dstring(source.bitdepth[i])]);
219 					}
220 					break;
221 				default:
222 					if(x == source.getXsize(i) && y == source.getYsize(i)){
223 						items ~= new ListBoxItem([to!dstring(source.bitmapID[i]), to!dstring(source.bitdepth[i])]);
224 					}
225 					break;
226 			}
227 		}
228 		listBox_Available.updateColumns(items);
229 	}
230 }