-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClassDiagramEditor.cs
executable file
·618 lines (519 loc) · 16.9 KB
/
ClassDiagramEditor.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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
using UnityEngine;
using UnityEditor;
using System.Collections;
public class ClassDiagramEditor: DiagramEditorWindow
{
override protected DiagramRoot getDiagramRoot (GameObject activeGameObject)
{
return activeGameObject.GetComponent<ClassDiagramRoot> ();
}
override protected string GetNullDiagramRootMessage ()
{
return "ClassDiagramRoot is not found.";
}
private Texture2D texAdd = loadTexture ("UnityClassDiagram/icons/add.png");
private Texture2D texRemove = loadTexture ("UnityClassDiagram/icons/remove.png");
private Texture2D texRemoveMini = loadTexture ("UnityClassDiagram/icons/removeMini.png");
private Texture2D texSuper = loadTexture ("UnityClassDiagram/icons/triangle_tool.png");
private Texture2D texNoImage = loadTexture ("UnityClassDiagram/icons/no_image.png");
private Texture2D texNoImage16 = loadTexture ("UnityClassDiagram/icons/no_image16.png");
private Texture2D texTriangle = loadTexture ("UnityClassDiagram/icons/triangle.png");
private Texture2D texComposite = loadTexture ("UnityClassDiagram/icons/composite.png");
private Texture2D texCompositeTool = loadTexture ("UnityClassDiagram/icons/composite_tool.png");
private Texture2D texReference = loadTexture ("UnityClassDiagram/icons/reference.png");
private Texture2D texReferenceTool = loadTexture ("UnityClassDiagram/icons/reference_tool.png");
private Texture2D lineTex = null;
private ClassDiagramRoot cuurentClassData;
private ClassNode focusClass = null;
private ClassNode refModeClass = null;
private ClassNode refModeTargetClass = null;
private int focusWidowId = -1;
private int mode = 0;
private const int mode_draw = 0;
private const int mode_ref_gen = 1;
private const int mode_ref_ref = 2;
private const int mode_ref_com = 3;
public static Texture2D loadTexture (string relativePath)
{
return (Texture2D)AssetDatabase.LoadAssetAtPath (Config.INSTALLED_PATH + relativePath, typeof(Texture2D));
}
public ClassDiagramEditor ()
{
}
private void changeModeDraw ()
{
wantsMouseMove = false;
//Debug.Log ("changeModeDraw" + " wantsMouseMove=" + wantsMouseMove);
refModeClass = null;
refModeTargetClass = null;
mode = mode_draw;
}
private void changeModeRef (ClassNode clazz, int refType)
{
wantsMouseMove = true;
//Debug.Log ("changeModeRef" + " wantsMouseMove=" + wantsMouseMove);
refModeClass = clazz;
mode = refType;
}
static ClassDiagramEditor window = null;
[MenuItem("Assets/Create/ClassDiagram")]
static void Create ()
{
GameObject gameObject = new GameObject ();
gameObject.AddComponent ("ClassDiagramRoot");
string newPrefabPath = "Assets/ClassDiagram";
string extension = ".prefab";
string newPath = newPrefabPath + extension;
GameObject newPrefab = null;
for (int i = 0; i < System.Int16.MaxValue; i++) {
if (i == 0) {
newPath = newPrefabPath + extension;
} else {
newPath = newPrefabPath + " " + i + extension;
}
if (!System.IO.File.Exists (newPath)) {
newPrefab = PrefabUtility.CreatePrefab (newPath, gameObject);
break;
}
}
Object.DestroyImmediate (gameObject, true);
Selection.activeObject = newPrefab;
init ();
}
[MenuItem("Window/ClassDiagramEditor")]
static void init ()
{
if (window) {
//window.Close ();
} else {
window = EditorWindow.GetWindow<ClassDiagramEditor> ();
}
}
void drawWindow (int id)
{
ClassNode clazz = (ClassNode)cuurentClassData.classes.GetValue (id);
Rect boxRect = new Rect (0, 16, clazz.rect.width, clazz.rect.height - 16);
GUI.Box (boxRect, "");
if ((Event.current.button == 0) && (Event.current.type == EventType.MouseDown)) {
focusWidowId = id;
if (mode != mode_draw) {
if (refModeClass != null && refModeClass != clazz) {
refModeTargetClass = clazz;
}
}
}
string iconPath = clazz.iconPath;
Texture2D texIcon = null;
Rect iconRect = new Rect (6, 22, 32, 32);
if (iconPath != null) {
texIcon = loadTexture (iconPath);
}
if (texIcon == null) {
texIcon = texNoImage;
}
GUIStyle iconStyle = new GUIStyle (GUIStyle.none);
iconStyle.normal.background = texIcon;
if (GUI.Button (iconRect, "", iconStyle)) {
string path = EditorUtility.OpenFilePanel ("Select Icon", "Assets", "");
if (path != null) {
if (Application.dataPath.Length < path.Length) {
path = path.Substring (Application.dataPath.Length);
char[] chTrims = {'/', '\\'};
path = path.TrimStart (chTrims);
clazz.iconPath = path;
}
}
}
Rect textRect = new Rect (iconRect.x + iconRect.width + 10, iconRect.y + 8, clazz.rect.width - (iconRect.x + iconRect.width + 8) - 20, 16);
if (id == focusWidowId) {
clazz.name = GUI.TextField (textRect, clazz.name);
} else {
GUI.Label (textRect, clazz.name);
}
float bx = clazz.rect.width - 18;
if (id == focusWidowId) {
GUIStyle style = new GUIStyle (GUIStyle.none);
style.normal.background = texSuper;
Rect btnRect = new Rect (0, 0, 16, 16);
if (GUI.Button (btnRect, "", style)) {
if (clazz.superClassName != null && 0 < clazz.superClassName.Length) {
clazz.superClassName = null;
} else {
changeModeRef (clazz, mode_ref_gen);
}
}
style.normal.background = texCompositeTool;
btnRect.x += 18;
if (GUI.Button (btnRect, "", style)) {
changeModeRef (clazz, mode_ref_com);
}
style.normal.background = texReferenceTool;
btnRect.x += 18;
if (GUI.Button (btnRect, "", style)) {
changeModeRef (clazz, mode_ref_ref);
}
style.normal.background = texAdd;
btnRect.x += 18;
if (GUI.Button (btnRect, "", style)) {
clazz.attributes.Add (new Attribute ());
}
style.normal.background = texRemove;
btnRect.x = bx;
btnRect.y = 0;
if (GUI.Button (btnRect, "", style)) {
System.Collections.Generic.List<ClassNode> classList = new System.Collections.Generic.List<ClassNode> (cuurentClassData.classes);
classList.Remove (clazz);
cuurentClassData.classes = classList.ToArray ();
}
}
for (int index = 0; index < clazz.attributes.Count; index++) {
Attribute attr = (Attribute)clazz.attributes[index];
float nwidth = 50;
float twidth = 90;
float y = 60 + 18 * index;
Rect irect = new Rect (12, y, 16, 16);
Rect nrect = new Rect (irect.x + irect.width + 4, y, nwidth, 16);
Rect crect = new Rect (nrect.x + nwidth, y, 8, 16);
Rect trect = new Rect (crect.x + crect.width - 10, y, twidth, 16);
texIcon = null;
string attrIconPath = attr.iconPath;
if (attrIconPath != null) {
texIcon = loadTexture (attrIconPath);
}
if (texIcon == null) {
texIcon = texNoImage16;
}
GUIStyle attrIconStyle = new GUIStyle (GUIStyle.none);
attrIconStyle.normal.background = texIcon;
if (GUI.Button (irect, "", attrIconStyle)) {
string path = EditorUtility.OpenFilePanel ("Select Icon", "Assets", "");
if (path != null) {
if (Application.dataPath.Length < path.Length) {
path = path.Substring (Application.dataPath.Length);
char[] chTrims = {'/', '\\'};
path = path.TrimStart (chTrims);
attr.iconPath = path;
}
}
}
if (id == focusWidowId) {
attr.name = GUI.TextField (nrect, attr.name);
GUI.Label (crect, ":");
attr.type = DrawTypeField (trect, attr.type);
} else {
GUI.Label (nrect, attr.name);
GUI.Label (crect, ":");
DrawTypeField (trect, attr.type);
//GUI.Label (trect, attr.type);
}
if (id == focusWidowId) {
GUIStyle style = new GUIStyle (GUIStyle.none);
style.normal.background = texRemoveMini;
if (GUI.Button (new Rect (trect.x + trect.width - 4, y, 13, 16), "", style)) {
clazz.attributes.Remove (attr);
}
}
}
int space = 12;
if (0 < clazz.attributes.Count) {
space = 20;
}
if (clazz.rect.height - boxRect.y - space < clazz.attributes.Count * 18 + iconRect.height) {
clazz.rect.height = boxRect.y + clazz.attributes.Count * 18 + iconRect.height + space;
} else if (0 < (clazz.rect.height - boxRect.y - space) - (clazz.attributes.Count * 18 + iconRect.height)) {
clazz.rect.height = clazz.attributes.Count * 18 + iconRect.height + boxRect.y + space;
}
GUI.DragWindow ();
}
string DrawTypeField (Rect rect, string type)
{
string[] types = null;
if (cuurentClassData != null) {
types = cuurentClassData.types;
} else {
types = new string[]{"int" , "string"};
}
for (int i = 0; i < types.Length; i++) {
if (types [i] == type) {
int selection = EditorGUI.Popup (rect, i, types);
return types [selection];
}
}
return null;
}
override protected void OnGUIimpl ()
{
Event e = Event.current;
switch (e.type) {
case EventType.keyDown:
{
if (mode != mode_draw) {
changeModeDraw ();
}
break;
}
case EventType.MouseMove:
{
Repaint ();
break;
}
}
GameObject gameObject = Selection.activeGameObject;
if (!gameObject) {
GUI.Label (new Rect (20, 20, 300, 100), "ClassDiagramRoot is not found.");
return;
}
ClassDiagramRoot classData = gameObject.GetComponent<ClassDiagramRoot> ();
if (!classData) {
GUI.Label (new Rect (20, 20, 300, 100), "Select ClassDiagramRoot.");
return;
}
if (cuurentClassData != classData) {
focusClass = null;
focusWidowId = -1;
mode = mode_draw;
refModeClass = null;
refModeTargetClass = null;
}
cuurentClassData = classData;
if (mode == mode_draw) {
if ((Event.current.button == 0) && (Event.current.type == EventType.MouseDown)) {
focusWidowId = -1;
focusClass = null;
}
}
if (GUI.Button (new Rect (10, 35, 25, 18), "+")) {
System.Collections.Generic.List<ClassNode> classList = new System.Collections.Generic.List<ClassNode> (classData.classes);
ClassNode newClass = new ClassNode ();
{
string newName = newClass.name;
int size = classData.classes.Length;
if (0 < size) {
string nameTemp = newName;
int index = 1;
while (true) {
bool exist = false;
foreach (ClassNode c in classList) {
if (c.name == nameTemp) {
exist = true;
break;
}
}
if (exist) {
nameTemp = newName + " " + index;
index ++;
} else {
newName = nameTemp;
break;
}
}
}
newClass.name = newName;
}
{
Rect newRect = new Rect (newClass.rect);
int size = classData.classes.Length;
if (0 < size) {
while (true) {
bool exist = false;
foreach (ClassNode c in classList) {
if (c.rect.x == newRect.x && c.rect.y == newRect.y) {
exist = true;
break;
}
}
if (exist) {
newRect.x += 20;
newRect.y += 20;
} else {
break;
}
}
}
newClass.rect = newRect;
}
classList.Add (newClass);
cuurentClassData.classes = classList.ToArray ();
}
GUIStyle windowStyle = new GUIStyle (GUIStyle.none);
BeginWindows ();
for (int index = 0; index < cuurentClassData.classes.Length; index++) {
ClassNode clazz = (ClassNode)cuurentClassData.classes.GetValue (index);
clazz.rect = GUI.Window (index, clazz.rect, drawWindow, "", windowStyle);
if (focusWidowId == index) {
focusClass = clazz;
}
if (clazz.superClassName != null) {
ClassNode target = clazz.GetSuperClassNode (cuurentClassData);
Rect clazzRect = new Rect (clazz.rect.x, clazz.rect.y + 16, clazz.rect.width, clazz.rect.height - 16);
if (target != null) {
drawLine (clazzRect
, new Rect (target.rect.x, target.rect.y + 16, target.rect.width, target.rect.height - 16), new Color (0.3f, 0.4f, 0.7f), false, null, texTriangle);
} else {
if (clazz.superClassName != null && 0 < clazz.superClassName.Length) {
drawLine (clazzRect
, new Rect (clazzRect.x, clazzRect.y - 50, clazzRect.width, clazzRect.height), new Color (0.3f, 0.4f, 0.7f), true, null, texTriangle);
}
}
}
}
EndWindows ();
if (mode != mode_draw) {
Vector2 mouse = Event.current.mousePosition;
drawLine (new Rect (refModeClass.rect.x, refModeClass.rect.y + 16, refModeClass.rect.width, refModeClass.rect.height - 16)
, new Rect (mouse.x - 5, mouse.y - 5, 5, 5), new Color (0.3f, 0.4f, 0.7f), true, GetSrcAnchorTex (), GetDestAnchorTex ());
if (refModeTargetClass != null) {
switch (mode) {
case mode_ref_gen:{
refModeClass.superClassName = refModeTargetClass.name;
break;
}
case mode_ref_com:{
break;
}
case mode_ref_ref:{
break;
}
}
changeModeDraw ();
}
}
}
Texture2D GetSrcAnchorTex ()
{
switch (mode) {
case mode_ref_gen:{
return null;
}
case mode_ref_com:{
return texComposite;
}
case mode_ref_ref:{
return null;
}
}
return null;
}
Texture2D GetDestAnchorTex ()
{
switch (mode) {
case mode_ref_gen:{
return texTriangle;
}
case mode_ref_com:{
return null;
}
case mode_ref_ref:{
return texReference;
}
}
return null;
}
Vector2 GetAnchorPos (Vector2 pointA, Rect wr2)
{
Vector2 pointB = new Vector2 (wr2.x + wr2.width / 2, wr2.y + wr2.height / 2);
Vector2 arrowPos = new Vector2 ();
float left = wr2.x;
float right = wr2.x + wr2.width;
float top = wr2.y;
float bottom = wr2.y + wr2.height;
Vector2 topLeft = new Vector2 (wr2.x, wr2.y);
Vector2 topRight = new Vector2 (wr2.x + wr2.width, wr2.y);
Vector2 bottomLeft = new Vector2 (wr2.x, wr2.y + wr2.height);
Vector2 bottomRight = new Vector2 (wr2.x + wr2.width, wr2.y + wr2.height);
float ans1 = calcPosition (topLeft, bottomRight, pointA);
float ans2 = calcPosition (bottomLeft, topRight, pointA);
if (0 <= ans1 && 0 <= ans2) {
//Debug.Log("bottom");
float w1 = Mathf.Abs (pointB.y - pointA.y);
float w2 = Mathf.Abs (pointB.y - bottom);
float h1 = pointB.x - pointA.x;
float h2 = h1 * w2 / w1;
arrowPos.y = bottom;
arrowPos.x = pointB.x - h2;
} else if (0 > ans1 && 0 <= ans2) {
//Debug.Log("right");
float w1 = Mathf.Abs (pointB.x - pointA.x);
float w2 = Mathf.Abs (pointB.x - right);
float h1 = pointB.y - pointA.y;
float h2 = h1 * w2 / w1;
arrowPos.x = right;
arrowPos.y = pointB.y - h2;
} else if (0 <= ans1 && 0 > ans2) {
//Debug.Log("left");
float w1 = Mathf.Abs (pointB.x - pointA.x);
float w2 = Mathf.Abs (pointB.x - left);
float h1 = pointB.y - pointA.y;
float h2 = h1 * w2 / w1;
arrowPos.x = left;
arrowPos.y = pointB.y - h2;
} else if (0 > ans1 && 0 > ans2) {
//Debug.Log("top");
float w1 = Mathf.Abs (pointB.y - pointA.y);
float w2 = Mathf.Abs (pointB.y - top);
float h1 = pointB.x - pointA.x;
float h2 = h1 * w2 / w1;
arrowPos.y = top;
arrowPos.x = pointB.x - h2;
}
return arrowPos;
}
void drawLine (Rect wr, Rect wr2, Color color, bool mouse, Texture2D srcAnchor, Texture2D destAnchor)
{
Vector2 pointA = new Vector2 (wr.x + wr.width / 2, wr.y + wr.height / 2);
Vector2 pointB = new Vector2 (wr2.x + wr2.width / 2, wr2.y + wr2.height / 2);
Vector2 destAnchorPos = GetAnchorPos (pointA, wr2);
Vector2 srcAnchorPos = GetAnchorPos (pointB, wr);
float width = 2;
Color savedColor = GUI.color;
Matrix4x4 savedMatrix = GUI.matrix;
width *= 3;
if (!lineTex) {
lineTex = new Texture2D (1, 3, TextureFormat.ARGB32, true);
lineTex.SetPixel (0, 0, new Color (1, 1, 1, 0));
lineTex.SetPixel (0, 1, Color.white);
lineTex.SetPixel (0, 2, new Color (1, 1, 1, 0));
lineTex.Apply ();
}
float angle = Vector3.Angle (destAnchorPos - srcAnchorPos, Vector2.right) * (srcAnchorPos.y <= pointB.y ? 1 : -1);
//Debug.Log (" a=" + pointA + " b=" + pointB + " A=" + angle);
float dx = destAnchorPos.x - srcAnchorPos.x;
float dy = destAnchorPos.y - srcAnchorPos.y;
float length = Mathf.Sqrt (dx * dx + dy * dy);
GUI.color = color;
GUI.matrix = Matrix4x4.TRS (new Vector3 (srcAnchorPos.x, srcAnchorPos.y, 0), Quaternion.identity, Vector3.one);
GUIUtility.RotateAroundPivot (angle, srcAnchorPos);
GUI.DrawTexture (new Rect (0, 0, length, 1), lineTex);
GUI.color = savedColor;
if (destAnchor != null) {
DrawAnchor (destAnchorPos, angle, destAnchor);
}
if (srcAnchor != null) {
DrawAnchor (srcAnchorPos, angle + 180, srcAnchor);
}
GUI.matrix = savedMatrix;
GUI.color = savedColor;
}
void DrawAnchor (Vector2 destAnchorPos, float angle, Texture2D texAnchor)
{
Color savedColor = GUI.color;
Matrix4x4 savedMatrix = GUI.matrix;
Vector2 arrowPivot = new Vector2 (destAnchorPos.x, destAnchorPos.y);
Vector2 arrowPoint = new Vector2 (destAnchorPos.x, destAnchorPos.y);
arrowPoint.x -= 16;
arrowPoint.y -= 8;
GUI.matrix = Matrix4x4.TRS (arrowPoint, Quaternion.identity, Vector3.one);
GUIUtility.RotateAroundPivot (angle, arrowPivot);
GUI.DrawTexture (new Rect (0, 0, 16, 16), texAnchor);
GUI.matrix = savedMatrix;
GUI.color = savedColor;
}
private float calcPosition (Vector2 topLeft, Vector2 bottomRight, Vector2 pointA)
{
float vx1 = bottomRight.x - topLeft.x;
float vy1 = bottomRight.y - topLeft.y;
float vx2 = pointA.x - topLeft.x;
float vy2 = pointA.y - topLeft.y;
return vx1 * vy2 - vy1 * vx2;
}
}