Mercurial > projects > mde
comparison mde/gui/Gui.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 Gui class. | |
17 * | |
18 * This is the module to use externally to create a graphical user interface (likely also with | |
19 * content modules). | |
20 * | |
21 * Possibly add a GuiManager to update all active GUIs and pass coordinates (remapping if necessary). */ | |
22 module mde.gui.Gui; | |
23 | |
24 import mde.gui.IGui; | |
25 import mde.gui.widget.Window; | |
26 import mde.gui.renderer.createRenderer; | |
27 import mde.gui.exception; | |
28 | |
29 // For loading from file: | |
30 import mt = mde.mergetag.DataSet; | |
31 import mt = mde.mergetag.DefaultData; | |
32 import mt = mde.mergetag.exception; | |
33 import mde.mergetag.Reader; | |
34 import mde.resource.paths; | |
35 | |
36 import tango.util.log.Log : Log, Logger; | |
37 | |
38 private Logger logger; | |
39 static this () { | |
40 logger = Log.getLogger ("mde.gui.gui"); | |
41 | |
42 gui = new Gui; // until Guis are handled otherwise, this may as well be the case | |
43 } | |
44 | |
45 Gui gui; // Currently just one instance; handle differently later. | |
46 // Handle externally or with a GUI Manager? | |
47 | |
48 /** A GUI handles a bunch of windows, all to be drawn to the same device. */ | |
49 /* NOTE: currently GUI just keeps a list of windows and simply calls draw and clickEvent on them all. | |
50 * Coords should be stored here and draw/clickEvent should be called like for widgets. | |
51 * (Also functionality like z-order?) */ | |
52 class Gui : IGui { | |
53 //BEGIN Methods for external use | |
54 //BEGIN Loading code | |
55 /** Load all windows from the file gui. */ | |
56 void load(char[] fileName) { | |
57 if (!confDir.exists (fileName)) { | |
58 logger.error ("Unable to load GUI: no config file!"); | |
59 return; // not a fatal error (so long as the game can run without a GUI!) | |
60 } | |
61 | |
62 IReader reader; | |
63 try { | |
64 reader = confDir.makeMTReader (fileName, PRIORITY.HIGH_ONLY, null, true); | |
65 reader.dataSecCreator = delegate mt.IDataSection(mt.ID) { | |
66 return new Window; | |
67 }; | |
68 reader.read; | |
69 } catch (mt.MTException e) { | |
70 logger.error ("Loading GUI aborted:"); | |
71 logger.error (e.msg); | |
72 | |
73 return; | |
74 } | |
75 | |
76 // Get the renderer | |
77 char[]* p = "Renderer" in reader.dataset.header.Arg!(char[]).Arg; | |
78 if (p is null || *p is null) { | |
79 logger.error ("Loading GUI aborted: no renderer specified"); | |
80 return; | |
81 } | |
82 rend = createRenderer (*p); | |
83 | |
84 // get list | |
85 windows.length = reader.dataset.sec.length; // pre-allocate | |
86 windows.length = 0; | |
87 foreach (sec; reader.dataset.sec) { | |
88 Window w = cast(Window) sec; | |
89 debug if (w is null) { | |
90 logger.error (__FILE__ ~ "(GUI.load): code error (w is null)"); | |
91 continue; | |
92 } | |
93 try { | |
94 w.finalise (this); | |
95 windows ~= w; // only add if load successful | |
96 } catch (Exception e) { | |
97 logger.error ("Window failed to load: " ~ e.msg); | |
98 } | |
99 } | |
100 } | |
101 //END Loading code | |
102 | |
103 /** Draw each window. | |
104 * | |
105 * Currently no concept of how to draw overlapping windows, or how to not bother drawing windows | |
106 * which don't need redrawing. */ | |
107 void draw() { | |
108 foreach (w; windows) | |
109 w.draw(); | |
110 } | |
111 | |
112 /** Send an input event. | |
113 * | |
114 * I.e. send all mouse click events to all active GUIs, which check the coordinates and forward | |
115 * to any relevent windows. */ | |
116 void clickEvent (ushort cx, ushort cy, ubyte b, bool state) { | |
117 foreach (w; windows) | |
118 w.clickEvent (cx,cy,b,state); | |
119 } | |
120 //END Methods for external use | |
121 | |
122 //BEGIN IGui methods | |
123 IRenderer renderer () | |
124 in { | |
125 assert (rend !is null, "Gui: rend is null"); | |
126 } body { | |
127 return rend; | |
128 } | |
129 //END IGui methods | |
130 | |
131 private: | |
132 Window[] windows; | |
133 IRenderer rend; | |
134 } |