1 module windows.materiallist;
2 
3 import pixelperfectengine.concrete.window;
4 import pixelperfectengine.map.mapformat;
5 
6 import app;
7 
8 import std.utf : toUTF32, toUTF8;
9 import pixelperfectengine.system.etc : intToHex;
10 
11 /**
12  * Preliminary, future version will feature material selection with images.
13  */
14 public class MaterialList : Window {
15 	//ListBox			listBox_materials;
16 	ListView		listView_materials;
17 	Label			palettePos;
18 	SmallButton		removeMaterial;
19 	SmallButton		addMaterial;
20 	CheckBox		horizMirror;
21 	CheckBox		vertMirror;
22 	CheckBox		ovrwrtIns;
23 	SmallButton		paletteUp;
24 	SmallButton		paletteDown;
25 	SmallButton		settings;
26 
27 	protected TileInfo[] tiles;
28 	protected ListViewHeader tileListHeader;
29 	protected ListViewHeader spriteListHeader;
30 	public this(int x, int y, void delegate() onClose) @trusted {
31 		super(Coordinate(x, y, x + 129, y + 249), "Materials"d);
32 		this.onClose = onClose;
33 		StyleSheet ss = getStyleSheet();
34 		/+listBox_materials = new ListBox("listBox0", Coordinate(1, 17, 129, 218), [], new ListBoxHeader(tileListHeaderS.dup,
35 				tileListHeaderW.dup));+/
36 		tileListHeader = new ListViewHeader(16, [32, 120], ["ID"d, "Name"d]);
37 		spriteListHeader = new ListViewHeader(16, [40, 120, 64], ["ID"d, "Name"d, "Dim"d]);
38 		listView_materials = new ListView(tileListHeader, null, "listView_materials", Box(1, 17, 128, 215));
39 		listView_materials.onItemSelect = &onItemSelect;
40 		listView_materials.editEnable = true;
41 		listView_materials.onTextInput = &onItemRename;
42 		addElement(listView_materials);
43 		
44 		removeMaterial = new SmallButton("removeMaterialB", "removeMaterialA", "rem", Box(113, 233, 129, 248));
45 		removeMaterial.onMouseLClick = &button_trash_onClick;
46 		addElement(removeMaterial);
47 		
48 		addMaterial = new SmallButton("addMaterialB", "addMaterialA", "add", Box(113, 217, 129, 232));
49 		addMaterial.onMouseLClick = &button_addMaterial_onClick;
50 		addElement(addMaterial);
51 		
52 		horizMirror = new CheckBox("horizMirrorB", "horizMirrorA", "horizMirror", Box(1, 217, 16, 232));
53 		horizMirror.onToggle = &horizMirror_onClick;
54 		addElement(horizMirror);
55 		
56 		vertMirror = new CheckBox("vertMirrorB", "vertMirrorA", "vertMirror", Box(17, 217, 32, 232));
57 		vertMirror.onToggle = &vertMirror_onClick;
58 		addElement(vertMirror);
59 		
60 		ovrwrtIns = new CheckBox("ovrwrtInsB", "ovrwrtInsA", "ovrwrtIns", Box(33, 217, 48, 232));
61 		ovrwrtIns.onToggle = &ovrwrtIns_onClick;
62 		addElement(ovrwrtIns);
63 		
64 		paletteUp = new SmallButton("paletteUpB", "paletteUpA", "palUp", Box(1, 233, 16, 248));
65 		paletteUp.onMouseLClick = &palUp_onClick;
66 		addElement(paletteUp);
67 		
68 		paletteDown = new SmallButton("paletteDownB", "paletteDownA", "palDown", Box(17, 233, 32, 248));
69 		paletteDown.onMouseLClick = &palDown_onClick;
70 		addElement(paletteDown);
71 		
72 		settings = new SmallButton("settingsButtonB", "settingsButtonA", "editMat", Box(97, 233, 112, 248));
73 		settings.onMouseLClick = &button_editMat_onClick;
74 		addElement(settings);
75 		
76 		palettePos = new Label("0x00", "palettePos", Box(34, 234, 96, 248));
77 		addElement(palettePos);
78 	}
79 	public void updateMaterialList(TileInfo[] list) @trusted {
80 		import pixelperfectengine.system.etc : intToHex;
81 		tiles = list;
82 		/+ListViewItem[] output;
83 		output.reserve = list.length;+/
84 		listView_materials.clear;
85 		foreach (item ; list) {
86 			ListViewItem f = new ListViewItem(16, [intToHex!dstring(item.id, 4) ~ "h", toUTF32(item.name)]);
87 			f[1].editable = true;
88 			listView_materials ~= f;
89 		}
90 		listView_materials.refresh;
91 		/+listBox_materials.updateColumns(output, new ListBoxHeader(tileListHeaderS.dup, tileListHeaderW.dup));+/
92 	}
93 	private void vertMirror_onClick(Event ev) {
94 		CheckBox sender = cast(CheckBox)ev.sender;
95 		if (prg.selDoc) {
96 			if(sender.isChecked) {
97 				prg.selDoc.tileMaterial_FlipVertical(true);
98 			} else {
99 				prg.selDoc.tileMaterial_FlipVertical(false);
100 			}
101 		}
102 	}
103 	private void horizMirror_onClick(Event ev) {
104 		CheckBox sender = cast(CheckBox)ev.sender;
105 		if (prg.selDoc) {
106 			if(sender.isChecked) {
107 				prg.selDoc.tileMaterial_FlipHorizontal(true);
108 			} else {
109 				prg.selDoc.tileMaterial_FlipHorizontal(false);
110 			}
111 		}
112 	}
113 	private void button_addMaterial_onClick(Event ev) {
114 		prg.initAddMaterials;
115 	}
116 	private void button_editMat_onClick(Event ev) {
117 
118 	}
119 	private void button_trash_onClick(Event ev) {
120 		if (listView_materials.value != -1) {
121 			prg.selDoc.removeTile(tiles[listView_materials.value].id);
122 		}
123 	}
124 	public void palUp_onClick(Event ev) {
125 		palettePos.setText("0x" ~ intToHex!dstring(prg.selDoc.tileMaterial_PaletteUp, 2));
126 	}
127 	public void palDown_onClick(Event ev) {
128 		palettePos.setText("0x" ~ intToHex!dstring(prg.selDoc.tileMaterial_PaletteDown, 2));
129 	}
130 
131 	private void ovrwrtIns_onClick(Event ev) {
132 		CheckBox sender = cast(CheckBox)ev.sender;
133 		if (prg.selDoc)
134 			prg.selDoc.voidfill = sender.isChecked;
135 	}
136 	private void onItemSelect(Event ev) {
137 		prg.selDoc.tileMaterial_Select(tiles[listView_materials.value].id);
138 	}
139 	private void onItemRename(Event ev) {
140 		CellEditEvent cee = cast(CellEditEvent)ev;
141 		string newName = toUTF8(cee.text().text());
142 		prg.selDoc.renameTile(tiles[listView_materials.value].id, newName);
143 	}
144 	public void nextTile() {
145 		listView_materials.value = listView_materials.value + 1;
146 		if (listView_materials.value != -1)
147 			prg.selDoc.tileMaterial_Select(tiles[listView_materials.value].id);
148 	}
149 	public void prevTile() {
150 		listView_materials.value = listView_materials.value - 1;
151 		if (listView_materials.value != -1)
152 			prg.selDoc.tileMaterial_Select(tiles[listView_materials.value].id);
153 	}
154 }
155 /**
156  * Defines a single material.
157  */
158 public struct Material {
159 	dstring	id;		///Hexanumeric value of the ID
160 	dstring	name;	///Name of the object or tile
161 	dstring dim;	///Dimensions of the object, null on tiles since they share the same size on one layer
162 }