diff mde/gui/widget/Widget.d @ 32:316b0230a849

Lots more work on the GUI. Also renamed lots of files. Lots of changes to the GUI. Renderer is now used exclusively for rendering and WidgetDecoration is gone. Renamed lots of files to conform to case policies. committer: Diggory Hardy <diggory.hardy@gmail.com>
author Diggory Hardy <diggory.hardy@gmail.com>
date Wed, 30 Apr 2008 18:05:56 +0100
parents
children 6b4116e6355c
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mde/gui/widget/Widget.d	Wed Apr 30 18:05:56 2008 +0100
@@ -0,0 +1,103 @@
+/* LICENSE BLOCK
+Part of mde: a Modular D game-oriented Engine
+Copyright © 2007-2008 Diggory Hardy
+
+This program is free software: you can redistribute it and/or modify it under the terms
+of the GNU General Public License as published by the Free Software Foundation, either
+version 2 of the License, or (at your option) any later version.
+
+This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+See the GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>. */
+
+/// GUI Widget module.
+module mde.gui.widget.Widget;
+
+public import mde.gui.widget.Ifaces;
+import mde.gui.exception;
+
+import gl = mde.gl.basic;
+
+import tango.io.Stdout;
+
+/** A base widget class. Widgets need not inherit this (they only need implement IWidget), but this
+* class provides a useful basic implementation for widgets.
+*
+* Do not use directly (i.e. only for inheriting from).
+*/
+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. */
+    void getCurrentSize (out int w, out int h) {
+        w = this.w;
+        h = this.h;
+    }
+    
+protected:
+    IWindow window;         // the enclosing window
+    int w, h;               // size
+}
+
+//BEGIN Widgets
+/// Draws a box. That's it.
+class BoxWidget : Widget
+{
+    this (IWindow wind, IParentWidget, int[] data) {
+        if (data.length != 2) throw new WidgetDataException;
+        
+        window = wind;
+        
+        w = data[0];
+        h = data[1];
+    }
+    void draw (int x, int y) {
+        gl.setColor(1f,0f,0f);
+        window.renderer.drawBox (x,y, w,h);
+    }
+}
+
+/// First interactible widget
+class ButtonWidget : Widget
+{
+    bool pushed = false;// true if button is pushed in
+    
+    this (IWindow wind, IParentWidget, int[] data) {
+        if (data.length != 2) throw new WidgetDataException;
+        
+        window = wind;
+        
+        w = data[0];
+        h = data[1];
+    }
+    
+    void draw (int x, int y) {
+        if (pushed)
+            gl.setColor (1f, 0f, 1f);
+        else
+            gl.setColor (.6f, 0f, .6f);
+        window.renderer.drawBox (x,y, w,h);
+    }
+    
+    void getMinimumSize (out int w, out int h) {
+        w = this.w; // button is not resizable
+        h = this.h;
+    }
+    
+    void clickEvent (ushort, ushort, ubyte b, bool state) {
+        if (b == 1) pushed = state; // very basic
+    }
+}
+//END Widgets