view mde/gui/widget/createWidget.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 07bd1a09e161
children 672b6b162a36
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.createWidget;

import mde.gui.widget.Ifaces;
import mde.gui.exception : WidgetDataException;

// Widgets to create:
import mde.gui.widget.layout;
import mde.gui.widget.miscWidgets;

/** Create a widget of type data[0] (see enum WIDGET_TYPES) for _window window, with initialisation
* data [1..$]. */
IWidget createWidget (IWindow window, int[] data)
in {
    assert (window !is null, "createWidget: window is null");
} body {
    if (data.length < 1) throw new WidgetDataException ("No widget data");
    int type = data[0];     // type is first element of data
    // the whole of data is passed to the Widget
    
    mixin (binarySearch ("type", WIDGETS));
    throw new WidgetDataException ("Bad widget type");
}

/+ for converting to a char[] name (unused)
static this() {
    WIDGET_NAMES = [
            FixedBlank : "FixedBlank",
            SizableBlank : "SizableBlank",
            Button : "Button",
            GridLayout : "GridLayout"
                    ];
}+/

private:
/// Widget types.
enum WIDGET_TYPE : int {
    WSIZABLE                = 0x1000,   // horizontally resizable
    HSIZABLE                = 0x2000,   // vertically resizable
    INTERACTIBLE            = 0x4000,   // any specific interaction
    LAYOUT                  = 0x8000,   // is a layout widget (i.e. has sub-widgets)?
    
    // Use widget names rather than usual capitals convention
    
    // blank: 0x1
    FixedBlank              = 0x1,
    SizableBlank            = WSIZABLE | HSIZABLE | 0x1,
    
    // text: 0x2
    Text		    = 0x2,
    
    // buttons: 0x10
    Button                  = INTERACTIBLE | 0x10,
    
    GridLayout              = LAYOUT | WSIZABLE | HSIZABLE | 0x4
}

//const char[][int] WIDGET_NAMES;

// Only used for binarySearch algorithm generation; must be ordered by numerical values.
const char[][] WIDGETS = [
        "FixedBlank",
        "Text",
        "SizableBlank",
        "Button",
        "GridLayout"   ];

// Purely to add indentation. Could just return "" without affecting functionality.
static char[] indent (uint i) {
    char[] ret;
    for (; i > 0; --i) ret ~= "  ";
        // This is not executable at compile time:
        //ret.length = i * 4;		// number of characters for each indentation
        //ret[] = ' ';		// character to indent with
    return ret;
}
//char[] indent (uint) {  return "";  }

/* Generates a binary search algorithm. */
char[] binarySearch (char[] var, char[][] consts, int indents = 0) {
    if (consts.length > 3) {
        return indent(indents) ~ "if (" ~ var ~ " <= WIDGET_TYPE." ~ consts[$/2 - 1] ~ ") {\n" ~
                binarySearch (var, consts[0 .. $/2], indents + 1) ~
                indent(indents) ~ "} else {\n" ~
                binarySearch (var, consts[$/2 .. $], indents + 1) ~
                indent(indents) ~ "}\n";
    } else {
        char[] ret;
        ret ~= indent(indents);
        foreach (c; consts) {
            ret ~= "if (" ~ var ~ " == WIDGET_TYPE." ~ c ~ ") {\n" ~
                    indent(indents+1) ~ "return new " ~ c ~ "Widget (window, data);\n" ~
                    indent(indents) ~ "} else ";
        }
        ret = ret[0..$-6] ~ '\n';  // remove last else
        return ret;
    }
}