public void DrawPortalSelector (Portal exit, Generator.InoutType type) { if (MapMagic.instance.guiGens == null) MapMagic.instance.guiGens = MapMagic.instance.gens; GeneratorsAsset gens = MapMagic.instance.guiGens; //if (MapMagic.instance.guiGens != null) gens = MapMagic.instance.guiGens; int entersNum = 0; for (int g=0; g<gens.list.Length; g++) { Portal portal = gens.list[g] as Portal; if (portal == null) continue; if (portal.form == Portal.PortalForm.Out) continue; if (portal.type != type) continue; entersNum++; } PopupMenu.MenuItem[] popupItems = new PopupMenu.MenuItem[entersNum]; int counter = 0; for (int g=0; g<gens.list.Length; g++) { Portal enter = gens.list[g] as Portal; if (enter == null) continue; if (enter.form == Portal.PortalForm.Out) continue; if (enter.type != type) continue; popupItems[counter] = new PopupMenu.MenuItem( enter.name, delegate () { if (enter.IsDependentFrom(exit)) { Debug.LogError("MapMagic: Linking portals this way will create dependency loop."); return; } exit.input.Link(enter.output, enter); gens.ChangeGenerator(exit); } ); counter++; } PopupMenu.DrawPopup(popupItems, Event.current.mousePosition, closeAllOther:true); }
public void DrawPopup () { //if (MapMagic.instance.guiGens == null) MapMagic.instance.guiGens = MapMagic.instance.gens; //GeneratorsAsset gens = MapMagic.instance.guiGens; //if (MapMagic.instance.guiGens != null) gens = MapMagic.instance.guiGens; Vector2 mousePos = gens.layout.ToInternal(Event.current.mousePosition); //finding something that was clicked Generator clickedGenerator = null; Group clickedGroup = null; Generator.Output clickedOutput = null; for (int i=0; i<gens.list.Length; i++) { Generator gen = gens.list[i]; if (gen.guiRect.Contains(mousePos)) { if (!(gen is Group)) clickedGenerator = gens.list[i]; else clickedGroup = gens.list[i] as Group; } foreach (Generator.Output output in gens.list[i].Outputs()) if (output.guiRect.Contains(mousePos)) clickedOutput = output; } if (clickedGenerator == null) clickedGenerator = clickedGroup; //create Dictionary<string, PopupMenu.MenuItem> itemsDict = new Dictionary<string, PopupMenu.MenuItem>(); foreach (System.Type type in typeof(Generator).Subtypes()) { if (System.Attribute.IsDefined(type, typeof(GeneratorMenuAttribute))) { GeneratorMenuAttribute attribute = System.Attribute.GetCustomAttribute(type, typeof(GeneratorMenuAttribute)) as GeneratorMenuAttribute; System.Type genType = type; if (attribute.disabled) continue; PopupMenu.MenuItem item = new PopupMenu.MenuItem(attribute.name, delegate () { CreateGenerator(genType, mousePos); }); item.priority = attribute.priority; if (attribute.menu.Length != 0) { if (!itemsDict.ContainsKey(attribute.menu)) itemsDict.Add(attribute.menu, new PopupMenu.MenuItem(attribute.menu, subs:new PopupMenu.MenuItem[0])); ArrayTools.Add(ref itemsDict[attribute.menu].subItems, createElement:() => item); } else itemsDict.Add(attribute.name, item); } } itemsDict["Map"].priority = 1; itemsDict["Objects"].priority = 2; itemsDict["Output"].priority = 3; itemsDict["Portal"].priority = 4; itemsDict["Group"].priority = 5; itemsDict["Biome"].priority = 6; itemsDict["Legacy"].priority = 7; PopupMenu.MenuItem[] createItems = new PopupMenu.MenuItem[itemsDict.Count]; itemsDict.Values.CopyTo(createItems, 0); //create group //PopupMenu.MenuItem createGroupItem = new PopupMenu.MenuItem("Group", delegate () { CreateGroup(mousePos); }); //Extensions.ArrayAdd(ref createItems, createItems.Length-1, createGroupItem); //additional name /*string additionalName = "All"; if (clickedGenerator != null) { additionalName = "Generator"; if (clickedGenerator is Group) additionalName = "Group"; }*/ //preview PopupMenu.MenuItem[] previewSubs = new PopupMenu.MenuItem[] { new PopupMenu.MenuItem("On Terrain", delegate() {PreviewOutput(clickedGenerator, clickedOutput, false);}, disabled:clickedOutput==null||clickedGenerator==null, priority:0), new PopupMenu.MenuItem("In Window", delegate() {PreviewOutput(clickedGenerator, clickedOutput, true);}, disabled:clickedOutput==null||clickedGenerator==null, priority:1), new PopupMenu.MenuItem("Clear", delegate() {PreviewOutput(null, null, false);}, priority:2 )//, disabled:MapMagic.instance.previewOutput==null) }; PopupMenu.MenuItem[] popupItems = new PopupMenu.MenuItem[] { new PopupMenu.MenuItem("Create", createItems, priority:0), new PopupMenu.MenuItem("Export", delegate () { ExportGenerator(clickedGenerator, mousePos); }, priority:10), new PopupMenu.MenuItem("Import", delegate () { ImportGenerator(mousePos); }, priority:20), new PopupMenu.MenuItem("Duplicate", delegate () { DuplicateGenerator(clickedGenerator); }, priority:30), new PopupMenu.MenuItem("Update", delegate () { UpdateGenerator(clickedGenerator); }, disabled:clickedGenerator==null || !CouldBeUpdated(clickedGenerator), priority:40), new PopupMenu.MenuItem("Remove", delegate () { if (clickedGenerator!=null) DeleteGenerator(clickedGenerator); }, disabled:clickedGenerator==null, priority:50), new PopupMenu.MenuItem("Reset", delegate () { if (clickedGenerator!=null) ResetGenerator(clickedGenerator); }, disabled:clickedGenerator==null, priority:60), new PopupMenu.MenuItem("Preview", previewSubs, priority:70) }; if (onDrawPopup != null) popupItems = onDrawPopup(popupItems, mousePos, gens, clickedGenerator, clickedGroup, clickedOutput); PopupMenu.DrawPopup(popupItems, Event.current.mousePosition, closeAllOther:true); }
public override void OnGUI(Rect rect) { if (background == null) { background = new Texture2D(1, 1, TextureFormat.RGBA32, false); background.SetPixel(0, 0, new Color(0.98f, 0.98f, 0.98f)); background.Apply(); } if (highlight == null) { highlight = new Texture2D(1, 1, TextureFormat.RGBA32, false); highlight.SetPixel(0, 0, new Color(0.6f, 0.7f, 0.9f)); highlight.Apply(); } //background //if (Event.current.type == EventType.repaint) GUI.skin.box.Draw(fullRect, false, true, true, false); GUI.DrawTexture(new Rect(1, 1, width - 2, lineHeight * items.Length), background, ScaleMode.StretchToFill); //list for (int i = 0; i < items.Length; i++) { MenuItem currentItem = items[i]; currentItem.clicked = false; Rect lineRect = new Rect(1, i * lineHeight + 1, width - 2, lineHeight - 2); bool highlighted = lineRect.Contains(Event.current.mousePosition); if (currentItem.disabled) { highlighted = false; } if (highlighted) { GUI.DrawTexture(lineRect, highlight); } //labels UnityEditor.EditorGUI.BeginDisabledGroup(currentItem.disabled); if (blackLabel == null) { blackLabel = new GUIStyle(UnityEditor.EditorStyles.label); blackLabel.normal.textColor = Color.black; } UnityEditor.EditorGUI.LabelField(new Rect(lineRect.x + 3, lineRect.y, lineRect.width - 3, lineRect.height), currentItem.name, blackLabel); UnityEditor.EditorGUI.EndDisabledGroup(); if (currentItem.hasSubs) { Rect rightRect = lineRect; rightRect.width = 10; rightRect.height = 10; rightRect.x = lineRect.x + lineRect.width - rightRect.width; rightRect.y = lineRect.y + lineRect.height / 2 - rightRect.height / 2; //UnityEditor.EditorGUI.LabelField(rightRect, "\u25B6"); if (triangle == null) { triangle = Resources.Load("PopupMenuTrangleIcon") as Texture2D; } GUI.DrawTexture(rightRect, triangle, ScaleMode.ScaleAndCrop); } //opening subsmenus if (highlighted) { bool clicked = Event.current.type == EventType.MouseDown && Event.current.type == 0; if (clicked && currentItem.onClick != null) { currentItem.onClick(); CloseRecursive(); } //starting timer on selected item change if (currentItem != lastItem) { lastTimestart = System.DateTime.Now; lastItem = currentItem; } //when holding for too long double highlightTime = (System.DateTime.Now - lastTimestart).TotalMilliseconds; if ((highlightTime > 300 && expandedItem != currentItem) || clicked) { //re-opening expanded window if (expandedWindow != null && expandedWindow.editorWindow != null) { expandedWindow.editorWindow.Close(); } expandedWindow = new PopupMenu() { items = currentItem.subItems, parent = this }; expandedItem = currentItem; if (currentItem.subItems != null) { PopupWindow.Show(new Rect(lineRect.max - new Vector2(0, lineHeight), Vector2.zero), expandedWindow); } } } } this.editorWindow.Repaint(); }
public void DrawPopup () { Vector2 mousePos = layout.ToInternal(Event.current.mousePosition); //finding something that was clicked Generator clickedGenerator = null; for (int i=0; i<MapMagic.instance.gens.list.Length; i++) { Generator gen = MapMagic.instance.gens.list[i]; if (gen.guiRect.Contains(mousePos) && !(gen is Group)) clickedGenerator = MapMagic.instance.gens.list[i]; } Group clickedGroup = null; for (int i=0; i<MapMagic.instance.gens.list.Length; i++) { Generator gen = MapMagic.instance.gens.list[i]; if (gen.guiRect.Contains(mousePos) && gen is Group) clickedGroup = MapMagic.instance.gens.list[i] as Group; } if (clickedGenerator == null) clickedGenerator = clickedGroup; Generator.Output clickedOutput = null; for (int i=0; i<MapMagic.instance.gens.list.Length; i++) foreach (Generator.Output output in MapMagic.instance.gens.list[i].Outputs()) if (output.guiRect.Contains(mousePos)) clickedOutput = output; //create Dictionary<string, PopupMenu.MenuItem> itemsDict = new Dictionary<string, PopupMenu.MenuItem>(); List<System.Type> allGeneratorTypes = typeof(Generator).GetAllChildTypes(); for (int i=0; i<allGeneratorTypes.Count; i++) { if (System.Attribute.IsDefined(allGeneratorTypes[i], typeof(GeneratorMenuAttribute))) { GeneratorMenuAttribute attribute = System.Attribute.GetCustomAttribute(allGeneratorTypes[i], typeof(GeneratorMenuAttribute)) as GeneratorMenuAttribute; System.Type genType = allGeneratorTypes[i]; if (attribute.disabled) continue; PopupMenu.MenuItem item = new PopupMenu.MenuItem(attribute.name, delegate () { CreateGenerator(genType, mousePos); }); item.priority = attribute.priority; if (attribute.menu.Length != 0) { if (!itemsDict.ContainsKey(attribute.menu)) itemsDict.Add(attribute.menu, new PopupMenu.MenuItem(attribute.menu, subs:new PopupMenu.MenuItem[0])); ArrayTools.Add(ref itemsDict[attribute.menu].subItems, item); } else itemsDict.Add(attribute.name, item); } } PopupMenu.MenuItem[] createItems = new PopupMenu.MenuItem[itemsDict.Count]; itemsDict.Values.CopyTo(createItems, 0); //create group //PopupMenu.MenuItem createGroupItem = new PopupMenu.MenuItem("Group", delegate () { CreateGroup(mousePos); }); //Extensions.ArrayAdd(ref createItems, createItems.Length-1, createGroupItem); //additional name string additionalName = "All"; if (clickedGenerator != null) { additionalName = "Generator"; if (clickedGenerator is Group) additionalName = "Group"; } //preview PopupMenu.MenuItem[] previewSubs = new PopupMenu.MenuItem[] { new PopupMenu.MenuItem("On Terrain", delegate() {PreviewOutput(clickedGenerator, clickedOutput, false);}, disabled:clickedOutput==null||clickedGenerator==null), new PopupMenu.MenuItem("In Window", delegate() {PreviewOutput(clickedGenerator, clickedOutput, true);}, disabled:clickedOutput==null||clickedGenerator==null), new PopupMenu.MenuItem("Clear", delegate() {PreviewOutput(null, null, false);}, disabled:MapMagic.instance.gens.GetGenerator<PreviewOutput>()==null) }; PopupMenu.MenuItem[] popupItems = new PopupMenu.MenuItem[] { new PopupMenu.MenuItem("Create", createItems), new PopupMenu.MenuItem("Save " + additionalName, delegate () { SaveGenerator(clickedGenerator, mousePos); }), new PopupMenu.MenuItem("Load", delegate () { LoadGenerator(mousePos); }), new PopupMenu.MenuItem("Duplicate", delegate () { DuplicateGenerator(clickedGenerator); }), new PopupMenu.MenuItem("Remove", delegate () { if (clickedGenerator!=null) DeleteGenerator(clickedGenerator); }, disabled:(clickedGenerator==null)), new PopupMenu.MenuItem("Reset", delegate () { if (clickedGenerator!=null) ResetGenerator(clickedGenerator); }, disabled:clickedGenerator==null), new PopupMenu.MenuItem("Preview", previewSubs) }; PopupMenu.DrawPopup(popupItems, Event.current.mousePosition, closeAllOther:true); }