aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Hive/HexGrid.cs
blob: af4598de0d9ec957d373857005fb345d3b5a2a83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
using System;
using System.Windows;
using System.Windows.Controls;

namespace Tango.Hive
{
    /// <summary>
    /// Hexagonal panel
    /// </summary>
    public class HexGrid: Panel
    {
        #region Orientation
        public static readonly DependencyProperty OrientationProperty =
            DependencyProperty.RegisterAttached("Orientation", typeof(Orientation), typeof(HexGrid),
                new FrameworkPropertyMetadata(Orientation.Horizontal,
                    FrameworkPropertyMetadataOptions.AffectsMeasure | 
                    FrameworkPropertyMetadataOptions.AffectsArrange |
                    FrameworkPropertyMetadataOptions.Inherits));

        public static void SetOrientation(DependencyObject element, Orientation value)
        {
            element.SetValue(OrientationProperty, value);
        }

        public static Orientation GetOrientation(DependencyObject element)
        {
            return (Orientation)element.GetValue(OrientationProperty);
        }

        public Orientation Orientation
        {
            get { return (Orientation) GetValue(OrientationProperty); }
            set { SetValue(OrientationProperty, value); }
        }
        #endregion

        public static readonly DependencyProperty RowCountProperty = 
            DependencyProperty.Register("RowCount", typeof (int), typeof (HexGrid),
            new FrameworkPropertyMetadata(1, FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsArrange), 
            ValidateCountCallback);


        public int RowCount
        {
            get { return (int) GetValue(RowCountProperty); }
            set { SetValue(RowCountProperty, value); }
        }

        public static readonly DependencyProperty ColumnCountProperty = 
            DependencyProperty.Register("ColumnCount", typeof (int), typeof (HexGrid),
            new FrameworkPropertyMetadata(1, FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsArrange),
            ValidateCountCallback);
        
        public int ColumnCount
        {
            get { return (int) GetValue(ColumnCountProperty); }
            set { SetValue(ColumnCountProperty, value); }
        }

        private static bool ValidateCountCallback(object value)
        {
            if (value is int)
            {
                int count = (int)value;
                return count > 0;
            }

            return false;
        }

        private int GetRow(UIElement e)
        {
            int row = (int)e.GetValue(Grid.RowProperty);
            if (row >= RowCount)
                row = RowCount - 1;
            return row;
        }

        private int GetColumn(UIElement e)
        {
            int column = (int)e.GetValue(Grid.ColumnProperty);
            if (column >= ColumnCount)
                column = ColumnCount - 1;
            return column;
        }

        protected override Size MeasureOverride(Size availableSize)
        {
            double w = availableSize.Width;
            double h = availableSize.Height;            

            // if there is Infinity size dimension
            if (Double.IsInfinity(w) || Double.IsInfinity(h))
            {
                // determine maximum desired size
                h = 0;
                w = 0;
                foreach (UIElement e in InternalChildren)
                {
                    e.Measure(availableSize);
                    var s = e.DesiredSize;
                    if (s.Height > h)
                        h = s.Height;
                    if (s.Width > w)
                        w = s.Width;
                }                

                // multiply maximum size to RowCount and ColumnCount to get total size
                if (Orientation == Orientation.Horizontal)
                    return new Size(w*(ColumnCount * 3 + 1)/4, h*(RowCount * 2 + 1)/2);

                return new Size(w*(ColumnCount * 2 + 1)/2, h*(RowCount * 3 + 1)/4);
            }            

            return availableSize;
        }

        #region HasShift
        private void HasShift(out bool first, out bool last)
        {
            if (Orientation == Orientation.Horizontal)
                HasRowShift(out first, out last);
            else
                HasColumnShift(out first, out last);
        }

        private void HasRowShift(out bool firstRow, out bool lastRow)
        {
            firstRow = lastRow = true;

            UIElementCollection elements = base.InternalChildren;
            for (int i = 0; i < elements.Count && (firstRow || lastRow); i++)
            {
                var e = elements[i];
                if (e.Visibility == Visibility.Collapsed)
                    continue;

                int row = GetRow(e);
                int column = GetColumn(e);

                int mod = column % 2;

                if (row == 0 && mod == 0)
                    firstRow = false;

                if (row == RowCount - 1 && mod == 1)
                    lastRow = false;
            }
        }

