ui/wpf/UIcore/DrawingArea.cs

Thu, 15 Feb 2024 21:33:08 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 15 Feb 2024 21:33:08 +0100
branch
newapi
changeset 262
81778b9589d0
parent 137
c9b8b9e0cfe8
permissions
-rw-r--r--

port progressbar to new API (GTK)

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