annotate mde/gui/widget/TextWidget.d @ 65:891211f034f2

Changes to widgets: widgets may now get strings as creation data. Strings for TextWidgets can be set in files (in a temporary mannor).
author Diggory Hardy <diggory.hardy@gmail.com>
date Sun, 29 Jun 2008 15:40:37 +0100
parents 66d555da083e
children 159775502bb4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
59
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
1 /* LICENSE BLOCK
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
2 Part of mde: a Modular D game-oriented Engine
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
3 Copyright © 2007-2008 Diggory Hardy
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
4
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
5 This program is free software: you can redistribute it and/or modify it under the terms
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
6 of the GNU General Public License as published by the Free Software Foundation, either
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
7 version 2 of the License, or (at your option) any later version.
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
8
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
10 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
11 See the GNU General Public License for more details.
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
12
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
13 You should have received a copy of the GNU General Public License
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
14 along with this program. If not, see <http://www.gnu.org/licenses/>. */
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
15
62
960206198cbd Documentation changes only. (Idea for new gui.content.Items module; unused gl.texture module commented out.)
Diggory Hardy <diggory.hardy@gmail.com>
parents: 61
diff changeset
16 /** Basic text widget and supporting code for widgets containing text.
960206198cbd Documentation changes only. (Idea for new gui.content.Items module; unused gl.texture module commented out.)
Diggory Hardy <diggory.hardy@gmail.com>
parents: 61
diff changeset
17 *
960206198cbd Documentation changes only. (Idea for new gui.content.Items module; unused gl.texture module commented out.)
Diggory Hardy <diggory.hardy@gmail.com>
parents: 61
diff changeset
18 * All content widgets have one (at least for basic content widgets) Content, from
960206198cbd Documentation changes only. (Idea for new gui.content.Items module; unused gl.texture module commented out.)
Diggory Hardy <diggory.hardy@gmail.com>
parents: 61
diff changeset
19 * mde.gui.content.Items . */
59
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
20 module mde.gui.widget.TextWidget;
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
21
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
22 import mde.gui.widget.Widget;
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
23 import mde.gui.exception;
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
24 import mde.gui.renderer.IRenderer;
61
7cab2af4ba21 A little bit of progress on the content handling system (relevent code is likely to be revised).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 59
diff changeset
25 import mde.gui.content.Content;
59
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
26
63
66d555da083e Moved many modules/packages to better reflect usage.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 62
diff changeset
27 import mde.font.font;
59
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
28
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
29 import tango.io.Stdout;
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
30
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
31 /// Adapter to ease use of ContentText
61
7cab2af4ba21 A little bit of progress on the content handling system (relevent code is likely to be revised).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 59
diff changeset
32 struct ContentAdapter(ContentT : IContent) {
65
891211f034f2 Changes to widgets: widgets may now get strings as creation data. Strings for TextWidgets can be set in files (in a temporary mannor).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 63
diff changeset
33 void set (char[] cID, int col) {
59
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
34 if (font is null) font = FontStyle.get("default");
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
35
65
891211f034f2 Changes to widgets: widgets may now get strings as creation data. Strings for TextWidgets can be set in files (in a temporary mannor).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 63
diff changeset
36 static if (is(ContentT == ContentText)) {
891211f034f2 Changes to widgets: widgets may now get strings as creation data. Strings for TextWidgets can be set in files (in a temporary mannor).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 63
diff changeset
37 content = getContentText (cID);
891211f034f2 Changes to widgets: widgets may now get strings as creation data. Strings for TextWidgets can be set in files (in a temporary mannor).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 63
diff changeset
38 } else static if (is(ContentT == ContentInt)) {
891211f034f2 Changes to widgets: widgets may now get strings as creation data. Strings for TextWidgets can be set in files (in a temporary mannor).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 63
diff changeset
39 content = getContentInt (cID);
891211f034f2 Changes to widgets: widgets may now get strings as creation data. Strings for TextWidgets can be set in files (in a temporary mannor).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 63
diff changeset
40 } else static assert (false, "Unsupported content type");
59
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
41 colour = Colour (cast(ubyte) (col >> 16u),
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
42 cast(ubyte) (col >> 8u),
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
43 cast(ubyte) col );
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
44 }
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
45
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
46 void getDimensions (out wdsize w, out wdsize h) {
61
7cab2af4ba21 A little bit of progress on the content handling system (relevent code is likely to be revised).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 59
diff changeset
47 font.updateBlock (content.toString, textCache);
59
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
48 w = cast(wdim) textCache.w;
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
49 h = cast(wdim) textCache.h;
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
50 }
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
51
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
52 void draw (wdabs x, wdabs y) {
61
7cab2af4ba21 A little bit of progress on the content handling system (relevent code is likely to be revised).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 59
diff changeset
53 font.textBlock (x,y, content.toString, textCache, colour);
59
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
54 }
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
55
61
7cab2af4ba21 A little bit of progress on the content handling system (relevent code is likely to be revised).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 59
diff changeset
56 ContentT content;
59
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
57 TextBlock textCache;
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
58 Colour colour;
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
59 static FontStyle font;
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
60 }
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
61
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
62 /// Basic text widget
61
7cab2af4ba21 A little bit of progress on the content handling system (relevent code is likely to be revised).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 59
diff changeset
63 class ContentWidget(ContentT : IContent) : Widget
59
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
64 {
65
891211f034f2 Changes to widgets: widgets may now get strings as creation data. Strings for TextWidgets can be set in files (in a temporary mannor).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 63
diff changeset
65 /** Constructor for a widget containing [fixed] content.
891211f034f2 Changes to widgets: widgets may now get strings as creation data. Strings for TextWidgets can be set in files (in a temporary mannor).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 63
diff changeset
66 *
891211f034f2 Changes to widgets: widgets may now get strings as creation data. Strings for TextWidgets can be set in files (in a temporary mannor).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 63
diff changeset
67 * Widget uses the initialisation data:
891211f034f2 Changes to widgets: widgets may now get strings as creation data. Strings for TextWidgets can be set in files (in a temporary mannor).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 63
diff changeset
68 * [widgetID, contentID, colour]
891211f034f2 Changes to widgets: widgets may now get strings as creation data. Strings for TextWidgets can be set in files (in a temporary mannor).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 63
diff changeset
69 * where contentID is an ID for the string ID of the contained content
891211f034f2 Changes to widgets: widgets may now get strings as creation data. Strings for TextWidgets can be set in files (in a temporary mannor).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 63
diff changeset
70 * and colour is an 8-bit-per-channel RGB colour of the form 0xRRGGBB. */
59
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
71 this (IWindow wind, int[] data) {
65
891211f034f2 Changes to widgets: widgets may now get strings as creation data. Strings for TextWidgets can be set in files (in a temporary mannor).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 63
diff changeset
72 if (data.length != 3) throw new WidgetDataException;
891211f034f2 Changes to widgets: widgets may now get strings as creation data. Strings for TextWidgets can be set in files (in a temporary mannor).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 63
diff changeset
73 text.set (wind.getWidgetString(data[1]), data[2]);
59
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
74 text.getDimensions (mw, mh);
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
75 super (wind,data);
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
76 }
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
77
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
78 void draw () {
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
79 super.draw();
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
80 text.draw (x,y);
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
81 }
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
82
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
83 protected:
61
7cab2af4ba21 A little bit of progress on the content handling system (relevent code is likely to be revised).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 59
diff changeset
84 ContentAdapter!(ContentT) text;
59
672b6b162a36 Added very basic (and currently useless) content support.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
85 }
61
7cab2af4ba21 A little bit of progress on the content handling system (relevent code is likely to be revised).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 59
diff changeset
86
7cab2af4ba21 A little bit of progress on the content handling system (relevent code is likely to be revised).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 59
diff changeset
87 alias ContentWidget!(ContentText) TextWidget;
7cab2af4ba21 A little bit of progress on the content handling system (relevent code is likely to be revised).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 59
diff changeset
88 alias ContentWidget!(ContentInt) IntWidget;