forked from iodes/GSharp
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBlockUtils.cs
282 lines (243 loc) · 10.7 KB
/
BlockUtils.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
using GSharp.Base.Objects;
using GSharp.Base.Objects.Customs;
using GSharp.Base.Objects.Logics;
using GSharp.Base.Objects.Numbers;
using GSharp.Base.Objects.Strings;
using GSharp.Base.Utilities;
using GSharp.Extension;
using GSharp.Extension.Optionals;
using GSharp.Graphic.Blocks;
using GSharp.Graphic.Holes;
using GSharp.Graphic.Objects;
using GSharp.Graphic.Objects.Customs;
using GSharp.Graphic.Objects.Logics;
using GSharp.Graphic.Objects.Numbers;
using GSharp.Graphic.Objects.Strings;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Xml;
namespace GSharp.Graphic
{
/// <summary>
/// Interface로 구현된 IVariable, IModule 등과 같은 객체를 생성하는 등의 몇가지 함수를 모아둔 클래스
/// </summary>
public static class BlockUtils
{
/// <summary>
/// 모듈 블럭들에게 필요한 구멍을 자동으로 찾아서 생성하고 위치시킵니다.
/// </summary>
/// <param name="Command">모듈을 호출하기 위해 필요한 GCommand 객체</param>
/// <param name="Content">생성한 구멍과 내용을 추가할 Panel 객체</param>
/// <returns>생성한 모든 구멍을 반환</returns>
public static List<BaseHole> SetContent(GCommand Command, Panel Content, Brush brush = null)
{
// 기존 패널에 있는 내용 삭제
Content.Children.Clear();
// 반환을 위한 구멍 목록 생성
var holeList = new List<BaseHole>();
// Command의 FriendlyName에서 구멍 파싱
var start = -1;
var last = 0;
for (int i = 0; i < Command.FriendlyName.Length; i++)
{
var chr = Command.FriendlyName[i];
if (chr == '{')
{
start = i;
}
else if (chr == '}' && start >= 0)
{
var text = Command.FriendlyName.Substring(last, start - last);
var holeNumber = Command.FriendlyName.Substring(start + 1, i - start - 1);
int number;
if (int.TryParse(holeNumber, out number) && 0 <= number && number < Command.Optionals.Length)
{
BaseHole hole = CreateHole(Command.Optionals[number].ObjectType);
if (brush == null)
{
brush = new BrushConverter().ConvertFromString("#086748") as Brush;
}
hole.Foreground = brush;
hole.VerticalAlignment = VerticalAlignment.Center;
Content.Children.Add(new TextBlock
{
Text = text,
FontWeight = FontWeights.Bold,
Foreground = Brushes.White,
VerticalAlignment = VerticalAlignment.Center
});
Content.Children.Add(hole);
holeList.Add(hole);
start = -1;
last = i + 1;
}
}
}
var lastText = Command.FriendlyName.Substring(last);
Content.Children.Add(new TextBlock
{
Text = lastText,
FontWeight = FontWeights.Bold,
Foreground = Brushes.White,
VerticalAlignment = VerticalAlignment.Center
});
// 구멍 목록 반환
return holeList;
}
/// <summary>
/// 객체를 끼울 수 있는 구멍을 만드는 함수
/// </summary>
/// <param name="holeType">구멍에 끼울 객체의 자료형</param>
/// <returns>
/// 매개변수의 자료형이 string이면 StringHole을 반환
/// 매개변수의 자료형이 bool이면 LogicHole을 반환
/// 매개변수의 자료형이 숫자 형태인 경우 NumberHole을 반환
/// 이 외의 경우 CustomHole을 반환합니다.
/// </returns>
public static BaseHole CreateHole(Type holeType)
{
// 자료형이 string인 경우
if (holeType == typeof(string))
{
return new StringHole
{
Foreground = new BrushConverter().ConvertFromString("#086748") as Brush
};
}
// 자료형이 bool인경우
if (holeType == typeof(bool))
{
return new LogicHole();
}
// 자료형이 숫자 형태인 경우
if (GSharpUtils.numberTypes.Contains(holeType))
{
return new NumberHole();
}
// 그 외의 경우
return new CustomHole(holeType);
}
/// <summary>
/// 변수 블럭을 생성해 주는 함수
/// </summary>
/// <param name="variableName">C# 형태에서의 변수의 이름</param>
/// <param name="variableType">매개변수의 자료형</param>
/// <param name="friendlyName">블럭으로 노출될 변수의 이름</param>
/// <returns>
/// 매개변수의 자료형이 string이면 StringVariableBlock을 반환
/// 매개변수의 자료형이 bool이면 LogicVariableBlock을 반환
/// 매개변수의 자료형이 숫자 형태인 경우 NumberVariableBlock을 반환
/// 이 외의 경우 CustomVariableBlock을 반환합니다.
/// </returns>
public static VariableBlock CreateVariableBlock(string variableName, string friendlyName = null)
{
GVariable variable = GSharpUtils.CreateGVariable(variableName);
if (friendlyName == null)
{
friendlyName = variableName;
}
return new VariableBlock(friendlyName, variable);
}
/// <summary>
/// 변수 블럭을 생성해 주는 함수
/// </summary>
/// <param name="variable">IVariable 객체</param>
/// <param name="friendlyName">블럭으로 노출될 변수의 이름</param>
/// <returns>
/// variable이 GStringVariable이면 StringVariableBlock을 반환
/// variable이 GLogicVariable이면 LogicVariableBlock을 반환
/// variable이 GNumberVariable이면 NumberVariableBlock을 반환
/// 이 외의 경우 CustomVariableBlock을 반환합니다.
/// </returns>
public static VariableBlock CreateVariableBlock(GVariable variable, string friendlyName = null)
{
if (friendlyName == null)
{
friendlyName = variable.Name;
}
return new VariableBlock(friendlyName, variable);
}
public static void SaveGCommand(XmlWriter writer, GCommand command)
{
writer.WriteAttributeString("NamespaceName", command.NamespaceName);
writer.WriteAttributeString("MethodName", command.MethodName);
writer.WriteAttributeString("FriendlyName", command.FriendlyName);
writer.WriteAttributeString("MethodType", command.MethodType.ToString());
writer.WriteAttributeString("ObjectType", command.ObjectType.AssemblyQualifiedName);
if (command.Optionals != null)
{
writer.WriteStartElement("Optionals");
foreach (var option in command.Optionals)
{
writer.WriteStartElement("Optional");
writer.WriteAttributeString("Name", option.Name);
writer.WriteAttributeString("FullName", option.FullName);
writer.WriteAttributeString("FriendlyName", option.FriendlyName);
writer.WriteAttributeString("ObjectType", option.ObjectType.AssemblyQualifiedName);
writer.WriteEndElement();
}
writer.WriteEndElement();
}
}
public static GCommand LoadGCommand(XmlElement element)
{
var namespaceName = element.GetAttribute("NamespaceName");
var methodName = element.GetAttribute("MethodName");
var friendlyName = element.GetAttribute("FriendlyName");
var methodType = element.GetAttribute("MethodType");
var objectTypeString = element.GetAttribute("ObjectType");
var objectType = Type.GetType(objectTypeString);
GCommand.CommandType methodEnum;
if (!Enum.TryParse(methodType, out methodEnum))
{
throw new Exception("CommandType 로드에 실패했습니다.");
}
if (objectType == null)
{
throw new Exception("ObjectType 로드에 실패했습니다.");
}
var options = new List<GOptional>();
foreach (XmlElement option in element.SelectNodes("Optionals/Optional"))
{
var optionName = option.GetAttribute("Name");
var optionFullName = option.GetAttribute("FullName");
var optionFriendlyName = option.GetAttribute("FriendlyName");
var optionObjectTypeString = option.GetAttribute("ObjectType");
var optionObjectType = Type.GetType(optionObjectTypeString);
if (optionObjectType == null)
{
throw new Exception("Option의 ObjectType 로드에 실패했습니다.");
}
options.Add(new GOptional(optionName, optionFullName, optionFriendlyName, optionObjectType));
}
return new GCommand(namespaceName, methodName, friendlyName, objectType, methodEnum, options.ToArray());
}
public static void SaveChildBlocks(XmlWriter writer, BaseBlock block, string tagName = "ChildBlocks")
{
writer.WriteStartElement(tagName);
block?.SaveXML(writer);
writer.WriteEndElement();
}
public static void ConnectToHole(BaseHole baseHole, BaseBlock baseBlock)
{
if (baseHole is BaseObjectHole)
{
(baseHole as BaseObjectHole).BaseObjectBlock = baseBlock as ObjectBlock;
}
else if (baseHole is SettableObjectHole)
{
(baseHole as SettableObjectHole).SettableObjectBlock = baseBlock as SettableObjectBlock;
}
else if (baseHole is NextConnectHole)
{
(baseHole as NextConnectHole).StatementBlock = baseBlock as StatementBlock;
}
}
}
}