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 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) {
263 AppBar.ClearPrompt();
267 private void OnDrawingAreaExpose(object o, ExposeEventArgs e) {
271 Drawable drawable = e.Event.Window;
272 Gdk.GC gc = new Gdk.GC(drawable);
273 drawable.DrawPixbuf(gc, pixbuf, 0, 0, 0, 0,
274 pixbuf.Width, pixbuf.Height, RgbDither.None, 0, 0);
276 gc.RgbFgColor = new Color(0xff, 0, 0);
277 foreach(Tile tile in Selection) {
278 System.Drawing.Rectangle rect
279 = ((ImageRegion) tile.Images[0]).Region;
280 drawable.DrawRectangle(gc, false, rect.X, rect.Y, rect.Width,
287 private void OnDrawingAreaButtonPress(object o, ButtonPressEventArgs e) {
288 if(SelectionArray == null)
293 for(int i = 0; i < SelectionArray.Length; ++i)
294 SelectionArray[i] = false;
295 select((int) e.Event.X, (int) e.Event.Y);
298 private void select(int x, int y) {
299 int tile = y/32 * TilesX + x/32;
300 if(tile < 0 || tile >= SelectionArray.Length)
303 SelectionArray[tile] = true;
304 SelectionArrayChanged();
307 private void OnDrawingAreaMotionNotify(object i, MotionNotifyEventArgs e) {
310 select((int) e.Event.X, (int) e.Event.Y);
313 private void OnDrawingAreaButtonRelease(object o, ButtonPressEventArgs e) {
317 private void OnCheckButtonToggled(object sender, EventArgs e) {
320 foreach(Tile tile in Selection) {
321 if(sender == SolidCheckButton)
322 tile.Solid = SolidCheckButton.Active;
323 if(sender == UniSolidCheckButton)
324 tile.UniSolid = UniSolidCheckButton.Active;
325 if(sender == IceCheckButton)
326 tile.Ice = IceCheckButton.Active;
327 if(sender == WaterCheckButton)
328 tile.Water = WaterCheckButton.Active;
329 if(sender == SlopeCheckButton)
330 tile.Slope = SlopeCheckButton.Active;
331 if(sender == DontUseCheckButton)
332 tile.ID = DontUseCheckButton.Active ? -1 : 0;
336 private void OnEntryChanged(object sender, EventArgs e) {
339 foreach(Tile tile in Selection) {
341 if(sender == IDEntry)
342 tile.ID = Int32.Parse(IDEntry.Text);
343 if(sender == DataEntry)
344 tile.Data = Int32.Parse(DataEntry.Text);
345 if(sender == AnimFpsEntry)
346 tile.AnimFps = Single.Parse(AnimFpsEntry.Text);
347 } catch(Exception exception) {
348 // ignore parse errors for now...
353 private void SelectionArrayChanged() {
355 for(int i = 0; i < SelectionArray.Length; ++i) {
356 if(!SelectionArray[i])
359 if(Tiles[i] == null) {
360 Console.WriteLine("Tile doesn't exist yet");
361 // TODO ask user to create new tile...
364 Selection.Add(Tiles[i]);
370 private void SelectionChanged() {
373 string nextimage = "";
374 foreach(Tile tile in Selection) {
376 SolidCheckButton.Active = tile.Solid;
377 UniSolidCheckButton.Active = tile.UniSolid;
378 IceCheckButton.Active = tile.Ice;
379 WaterCheckButton.Active = tile.Water;
380 SlopeCheckButton.Active = tile.Slope;
381 DontUseCheckButton.Active = tile.ID == -1;
382 DataEntry.Text = tile.Data.ToString();
383 AnimFpsEntry.Text = tile.AnimFps.ToString();
384 IDEntry.Text = tile.ID.ToString();
385 IDEntry.Editable = true;
388 if(tile.Images.Count > 0) {
389 nextimage = ((ImageRegion) tile.Images[0]).ImageFile;
392 IDEntry.Text += "," + tile.ID.ToString();
393 IDEntry.Editable = false;
394 if(tile.Images.Count > 0
395 && ((ImageRegion) tile.Images[0]).ImageFile != nextimage) {
401 if(nextimage != currentimage)
402 ChangeImage(nextimage);
404 DrawingArea.QueueDraw();
407 private void FillTileList() {
408 TileList.HeadersVisible = true;
409 if(TileList.Columns.Length == 0)
410 TileList.AppendColumn("Tile", new CellRendererText(), "text", 0);
412 ListStore store = new ListStore(typeof(string));
414 if(selectedgroup == null) {
415 foreach(Tile tile in tileset.Tiles) {
418 store.AppendValues(new object[] { tile.ID.ToString() });
421 foreach(int id in selectedgroup.Tiles) {
422 Tile tile = (Tile) tileset.Tiles[id];
424 Console.WriteLine("tilegroup contains deleted tile");
427 store.AppendValues(new object[] { id.ToString() });
431 TileList.Model = store;
432 TileList.Selection.Mode = SelectionMode.Multiple;
435 private void FillTileGroupComboBox() {
436 string[] groups = new string[tileset.TileGroups.Count+1];
439 //Submenu submenu = new Submenu();
440 for(int i = 0; i < tileset.TileGroups.Count; ++i) {
441 String tilegroup = ((TileGroup) tileset.TileGroups[i]).Name;
442 groups[i+1] = tilegroup;
443 //submenu.Add(new MenuItem(tilegroup));
445 TileGroupComboBox.PopdownStrings = groups;
446 TileGroupComboBox.Entry.Editable = false;
448 //AddTileGroupMenu.Submenu = submenu;
451 private void OnTileGroupComboBoxEntryActivated(object o, EventArgs args) {
452 if(TileGroupComboBox.Entry.Text == "All") {
453 selectedgroup = null;
455 foreach(TileGroup tilegroup in tileset.TileGroups) {
456 if(tilegroup.Name == TileGroupComboBox.Entry.Text) {
457 selectedgroup = tilegroup;
465 private void OnTileListCursorChanged(object sender, EventArgs e) {
467 TreePath[] selectpaths =
468 TileList.Selection.GetSelectedRows(out model);
471 foreach(TreePath path in selectpaths) {
473 model.GetIter(out iter, path);
474 int id = Int32.Parse(model.GetValue(iter, 0).ToString());
475 Selection.Add(tileset.Tiles[id]);
480 private void ShowException(Exception e) {
481 MessageDialog dialog = new MessageDialog(MainWindow,
482 DialogFlags.Modal | DialogFlags.DestroyWithParent,
483 MessageType.Error, ButtonsType.Ok,