-
Notifications
You must be signed in to change notification settings - Fork 204
/
Connectors.cs
525 lines (458 loc) · 23.9 KB
/
Connectors.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
using System;
using System.Collections.ObjectModel;
using Android.Content;
using Android.Graphics;
using Android.Views;
using Android.Widget;
using Syncfusion.SfDiagram.Android;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
namespace SampleBrowser
{
public partial class Connectors : SamplePage
{
SfDiagram diagram;
private Context m_context;
private float currentDensity;
private LinearLayout buttons;
private TextView label;
public override View GetSampleContent(Context context)
{
m_context = context;
currentDensity = m_context.Resources.DisplayMetrics.Density;
//Initialize the SfDiagram and set its background color.
diagram = new SfDiagram(context);
diagram.LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);
diagram.IsReadOnly = true;
ObservableCollection<Employee> employees = new ObservableCollection<Employee>();
employees.Add(new Employee() { Name = "Elizabeth", EmpId = "1", ParentId = "", Designation = "CEO" });
employees.Add(new Employee() { Name = "Christina", EmpId = "2", ParentId = "1", Designation = "Manager" });
employees.Add(new Employee() { Name = "Yang", EmpId = "3", ParentId = "1", Designation = "Manager" });
employees.Add(new Employee() { Name = "Yoshi", EmpId = "4", ParentId = "2", Designation = "Team Lead" });
employees.Add(new Employee() { Name = "Yoshi", EmpId = "5", ParentId = "2", Designation = "Co-ordinator" });
employees.Add(new Employee() { Name = "Philip", EmpId = "6", ParentId = "4", Designation = "Developer" });
employees.Add(new Employee() { Name = "Philip", EmpId = "7", ParentId = "4", Designation = "Testing Engineer" });
employees.Add(new Employee() { Name = "Roland", EmpId = "8", ParentId = "3", Designation = "Team Lead" });
employees.Add(new Employee() { Name = "Yoshi", EmpId = "9", ParentId = "3", Designation = "Co-ordinator" });
employees.Add(new Employee() { Name = "Yuonne", EmpId = "10", ParentId = "8", Designation = "Developer" });
employees.Add(new Employee() { Name = "Philip", EmpId = "10", ParentId = "8", Designation = "Testing Engineer" });
//Initializes the DataSourceSettings
diagram.DataSourceSettings = new DataSourceSettings() { DataSource = employees, Id = "EmpId", ParentId = "ParentId" };
//Initializes the Layout
DirectedTreeLayout treelayout = new DirectedTreeLayout() { HorizontalSpacing = 80, VerticalSpacing = 50 * currentDensity, TreeOrientation = TreeOrientation.TopToBottom };
diagram.LayoutManager = new LayoutManager() { Layout = treelayout };
diagram.BeginNodeRender += Diagram_BeginNodeRender;
diagram.Loaded += Diagram_Loaded;
LinearLayout linearLayout = new LinearLayout(context) { Orientation = Android.Widget.Orientation.Vertical };
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);
layoutParams.TopMargin = 25 * (int)MainActivity.Factor;
linearLayout.LayoutParameters = layoutParams;
LinearLayout typesBar = new LinearLayout(context);
typesBar.SetBackgroundColor(Color.Rgb(245, 245, 245));
layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);
typesBar.SetMinimumHeight(200 * (int)MainActivity.Factor);
typesBar.SetPadding(0, 10 * (int)MainActivity.Factor, 0, 10 * (int)MainActivity.Factor);
TextView selectText = new TextView(context)
{
LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.MatchParent),
Text = "Connector Types:",
TextAlignment = TextAlignment.Gravity,
Gravity = GravityFlags.Center | GravityFlags.CenterVertical,
TextSize = 15 * MainActivity.Factor
};
int width = (int)(context.Resources.DisplayMetrics.WidthPixels - context.Resources.DisplayMetrics.Density);
selectText.SetMinimumWidth((int)(width * 0.4 * MainActivity.Factor));
ImageButtonView straightButton = new ImageButtonView(context);
straightButton.Click += ButtonTouch;
straightButton.ImageId = "DiagramStraight";
straightButton.LayoutParameters = new ViewGroup.LayoutParams(180 * (int)MainActivity.Factor, 180 * (int)MainActivity.Factor);
ImageView straightImage = new ImageView(context);
var imageId = straightButton.ImageId + ".png";
if (imageId != null)
{
var imageData = LoadResource(imageId).ToArray();
straightImage.SetImageBitmap(BitmapFactory.DecodeByteArray(imageData, 0, imageData.Length));
}
straightImage.Layout((int)(40 * MainActivity.Factor), (int)(40 * MainActivity.Factor), (int)(110 * MainActivity.Factor), (int)(110 * MainActivity.Factor));
straightButton.AddView(straightImage);
ImageButtonView curveButton = new ImageButtonView(context);
curveButton.Click += ButtonTouch;
curveButton.ImageId = "DiagramCurve";
curveButton.LayoutParameters = new ViewGroup.LayoutParams(180 * (int)MainActivity.Factor, 180 * (int)MainActivity.Factor);
ImageView curveImage = new ImageView(context);
imageId = curveButton.ImageId + ".png";
if (imageId != null)
{
var imageData = LoadResource(imageId).ToArray();
curveImage.SetImageBitmap(BitmapFactory.DecodeByteArray(imageData, 0, imageData.Length));
}
curveImage.Layout((int)(40 * MainActivity.Factor), (int)(40 * MainActivity.Factor), (int)(110 * MainActivity.Factor), (int)(110 * MainActivity.Factor));
curveButton.AddView(curveImage);
ImageButtonView edgeButton = new ImageButtonView(context);
edgeButton.Click += ButtonTouch;
edgeButton.LayoutParameters = new ViewGroup.LayoutParams(180 * (int)MainActivity.Factor, 180 * (int)MainActivity.Factor);
edgeButton.ImageId = "DiagramEdge";
ImageView edgeImage = new ImageView(context);
imageId = edgeButton.ImageId + ".png";
if (imageId != null)
{
var imageData = LoadResource(imageId).ToArray();
edgeImage.SetImageBitmap(BitmapFactory.DecodeByteArray(imageData, 0, imageData.Length));
}
edgeImage.Layout((int)(40 * MainActivity.Factor), (int)(40 * MainActivity.Factor), (int)(110 * MainActivity.Factor), (int)(110 * MainActivity.Factor));
edgeButton.AddView(edgeImage);
buttons = new LinearLayout(context);
buttons.SetMinimumWidth((int)(width - width * 0.4 * MainActivity.Factor));
buttons.SetBackgroundColor(Color.Transparent);
buttons.AddView(straightButton);
TextView view = new TextView(context);
view.SetWidth(30 * (int)MainActivity.Factor);
view.SetBackgroundColor(Color.Transparent);
buttons.AddView(view);
buttons.AddView(curveButton);
view = new TextView(context);
view.SetWidth(30 * (int)MainActivity.Factor);
view.SetBackgroundColor(Color.Transparent);
buttons.AddView(view);
buttons.AddView(edgeButton);
typesBar.AddView(selectText);
typesBar.AddView(buttons);
linearLayout.AddView(typesBar);
linearLayout.AddView(diagram);
for (int i = 0; i < diagram.Connectors.Count; i++)
{
diagram.Connectors[i].TargetDecoratorType = DecoratorType.None;
diagram.Connectors[i].Style.StrokeWidth = 1;
}
return linearLayout;
}
private void Diagram_BeginNodeRender(object sender, BeginNodeRenderEventArgs args)
{
var node = args.Item;
node.ShapeType = ShapeType.RoundedRectangle;
node.Style.StrokeWidth = 0;
if ((node.Content as Employee).Designation == "Manager")
node.Style.Brush = new SolidBrush(Color.Rgb(23, 132, 206));
else if ((node.Content as Employee).Designation == "CEO")
node.Style.Brush = new SolidBrush(Color.Rgb(201, 32, 61));
else if ((node.Content as Employee).Designation == "Team Lead" || (node.Content as Employee).Designation == "Co-ordinator")
node.Style.Brush = new SolidBrush(Color.Rgb(4, 142, 135));
else
node.Style.Brush = new SolidBrush(Color.Rgb(206, 98, 9));
node.Width = 144 * MainActivity.Factor;
node.Height = 60 * MainActivity.Factor;
AnnotationCollection annotations = new AnnotationCollection(node);
annotations.Add(new Annotation() { Content = (node.Content as Employee).Designation, FontSize = 14 * MainActivity.Factor, TextBrush = new SolidBrush(Color.White) });
node.Annotations = annotations;
}
private void ButtonTouch(object sender, EventArgs e)
{
ImageButtonView button = (ImageButtonView)sender;
for (int i = 0; i < buttons.ChildCount; i++)
{
ImageButtonView imageButton = buttons.GetChildAt(i) as ImageButtonView;
if (imageButton == null || imageButton.ImageId.TrimEnd('1').Equals(button.ImageId.TrimEnd('1')))
continue;
else if (imageButton.ImageId.EndsWith("1", StringComparison.InvariantCulture))
{
imageButton.ImageId = imageButton.ImageId.TrimEnd('1');
ImageView image = imageButton.GetChildAt(0) as ImageView;
string imageId = imageButton.ImageId + ".png";
if (imageId != null)
{
var imageData = LoadResource(imageId).ToArray();
image.SetImageBitmap(BitmapFactory.DecodeByteArray(imageData, 0, imageData.Length));
}
imageButton.SetWillNotDraw(true);
imageButton.SetWillNotDraw(false);
}
}
if (!button.ImageId.EndsWith("1", StringComparison.InvariantCulture))
{
SegmentType type = SegmentType.OrthoSegment;
switch (button.ImageId)
{
case "DiagramStraight":
type = SegmentType.StraightSegment;
break;
case "DiagramCurve":
type = SegmentType.CurveSegment;
break;
}
foreach (Connector connector in diagram.Connectors)
connector.SegmentType = type;
button.ImageId = button.ImageId + "1";
ImageView image = button.GetChildAt(0) as ImageView;
string imageId = button.ImageId + ".png";
if (imageId != null)
{
var imageData = LoadResource(imageId).ToArray();
image.SetImageBitmap(BitmapFactory.DecodeByteArray(imageData, 0, imageData.Length));
}
button.SetWillNotDraw(true);
button.SetWillNotDraw(false);
}
}
internal static MemoryStream LoadResource(string name)
{
MemoryStream aMem = new MemoryStream();
var assm = Assembly.GetExecutingAssembly();
var path = string.Format("SampleBrowser.Resources.drawable.{0}", name);
var aStream = assm.GetManifestResourceStream(path);
aStream.CopyTo(aMem);
return aMem;
}
internal class ImageButtonView : ViewGroup
{
internal ImageButtonView(Context context) : base(context)
{
SetBackgroundColor(Color.Transparent);
}
public string ImageId { get; internal set; }
protected override void OnDraw(Canvas canvas)
{
Paint strokePaint = new Paint();
strokePaint.SetStyle(Paint.Style.Stroke);
strokePaint.StrokeWidth = 5 * MainActivity.Factor;
strokePaint.Color = Color.Black;
canvas.DrawCircle(Width / 2, Height / 2, Width / 3, strokePaint);
Paint fillPaint = new Paint();
if (ImageId.EndsWith("1"))
fillPaint.Color = Color.Rgb(30, 144, 255);
else
fillPaint.Color = Color.White;
fillPaint.SetStyle(Paint.Style.Fill);
canvas.DrawCircle(Width / 2, Height / 2, Width / 3, fillPaint);
base.OnDraw(canvas);
}
protected override void OnLayout(bool changed, int l, int t, int r, int b)
{
}
}
public override View GetPropertyWindowLayout(Context context)
{
LinearLayout gridLinearLayout = new LinearLayout(context) { Orientation = Android.Widget.Orientation.Vertical };
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);
layoutParams.TopMargin = 25;
gridLinearLayout.LayoutParameters = layoutParams;
gridLinearLayout.SetBackgroundResource(Resource.Drawable.LinearLayout_Border);
int width = (int)(context.Resources.DisplayMetrics.WidthPixels - context.Resources.DisplayMetrics.Density) / 3;
LinearLayout linearLayout4 = new LinearLayout(context);
linearLayout4.Orientation = Android.Widget.Orientation.Horizontal;
linearLayout4.SetPadding(0, (int)(10 * currentDensity), 0, 0);
TextView selectText = new TextView(context)
{
Text = "Connector Style",
Gravity = GravityFlags.Start,
TextSize = 5 * currentDensity
};
selectText.SetWidth((int)(width * 0.4 * currentDensity));
Spinner spinner = new Spinner(context, SpinnerMode.Dialog);
spinner.SetMinimumHeight((int)(20 * currentDensity));
spinner.SetBackgroundColor(Color.Gray);
spinner.DropDownWidth = (int)(200 * currentDensity);
List<string> list = new List<string>();
list.Add("Default");
list.Add("Dashed");
list.Add("Dotted");
ArrayAdapter<string> dataAdapter = new ArrayAdapter<string>(context, Android.Resource.Layout.SimpleSpinnerItem, list);
dataAdapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
spinner.Adapter = dataAdapter;
spinner.ItemSelected += sType_spinner_ItemSelected;
spinner.SetGravity(GravityFlags.Start);
spinner.SetMinimumWidth(width - (int)(width * 0.4));
linearLayout4.AddView(selectText);
linearLayout4.AddView(spinner);
LinearLayout linearLayout3 = new LinearLayout(context);
linearLayout3.SetPadding(0, (int)(10 * currentDensity), 0, 0);
linearLayout3.SetMinimumHeight((int)(40 * currentDensity));
TextView connectorSize = new TextView(context)
{
Text = "Connector Size",
Gravity = GravityFlags.CenterVertical,
TextSize = 5 * currentDensity
};
connectorSize.SetMinimumHeight((int)(40 * currentDensity));
connectorSize.SetWidth((int)(width * 0.4 * currentDensity));
LinearLayout plusMinus = new LinearLayout(context);
plusMinus.LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.MatchParent);
plusMinus.SetMinimumWidth(width - (int)(width * 0.4));
ImageButton minusButton = new ImageButton(context);
minusButton.SetMinimumHeight((int)(40 * currentDensity));
minusButton.Click += MinusButton_Click;
string imageId = "diagramsub.png";
if (imageId != null)
{
var imageData = LoadResource(imageId).ToArray();
minusButton.SetImageBitmap(BitmapFactory.DecodeByteArray(imageData, 0, imageData.Length));
}
plusMinus.AddView(minusButton);
label = new TextView(context);
label.Text = "1";
label.TextAlignment = TextAlignment.Center;
label.Gravity = GravityFlags.Center;
label.SetMinimumHeight((int)(40 * currentDensity));
label.SetWidth((int)(40 * currentDensity));
plusMinus.AddView(label);
ImageButton plusButton = new ImageButton(context);
imageId = "diagramplus.png";
plusButton.Click += PlusButton_Click;
plusButton.SetMinimumHeight((int)(40 * currentDensity));
if (imageId != null)
{
var imageData = LoadResource(imageId).ToArray();
plusButton.SetImageBitmap(BitmapFactory.DecodeByteArray(imageData, 0, imageData.Length));
}
plusMinus.AddView(plusButton);
linearLayout3.AddView(connectorSize);
linearLayout3.AddView(plusMinus);
gridLinearLayout.AddView(linearLayout4);
gridLinearLayout.AddView(linearLayout3);
return gridLinearLayout;
}
private void PlusButton_Click(object sender, EventArgs e)
{
int value = int.Parse(label.Text);
if (value < 5)
{
foreach (Connector connector in diagram.Connectors)
connector.Style.StrokeWidth = (value + 1) * MainActivity.Factor;
label.Text = (value + 1).ToString();
}
}
private void MinusButton_Click(object sender, EventArgs e)
{
int value = int.Parse(label.Text);
if (value > 1)
{
foreach (Connector connector in diagram.Connectors)
connector.Style.StrokeWidth = (value - 1) * MainActivity.Factor;
label.Text = (value - 1).ToString();
}
}
void sType_spinner_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
{
Spinner spinner = (Spinner)sender;
string selectedItem = spinner.GetItemAtPosition(e.Position).ToString();
StrokeStyle strokeStyle = StrokeStyle.Default;
switch (selectedItem)
{
case "Default":
strokeStyle = StrokeStyle.Default;
break;
case "Dashed":
strokeStyle = StrokeStyle.Dashed;
break;
case "Dotted":
strokeStyle = StrokeStyle.Dotted;
break;
}
foreach (Connector connector in diagram.Connectors)
{
connector.Style.StrokeStyle = strokeStyle;
}
}
void sType_spinner_ItemSelected1(object sender, AdapterView.ItemSelectedEventArgs e)
{
Spinner spinner = (Spinner)sender;
string selectedItem = spinner.GetItemAtPosition(e.Position).ToString();
int strokeWidth = int.Parse(selectedItem);
if (strokeWidth > 0)
{
foreach (Connector connector in diagram.Connectors)
{
connector.Style.StrokeWidth = strokeWidth;
}
}
}
private Connector AddConnector(Node sourceNode, Node targetNode)
{
var c1 = new Connector(m_context);
c1.SourceNode = sourceNode;
c1.TargetNode = targetNode;
c1.Style.StrokeWidth = 1 * currentDensity;
c1.Style.StrokeBrush = new SolidBrush(Color.Rgb(127, 132, 133));
c1.TargetDecoratorType = DecoratorType.None;
return c1;
}
Node AddNode(int x, int y, string text, Color nodeColor)
{
Node node = new Node(m_context);
node = new Node(x * MainActivity.Factor, y * MainActivity.Factor, 144 * MainActivity.Factor, 108 * MainActivity.Factor, m_context);
node.ShapeType = ShapeType.RoundedRectangle;
node.Style.StrokeWidth = 1;
node.Style.Brush = new SolidBrush(nodeColor);
node.Style.StrokeBrush = new SolidBrush(Color.Rgb(127, 132, 133));
node.Annotations.Add(new Annotation() { Content = text, FontSize = 20 * MainActivity.Factor, TextBrush = new SolidBrush(Color.White) });
return node;
}
public override void Destroy()
{
if (diagram != null)
diagram.Dispose();
base.Destroy();
}
private void Diagram_Loaded(object sender)
{
diagram.BringToView(diagram.Nodes[0]);
}
//Creates the Node with Specified input
Node DrawNode(float x, float y, float w, float h, ShapeType shape)
{
var node = new Node(m_context);
node.OffsetX = x;
node.OffsetY = y;
node.Width = w;
node.Height = h;
node.ShapeType = shape;
node.Style.StrokeWidth = 1 * 2 * MainActivity.Factor;
node.CornerRadius = 30 * 2 * MainActivity.Factor;
node.Style.Brush = new SolidBrush(Color.Rgb(255, 129, 0));
node.Ports.Add(new Port() { NodeOffsetX = 0.5, NodeOffsetY = 0, IsVisible = false });
node.Ports.Add(new Port() { NodeOffsetX = 1, NodeOffsetY = 0.5, IsVisible = false });
node.Ports.Add(new Port() { NodeOffsetX = 0.5, NodeOffsetY = 1, IsVisible = false });
node.Ports.Add(new Port() { NodeOffsetX = 0, NodeOffsetY = 0.5, IsVisible = false });
return node;
}
Node DrawCustomShape(float x, float y)
{
var node = new Node(m_context);
node.OffsetX = x * MainActivity.Factor;
node.OffsetY = y * MainActivity.Factor;
node.Width = 50 * 2 * MainActivity.Factor; node.Height = 40 * 2 * MainActivity.Factor;
SfGraphics grap = new SfGraphics();
Pen stroke = new Pen();
stroke.Brush = new SolidBrush(Color.Transparent);
stroke.StrokeWidth = 3 * MainActivity.Factor;
stroke.StrokeBrush = new SolidBrush(Color.Rgb(24, 161, 237));
grap.DrawEllipse(stroke, new System.Drawing.Rectangle(10, 0, 20, 20));
grap.DrawArc(stroke, 0, 20, 40, 40, 180, 180);
node.UpdateSfGraphics(grap);
return node;
}
Connector DrawConnector(Node Src, Node Trgt, Port srcport, Port trgtport, SegmentType type, Color strokeColor, StrokeStyle style, DecoratorType decorator, Color fillColor, Color strokeFill, float sw)
{
var Conn = new Connector(m_context, Src, Trgt);
Conn.SourcePort = srcport;
Conn.TargetPort = trgtport;
Conn.SegmentType = type;
Conn.TargetDecoratorType = decorator;
Conn.TargetDecoratorStyle.Fill = fillColor;
Conn.TargetDecoratorStyle.StrokeColor = strokeFill;
Conn.Style.StrokeWidth = 1 * 2 * MainActivity.Factor;
Conn.Style.StrokeBrush = new SolidBrush(strokeColor);
Conn.Style.StrokeStyle = style;
Conn.TargetDecoratorStyle.Size = sw;
Conn.TargetDecoratorStyle.StrokeWidth = 2 * 2 * MainActivity.Factor;
return Conn;
}
public class Employee
{
public string ParentId { get; set; }
public string Name { get; set; }
public string Designation { get; set; }
public string EmpId { get; set; }
}
}
}