diff mde/gui/widget/Widget.d @ 95:2a364c7d82c9

Boolean options can be adjusted from the gui now (using a very basic widget). Also some bug-fixes. Fixed a minor bug where layouts with the same id but without shared alignments would be messed up. Tracked down the "nothing trawn until a resize" bug (see jobs.txt). If widgets throw during creation they're now replaced by debug widgets. Function pointers are converted to delegates using a safer method.
author Diggory Hardy <diggory.hardy@gmail.com>
date Thu, 06 Nov 2008 11:07:18 +0000
parents 08a4ae11454b
children dbf332403c6e
line wrap: on
line diff
--- a/mde/gui/widget/Widget.d	Thu Oct 23 17:45:49 2008 +0100
+++ b/mde/gui/widget/Widget.d	Thu Nov 06 11:07:18 2008 +0000
@@ -19,6 +19,9 @@
  * This module contains some base widget classes suitable for widget classes to inherit. However,
  * inheriting one of them is by no means necessary for a widget so long as the IWidget interface
  * is implemented.
+ * 
+ * Abstract widget classes have an 'A' prepended to the name, similar to the 'I' convention for
+ * interfaces.
  *************************************************************************************************/
 module mde.gui.widget.Widget;
 
@@ -41,11 +44,11 @@
  * useful basic implementation for widgets. Widgets need not inherit these (they only need
  * implement IWidget); they are simply provided for convenience and to promote code reuse.
  *************************************************************************************************/
-abstract class Widget : IChildWidget
+abstract class AWidget : IChildWidget
 {
 //BEGIN Load and save
     // Base this() for child Widgets.
-    this (IWidgetManager mgr, widgetID id, WidgetData) {
+    protected this (IWidgetManager mgr, widgetID id, WidgetData) {
         this.mgr = mgr;
         this.id = id;
     }
@@ -155,7 +158,7 @@
 /*************************************************************************************************
 * An abstract base widget class for parent widgets.
 *************************************************************************************************/
-abstract class ParentWidget : Widget
+abstract class AParentWidget : AWidget
 {
     this (IWidgetManager mgr, widgetID id, WidgetData data) {
         super (mgr, id, data);
@@ -177,7 +180,7 @@
 }
 
 /** A base for fixed-size widgets taking their size from the creation data. */
-class FixedWidget : Widget {
+class FixedWidget : AWidget {
     // Check data.length is at least 3 before calling!
     /** Constructor for a fixed-size [blank] widget.
      *
@@ -186,15 +189,13 @@
      * where w, h is the fixed size. */
     this (IWidgetManager mgr, widgetID id, WidgetData data) {
         super (mgr, id, data);
-        mw = cast(wdim) data.ints[1];
-        mh = cast(wdim) data.ints[2];
-        w = mw;
-        h = mh;
+        w = mw = cast(wdim) data.ints[1];
+        h = mh = cast(wdim) data.ints[2];
     }
 }
 
 /** A base for resizable widgets. */
-class SizableWidget : Widget {
+class SizableWidget : AWidget {
     // Check data.length is at least 1 before calling!
     /// Constructor for a completely resizable [blank] widget.
     this (IWidgetManager mgr, widgetID id, WidgetData data) {
@@ -204,3 +205,59 @@
     bool isWSizable () {    return true;    }
     bool isHSizable () {    return true;    }
 }
+
+/** For pressable buttons.
+ *
+ * Normally overriding classes implement this, draw and activated. */
+abstract class AButtonWidget : AWidget
+{
+    protected this (IWidgetManager mgr, widgetID id, WidgetData data) {
+        super (mgr, id, data);
+    }
+    
+    /// May be over-ridden. Pushed is true if the button has been pushed and not released.
+    void draw () {
+        mgr.renderer.drawButton (x,y, w,h, pushed);
+    }
+    
+    /// Handles the down-click
+    void clickEvent (wdabs, wdabs, ubyte b, bool state) {
+        if (b == 1 && state == true) {
+            pushed = true;
+            mgr.requestRedraw;
+            mgr.addClickCallback (&clickWhilePushed);
+            mgr.addMotionCallback (&motionWhilePushed);
+        }
+    }
+    
+    /// Called when a mouse click event occurs while held; handles up-click
+    bool clickWhilePushed (wdabs cx, wdabs cy, ubyte b, bool state) {
+        if (b == 1 && state == false) {
+            if (cx >= x && cx < x+w && cy >= y && cy < y+h) // button event
+                activated();
+            
+            pushed = false;
+            mgr.requestRedraw;
+            mgr.removeCallbacks (cast(void*) this);
+            
+            return true;
+        }
+        return false;
+    }
+    /// Called when a mouse motion event occurs while held; handles pushing in/out on hover
+    void motionWhilePushed (wdabs cx, wdabs cy) {
+        bool oldPushed = pushed;
+        if (cx >= x && cx < x+w && cy >= y && cy < y+h) pushed = true;
+        else pushed = false;
+        if (oldPushed != pushed)
+            mgr.requestRedraw;
+    }
+    
+    /// The action triggered when the button is clicked...
+    void activated ();
+    
+protected:
+    bool pushed = false;        /// True if button is pushed in (visually)
+}
+
+