TC_LayerGroupGUI.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEditor;
  6. namespace TerrainComposer2
  7. {
  8. static class DrawCommand
  9. {
  10. static List<TextureCommand> drawTextureList0 = new List<TextureCommand>();
  11. static List<TextureCommand> drawTextureList1 = new List<TextureCommand>();
  12. static List<TextureCommand> drawTextureList2 = new List<TextureCommand>();
  13. static List<TextCommand> drawTextList = new List<TextCommand>();
  14. static public void Add(Rect rect, Texture tex, Color color, int queue = 0)
  15. {
  16. if (queue == 0) drawTextureList0.Add(new TextureCommand(rect, tex, color));
  17. else if (queue == 1) drawTextureList1.Add(new TextureCommand(rect, tex, color));
  18. else if (queue == 2) drawTextureList2.Add(new TextureCommand(rect, tex, color));
  19. }
  20. static public void Add(Vector2 pos, Texture tex, Color color, int queue = 0)
  21. {
  22. if (queue == 0) drawTextureList0.Add(new TextureCommand(pos, tex, color));
  23. else if (queue == 1) drawTextureList1.Add(new TextureCommand(pos, tex, color));
  24. else if (queue == 2) drawTextureList2.Add(new TextureCommand(pos, tex, color));
  25. }
  26. static public void Add(Vector2 pos, string text, int fontSize, Color color, FontStyle fontStyle = FontStyle.Normal, HorTextAlign horTextAlign = HorTextAlign.Left, VerTextAlign vertTextAlign = VerTextAlign.Top)
  27. {
  28. drawTextList.Add(new TextCommand(pos, text, fontSize, color, fontStyle, horTextAlign, vertTextAlign));
  29. }
  30. static public void Add(Vector2 pos, int space, string prefixText, string text, int fontSize, Color color, FontStyle fontStyle = FontStyle.Normal, HorTextAlign horTextAlign = HorTextAlign.Left, VerTextAlign vertTextAlign = VerTextAlign.Top)
  31. {
  32. drawTextList.Add(new TextCommand(pos, prefixText, fontSize, color, fontStyle, horTextAlign, vertTextAlign));
  33. drawTextList.Add(new TextCommand(pos + new Vector2(space, 0), text, fontSize, color, fontStyle, horTextAlign, vertTextAlign));
  34. }
  35. static public void DrawTextureList(List<TextureCommand> drawTextureList)
  36. {
  37. for (int i = 0; i < drawTextureList.Count; i++)
  38. {
  39. TextureCommand textureCommand = drawTextureList[i];
  40. TD.DrawTextureScaled(textureCommand.rect, textureCommand.tex, textureCommand.color);
  41. }
  42. drawTextureList.Clear();
  43. }
  44. static public void DrawCommandLists()
  45. {
  46. DrawTextureList(drawTextureList0);
  47. DrawTextureList(drawTextureList1);
  48. DrawTextureList(drawTextureList2);
  49. for (int i = 0; i < drawTextList.Count; i++) drawTextList[i].Draw();
  50. GUI.color = Color.white;
  51. drawTextList.Clear();
  52. }
  53. public class TextureCommand
  54. {
  55. public Rect rect;
  56. public Texture tex;
  57. public Color color;
  58. public TextureCommand(Vector2 pos, Texture tex, Color color)
  59. {
  60. rect = new Rect(pos.x, pos.y, tex.width, tex.height);
  61. this.tex = tex; this.color = color;
  62. }
  63. public TextureCommand(Rect rect, Texture tex, Color color)
  64. {
  65. this.rect = rect;
  66. this.tex = tex; this.color = color;
  67. }
  68. }
  69. public class TextCommand
  70. {
  71. public Vector2 pos;
  72. public string text;
  73. public int fontSize;
  74. public Color color;
  75. public FontStyle fontStyle;
  76. public HorTextAlign horTextAlign;
  77. public VerTextAlign vertTextAlign;
  78. public TextCommand(Vector2 pos, string text, int fontSize, Color color, FontStyle fontStyle = FontStyle.Normal, HorTextAlign horTextAlign = HorTextAlign.Left, VerTextAlign vertTextAlign = VerTextAlign.Top)
  79. {
  80. this.pos = pos; this.text = text; this.color = color; this.fontSize = fontSize; this.fontStyle = fontStyle; this.horTextAlign = horTextAlign; this.vertTextAlign = vertTextAlign;
  81. }
  82. public void Draw()
  83. {
  84. TD.DrawText(pos, text, fontSize, color, fontStyle, horTextAlign, vertTextAlign);
  85. }
  86. }
  87. }
  88. static public class TC_LayerGroupGUI
  89. {
  90. static public Rect DrawLayerOrLayerGroup(TC_ItemBehaviour item, ref Vector2 pos, Color color, ref bool isCulled, float activeMulti, bool drawMethod, bool isFirst, bool isLast)
  91. {
  92. TC_GlobalSettings g = TC_Settings.instance.global; // TODO: Make global static and init
  93. TC_LayerGroup layerGroup = item as TC_LayerGroup;
  94. TC_Layer layer = item as TC_Layer;
  95. bool nodeFoldout = (layerGroup != null) ? layerGroup.nodeFoldout : layer.nodeFoldout;
  96. if (isFirst)
  97. {
  98. pos.y += TD.shelfOffsetY;
  99. TD.DrawTextureScaled(pos.x, pos.y, TD.texShelfLinesConnectDown, Color.white);
  100. TD.DrawTextureScaled(pos.x, pos.y, TD.texLineConnectDown, g.colLayer * activeMulti);
  101. pos.y += TD.texShelfLinesConnectDown.height;
  102. }
  103. else if (item.level == 0)
  104. {
  105. TD.DrawTextureScaled(pos.x + 64, pos.y, TD.texShelfStartConnect, Color.white);
  106. }
  107. else pos.y += 15;
  108. Texture texShelf;
  109. Texture texLine = null;
  110. if (item.level == 0)
  111. {
  112. if (nodeFoldout) texShelf = TD.texShelfStartOutput; else texShelf = TD.texShelfStartOutputCollapsed;
  113. }
  114. else if (isLast)
  115. {
  116. if (nodeFoldout) { texShelf = TD.texShelfLayerStart2; texLine = TD.texLineLayerStart2; }
  117. else { texShelf = TD.texShelfLayerCollapsedStart2; texLine = TD.texLineLayerStart2; }
  118. }
  119. else
  120. {
  121. if (nodeFoldout) { texShelf = TD.texShelfLayerStart1; texLine = TD.texLineLayerStart1; }
  122. else { texShelf = TD.texShelfLayerCollapsedStart1; texLine = TD.texLineLayerCollapsedStart1; }
  123. }
  124. TD.DrawTextureScaled(pos.x, pos.y, texShelf, Color.white);
  125. if (item.level > 0) TD.DrawTextureScaled(pos.x, pos.y, texLine, g.colLayer * activeMulti);
  126. TD.DrawTextureScaled(pos.x - TD.texShelfStartOutput.width, pos.y, nodeFoldout ? TD.texShelfLayer : TD.texShelfLayerCollapsed, Color.white, false, StretchMode.Left);
  127. pos.y += TD.shelfOffsetY;
  128. pos.x -= TD.texCardBody.width - (item.level == 0 ? 20 : 34);
  129. isCulled = false;
  130. Rect nodeRect = TD.DrawNode(item, pos, color, Color.white, ref isCulled, activeMulti, nodeFoldout, drawMethod);
  131. if (isCulled) return nodeRect;
  132. int mouseButton = TD.Button(new Rect(pos.x + 245.1f, pos.y + 6.5f, 20, 20), TD.texFoldout, true, new Color(1, 1, 1, 0.25f), Color.white, Color.white, true);
  133. if (mouseButton >= 0)
  134. {
  135. if (layerGroup != null)
  136. {
  137. layerGroup.nodeFoldout = !layerGroup.nodeFoldout;
  138. if (mouseButton == 0) layerGroup.foldout = layerGroup.nodeFoldout ? 2 : 0;
  139. }
  140. else layer.nodeFoldout = !layer.nodeFoldout;
  141. Event.current.Use();
  142. }
  143. //if (GUI.Button(TD.GetRectScaled(pos.x + 225, pos.y + 2, 40, 25), ""))
  144. //{
  145. // if (layerGroup != null) layerGroup.nodeFoldout = !layerGroup.nodeFoldout;
  146. // else layer.nodeFoldout = !layer.nodeFoldout;
  147. //}
  148. if (item.method == Method.Lerp && drawMethod)
  149. {
  150. // TD.DrawSlider(startOffset, ref item.overlay, 0, 1);
  151. // if (GUI.changed) TC_Generate.singleton.AutoGenerate();
  152. }
  153. if (item.level > 0)
  154. {
  155. DrawConnectionIndicator(item, new Vector2(pos.x + 289, pos.y - 27), false, true);
  156. if (isLast)
  157. {
  158. DrawConnectionIndicator(item, new Vector2(pos.x + 289, pos.y + TD.cardHeight + 4), true, nodeFoldout);
  159. }
  160. }
  161. return nodeRect;
  162. }
  163. static void DrawConnectionIndicator(TC_ItemBehaviour item, Vector2 pos, bool addBefore, bool nodeFoldout)
  164. {
  165. if (!nodeFoldout) pos.y -= 351;
  166. //if (TD.startDrag)
  167. //{
  168. // Rect dropRect = TD.GetRectScaled(new Rect(pos.x, pos.y, TD.texConnectionIndicator.width, TD.texConnectionIndicator.height));
  169. // // TD.DragDropNode(item, dropRect);
  170. // DrawTextureCommand.Add(pos, TD.texConnectionIndicator, Color.white);
  171. //}
  172. //else
  173. //{
  174. TC_GlobalSettings g = TC_Settings.instance.global;
  175. pos -= new Vector2(2.5f, 2.5f);
  176. Rect rect = new Rect(pos.x + g.rect4.x, pos.y + g.rect4.y, g.rect4.width, g.rect4.width);
  177. // DrawTextureCommand.Add(rect, TD.texCardCounter, TD.g.colLayer * 0.25f);
  178. int clickedButton = TD.Button(rect, TD.texAddFirstCard, true, new Color(1, 1, 1, 0.25f), Color.white, Color.white, true, true);
  179. // tooltip
  180. //Vector2 mousePosition = Event.current.mousePosition;
  181. //if (rectScaled.Contains(mousePosition))
  182. //{
  183. // DrawCommand.Add(mousePosition + new Vector2(9, 19), 65, "Left Click", "-> Add a new Layer", Color.white);
  184. // DrawCommand.Add(mousePosition + new Vector2(9, 19 + TD.labelHeight), 65, "Right Click","-> Add a new Layer Group", Color.white);
  185. // TD.repaintNodeWindow = true;
  186. //}
  187. if (clickedButton == 0) Undo.RegisterCreatedObjectUndo(item.Add<TC_Layer>("", true, addBefore, true).gameObject, "Created Layer");
  188. else if (clickedButton == 1) Undo.RegisterCreatedObjectUndo(item.Add<TC_LayerGroup>("", true, addBefore, true).gameObject, "Created GameObject");
  189. //}
  190. }
  191. static public void Draw(TC_LayerGroup layerGroup, bool drawMethod, ref Vector2 pos, float activeMulti, bool isFirst, bool isLast, ref float shelfLineVerticalStartY)
  192. {
  193. TC_GlobalSettings g = TC_Settings.instance.global;
  194. if (layerGroup.level == 0) pos.x -= TD.texShelfStartOutput.width;
  195. float posOldX = pos.x;
  196. bool isCulled = false;
  197. layerGroup.nodePos = pos - new Vector2(0, TD.cardHeight);
  198. Rect rect = DrawLayerOrLayerGroup(layerGroup, ref pos, g.colLayerGroup, ref isCulled, activeMulti, drawMethod, isFirst, isLast);
  199. activeMulti = layerGroup.active ? activeMulti : activeMulti * 0.75f;
  200. // Rect rectFoldout = TD.GetRectScaled(pos.x + 240, pos.y + 43 + (layerGroup.nodeFoldout ? TD.cardHeight : 32), 20, 20);
  201. // if (GUI.Button(rectFoldout, "")) layerGroup.foldout = !layerGroup.foldout;
  202. shelfLineVerticalStartY = pos.y + (layerGroup.nodeFoldout ? TD.texShelfLayerStart1.height : TD.texShelfLayerCollapsed.height) - TD.shelfOffsetY;
  203. DropDownMenu(rect, layerGroup);
  204. Vector2 bar2 = pos;
  205. bar2.x -= TD.texCardBody.width * 1.5f;
  206. bar2.y += TD.cardHeight;
  207. TC_NodeGroupGUI.Draw(layerGroup.maskNodeGroup, ref pos, g.colMaskNodeGroup, g.colMaskNode, g.colLayerGroup, activeMulti, layerGroup.nodeFoldout, false, false, false);
  208. // if (startOffsetXMax > startOffset.x) startOffsetXMax = startOffset.x;
  209. // Draw Result Node
  210. TC_LayerGroupResultGUI.Draw(layerGroup, ref pos, posOldX, activeMulti, layerGroup.nodeFoldout);
  211. int layerGroupCount = 0;
  212. int layerCount = 0;
  213. float lineOffsetX;
  214. if (layerGroup.maskNodeGroup.itemList.Count > 0) { lineOffsetX = 2.5f; } else lineOffsetX = 0;
  215. bool m_isFirst, m_isLast;
  216. float m_shelfLineVerticalStartY = 0;
  217. pos.y += layerGroup.nodeFoldout ? (TD.cardHeight) : 32;
  218. if (layerGroup.foldout == 2)
  219. {
  220. for (int i = layerGroup.groupResult.itemList.Count - 1; i >= 0; --i)
  221. {
  222. pos.x = posOldX;// - lineOffsetX ;
  223. pos.x -= g.layerHSpace;
  224. TC_LayerGroup layerGroupChild = layerGroup.groupResult.itemList[i] as TC_LayerGroup;
  225. m_isLast = (i == 0);
  226. m_isFirst = (i == layerGroup.groupResult.itemList.Count - 1);
  227. if (layerGroupChild != null)
  228. {
  229. Draw(layerGroupChild, i != 0, ref pos, activeMulti, m_isFirst, m_isLast, ref m_shelfLineVerticalStartY);
  230. if (!m_isLast)
  231. {
  232. pos.y += layerGroupChild.nodeFoldout ? 0 : 32;
  233. TD.DrawTextureScaledV(pos.x + 64, m_shelfLineVerticalStartY, (pos.y - m_shelfLineVerticalStartY) + 16, TD.texShelfLinesVertical, Color.white);
  234. TD.DrawTextureScaledV(pos.x + 64, m_shelfLineVerticalStartY, (pos.y - m_shelfLineVerticalStartY) + 16, TD.texLineVertical, g.colLayer * activeMulti);
  235. }
  236. ++layerGroupCount;
  237. }
  238. else
  239. {
  240. TC_Layer layer = layerGroup.groupResult.itemList[i] as TC_Layer;
  241. if (layer == null) continue;
  242. TC_LayerGUI.Draw(layer, ref pos, activeMulti, i != 0, m_isFirst, m_isLast);
  243. pos.y += layer.nodeFoldout ? TD.cardHeight : 32;
  244. ++layerCount;
  245. }
  246. }
  247. }
  248. pos.y += 64;
  249. Rect clickRect = TD.GetRectScaled(new Rect(bar2.x + (TD.texCardBody.width * 1.5f) - (2.5f + lineOffsetX), bar2.y - 5f, 10, 10));
  250. if (TD.ClickRect(clickRect, 0))
  251. {
  252. if (layerGroup.foldout == 0) layerGroup.foldout = 2; else layerGroup.foldout = 0;
  253. }
  254. pos.x = posOldX;
  255. }
  256. static void DropDownMenu(Rect rect, TC_LayerGroup layerGroup)
  257. {
  258. if (TD.ClickRect(rect) != 1) return;
  259. GenericMenu menu = new GenericMenu();
  260. // menu.AddItem(new GUIContent("Add Layer"), false, LeftClickMenu, "Add Layer");
  261. string instanceID = layerGroup.GetInstanceID().ToString();
  262. //if (layerGroup.level > 1)
  263. //{
  264. // menu.AddSeparator("");
  265. // menu.AddItem(new GUIContent("Add Layer"), false, LeftClickMenu, instanceID + ":Add Layer");
  266. // menu.AddItem(new GUIContent("Add Layer Group"), false, LeftClickMenu, instanceID + ":Add LayerGroup");
  267. // menu.AddItem(new GUIContent("Duplicate Layer Group"), false, LeftClickMenu, instanceID + ":Duplicate LayerGroup");
  268. // menu.AddSeparator("");
  269. //}
  270. if (layerGroup.groupResult.itemList.Count > 0)
  271. {
  272. //if (layerGroup.level == 0 && layerGroup.outputId == TC.heightOutput)
  273. //{
  274. // menu.AddItem(new GUIContent("Export Heightmap"), false, LeftClickMenu, instanceID + ":Export Heightmap");
  275. // menu.AddSeparator("");
  276. //}
  277. //else if (layerGroup.level == 0 && layerGroup.outputId == TC.colorOutput)
  278. //{
  279. // menu.AddItem(new GUIContent("Export Colormap"), false, LeftClickMenu, instanceID + ":Export Colormap");
  280. // menu.AddSeparator("");
  281. //}
  282. menu.AddItem(new GUIContent("Clear Layer Group"), false, LeftClickMenu, instanceID + ":Clear LayerGroup");
  283. }
  284. // if (layerGroup.level > 1) menu.AddItem(new GUIContent("Erase Layer Group"), false, LeftClickMenu, instanceID + ":Erase LayerGroup");
  285. menu.ShowAsContext();
  286. }
  287. static public void LeftClickMenu(object obj)
  288. {
  289. int instanceID;
  290. string command = TD.ObjectToCommandAndInstanceID(obj, out instanceID);
  291. TC_LayerGroup layerGroup = EditorUtility.InstanceIDToObject(instanceID) as TC_LayerGroup;
  292. if (layerGroup != null)
  293. {
  294. if (command == "Add Mask")
  295. {
  296. layerGroup.maskNodeGroup.Add<TC_Node>("", false, false, true, 0);
  297. }
  298. else if (command == "Add Layer Inside") layerGroup.groupResult.Add<TC_Layer>("", false, true);
  299. else if (command == "Add LayerGroup Inside") layerGroup.groupResult.Add<TC_LayerGroup>("", false, false, true);
  300. else if (command == "Add Layer") layerGroup.groupResult.Add<TC_Layer>("", true, true);
  301. else if (command == "Add LayerGroup") layerGroup.groupResult.Add<TC_LayerGroup>("", true, false, true);
  302. else if (command == "Duplicate LayerGroup") layerGroup.Duplicate(layerGroup.t.parent);
  303. else if (command == "Clear LayerGroup") layerGroup.groupResult.Clear(true);
  304. else if (command == "Erase LayerGroup") layerGroup.DestroyMe(true);
  305. }
  306. }
  307. }
  308. }