예제 #1
0
        public void GetChildren()
        {
            if (!IsEnumerable(field.FieldType))
            {
                Verse.Log.Error("Tried to get children when not collection");
                return;
            }
            var valCollection = currentVal as IEnumerable;
            var enumerator    = valCollection.GetEnumerator();

            if (IsDirectEditable(childType))
            {
                directEditableChildren = new List <object>();
                while (enumerator.MoveNext())
                {
                    object cur = enumerator.Current;
                    directEditableChildren.Add(cur);
                }
            }
            else if (!IsEnumerable(childType))
            {
                childDetailWrappers = new List <DetailWrapper>();
                while (enumerator.MoveNext())
                {
                    object        cur     = enumerator.Current;
                    DetailWrapper details = new DetailWrapper(cur, null, true, this);
                    childDetailWrappers.Add(details);
                }
            }
        }
예제 #2
0
        public bool TryAddNewIndirect()
        {
            if (!IsEnumerable(field.FieldType))
            {
                Verse.Log.Error("Tried to add indirect to non-collection");
                Reset();
                return(false);
            }
            if (!isAddable)
            {
                Verse.Log.Error("Tried to make new object of type that that can't be insantiated, in addNewIndirect");
                Reset();
                return(false);
            }

            object     newObj         = Activator.CreateInstance(childType, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, null, null);
            Type       collectionType = field.FieldType;
            MethodInfo add            = AccessTools.Method(collectionType, "Add");

            add.Invoke(currentVal, new object[] { newObj });
            DetailWrapper details = new DetailWrapper(newObj, null, true, this);

            childDetailWrappers.Add(details);

            string command = "".Get(field).Add().New(childType);

            AddCommand(command);
            return(true);
        }
예제 #3
0
        public bool TryRemoveIndirect(DetailWrapper childSection)
        {
            if (!IsEnumerable(field.FieldType))
            {
                Verse.Log.Error("Tried to remove indirect from non-collection");
                Reset();
                return(false);
            }
            if (!childrenHaveDirectChildren)
            {
                Verse.Log.Error("Tried to remove indirect but no direct editable fields");
                Reset();
                return(false);
            }
            object targetObj = childSection.parentObject; // of childType

            // make command, have to compare directly editable field values and hope we get the right object
            string command = "".Get(field).Remove();

            GenerateFromCommand(childType, targetObj, ref command);
            AddCommand(command);
            Type       collectionType = field.FieldType;
            MethodInfo remove         = AccessTools.Method(collectionType, "Remove");

            remove.Invoke(currentVal, new object[] { targetObj });
            childDetailWrappers.Remove(childSection);
            return(true);
        }
예제 #4
0
 public DetailSection(object parent,
                      string label,
                      FieldInfo[] fields,
                      DetailWrapper parentWrapper,
                      bool align           = true,
                      FieldDesc parentDesc = null)
 {
     if (fields == null)
     {
         Log.Warning("Building DetailSection with empty fields");
     }
     this.parentObject = parent;
     this.parentDesc   = parentDesc;
     rawFields         = fields;
     Label             = label;
     wrapper           = parentWrapper;
     //if (parent is Def)
     //	ParentFieldDescs = !DatabaseBuilder.allDefFields.NullOrEmpty() ? DetailSectionHelper.BuildFieldDescList(parent, DatabaseBuilder.allDefFields.ToArray(), parentDesc) : null;
     Align = align;
     if (label != null)
     {
         InsetString = DefaultInsetString;
         Inset       = DefaultInset;
     }
     else
     {
         InsetString = string.Empty;
         Inset       = 0f;
     }
 }
예제 #5
0
        public static ItemDetail MakeItemDetail <T>(T def, DetailCategory category) where T : Def
        {
            var itemDef = new ItemDetail();

            itemDef.keyObject = def;
            if (def.label == null || def.label == "")
            {
                itemDef.label = def.defName;
            }
            else
            {
                itemDef.label = def.LabelCap;
            }
            itemDef.category    = category;
            itemDef.description = def.description;

            DetailWrapper subDefSection = new DetailWrapper(def, category, true, null);

            itemDef.HelpDetailWrappers.Add(subDefSection);
            return(itemDef);
        }
