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
200 lines (175 loc) · 6.88 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DWSIM.Drawing.SkiaSharp;
using DWSIM.UI.Controls;
using SkiaSharp;
using Eto.Forms;
using Eto.GtkSharp.Forms;
using Eto.Drawing;
using Eto.GtkSharp;
namespace DWSIM.UI.Desktop.GTK
{
public class FlowsheetSurfaceControlHandler : Eto.GtkSharp.Forms.GtkControl<Gtk.EventBox, FlowsheetSurfaceControl, FlowsheetSurfaceControl.ICallback>, FlowsheetSurfaceControl.IFlowsheetSurface
{
private FlowsheetSurface_GTK nativecontrol;
public FlowsheetSurfaceControlHandler()
{
nativecontrol = new FlowsheetSurface_GTK();
this.Control = nativecontrol;
}
public override void OnLoadComplete(EventArgs e)
{
base.OnLoadComplete(e);
nativecontrol.fbase = this.Widget.FlowsheetObject;
nativecontrol.fsurface = this.Widget.FlowsheetSurface;
nativecontrol.DragDataGet += (sender, e2) => {
Console.WriteLine(e2.SelectionData.Type.Name);
};
nativecontrol.DragEnd += (sender, e2) => {
foreach (var item in e2.Args)
{
Console.WriteLine(item.ToString());
}
};
nativecontrol.DragFailed += (sender, e2) => {
foreach (var item in e2.Args)
{
Console.WriteLine(item.ToString());
}
};
}
public override Eto.Drawing.Color BackgroundColor
{
get
{
return Eto.Drawing.Colors.White;
}
set
{
return;
}
}
public GraphicsSurface FlowsheetSurface
{
get
{
return ((FlowsheetSurface_GTK)this.Control).fsurface;
}
set
{
((FlowsheetSurface_GTK)this.Control).fsurface = value;
}
}
public DWSIM.UI.Desktop.Shared.Flowsheet FlowsheetObject
{
get
{
return ((FlowsheetSurface_GTK)this.Control).fbase;
}
set
{
((FlowsheetSurface_GTK)this.Control).fbase = value;
}
}
}
public class FlowsheetSurface_GTK : Gtk.EventBox
{
public GraphicsSurface fsurface;
public DWSIM.UI.Desktop.Shared.Flowsheet fbase;
private float _lastTouchX;
private float _lastTouchY;
public FlowsheetSurface_GTK()
{
this.AddEvents((int)Gdk.EventMask.PointerMotionMask);
this.ButtonPressEvent += FlowsheetSurface_GTK_ButtonPressEvent;
this.ButtonReleaseEvent += FlowsheetSurface_GTK_ButtonReleaseEvent;
this.MotionNotifyEvent += FlowsheetSurface_GTK_MotionNotifyEvent;
this.ScrollEvent += FlowsheetSurface_GTK_ScrollEvent;
var targets = new List<Gtk.TargetEntry>();
targets.Add(new Gtk.TargetEntry("ObjectName", 0, 1));
Gtk.Drag.DestSet(this, Gtk.DestDefaults.All, targets.ToArray(), Gdk.DragAction.Copy | Gdk.DragAction.Link | Gdk.DragAction.Move);
}
void FlowsheetSurface_GTK_ScrollEvent(object o, Gtk.ScrollEventArgs args)
{
if (args.Event.Direction == Gdk.ScrollDirection.Down)
{
fsurface.Zoom += -5 / 100f;
}
else
{
fsurface.Zoom += 5 / 100f;
}
if (fsurface.Zoom < 0.05) fsurface.Zoom = 0.05f;
this.QueueDraw();
}
void FlowsheetSurface_GTK_MotionNotifyEvent(object o, Gtk.MotionNotifyEventArgs args)
{
float x = (int)args.Event.X;
float y = (int)args.Event.Y;
_lastTouchX = x;
_lastTouchY = y;
fsurface.InputMove((int)_lastTouchX, (int)_lastTouchY);
this.QueueDraw();
}
void FlowsheetSurface_GTK_ButtonReleaseEvent(object o, Gtk.ButtonReleaseEventArgs args)
{
fsurface.InputRelease();
this.QueueDraw();
}
void FlowsheetSurface_GTK_ButtonPressEvent(object o, Gtk.ButtonPressEventArgs args)
{
if (args.Event.Type == Gdk.EventType.TwoButtonPress)
{
//if (args.Event.State == Gdk.ModifierType.ShiftMask)
//{
// fsurface.Zoom = 1.0f;
//}
//else {
// fsurface.ZoomAll((int)this.Allocation.Width, (int)this.Allocation.Height);
//}
}
else
{
_lastTouchX = (int)args.Event.X;
_lastTouchY = (int)args.Event.Y;
fsurface.InputPress((int)_lastTouchX, (int)_lastTouchY);
}
this.QueueDraw();
}
protected override bool OnExposeEvent(Gdk.EventExpose evnt)
{
var rect = Allocation;
if (rect.Width > 0 && rect.Height > 0)
{
var area = evnt.Area;
SKColorType ctype = SKColorType.Bgra8888;
if (GlobalSettings.Settings.RunningPlatform() == GlobalSettings.Settings.Platform.Mac) ctype = SKColorType.Rgba8888;
using (Cairo.Context cr = Gdk.CairoHelper.Create(base.GdkWindow))
{
if (cr == null) { Console.WriteLine("Cairo Context is null"); }
using (var bitmap = new SKBitmap(rect.Width, rect.Height, ctype, SKAlphaType.Premul))
{
if (bitmap == null) { Console.WriteLine("Bitmap is null"); }
IntPtr len;
using (var skSurface = SKSurface.Create(bitmap.Info.Width, bitmap.Info.Height, ctype, SKAlphaType.Premul, bitmap.GetPixels(out len), bitmap.Info.RowBytes))
{
if (skSurface == null) { Console.WriteLine("skSurface is null"); }
if (fsurface != null) fsurface.UpdateSurface(skSurface);
skSurface.Canvas.Flush();
using (Cairo.Surface surface = new Cairo.ImageSurface(bitmap.GetPixels(out len), Cairo.Format.Argb32, bitmap.Width, bitmap.Height, bitmap.Width * 4))
{
surface.MarkDirty();
cr.SetSourceSurface(surface, 0, 0);
cr.Paint();
}
}
}
}
}
return true;
}
}
}