comparison mde/gui/widget/Window.d @ 32:316b0230a849

Lots more work on the GUI. Also renamed lots of files. Lots of changes to the GUI. Renderer is now used exclusively for rendering and WidgetDecoration is gone. Renamed lots of files to conform to case policies. committer: Diggory Hardy <diggory.hardy@gmail.com>
author Diggory Hardy <diggory.hardy@gmail.com>
date Wed, 30 Apr 2008 18:05:56 +0100
parents
children 6b4116e6355c
comparison
equal deleted inserted replaced
31:baa87e68d7dc 32:316b0230a849
1 /* LICENSE BLOCK
2 Part of mde: a Modular D game-oriented Engine
3 Copyright © 2007-2008 Diggory Hardy
4
5 This program is free software: you can redistribute it and/or modify it under the terms
6 of the GNU General Public License as published by the Free Software Foundation, either
7 version 2 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 See the GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program. If not, see <http://www.gnu.org/licenses/>. */
15
16 /** The Window class. Hopefully eventually this will become a widget to make things a bit more
17 * generic. */
18 module mde.gui.widget.Window;
19
20 import mde.gui.widget.Ifaces;
21 import mde.gui.widget.createWidget;
22
23 import mde.gui.IGui;
24 import mde.gui.exception;
25
26 import mt = mde.mergetag.DataSet;
27 import tango.scrapple.text.convert.parseTo : parseTo;
28 // not yet implemented:
29 //import tango.scrapple.text.convert.parseFrom : parseFrom;
30
31 /** GUI Window class
32 *
33 * A window class instance does two things: (1) specify a region of the screen upon which the window
34 * and its associated widgets are drawn, and (2) load, save, and generally manage all its widgets.
35 *
36 * Let the window load a table of widget data, of type int[][widgetID]. Each widget will, when
37 * created, be given its int[] of data, which this() must confirm is valid (or throw).
38 */
39 class Window : mt.IDataSection, IWindow
40 {
41 //BEGIN Methods for GUI
42 /** Call after loading is finished to setup the window and confirm that it's valid.
43 *
44 * Throws: WindowLoadException. Do not use the instance in this case! */
45 void finalise (IGui gui) {
46 // Check data was loaded:
47 if (widgetData is null) throw new WindowLoadException ("No widget data");
48
49 // Create the renderer:
50 rend = gui.renderer;
51
52 // Create the primary widget (and indirectly all sub-widgets), throwing on error:
53 widget = getWidget (0, this);// primary widget always has ID 0.
54
55 widgetData = null; // data is no longer needed: allow GC to collect (cannot safely delete)
56
57 widgetX = x + rend.windowBorder; // widget position
58 widgetY = y + rend.windowBorder; // must be updated if the window is moved
59
60 widget.getCurrentSize (w,h);// Find the initial size
61 w += rend.windowBorder * 2; // Adjust for border
62 h += rend.windowBorder * 2;
63
64 xw = x+w;
65 yh = y+h;
66 }
67
68 void draw () {
69 // background
70 rend.drawWindow (x,y, w,h);
71
72 // Tell the widget to draw itself:
73 widget.draw(widgetX, widgetY);
74 }
75
76 void clickEvent (ushort cx, ushort cy, ubyte b, bool state) {
77 if (cx >= x && cx < xw && cy >= y && cy < yh) { // click on window?
78 if (cx >= widgetX && cx < xw-rend.windowBorder && cy >= widgetY && cy < yh-rend.windowBorder) // click on widget?
79 widget.clickEvent (cx-widgetX, cy-widgetY, b, state);
80 // FIXME: else window dragging?
81 }
82 }
83
84 //BEGIN Mergetag code
85 void addTag (char[] tp, mt.ID id, char[] dt) {
86 if (tp == "int[][int]") {
87 if (id == "widgetData") {
88 widgetData = cast(int[][widgetID]) parseTo!(int[][int]) (dt);
89 }
90 } else if (tp == "int") {
91 if (id == "x") {
92 x = parseTo!(int) (dt);
93 } else if (id == "y") {
94 y = parseTo!(int) (dt);
95 }
96 }
97 }
98 void writeAll (ItemDelg dlg) {
99 }
100 //END Mergetag code
101 //END Methods for GUI
102
103 //BEGIN IWindow methods
104 /** Get/create a widget by ID.
105 *
106 * Should $(I only) be called internally and by sub-widgets! */
107 IWidget getWidget (widgetID i, IParentWidget parent)
108 in {
109 // widgetData is normally left to be garbage collected after widgets have been created:
110 assert (widgetData !is null, "getWidget: widgetData is null");
111 } body {
112 // See if it's already been created:
113 IWidget* p = i in widgets;
114 if (p !is null) return *p; // yes
115 else { // no
116 int[]* d = i in widgetData;
117 if (d is null) throw new WindowLoadException ("Widget not found");
118
119 // Throws WidgetDataException (a WindowLoadException) if bad data:
120 IWidget widg = createWidget (this, parent, *d);
121 widgets[i] = widg;
122 return widg;
123 }
124 }
125
126 /+void requestRedraw () {
127 }+/
128
129 IRenderer renderer () {
130 return rend;
131 }
132 //END IWindow methods
133
134 //BEGIN IParentWidget methods
135 //END IParentWidget methods
136
137 private:
138 int[][widgetID] widgetData; // Data for all widgets under this window (deleted after loading)
139 IWidget[widgetID] widgets; // List of all widgets under this window (created on demand).
140 IWidget widget; // The primary widget in this window.
141
142 IRenderer rend; // The window's renderer
143 int x,y; // Window position
144 int w,h; // Window size (calculated from Widgets)
145 int xw, yh; // x+w, y+h (frequent use by clickEvent)
146 int widgetX, widgetY; // Widget position (= window position plus BORDER_WIDTH)
147 }