Mercurial > projects > mde
comparison mde/gui/widget/Widget.d @ 34:6b4116e6355c
Work on the Gui: some of the framework for drag & drop. Also made Window an IWidget.
Implemented getWidget(x,y) to find the widget under this location for IWidgets (but not Gui).
Made Window an IWidget and made it work a little more similarly to widgets.
Implemented callbacks on the Gui for mouse events (enabling drag & drop, etc.).
committer: Diggory Hardy <diggory.hardy@gmail.com>
author | Diggory Hardy <diggory.hardy@gmail.com> |
---|---|
date | Fri, 02 May 2008 16:03:52 +0100 |
parents | 316b0230a849 |
children | 57d000574d75 |
comparison
equal
deleted
inserted
replaced
33:6886402c1545 | 34:6b4116e6355c |
---|---|
15 | 15 |
16 /// GUI Widget module. | 16 /// GUI Widget module. |
17 module mde.gui.widget.Widget; | 17 module mde.gui.widget.Widget; |
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.exception; | 21 import mde.gui.exception; |
21 | 22 |
22 import gl = mde.gl.basic; | 23 import gl = mde.gl.basic; |
23 | 24 |
24 import tango.io.Stdout; | 25 import tango.io.Stdout; |
28 * | 29 * |
29 * Do not use directly (i.e. only for inheriting from). | 30 * Do not use directly (i.e. only for inheriting from). |
30 */ | 31 */ |
31 class Widget : IWidget | 32 class Widget : IWidget |
32 { | 33 { |
33 /** Basic draw method: draw the background */ | |
34 void draw (int x, int y) { | |
35 window.renderer.drawWidgetBack (x,y, w,h); | |
36 } | |
37 | |
38 /** Dummy event method (ignore) */ | |
39 void clickEvent (ushort cx, ushort cy, ubyte b, bool state) {} | |
40 | |
41 /** Minimum size is zero. */ | 34 /** Minimum size is zero. */ |
42 void getMinimumSize (out int w, out int h) {} // w,h initialised to 0 | 35 void getMinimumSize (out int w, out int h) {} // w,h initialised to 0 |
43 /** Current size. */ | 36 /** Current size. */ |
44 void getCurrentSize (out int w, out int h) { | 37 void getCurrentSize (out int w, out int h) { |
45 w = this.w; | 38 w = this.w; |
46 h = this.h; | 39 h = this.h; |
47 } | 40 } |
48 | 41 |
42 void setPosition (int x, int y) { | |
43 this.x = x; | |
44 this.y = y; | |
45 } | |
46 | |
47 /** 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. */ | |
49 IWidget getWidget (int,int) { | |
50 return this; | |
51 } | |
52 | |
53 /** Dummy event method (widget doesn't respond to events) */ | |
54 void clickEvent (ushort cx, ushort cy, ubyte b, bool state) {} | |
55 | |
56 /** Basic draw method: draw the background (all widgets should do this) */ | |
57 void draw () { | |
58 window.renderer.drawWidgetBack (x,y, w,h); | |
59 } | |
60 | |
49 protected: | 61 protected: |
50 IWindow window; // the enclosing window | 62 IWindow window; // the enclosing window |
63 int x, y; // position | |
51 int w, h; // size | 64 int w, h; // size |
52 } | 65 } |
53 | 66 |
54 //BEGIN Widgets | 67 //BEGIN Widgets |
55 /// Draws a box. That's it. | 68 /// Draws a box. That's it. |
56 class BoxWidget : Widget | 69 class BoxWidget : Widget |
57 { | 70 { |
58 this (IWindow wind, IParentWidget, int[] data) { | 71 this (IWindow wind, IWidget, int[] data) { |
59 if (data.length != 2) throw new WidgetDataException; | 72 if (data.length != 2) throw new WidgetDataException; |
60 | 73 |
61 window = wind; | 74 window = wind; |
62 | 75 |
63 w = data[0]; | 76 w = data[0]; |
64 h = data[1]; | 77 h = data[1]; |
65 } | 78 } |
66 void draw (int x, int y) { | 79 void draw () { |
67 gl.setColor(1f,0f,0f); | 80 gl.setColor(1f,0f,0f); |
68 window.renderer.drawBox (x,y, w,h); | 81 window.renderer.drawBox (x,y, w,h); |
69 } | 82 } |
70 } | 83 } |
71 | 84 |
72 /// First interactible widget | 85 /// First interactible widget |
73 class ButtonWidget : Widget | 86 class ButtonWidget : Widget |
74 { | 87 { |
75 bool pushed = false;// true if button is pushed in | 88 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. | |
90 // it is whether the mouse is over the button after being clicked. | |
76 | 91 |
77 this (IWindow wind, IParentWidget, int[] data) { | 92 this (IWindow wind, IWidget, int[] data) { |
78 if (data.length != 2) throw new WidgetDataException; | 93 if (data.length != 2) throw new WidgetDataException; |
79 | 94 |
80 window = wind; | 95 window = wind; |
81 | 96 |
82 w = data[0]; | 97 w = data[0]; |
83 h = data[1]; | 98 h = data[1]; |
84 } | 99 } |
85 | 100 |
86 void draw (int x, int y) { | 101 void draw () { |
87 if (pushed) | 102 if (pushed) |
88 gl.setColor (1f, 0f, 1f); | 103 gl.setColor (1f, 0f, 1f); |
89 else | 104 else |
90 gl.setColor (.6f, 0f, .6f); | 105 gl.setColor (.6f, 0f, .6f); |
91 window.renderer.drawBox (x,y, w,h); | 106 window.renderer.drawBox (x,y, w,h); |
95 w = this.w; // button is not resizable | 110 w = this.w; // button is not resizable |
96 h = this.h; | 111 h = this.h; |
97 } | 112 } |
98 | 113 |
99 void clickEvent (ushort, ushort, ubyte b, bool state) { | 114 void clickEvent (ushort, ushort, ubyte b, bool state) { |
100 if (b == 1) pushed = state; // very basic | 115 if (b == 1 && state == true) { |
116 pushed = true; | |
117 window.gui.addClickCallback (&clickWhileHeld); | |
118 window.gui.addMotionCallback (&motionWhileHeld); | |
119 } | |
120 } | |
121 // Called when a mouse motion/click event occurs while (held == true) | |
122 void clickWhileHeld (ushort cx, ushort cy, ubyte b, bool state) { | |
123 if (cx >= x && cx < x+w && cy >= y && cy < y+h) // button event | |
124 Stdout ("Button clicked!").newline; | |
125 | |
126 pushed = false; | |
127 window.gui.removeCallbacks (cast(void*) this); | |
128 } | |
129 void motionWhileHeld (ushort cx, ushort cy) { | |
130 if (cx >= x && cx < x+w && cy >= y && cy < y+h) pushed = true; | |
131 else pushed = false; | |
101 } | 132 } |
102 } | 133 } |
103 //END Widgets | 134 //END Widgets |