        private void HasColumnShift(out bool firstColumn, out bool lastColumn)
        {
            firstColumn = lastColumn = true;

            UIElementCollection elements = base.InternalChildren;
            for (int i = 0; i < elements.Count && (firstColumn || lastColumn); i++)
            {
                var e = elements[i];
                if (e.Visibility == Visibility.Collapsed)
                    continue;

                int row = GetRow(e);
                int column = GetColumn(e);

                int mod = row % 2;

                if (column == 0 && mod == 0)
                    firstColumn = false;

                if (column == ColumnCount - 1 && mod == 1)
                    lastColumn = false;
            }
        }
        #endregion

        #region GetHexSize
        private Size GetHexSize(Size gridSize)
        {
            double minH = 0;
            double minW = 0;

            foreach (UIElement e in InternalChildren)
            {
                var f = e as FrameworkElement;
                if (f != null)
                {
                    if (f.MinHeight > minH)
                        minH = f.MinHeight;
                    if (f.MinWidth > minW)
                        minW = f.MinWidth;
                }
            }

            bool first, last;
            HasShift(out first, out last);

            var possibleSize = GetPossibleSize(gridSize);
            double possibleW = possibleSize.Width;
            double possibleH = possibleSize.Height;

            var w = Math.Max(minW, possibleW);
            var h = Math.Max(minH, possibleH);

            return new Size(w, h);
        }

        private Size GetPossibleSize(Size gridSize)
        {
            bool first, last;
            HasShift(out first, out last);

            if (Orientation == Orientation.Horizontal)
                return GetPossibleSizeHorizontal(gridSize, first, last);

            return GetPossibleSizeVertical(gridSize, first, last);
        }

        private Size GetPossibleSizeVertical(Size gridSize, bool first, bool last)
        {
            int columns = ((first ? 0 : 1) + 2*ColumnCount - (last ? 1 : 0));
            double w = 2 * (gridSize.Width / columns);

            int rows = 1 + 3*RowCount;
            double h = 4 * (gridSize.Height / rows);

            return new Size(w, h);
        }

        private Size GetPossibleSizeHorizontal(Size gridSize, bool first, bool last)
        {
            int columns = 1 + 3*ColumnCount;
            double w = 4 * (gridSize.Width / columns);

            int rows = (first ? 0 : 1) + 2 * RowCount - (last ? 1 : 0);
            double h = 2 * (gridSize.Height / rows);

            return new Size(w, h);
        }
        #endregion

        protected override Size ArrangeOverride(Size finalSize)
        {            
            // determine if there is empty space at grid borders
            bool first, last;
            HasShift(out first, out last);

            // compute final hex size
            Size hexSize = GetHexSize(finalSize);
            
            // compute arrange line sizes
            double columnWidth, rowHeight;
            if (Orientation == Orientation.Horizontal)
            {
                rowHeight   = 0.50 * hexSize.Height;
                columnWidth = 0.25 * hexSize.Width;
            }
            else
            {
                rowHeight   = 0.25 * hexSize.Height;
                columnWidth = 0.50 * hexSize.Width;
            }            

            // arrange elements
            UIElementCollection elements = base.InternalChildren;
            for (int i = 0; i < elements.Count; i++)
            {
                if (elements[i].Visibility == Visibility.Collapsed)
                    continue;
                ArrangeElement(elements[i], hexSize, columnWidth, rowHeight, first);
            }
                        
            return finalSize;
        }

        private void ArrangeElement(UIElement e, Size hexSize, double columnWidth, double rowHeight, bool shift)
        {
            int row = GetRow(e);
            int column = GetColumn(e);

            double x;
            double y;

            if (Orientation == Orientation.Horizontal)
            {
                x = 3 * columnWidth * column;
                y = rowHeight * (2 * row + (column % 2 == 1 ? 1 : 0) + (shift ? -1 : 0));
            }
            else
            {
                x = columnWidth * (2 * column + (row % 2 == 1 ? 1 : 0) + (shift ? -1 : 0));
                y = 3 * rowHeight * row;
            }

            e.Arrange(new Rect(x, y, hexSize.Width, hexSize.Height));
        }
    }
}