comparison 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
comparison
equal deleted inserted replaced
79:61ea26abe4dd 80:ea58f277f487
1 /* LICENSE BLOCK
2 Part of mde: a Modular D game-oriented Engine
3 Copyright © 2007-2008 Diggory Hardy
4
5 This program is free software: you can redistribute it and/or modify it under the terms
6 of the GNU General Public License as published by the Free Software Foundation, either
7 version 2 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 See the GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program. If not, see <http://www.gnu.org/licenses/>. */
15
16 /*************************************************************************************************
17 * Code to manage the data used to create widgets and save changes to it.
18 *
19 * When loading, a WidgetDataSet instance is loaded from file and its data used to create the
20 * widgets. An empty WidgetDataChanges instance is also created.
21 *
22 * If any data requires changing, it is added to the WidgetDataChanges instance, which also
23 * updates the the WidgetDataSet instance used to load the widgets (in case of a re-load from this
24 * data). When the data should be saved, if the WidgetDataChanges instance is not empty, data from
25 * the highest priority (i.e. the user) file is merged into it, preserving both the current
26 * changes and previous changes saved to the use file, before saving to the user file.
27 *************************************************************************************************/
28 module mde.gui.WidgetDataSet;
29
30 public import mde.gui.types;
31
32 // For loading from file:
33 import mt = mde.mergetag.DataSet;
34 import mt = mde.mergetag.DefaultData;
35 import mt = mde.mergetag.exception;
36 import mde.mergetag.serialize;
37 import tango.util.log.Log : Log, Logger;
38
39 private Logger logger;
40 static this () {
41 logger = Log.getLogger ("mde.gui.WidgetDataSet");
42 }
43
44 /*************************************************************************************************
45 * Contains data for all widgets in a GUI.
46 *************************************************************************************************/
47 class WidgetDataSet : mt.IDataSection
48 {
49 //BEGIN Mergetag code
50 void addTag (char[] tp, mt.ID id, char[] dt) {
51 // Priority is HIGH_LOW. Only load tag if it doesn't already exist.
52 if (tp == "WidgetData" && (id in widgetData) is null) {
53 widgetData[id] = deserialize!(WidgetData) (dt);
54 }
55 }
56 // Only WidgetDataChanges is used for writing.
57 void writeAll (ItemDelg dlg) {}
58 //END Mergetag code
59
60 /** Get the widget data for widget i. */
61 WidgetData opIndex (widgetID id) {
62 auto p = id in widgetData;
63 if (p is null) {
64 logger.error ("No data for widget "~id~"; creating a debug widget instead.");
65 return WidgetData.dbg;
66 }
67 return *p;
68 }
69
70 // Per-widget data:
71 protected WidgetData[widgetID] widgetData;
72 }
73
74 /*************************************************************************************************
75 * Contains changes to widget data.
76 *
77 * Can be read as normal and written.
78 *************************************************************************************************/
79 class WidgetDataChanges : WidgetDataSet
80 {
81 /**
82 * Params:
83 * wds = The base WidgetDataSet these changes are applied against. */
84 this (WidgetDataSet wds) {
85 base = wds;
86 }
87
88 //BEGIN Mergetag code
89 // HIGH_LOW priority of addTag allows existing entries (i.e. the changes) to be preserved while
90 // other entries are read from files.
91 void writeAll (ItemDelg dlg) {
92 foreach (id,data; widgetData)
93 dlg ("WidgetData", id, serialize!(WidgetData) (data));
94 }
95 //END Mergetag code
96
97 /** Set the widget data for widget i.
98 */
99 void opIndexAssign (WidgetData d, widgetID i) {
100 widgetData[i] = d;
101 base.widgetData[i] = d;
102 }
103
104 /** Do any changes exist? True if no changes have been stored. */
105 bool none () {
106 return widgetData.length == 0;
107 }
108
109 protected WidgetDataSet base;
110 }