1 /*
2  * PixelPerfectEditor, multiImportDialog module
3  *
4  * Copyright 2017, under Boost License
5  *
6  * by Laszlo Szeremi
7  */
8 
9 module multiImportDialog;
10 
11 import PixelPerfectEngine.concrete.window;
12 import PixelPerfectEngine.graphics.common;
13 
14 import std.conv;
15 import std..string;
16 
17 import converterdialog;
18 
19 class MultiImportDialog : Window {
20 	Label label1;
21 	Label label2;
22 	//Label label3;
23 	Label label4;
24 	Label label5;
25 	Label label6;
26 	TextBox bitmapID;
27 	TextBox numFrom;
28 	//TextBox secBitmapID;
29 	TextBox sWidth;
30 	TextBox sHeight;
31 	TextBox paletteID;
32 	CheckBox checkBox_palImp;
33 	CheckBox checkBox_hex;
34 	RadioButtonGroup rb_bitDepth;
35 	Button button_ok;
36 	Button button_cancel;
37 	int fullX, fullY;
38 	ConverterDialog c;
39 	this(int x, int y, ConverterDialog c){
40 		super(Coordinate(0, 0, 225, 320), "Import sheet");
41 		label1 = new Label("IDbase:", "label1", Coordinate(5, 22, 75, 41));
42 		addElement(label1, EventProperties.MOUSE);
43 		label2 = new Label("NumFrom:", "label2", Coordinate(5, 47, 75, 65));
44 		addElement(label2, EventProperties.MOUSE);
45 		//label3 = new Label("IDsec:"w, "label3", Coordinate(5, 72, 75, 88));
46 		//addElement(label1, EventProperties.MOUSE);
47 		label4 = new Label("Width:", "label4", Coordinate(5, 72, 75, 88));
48 		addElement(label4, EventProperties.MOUSE);
49 		label5 = new Label("Height:", "label5", Coordinate(5, 97, 75, 115));
50 		addElement(label5, EventProperties.MOUSE);
51 		label6 = new Label("Palette:", "label6", Coordinate(5, 122, 75, 138));
52 		addElement(label6, EventProperties.MOUSE);
53 		bitmapID = new TextBox("bitmapID##xyz", "bitmapID", Coordinate(75, 20, 220, 39));
54 		addElement(bitmapID, EventProperties.MOUSE);
55 		numFrom = new TextBox("0", "numFrom", Coordinate(75, 45, 220, 65));
56 		addElement(numFrom, EventProperties.MOUSE);
57 		numFrom.onTextInput = &numFrom_onTextInput;
58 		//numTo = new TextBox("1"w, "numTo", Coordinate(75, 70, 220, 90));
59 		sWidth = new TextBox("32", "sWidth", Coordinate(75, 70, 220, 90));
60 		addElement(sWidth, EventProperties.MOUSE);
61 		sWidth.onTextInput = &sWidth_onTextInput;
62 		sHeight = new TextBox("32", "sHeight", Coordinate(75, 95, 220, 115));
63 		addElement(sHeight, EventProperties.MOUSE);
64 		sHeight.onTextInput = &sHeight_onTextInput;
65 		paletteID = new TextBox("default", "paletteID", Coordinate(75, 120, 220, 140));
66 		addElement(paletteID, EventProperties.MOUSE);
67 		checkBox_palImp = new CheckBox("Import palette from file", "checkBox_palImp", Coordinate(5, 150, 220, 166));
68 		addElement(checkBox_palImp, EventProperties.MOUSE);
69 		checkBox_hex = new CheckBox("Use hex numbering", "checkBox_hex", Coordinate(5, 170, 220, 186));
70 		addElement(checkBox_hex, EventProperties.MOUSE);
71 		rb_bitDepth = new RadioButtonGroup("BitDepth:", "rb_bitDepth", Coordinate(5, 190, 220, 290),[ "1bit", "4bit", "8bit", "16bit", "32bit"], 16, 0);
72 		addElement(rb_bitDepth, EventProperties.MOUSE);
73 		button_ok = new Button("Ok", "button_ok", Coordinate(160, 295, 220, 315));
74 		addElement(button_ok, EventProperties.MOUSE);
75 		button_ok.onMouseLClickRel = &buttonOk_mouseLClickRel;
76 		button_cancel = new Button("Cancel", "button_cancel", Coordinate(95, 295, 155, 315));
77 		addElement(button_cancel, EventProperties.MOUSE);
78 		button_cancel.onMouseLClickRel = &buttonClose_mouseLClickRel;
79 		this.c = c;
80 		fullX = x;
81 		fullY = y;
82 	}
83 	private bool checkIDbase(dstring s, out dstring foreTag, out dstring afterTag, out int digits){
84 		byte state;
85 		foreach(c; s){
86 			if(state == 0 && c == '#'){
87 				state = 1;
88 				digits++;
89 			}else if(state == 0){
90 				foreTag ~= c;
91 			}else if(state == 1 && c == '#'){
92 				digits++;
93 			}else if(state == 2 && c == '#'){
94 				return false;
95 			}else if(state == 2){
96 				afterTag ~= c;
97 			}else{
98 				state = 2;
99 				afterTag ~= c;
100 			}
101 		}
102 		if(!digits){
103 			return false;
104 		}
105 		return true;
106 	}
107 	private void numFrom_onTextInput(Event ev){
108 		if(!isNumeric(ev.text, true)){
109 			parent.messageWindow("Input Type Error", "Input field \"numFrom\"'s text is impossible to parse as a number!");
110 		}
111 	}
112 	private void sWidth_onTextInput(Event ev){
113 
114 	}
115 	private void sHeight_onTextInput(Event ev){
116 
117 	}
118 	private void buttonOk_mouseLClickRel(Event ev){
119 		try{
120 			dstring foreTag, afterTag;
121 			int digits;
122 			if(checkIDbase(bitmapID.getText(),foreTag,afterTag,digits)){
123 				string bitDepth;
124 				switch(rb_bitDepth.value){
125 					case 1:
126 						bitDepth = "4bit";
127 						break;
128 					case 2:
129 						bitDepth = "8bit";
130 						break;
131 					case 3:
132 						bitDepth = "16bit";
133 						break;
134 					case 4:
135 						bitDepth = "32bit";
136 						break;
137 					default:
138 						bitDepth = "1bit";
139 						break;
140 				}
141 				//string palID = checkBox_palImp.value ? to!string(paletteID.getText()) : null;
142 				c.multiImport(to!string(foreTag), to!string(afterTag), digits, to!string(paletteID.getText()), checkBox_palImp.value, bitDepth, to!int(numFrom.getText()), to!int(sWidth.getText()), to!int(sHeight.getText()), checkBox_hex.value);
143 				close();
144 			}else{
145 				parent.messageWindow("Input Error", "IDBase must have the following format:\n <foretag><####><aftertag>");
146 			}
147 		}catch(ConvException e){
148 			parent.messageWindow("Input Error", "Please check the fields if they're containing the correct type of data.");
149 		}catch(Exception e){
150 			parent.messageWindow(to!dstring(e.classinfo.toString()), to!dstring(e.msg));
151 		}
152 	}
153 	private void buttonClose_mouseLClickRel(Event ev){
154 		close();
155 	}
156 	deprecated public void actionEvent(Event event){
157 		switch(event.source){
158 			case "numFrom":
159 				if(!isNumeric(event.text, true)){
160 					parent.messageWindow("Input Type Error", "Input field \"numFrom\"'s text is impossible to parse as a number!");
161 				}
162 				break;
163 			case "sWidth":
164 				if(!isNumeric(event.text, true)){
165 					parent.messageWindow("Input Type Error", "Input field \"sWidth\"'s text is impossible to parse as a number!");
166 				}else if(fullX % to!int(sWidth.getText())){
167 					parent.messageWindow("Size Mismatch Error", "Current width is not suitable for input image's width. Try to choose
168 							a size that's dividable with the input image's width.");
169 				}
170 				break;
171 			case "sHeight":
172 				if(!isNumeric(event.text, true)){
173 					parent.messageWindow("Input Type Error", "Input field \"sHeight\"'s text is impossible to parse as a number!");
174 				}else if(fullY % to!int(sHeight.getText())){
175 					parent.messageWindow("Size Mismatch Error", "Current height is not suitable for input image's height. Try to choose
176 							a size that's dividable with the input image's height.");
177 				}
178 				break;
179 			case "button_ok":
180 				try{
181 					dstring foreTag, afterTag;
182 					int digits;
183 					if(checkIDbase(bitmapID.getText(),foreTag,afterTag,digits)){
184 						string bitDepth;
185 						switch(rb_bitDepth.value){
186 							case 1:
187 								bitDepth = "4bit";
188 								break;
189 							case 2:
190 								bitDepth = "8bit";
191 								break;
192 							case 3:
193 								bitDepth = "16bit";
194 								break;
195 							case 4:
196 								bitDepth = "32bit";
197 								break;
198 							default:
199 								bitDepth = "1bit";
200 								break;
201 						}
202 						//string palID = checkBox_palImp.value ? to!string(paletteID.getText()) : null;
203 						c.multiImport(to!string(foreTag), to!string(afterTag), digits, to!string(paletteID.getText()), checkBox_palImp.value, bitDepth, to!int(numFrom.getText()), to!int(sWidth.getText()), to!int(sHeight.getText()), checkBox_hex.value);
204 						close();
205 					}else{
206 						parent.messageWindow("Input Error", "IDBase must have the following format:\n <foretag><####><aftertag>");
207 					}
208 				}catch(ConvException e){
209 					parent.messageWindow("Input Error", "Please check the fields if they're containing the correct type of data.");
210 				}catch(Exception e){
211 					parent.messageWindow(to!dstring(e.classinfo.toString()), to!dstring(e.msg));
212 				}
213 				break;
214 			case "button_cancel":
215 				close();
216 				break;
217 			default:
218 				break;
219 		}
220 	}
221 
222 }