view mde/gui/Widget.d @ 27:0aa621b3e070

Some GUI work, plus a small fix in the paths module. Implemented GUI code to load windows from file with a basic widget and draw. Fixed a bug in mde.resource.paths.mdeDirectory.makeMTReader when called with readOrder == PRIORITY.HIGH_ONLY. committer: Diggory Hardy <diggory.hardy@gmail.com>
author Diggory Hardy <diggory.hardy@gmail.com>
date Fri, 04 Apr 2008 17:07:38 +0100
parents
children b5fadd8d930b
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/>. */

/// GUI Widget module.
module mde.gui.Widget;

import mde.gui.exception;

import gl = mde.gl;

/** Interface for widgets (may become a class).
*
* Variable loading/saving efficiency and code-reuse need to be revised later!
* Give each Widget an int[] of data which it should check in this() and throw if bad?
*/
interface Widget
{
    /** Draw, starting from given x and y.
    *
    * Maybe replace later with drawClipped, especially for cases where only part of the widget is
    * visible behind a scrolling window or hidden window. */
    void draw (int x, int y);
    
    /** Calculate the size of the widget, taking into account child-widgets.
    *
    * Later will work out how to make this more flexible. */
    void getSize (out int w, out int h);
}

/// Draws a box. That's it.
class BoxWidget : Widget
{
    int w, h;   // size
    
    this (int[] data) {
        if (data.length != 2) throw new WidgetDataException;
        
        w = data[0];
        h = data[1];
    }
    
    void draw (int x, int y) {
        gl.setColor (1.0f, 1.0f, 0.0f);
        gl.drawBox (x,x+w, y,y+h);
    }
    
    void getSize (out int w, out int h) {
        w = this.w;
        h = this.h;
    }
}

enum WIDGET_TYPES : int {
    BOX = 1
}

Widget createWidget (int[] data) {
    if (data.length < 1) throw new WidgetDataException ("No widget data");
    int type = data[0];     // type is first element of data
    data = data[1..$];      // the rest is passed to the Widget
    
    if (type == WIDGET_TYPES.BOX) return new BoxWidget (data);
    else throw new WidgetDataException ("Bad widget type");
}