fix some more timings and the long standing gradient software bug (which was function...
[supertux.git] / contrib / tilemanager / Application.cs
1 using System;
2 using System.IO;
3 using System.Collections;
4 using Gtk;
5 using Gdk;
6 using Gnome;
7 using Glade;
8
9 public class Application {
10     [Glade.Widget]
11     private Gtk.Window MainWindow;
12     [Glade.Widget]
13     private Gtk.DrawingArea DrawingArea;
14     [Glade.Widget]
15     private Gtk.CheckButton SolidCheckButton;
16     [Glade.Widget]
17     private Gtk.CheckButton UniSolidCheckButton;
18     [Glade.Widget]
19     private Gtk.CheckButton IceCheckButton;
20     [Glade.Widget]
21     private Gtk.CheckButton WaterCheckButton;
22     [Glade.Widget]
23     private Gtk.CheckButton SlopeCheckButton;
24     [Glade.Widget]
25     private Gtk.CheckButton DontUseCheckButton;
26     [Glade.Widget]
27     private Gtk.Entry DataEntry;
28     [Glade.Widget]
29     private Gtk.Entry AnimFpsEntry;
30     [Glade.Widget]                 
31     private Gtk.Entry IDEntry;
32     [Glade.Widget]
33     private Gnome.AppBar AppBar;
34     [Glade.Widget]
35     private Gtk.VBox MainLayout;
36     [Glade.Widget]
37     private Gtk.TreeView TileList;
38     [Glade.Widget]
39     private Gtk.Combo TileGroupComboBox;
40     [Glade.Widget]
41     private Gtk.MenuItem AddTileGroupMenu;
42
43     private string tilesetdir;
44     private string tilesetfile;
45     private TileSet tileset;
46     private TileGroup selectedgroup;
47
48     private Tile[] Tiles;
49     private bool[] SelectionArray;
50     private ArrayList Selection = new ArrayList();
51     private int TilesX;
52     private int TilesY;
53     private bool toggling;
54     private bool selecting;
55
56     private string currentimage;
57     private Gdk.Pixbuf pixbuf;
58     
59     public static int Main(string[] args) {
60         Program kit = new Program("tiler", "0.0.1", Modules.UI, args);
61
62         Application app = new Application();
63
64         /* that's no proper commandlineparsing, but who'll notice... */
65         if(args.Length == 1)
66             app.LoadTileSet(args[0]);
67
68         kit.Run();
69         return 0;
70     }
71
72     public Application() {
73         Glade.XML gxml = new Glade.XML(null, "tiler.glade", null, null);
74         gxml.Autoconnect(this);
75
76         if(MainWindow == null || DrawingArea == null || AppBar == null)
77             throw new Exception("soem widgets not found");
78
79         DrawingArea.AddEvents((int) Gdk.EventMask.ButtonPressMask);
80         DrawingArea.AddEvents((int) Gdk.EventMask.ButtonReleaseMask);
81         DrawingArea.AddEvents((int) Gdk.EventMask.ButtonMotionMask);
82
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);
88         AppBar.Show();
89
90         TileGroupComboBox.Entry.Activated 
91             += new EventHandler (OnTileGroupComboBoxEntryActivated);
92         
93         MainWindow.Show();
94     }
95
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);
100         selection.Show();
101     }
102
103     private void OnSelectTileSetOk(object o, EventArgs e) {
104         FileSelection selection = ((FileSelection.FSButton) o).FileSelection;
105         string file = selection.Filename;
106         selection.Destroy();
107
108         LoadTileSet(file);
109     }
110
111     private void LoadTileSet(string file) {
112         try {
113             tileset = new TileSet();
114             tileset.Parse(file);
115             tilesetfile = file;
116             tilesetdir = new FileInfo(file).Directory.ToString();
117         } catch(Exception exception) {
118             ShowException(exception);
119         }
120
121         Selection.Clear();
122         SelectionChanged();
123         FillTileGroupComboBox();
124         FillTileList();
125     }
126
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);
131         selection.Show();                           
132     }
133
134     private void OnSelectImageCancel(object o, EventArgs args) {
135         FileSelection selection = ((FileSelection.FSButton) o).FileSelection;
136         selection.Destroy();
137     }
138     
139     private void OnSelectImageOk(object o, EventArgs args) {
140         FileSelection selection = ((FileSelection.FSButton) o).FileSelection;
141         string file = selection.Filename;
142         selection.Destroy();
143
144         ChangeImage(new FileInfo(file).Name);
145         
146         int startid = tileset.Tiles.Count;
147         for(int y = 0; y < TilesY; ++y) {
148             for(int x = 0; x < TilesX; ++x) {
149                 int i = y*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) {
157                     Console.WriteLine(
158                             "Warning Tile in this region already existed...");
159                 }
160                 Tiles[i] = tile;
161                 tileset.Tiles.Add(tile);
162             }
163         }
164
165         FillTileList();
166     }
167
168     private void ChangeImage(string file) {
169         if(file == "") {
170             currentimage = "";
171             pixbuf = null;
172             return;
173         }
174         try {
175             pixbuf = new Pixbuf(tilesetdir + "/" + file);
176             if(pixbuf.Width % 32 != 0 || pixbuf.Height % 32 != 0)
177                 throw new Exception(
178                         "Image Width or Height is not a multiple of 32");
179         } catch(Exception e) {
180             ShowException(e);
181             return;
182         }
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];
188         
189         // search tileset for tiles with matching image
190         foreach(Tile tile in tileset.Tiles) {
191             if(tile == null)
192                 continue;
193             if(tile.Images.Count == 0)
194                 continue;
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 " +
202                             tile.ID);
203                     continue;
204                 }
205                 if(Tiles[i] != null) {
206                     Console.WriteLine("Multiple tiles for region " +
207                             px*32 + " , " + py*32);
208                     continue;
209                 }
210                 Tiles[i] = tile;
211             }
212         } 
213
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();
219     }
220
221     private void OnSave(object o, EventArgs e) {
222         tileset.Write(tilesetfile);
223     }
224
225     private void OnQuit(object o, EventArgs e) {
226         Gtk.Application.Quit();
227     }
228
229     private void OnAbout(object o, EventArgs e) {
230     }
231
232     private void OnRemapTiles(object o, EventArgs e) {
233         AppBar.SetPrompt("Start-ID:", true);
234     }
235
236     private void OnCreateTileGroup(object o, EventArgs e) {
237     }
238
239     private void OnRenameTileGroup(object o, EventArgs e) {
240     }
241
242     private void OnAppBarUserResponse(object o, EventArgs e) {
243         try {
244             if(AppBar.Response == null || AppBar.Response == "" 
245                     || Tiles == null)
246                 return;
247         
248             // remap tiles
249             int id;
250             try {
251                 id = Int32.Parse(AppBar.Response);
252             } catch(Exception exception) {
253                 ShowException(exception);
254                 return;
255             }
256             foreach(Tile tile in Selection) {
257                 if(tile.ID != -1)
258                     tile.ID = id++;
259             }
260             FillTileList();
261             SelectionChanged();
262         } finally {
263             AppBar.ClearPrompt();
264         }
265     }
266
267     private void OnDrawingAreaExpose(object o, ExposeEventArgs e) {
268         if(pixbuf == null)
269             return;
270
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);
275
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,
281                     rect.Height);
282         }
283
284         e.RetVal = false;
285     }
286
287     private void OnDrawingAreaButtonPress(object o, ButtonPressEventArgs e) {
288         if(SelectionArray == null)
289             return;
290
291         selecting = true;
292         
293         for(int i = 0; i < SelectionArray.Length; ++i)
294             SelectionArray[i] = false;
295         select((int) e.Event.X, (int) e.Event.Y);
296     }
297
298     private void select(int x, int y) {
299         int tile = y/32 * TilesX + x/32;
300         if(tile < 0 || tile >= SelectionArray.Length)
301             return;
302
303         SelectionArray[tile] = true;
304         SelectionArrayChanged();
305     }
306
307     private void OnDrawingAreaMotionNotify(object i, MotionNotifyEventArgs e) {
308         if(!selecting)
309             return;
310         select((int) e.Event.X, (int) e.Event.Y);
311     }
312
313     private void OnDrawingAreaButtonRelease(object o, ButtonPressEventArgs e) {
314         selecting = false;
315     }
316
317     private void OnCheckButtonToggled(object sender, EventArgs e) {
318         if(toggling)
319             return;
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;
333         }
334     }
335
336     private void OnEntryChanged(object sender, EventArgs e) {
337         if(toggling)
338             return;
339         foreach(Tile tile in Selection) {
340             try {
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...
349             }
350         }
351     }
352
353     private void SelectionArrayChanged() {
354         Selection.Clear();
355         for(int i = 0; i < SelectionArray.Length; ++i) {
356             if(!SelectionArray[i])
357                 continue;
358             
359             if(Tiles[i] == null) {
360                 Console.WriteLine("Tile doesn't exist yet");
361                 // TODO ask user to create new tile...
362                 continue;
363             }
364             Selection.Add(Tiles[i]);
365         }
366
367         SelectionChanged();
368     }
369
370     private void SelectionChanged() {
371         bool first = true;
372         toggling = true;
373         string nextimage = "";
374         foreach(Tile tile in Selection) {
375             if(first) {
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;
386                 first = false;
387
388                 if(tile.Images.Count > 0) {
389                     nextimage = ((ImageRegion) tile.Images[0]).ImageFile;
390                 }
391             } else {
392                 IDEntry.Text += "," + tile.ID.ToString();
393                 IDEntry.Editable = false;
394                 if(tile.Images.Count > 0 
395                         && ((ImageRegion) tile.Images[0]).ImageFile != nextimage) {
396                     nextimage = "";
397                     pixbuf = null;       
398                 }
399             }
400         }
401         if(nextimage != currentimage)
402             ChangeImage(nextimage);
403         toggling = false;
404         DrawingArea.QueueDraw();
405     }
406
407     private void FillTileList() {
408         TileList.HeadersVisible = true;
409         if(TileList.Columns.Length == 0)
410             TileList.AppendColumn("Tile", new CellRendererText(), "text", 0);
411
412         ListStore store = new ListStore(typeof(string));
413
414         if(selectedgroup == null) {
415             foreach(Tile tile in tileset.Tiles) {
416                 if(tile == null)
417                     continue;
418                 store.AppendValues(new object[] { tile.ID.ToString() });
419             }
420         } else {
421             foreach(int id in selectedgroup.Tiles) {
422                 Tile tile = (Tile) tileset.Tiles[id];
423                 if(tile == null) {
424                     Console.WriteLine("tilegroup contains deleted tile");
425                     continue;
426                 }
427                 store.AppendValues(new object[] { id.ToString() });
428             }
429         }
430         
431         TileList.Model = store;
432         TileList.Selection.Mode = SelectionMode.Multiple;
433     }
434
435     private void FillTileGroupComboBox() {
436         string[] groups = new string[tileset.TileGroups.Count+1];
437         groups[0] = "All";
438
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));
444         }
445         TileGroupComboBox.PopdownStrings = groups;
446         TileGroupComboBox.Entry.Editable = false;
447
448         //AddTileGroupMenu.Submenu = submenu;
449     }
450
451     private void OnTileGroupComboBoxEntryActivated(object o, EventArgs args) {
452         if(TileGroupComboBox.Entry.Text == "All") {
453             selectedgroup = null;
454         } else {
455             foreach(TileGroup tilegroup in tileset.TileGroups) {
456                 if(tilegroup.Name == TileGroupComboBox.Entry.Text) {
457                     selectedgroup = tilegroup;
458                     break;
459                 }
460             }
461         }
462         FillTileList();
463     }
464
465     private void OnTileListCursorChanged(object sender, EventArgs e) {
466         TreeModel model;
467         TreePath[] selectpaths =
468             TileList.Selection.GetSelectedRows(out model); 
469
470         Selection.Clear();
471         foreach(TreePath path in selectpaths) {
472             TreeIter iter;
473             model.GetIter(out iter, path);
474             int id = Int32.Parse(model.GetValue(iter, 0).ToString());
475             Selection.Add(tileset.Tiles[id]);
476         }
477         SelectionChanged();
478     }
479
480     private void ShowException(Exception e) {
481         MessageDialog dialog = new MessageDialog(MainWindow,
482                 DialogFlags.Modal | DialogFlags.DestroyWithParent,
483                 MessageType.Error, ButtonsType.Ok,
484                 e.Message);
485         dialog.Run();
486         dialog.Destroy();
487     }
488 }