-
Notifications
You must be signed in to change notification settings - Fork 1
/
Extensions.cs
114 lines (94 loc) · 4.46 KB
/
Extensions.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
using System;
using System.Drawing;
using System.Linq;
using VisualAzureStudio.Models;
using VisualAzureStudio.Models.Connections;
namespace VisualAzureStudio
{
internal static class Extensions
{
internal static Point OffsetPoint(this Point point, int dx, int dy)
{
return new Point(point.X + dx, point.Y + dy);
}
internal static ConnectionBase FindByPoint(this Design design, Point point, int lineWidth)
{
if (design?.Connections == null) {
return null;
}
int lineWidthOffset = lineWidth / 2;
foreach (ConnectionBase connection in design.Connections) {
Point corner = new Point(connection.End.X, connection.Start.Y);
// test first segment (horizontal)
int top = connection.Start.Y - lineWidthOffset;
int bottom = top + lineWidth;
int left = Math.Min(connection.Start.X, connection.End.X) - lineWidthOffset;
int right = Math.Max(connection.Start.X, connection.End.X) + lineWidthOffset;
if (top <= point.Y && bottom >= point.Y && left <= point.X && right >= point.X) {
return connection;
}
// test second segment (vertical)
top = Math.Min(connection.Start.Y, connection.End.Y) - lineWidthOffset;
bottom = Math.Max(connection.Start.Y, connection.End.Y) + lineWidthOffset;
left = connection.End.X - lineWidthOffset;
right = left + lineWidth;
if (top <= point.Y && bottom >= point.Y && left <= point.X && right >= point.X) {
return connection;
}
}
return null;
}
/// <summary>
/// Calculates and sets the locations of the connection coordinates in the design.
/// </summary>
/// <param name="design">Design containing the connection coordinates to calculate.</param>
internal static void CalculateLines(this Design design)
{
foreach (ConnectionBase connection in design.Connections) {
connection.Start = design.Components.First(c => c.Id == connection.Item1Id).Location.OffsetPoint(75, 34);
connection.End = design.Components.First(c => c.Id == connection.Item2Id).Location.OffsetPoint(75, 34);
}
}
/// <summary>
/// Draws the connection lines of the given design to to the given graphics object.
/// </summary>
/// <param name="design">Design containing the connections to draw.</param>
/// <param name="graphics">Graphics object to draw the lines on.</param>
internal static void DrawLines(this Design design, Graphics graphics)
{
Pen pen = new Pen(Color.Silver, 5);
foreach (ConnectionBase connection in design.Connections) {
graphics.DrawLines(pen, new[] { connection.Start, new Point(connection.End.X, connection.Start.Y), connection.End });
}
}
internal static string GetCommonResourceGroup(this Design design)
{
if (design.Components == null || design.Components.Count == 0) {
return "MyResourceGroup";
}
string commonResourceGroup = design.Components[0].ResourceGroup;
if (design.Components.Count == 1) {
return commonResourceGroup;
}
if (design.Components.Skip(1).Any(component => component.ResourceGroup != commonResourceGroup)) {
return "MyResourceGroup";
}
return commonResourceGroup;
}
/// <summary>
/// Returns the most popular region used by all component referencing the given resource group.
/// </summary>
/// <param name="design">Design containing the components to get region from.</param>
/// <param name="resourceGroup">Name of the resource group to filter to.</param>
/// <returns></returns>
internal static Regions GetMostPopularRegion(this Design design, string resourceGroup)
{
return design.Components?
.Where(c => c.ResourceGroup == resourceGroup)
.GroupBy(c => c.Region)
.OrderByDescending(c => c.Count())
.FirstOrDefault()?
.FirstOrDefault()?.Region ?? Regions.EastUS;
}
}
}