Mercurial > projects > mde
view mde/gui/Gui.d @ 63:66d555da083e
Moved many modules/packages to better reflect usage.
author | Diggory Hardy <diggory.hardy@gmail.com> |
---|---|
date | Fri, 27 Jun 2008 18:35:33 +0100 |
parents | d43523ed4b62 |
children | 891211f034f2 |
line wrap: on
line source
/* LICENSE BLOCK Part of mde: a Modular D game-oriented Engine Copyright © 2007-2008 Diggory Hardy This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ /** The Gui class. * * This is the module to use externally to create a graphical user interface (likely also with * content modules). * * Possibly add a GuiManager to update all active GUIs and pass coordinates (remapping if necessary). */ module mde.gui.Gui; import mde.gui.IGui; import mde.gui.widget.Ifaces; import mde.gui.widget.Window; import mde.gui.exception; // For adding the input event callbacks and requesting redraws: import imde = mde.imde; import mde.input.Input; import mde.scheduler.Scheduler; // For loading from file: import mt = mde.mergetag.DataSet; import mt = mde.mergetag.DefaultData; import mt = mde.mergetag.exception; import mde.mergetag.Reader; import mde.mergetag.Writer; import mde.setup.paths; import tango.util.log.Log : Log, Logger; private Logger logger; static this () { logger = Log.getLogger ("mde.gui.gui"); gui = new Gui; // until Guis are handled otherwise, this may as well be the case } Gui gui; // Currently just one instance; handle differently later. // Handle externally or with a GUI Manager? /** A GUI handles a bunch of windows, all to be drawn to the same device. */ class Gui : IGui { //BEGIN Methods for external use //BEGIN Loading code /** Load all windows from the file gui. */ void load (char[] fileName) { if (!confDir.exists (fileName)) { logger.error ("Unable to load GUI: no config file!"); return; // not a fatal error (so long as the game can run without a GUI!) } IReader reader; try { reader = confDir.makeMTReader (fileName, PRIORITY.HIGH_LOW, null, true); reader.dataSecCreator = delegate mt.IDataSection(mt.ID id) { return new Window (id); }; reader.read; } catch (Exception e) { logger.error ("Unable to load GUI: errors parsing config file ("~confDir.getFileName(fileName,PRIORITY.HIGH_LOW)~"):"); logger.error (e.msg); throw new GuiException ("Failure parsing config file"); } // Get the renderer char[]* p = RENDERER in reader.dataset.header.Arg!(char[]).Arg; if (p is null || *p is null) { logger.warn ("no renderer specified: defaulting to Simple"); rendName = "Simple"; } else rendName = *p; // get list windows.length = reader.dataset.sec.length; // pre-allocate windows.length = 0; foreach (sec; reader.dataset.sec) { Window w = cast(Window) sec; debug if (w is null) { logger.error (__FILE__ ~ "(GUI.load): code error (w is null)"); continue; } try { w.finalise (this); // if this fails, the window (but nothing else) won't work windows ~= w; // only add if load successful } catch (Exception e) { logger.error ("Window failed to load: " ~ e.msg); } } imde.input.addMouseClickCallback(&clickEvent); imde.input.addMouseMotionCallback(&motionEvent); } void save (char[] fileName) { mt.DataSet ds = new mt.DataSet; // Add header: ds.header = new mt.DefaultData; ds.header.Arg!(char[]).Arg[RENDERER] = rendName; // Add windows to be saved: foreach (window; windows) ds.sec [window.name] = window; try { // Save IWriter writer; writer = confDir.makeMTWriter (fileName, ds); writer.write; } catch (mt.MTException e) { logger.error ("Saving GUI failed:"); logger.error (e.msg); return; } } private static const { auto RENDERER = "Renderer"; } //END Loading code /** Draw each window. * * Currently no concept of how to draw overlapping windows, or how to not bother drawing windows * which don't need redrawing. */ void draw() { foreach_reverse (w; windows) // Draw, starting with back-most window. w.draw; } /** For mouse click events. * * Sends the event on to the relevant windows and all click callbacks. */ void clickEvent (ushort cx, ushort cy, ubyte b, bool state) { debug scope (failure) logger.warn ("clickEvent: failed!"); // NOTE: buttons receive the up-event even when drag-callbacks are in place. foreach (dg; clickCallbacks) if (dg (cast(wdabs)cx, cast(wdabs)cy, b, state)) return; // See IGui.addClickCallback's documentation foreach (i,w; windows) { IWidget widg = w.getWidget (cast(wdabs)cx,cast(wdabs)cy); if (widg !is null) { // Bring to front windows = w ~ windows[0..i] ~ windows[i+1..$]; widg.clickEvent (cast(wdabs)cx,cast(wdabs)cy,b,state); return; // only pass to first window } } } /** For mouse motion events. * * Sends the event on to all motion callbacks. */ void motionEvent (ushort cx, ushort cy) { debug scope (failure) logger.warn ("motionEvent: failed!"); foreach (dg; motionCallbacks) dg (cast(wdabs)cx, cast(wdabs)cy); } //END Methods for external use //BEGIN IGui methods char[] rendererName () { return rendName; } void requestRedraw () { imde.mainSchedule.request(imde.SCHEDULE.DRAW); } void addClickCallback (bool delegate(wdabs, wdabs, ubyte, bool) dg) { clickCallbacks[dg.ptr] = dg; } void addMotionCallback (void delegate(wdabs, wdabs) dg) { motionCallbacks[dg.ptr] = dg; } void removeCallbacks (void* frame) { clickCallbacks.remove(frame); motionCallbacks.remove(frame); } //END IGui methods private: Window[] windows; // Windows. First window is "on top", others may be obscured. char[] rendName; // Name of renderer; for saving and creating renderers // callbacks indexed by their frame pointers: bool delegate(wdabs cx, wdabs cy, ubyte b, bool state) [void*] clickCallbacks; void delegate(wdabs cx, wdabs cy) [void*] motionCallbacks; }