ui/wpf/UIcore/DrawingArea.cs

Mon, 23 Jan 2017 10:50:22 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 23 Jan 2017 10:50:22 +0100
changeset 137
c9b8b9e0cfe8
permissions
-rw-r--r--

adds drawingarea (WPF)

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