You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
242 lines
6.4 KiB
242 lines
6.4 KiB
10 years ago
|
using UnityEngine;
|
||
|
using UnityEditor;
|
||
10 years ago
|
using System;
|
||
10 years ago
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
|
||
10 years ago
|
namespace Fungus.Script
|
||
10 years ago
|
{
|
||
|
|
||
10 years ago
|
public class FungusScriptWindow : EditorWindow
|
||
10 years ago
|
{
|
||
10 years ago
|
private List<Sequence> windowSequenceMap = new List<Sequence>();
|
||
10 years ago
|
|
||
10 years ago
|
[MenuItem("Window/Fungus Script")]
|
||
10 years ago
|
static void Init()
|
||
|
{
|
||
10 years ago
|
GetWindow(typeof(FungusScriptWindow), false, "Fungus Script");
|
||
10 years ago
|
}
|
||
|
|
||
|
public void OnInspectorUpdate()
|
||
10 years ago
|
{
|
||
10 years ago
|
Repaint();
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
static public FungusScript GetFungusScript()
|
||
10 years ago
|
{
|
||
10 years ago
|
if (Selection.activeGameObject != null)
|
||
10 years ago
|
{
|
||
10 years ago
|
return Selection.activeGameObject.GetComponent<FungusScript>();
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
10 years ago
|
return null;
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
void OnGUI()
|
||
|
{
|
||
|
FungusScript fungusScript = GetFungusScript();
|
||
|
if (fungusScript == null)
|
||
|
{
|
||
10 years ago
|
GUILayout.Label("No Fungus Script object selected");
|
||
10 years ago
|
return;
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
EditorUtility.SetDirty(fungusScript);
|
||
|
|
||
10 years ago
|
Sequence[] sequences = fungusScript.GetComponentsInChildren<Sequence>();
|
||
10 years ago
|
|
||
10 years ago
|
Rect scrollViewRect = new Rect();
|
||
10 years ago
|
|
||
10 years ago
|
foreach (Sequence s in sequences)
|
||
|
{
|
||
|
scrollViewRect.xMin = Mathf.Min(scrollViewRect.xMin, s.nodeRect.xMin);
|
||
|
scrollViewRect.xMax = Mathf.Max(scrollViewRect.xMax, s.nodeRect.xMax);
|
||
|
scrollViewRect.yMin = Mathf.Min(scrollViewRect.yMin, s.nodeRect.yMin);
|
||
|
scrollViewRect.yMax = Mathf.Max(scrollViewRect.yMax, s.nodeRect.yMax);
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
// Empty buffer area around edges of scroll rect
|
||
10 years ago
|
float bufferScale = 0.25f;
|
||
10 years ago
|
scrollViewRect.xMin -= position.width * bufferScale;
|
||
|
scrollViewRect.yMin -= position.height * bufferScale;
|
||
|
scrollViewRect.xMax += position.width * bufferScale;
|
||
|
scrollViewRect.yMax += position.height * bufferScale;
|
||
10 years ago
|
|
||
10 years ago
|
fungusScript.scrollPos = GUI.BeginScrollView(new Rect(0, 0, position.width, position.height), fungusScript.scrollPos, scrollViewRect);
|
||
10 years ago
|
|
||
10 years ago
|
if (Event.current.type == EventType.ContextClick)
|
||
|
{
|
||
|
GenericMenu menu = new GenericMenu();
|
||
|
Vector2 mousePos = Event.current.mousePosition;
|
||
|
mousePos += fungusScript.scrollPos;
|
||
|
menu.AddItem (new GUIContent ("Create Sequence"), false, CreateSequenceCallback, mousePos);
|
||
|
menu.ShowAsContext ();
|
||
|
|
||
|
Event.current.Use();
|
||
|
}
|
||
|
|
||
10 years ago
|
BeginWindows();
|
||
10 years ago
|
|
||
10 years ago
|
GUIStyle windowStyle = new GUIStyle(GUI.skin.window);
|
||
|
|
||
10 years ago
|
windowSequenceMap.Clear();
|
||
|
for (int i = 0; i < sequences.Length; ++i)
|
||
|
{
|
||
|
Sequence sequence = sequences[i];
|
||
10 years ago
|
|
||
10 years ago
|
float titleWidth = windowStyle.CalcSize(new GUIContent(sequence.name)).x;
|
||
|
float windowWidth = Mathf.Max (titleWidth + 10, 100);
|
||
|
|
||
10 years ago
|
sequence.nodeRect = GUILayout.Window(i, sequence.nodeRect, DrawWindow, sequence.name, GUILayout.Width(windowWidth), GUILayout.Height(20), GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));
|
||
10 years ago
|
|
||
10 years ago
|
windowSequenceMap.Add(sequence);
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
// Draw connections
|
||
|
foreach (Sequence s in windowSequenceMap)
|
||
|
{
|
||
10 years ago
|
DrawConnections(fungusScript, s, false);
|
||
10 years ago
|
}
|
||
|
foreach (Sequence s in windowSequenceMap)
|
||
|
{
|
||
10 years ago
|
DrawConnections(fungusScript, s, true);
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
10 years ago
|
EndWindows();
|
||
|
|
||
10 years ago
|
if (fungusScript.selectedSequence != null ||
|
||
|
fungusScript.executingSequence != null)
|
||
10 years ago
|
{
|
||
10 years ago
|
Rect outlineRect = new Rect();
|
||
|
if (fungusScript.executingSequence != null)
|
||
|
{
|
||
|
outlineRect = fungusScript.executingSequence.nodeRect;
|
||
|
}
|
||
|
else if (fungusScript.selectedSequence != null)
|
||
|
{
|
||
|
outlineRect = fungusScript.selectedSequence.nodeRect;
|
||
|
}
|
||
10 years ago
|
outlineRect.width += 10;
|
||
|
outlineRect.x -= 5;
|
||
|
outlineRect.height += 10;
|
||
|
outlineRect.y -= 5;
|
||
10 years ago
|
GLDraw.DrawBox(outlineRect, Color.green, 2);
|
||
|
}
|
||
|
|
||
10 years ago
|
GUI.EndScrollView();
|
||
10 years ago
|
|
||
|
GUILayout.BeginVertical();
|
||
|
GUILayout.FlexibleSpace();
|
||
|
GUILayout.BeginHorizontal();
|
||
10 years ago
|
GUILayout.Space(10);
|
||
10 years ago
|
GUILayout.Label(fungusScript.name, EditorStyles.miniLabel);
|
||
|
GUILayout.EndHorizontal();
|
||
|
GUILayout.BeginHorizontal();
|
||
|
GUILayout.Space(10);
|
||
10 years ago
|
GUI.backgroundColor = Color.white;
|
||
|
if (GUILayout.Button("Show Variables"))
|
||
|
{
|
||
10 years ago
|
EditorWindow.GetWindow<VariablesWindow>("Fungus Variables");
|
||
10 years ago
|
}
|
||
10 years ago
|
GUILayout.FlexibleSpace();
|
||
|
GUILayout.EndHorizontal();
|
||
10 years ago
|
GUILayout.Space(20);
|
||
10 years ago
|
GUILayout.EndVertical();
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
10 years ago
|
void CreateSequenceCallback(object item)
|
||
|
{
|
||
|
FungusScript fungusScript = GetFungusScript();
|
||
|
if (fungusScript != null)
|
||
|
{
|
||
|
Vector2 position = (Vector2)item;
|
||
|
position -= fungusScript.scrollPos;
|
||
|
Sequence newSequence = fungusScript.CreateSequence(position);
|
||
|
Undo.RegisterCreatedObjectUndo(newSequence, "New Sequence");
|
||
|
fungusScript.selectedSequence = newSequence;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void DrawWindow(int windowId)
|
||
|
{
|
||
10 years ago
|
// Select sequence when node is clicked
|
||
10 years ago
|
if (Event.current.button == 0 &&
|
||
10 years ago
|
Event.current.type == EventType.MouseDown)
|
||
10 years ago
|
{
|
||
10 years ago
|
if (windowId < windowSequenceMap.Count)
|
||
10 years ago
|
{
|
||
10 years ago
|
Sequence s = windowSequenceMap[windowId];
|
||
|
if (s != null)
|
||
|
{
|
||
10 years ago
|
FungusScript fungusScript = s.GetFungusScript();
|
||
|
fungusScript.selectedSequence = s;
|
||
10 years ago
|
|
||
|
Selection.activeGameObject = fungusScript.gameObject;
|
||
10 years ago
|
}
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
Sequence sequence = windowSequenceMap[windowId];
|
||
10 years ago
|
|
||
10 years ago
|
GUIStyle labelStyle = new GUIStyle(GUI.skin.label);
|
||
|
labelStyle.wordWrap = true;
|
||
|
GUILayout.Label(sequence.description, labelStyle);
|
||
10 years ago
|
|
||
10 years ago
|
GUILayout.Space(1);
|
||
10 years ago
|
|
||
10 years ago
|
GUI.DragWindow();
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
void DrawConnections(FungusScript fungusScript, Sequence sequence, bool highlightedOnly)
|
||
10 years ago
|
{
|
||
10 years ago
|
List<Sequence> connectedSequences = new List<Sequence>();
|
||
10 years ago
|
|
||
10 years ago
|
bool sequenceIsSelected = (fungusScript.selectedSequence == sequence);
|
||
|
|
||
10 years ago
|
FungusCommand[] commands = sequence.GetComponentsInChildren<FungusCommand>();
|
||
|
foreach (FungusCommand command in commands)
|
||
10 years ago
|
{
|
||
10 years ago
|
bool highlight = command.IsExecuting() || (sequenceIsSelected && command.expanded);
|
||
10 years ago
|
|
||
10 years ago
|
if (highlightedOnly && !highlight ||
|
||
|
!highlightedOnly && highlight)
|
||
|
{
|
||
|
continue;
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
connectedSequences.Clear();
|
||
|
command.GetConnectedSequences(ref connectedSequences);
|
||
10 years ago
|
|
||
10 years ago
|
foreach (Sequence sequenceB in connectedSequences)
|
||
|
{
|
||
10 years ago
|
DrawRectConnection(sequence.nodeRect, sequenceB.nodeRect, highlight);
|
||
10 years ago
|
}
|
||
10 years ago
|
}
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
void DrawRectConnection(Rect rectA, Rect rectB, bool highlight)
|
||
10 years ago
|
{
|
||
10 years ago
|
Vector2 pointA;
|
||
|
Vector2 pointB;
|
||
|
|
||
|
Vector2 p1 = rectA.center;
|
||
|
Vector2 p2 = rectB.center;
|
||
|
GLDraw.segment_rect_intersection(rectA, ref p1, ref p2);
|
||
|
pointA = p2;
|
||
|
|
||
|
p1 = rectB.center;
|
||
|
p2 = rectA.center;
|
||
|
GLDraw.segment_rect_intersection(rectB, ref p1, ref p2);
|
||
|
pointB = p2;
|
||
|
|
||
|
Color color = Color.grey;
|
||
|
if (highlight)
|
||
|
{
|
||
|
color = Color.green;
|
||
|
}
|
||
|
|
||
|
GLDraw.DrawConnectingCurve(pointA, pointB, color, 2);
|
||
10 years ago
|
}
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
|
}
|