comparison mde/content/Content.d @ 105:08651e8a8c51

Quit button, big changes to content system. Moved mde.gui.content to mde.content to reflect it's not only used by the gui. Split Content module into Content and AStringContent. New AContent and EventContent class. Callbacks are now generic and implemented in AContent. Renamed TextContent to StringContent and ValueContent to AStringContent.
author Diggory Hardy <diggory.hardy@gmail.com>
date Sat, 29 Nov 2008 12:36:39 +0000
parents mde/gui/content/Content.d@ee209602770d
children 6acd96f8685f
comparison
equal deleted inserted replaced
104:ee209602770d 105:08651e8a8c51
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 /** The content system − common interface and a few classes without external dependencies.
17 */
18 module mde.content.Content;
19
20 debug {
21 import tango.util.log.Log : Log, Logger;
22 private Logger logger;
23 static this () {
24 logger = Log.getLogger ("mde.content.Content");
25 }
26 }
27
28 /** IContent − interface for all Content classes.
29 *
30 * Services like copy/paste could work on universal content. However, they would need to run a
31 * conversion to the appropriate type (or try next-oldest item on clipboard?). */
32 interface IContent
33 {
34 /** Generically return strings.
35 *
36 * This serves two purposes: generically returning a string of/related to the content (i == 0),
37 * and returning associated descriptors. Functions should adhere to (or add to) this table.
38 *
39 * $(TABLE
40 * $(TR $(TH i) $(TH returns))
41 * $(TR $(TD 0) $(TD value))
42 * $(TR $(TD 1) $(TD Translated name or null))
43 * $(TR $(TD 2) $(TD Translated description or null))
44 * $(TR $(TD other) $(TD null))
45 * ) */
46 char[] toString (uint i);
47 }
48
49 /** The base for $(I most) content classes.
50 *
51 * Includes generic callback support, toString implementation and symbol access.
52 *
53 * Derived classes should impement:
54 * ---
55 * this (char[] symbol, T val = /+ default value +/);
56 * void opAssign (T val); // assign val, calling callbacks
57 * T opCall (); // return value
58 * alias opCall opCast;
59 * --- */
60 class AContent : IContent
61 {
62 this (char[] symbol) {
63 this.symbol = symbol;
64 }
65
66 void name (char[] n, char[] d = null) {
67 name_ = n;
68 desc_ = d;
69 }
70
71 /// Current implementation has 1 callback; can be changed to allow many.
72 EventContent addCallback (void delegate (AContent) cb) {
73 this.cb = cb;
74 return this;
75 }
76
77 char[] toString (uint i) {
78 return i == 0 ? "No value"
79 : i == 1 ? name_
80 : i == 2 ? desc_
81 : null;
82 }
83
84 /// End of an event, e.g. a button release or end of an edit (calls callbacks).
85 void endEvent () {
86 if (cb)
87 cb (this);
88 }
89
90 final char[] symbol; // Symbol name for this content
91 protected:
92 char[] name_, desc_; // name and description
93 void delegate (AContent) cb;
94 }
95
96 // FIXME: needs changes to allow updating translated strings. Move to Content and extend AContent?
97 /** A generic way to handle a list of type IContent. */
98 class ContentList : AContent
99 {
100 this (char[] symbol, AContent[] list = null) {
101 list_.length = list.length;
102 foreach (i,c; list)
103 list_[i] = c;
104 super (symbol);
105 }
106 this (char[] symbol, AContent[char[]] l) {
107 list_.length = l.length;
108 size_t i;
109 foreach (c; l)
110 list_[i++] = c;
111 super (symbol);
112 }
113
114 IContent[] list () {
115 return list_;
116 }
117
118 protected:
119 final IContent[] list_;
120 }
121
122 /** Created on errors to display and log a message. */
123 class ErrorContent : IContent
124 {
125 this (char[] msg) {
126 msg_ = msg;
127 }
128
129 char[] toString (uint i) {
130 return i == 0 ? msg_
131 : i == 1 ? "Error"
132 : null;
133 }
134
135 protected:
136 char[] msg_;
137 }
138
139 /** A Content with no value but able to pass on an event.
140 *
141 * The point being that a button can be tied to one of these. */
142 alias AContent EventContent;