view mde/gui/Gui.d @ 34:6b4116e6355c

Work on the Gui: some of the framework for drag & drop. Also made Window an IWidget. Implemented getWidget(x,y) to find the widget under this location for IWidgets (but not Gui). Made Window an IWidget and made it work a little more similarly to widgets. Implemented callbacks on the Gui for mouse events (enabling drag & drop, etc.). committer: Diggory Hardy <diggory.hardy@gmail.com>
author Diggory Hardy <diggory.hardy@gmail.com>
date Fri, 02 May 2008 16:03:52 +0100
parents 316b0230a849
children 928db3c75ed3
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.renderer.createRenderer;
import mde.gui.exception;

// 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.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;
        try {
            reader = confDir.makeMTReader (fileName, PRIORITY.HIGH_ONLY, null, true);
            reader.dataSecCreator = delegate mt.IDataSection(mt.ID id) {
                return new Window (id);
            };
            reader.read;
        } catch (mt.MTException e) {
            logger.error ("Loading GUI aborted:");
            logger.error (e.msg);
            
            return;
        }
        
        // Get the renderer
        char[]* p = "Renderer" in reader.dataset.header.Arg!(char[]).Arg;
        if (p is null || *p is null) {
            logger.error ("Loading GUI aborted: no renderer specified");
            return;
        }
        rend = createRenderer (*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);
                windows ~= w;       // only add if load successful
            } catch (Exception e) {
                logger.error ("Window failed to load: " ~ e.msg);
            }
        }
    }
    //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) {
        // NOTE: buttons receive the up-event even when drag-callbacks are in place.
        foreach (dg; clickCallbacks)
            dg (cx, cy, b, state);
        
        foreach (w; windows) {
            IWidget widg = w.getWidget (cx,cy);
            if (widg !is null) {
                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) {
        foreach (dg; motionCallbacks)
            dg (cx, cy);
    }
    
    //END Methods for external use
    
    //BEGIN IGui methods
    IRenderer renderer ()
    in {
        assert (rend !is null, "Gui: rend is null");
    } body {
        return rend;
    }
    
    void addClickCallback (void 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.
    IRenderer rend;
    // callbacks indexed by their frame pointers:
    void delegate(ushort cx, ushort cy, ubyte b, bool state) [void*] clickCallbacks;
    void delegate(ushort cx, ushort cy) [void*] motionCallbacks;
}