Example #1
0
        /// <summary>
        /// Creates a subcategory based on subcategoryDef in categoryDef at position, containing elements buildableDefs.
        /// Position defaults to adding on the right.
        ///
        /// Note that if designators for the buildables in buildableDefs already exist, they will NOT be removed - this method
        /// creates NEW designators, and is primarily meant for mods that programatically generate defs.
        /// </summary>
        /// <param name="categoryDef"></param>
        /// <param name="subcategoryDef"></param>
        /// <param name="buildableDefs"></param>
        /// <param name="position"></param>
        public static void AddSubCategory(DesignationCategoryDef categoryDef, DesignationSubCategoryDef subcategoryDef,
                                          List <BuildableDef> buildableDefs, int position = -1)
        {
            // cop out on null
            if (categoryDef == null)
            {
                throw new ArgumentNullException(nameof(categoryDef));
            }

            // get designation category's resolved designators
            List <Designator> resolvedDesignators = categoryDef.AllResolvedDesignators;

            // check position argument
            if (position > resolvedDesignators.Count)
            {
                throw new ArgumentOutOfRangeException(nameof(position));
            }

            // hide existing designators
            foreach (BuildableDef def in buildableDefs)
            {
                HideDesignator(def);
            }

            // create subcategory
            var subcategory = new Designator_SubCategory(subcategoryDef,
                                                         buildableDefs.Select(bd => new Designator_Build(bd))
                                                         .ToList());

            // if no position is specified, add it at the end
            if (position < 0)
            {
                resolvedDesignators.Add(subcategory);
            }
            else
            {
                resolvedDesignators.Insert(position, subcategory);
            }
        }
Example #2
0
 // default constructor from ThingDef, forwarded to base.
 public Designator_SubCategoryItem(ThingDef entDef, Designator_SubCategory subCategory) : base(entDef)
 {
     this.subCategory = subCategory;
 }
Example #3
0
 // constructor from Designator_Build, links to constructor from ThingDef.
 public Designator_SubCategoryItem(Designator_Build designator, Designator_SubCategory subCategory)
     : base(designator.PlacingDef)
 {
     this.subCategory = subCategory;
 }
        private static void CreateSubCategories()
        {
            Logger.Debug("Creating subcategories");
            foreach (DesignationSubCategoryDef category in DefDatabase <DesignationSubCategoryDef> .AllDefsListForReading
                     )
            {
                if (category.debug)
                {
                    Logger.Message("Creating subcategory {0} in category {1}", category.LabelCap,
                                   category.designationCategory);
                }

                // cop out if main cat not found
                if (category.designationCategory == null)
                {
                    Logger.Warning("Category {0} not found! Skipping.", category.designationCategory);
                    continue;
                }

                // set up sub category
                var designators = new List <Designator_Build>();

                // keep track of best position for the subcategory - it will replace the first subitem in the original category.
                int firstDesignatorIndex = -1;

                // get list of current designators in the category
                List <Designator> resolvedDesignators = category.designationCategory.AllResolvedDesignators;

                // start adding designators to the subcategory
                if (category.defNames != null)
                {
                    foreach (string defName in category.defNames)
                    {
                        BuildableDef bdef = DefDatabase <ThingDef> .GetNamedSilentFail(defName) ??
                                            (BuildableDef)DefDatabase <TerrainDef> .GetNamedSilentFail(defName);

                        // do some common error checking
                        // buildable def exists
                        if (bdef == null)
                        {
                            if (category.debug)
                            {
                                Logger.Warning("ThingDef {0} not found! Skipping.", defName);
                            }
                            continue;
                        }

                        // find the designator for this buildabledef
                        DesignationCategoryDef designatorCategory;
                        var bdefDesignator = FindDesignator(bdef, out designatorCategory);
                        if (category.debug && bdefDesignator == null)
                        {
                            Log.Warning("No designator found with matching entity def! Skipping.");
                        }

                        // if not null, add designator to the subcategory, and remove from main category
                        if (bdefDesignator != null)
                        {
                            // if taken designator was in the same category as the new subcategory, find index and update FirstDesignatorIndex
                            if (designatorCategory == category.designationCategory)
                            {
                                int index = resolvedDesignators.IndexOf(bdefDesignator);
                                if (firstDesignatorIndex < 0 || index < firstDesignatorIndex)
                                {
                                    firstDesignatorIndex = index;
                                }
                            }

                            designators.Add(bdefDesignator);
                            HideDesignator(bdefDesignator);

                            if (category.debug)
                            {
                                Logger.Message("ThingDef {0} passed checks and was added to subcategory.", defName);
                            }
                        }
                        // done with this designator
                    }
                }

                // check if any designators were added to subdesignator
                if (!designators.NullOrEmpty())
                {
                    // create subcategory
                    var subCategory = new Designator_SubCategory(category, designators);

                    // insert to replace first designator removed, or just add at the end if taken from different categories
                    if (firstDesignatorIndex >= 0)
                    {
                        resolvedDesignators.Insert(firstDesignatorIndex, subCategory);
                    }
                    else
                    {
                        resolvedDesignators.Add(subCategory);
                    }

                    if (category.debug)
                    {
                        Logger.Message("Subcategory {0} created.", subCategory.LabelCap);
                    }
                }
                else if (category.debug)
                {
                    Logger.Warning("Subcategory {0} did not have any (resolved) contents! Skipping.", category.LabelCap);
                }
            }
        }