-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComponentControl.cs
128 lines (103 loc) · 4.53 KB
/
ComponentControl.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using VisualAzureStudio.Models.Components;
using VisualAzureStudio.Models.Connections;
using VisualAzureStudio.Properties;
namespace VisualAzureStudio
{
public partial class ComponentControl : UserControl
{
public event EventHandler Selected;
public event EventHandler Deleted;
public ComponentControl()
{
InitializeComponent();
}
private Control activeControl;
private Point previousLocation;
private void componentControl_MouseDown(object sender, MouseEventArgs e)
{
Selected?.Invoke(this, new EventArgs());
if (e.Button != MouseButtons.Left) {
return;
}
switch (sender) {
case ComponentControl _:
activeControl = sender as Control;
break;
case PictureBox _:
case TextBox _:
case Label _:
activeControl = (sender as Control).Parent;
break;
}
if (activeControl.GetType() != typeof(ComponentControl)) {
Debugger.Break();
}
previousLocation = e.Location;
}
private void componentControl_MouseMove(object sender, MouseEventArgs e)
{
if (activeControl == null || activeControl != sender) {
return;
}
Point location = activeControl.Location;
location.Offset(e.Location.X - previousLocation.X, e.Location.Y - previousLocation.Y);
activeControl.Location = location;
Parent.Invalidate();
}
private void componentControl_MouseUp(object sender, MouseEventArgs e)
{
activeControl = null;
}
private void ComponentControl_LocationChanged(object sender, EventArgs e)
{
((ComponentBase)Tag).Location = Location;
}
private void ContextMenu_Opening(object sender, System.ComponentModel.CancelEventArgs e)
{
// determine allowed connection types and populate the Connect To menu
ConnectToMenuItem.DropDownItems.Clear();
ComponentBase tagComponent = (ComponentBase)Tag;
foreach (ComponentBase component in Form1.design.Components.Where(c => c != Tag)) {
// skip if connection is not allowed
List<AllowedConnection> allowedConnections = AllowedConnections.Allowed.Where(a => a.Item1 == tagComponent.GetType() && a.Item2 == component.GetType() || a.Item1 == component.GetType() && a.Item2 == tagComponent.GetType()).ToList();
if (allowedConnections.Count == 0) {
continue;
}
// skip if connection already exists
if (Form1.design.Connections.Any(c => c.Item1Id == component.Id && c.Item2Id == tagComponent.Id || c.Item1Id == tagComponent.Id && c.Item2Id == component.Id)) {
continue;
}
ToolStripButton toolStripButton = new ToolStripButton {
Text = component.Name,
Tag = new Tuple<ComponentBase, ComponentBase, Type>(component, tagComponent, allowedConnections.First().Item3),
Width = 128
};
toolStripButton.Click += ComponentControl_Click;
ConnectToMenuItem.DropDownItems.Add(toolStripButton);
}
}
private void ComponentControl_Click(object sender, EventArgs e)
{
Tuple<ComponentBase, ComponentBase, Type> items = (sender as ToolStripItem).Tag as Tuple<ComponentBase, ComponentBase, Type>;
ConnectionBase newConnection = (ConnectionBase)Activator.CreateInstance(items.Item3);
newConnection.Item1Id = items.Item1.Id;
newConnection.Item2Id = items.Item2.Id;
Form1.design.Connections.Add(newConnection);
Form1.design.IsDirty = true;
Parent.Invalidate();
}
private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
{
ComponentBase component = Tag as ComponentBase;
if (MessageBox.Show($"Delete {component.TypeDescription} \"{component.Name}\"?", Resources.ProgramName, MessageBoxButtons.YesNo) == DialogResult.Yes) {
Deleted?.Invoke(this, new EventArgs());
}
}
}
}