3 using System.Collections;
9 public class Application {
11 private Gtk.Window MainWindow;
13 private Gtk.DrawingArea DrawingArea;
15 private Gtk.CheckButton SolidCheckButton;
17 private Gtk.CheckButton UniSolidCheckButton;
19 private Gtk.CheckButton IceCheckButton;
21 private Gtk.CheckButton WaterCheckButton;
23 private Gtk.CheckButton SlopeCheckButton;
25 private Gtk.CheckButton DontUseCheckButton;
27 private Gtk.Entry DataEntry;
29 private Gtk.Entry AnimFpsEntry;
31 private Gtk.Entry IDEntry;
33 private Gnome.AppBar AppBar;
35 private Gtk.VBox MainLayout;
37 private Gtk.TreeView TileList;
39 private Gtk.Combo TileGroupComboBox;
41 private Gtk.MenuItem AddTileGroupMenu;
43 private string tilesetdir;
44 private string tilesetfile;
45 private TileSet tileset;
46 private TileGroup selectedgroup;
49 private bool[] SelectionArray;
50 private ArrayList Selection = new ArrayList();
53 private bool toggling;
54 private bool selecting;
56 private string currentimage;
57 private Gdk.Pixbuf pixbuf;
59 public static int Main(string[] args) {
60 Program kit = new Program("tiler", "0.0.1", Modules.UI, args);
62 Application app = new Application();
64 /* that's no proper commandlineparsing, but who'll notice... */
66 app.LoadTileSet(args[0]);
72 public Application() {
73 Glade.XML gxml = new Glade.XML(null, "tiler.glade", null, null);
74 gxml.Autoconnect(this);
76 if(MainWindow == null || DrawingArea == null || AppBar == null)
77 throw new Exception("soem widgets not found");
79 DrawingArea.AddEvents((int) Gdk.EventMask.ButtonPressMask);
80 DrawingArea.AddEvents((int) Gdk.EventMask.ButtonReleaseMask);
81 DrawingArea.AddEvents((int) Gdk.EventMask.ButtonMotionMask);
83 // libglade missed interactivity property :-/
84 MainLayout.Remove(AppBar);
85 AppBar = new AppBar(true, true, PreferencesType.Always);
86 AppBar.UserResponse += new EventHandler(OnAppBarUserResponse);
87 MainLayout.PackStart(AppBar, false, false, 0);
90 TileGroupComboBox.Entry.Activated
91 += new EventHandler (OnTileGroupComboBoxEntryActivated);
96 private void OnOpen(object o, EventArgs e) {
97 FileSelection selection = new FileSelection("Select TileSet");
98 selection.OkButton.Clicked += new EventHandler(OnSelectTileSetOk);
99 selection.CancelButton.Clicked += new EventHandler(OnSelectImageCancel);
103 private void OnSelectTileSetOk(object o, EventArgs e) {
104 FileSelection selection = ((FileSelection.FSButton) o).FileSelection;
105 string file = selection.Filename;
111 private void LoadTileSet(string file) {
113 tileset = new TileSet();
116 tilesetdir = new FileInfo(file).Directory.ToString();
117 } catch(Exception exception) {
118 ShowException(exception);
123 FillTileGroupComboBox();
127 private void OnImportImage(object o, EventArgs e) {
128 FileSelection selection = new FileSelection("Select ImageFile");
129 selection.OkButton.Clicked += new EventHandler(OnSelectImageOk);
130 selection.CancelButton.Clicked += new EventHandler(OnSelectImageCancel);
134 private void OnSelectImageCancel(object o, EventArgs args) {
135 FileSelection selection = ((FileSelection.FSButton) o).FileSelection;
139 private void OnSelectImageOk(object o, EventArgs args) {
140 FileSelection selection = ((FileSelection.FSButton) o).FileSelection;
141 string file = selection.Filename;
144 ChangeImage(new FileInfo(file).Name);
146 int startid = tileset.Tiles.Count;
147 for(int y = 0; y < TilesY; ++y) {
148 for(int x = 0; x < TilesX; ++x) {
150 Tile tile = new Tile();
151 tile.ID = startid + i;
152 ImageRegion region = new ImageRegion();
153 region.ImageFile = currentimage;
154 region.Region = new System.Drawing.Rectangle(x*32, y*32, 32, 32);
155 tile.Images.Add(region);
156 if(Tiles[i] != null) {
158 "Warning Tile in this region already existed...");
161 tileset.Tiles.Add(tile);
168 private void ChangeImage(string file) {
175 pixbuf = new Pixbuf(tilesetdir + "/" + file);
176 if(pixbuf.Width % 32 != 0 || pixbuf.Height % 32 != 0)
178 "Image Width or Height is not a multiple of 32");
179 } catch(Exception e) {
183 currentimage = new FileInfo(file).Name;
184 TilesX = pixbuf.Width / 32;
185 TilesY = pixbuf.Height / 32;
186 SelectionArray = new bool[TilesX * TilesY];
187 Tiles = new Tile[TilesX * TilesY];
189 // search tileset for tiles with matching image
190 foreach(Tile tile in tileset.Tiles) {
193 if(tile.Images.Count == 0)
195 ImageRegion region = (ImageRegion) tile.Images[0];
196 if(region.ImageFile == currentimage) {
197 int px = region.Region.X / 32;
198 int py = region.Region.Y / 32;
199 int i = py*TilesX+px;
200 if(i < 0 || i >= Tiles.Length) {
201 Console.WriteLine("Invalid Imageregion at tile " +
205 if(Tiles[i] != null) {
206 Console.WriteLine("Multiple tiles for region " +
207 px*32 + " , " + py*32);
214 /* DrawingArea.Allocation
215 = new Gdk.Rectangle(0, 0, pixbuf.Width, pixbuf.Height);*/
216 DrawingArea.WidthRequest = pixbuf.Width;
217 DrawingArea.HeightRequest = pixbuf.Height;
218 DrawingArea.QueueResize();
221 private void OnSave(object o, EventArgs e) {
222 tileset.Write(tilesetfile);
225 private void OnQuit(object o, EventArgs e) {
226 Gtk.Application.Quit();
229 private void OnAbout(object o, EventArgs e) {
232 private void OnRemapTiles(object o, EventArgs e) {
233 AppBar.SetPrompt("Start-ID:", true);
236 private void OnCreateTileGroup(object o, EventArgs e) {
239 private void OnRenameTileGroup(object o, EventArgs e) {
242 private void OnAppBarUserResponse(object o, EventArgs e) {
244 if(AppBar.Response == null || AppBar.Response == ""
251 id = Int32.Parse(AppBar.Response);
252 } catch(Exception exception) {
253 ShowException(exception);
256 foreach(Tile tile in Selection) {
262 // remap in all tilegroups...
263 foreach(TileGroup tilegroup in tileset.TileGroups) {
264 int idx = tilegroup.Tiles.IndexOf(oldid);
266 tilegroup.Tiles[idx] = tile.ID;
273 AppBar.ClearPrompt();
277 private void OnDrawingAreaExpose(object o, ExposeEventArgs e) {
281 Drawable drawable = e.Event.Window;
282 Gdk.GC gc = new Gdk.GC(drawable);
283 drawable.DrawPixbuf(gc, pixbuf, 0, 0, 0, 0,
284 pixbuf.Width, pixbuf.Height, RgbDither.None, 0, 0);
286 gc.RgbFgColor = new Color(0xff, 0, 0);
287 foreach(Tile tile in Selection) {
288 System.Drawing.Rectangle rect
289 = ((ImageRegion) tile.Images[0]).Region;
290 drawable.DrawRectangle(gc, false, rect.X, rect.Y, rect.Width,
297 private void OnDrawingAreaButtonPress(object o, ButtonPressEventArgs e) {
298 if(SelectionArray == null)
303 for(int i = 0; i < SelectionArray.Length; ++i)
304 SelectionArray[i] = false;
305 select((int) e.Event.X, (int) e.Event.Y);
308 private void select(int x, int y) {
309 int tile = y/32 * TilesX + x/32;
310 if(tile < 0 || tile >= SelectionArray.Length)
313 SelectionArray[tile] = true;
314 SelectionArrayChanged();
317 private void OnDrawingAreaMotionNotify(object i, MotionNotifyEventArgs e) {
320 select((int) e.Event.X, (int) e.Event.Y);
323 private void OnDrawingAreaButtonRelease(object o, ButtonPressEventArgs e) {
327 private void OnCheckButtonToggled(object sender, EventArgs e) {
330 foreach(Tile tile in Selection) {
331 if(sender == SolidCheckButton)
332 tile.Solid = SolidCheckButton.Active;
333 if(sender == UniSolidCheckButton)
334 tile.UniSolid = UniSolidCheckButton.Active;
335 if(sender == IceCheckButton)
336 tile.Ice = IceCheckButton.Active;
337 if(sender == WaterCheckButton)
338 tile.Water = WaterCheckButton.Active;
339 if(sender == SlopeCheckButton)
340 tile.Slope = SlopeCheckButton.Active;
341 if(sender == DontUseCheckButton)
342 tile.ID = DontUseCheckButton.Active ? -1 : 0;
346 private void OnEntryChanged(object sender, EventArgs e) {
349 foreach(Tile tile in Selection) {
351 if(sender == IDEntry)
352 tile.ID = Int32.Parse(IDEntry.Text);
353 if(sender == DataEntry)
354 tile.Data = Int32.Parse(DataEntry.Text);
355 if(sender == AnimFpsEntry)
356 tile.AnimFps = Single.Parse(AnimFpsEntry.Text);
357 } catch(Exception exception) {
358 // ignore parse errors for now...
363 private void SelectionArrayChanged() {
365 for(int i = 0; i < SelectionArray.Length; ++i) {
366 if(!SelectionArray[i])
369 if(Tiles[i] == null) {
370 Console.WriteLine("Tile doesn't exist yet");
371 // TODO ask user to create new tile...
374 Selection.Add(Tiles[i]);
380 private void SelectionChanged() {
383 string nextimage = "";
384 foreach(Tile tile in Selection) {
386 SolidCheckButton.Active = tile.Solid;
387 UniSolidCheckButton.Active = tile.UniSolid;
388 IceCheckButton.Active = tile.Ice;
389 WaterCheckButton.Active = tile.Water;
390 SlopeCheckButton.Active = tile.Slope;
391 DontUseCheckButton.Active = tile.ID == -1;
392 DataEntry.Text = tile.Data.ToString();
393 AnimFpsEntry.Text = tile.AnimFps.ToString();
394 IDEntry.Text = tile.ID.ToString();
395 IDEntry.Editable = true;
398 if(tile.Images.Count > 0) {
399 nextimage = ((ImageRegion) tile.Images[0]).ImageFile;
402 IDEntry.Text += "," + tile.ID.ToString();
403 IDEntry.Editable = false;
404 if(tile.Images.Count > 0
405 && ((ImageRegion) tile.Images[0]).ImageFile != nextimage) {
411 if(nextimage != currentimage)
412 ChangeImage(nextimage);
414 DrawingArea.QueueDraw();
417 private void FillTileList() {
418 TileList.HeadersVisible = true;
419 if(TileList.Columns.Length == 0)
420 TileList.AppendColumn("Tile", new CellRendererText(), "text", 0);
422 ListStore store = new ListStore(typeof(string));
424 if(selectedgroup == null) {
425 foreach(Tile tile in tileset.Tiles) {
428 store.AppendValues(new object[] { tile.ID.ToString() });
431 foreach(int id in selectedgroup.Tiles) {
432 Tile tile = (Tile) tileset.Tiles[id];
434 Console.WriteLine("tilegroup contains deleted tile");
437 store.AppendValues(new object[] { id.ToString() });
441 TileList.Model = store;
442 TileList.Selection.Mode = SelectionMode.Multiple;
445 private void FillTileGroupComboBox() {
446 string[] groups = new string[tileset.TileGroups.Count+1];
449 //Submenu submenu = new Submenu();
450 for(int i = 0; i < tileset.TileGroups.Count; ++i) {
451 String tilegroup = ((TileGroup) tileset.TileGroups[i]).Name;
452 groups[i+1] = tilegroup;
453 //submenu.Add(new MenuItem(tilegroup));
455 TileGroupComboBox.PopdownStrings = groups;
456 TileGroupComboBox.Entry.Editable = false;
458 //AddTileGroupMenu.Submenu = submenu;
461 private void OnTileGroupComboBoxEntryActivated(object o, EventArgs args) {
462 if(TileGroupComboBox.Entry.Text == "All") {
463 selectedgroup = null;
465 foreach(TileGroup tilegroup in tileset.TileGroups) {
466 if(tilegroup.Name == TileGroupComboBox.Entry.Text) {
467 selectedgroup = tilegroup;
475 private void OnTileListCursorChanged(object sender, EventArgs e) {
477 TreePath[] selectpaths =
478 TileList.Selection.GetSelectedRows(out model);
481 foreach(TreePath path in selectpaths) {
483 model.GetIter(out iter, path);
484 int id = Int32.Parse(model.GetValue(iter, 0).ToString());
485 Selection.Add(tileset.Tiles[id]);
490 private void ShowException(Exception e) {
491 MessageDialog dialog = new MessageDialog(MainWindow,
492 DialogFlags.Modal | DialogFlags.DestroyWithParent,
493 MessageType.Error, ButtonsType.Ok,