//public static virtual void Process (Chunk chunk) //gui public void DrawHeader(IMapMagic mapMagic, GeneratorsAsset gens, bool debug = false) { //drawing header background layout.Icon("MapMagic_Window_Header", new Rect(layout.field.x, layout.field.y, layout.field.width, 16)); //drawing eye icon layout.Par(14); layout.Inset(2); Rect eyeRect = layout.Inset(18); GeneratorMenuAttribute attribute = System.Attribute.GetCustomAttribute(GetType(), typeof(GeneratorMenuAttribute)) as GeneratorMenuAttribute; if (attribute != null && attribute.disengageable) { layout.Toggle(ref enabled, rect: eyeRect, onIcon: "MapMagic_GeneratorEnabled", offIcon: "MapMagic_GeneratorDisabled"); } else { layout.Icon("MapMagic_GeneratorAlwaysOn", eyeRect, Layout.IconAligment.center, Layout.IconAligment.center); } //drawing label string genName = ""; if (mapMagic != null && debug) { int num = -1; for (int n = 0; n < gens.list.Length; n++) { if (gens.list[n] == this) { num = n; } } genName += num + ". "; } genName += attribute == null? "Unknown" : attribute.name; if (mapMagic != null && debug && !mapMagic.IsGeneratorReady(this)) { genName += "*"; } Rect labelRect = layout.Inset(layout.field.width - 18 - 22); labelRect.height = 25; labelRect.y -= (1f - layout.zoom) * 6 + 2; layout.Label(genName, labelRect, fontStyle: FontStyle.Bold, fontSize: 19 - layout.zoom * 8); //drawing help link Rect helpRect = layout.Inset(22); if (attribute != null && attribute.helpLink != null && attribute.helpLink.Length != 0) { layout.Label("", helpRect, url: attribute.helpLink, icon: "MapMagic_Help"); //if (layout.Button("", helpRect, icon:"MapMagic_Help")) Application.OpenURL(attribute.helpLink); //UnityEditor.EditorGUIUtility.AddCursorRect (layout.ToDisplay(helpRect), UnityEditor.MouseCursor.Link); } layout.Par(4); }
bool CouldBeUpdated (Generator gen) { if (gen == null) return false; System.Type type = gen.GetType(); GeneratorMenuAttribute attribute = System.Attribute.GetCustomAttribute(type, typeof(GeneratorMenuAttribute)) as GeneratorMenuAttribute; if (attribute.menu == "Legacy") return true; else return false; }
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); }
void UpdateGenerator (Generator gen) { if (!updateWarningShowed) { if (EditorUtility.DisplayDialog("Warning", "Updating generator can break your graph. Make a backup before continue", "Go on, I've made a backup", "Cancel")) updateWarningShowed = true; else return; } Undo.RecordObject (gens, "MapMagic Update Generator"); gens.setDirty = !gens.setDirty; //Generator[] copyGens = gens.SmartDuplicateGenerators(gen); System.Type type = gen.GetType(); GeneratorMenuAttribute attribute = System.Attribute.GetCustomAttribute(type, typeof(GeneratorMenuAttribute)) as GeneratorMenuAttribute; //finding type to replace System.Type newType = null; if (attribute.updateType != null) newType = attribute.updateType; else { string name = attribute.name; name = name.Split(new string[] {" (Legacy"}, System.StringSplitOptions.None)[0]; //iterating types starting from current version for (int v=MapMagic.version; v>=0; v--) { //newType = System.Type.GetType("MapMagic." + name + "Generator" + v.ToString()); //does not work newType = System.Reflection.Assembly.Load("Assembly-CSharp").GetType("MapMagic." + name + "Generator" + v.ToString()); if (newType != null) break; } if (newType == null) for (int v=MapMagic.version; v>=0; v--) { newType = System.Reflection.Assembly.Load("Assembly-CSharp").GetType("MapMagic." + name + v.ToString()); //for generators that have no "Generator" in name if (newType != null) break; } } if (newType == null) { Debug.Log("Could not find a proper type to update"); return; } Generator newGen = System.Activator.CreateInstance(newType) as Generator; newGen.ReflectionCopyFrom(gen); //changing links for (int g=0; g<gens.list.Length; g++) foreach (Generator.Input input in gens.list[g].Inputs()) if (input != null && input.linkGen == gen) input.linkGen = newGen; //replacing in array int numInArray = ArrayTools.Find(gens.list, gen); gens.list[numInArray] = newGen; if (mapMagic != null) { mapMagic.ClearResults(gen); //copyGens mapMagic.Generate(); } EditorUtility.SetDirty(gens); }
public void OnGUIBase() { //drawing background layout.Element("MapMagic_Window", layout.field, new RectOffset(34, 34, 34, 34), new RectOffset(33, 33, 33, 33)); //resetting layout layout.field.height = 0; layout.field.width = 160; layout.cursor = new Rect(); layout.change = false; layout.margin = 1; layout.rightMargin = 1; layout.fieldSize = 0.4f; //drawing window header if (MapMagic.instance.guiDebug) { UnityEngine.Profiling.Profiler.BeginSample("Header"); } layout.Icon("MapMagic_Window_Header", new Rect(layout.field.x, layout.field.y, layout.field.width, 16)); //drawing eye icon layout.Par(14); layout.Inset(2); Rect eyeRect = layout.Inset(18); GeneratorMenuAttribute attribute = System.Attribute.GetCustomAttribute(GetType(), typeof(GeneratorMenuAttribute)) as GeneratorMenuAttribute; if (attribute != null && attribute.disengageable) { layout.Toggle(ref enabled, rect: eyeRect, onIcon: "MapMagic_GeneratorEnabled", offIcon: "MapMagic_GeneratorDisabled"); } else { layout.Icon("MapMagic_GeneratorAlwaysOn", eyeRect, Layout.IconAligment.center, Layout.IconAligment.center); } if (layout.lastChange && !enabled && this is IOutput) //hack to refresh on output disable { // MapMagic.instance.ForceGenerate(); foreach (Generator sameOut in MapMagic.instance.gens.OutputGenerators(onlyEnabled:false, checkBiomes:true)) { if (sameOut.GetType() == this.GetType()) { foreach (Chunk chunk in MapMagic.instance.terrains.Objects()) { chunk.ready.CheckRemove(sameOut); //MapMagic.instance.gens.ChangeGenerator(sameOut); } } } //MapMagic.instance.Generate(); //re-generate starts itself because generator changed } //drawing label string genName = attribute == null? "Unknown" : attribute.name; if (MapMagic.instance.guiDebug) { bool generated = true; foreach (Chunk tw in MapMagic.instance.terrains.Objects()) { if (!tw.ready.Contains(this)) { generated = false; } } if (!generated) { genName += "*"; } } Rect labelRect = layout.Inset(); labelRect.height = 25; labelRect.y -= (1f - layout.zoom) * 6 + 2; layout.Label(genName, labelRect, fontStyle: FontStyle.Bold, fontSize: 19 - layout.zoom * 8); layout.Par(1); if (MapMagic.instance.guiDebug) { UnityEngine.Profiling.Profiler.EndSample(); } //gen params if (MapMagic.instance.guiDebug) { UnityEngine.Profiling.Profiler.BeginSample("Gen Params"); } layout.Par(3); if (!MapMagic.instance.guiDebug) { try { OnGUI(); } catch (System.Exception e) { if (e is System.ArgumentOutOfRangeException || e is System.NullReferenceException) { Debug.LogError("Error drawing generator " + GetType() + "\n" + e); } } } else { OnGUI(); } layout.Par(3); if (MapMagic.instance.guiDebug) { UnityEngine.Profiling.Profiler.EndSample(); } //drawing debug generate time if (MapMagic.instance.guiDebug) { Rect timerRect = new Rect(layout.field.x, layout.field.y + layout.field.height, 200, 20); string timeLabel = "g:" + guiDebugTime + "ms "; if (this is IOutput) { if (MapMagic.instance.guiDebugProcessTimes.ContainsKey(this.GetType())) { timeLabel += " p:" + MapMagic.instance.guiDebugProcessTimes[this.GetType()] + "ms "; } if (MapMagic.instance.guiDebugApplyTimes.ContainsKey(this.GetType())) { timeLabel += " a:" + MapMagic.instance.guiDebugApplyTimes[this.GetType()] + "ms "; } } layout.Label(timeLabel, timerRect); //EditorGUI.LabelField(gen.layout.ToLocal(timerRect), gen.timer.ElapsedMilliseconds + "ms"); } }
public void OnGUIBase() { //drawing background layout.Element("MapMagic_Window", layout.field, new RectOffset(34, 34, 34, 34), new RectOffset(33, 33, 33, 33)); //resetting layout layout.field.height = 0; layout.field.width = 160; layout.cursor = new Rect(); layout.change = false; layout.margin = 1; layout.rightMargin = 1; layout.fieldSize = 0.4f; //drawing window header layout.Icon("MapMagic_Window_Header", new Rect(layout.field.x, layout.field.y, layout.field.width, 16)); //drawing eye icon //if (layout.zoom > 0.5f) //{ layout.Par(14); layout.Inset(2); Rect eyeRect = layout.Inset(18); GeneratorMenuAttribute attribute = System.Attribute.GetCustomAttribute(GetType(), typeof(GeneratorMenuAttribute)) as GeneratorMenuAttribute; if (attribute != null && attribute.disengageable) { layout.Toggle(ref enabled, rect: eyeRect, onIcon: "MapMagic_GeneratorEnabled", offIcon: "MapMagic_GeneratorDisabled"); } else { layout.Icon("MapMagic_GeneratorAlwaysOn", eyeRect, Layout.IconAligment.center, Layout.IconAligment.center); } //} //drawing label string genName = attribute == null? "Unknown" : attribute.name; bool generated = true; foreach (MapMagic.Chunk tw in MapMagic.instance.terrains.Objects()) { if (!tw.ready.Contains(this)) { generated = false; } } if (!generated) { genName += "*"; } Rect labelRect = layout.Inset(); labelRect.height = 25; labelRect.y -= (1f - layout.zoom) * 6 + 2; layout.Label(genName, labelRect, fontStyle: FontStyle.Bold, fontSize: 19 - layout.zoom * 8); layout.Par(1); //gen params layout.Par(3); if (!MapMagic.instance.guiDebug) { try { OnGUI(); } catch (System.Exception e) { if (e is System.ArgumentOutOfRangeException || e is System.NullReferenceException) { Debug.LogError("Error drawing generator " + GetType() + "\n" + e); } } } else { OnGUI(); } layout.Par(3); //drawing debug generate time if (MapMagic.instance.guiDebug && timer != null) { Rect timerRect = new Rect(layout.field.x, layout.field.y + layout.field.height, 200, 20); layout.Label(timer.ElapsedMilliseconds + "ms", timerRect); //EditorGUI.LabelField(gen.layout.ToLocal(timerRect), gen.timer.ElapsedMilliseconds + "ms"); } }
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); }