ui/wpf/UIcore/DrawingArea.cs

Mon, 22 May 2023 19:44:27 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 22 May 2023 19:44:27 +0200
branch
newapi
changeset 175
2cb06c231057
parent 137
c9b8b9e0cfe8
permissions
-rw-r--r--

move some menu code to common

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