changeset 61:7cab2af4ba21

A little bit of progress on the content handling system (relevent code is likely to be revised). Also some changes to debug statements, in particular a new drawGlyphCache debug version.
author Diggory Hardy <diggory.hardy@gmail.com>
date Wed, 25 Jun 2008 17:25:48 +0100
parents 23a1d2b1ec5f
children 960206198cbd
files codeDoc/jobs.txt data/conf/gui.mtt mde/events.d mde/gl/draw.d mde/gui/content/Content.d mde/gui/content/ContentText.d mde/gui/content/Services.d mde/gui/widget/TextWidget.d mde/gui/widget/createWidget.d mde/input/Input.d mde/resource/FontTexture.d mde/resource/font.d
diffstat 12 files changed, 211 insertions(+), 56 deletions(-) [+]
line wrap: on
line diff
--- a/codeDoc/jobs.txt	Tue Jun 24 20:40:52 2008 +0100
+++ b/codeDoc/jobs.txt	Wed Jun 25 17:25:48 2008 +0100
@@ -3,6 +3,7 @@
 
 
 In progress:
+GUI Content.
 
 
 
--- a/data/conf/gui.mtt	Tue Jun 24 20:40:52 2008 +0100
+++ b/data/conf/gui.mtt	Wed Jun 25 17:25:48 2008 +0100
@@ -7,7 +7,7 @@
 {W2}
 <int|x=150>
 <int|y=200>
-<int[][int]|widgetData=[0:[0xB004,5,5,2,1,2,1,2,1,1,1,1,1,2,1,2,1,2,1,1,1,1,1,2,1,2,1,2],1:[0x3001],2:[0x2,0xFFFF00]]>
+<int[][int]|widgetData=[0:[0xB004,5,5,2,1,22,1,2,1,1,1,1,1,2,1,22,1,2,1,1,1,1,1,2,1,22,1,2],1:[0x3001],2:[0x21,0xFFFF00],22:[0x22,0xFF00]]>
 {WEmbedded}
 <int|x=20>
 <int|y=100>
--- a/mde/events.d	Tue Jun 24 20:40:52 2008 +0100
+++ b/mde/events.d	Wed Jun 25 17:25:48 2008 +0100
@@ -25,6 +25,7 @@
 
 import tango.time.Time;
 import tango.util.log.Log : Log, Logger;
