view mde/gui/WidgetDataSet.d @ 80:ea58f277f487

Gui reorganization and changes; partial implementation of floating widgets. Moved contents of mde/gui/WidgetData.d elsewhere; new gui/WidgetDataSet.d and gui/types.d modules. Changes to widget/createWidget.d Partially implemented FloatingAreaWidget to provide an area for floating "window" widgets. New DebugWidget and some uses of it (e.g. bad widget data). Decoupled OptionChanges from Options.
author Diggory Hardy <diggory.hardy@gmail.com>
date Thu, 07 Aug 2008 11:25:27 +0100
parents mde/gui/WidgetData.d@61ea26abe4dd
children d8fccaa45d5f
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/>. */

/*************************************************************************************************
 * Code to manage the data used to create widgets and save changes to it.
 * 
 * When loading, a WidgetDataSet instance is loaded from file and its data used to create the
 * widgets. An empty WidgetDataChanges instance is also created.
 * 
 * If any data requires changing, it is added to the WidgetDataChanges instance, which also
 * updates the the WidgetDataSet instance used to load the widgets (in case of a re-load from this
 * data). When the data should be saved, if the WidgetDataChanges instance is not empty, data from
 * the highest priority (i.e. the user) file is merged into it, preserving both the current
 * changes and previous changes saved to the use file, before saving to the user file.
 *************************************************************************************************/
module mde.gui.WidgetDataSet;

public import mde.gui.types;

// For loading from file:
import mt = mde.mergetag.DataSet;
import mt = mde.mergetag.DefaultData;
import mt = mde.mergetag.exception;
import mde.mergetag.serialize;
import tango.util.log.Log : Log, Logger;

private Logger logger;
static this () {
    logger = Log.getLogger ("mde.gui.WidgetDataSet");
}

/*************************************************************************************************
 * Contains data for all widgets in a GUI.
 *************************************************************************************************/
class WidgetDataSet : mt.IDataSection
{
    //BEGIN Mergetag code
    void addTag (char[] tp, mt.ID id, char[] dt) {
        // Priority is HIGH_LOW. Only load tag if it doesn't already exist.
        if (tp == "WidgetData" && (id in widgetData) is null) {
            widgetData[id] = deserialize!(WidgetData) (dt);
        }
    }
    // Only WidgetDataChanges is used for writing.
    void writeAll (ItemDelg dlg) {}
    //END Mergetag code
    
    /** Get the widget data for widget i. */
    WidgetData opIndex (widgetID id) {
        auto p = id in widgetData;
        if (p is null) {
            logger.error ("No data for widget "~id~"; creating a debug widget instead.");
            return WidgetData.dbg;
        }
        return *p;
    }
    
    // Per-widget data:
    protected WidgetData[widgetID] widgetData;
}

/*************************************************************************************************
 * Contains changes to widget data.
 * 
 * Can be read as normal and written.
 *************************************************************************************************/
class WidgetDataChanges : WidgetDataSet
{
    /**
     * Params:
     *  wds = The base WidgetDataSet these changes are applied against. */
    this (WidgetDataSet wds) {
        base = wds;
    }
    
    //BEGIN Mergetag code
    // HIGH_LOW priority of addTag allows existing entries (i.e. the changes) to be preserved while
    // other entries are read from files.
    void writeAll (ItemDelg dlg) {
        foreach (id,data; widgetData)
            dlg ("WidgetData", id, serialize!(WidgetData) (data));
    }
    //END Mergetag code
    
    /** Set the widget data for widget i.
     */
    void opIndexAssign (WidgetData d, widgetID i) {
        widgetData[i] = d;
        base.widgetData[i] = d;
    }
    
    /** Do any changes exist? True if no changes have been stored. */
    bool none () {
        return widgetData.length == 0;
    }
    
    protected WidgetDataSet base;
}