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 AnimSpeedEntry;
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();
68 public Application() {
69 Glade.XML gxml = new Glade.XML(null, "tiler.glade", null, null);
70 gxml.Autoconnect(this);
72 if(MainWindow == null || DrawingArea == null || AppBar == null)
73 throw new Exception("soem widgets not found");
75 DrawingArea.AddEvents((int) Gdk.EventMask.ButtonPressMask);
76 DrawingArea.AddEvents((int) Gdk.EventMask.ButtonReleaseMask);
77 DrawingArea.AddEvents((int) Gdk.EventMask.ButtonMotionMask);
79 // libglade missed interactivity property :-/
80 MainLayout.Remove(AppBar);
81 AppBar = new AppBar(true, true, PreferencesType.Always);
82 AppBar.UserResponse += new EventHandler(OnAppBarUserResponse);
83 MainLayout.PackStart(AppBar, false, false, 0);
86 TileGroupComboBox.Entry.Activated
87 += new EventHandler (OnTileGroupComboBoxEntryActivated);
92 private void OnOpen(object o, EventArgs e) {
93 FileSelection selection = new FileSelection("Select TileSet");
94 selection.OkButton.Clicked += new EventHandler(OnSelectTileSetOk);
95 selection.CancelButton.Clicked += new EventHandler(OnSelectImageCancel);
99 private void OnSelectTileSetOk(object o, EventArgs e) {
100 FileSelection selection = ((FileSelection.FSButton) o).FileSelection;
101 string file = selection.Filename;
105 tileset = new TileSet();
108 tilesetdir = new FileInfo(file).Directory.ToString();
109 } catch(Exception exception) {
110 ShowException(exception);
115 FillTileGroupComboBox();
119 private void OnImportImage(object o, EventArgs e) {
120 FileSelection selection = new FileSelection("Select ImageFile");
121 selection.OkButton.Clicked += new EventHandler(OnSelectImageOk);
122 selection.CancelButton.Clicked += new EventHandler(OnSelectImageCancel);
126 private void OnSelectImageCancel(object o, EventArgs args) {
127 FileSelection selection = ((FileSelection.FSButton) o).FileSelection;
131 private void OnSelectImageOk(object o, EventArgs args) {
132 FileSelection selection = ((FileSelection.FSButton) o).FileSelection;
133 string file = selection.Filename;
136 ChangeImage(new FileInfo(file).Name);
138 int startid = tileset.Tiles.Count;
139 for(int y = 0; y < TilesY; ++y) {
140 for(int x = 0; x < TilesX; ++x) {
142 Tile tile = new Tile();
143 tile.ID = startid + i;
144 ImageRegion region = new ImageRegion();
145 region.ImageFile = currentimage;
146 region.Region = new Rectangle(x*32, y*32, 32, 32);
147 tile.Images.Add(region);
148 if(Tiles[i] != null) {
150 "Warning Tile in this region already existed...");
153 tileset.Tiles.Add(tile);
160 private void ChangeImage(string file) {
167 pixbuf = new Pixbuf(tilesetdir + "/" + file);
168 if(pixbuf.Width % 32 != 0 || pixbuf.Height % 32 != 0)
170 "Image Width or Height is not a multiple of 32");
171 } catch(Exception e) {
175 currentimage = new FileInfo(file).Name;
176 TilesX = pixbuf.Width / 32;
177 TilesY = pixbuf.Height / 32;
178 SelectionArray = new bool[TilesX * TilesY];
179 Tiles = new Tile[TilesX * TilesY];
181 // search tileset for tiles with matching image
182 foreach(Tile tile in tileset.Tiles) {
185 if(tile.Images.Count == 0)
187 ImageRegion region = (ImageRegion) tile.Images[0];
188 if(region.ImageFile == currentimage) {
189 int px = region.Region.X / 32;
190 int py = region.Region.Y / 32;
191 int i = py*TilesX+px;
192 if(i < 0 || i >= Tiles.Length) {
193 Console.WriteLine("Invalid Imageregion at tile " +
197 if(Tiles[i] != null) {
198 Console.WriteLine("Multiple tiles for region " +
199 px*32 + " , " + py*32);
206 /* DrawingArea.Allocation
207 = new Gdk.Rectangle(0, 0, pixbuf.Width, pixbuf.Height);*/
208 DrawingArea.WidthRequest = pixbuf.Width;
209 DrawingArea.HeightRequest = pixbuf.Height;
210 DrawingArea.QueueResize();
213 private void OnSave(object o, EventArgs e) {
214 tileset.Write(tilesetfile);
217 private void OnQuit(object o, EventArgs e) {
218 Gtk.Application.Quit();
221 private void OnAbout(object o, EventArgs e) {
224 private void OnRemapTiles(object o, EventArgs e) {
225 AppBar.SetPrompt("Start-ID:", true);
228 private void OnCreateTileGroup(object o, EventArgs e) {
231 private void OnRenameTileGroup(object o, EventArgs e) {
234 private void OnAppBarUserResponse(object o, EventArgs e) {
236 if(AppBar.Response == null || AppBar.Response == ""
243 id = Int32.Parse(AppBar.Response);
244 } catch(Exception exception) {
245 ShowException(exception);
248 foreach(Tile tile in Selection) {
255 AppBar.ClearPrompt();
259 private void OnDrawingAreaExpose(object o, ExposeEventArgs e) {
263 Drawable drawable = e.Event.Window;
264 Gdk.GC gc = new Gdk.GC(drawable);
265 drawable.DrawPixbuf(gc, pixbuf, 0, 0, 0, 0,
266 pixbuf.Width, pixbuf.Height, RgbDither.None, 0, 0);
268 gc.RgbFgColor = new Color(0xff, 0, 0);
269 foreach(Tile tile in Selection) {
270 System.Drawing.Rectangle rect
271 = ((ImageRegion) tile.Images[0]).Region;
272 drawable.DrawRectangle(gc, false, rect.X, rect.Y, rect.Width,
279 private void OnDrawingAreaButtonPress(object o, ButtonPressEventArgs e) {
282 for(int i = 0; i < SelectionArray.Length; ++i)
283 SelectionArray[i] = false;
284 select((int) e.Event.X, (int) e.Event.Y);
287 private void select(int x, int y) {
288 int tile = y/32 * TilesX + x/32;
289 if(tile < 0 || tile >= SelectionArray.Length)
292 SelectionArray[tile] = true;
293 SelectionArrayChanged();
296 private void OnDrawingAreaMotionNotify(object i, MotionNotifyEventArgs e) {
299 select((int) e.Event.X, (int) e.Event.Y);
302 private void OnDrawingAreaButtonRelease(object o, ButtonPressEventArgs e) {
306 private void OnCheckButtonToggled(object sender, EventArgs e) {
309 foreach(Tile tile in Selection) {
310 if(sender == SolidCheckButton)
311 tile.Solid = SolidCheckButton.Active;
312 if(sender == UniSolidCheckButton)
313 tile.UniSolid = UniSolidCheckButton.Active;
314 if(sender == IceCheckButton)
315 tile.Ice = IceCheckButton.Active;
316 if(sender == WaterCheckButton)
317 tile.Water = WaterCheckButton.Active;
318 if(sender == SlopeCheckButton)
319 tile.Slope = SlopeCheckButton.Active;
320 if(sender == DontUseCheckButton)
321 tile.ID = DontUseCheckButton.Active ? -1 : 0;
325 private void OnEntryChanged(object sender, EventArgs e) {
328 foreach(Tile tile in Selection) {
330 if(sender == IDEntry)
331 tile.ID = Int32.Parse(IDEntry.Text);
332 if(sender == DataEntry)
333 tile.Data = Int32.Parse(DataEntry.Text);
334 if(sender == AnimSpeedEntry)
335 tile.AnimSpeed = Int32.Parse(AnimSpeedEntry.Text);
336 } catch(Exception exception) {
337 // ignore parse errors for now...
342 private void SelectionArrayChanged() {
344 for(int i = 0; i < SelectionArray.Length; ++i) {
345 if(!SelectionArray[i])
348 if(Tiles[i] == null) {
349 Console.WriteLine("Tile doesn't exist yet");
350 // TODO ask user to create new tile...
353 Selection.Add(Tiles[i]);
359 private void SelectionChanged() {
362 string nextimage = "";
363 foreach(Tile tile in Selection) {
365 SolidCheckButton.Active = tile.Solid;
366 UniSolidCheckButton.Active = tile.UniSolid;
367 IceCheckButton.Active = tile.Ice;
368 WaterCheckButton.Active = tile.Water;
369 SlopeCheckButton.Active = tile.Slope;
370 DontUseCheckButton.Active = tile.ID == -1;
371 DataEntry.Text = tile.Data.ToString();
372 AnimSpeedEntry.Text = tile.AnimSpeed.ToString();
373 IDEntry.Text = tile.ID.ToString();
374 IDEntry.Editable = true;
377 if(tile.Images.Count > 0) {
378 nextimage = ((ImageRegion) tile.Images[0]).ImageFile;
381 IDEntry.Text += "," + tile.ID.ToString();
382 IDEntry.Editable = false;
383 if(tile.Images.Count > 0
384 && ((ImageRegion) tile.Images[0]).ImageFile != nextimage) {
390 if(nextimage != currentimage)
391 ChangeImage(nextimage);
393 DrawingArea.QueueDraw();
396 private void FillTileList() {
397 TileList.HeadersVisible = true;
398 if(TileList.Columns.Length == 0)
399 TileList.AppendColumn("Tile", new CellRendererText(), "text", 0);
401 ListStore store = new ListStore(typeof(string));
403 if(selectedgroup == null) {
404 foreach(Tile tile in tileset.Tiles) {
407 store.AppendValues(new object[] { tile.ID.ToString() });
410 foreach(int id in selectedgroup.Tiles) {
411 Tile tile = (Tile) tileset.Tiles[id];
413 Console.WriteLine("tilegroup contains deleted tile");
416 store.AppendValues(new object[] { id.ToString() });
420 TileList.Model = store;
421 TileList.Selection.Mode = SelectionMode.Multiple;
424 private void FillTileGroupComboBox() {
425 string[] groups = new string[tileset.TileGroups.Count+1];
428 //Submenu submenu = new Submenu();
429 for(int i = 0; i < tileset.TileGroups.Count; ++i) {
430 String tilegroup = ((TileGroup) tileset.TileGroups[i]).Name;
431 groups[i+1] = tilegroup;
432 //submenu.Add(new MenuItem(tilegroup));
434 TileGroupComboBox.PopdownStrings = groups;
435 TileGroupComboBox.Entry.Editable = false;
437 //AddTileGroupMenu.Submenu = submenu;
440 private void OnTileGroupComboBoxEntryActivated(object o, EventArgs args) {
441 if(TileGroupComboBox.Entry.Text == "All") {
442 selectedgroup = null;
444 foreach(TileGroup tilegroup in tileset.TileGroups) {
445 if(tilegroup.Name == TileGroupComboBox.Entry.Text) {
446 selectedgroup = tilegroup;
454 private void OnTileListCursorChanged(object sender, EventArgs e) {
456 TreePath[] selectpaths =
457 TileList.Selection.GetSelectedRows(out model);
460 foreach(TreePath path in selectpaths) {
462 model.GetIter(out iter, path);
463 int id = Int32.Parse(model.GetValue(iter, 0).ToString());
464 Selection.Add(tileset.Tiles[id]);
469 private void ShowException(Exception e) {
470 MessageDialog dialog = new MessageDialog(MainWindow,
471 DialogFlags.Modal | DialogFlags.DestroyWithParent,
472 MessageType.Error, ButtonsType.Ok,