Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma strict
- import System.Collections.Generic;
- import System.Xml;
- import System.Xml.Serialization;
- import System.IO;
- import System.Text;
- //@XmlRoot("TextNodeCollection")
- class NodeEditor extends EditorWindow {
- // @XmlArray("Bubbles")
- // @XmlArrayItem("Bubble")
- var bubbles:List.<TextBubble>;
- var conditions:List.<CCondition>;
- var enterNode:TextBubble;
- private var condOptions:String[];
- private var evt:Event;
- private var selectedBubble:TextBubble;
- private var reservedWinMin:int = 10;
- private var menuRect:Rect;
- private var bgSize:int = 10000;
- private var panX:int = -bgSize*0.5;
- private var panY:int = -bgSize*0.5;
- @MenuItem("Window/Node editor")
- static function ShowEditor() {
- var editor:NodeEditor = EditorWindow.GetWindow.<NodeEditor>();
- editor.Init();
- }
- function Init(){
- bubbles = new List.<TextBubble>();
- conditions = new List.<CCondition>();
- }
- function OnGUI() {
- evt = Event.current;
- if(evt.type == EventType.ContextClick)DrawMainContextMenu();
- var winPos:Rect = this.position;
- menuRect = Rect(winPos.width - 300 - panX, 0 - panY, 300, winPos.height);
- var didPan = false;
- if (Event.current.type == EventType.MouseDrag) {
- didPan = PanBackground();
- }
- GUI.BeginGroup(new Rect( panX, panY, bgSize, bgSize),"qwe");
- BeginWindows();
- for (bubble in bubbles){
- var prefix:String;
- if(bubble == enterNode) prefix = "enter - ";
- bubble.rect = GUI.Window(bubble.id, bubble.rect, DrawBubbleWindow, prefix + bubble.actor+" #"+bubble.id); //bubble.OnGUI();
- for (var link in bubble.links)DrawNodeCurve(bubble, link.id);
- }
- GUI.Window(-1, menuRect, DrawMenuWindow, "Main Menu");
- //for (bubble in bubbles)//DrawNodeCurve(bubble);
- GUI.BringWindowToFront(-1);
- EndWindows();
- //GUI.EndGroup();
- GUI.EndGroup();
- if (didPan) Repaint();
- }
- function PanBackground():boolean{
- var mouseDelta = Vector2(panX, panY);
- for(var bubble in bubbles){//check nodes
- if (bubble.rect.Contains(Event.current.mousePosition - mouseDelta)) return false;
- }
- if (menuRect.Contains(Event.current.mousePosition - mouseDelta)) return false;
- panX += Event.current.delta.x;
- panY += Event.current.delta.y;
- //Debug.Log(panX);
- return true;
- }
- function DrawMenuWindow(id:int) { //side menu
- if(selectedBubble)DrawBubbleControllMenu();
- GUILayout.FlexibleSpace();
- DrowConditionParamsMenu();
- DrowIOMenu();
- }
- function DrawBubbleControllMenu(){ //node controll menu part
- EditorGUILayout.LabelField("Selected node: "+selectedBubble.id);
- EditorGUILayout.BeginHorizontal();
- EditorGUILayout.LabelField("Actor:");
- selectedBubble.actor = EditorGUILayout.TextField(selectedBubble.actor);
- EditorGUILayout.EndHorizontal();
- EditorGUILayout.LabelField("Text:");
- //EditorStyles.textField.wordWrap;
- EditorGUILayout.BeginVertical();
- selectedBubble.text = EditorGUILayout.TextField(selectedBubble.text);
- EditorGUILayout.EndVertical();
- DrawLinksMenu();
- DrawNodeOptionsMenu();
- }
- function DrawNodeOptionsMenu(){
- GUILayout.BeginHorizontal();
- GUILayout.FlexibleSpace();
- if ( GUILayout.Button("Set as start") ) SetNodeAsStart();
- if ( GUILayout.Button("Remove node") ) RemoveNode();
- GUILayout.EndHorizontal();
- }
- function RemoveNode(){
- bubbles.Remove(selectedBubble);
- }
- function SetNodeAsStart(){
- enterNode = selectedBubble;
- }
- function DrowIOMenu(){
- GUILayout.BeginHorizontal();
- if ( GUILayout.Button("Save Sheet") ) SaveSheet();
- if(GUILayout.Button("Load Sheet")) LoadSheet();
- GUILayout.EndHorizontal();
- }
- function DrawLinksMenu(){
- for (link in selectedBubble.links){
- GUILayout.BeginHorizontal();
- GUILayout.Label("Link id:");
- link.id = int.Parse(GUILayout.TextField(link.id.ToString()));
- GUILayout.Space(50);
- var conditionName = link.condition ? link.condition.name : "None";
- if(GUILayout.Button("Condition: " + conditionName))DrawConditionsContextMenu(link);
- if(GUILayout.Button("Remove"))RemoveLink(link, selectedBubble.links);
- //link.conditionIndex = EditorGUILayout.Popup(link.conditionIndex, condOptions);
- GUILayout.EndHorizontal();
- }
- if(GUILayout.Button("Add link"))selectedBubble.AddLink();
- }
- function RemoveLink(link:TextBubble.CLink, list:List.<TextBubble.CLink>){
- list.Remove(link);
- }
- function DrawConditionsContextMenu(link:TextBubble.CLink){
- var menu : GenericMenu = new GenericMenu ();
- menu.AddItem (new GUIContent ("None"), false, AddNullPositionToConditionsMenu, link);
- menu.AddSeparator ("");
- for (var condition in conditions)
- menu.AddItem (new GUIContent (condition.name), false, AddPositionToConditionsMenu, new KeyValuePair.<TextBubble.CLink,CCondition>(link, condition));
- //menu.AddItem (new GUIContent ("Connect bubble"), false, ConnectBubble);
- //menu.AddItem (new GUIContent ("Debug"), false, DebugInfo);
- menu.ShowAsContext ();
- evt.Use();
- }
- function AddNullPositionToConditionsMenu(link:TextBubble.CLink){
- link.condition = null;
- }
- function AddPositionToConditionsMenu(pair:KeyValuePair.<TextBubble.CLink,CCondition>){
- pair.Key.condition = pair.Value;
- }
- function DrowConditionParamsMenu(){
- GUILayout.BeginHorizontal();
- //GUI.SetNextControlName ("MyTextField");
- if(GUILayout.Button("Add condition"))AddCondition();
- //if(GUILayout.Button("Update"))RebuildConditionOptions();
- GUILayout.EndHorizontal();
- for (condition in conditions){
- GUILayout.BeginHorizontal();
- if(GUILayout.Button("Remove"))conditions.Remove(condition);
- GUILayout.Label("Name:");
- condition.name = GUILayout.TextField(condition.name);
- GUILayout.EndHorizontal();
- }
- }
- function GetBubble(idP:int):TextBubble{
- return bubbles.Find( function(bubble) bubble.id == idP );
- }
- function DrawBubbleWindow(id:int) {
- //GUI.skin.window.
- var bubble = GetBubble(id);
- EditorStyles.label.wordWrap = true;
- GUI.SetNextControlName ("MyTextField");
- EditorGUILayout.SelectableLabel(bubble.text);
- if(evt.type == EventType.mouseDown && evt.button == 0) {
- var window = EditorWindow.GetWindow.<NodeEditor>();
- window.UpdateMenu(bubble);
- if(GUI.GetNameOfFocusedControl() != "MyTextField")
- //Debug.Log(GUI.GetNameOfFocusedControl().ToString());
- EditorGUI.FocusTextInControl ("MyTextField");
- }//else if(evt.type == EventType.mouseDown && evt.button == 1)ConnectBubble();
- GUI.DragWindow();
- //
- }
- function AddCondition(){
- conditions.Add(new CCondition());
- //RebuildConditionOptions();
- }
- function DrawNodeCurve(bubble:TextBubble, link:int) {
- var target = GetBubble(link);
- if(target == null)return; //check if target connected
- var start = bubble.rect;
- var end = target.rect;
- var startPos = new Vector3(start.x + start.width, start.y + start.height / 2, 0);
- var endPos = new Vector3(end.x, end.y + end.height / 2, 0);
- var startTan = startPos + Vector3.right * 50;
- var endTan = endPos + Vector3.left * 50;
- var shadowCol = new Color(0, 0, 0, 0.06f);
- for (var i = 0; i < 3; i++) // Draw a shadow
- Handles.DrawBezier(startPos, endPos, startTan, endTan, shadowCol, null, (i + 1) * 5);
- Handles.DrawBezier(startPos, endPos, startTan, endTan, Color.black, null, 1);
- }
- function DrawMainContextMenu(){
- var menu : GenericMenu = new GenericMenu ();
- menu.AddItem (new GUIContent ("Add new node"), false, AddNewNode);
- //menu.AddItem (new GUIContent ("Connect bubble"), false, ConnectBubble);
- menu.AddSeparator ("");
- //menu.AddItem (new GUIContent ("Debug"), false, DebugInfo);
- menu.ShowAsContext ();
- evt.Use();
- }
- function AddNewNode(){
- var mPos = evt.mousePosition;
- var rect = new Rect(mPos.x-panX, mPos.y-panY, 200, 80);
- var id = GetNewId();
- var bubble = new TextBubble(id, rect);
- bubbles.Add(bubble);
- }
- function ConnectBubble(){
- Debug.Log(evt.mousePosition);
- }
- function GetNewId():int{
- var max:int;
- for(var bubble in bubbles) if(bubble.id > max)max = bubble.id;
- return max+1;
- }
- function DebugInfo(){
- Debug.Log(this.position);
- }
- function UpdateMenu(bubble:TextBubble){
- selectedBubble = bubble;
- }
- function SaveSheet(){
- var path = EditorUtility.SaveFilePanelInProject("Save dialogue as dialogue", "dialogue", "xml", "Please enter a file name to save the dialogue to");
- if (!path) return;
- if(!enterNode)Debug.Log("enter node not selected");
- // var sw = new StreamWriter( new FileStream(path, FileMode.Create, FileAccess.ReadWrite), Encoding.UTF8);
- // sw.Write(dialog.ToString());
- // sw.Close();
- // var sw:StreamWriter = new StreamWriter(path, false, Encoding.UTF8)) {
- // sw.Write(sb.ToString());
- // sw.Close();
- var container = new SaveFile(enterNode, bubbles, conditions);
- var serializer : XmlSerializer = new XmlSerializer(SaveFile);
- var stream : StreamWriter = new StreamWriter(path, false, Encoding.UTF8);
- //var stream : Stream = new FileStream(path, FileMode.Create);
- serializer.Serialize(stream, container);
- stream.Close();
- }
- function SaveSheet_obsolate(){
- // var sw = new StreamWriter( new FileStream(temporaryFilePath, FileMode.Create, FileAccess.ReadWrite), Encoding.UTF8) {
- // sw.Write(sb.ToString());
- // }
- var path = EditorUtility.SaveFilePanelInProject("Save dialogue as dialogue", "dialogue", "xml", "Please enter a file name to save the dialogue to");
- if (!path) return;
- var container = new SaveFile(enterNode, bubbles, conditions);
- var serializer : XmlSerializer = new XmlSerializer(SaveFile);
- var stream : Stream = new FileStream(path, FileMode.Create);
- serializer.Serialize(stream, container);
- stream.Close();
- }
- function LoadSheet(){
- var path = EditorUtility.OpenFilePanel("Load file", "assets", "xml");
- if (!path) return;
- var serializer : XmlSerializer = new XmlSerializer(SaveFile);
- var stream : Stream = new FileStream(path, FileMode.Open);
- var container : SaveFile = serializer.Deserialize(stream) as SaveFile;
- stream.Close();
- bubbles = container.bubbles;
- conditions = container.conditions;
- enterNode = GetBubble(container.startNode);
- }
- }
- class CCondition {
- var name:String = "";
- //var state:boolean;
- function CCondition(){
- }
- }
- public class TextBubble {
- public var text:String = "";
- public var actor:String = "";
- @XmlAttribute("id")
- public var id:int;
- var rect:Rect;
- var links:List.<CLink>;
- function TextBubble(){}
- function TextBubble(newID:int, newRect:Rect){
- id = newID;
- rect = newRect;
- links = new List.<CLink>();
- //style = new GUIStyle();
- }
- function AddLink(){
- links.Add(new CLink());
- }
- class CLink{
- var id:int;
- var condition:CCondition;
- function CLink(){};
- // function CLink(linkP:int, nameP:String, index:int){
- // //id = linkP;
- // //conditionName = nameP;
- // //conditionIndex = index;
- // }
- }
- }
- @XmlRoot("TextNodeCollection")
- public class SaveFile{
- public var startNode:int;
- @XmlArray("Bubbles")
- @XmlArrayItem("Bubble")
- public var bubbles:List.<TextBubble>;
- @XmlArray("Conditions")
- @XmlArrayItem("Conditions")
- public var conditions:List.<CCondition>;
- function SaveFile(){}
- function SaveFile (enterNode:TextBubble, bubblesInp:List.<TextBubble>, conditionsInp:List.<CCondition> ) {
- startNode = enterNode.id;//bubblesInp.IndexOf(enterNode);
- bubbles = bubblesInp;
- conditions = conditionsInp;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment