diff 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
line wrap: on
line diff
--- a/mde/gui/widget/Widget.d	Thu May 01 10:55:04 2008 +0100
+++ b/mde/gui/widget/Widget.d	Fri May 02 16:03:52 2008 +0100
@@ -17,6 +17,7 @@
 module mde.gui.widget.Widget;
 
 public import mde.gui.widget.Ifaces;
+import mde.gui.IGui;
 import mde.gui.exception;
 
 import gl = mde.gl.basic;
@@ -30,14 +31,6 @@
 */
 class Widget : IWidget
 {
-    /** Basic draw method: draw the background */
-    void draw (int x, int y) {
-        window.renderer.drawWidgetBack (x,y, w,h);
-    }
-    
-    /** Dummy event method (ignore) */
-    void clickEvent (ushort cx, ushort cy, ubyte b, bool state) {}
-    
     /** Minimum size is zero. */
     void getMinimumSize (out int w, out int h) {}   // w,h initialised to 0
     /** Current size. */
@@ -46,8 +39,28 @@
         h = this.h;
     }
     
+    void setPosition (int x, int y) {
+        this.x = x;
+        this.y = y;
+    }
+    
+    /** Return self, since we don't have child widgets and the method wouldn't have been called
+    * unless the location was over us. Valid for all widgets without children. */
+    IWidget getWidget (int,int) {
+        return this;
+    }
+    
+    /** Dummy event method (widget doesn't respond to events) */
+    void clickEvent (ushort cx, ushort cy, ubyte b, bool state) {}
+    
+    /** Basic draw method: draw the background (all widgets should do this) */
+    void draw () {
+        window.renderer.drawWidgetBack (x,y, w,h);
+    }
+    
 protected:
     IWindow window;         // the enclosing window
+    int x, y;               // position
     int w, h;               // size
 }
 
@@ -55,7 +68,7 @@
 /// Draws a box. That's it.
 class BoxWidget : Widget
 {
-    this (IWindow wind, IParentWidget, int[] data) {
+    this (IWindow wind, IWidget, int[] data) {
         if (data.length != 2) throw new WidgetDataException;
         
         window = wind;
@@ -63,7 +76,7 @@
         w = data[0];
         h = data[1];
     }
-    void draw (int x, int y) {
+    void draw () {
         gl.setColor(1f,0f,0f);
         window.renderer.drawBox (x,y, w,h);
     }
@@ -72,9 +85,11 @@
 /// First interactible widget
 class ButtonWidget : Widget
 {
-    bool pushed = false;// true if button is pushed in
+    bool pushed = false;    // true if button is pushed in (visually)
+    // pushed is not the same as the button being clicked but not yet released.
+    // it is whether the mouse is over the button after being clicked.
     
-    this (IWindow wind, IParentWidget, int[] data) {
+    this (IWindow wind, IWidget, int[] data) {
         if (data.length != 2) throw new WidgetDataException;
         
         window = wind;
@@ -83,7 +98,7 @@
         h = data[1];
     }
     
-    void draw (int x, int y) {
+    void draw () {
         if (pushed)
             gl.setColor (1f, 0f, 1f);
         else
@@ -97,7 +112,23 @@
     }
     
     void clickEvent (ushort, ushort, ubyte b, bool state) {
-        if (b == 1) pushed = state; // very basic
+        if (b == 1 && state == true) {
+            pushed = true;
+            window.gui.addClickCallback (&clickWhileHeld);
+            window.gui.addMotionCallback (&motionWhileHeld);
+        }
+    }
+    // Called when a mouse motion/click event occurs while (held == true)
+    void clickWhileHeld (ushort cx, ushort cy, ubyte b, bool state) {
+        if (cx >= x && cx < x+w && cy >= y && cy < y+h) // button event
+            Stdout ("Button clicked!").newline;
+        
+        pushed = false;
+        window.gui.removeCallbacks (cast(void*) this);
+    }
+    void motionWhileHeld (ushort cx, ushort cy) {
+        if (cx >= x && cx < x+w && cy >= y && cy < y+h) pushed = true;
+        else pushed = false;
     }
 }
 //END Widgets