comparison mde/gui/widget/Widget.d @ 37:052df9b2fe07

Allowed widget resizing, changed widget IDs and made Input catch any callback exceptions. Enabled widget resizing. Removed IRenderer's temporary drawBox method and added drawButton for ButtonWidget. Made the Widget class abstract and added FixedWidget and SizableWidget classes. Rewrote much of createWidget to use meta-code; changed widget IDs. Made Input catch callback exceptions and report error messages. committer: Diggory Hardy <diggory.hardy@gmail.com>
author Diggory Hardy <diggory.hardy@gmail.com>
date Mon, 05 May 2008 14:47:25 +0100
parents 57d000574d75
children 5132301e9ed7
comparison
equal deleted inserted replaced
36:57d000574d75 37:052df9b2fe07
18 18
19 public import mde.gui.widget.Ifaces; 19 public import mde.gui.widget.Ifaces;
20 import mde.gui.IGui; 20 import mde.gui.IGui;
21 import mde.gui.exception; 21 import mde.gui.exception;
22 22
23 import gl = mde.gl.basic;
24
25 import tango.io.Stdout; 23 import tango.io.Stdout;
26 24
27 /** A base widget class. Widgets need not inherit this (they only need implement IWidget), but this 25 /** An abstract base widget class.
28 * class provides a useful basic implementation for widgets.
29 * 26 *
30 * Do not use directly (i.e. only for inheriting from). 27 * This abstract class, and the more concrete FixedWidget and ScalableWidget classes provides a
31 */ 28 * useful basic implementation for widgets. Widgets need not inherit these (they only need implement
32 class Widget : IWidget 29 * IWidget); they are simply provided for convenience and to promote code reuse. */
30 abstract class Widget : IWidget
33 { 31 {
34 /** Minimum size is zero. */ 32 void getCurrentSize (out int cw, out int ch) {
35 void getMinimumSize (out int w, out int h) {} // w,h initialised to 0 33 cw = w;
36 /** Current size. */ 34 ch = h;
37 void getCurrentSize (out int w, out int h) {
38 w = this.w;
39 h = this.h;
40 } 35 }
41 36
42 void setPosition (int x, int y) { 37 void setPosition (int nx, int ny) {
43 this.x = x; 38 x = nx;
44 this.y = y; 39 y = ny;
45 } 40 }
46 41
47 /** Return self, since we don't have child widgets and the method wouldn't have been called 42 /* Return self, since we don't have child widgets and the method wouldn't have been called
48 * unless the location was over us. Valid for all widgets without children. */ 43 * unless the location was over us. Valid for all widgets without children. */
49 IWidget getWidget (int,int) { 44 IWidget getWidget (int,int) {
50 return this; 45 return this;
51 } 46 }
52 47
53 /** Dummy event method (widget doesn't respond to events) */ 48 /* Dummy event method (widget doesn't respond to events) */
54 void clickEvent (ushort cx, ushort cy, ubyte b, bool state) {} 49 void clickEvent (ushort cx, ushort cy, ubyte b, bool state) {}
55 50
56 /** Basic draw method: draw the background (all widgets should do this) */ 51 /* Basic draw method: draw the background (all widgets should do this) */
57 void draw () { 52 void draw () {
58 window.renderer.drawWidgetBack (x,y, w,h); 53 window.renderer.drawWidgetBack (x,y, w,h);
59 } 54 }
60 55
61 protected: 56 protected:
62 IWindow window; // the enclosing window 57 IWindow window; // the enclosing window
63 int x, y; // position 58 int x, y; // position
64 int w, h; // size 59 int w, h; // size
65 } 60 }
61 /** A base for fixed-size widgets. */
62 class FixedWidget : Widget {
63 bool isWSizable () { return false; }
64 bool isHSizable () { return false; }
65
66 /* Not resizable, so return current size. */
67 void getMinimalSize (out int mw, out int mh) {
68 mw = wF;
69 mh = hF;
70 }
71
72 /* Ignore: a fixed size widget. */
73 void setSize (int nw, int nh) {
74 w = (nw >= wF ? nw : wF);
75 h = (nh >= hF ? nh : hF);
76 }
77
78 protected:
79 int wF, hF; // The "fixed" size, i.e. the preferred & minimal size
80 }
81 /** A base for resizable widgets. */
82 class SizableWidget : Widget {
83 bool isWSizable () { return true; }
84 bool isHSizable () { return true; }
85
86 /* Return zero. */
87 void getMinimalSize (out int mw, out int mh) {}
88
89 /* Set size: a fully resizable widget. */
90 void setSize (int nw, int nh) {
91 w = (nw >= 0 ? nw : 0);
92 h = (nh >= 0 ? nh : 0);
93 }
94 }
66 95
67 //BEGIN Widgets 96 //BEGIN Widgets
68 /// Draws a box. That's it. 97 /// A fixed-size blank widget.
69 class BoxWidget : Widget 98 class FixedBlankWidget : FixedWidget
70 { 99 {
71 this (IWindow wind, IWidget, int[] data) { 100 this (IWindow wind, IWidget, int[] data) {
72 if (data.length != 2) throw new WidgetDataException; 101 if (data.length != 2) throw new WidgetDataException;
73 102
74 window = wind; 103 window = wind;
75 104
76 w = data[0]; 105 w = wF = data[0];
77 h = data[1]; 106 h = hF = data[1];
78 } 107 }
79 void draw () { 108 }
80 gl.setColor(1f,0f,0f); 109
81 window.renderer.drawBox (x,y, w,h); 110 /// A completely resizable blank widget (initial size zero).
111 class SizableBlankWidget : SizableWidget
112 {
113 this (IWindow wind, IWidget, int[] data) {
114 if (data.length != 0) throw new WidgetDataException;
115
116 window = wind;
82 } 117 }
83 } 118 }
84 119
85 /// First interactible widget 120 /// First interactible widget
86 class ButtonWidget : Widget 121 class ButtonWidget : FixedWidget
87 { 122 {
88 bool pushed = false; // true if button is pushed in (visually) 123 bool pushed = false; // true if button is pushed in (visually)
89 // pushed is not the same as the button being clicked but not yet released. 124 // pushed is not the same as the button being clicked but not yet released.
90 // it is whether the mouse is over the button after being clicked. 125 // it is whether the mouse is over the button after being clicked.
91 126
92 this (IWindow wind, IWidget, int[] data) { 127 this (IWindow wind, IWidget, int[] data) {
93 if (data.length != 2) throw new WidgetDataException; 128 if (data.length != 2) throw new WidgetDataException;
94 129
95 window = wind; 130 window = wind;
96 131
97 w = data[0]; 132 w = wF = data[0];
98 h = data[1]; 133 h = hF = data[1];
99 } 134 }
100 135
101 void draw () { 136 void draw () {
102 if (pushed) 137 window.renderer.drawButton (x,y, w,h, pushed);
103 gl.setColor (1f, 0f, 1f);
104 else
105 gl.setColor (.6f, 0f, .6f);
106 window.renderer.drawBox (x,y, w,h);
107 }
108
109 void getMinimumSize (out int w, out int h) {
110 w = this.w; // button is not resizable
111 h = this.h;
112 } 138 }
113 139
114 void clickEvent (ushort, ushort, ubyte b, bool state) { 140 void clickEvent (ushort, ushort, ubyte b, bool state) {
115 if (b == 1 && state == true) { 141 if (b == 1 && state == true) {
116 pushed = true; 142 pushed = true;