ui/wpf/UIcore/DrawingArea.cs

Sun, 22 Sep 2024 21:17:15 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 22 Sep 2024 21:17:15 +0200
branch
newapi
changeset 307
301d6c73087a
parent 137
c9b8b9e0cfe8
permissions
-rw-r--r--

make headerbar buttons flat (GTK)

137
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 using System;
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 using System.Collections.Generic;
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 using System.Linq;
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 using System.Text;
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 using System.Threading.Tasks;
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 using System.Windows;
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 using System.Windows.Controls;
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 using System.Windows.Media;
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 using System.Windows.Shapes;
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 using System.Diagnostics;
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 namespace UI
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 {
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 public class DrawingArea : System.Windows.Controls.Canvas
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 {
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 public Action<int,int> resizeCallback;
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 public Color Color;
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 private Brush Brush;
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 public DrawingArea(Container container) : base()
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 {
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 this.SizeChanged += UpdateSize;
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 ResetGraphics();
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 container.Add(this, true);
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 }
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 public void UpdateSize(object sender, SizeChangedEventArgs e)
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 {
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 if(resizeCallback != null)
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32 {
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33 Children.Clear();
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 ResetGraphics();
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 Size s = e.NewSize;
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 resizeCallback((int)s.Width, (int)s.Height);
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 }
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 }
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 public void Redraw()
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 {
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 if (resizeCallback != null)
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 {
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 Children.Clear();
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 ResetGraphics();
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 resizeCallback((int)ActualWidth, (int)ActualHeight);
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 }
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 }
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 private void ResetGraphics()
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 {
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 Color = Color.FromRgb(0, 0, 0);
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56 Brush = System.Windows.Media.Brushes.Black;
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 }
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 public void SetColor(int r, int g, int b)
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 {
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 Color = Color.FromRgb((byte)r, (byte)g, (byte)b);
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 Brush = new SolidColorBrush(Color);
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 }
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 public void DrawLine(int x1, int y1, int x2, int y2)
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 {
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 Line line = new Line();
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 line.Stroke = Brush;
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 line.StrokeThickness = 1;
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70 line.SnapsToDevicePixels = true;
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 line.X1 = x1;
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73 line.Y1 = y1;
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74 line.X2 = x2;
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 line.Y2 = y2;
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 Children.Add(line);
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78 }
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 public void DrawRect(int x, int y, int w, int h, bool fill)
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 {
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82 Rectangle rect = new Rectangle();
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 rect.Stroke = Brush;
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 rect.StrokeThickness = 1;
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 rect.SnapsToDevicePixels = true;
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86 if(fill)
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87 {
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88 rect.Fill = Brush;
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89 }
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91 rect.Width = w;
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92 rect.Height = h;
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93 SetLeft(rect, x);
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94 SetTop(rect, y);
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96 Children.Add(rect);
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
97 }
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98 }
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99 }

mercurial