+import tango.io.Stdout;
 
 private Logger logger;
 static this() {
@@ -49,7 +50,8 @@
                 break;
             default:
                 try {
-                    imde.input (event);
+                    if (!imde.input (event))
+                        debug Stdout ("Unrecognised event with code ")(event.type).newline;
                 } catch (Exception e) {
                     logger.error ("Caught input exception; event will be ignored. Exception was:");
                     logger.error (e.msg);
--- a/mde/gl/draw.d	Tue Jun 24 20:40:52 2008 +0100
+++ b/mde/gl/draw.d	Wed Jun 25 17:25:48 2008 +0100
@@ -38,7 +38,7 @@
     glClear(GL_COLOR_BUFFER_BIT);
     
     gui.draw ();
-    debug FontStyle.drawTexture;
+    debug (drawGlyphCache) FontStyle.drawTexture;
     
     GLenum err = glGetError();
     while (err != GL_NO_ERROR) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mde/gui/content/Content.d	Wed Jun 25 17:25:48 2008 +0100
@@ -0,0 +1,126 @@
+/* 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 − type agnostic part.
+ */
+module mde.gui.content.Content;
+
+import Int = tango.text.convert.Integer;
+
+/** Content − universal part.
+ *
+ * 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().
+ */
+// Don't include dimension/drawing stuff because it's renderer specific and content should not be!
+// NOTE: an interface or a class?
+alias IContent Content;	// use either name until it's settled...
+/** ditto */
+interface IContent
+{
+    /** 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
+    
+    /** Every Content should be convertible to a string, which, if possible, should be a sensible
+     * conversion of its content. */
+    char[] toString ();
+}
+
+/** Text content. */
+/* May end up extending a universal content type.
+ *  Services like copy/paste could work on universal content.
+ *
+ * NOTE: Needs to be a reference type really.
+ *  Could alternately be:
+ *      alias ContentTextStruct* ContentText
+ *  where ContentTextStruct is a struct. */
+class ContentText : Content
+{
+    this () {
+        // NOTE: testing
+        text_ = "\"a@b\" − an example";
+    }
+    this (char[] text) {
+        text_ = text;
+    }
+    
+    ContentText dup () {
+        return new ContentText (text_);
+    }
+    
+    ContentText toText () {
+        return this;
+    }
+    ContentInt  toInt () {
+        // FIXME: convert
+        return null;
+    }
+    
+    alias toString text;
+    
+    /// Get the text.
+    char[] toString () {
+        return text_;
+    }
+    
+protected:
+    //NOTE: need to allow cache-invalidating when text changes!
+    char[] text_;
+}
+
+/** Integer content. */
+class ContentInt : Content
+{
+    this () {
+        // NOTE: testing
+        int_ = -496;
+    }
+    this (int integer) {
+        int_ = integer;
+    }
+    
+    ContentInt dup () {
+        return new ContentInt (int_);
+    }
+    
+    ContentText toText () {
+        return new ContentText(toString);
+    }
+    ContentInt  toInt () {
+        return this;
+    }
+    
+    /// Get the integer.
+    int integer () {
+        return int_;
+    }
+    
+    char[] toString () {
+        return Int.toString (int_);
+    }
+    
+protected:
+    int int_;
+}
--- a/mde/gui/content/ContentText.d	Tue Jun 24 20:40:52 2008 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,39 +0,0 @@
-/* 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 − text.
- */
-module mde.gui.content.ContentText;
-
-/** Text content. */
-/* May end up extending a universal content type.
- *  Services like copy/paste could work on universal content.
- *
- * NOTE: Needs to be a reference type really.
- *  Could alternately be:
- *      alias ContentTextStruct* ContentText
- *  where ContentTextStruct is a struct. */
-class ContentText /+ : Content +/
-{
-    
-    /// Get the text.
-    char[] text () {
-        return text_;
-    }
-    
-protected:
-    //NOTE: need to allow cache-invalidating when text changes!
-    const char[] text_ = "\"a@b\" − an example";
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mde/gui/content/Services.d	Wed Jun 25 17:25:48 2008 +0100
@@ -0,0 +1,57 @@
+/* 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/>. */
+
+// NOTE: This module is currently unused and untested.
+// FIXME: write unittests
+
+/** Content services.
+ *
+ * Services act on a content, potentially modifying it. They have the form:
+ * ---
+ * bool service (ref Content c);
+ * ---
+ * where c is the content acted on, and the return value is true if the content was changed (useful
+ * to know whether caches need updating or not).
+ */
+module mde.gui.content.Services;
+
+bool copy (ref Content c) {
+    // Put a duplicate on the clipboard, so later changes don't affect what's on the clipboard.
+    clipboard = c.dup;
+    return false;
+}
+
+bool paste (ref ContentText c) {
+    if (clipboard is null)
+        return false;	// no item on clipboard, so don't do anything
+    c = clipboard.toText;
+    return true;
+}
+
+bool paste (ref ContentInt c) {
+    if (clipboard is null)
+        return false;	// no item on clipboard, so don't do anything
+    c = clipboard.toInt;
+    return true;
+}
+
+bool link (ref ContentText c) {
+    if (clipboard is null)
+        return false;	// no item on clipboard, so don't do anything
+    c = clipboard;
+    return true;
+}
+
+Content clipboard;
--- a/mde/gui/widget/TextWidget.d	Tue Jun 24 20:40:52 2008 +0100
+++ b/mde/gui/widget/TextWidget.d	Wed Jun 25 17:25:48 2008 +0100
@@ -19,41 +19,41 @@
 import mde.gui.widget.Widget;
 import mde.gui.exception;
 import mde.gui.renderer.IRenderer;
-import mde.gui.content.ContentText;
+import mde.gui.content.Content;
 
 import mde.resource.font;
 
 import tango.io.Stdout;
 
 /// Adapter to ease use of ContentText
-struct AdapterText {
+struct ContentAdapter(ContentT : IContent) {
     void set (int col) {
         if (font is null) font = FontStyle.get("default");
         
-        content = new ContentText;
+        content = new ContentT;
         colour = Colour (cast(ubyte) (col >> 16u),
                          cast(ubyte) (col >> 8u),
                          cast(ubyte) col );
     }
     
     void getDimensions (out wdsize w, out wdsize h) {
-        font.updateBlock (content.text, textCache);
+        font.updateBlock (content.toString, textCache);
         w = cast(wdim) textCache.w;
         h = cast(wdim) textCache.h;
     }
     
     void draw (wdabs x, wdabs y) {
-        font.textBlock (x,y, content.text, textCache, colour);
+        font.textBlock (x,y, content.toString, textCache, colour);
     }
     
-    ContentText content;
+    ContentT content;
     TextBlock textCache;
     Colour colour;
     static FontStyle font;
 }
 
 /// Basic text widget
-class TextWidget : Widget
+class ContentWidget(ContentT : IContent) : Widget
 {
     this (IWindow wind, int[] data) {
         if (data.length != 2) throw new WidgetDataException;
@@ -68,5 +68,8 @@
     }
     
 protected:
-    AdapterText text;
+    ContentAdapter!(ContentT) text;
 }
+
+alias ContentWidget!(ContentText) TextWidget;
+alias ContentWidget!(ContentInt) IntWidget;
--- a/mde/gui/widget/createWidget.d	Tue Jun 24 20:40:52 2008 +0100
+++ b/mde/gui/widget/createWidget.d	Wed Jun 25 17:25:48 2008 +0100
@@ -62,12 +62,13 @@
     FixedBlank              = 0x1,
     SizableBlank            = WSIZABLE | HSIZABLE | 0x1,
     
-    // text: 0x2
-    Text		    = 0x2,
-    
     // buttons: 0x10
     Button                  = INTERACTIBLE | 0x10,
     
+    // content: 0x20
+    Text		    = 0x21,
+    Int			    = 0x22,
+    
     GridLayout              = LAYOUT | WSIZABLE | HSIZABLE | 0x4
 }
 
@@ -77,6 +78,7 @@
 const char[][] WIDGETS = [
         "FixedBlank",
         "Text",
+        "Int",
         "SizableBlank",
         "Button",
         "GridLayout"   ];
--- a/mde/input/Input.d	Tue Jun 24 20:40:52 2008 +0100
+++ b/mde/input/Input.d	Wed Jun 25 17:25:48 2008 +0100
@@ -181,6 +181,8 @@
     *
     * Other types of event functions may be added. Returns true if the event was used, false if not
     * or no config was available. Hmm... doesn't seem very useful, but has practically no cost.
+    * (Due to lack of use of this feature, false is returned even for used events when no config is
+    * available).
     *
     * May throw InputClassExceptions (on configuration errors). Catching the exception and continuing should
     * be fine. */
--- a/mde/resource/FontTexture.d	Tue Jun 24 20:40:52 2008 +0100
+++ b/mde/resource/FontTexture.d	Wed Jun 25 17:25:48 2008 +0100
@@ -324,7 +324,8 @@
         cachedGlyphs[chr] = ga;
     }
     
-    void drawTexture () {	// temp func
+    // Draw the first glyph cache texture in the upper-left corner of the screen.
+    debug (drawGlyphCache) void drawTexture () {
         if (tex.length == 0) return;
         glEnable (GL_TEXTURE_2D);
         glBindTexture(GL_TEXTURE_2D, tex[0].texID);
@@ -365,7 +366,7 @@
         p.texID = 0;
         
         // add a pretty background to the texture
-        static if (true) {
+        debug (drawGlyphCache) {
             glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
             glPixelStorei (GL_UNPACK_ROW_LENGTH, 0);
             ubyte[3][dimH][dimW] testTex;
--- a/mde/resource/font.d	Tue Jun 24 20:40:52 2008 +0100
+++ b/mde/resource/font.d	Wed Jun 25 17:25:48 2008 +0100
@@ -51,7 +51,7 @@
 {
     //BEGIN Static: manager
     static {
-        debug void drawTexture() {
+        debug (drawGlyphCache) void drawTexture() {
             if (fontTex !is null)
                 fontTex.drawTexture;
         }