예제 #6
0
        public void DrawExpanded(ref Vector3 cur, float lineHeight)
        {
            if (!IsEnumerable(field.FieldType))
            {
                if (childDetailWrappers == null)
                {
                    childDetailWrappers = new List <DetailWrapper>();
                    DetailWrapper details = new DetailWrapper(currentVal, null, true, this);
                    childDetailWrappers.Add(details);
                }
                childDetailWrappers[0].Draw(ref cur, cur.z);
            }
            else
            {
                float oldx           = cur.x;
                float newz           = cur.x;
                bool  directEditable = IsDirectEditable(childType);
                if ((!directEditable && childDetailWrappers == null) || (directEditable && directEditableChildren == null))
                {
                    GetChildren();
                }
                if ((!directEditable && childDetailWrappers.Count == 0) || (directEditable && directEditableChildren.Count == 0))
                {
                    string emptyString = "(empty)";
                    float  emptyWidth  = Text.CalcSize(emptyString).x;
                    DrawLabels(ref cur, new Vector3(0, emptyWidth, 0), lineHeight, null, emptyString);
                    cur.y += lineHeight;
                    newz   = Mathf.Max(newz, cur.x);
                    cur.x  = oldx;
                }
                if (directEditable)
                {
                    foreach (object child in directEditableChildren ?? Enumerable.Empty <object>())
                    {
                        Rect changeRect = new Rect(cur.x, cur.y, lineHeight, lineHeight);
                        bool delete     = Widgets.ButtonText(changeRect, "-");
                        if (delete)
                        {
                            if (TryRemoveDirect(child))
                            {
                                break;
                            }
                        }
                        cur.x += lineHeight + 2f;

                        Rect   valRect    = new Rect(cur.x, cur.y, 1f, lineHeight);
                        string typeString = child.GetType().ToString();
                        float  typeWidth  = Text.CalcSize(typeString).x + 10f;
                        string valString  = GetValueString(child);
                        float  valWidth   = Text.CalcSize(valString).x;

                        Vector3 childWidths = new Vector3(typeWidth, valWidth, 0);
                        DrawLabels(ref cur, childWidths, lineHeight, typeString, valString);

                        valRect.width = (cur.x - valRect.x);
                        if (Mouse.IsOver(valRect) && Event.current.type == EventType.MouseDown && Event.current.button == 1 && typeof(Def).IsAssignableFrom(childType) && currentVal != null)
                        {
                            var options = new List <FloatMenuOption>();
                            options.Add(new FloatMenuOption("Jump to " + GetValueString(child), () => RightClickHandler(child), MenuOptionPriority.High, null));
                            Verse.Find.WindowStack.Add(new FloatMenu(options));
                            Event.current.Use();
                        }

                        cur.y += lineHeight;
                        newz   = Mathf.Max(newz, cur.x);
                        cur.x  = oldx;
                    }
                    DrawDirectAdd(ref cur, lineHeight);
                    cur.y += lineHeight;
                    newz   = Mathf.Max(newz, cur.x);
                    cur.x  = oldx;
                }
                else
                {
                    foreach (DetailWrapper child in childDetailWrappers ?? Enumerable.Empty <DetailWrapper>())
                    {
                        Rect changeRect = new Rect(cur.x, cur.y, lineHeight, lineHeight);
                        bool delete     = Widgets.ButtonText(changeRect, "-");
                        if (delete)
                        {
                            if (TryRemoveIndirect(child))
                            {
                                break;
                            }
                        }
                        cur.x += lineHeight + 2f;

                        string  typeString  = child.parentObject.GetType().ToString();
                        float   typeWidth   = Text.CalcSize(typeString).x + 10f;
                        string  valString   = GetValueString(child.parentObject);
                        float   valWidth    = Text.CalcSize(valString).x;
                        Vector3 childWidths = new Vector3(typeWidth, valWidth, 0);
                        DrawLabels(ref cur, childWidths, lineHeight, typeString, valString);

                        float arrowSize  = lineHeight / 2f;
                        Rect  expandRect = new Rect(cur.x, cur.y + arrowSize / 2f, arrowSize, arrowSize);
                        bool  expand     = Widgets.ButtonImage(expandRect, child.expanded ? ResourceBank.Icon.HelpMenuArrowDown : ResourceBank.Icon.HelpMenuArrowRight);
                        if (expand)
                        {
                            child.expanded = !child.expanded;
                        }

                        cur.y += lineHeight;
                        newz   = Mathf.Max(newz, cur.x);
                        cur.x  = oldx;
                        if (child.expanded)
                        {
                            child.Draw(ref cur, cur.z);
                        }
                    }
                    if (isAddable)
                    {
                        Rect detailAddRect = new Rect(cur.x, cur.y, lineHeight, lineHeight);
                        bool add           = Widgets.ButtonText(detailAddRect, "+");
                        if (add)
                        {
                            TryAddNewIndirect();
                        }
                        cur.y += lineHeight;
                    }
                    cur.x = newz;
                }
            }
        }