ui/wpf/UIcore/DrawingArea.cs

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

mercurial