add MapWindow.cs; don't show resources in levels menu
[weland.git] / MapWindow.cs
blob3a2a714b540fffb1fa8bb3fc02b86bd7b69e1a0d
1 using Gtk;
2 using System;
4 namespace Weland {
5 // resizable, scrollable map view
6 public class MapWindow : Gtk.Window {
7 public Level Level {
8 get { return drawingArea.Level; }
9 set { drawingArea.Level = value; }
12 Wadfile wadfile = new Wadfile();
14 MenuItem levelMenu;
15 MenuItem fileItem;
17 public MapWindow(string title) : base(title) {
18 AllowShrink = true;
20 Table table = new Table(2, 2, false);
22 drawingArea.ConfigureEvent += new ConfigureEventHandler(ConfigureDrawingArea);
23 drawingArea.ButtonPressEvent += new ButtonPressEventHandler(ButtonPress);
24 drawingArea.Events = Gdk.EventMask.ExposureMask | Gdk.EventMask.ButtonPressMask;
25 drawingArea.SetSizeRequest(600, 400);
27 hadjust = new Adjustment(0, 0, 0, 0, 0, 0);
28 vadjust = new Adjustment(0, 0, 0, 0, 0, 0);
30 hscroll = new HScrollbar(hadjust);
31 vscroll = new VScrollbar(vadjust);
33 hscroll.ValueChanged += new EventHandler(HValueChangedEvent);
34 vscroll.ValueChanged += new EventHandler(VValueChangedEvent);
35 table.Attach(drawingArea, 0, 1, 0, 1, AttachOptions.Shrink | AttachOptions.Expand | AttachOptions.Fill, AttachOptions.Shrink | AttachOptions.Expand | AttachOptions.Fill, 0, 0);
36 table.Attach(hscroll, 0, 1, 1, 2, AttachOptions.Shrink | AttachOptions.Expand | AttachOptions.Fill, 0, 0, 0);
37 table.Attach(vscroll, 1, 2, 0, 1, 0, AttachOptions.Shrink | AttachOptions.Expand | AttachOptions.Fill, 0, 0);
39 AccelGroup agr = new AccelGroup();
40 AddAccelGroup(agr);
42 VBox box = new VBox();
44 MenuBar mb = new MenuBar();
45 Menu fileMenu = new Menu();
46 MenuItem openItem = new MenuItem("Open");
47 openItem.AddAccelerator("activate", agr, new AccelKey(Gdk.Key.o, Gdk.ModifierType.ControlMask, AccelFlags.Visible));
48 openItem.Activated += new EventHandler(OpenFile);
50 fileMenu.Append(openItem);
52 MenuItem exitItem = new MenuItem("Quit");
54 exitItem.AddAccelerator("activate", agr, new AccelKey(Gdk.Key.q, Gdk.ModifierType.ControlMask, AccelFlags.Visible));
55 exitItem.Activated += new EventHandler(
56 delegate(object obj, EventArgs a) {
57 Application.Quit();
58 });
59 fileMenu.Append(exitItem);
60 fileItem = new MenuItem("File");
61 fileItem.Submenu = fileMenu;
62 mb.Append(fileItem);
63 levelMenu = new MenuItem("Level");
64 mb.Append(levelMenu);
65 box.PackStart(mb, false, false, 0);
66 box.Add(table);
68 Add(box);
72 MapDrawingArea drawingArea = new MapDrawingArea();
73 HScrollbar hscroll;
74 VScrollbar vscroll;
76 Adjustment hadjust;
77 Adjustment vadjust;
79 public void Center(short X, short Y) {
80 drawingArea.Center(X, Y);
81 vscroll.Value = drawingArea.Transform.YOffset;
82 hscroll.Value = drawingArea.Transform.XOffset;
85 void AdjustScrollRange() {
86 int width, height;
87 width = drawingArea.Allocation.Width;
88 height = drawingArea.Allocation.Height;
89 double scale = drawingArea.Transform.Scale;
90 double VUpper = short.MaxValue - height / scale;
91 if (VUpper < short.MinValue)
92 VUpper = short.MinValue;
94 vadjust.Lower = short.MinValue;
95 vadjust.Upper = VUpper;
96 if (drawingArea.Transform.YOffset > VUpper) {
97 vscroll.Value = VUpper;
98 drawingArea.Transform.YOffset = (short) VUpper;
101 vadjust.StepIncrement = 24.0 / scale;
102 vadjust.PageIncrement = 240.0 / scale;
104 hadjust.Lower = short.MinValue;
105 double HUpper = short.MaxValue - width / scale;
106 if (HUpper < short.MinValue)
107 HUpper = short.MinValue;
108 hadjust.Upper = HUpper;
109 if (drawingArea.Transform.XOffset > HUpper) {
110 hscroll.Value = HUpper;
111 drawingArea.Transform.XOffset = (short) HUpper;
114 hadjust.StepIncrement = 24.0 / scale;
115 hadjust.PageIncrement = 240.0 / scale;
117 drawingArea.QueueDrawArea(0, 0, width, height);
120 void HValueChangedEvent(object obj, EventArgs e) {
121 drawingArea.Transform.XOffset = (short) hscroll.Value;
123 drawingArea.QueueDrawArea(0, 0, drawingArea.Allocation.Width, drawingArea.Allocation.Height);
126 void VValueChangedEvent(object obj, EventArgs e) {
127 drawingArea.Transform.YOffset = (short) vscroll.Value;
129 drawingArea.QueueDrawArea(0, 0, drawingArea.Allocation.Width, drawingArea.Allocation.Height);
132 void ConfigureDrawingArea(object obj, ConfigureEventArgs args) {
133 AdjustScrollRange();
134 args.RetVal = true;
137 void ButtonPress(object obj, ButtonPressEventArgs args) {
138 Gdk.EventButton ev = args.Event;
139 short X = drawingArea.Transform.ToMapX(ev.X);
140 short Y = drawingArea.Transform.ToMapY(ev.Y);
141 if (ev.Button == 1) {
142 drawingArea.Transform.Scale *= Math.Sqrt(2.0);
143 } else if (ev.Button == 3) {
144 drawingArea.Transform.Scale /= Math.Sqrt(2.0);
146 Center(X, Y);
147 AdjustScrollRange();
148 args.RetVal = true;
151 public void SelectLevel(int n) {
152 Level = new Level();
153 Level.Load(wadfile.Directory[n]);
154 drawingArea.Transform = new Transform();
155 Center(0, 0);
156 AdjustScrollRange();
157 Title = wadfile.Directory[n].LevelName;
160 public void OpenFile(string filename) {
161 wadfile = new Wadfile();
162 wadfile.Load(filename);
163 Menu menu = new Menu();
164 foreach (var kvp in wadfile.Directory) {
165 if (kvp.Value.Chunks.ContainsKey(Level.Tag)) {
166 MenuItem item = new MenuItem(kvp.Value.LevelName);
167 int levelNumber = kvp.Key;
168 item.Activated += new EventHandler(delegate(object obj, EventArgs args) { SelectLevel(levelNumber); });
169 menu.Append(item);
172 menu.ShowAll();
173 levelMenu.Submenu = menu;
174 SelectLevel(0);
177 void OpenFile(object obj, EventArgs args) {
178 FileChooserDialog d = new FileChooserDialog("Choose the file to open", this, FileChooserAction.Open, "Cancel", ResponseType.Cancel, "Open", ResponseType.Accept);
179 if (d.Run() == (int) ResponseType.Accept) {
180 OpenFile(d.Filename);
182 d.Destroy();