TC_NodeGUI.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. using System;
  2. using UnityEngine;
  3. using UnityEditor;
  4. using System.Collections;
  5. namespace TerrainComposer2
  6. {
  7. static public class TC_NodeGUI
  8. {
  9. static public float input = 0;
  10. static public float position = 0;
  11. static public void Draw(TC_Node node, bool nodeFoldout, bool drawMethod, Vector2 pos, Color color, float activeMulti)
  12. {
  13. // Draw total node
  14. // startOffset.x -= TD.nodeSpaceWidth;
  15. // parent.DropDownMenu();
  16. bool isCulled = false;
  17. Rect rect = TD.DrawNode(node, pos, color, Color.white, ref isCulled, activeMulti, nodeFoldout, drawMethod, false);
  18. if (nodeFoldout)
  19. {
  20. Rect rectButton = new Rect(pos.x + 7.24f, pos.y + 306.09f, 253.92f, 30);
  21. if (TD.GetRectScaled(rectButton).y > 0)
  22. {
  23. string text = node.GetInputPopup().ToString();
  24. TD.DrawTextureScaled(rectButton, TD.texButton, color * activeMulti);
  25. TD.DrawText(new Vector2(rectButton.x + 5.02f, rectButton.y + 14.54f), text, 21, Color.white, FontStyle.Normal, HorTextAlign.Left, VerTextAlign.Center);
  26. //if (node.inputKind == InputKind.Terrain && node.inputTerrain == InputTerrain.Splatmap)
  27. //{
  28. // TD.DrawTextureScaled(new Rect(pos.x + g.rect3.x, pos.y + g.rect3.y, 75, 75), TC_Settings.instance.GetPreviewSplatTexture(node.splatSelectIndex), Color.white * activeMulti * 0.8f);
  29. //}
  30. DrawAddItem(rect, pos, node);
  31. DropDownMenuInput(TD.GetRectScaled(rectButton), node);
  32. }
  33. LeftClickMenu(rect, node);
  34. }
  35. }
  36. static public void DropDownMenuInput(Rect rect, TC_Node node)
  37. {
  38. if (TD.ClickRect(rect) != 0) return;
  39. GenericMenu menu = new GenericMenu();
  40. string instanceID = node.GetInstanceID().ToString();
  41. if (node.outputId == TC.heightOutput)
  42. AddMenuItem(node, menu, instanceID, node.inputTerrain, 5, true);
  43. else
  44. AddMenuItem(node, menu, instanceID, node.inputTerrain, 0);
  45. AddMenuItem(node, menu, instanceID, node.inputNoise);
  46. AddMenuItem(node, menu, instanceID, node.inputShape);
  47. AddMenuItem(node, menu, instanceID, node.inputFile);
  48. AddMenuItem(node, menu, instanceID, node.inputCurrent);
  49. menu.AddItem(new GUIContent("Portal"), false, ClickMenuInput, instanceID + ":Portal/Portal");
  50. menu.ShowAsContext();
  51. Event.current.Use();
  52. }
  53. static public void AddMenuItem(TC_Node node, GenericMenu menu, string instanceID, Enum e, int startIndex = 0, bool showOne = false)
  54. {
  55. int length;
  56. if (showOne) length = startIndex + 1; else length = Enum.GetNames(e.GetType()).Length;
  57. string inputName = e.GetType().ToString().Replace("TerrainComposer2.Input", "");
  58. for (int i = startIndex; i < length; i++)
  59. {
  60. string name = Enum.GetName(e.GetType(), i);
  61. if ((name == "Convexity" || name == "Collision") && node.outputId != TC.heightOutput) menu.AddSeparator("Terrain/");
  62. if (name == "IQ" || name == "Random") menu.AddSeparator("Noise/");
  63. menu.AddItem(new GUIContent(inputName + "/" + name), false, ClickMenuInput, instanceID + ":" + inputName+ "/" + name);
  64. }
  65. }
  66. static public void ClickMenuInput(object obj)
  67. {
  68. int instanceID;
  69. string command = TD.ObjectToCommandAndInstanceID(obj, out instanceID);
  70. TC_Node node = EditorUtility.InstanceIDToObject(instanceID) as TC_Node;
  71. if (node != null)
  72. {
  73. int index = command.IndexOf("/");
  74. string inputKind = command.Substring(0, index);
  75. string input = command.Substring(index + 1);
  76. bool changed = false;
  77. InputKind oldInputKind = node.inputKind;
  78. node.inputKind = (InputKind)Enum.Parse(typeof(InputKind), inputKind);
  79. if (node.inputKind != oldInputKind) changed = true;
  80. if (inputKind == "Terrain")
  81. {
  82. InputTerrain oldInputTerrain = node.inputTerrain;
  83. node.inputTerrain = (InputTerrain)Enum.Parse(typeof(InputTerrain), input);
  84. if (node.inputTerrain != oldInputTerrain) changed = true;
  85. }
  86. else if (inputKind == "Noise")
  87. {
  88. InputNoise oldInputNoise = node.inputNoise;
  89. node.inputNoise = (InputNoise)Enum.Parse(typeof(InputNoise), input);
  90. if (node.inputNoise != oldInputNoise) changed = true;
  91. }
  92. else if (inputKind == "Shape")
  93. {
  94. InputShape oldInputShape = node.inputShape;
  95. node.inputShape = (InputShape)Enum.Parse(typeof(InputShape), input);
  96. if (node.inputShape != oldInputShape) changed = true;
  97. }
  98. else if (inputKind == "File")
  99. {
  100. InputFile oldInputFile = node.inputFile;
  101. node.inputFile = (InputFile)Enum.Parse(typeof(InputFile), input);
  102. if (node.inputFile != oldInputFile) changed = true;
  103. }
  104. else if (inputKind == "Current")
  105. {
  106. InputCurrent oldInputCurrent = node.inputCurrent;
  107. node.inputCurrent = (InputCurrent)Enum.Parse(typeof(InputCurrent), input);
  108. if (node.inputCurrent != oldInputCurrent) changed = true;
  109. }
  110. if (changed)
  111. {
  112. node.Init();
  113. EditorUtility.SetDirty(node);
  114. TC.RefreshOutputReferences(node.outputId, true);
  115. }
  116. }
  117. }
  118. static void LeftClickMethodMenu(object obj)
  119. {
  120. //int instanceID;
  121. //string command = ObjectToCommandAndInstanceID(obj, out instanceID);
  122. //TC_ItemBehaviour item = EditorUtility.InstanceIDToObject(instanceID) as TC_ItemBehaviour;
  123. //Method oldMethod = item.method;
  124. //item.method = (Method)System.Enum.Parse(typeof(Method), command);
  125. //if (item.method != oldMethod) TC_Generate.instance.AutoGenerate();
  126. }
  127. static public void DrawAddItem(Rect rect, Vector2 pos, TC_ItemBehaviour item)
  128. {
  129. Event eventCurrent = Event.current;
  130. if (DragAndDrop.objectReferences.Length > 0) return;
  131. Vector2 posMouse = eventCurrent.mousePosition;
  132. if (rect.Contains(posMouse))
  133. {
  134. if (eventCurrent.alt && !eventCurrent.control)
  135. {
  136. if (posMouse.x < rect.x + ((TD.texCardHeader.width / 2) * TD.scale)) item.dropPosition = DropPosition.Left;
  137. else item.dropPosition = DropPosition.Right;
  138. if (eventCurrent.type == EventType.MouseDown && eventCurrent.button == 0 && eventCurrent.clickCount == 2)
  139. {
  140. AddItem(item, eventCurrent.shift);
  141. eventCurrent.Use();
  142. }
  143. TC.repaintNodeWindow = true;
  144. }
  145. else item.dropPosition = DropPosition.None;
  146. }
  147. }
  148. static public void LeftClickMenu(Rect rect, TC_ItemBehaviour item)
  149. {
  150. if (TD.ClickRect(rect) != 1) return;
  151. string itemText = "";
  152. if (item.GetType() == typeof(TC_SelectItem)) itemText = "Item"; else itemText = "Node";
  153. GenericMenu menu = new GenericMenu();
  154. string instanceID = item.GetInstanceID().ToString();
  155. if (Event.current.mousePosition.x < rect.x + ((TD.texCardHeader.width / 2) * TD.scale))
  156. {
  157. menu.AddItem(new GUIContent("<-- Add " + itemText), false, LeftClickMenu, instanceID + ":Add Left");
  158. //if (itemText == "Node")
  159. //{
  160. // menu.AddItem(new GUIContent("<-- Add Node Group"), false, LeftClickMenu, instanceID + ":Add NodeGroup Left");
  161. // menu.AddSeparator("");
  162. //}
  163. menu.AddItem(new GUIContent("<-- Duplicate " + itemText), false, LeftClickMenu, instanceID + ":Duplicate Left");
  164. }
  165. else
  166. {
  167. menu.AddItem(new GUIContent("--> Add " + itemText), false, LeftClickMenu, instanceID + ":Add Right");
  168. //if (itemText == "Node")
  169. //{
  170. // menu.AddItem(new GUIContent("--> Add Node Group"), false, LeftClickMenu, instanceID + ":Add NodeGroup Right");
  171. // menu.AddSeparator("");
  172. //}
  173. menu.AddItem(new GUIContent("--> Duplicate " + itemText), false, LeftClickMenu, instanceID + ":Duplicate Right");
  174. }
  175. // bool eraseNodeMenu = true;
  176. //if (node.nodeType == NodeType.Select)
  177. //{
  178. // TC_NodeGroup nodeGroup = node.t.parent.GetComponent<TC_NodeGroup>();
  179. // if (nodeGroup != null)
  180. // {
  181. // if (nodeGroup.itemList.Count == 1) eraseNodeMenu = false;
  182. // }
  183. //}
  184. //if (eraseNodeMenu) {
  185. // menu.AddSeparator("");
  186. // menu.AddItem(new GUIContent("Erase Node"), false, LeftClickMenu, instanceID + ":Erase Node");
  187. //}
  188. menu.ShowAsContext();
  189. }
  190. static public void LeftClickMenu(object obj)
  191. {
  192. int instanceID;
  193. string command = TD.ObjectToCommandAndInstanceID(obj, out instanceID);
  194. TC_ItemBehaviour item = EditorUtility.InstanceIDToObject(instanceID) as TC_ItemBehaviour;
  195. if (item != null)
  196. {
  197. // if (command == "Add Node ") node.Add<TC_Node>("", true, false, true);
  198. // else if (command == "Duplicate Node") node.Duplicate(node.t.parent);
  199. if (command.Contains("Right")) item.dropPosition = DropPosition.Right;
  200. else if (command.Contains("Left")) item.dropPosition = DropPosition.Left;
  201. bool addNodeGroup = command.Contains("NodeGroup");
  202. bool clone;
  203. if (command.Contains("Duplicate")) clone = true; else clone = false;
  204. AddItem(item, clone, addNodeGroup);
  205. item.dropPosition = DropPosition.None;
  206. // else if (command == "Erase Node") node.Destroy(true);
  207. }
  208. }
  209. static public TC_ItemBehaviour AddItem(TC_ItemBehaviour item, bool clone, bool addNodeGroup = false)
  210. {
  211. TC_ItemBehaviour itemToDrop = null;
  212. if (!clone)
  213. {
  214. if (item.GetType() == typeof(TC_Node))
  215. {
  216. if (addNodeGroup) itemToDrop = (TC_ItemBehaviour)item.Add<TC_NodeGroup>("Node Group", true); else itemToDrop = (TC_ItemBehaviour)item.Add<TC_Node>("", true);
  217. }
  218. else if (item.GetType() == typeof(TC_SelectItem))
  219. {
  220. itemToDrop = (TC_ItemBehaviour)item.Add<TC_SelectItem>("", true);
  221. TC_SelectItem newSelectItem = (TC_SelectItem)itemToDrop;
  222. TC_SelectItem selectItem = (TC_SelectItem)item;
  223. if (selectItem.dropPosition == DropPosition.Right) newSelectItem.selectIndex = Mathf.Min(selectItem.selectIndex + 1, Mathf.Max(0, newSelectItem.GetItemTotalFromTerrain() - 1));
  224. else newSelectItem.selectIndex = Mathf.Max(selectItem.selectIndex - 1, 0);
  225. }
  226. Undo.RegisterCreatedObjectUndo(item.gameObject, "Created " + item.name);
  227. }
  228. else itemToDrop = item;
  229. itemToDrop = TD.DropItemSameLevel(item, itemToDrop, clone, false);
  230. Selection.activeTransform = itemToDrop.t;
  231. return itemToDrop;
  232. }
  233. }
  234. }