Repository navigation
Expand file tree
/
Copy pathMplPanel.cs
More file actions
108 lines (92 loc) · 2.85 KB
/
Copy pathMplPanel.cs
File metadata and controls
108 lines (92 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace Matplotlib.Net;
public class MplPanel : Panel
{
private readonly NetMplAdapter _adapter;
private WriteableBitmap _buffer = new(800, 600, 96, 96, PixelFormats.Pbgra32, null);
private readonly DrawingGroup _plot = new();
static MplPanel()
{
NetMplAdapter.Init();
}
public MplPanel()
{
_adapter = NetMplAdapter.Create();
_adapter.SetBuffer(_buffer);
_adapter.SetRef(this);
MouseMove += HandleMouseMove;
MouseLeftButtonDown += HandleMouseDown;
MouseRightButtonDown += HandleMouseDown;
MouseRightButtonUp += HandleMouseUp;
MouseLeftButtonUp += HandleMouseUp;
MouseWheel += HandleMouseWheel;
}
public void ExecPython(string code)
{
_adapter.Exec(code);
}
public void Plot1D(double[] x, double[] y)
{
_adapter.Plot1D(x, y);
}
private void HandleMouseWheel(object sender, MouseWheelEventArgs e)
{
var pos = e.GetPosition(this);
_adapter.HandleWheel(pos, e.Delta);
InvalidateVisual();
}
private void HandleMouseMove(object sender, MouseEventArgs e)
{
if (e.RightButton == MouseButtonState.Pressed || e.LeftButton == MouseButtonState.Pressed)
{
var pos = e.GetPosition(this);
_adapter.HandleMouseMove(pos, ButtonToMpl(e));
}
}
private void HandleMouseUp(object sender, MouseEventArgs e)
{
var pos = e.GetPosition(this);
_adapter.HandleMouseUp(pos, ButtonToMpl(e));
}
private void HandleMouseDown(object sender, MouseEventArgs e)
{
var pos = e.GetPosition(this);
_adapter.HandleMouseDown(pos, ButtonToMpl(e));
}
private static string ButtonToMpl(MouseEventArgs state)
{
return state switch
{
{ LeftButton: MouseButtonState.Pressed } => "left",
{ RightButton: MouseButtonState.Pressed } => "right",
_ => ""
};
}
protected override void OnRender(DrawingContext dc)
{
base.OnRender(dc);
RenderPlot();
dc.DrawDrawing(_plot);
}
public void RenderPlot()
{
var w = ActualWidth;
var h = ActualHeight;
if (w > 0 || h > 0)
{
if (w > _buffer.Width || h > _buffer.Height)
{
_buffer = new WriteableBitmap((int)w + 200, (int)h + 200, 96, 96, PixelFormats.Pbgra32, null);
_adapter.SetBuffer(_buffer);
}
_adapter.SetFigureSize(w, h);
}
var dc = _plot.Open();
dc.DrawImage(_buffer, new Rect(new Point(), new Point(_buffer.PixelWidth, _buffer.PixelHeight)));
dc.Close();
}
}