aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Tango.PPC.Common/Controls/TwineCatalogRenderer.cs
blob: 8d57cbfd06cbad5fa8604e70c4f7b1abbb671199 (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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Tango.BL.Entities;
using Tango.Touch.Components;
using Tango.Touch.Controls;

namespace Tango.PPC.Common.Controls
{
    public class TwineCatalogRenderer : FrameworkElement
    {
        private class ItemContainer
        {
            public DrawingVisual Visual { get; set; }
            public ColorCatalogsItem Item { get; set; }
            public double PositionY { get; set; }
            public Rect ImageRect { get; set; }
        }

        private class GroupContainer
        {
            public DrawingVisual Visual { get; set; }
            public ColorCatalogsGroup Group { get; set; }
            public double PositionY { get; set; }

            public List<ItemContainer> Items { get; set; }

            public GroupContainer()
            {
                Items = new List<ItemContainer>();
            }
        }

        private readonly VisualCollection _children;
        private List<ItemContainer> _catalogItems;
        private List<GroupContainer> _groupItems;
        private double ellipseWidth = 70;
        private double ellipseHeight = 70;
        private double textHeight = 20;
        private double ellipseMarginX = 18;
        private double ellipseMarginY = 50;
        private double textMargin = 8;
        private double groupMargin = 50;
        private bool selectedFromClick;
        private LightTouchScrollViewer _scrollViewer;
       // private BitmapSource _myColorBitmap;
        private double imageWidth = 24;
        private double imageHeight = 22;
        private Point _lastMouseClickPoint;

        public IEnumerable<ColorCatalogsGroup> Groups
        {
            get { return (IEnumerable<ColorCatalogsGroup>)GetValue(GroupsProperty); }
            set { SetValue(GroupsProperty, value); }
        }
        public static readonly DependencyProperty GroupsProperty =
            DependencyProperty.Register("Groups", typeof(IEnumerable<ColorCatalogsGroup>), typeof(TwineCatalogRenderer), new PropertyMetadata(null, (d, e) => { (d as TwineCatalogRenderer).OnCatalogChanged(); }));

        public ColorCatalogsItem SelectedItem
        {
            get { return (ColorCatalogsItem)GetValue(SelectedItemProperty); }
            set { SetValue(SelectedItemProperty, value); }
        }
        public static readonly DependencyProperty SelectedItemProperty =
            DependencyProperty.Register("SelectedItem", typeof(ColorCatalogsItem), typeof(TwineCatalogRenderer), new PropertyMetadata(null, (d, e) => (d as TwineCatalogRenderer).OnSelectedItemChanged(e.OldValue, e.NewValue)));

        public Brush Foreground
        {
            get { return (Brush)this.GetValue(ForegroundProperty); }
            set { this.SetValue(ForegroundProperty, value); }
        }
        public static readonly DependencyProperty ForegroundProperty =
               TextBlock.ForegroundProperty.AddOwner(typeof(TwineCatalogRenderer));

        public Brush Background
        {
            get { return (Brush)GetValue(BackgroundProperty); }
            set { SetValue(BackgroundProperty, value); }
        }
        public static readonly DependencyProperty BackgroundProperty =
            DependencyProperty.Register("Background", typeof(Brush), typeof(TwineCatalogRenderer), new PropertyMetadata(null));


        public TwineCatalogRenderer()
        {
            Background = new SolidColorBrush(Colors.White);

            _children = new VisualCollection(this);
            _catalogItems = new List<ItemContainer>();
            _groupItems = new List<GroupContainer>();

            this.PreviewMouseUp += TwineCatalogRenderer_MouseLeftButtonUp;
            this.Loaded += TwineCatalogRenderer_Loaded;
           // _myColorBitmap = new BitmapImage(new Uri($"pack://application:,,,/Tango.PPC.JobsV2;component/Images/ColorSelection/Heart.png", UriKind.Absolute));
        }

        private void TwineCatalogRenderer_Loaded(object sender, RoutedEventArgs e)
        {
            _scrollViewer = this.FindAncestor<LightTouchScrollViewer>();
        }

        public ColorCatalogsGroup GetVisibleGroup()
        {
            double currentPosition = _scrollViewer.GetScrollPosition();

            var viewportRect = _scrollViewer.GetViewPortRect();

            foreach (var group in _groupItems.Where(x => x.Items.Count > 0).Reverse().ToList())
            {
                var groupRect = GetGroupRect(group);

                if (groupRect.IntersectsWith(viewportRect))
                {
                    return group.Group;
                }
            }

            return null;
        }

        public double GetGroupPosition(ColorCatalogsGroup group)
        {
            return _groupItems.Single(x => x.Group == group).PositionY;
        }

        private Rect GetGroupRect(GroupContainer group)
        {
            var height = group.Items.Last().PositionY - group.PositionY;
            return new Rect(0, group.PositionY + (height / 4), ActualWidth, height);
        }

        private void TwineCatalogRenderer_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {

            if (!_scrollViewer.IsAfterScrolling)
            {
                // Retreive the coordinates of the mouse button event.
                Point pt = e.GetPosition((UIElement)this);
                _lastMouseClickPoint = pt;
                // Initiate the hit test by setting up a hit test result callback method.
                VisualTreeHelper.HitTest(this, null, HitTestCallback, new PointHitTestParameters(pt));
            }
        }
        
        // If a child visual object is hit, toggle its opacity to visually indicate a hit.
        public HitTestResultBehavior HitTestCallback(HitTestResult result)
        {
            if (result.VisualHit.GetType() == typeof(System.Windows.Media.DrawingVisual))
            {
                var visual = result.VisualHit as DrawingVisual;

                if (visual != null)
                {
                    var container = _catalogItems.SingleOrDefault(x => x.Visual == visual);
                    
                    if (container != null && container.Item is ColorCatalogsItem)
                    {
                        if (container.ImageRect.Contains(_lastMouseClickPoint))
                        {
                            // clicked on image
                        }
                        selectedFromClick = true;
                        SelectedItem = container.Item;
                        selectedFromClick = false;
                    }
                }
            }

            // Stop the hit test enumeration of objects in the visual tree.
            return HitTestResultBehavior.Stop;
        }

        private void OnSelectedItemChanged(object oldValue, object newValue)
        {
            var newItem = _catalogItems.SingleOrDefault(x => x.Item == newValue);
            var oldItem = _catalogItems.SingleOrDefault(x => x.Item == oldValue);

            if (newItem != null)
            {
                (newItem.Visual.Transform as ScaleTransform).ScaleX = 1.3;
                (newItem.Visual.Transform as ScaleTransform).ScaleY = 1.3;

                if (!selectedFromClick && _scrollViewer != null)
                {
                    
                    _scrollViewer.ScrollToPosition(newItem.PositionY - _scrollViewer.ActualHeight / 2);
                }
            }

            if (oldItem != null)
            {
                (oldItem.Visual.Transform as ScaleTransform).ScaleX = 1;
                (oldItem.Visual.Transform as ScaleTransform).ScaleY = 1;
            }
        }

        private ItemContainer AddCatalogItem(DrawingVisual visual, ColorCatalogsItem item, double y, Rect imageRect)
        {
            _children.Add(visual);

            var container = new ItemContainer()
            {
                Visual = visual,
                Item = item,
                PositionY = y,
                ImageRect = imageRect
            };

            _catalogItems.Add(container);

            return container;
        }

        private GroupContainer AddGroupItem(DrawingVisual visual, ColorCatalogsGroup group, double y)
        {
            _children.Add(visual);

            var container = new GroupContainer()
            {
                Visual = visual,
                Group = group,
                PositionY = y,
            };

            _groupItems.Add(container);

            return container;
        }

        private void OnCatalogChanged()
        {
            _children.Clear();
            _catalogItems.Clear();
            _groupItems.Clear();

            double position_y = 0;
            double position_x = 0;

            if (Groups != null)
            {
                foreach (var group in Groups.OrderBy(x => x.GroupIndex))
                {
                    position_x = 0;

                    var groupContainer = AddGroupItem(CreateGroup(group.Name, position_y), group, position_y);

                    position_y += ellipseMarginX;

                    foreach (var item in group.ColorCatalogsItems.OrderBy(x => x.ItemIndex))
                    {
                        double x = position_x + ellipseMarginX;

                        if (x + ellipseWidth > ActualWidth)
                        {
                            position_x = 0;
                            position_y += (ellipseHeight + ellipseMarginY + textMargin + textHeight);
                        }
                        Rect imageRect = new Rect(new Point(position_x + ellipseWidth, position_y + ellipseMarginY/2 + 2), new Size( imageWidth,  imageHeight));
                        var itemContainer = AddCatalogItem(CreateItem(item, position_x + ellipseMarginX, position_y + ellipseMarginY, imageRect), item, position_y + ellipseMarginY, imageRect);
                        groupContainer.Items.Add(itemContainer);

                        position_x += ellipseWidth + ellipseMarginX;
                    }

                    position_y += (ellipseHeight + ellipseMarginY + textMargin + textHeight + groupMargin);
                }

                Height = position_y;
            }
            
            UpdateLayout();

            if (SelectedItem != null)
            {
                OnSelectedItemChanged( null, SelectedItem);
            }
            
            //InvalidateVisual();
        }

        private DrawingVisual CreateItem(ColorCatalogsItem item, double x, double y, Rect imageRect)
        {
            DrawingVisual drawingVisual = new DrawingVisual();
            DrawingContext drawingContext = drawingVisual.RenderOpen();
            
           // drawingContext.DrawImage(_myColorBitmap, imageRect);

            drawingContext.DrawEllipse(new SolidColorBrush(item.Color), null, new Point(x + ellipseWidth / 2, y + ellipseHeight / 2), ellipseWidth / 2, ellipseHeight / 2);
            
            var formattedText = new FormattedText(item.Name,
                                    CultureInfo.GetCultureInfo("en-us"),
                                    FlowDirection.LeftToRight,
                                    new Typeface("Flexo"),
                                    16,
                                    Foreground);

            formattedText.MaxTextWidth = ellipseWidth + ellipseMarginX;

            drawingContext.DrawText(formattedText, new Point((x + (ellipseWidth / 2)) - (formattedText.Width / 2), y + ellipseHeight + textMargin));

            var center = new Point(x + ellipseWidth / 2, (y + ellipseHeight / 2) + textHeight + textMargin);

            drawingVisual.Transform = new ScaleTransform(1, 1, center.X, center.Y);

            drawingContext.Close();

            return drawingVisual;
        }

        private void Clear()
        {
            DrawingVisual drawingVisual = new DrawingVisual();
            DrawingContext drawingContext = drawingVisual.RenderOpen();

            drawingContext.DrawRectangle(Background, null, new Rect(0, 0, ActualWidth, ActualHeight));

            drawingContext.Close();

            _children.Add(drawingVisual);
        }

        private DrawingVisual CreateGroup(String name, double y)
        {
            DrawingVisual drawingVisual = new DrawingVisual();
            DrawingContext drawingContext = drawingVisual.RenderOpen();

            var formattedText = new FormattedText(name,
                                   CultureInfo.GetCultureInfo("en-us"),
                                   FlowDirection.LeftToRight,
                                   new Typeface("Flexo"),
                                   23,
                                   Foreground);

            //formattedText.SetFontWeight(FontWeights.SemiBold);

           // drawingContext.DrawText(formattedText, new Point((ActualWidth / 2) - (formattedText.Width / 2), y));
            drawingContext.DrawText(formattedText, new Point(ellipseMarginX, y));

            drawingContext.Close();

            return drawingVisual;
        }

        // Provide a required override for the VisualChildrenCount property.
        protected override int VisualChildrenCount => _children != null ? _children.Count : 0;

        // Provide a required override for the GetVisualChild method.
        protected override Visual GetVisualChild(int index)
        {
            if (index < 0 || index >= _children.Count)
            {
                throw new ArgumentOutOfRangeException();
            }

            return _children[index];
        }
        
        public void ScrollToFirstFindItem(ICollectionFilter collectionFilter)
        {
            foreach (var group in _groupItems.Where(x => x.Items.Count > 0).Reverse().ToList())
            {
                foreach( var item in group.Items)
                {
                    if(collectionFilter.Filter(item.Item))
                    {
                        if (_scrollViewer != null)
                            _scrollViewer.ScrollToPosition(item.PositionY - _scrollViewer.ActualHeight / 2);
                        break;
                    }
                }
            }
        }
    }
}