Mercurial > projects > mde
view mde/gui/content/Content.d @ 98:49e7cfed4b34
All types of Option have been converted to use ValueContent classes, and their values can be displayed.
author | Diggory Hardy <diggory.hardy@gmail.com> |
---|---|
date | Wed, 12 Nov 2008 13:18:51 +0000 |
parents | 2a364c7d82c9 |
children | 5de5810e3516 |
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/>. */ /** The content system − common types. */ module mde.gui.content.Content; //FIXME: efficient conversions? Need to dup result when formatting a string anyway? import Int = tango.text.convert.Integer; import Float = tango.text.convert.Float; debug { import tango.util.log.Log : Log, Logger; private Logger logger; static this () { logger = Log.getLogger ("mde.gui.content.Content"); } } /** IContent − interface for all Content classes. * * Services like copy/paste could work on universal content. However, they would need to run a * conversion to the appropriate type (or try next-oldest item on clipboard?). * * Currently Content instances can only have their data set on creation. * Each Content class should provide a method to get it's content, e.g. the method text(). * * Extensions to content: * * These extensions require that a content can notify any dependants of changes. * * Use as a state switch (one option from an enumeration). E.g. a button/selection box could set a * state, and a tabbed box could show a tab based on this. Or could represent an option. */ //TODO - write a generic IContent displaying widget. Also a generic editable? // Don't include dimension/drawing stuff because it's renderer specific and content should not be! // NOTE: an interface or a class? interface IContent { /+ NOTE: None of this is really used yet, but was (mostly) intended for clipboard copying. /** Return a copy of self. */ IContent dup (); /** Attempt to convert the content to a specific type (may simply return this if appropriate). * * Annoyingly we can't use cast because overloading by return type isn't supported. */ // FIXME: throw or return null on error or unsupported conversion? ContentText toText (); ContentInt toInt (); /// ditto +/ /** Generically return strings. * * This serves two purposes: generically returning a string of/related to the content (i == 0), * and returning associated descriptors. Functions should adhere to (or add to) this table. * * $(TABLE * $(TR $(TH i) $(TH returns)) * $(TR $(TD 0) $(TD value)) * $(TR $(TD 1) $(TD Translated name or null)) * $(TR $(TD 2) $(TD Translated description or null)) * $(TR $(TD other) $(TD null)) * ) */ char[] toString (uint i); } /** Base class for content containing a simple value. * * All derived classes should support functions to set/get any ValueContent type, but return the * default value of any type other than it's own. */ abstract class ValueContent : IContent { protected this () {} void name (char[] n, char[] d = null) { name_ = n; desc_ = d; } protected: char[] name_, desc_;// name and description, loaded by lookup.Translation } template VContentN(T) { static if (is(T == bool)) { const char[] VContentN = "BoolContent"; } else static if (is(T == int)) { const char[] VContentN = "IntContent"; } else static if (is(T == double)) { const char[] VContentN = "DoubleContent"; } else static if (is(T == char[])) { const char[] VContentN = "TextContent"; } else static assert (false, "No ValueContent of type "~T.stringof); } class BoolContent : ValueContent { /** Create a content with _symbol name symbol. */ this (char[] symbol, bool val = false) { symb = symbol; v = val; } /** Adds cb to the list of callback functions called when the value is changed. Returns this. */ BoolContent addChangeCb (void delegate (char[] symbol,bool value) cb) { cngCb ~= cb; return this; } /// Get the text. char[] toString (uint i) { return (i == 0) ? v ? "true" : "false" : (i == 1) ? name_ : (i == 2) ? desc_ : null; } void opAssign (bool val) { v = val; foreach (cb; cngCb) cb(symb, val); } bool opCall () { return v; } alias opCall opCast; bool v; //FIXME: should be protected but Options needs to set without calling callbacks protected: char[] symb; void delegate (char[],bool)[] cngCb; // change callbacks } /** Text content. */ class TextContent : ValueContent { this (char[] symbol, char[] val = null) { symb = symbol; v = val; } /** Adds cb to the list of callback functions called when the value is changed. Returns this. */ TextContent addChangeCb (void delegate (char[] symbol,char[] value) cb) { cngCb ~= cb; return this; } /// Get the text. char[] toString (uint i) { return (i == 0) ? v : (i == 1) ? name_ : (i == 2) ? desc_ : null; } void opAssign (char[] val) { v = val; foreach (cb; cngCb) cb(symb, val); } char[] opCall () { return v; } alias opCall opCast; char[] v; protected: char[] symb; void delegate (char[],char[])[] cngCb; } /** Integer content. */ class IntContent : ValueContent { /** Create a content with _symbol name symbol. */ this (char[] symbol, int val = 0) { symb = symbol; v = val; } /** Adds cb to the list of callback functions called when the value is changed. Returns this. */ IntContent addChangeCb (void delegate (char[] symbol,int value) cb) { cngCb ~= cb; return this; } /// Get the text. char[] toString (uint i) { return (i == 0) ? Int.toString (v) : (i == 1) ? name_ : (i == 2) ? desc_ : null; } void opAssign (int val) { v = val; foreach (cb; cngCb) cb(symb, val); } int opCall () { return v; } alias opCall opCast; int v; protected: char[] symb; void delegate (char[],int)[] cngCb; } /** Double content. */ class DoubleContent : ValueContent { /** Create a content with _symbol name symbol. */ this (char[] symbol, double val = 0) { symb = symbol; v = val; } /** Adds cb to the list of callback functions called when the value is changed. Returns this. */ DoubleContent addChangeCb (void delegate (char[] symbol,double value) cb) { cngCb ~= cb; return this; } /// Get the text. char[] toString (uint i) { return (i == 0) ? Float.toString (v) : (i == 1) ? name_ : (i == 2) ? desc_ : null; } void opAssign (double val) { v = val; foreach (cb; cngCb) cb(symb, val); } double opCall () { return v; } alias opCall opCast; double v; protected: char[] symb; void delegate (char[],double)[] cngCb; }