This repository has been archived by the owner on Jun 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
/
CanvasControl.cs
173 lines (138 loc) · 4.74 KB
/
CanvasControl.cs
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
using System;
using System.Linq;
using SkiaSharp;
using SkiaSharp.Views.Desktop;
using DWSIM.Drawing.SkiaSharp;
using DWSIM.UI.Controls;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;
namespace DWSIM.UI.Desktop.WinForms
{
public class FlowsheetSurfaceControlHandler : Eto.WinForms.Forms.WindowsControl<Control, FlowsheetSurfaceControl, FlowsheetSurfaceControl.ICallback>, FlowsheetSurfaceControl.IFlowsheetSurface
{
private FlowsheetSurface_WinForms nativecontrol;
public FlowsheetSurfaceControlHandler()
{
nativecontrol = new FlowsheetSurface_WinForms();
this.Control = nativecontrol;
}
public override void OnLoadComplete(EventArgs e)
{
base.OnLoadComplete(e);
nativecontrol.fbase = this.Widget.FlowsheetObject;
nativecontrol.fsurface = this.Widget.FlowsheetSurface;
}
public override Eto.Drawing.Color BackgroundColor
{
get
{
return Eto.Drawing.Colors.White;
}
set
{
return;
}
}
public GraphicsSurface FlowsheetSurface
{
get
{
return ((FlowsheetSurface_WinForms)this.Control).fsurface;
}
set
{
((FlowsheetSurface_WinForms)this.Control).fsurface = value;
}
}
public DWSIM.UI.Desktop.Shared.Flowsheet FlowsheetObject
{
get
{
return ((FlowsheetSurface_WinForms)this.Control).fbase;
}
set
{
((FlowsheetSurface_WinForms)this.Control).fbase = value;
}
}
}
public class FlowsheetSurface_WinForms : SkiaSharp.Views.Desktop.SKControl
{
double DpiScale = 1.0;
public GraphicsSurface fsurface;
public DWSIM.UI.Desktop.Shared.Flowsheet fbase;
private Bitmap bitmap;
private float _lastTouchX;
private float _lastTouchY;
public FlowsheetSurface_WinForms()
{
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
DpiScale = e.Graphics.DpiX / 96.0;
// get the bitmap
CreateBitmap();
var data = bitmap.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.WriteOnly, bitmap.PixelFormat);
// create the surface
var info = new SKImageInfo(Width, Height, SKImageInfo.PlatformColorType, SKAlphaType.Premul);
using (var surface = SKSurface.Create(info, data.Scan0, data.Stride))
{
// start drawing
if (fsurface != null) fsurface.UpdateSurface(surface);
OnPaintSurface(new SKPaintSurfaceEventArgs(surface, info));
surface.Canvas.Flush();
}
// write the bitmap to the graphics
bitmap.UnlockBits(data);
e.Graphics.DrawImage(bitmap, 0, 0);
}
private void CreateBitmap()
{
if (bitmap == null || bitmap.Width != Width || bitmap.Height != Height)
{
FreeBitmap();
bitmap = new Bitmap(Width, Height, PixelFormat.Format32bppPArgb);
}
}
private void FreeBitmap()
{
if (bitmap != null)
{
bitmap.Dispose();
bitmap = null;
}
}
protected override void OnMouseDown(MouseEventArgs e)
{
_lastTouchX = e.X;
_lastTouchY = e.Y;
fsurface.InputPress((int)_lastTouchX, (int)_lastTouchY);
this.Invalidate();
}
protected override void OnMouseUp(MouseEventArgs e)
{
fsurface.InputRelease();
this.Invalidate();
}
protected override void OnMouseMove(MouseEventArgs e)
{
_lastTouchX = e.X;
_lastTouchY = e.Y;
fsurface.InputMove((int)_lastTouchX, (int)_lastTouchY);
this.Invalidate();
}
protected override void OnMouseDoubleClick(MouseEventArgs e)
{
//fsurface.ZoomAll((int)this.Width, (int)this.Height);
//this.Invalidate();
}
protected override void OnMouseWheel(MouseEventArgs e)
{
fsurface.Zoom += e.Delta / 4 / 100.0f;
if (fsurface.Zoom < 0.05) fsurface.Zoom = 0.05f;
this.Invalidate();
}
}
}