Example #1
0
        public static Item CreateItems(Vector2 mousePos, Graph graph, int priority = 5)
        {
            Item create = new Item("Add (Create)");

            create.onDraw   = RightClick.DrawItem;
            create.icon     = RightClick.texturesCache.GetTexture("MapMagic/Popup/Create");
            create.color    = Color.gray;
            create.subItems = new List <Item>();
            create.priority = priority;

            //automatically adding generators from this assembly
            Type[] types = typeof(Generator).Subtypes();
            for (int t = 0; t < types.Length; t++)
            {
                if (!generatorTypes.Contains(types[t]))
                {
                    generatorTypes.Add(types[t]);
                }
            }

            //adding outer-assembly types
            //via their initialize

            //creating unsorted create items
            foreach (Type type in generatorTypes)
            {
                GeneratorMenuAttribute attribute = GeneratorDraw.GetMenuAttribute(type);
                if (attribute == null)
                {
                    continue;
                }

                string texPath = attribute.iconName ?? "MapMagic/Popup/Standard";
                string texName = texPath;
                //if (StylesCache.isPro) texName += "_icon";

                Item item = new Item( )
                {
                    name     = attribute.name,
                    onDraw   = RightClick.DrawItem,
                    icon     = RightClick.texturesCache.GetTexture(texPath, texName),
                    color    = GeneratorDraw.GetGeneratorColor(attribute.colorType ?? Generator.GetGenericType(type)),
                    onClick  = () => GraphEditorActions.CreateGenerator(graph, type, mousePos),
                    priority = attribute.priority
                };

                //moving into the right section using priority
                //int sectionPriority = 10000 - attribute.section*1000;
                //item.priority += sectionPriority;


                //placing items in categories
                string catName = attribute.menu;
                if (catName == null)
                {
                    continue;                                  //if no 'menu' defined this generator could not be created
                }
                string[] catNameSplit = catName.Split('/');

                Item currCat = create;
                if (catName != "")                  //if empty menu is defined using root category
                {
                    for (int i = 0; i < catNameSplit.Length; i++)
                    {
                        //trying to find category
                        bool catFound = false;
                        if (currCat.subItems != null)
                        {
                            foreach (Item sub in currCat.subItems)
                            {
                                if (sub.onClick == null && sub.name == catNameSplit[i])
                                {
                                    currCat  = sub;
                                    catFound = true;
                                    break;
                                }
                            }
                        }

                        //creating if not found
                        if (!catFound)
                        {
                            Item newCat = new Item(catNameSplit[i]);
                            if (currCat.subItems == null)
                            {
                                currCat.subItems = new List <Item>();
                            }
                            currCat.subItems.Add(newCat);
                            currCat = newCat;

                            newCat.color = item.color;
                        }
                    }
                }

                if (currCat.subItems == null)
                {
                    currCat.subItems = new List <Item>();
                }
                currCat.subItems.Add(item);
            }

            //default sorting order
            foreach (Item item in create.All(true))
            {
                if (item.name == "Map")
                {
                    item.priority = 10004; item.icon = RightClick.texturesCache.GetTexture("MapMagic/Popup/Map"); item.color = Color.gray;
                }
                if (item.name == "Objects" && item.onClick == null)
                {
                    item.priority = 10003; item.icon = RightClick.texturesCache.GetTexture("MapMagic/Popup/Objects"); item.color = Color.gray;
                }
                if (item.name == "Spline")
                {
                    item.priority = 10002; item.icon = RightClick.texturesCache.GetTexture("MapMagic/Popup/Spline"); item.color = Color.gray;
                }
                if (item.name == "Biomes")
                {
                    item.priority = 9999; item.icon = RightClick.texturesCache.GetTexture("MapMagic/Popup/Biomes");
                }

                if (item.name == "Initial")
                {
                    item.priority = 10009; item.icon = RightClick.texturesCache.GetTexture("MapMagic/Popup/Initial");
                }
                if (item.name == "Modifiers")
                {
                    item.priority = 10008; item.icon = RightClick.texturesCache.GetTexture("MapMagic/Popup/Modifier");
                }
                if (item.name == "Standard")
                {
                    item.priority = 10009; item.icon = RightClick.texturesCache.GetTexture("MapMagic/Popup/Standard");
                }
                if (item.name == "Output")
                {
                    item.priority = 10007; item.icon = RightClick.texturesCache.GetTexture("MapMagic/Popup/Output");
                }
                if (item.name == "Outputs")
                {
                    item.priority = 10006; item.icon = RightClick.texturesCache.GetTexture("MapMagic/Popup/Output");
                }
                if (item.name == "Input")
                {
                    item.priority = 10005; item.icon = RightClick.texturesCache.GetTexture("MapMagic/Popup/Input");
                }
                if (item.name == "Inputs")
                {
                    item.priority = 10004; item.icon = RightClick.texturesCache.GetTexture("MapMagic/Popup/Input");
                }
                if (item.name == "Portals")
                {
                    item.priority = 10003; item.icon = RightClick.texturesCache.GetTexture("MapMagic/Popup/Portals");
                }
                if (item.name == "Function")
                {
                    item.priority = 10002; item.icon = RightClick.texturesCache.GetTexture("MapMagic/Popup/Function");
                }

                if (item.name == "Height")
                {
                    item.priority = 10003;
                }
                if (item.name == "Textures")
                {
                    item.priority = 10002;
                }
                if (item.name == "Grass")
                {
                    item.priority = 10001;
                }

                if (item.onDraw == null)
                {
                    item.onDraw = RightClick.DrawItem;
                }
            }

            //adding separator between standard and special categories
            if (create.subItems.FindIndex(i => i.name == "Biomes") >= 0)         //add separator if biomes item present
            {
                Item separator = Item.Separator(priority: 10001);
                separator.onDraw = RightClick.DrawSeparator;
                separator.color  = Color.gray;
                create.subItems.Add(separator);
            }

            return(create);
        }