view mde/gui/widget/Window.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 Window class. Hopefully eventually this will become a widget to make things a bit more
* generic. */
module mde.gui.widget.Window;

import mde.gui.widget.Ifaces;
import mde.gui.widget.createWidget;

import mde.gui.IGui;
import mde.gui.exception;

import mt = mde.mergetag.DataSet;
import tango.scrapple.text.convert.parseTo : parseTo;
// not yet implemented:
//import tango.scrapple.text.convert.parseFrom : parseFrom;

/** GUI Window class
 *
 * A window class instance does two things: (1) specify a region of the screen upon which the window
 * and its associated widgets are drawn, and (2) load, save, and generally manage all its widgets.
 *
 * Let the window load a table of widget data, of type int[][widgetID]. Each widget will, when
 * created, be given its int[] of data, which this() must confirm is valid (or throw).
 */
class Window : mt.IDataSection, IWindow
{
    //BEGIN Methods for GUI
    /** Call after loading is finished to setup the window and confirm that it's valid.
     *
     * Throws: WindowLoadException. Do not use the instance in this case! */
    void finalise (IGui gui) {
        // Check data was loaded:
        if (widgetData is null) throw new WindowLoadException ("No widget data");
        
        // Create the renderer:
        rend = gui.renderer;
        
        // Create the primary widget (and indirectly all sub-widgets), throwing on error:
        widget = getWidget (0, this);// primary widget always has ID 0.
        
        widgetData = null;          // data is no longer needed: allow GC to collect (cannot safely delete)
        
        widgetX = x + rend.windowBorder;  // widget position
        widgetY = y + rend.windowBorder;  // must be updated if the window is moved
        
        widget.getCurrentSize (w,h);// Find the initial size
        w += rend.windowBorder * 2;       // Adjust for border
        h += rend.windowBorder * 2;
        
        xw = x+w;
        yh = y+h;
    }
    
    void draw () {
        // background
        rend.drawWindow (x,y, w,h);
        
        // Tell the widget to draw itself:
        widget.draw(widgetX, widgetY);
    }
    
    void clickEvent (ushort cx, ushort cy, ubyte b, bool state) {
        if (cx >= x && cx < xw && cy >= y && cy < yh) { // click on window?
            if (cx >= widgetX && cx < xw-rend.windowBorder && cy >= widgetY && cy < yh-rend.windowBorder)   // click on widget?
                widget.clickEvent (cx-widgetX, cy-widgetY, b, state);
            // FIXME: else window dragging?
        }
    }
    
    //BEGIN Mergetag code
    void addTag (char[] tp, mt.ID id, char[] dt) {
        if (tp == "int[][int]") {
            if (id == "widgetData") {
                widgetData = cast(int[][widgetID]) parseTo!(int[][int]) (dt);
            }
        } else if (tp == "int") {
            if (id == "x") {
                x = parseTo!(int) (dt);
            } else if (id == "y") {
                y = parseTo!(int) (dt);
            }
        }
    }
    void writeAll (ItemDelg dlg) {
    }
    //END Mergetag code
    //END Methods for GUI
    
    //BEGIN IWindow methods
    /** Get/create a widget by ID.
     *
     * Should $(I only) be called internally and by sub-widgets! */
    IWidget getWidget (widgetID i, IParentWidget parent)
    in {
        // widgetData is normally left to be garbage collected after widgets have been created:
        assert (widgetData !is null, "getWidget: widgetData is null");
    } body {
        // See if it's already been created:
        IWidget* p = i in widgets;
        if (p !is null) return *p;  // yes
        else {                      // no
            int[]* d = i in widgetData;
            if (d is null) throw new WindowLoadException ("Widget not found");
            
            // Throws WidgetDataException (a WindowLoadException) if bad data:
            IWidget widg = createWidget (this, parent, *d);
            widgets[i] = widg;
            return widg;
        }
    }
    
    /+void requestRedraw () {
    }+/
    
    IRenderer renderer () {
        return rend;
    }
    //END IWindow methods
    
    //BEGIN IParentWidget methods
    //END IParentWidget methods
    
private:
    int[][widgetID] widgetData;     // Data for all widgets under this window (deleted after loading)
    IWidget[widgetID] widgets;      // List of all widgets under this window (created on demand).
    IWidget widget;                 // The primary widget in this window.
    
    IRenderer rend;                 // The window's renderer
    int x,y;                        // Window position
    int w,h;                        // Window size (calculated from Widgets)
    int xw, yh;                     // x+w, y+h (frequent use by clickEvent)
    int widgetX, widgetY;           // Widget position (= window position plus BORDER_WIDTH)
}