5 // resizable, scrollable map view
6 public class MapWindow
: Gtk
.Window
{
8 get { return drawingArea.Level; }
9 set { drawingArea.Level = value; }
12 Wadfile wadfile
= new Wadfile();
17 public MapWindow(string title
) : base(title
) {
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();
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
) {
59 fileMenu
.Append(exitItem
);
60 fileItem
= new MenuItem("File");
61 fileItem
.Submenu
= fileMenu
;
63 levelMenu
= new MenuItem("Level");
65 box
.PackStart(mb
, false, false, 0);
72 MapDrawingArea drawingArea
= new MapDrawingArea();
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() {
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
) {
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);
151 public void SelectLevel(int n
) {
153 Level
.Load(wadfile
.Directory
[n
]);
154 drawingArea
.Transform
= new Transform();
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); }
);
173 levelMenu
.Submenu
= menu
;
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
);