comparison 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
comparison
equal deleted inserted replaced
31:baa87e68d7dc 32:316b0230a849
1 /* LICENSE BLOCK
2 Part of mde: a Modular D game-oriented Engine
3 Copyright © 2007-2008 Diggory Hardy
4
5 This program is free software: you can redistribute it and/or modify it under the terms
6 of the GNU General Public License as published by the Free Software Foundation, either
7 version 2 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 See the GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program. If not, see <http://www.gnu.org/licenses/>. */
15
16 /// Gui layout widgets.
17 module mde.gui.widget.layout;
18
19 import mde.gui.widget.Widget;
20 import mde.gui.exception : WidgetDataException;
21
22 /// Encapsulates a grid of Widgets
23 class GridWidget : Widget
24 {
25 this (IWindow wind, IParentWidget, int[] data) {
26 // Get grid size
27 if (data.length < 2) throw new WidgetDataException;
28 rows = data[0];
29 cols = data[1];
30
31 window = wind;
32
33 // Get all sub-widgets
34 // Check: correct data length and rows*cols >= 0 (know data.length - 2 >= 0).
35 if (data.length != 2 + rows * cols) throw new WidgetDataException;
36 subWidgets.length = rows*cols;
37 foreach (i, inout subWidget; subWidgets) {
38 subWidget = window.getWidget (data[i+2], this);
39 }
40
41 getMinimumSize (w,h); // Calculate the size (current size is not saved)
42 }
43
44 // Calculates from all rows and columns of widgets.
45 void getMinimumSize (out int w, out int h) {
46 if (rows*cols == 0) { // special case
47 w = h = 0;
48 return;
49 }
50
51 // Find the sizes of all subWidgets
52 int[] widgetW = new int[subWidgets.length]; // dimensions
53 int[] widgetH = new int[subWidgets.length];
54 foreach (i,widget; subWidgets) widget.getCurrentSize (widgetW[i],widgetH[i]);
55
56 // Find row heights and column widths (non cumulative)
57 rowH.length = rows;
58 colW.length = cols; //WARNING: code reliant on these being initialised to zero
59 for (uint i = 0; i < subWidgets.length; ++i) {
60 uint x = i / cols; // row
61 if (rowH[x] < widgetH[i]) rowH[x] = widgetH[i];
62 x = i % cols; // column
63 if (colW[x] < widgetW[i]) colW[x] = widgetW[i];
64 }
65
66 // rowY / colX
67 rowY.length = rows;
68 colX.length = cols;
69 int spacing = window.renderer.layoutSpacing;
70
71 int cum = 0;
72 foreach (i, x; rowH) {
73 rowY[i] = cum;
74 cum += x + spacing;
75 }
76 h = cum - spacing; // total height
77 cum = 0;
78 foreach (i, x; colW) {
79 colX[i] = cum;
80 cum += x + spacing;
81 }
82 w = cum - spacing; // total width
83 }
84
85 void draw (int x, int y) {
86 super.draw (x,y);
87
88 foreach (i,widget; subWidgets) {
89 widget.draw (x + colX[i % cols], y + rowY[i / cols]);
90 }
91 }
92
93 // Pass event on to relevant widget. Simply return if not on a widget.
94 void clickEvent (ushort cx, ushort cy, ubyte b, bool state) {
95 if (rows*cols == 0) return; // special case
96
97 // Find the column
98 int i = cols - 1; // starting from right...
99 while (cx < colX[i]) { // decrement while left of this column
100 if (i == 0) return; // left of first column
101 --i;
102 } // now (cx >= colX[i])
103 if (cx >= colX[i] + colW[i]) return; // between columns
104
105 // Find the row;
106 int j = rows - 1;
107 while (cy < rowY[j]) {
108 if (j == 0) return;
109 --j;
110 }
111 if (cy >= rowY[j] + rowH[j]) return;
112
113 // Now we know it's in widget (i,j)'s cell (but the widget may not take up the whole cell)
114 cx -= colX[i];
115 cy -= rowY[j];
116 IWidget widg = subWidgets[i + j*cols];
117 widg.getCurrentSize (i,j);
118 if (cx < i && cy < j)
119 widg.clickEvent (cx, cy, b, state);
120 }
121
122 protected:
123 int rows, cols; // number of cells in grid
124 int[] rowH; // row height (highest widget in the row)
125 int[] colW; // column width (widest widget)
126 int[] rowY; // cumulative rowH[i-1] + border and padding
127 int[] colX; // cumulative colW[i-1] + border and padding
128 IWidget[] subWidgets; // all widgets in the grid (by row):
129 /* SubWidget order: [ 2 3 ]
130 * [ 0 1 ] */
131 }