diff mde/gui/widget/layout.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/layout.d	Wed Apr 30 18:05:56 2008 +0100
@@ -0,0 +1,131 @@
+/* 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 layout widgets.
+module mde.gui.widget.layout;
+
+import mde.gui.widget.Widget;
+import mde.gui.exception : WidgetDataException;
+
+/// Encapsulates a grid of Widgets
+class GridWidget : Widget
+{
+    this (IWindow wind, IParentWidget, int[] data) {
+        // Get grid size
+        if (data.length < 2) throw new WidgetDataException;
+        rows = data[0];
+        cols = data[1];
+        
+        window = wind;
+        
+        // Get all sub-widgets
+        // Check: correct data length and rows*cols >= 0 (know data.length - 2 >= 0).
+        if (data.length != 2 + rows * cols) throw new WidgetDataException;
+        subWidgets.length = rows*cols;
+        foreach (i, inout subWidget; subWidgets) {
+            subWidget = window.getWidget (data[i+2], this);
+        }
+        
+        getMinimumSize (w,h);   // Calculate the size (current size is not saved)
+    }
+    
+    // Calculates from all rows and columns of widgets.
+    void getMinimumSize (out int w, out int h) {
+        if (rows*cols == 0) {    // special case
+            w = h = 0;
+            return;
+        }
+        
+        // Find the sizes of all subWidgets
+        int[] widgetW = new int[subWidgets.length]; // dimensions
+        int[] widgetH = new int[subWidgets.length];
+        foreach (i,widget; subWidgets) widget.getCurrentSize (widgetW[i],widgetH[i]);
+        
+        // Find row heights and column widths (non cumulative)
+        rowH.length = rows;
+        colW.length = cols; //WARNING: code reliant on these being initialised to zero
+        for (uint i = 0; i < subWidgets.length; ++i) {
+            uint x = i / cols;  // row
+            if (rowH[x] < widgetH[i]) rowH[x] = widgetH[i];
+            x = i % cols;       // column
+            if (colW[x] < widgetW[i]) colW[x] = widgetW[i];
+        }
+        
+        // rowY / colX
+        rowY.length = rows;
+        colX.length = cols;
+        int spacing = window.renderer.layoutSpacing;
+        
+        int cum = 0;
+        foreach (i, x; rowH) {
+            rowY[i] = cum;
+            cum += x + spacing;
+        }
+        h = cum - spacing;      // total height
+        cum = 0;
+        foreach (i, x; colW) {
+            colX[i] = cum;
+            cum += x + spacing;
+        }
+        w = cum - spacing;      // total width
+    }
+    
+    void draw (int x, int y) {
+        super.draw (x,y);
+        
+        foreach (i,widget; subWidgets) {
+            widget.draw (x + colX[i % cols], y + rowY[i / cols]);
+        }
+    }
+    
+    // Pass event on to relevant widget. Simply return if not on a widget.
+    void clickEvent (ushort cx, ushort cy, ubyte b, bool state) {
+        if (rows*cols == 0) return; // special case
+        
+        // Find the column
+        int i = cols - 1;          // starting from right...
+        while (cx < colX[i]) {      // decrement while left of this column
+            if (i == 0) return;     // left of first column
+            --i;
+        }                           // now (cx >= colX[i])
+        if (cx >= colX[i] + colW[i]) return;    // between columns
+        
+        // Find the row;
+        int j = rows - 1;
+        while (cy < rowY[j]) {
+            if (j == 0) return;
+            --j;
+        }
+        if (cy >= rowY[j] + rowH[j]) return;
+        
+        // Now we know it's in widget (i,j)'s cell (but the widget may not take up the whole cell)
+        cx -= colX[i];
+        cy -= rowY[j];
+        IWidget widg = subWidgets[i + j*cols];
+        widg.getCurrentSize (i,j);
+        if (cx < i && cy < j)
+            widg.clickEvent (cx, cy, b, state);
+    }
+    
+protected:
+    int rows, cols;     // number of cells in grid
+    int[] rowH;         // row height (highest widget in the row)
+    int[] colW;         // column width (widest widget)
+    int[] rowY;         // cumulative rowH[i-1] + border and padding
+    int[] colX;         // cumulative colW[i-1] + border and padding
+    IWidget[] subWidgets;   // all widgets in the grid (by row):
+    /* SubWidget order:    [ 2 3 ]
+    *                      [ 0 1 ] */
+}