Mercurial > projects > mde
view mde/gui/content/Content.d @ 99:5de5810e3516
Implemented an editable TextContent widget; it's now possible to edit text options using the GUI.
The widget supports moving the text entry-point using arrows and home/end, but there's no visual indicator or edit-point setting using the mouse.
author | Diggory Hardy <diggory.hardy@gmail.com> |
---|---|
date | Fri, 14 Nov 2008 12:44:32 +0000 |
parents | 49e7cfed4b34 |
children | 0ea4a3e651ae |
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; import derelict.sdl.keysym; 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; pos = v.length; } /** 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; /** Acts on a keystroke and returns the new value. * * Supports one-line editing: left/right, home/end, backspace/delete. */ char[] keyStroke (ushort sym, char[] i) { debug assert (i.length, "TextContent.keyStroke: no value (??)"); // impossible? char k = *i; if (k > 0x20) { if (k == 0x7f) { // delete size_t p = pos; if (p < v.length) ++p; while (p < v.length && (v[p] & 0x80) && !(v[p] & 0x40)) ++p; v = v[0..pos] ~ v[p..$]; } else { // insert character char[] tail = v[pos..$]; v.length = v.length + i.length; size_t npos = pos+i.length; if (tail) v[npos..$] = tail.dup; // cannot assign with overlapping ranges v[pos..npos] = i; pos = npos; } } else { // use sym; many keys output 0 if (sym == SDLK_BACKSPACE) { // backspace; k == 0x8 char[] tail = v[pos..$]; if (pos) --pos; while (pos && (v[pos] & 0x80) && !(v[pos] & 0x40)) --pos; v = v[0..pos] ~ tail; } else if (sym == SDLK_LEFT) { if (pos) --pos; while (pos && (v[pos] & 0x80) && !(v[pos] & 0x40)) --pos; } else if (sym == SDLK_RIGHT) { if (pos < v.length) ++pos; while (pos < v.length && (v[pos] & 0x80) && !(v[pos] & 0x40)) ++pos; } else if (sym == SDLK_HOME || sym == SDLK_UP) { pos = 0; } else if (sym == SDLK_END || sym == SDLK_DOWN) { pos = v.length; } else debug logger.trace ("Symbol: {}", sym); } return v; } /// Gives all callbacks the modified value void endEdit () { foreach (cb; cngCb) cb(symb, v); } char[] v; protected: char[] symb; size_t pos; // editing position; used by keyStroke 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; }