Mercurial > projects > mde
view mde/gui/Gui.d @ 45:0fd51d2c6c8a
Several changes to resising windows and layout widgets. This commit still has some bugs.
Moved the implementable widgets from mde.gui.widget.Widget to miscWidgets, leaving base widgets in Widget.
Rewrote some of GridLayoutWidget's implementation. Made many operations general to work for either columns or rows. Some optimisations were intended but ended up being removed due to problems.
Allowed layout's to resize from either direction (only with window resizes).
committer: Diggory Hardy <diggory.hardy@gmail.com>
author | Diggory Hardy <diggory.hardy@gmail.com> |
---|---|
date | Thu, 22 May 2008 11:34:09 +0100 |
parents | 1530d9c04d4d |
children | e0839643ff52 |
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.resource.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; reader = confDir.makeMTReader (fileName, PRIORITY.HIGH_LOW, null, true); reader.dataSecCreator = delegate mt.IDataSection(mt.ID id) { return new Window (id); }; reader.read; // 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 (cx, cy, b, state)) return; // See IGui.addClickCallback's documentation foreach (i,w; windows) { IWidget widg = w.getWidget (cx,cy); if (widg !is null) { // Bring to front windows = w ~ windows[0..i] ~ windows[i+1..$]; widg.clickEvent (cx,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 (cx, 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(ushort, ushort, ubyte, bool) dg) { clickCallbacks[dg.ptr] = dg; } void addMotionCallback (void delegate(ushort, ushort) 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(ushort cx, ushort cy, ubyte b, bool state) [void*] clickCallbacks; void delegate(ushort cx, ushort cy) [void*] motionCallbacks; }