view 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
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.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. */
/* NOTE: currently GUI just keeps a list of windows and simply calls draw and clickEvent on them all.
* Coords should be stored here and draw/clickEvent should be called like for widgets.
* (Also functionality like z-order?) */
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) {
                return new Window;
            };
            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 (w; windows)
            w.draw();
    }
    
    /** Send an input event.
    *
    * I.e. send all mouse click events to all active GUIs, which check the coordinates and forward
    * to any relevent windows. */
    void clickEvent (ushort cx, ushort cy, ubyte b, bool state) {
        foreach (w; windows)
            w.clickEvent (cx,cy,b,state);
    }
    //END Methods for external use
    
    //BEGIN IGui methods
    IRenderer renderer ()
    in {
        assert (rend !is null, "Gui: rend is null");
    } body {
        return rend;
    }
    //END IGui methods
    
private:
    Window[] windows;
    IRenderer rend;
}