ui/wpf/UIcore/DrawingArea.cs

changeset 137
c9b8b9e0cfe8
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/wpf/UIcore/DrawingArea.cs	Mon Jan 23 10:50:22 2017 +0100
@@ -0,0 +1,99 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Media;
+using System.Windows.Shapes;
+using System.Diagnostics;
+
+namespace UI
+{
+    public class DrawingArea : System.Windows.Controls.Canvas
+    {
+        public Action<int,int> resizeCallback;
+
+        public Color Color;
+        private Brush Brush;
+
+        public DrawingArea(Container container) : base()
+        {
+            this.SizeChanged += UpdateSize;
+            ResetGraphics();
+
+            container.Add(this, true);
+        }
+
+        public void UpdateSize(object sender, SizeChangedEventArgs e)
+        {
+            if(resizeCallback != null)
+            {
+                Children.Clear();
+                ResetGraphics();
+
+                Size s = e.NewSize;
+                resizeCallback((int)s.Width, (int)s.Height);
+            }
+        }
+
+        public void Redraw()
+        {
+            if (resizeCallback != null)
+            {
+                Children.Clear();
+                ResetGraphics();
+
+                resizeCallback((int)ActualWidth, (int)ActualHeight);
+            }
+
+        }
+
+        private void ResetGraphics()
+        {
+            Color = Color.FromRgb(0, 0, 0);
+            Brush = System.Windows.Media.Brushes.Black;
+        }
+
+        public void SetColor(int r, int g, int b)
+        {
+            Color = Color.FromRgb((byte)r, (byte)g, (byte)b);
+            Brush = new SolidColorBrush(Color);
+        }
+
+        public void DrawLine(int x1, int y1, int x2, int y2)
+        {
+            Line line = new Line();
+            line.Stroke = Brush;
+            line.StrokeThickness = 1;
+            line.SnapsToDevicePixels = true;
+
+            line.X1 = x1;
+            line.Y1 = y1;
+            line.X2 = x2;
+            line.Y2 = y2;
+
+            Children.Add(line);
+        }
+
+        public void DrawRect(int x, int y, int w, int h, bool fill)
+        {
+            Rectangle rect = new Rectangle();
+            rect.Stroke = Brush;
+            rect.StrokeThickness = 1;
+            rect.SnapsToDevicePixels = true;
+            if(fill)
+            {
+                rect.Fill = Brush;
+            }
+
+            rect.Width = w;
+            rect.Height = h;
+            SetLeft(rect, x);
+            SetTop(rect, y);
+
+            Children.Add(rect);
+        }
+    }
+}

mercurial