Esempio n. 1
0
        private IGuiStyle FindDefaultStyle(StyleTypeNode root, System.Type elementType)
        {
            IGuiStyle result = null;

            if (root != null)
            {
                if (root.RegisteredType == elementType)
                {
                    result = root.DefaultStyle;
                }
                else
                {
                    foreach (StyleTypeNode child in root.Children)
                    {
                        if (elementType.IsSubclassOf(child.RegisteredType))
                        {
                            result = FindDefaultStyle(child, elementType);
                        }
                        else if (elementType == child.RegisteredType)
                        {
                            result = child.DefaultStyle;
                        }
                    }

                    if (result == null)
                    {
                        result = root.DefaultStyle;
                    }
                }
            }

            return(result);
        }
Esempio n. 2
0
 public StyleTypeNode(StyleTypeNode copy)
 {
     mRegisteredType = copy.RegisteredType;
     mDefaultStyle   = copy.DefaultStyle;
     mChildren       = new List <StyleTypeNode>(copy.Children);
     mParent         = copy.Parent;
 }
Esempio n. 3
0
        private static void InsertStyleDefault(StyleTypeNode root, System.Type type, IGuiStyle style)
        {
            if (type.IsSubclassOf(root.RegisteredType))
            {
                // Construct the branch to this type
                StyleTypeNode newBranchRoot = root.Find(type);
                if (newBranchRoot == null)
                {
                    newBranchRoot = new StyleTypeNode(type, style);

                    // Create empty elements up a known point in the tree
                    StyleTypeNode ancestor = FindClosestAncestor(root, type);

                    while (newBranchRoot.RegisteredType.BaseType != ancestor.RegisteredType)
                    {
                        StyleTypeNode oldBranchRoot = new StyleTypeNode(newBranchRoot);
                        newBranchRoot        = new StyleTypeNode(oldBranchRoot.RegisteredType.BaseType, null);
                        oldBranchRoot.Parent = newBranchRoot;
                        newBranchRoot.Children.Add(oldBranchRoot);
                    }

                    newBranchRoot.Parent = ancestor;
                    ancestor.Children.Add(newBranchRoot);
                }
            }
            else
            {
                throw new ArgumentException("Argument type(" + type.Name + ") must be a subclass of root.RegisteredType(" + root.RegisteredType.Name + ")");
            }
        }
Esempio n. 4
0
        private List <string> ListDefaults(StyleTypeNode root, uint level)
        {
            StringBuilder thisLine = new StringBuilder();

            for (uint i = 0; i < level; ++i)
            {
                thisLine.Append("\t");
            }
            thisLine.Append(root.RegisteredType.Name);
            thisLine.Append(" => ");
            if (root.DefaultStyle != null)
            {
                thisLine.Append(root.DefaultStyle.Name);
            }
            else
            {
                thisLine.Append("null");
            }
            List <string> result = new List <string>();

            result.Add(thisLine.ToString());
            foreach (StyleTypeNode child in root.Children)
            {
                result.AddRange(ListDefaults(child, level + 1));
            }

            return(result);
        }
Esempio n. 5
0
        private void LoadDefaultStylesFromFactory(XmlGuiFactory factory)
        {
            IEnumerable <IGuiStyle> styles = factory.ConstructAllStyles();

            // Find the root style
            foreach (IGuiStyle style in styles)
            {
                if (style != null)
                {
                    foreach (System.Type t in style.DefaultFor())
                    {
                        if (t == typeof(GuiElement))
                        {
                            mDefaultStylesRoot = new StyleTypeNode(t, style);
                            break;
                        }
                    }

                    if (mDefaultStylesRoot != null)
                    {
                        break;
                    }
                }
            }

            if (mDefaultStylesRoot == null)
            {
                throw new Exception("Cannot load default styles, no default for GuiElement was defined.");
            }

            // Insert each style into the tree
            foreach (IGuiStyle style in styles)
            {
                if (style != null)
                {
                    foreach (System.Type t in style.DefaultFor())
                    {
                        if (t != typeof(GuiElement))
                        {                         // GuiElement should have already been inserted
                            InsertStyleDefault(mDefaultStylesRoot, t, style);
                        }
                    }
                }
            }

            /*
             * // Debug.Log the loaded heiarchy of style defaults
             * StringBuilder sb = new StringBuilder("\n");
             * foreach( string line in ListDefaults(mDefaultStylesRoot, 0) )
             * {
             *      sb.AppendLine(line);
             * }
             * Debug.Log(sb);
             */
        }
Esempio n. 6
0
        private static StyleTypeNode FindClosestAncestor(StyleTypeNode root, System.Type type)
        {
            if (root.RegisteredType == type.BaseType || root.Children.Count == 0)
            {
                return(root);
            }

            foreach (StyleTypeNode child in root.Children)
            {
                if (type.IsSubclassOf(child.RegisteredType))
                {
                    return(FindClosestAncestor(child, type));
                }
            }

            return(root);
        }
Esempio n. 7
0
 //Currently a linear search, can easily be optimized if necessary
 public StyleTypeNode Find(System.Type t)
 {
     if (t == mRegisteredType)
     {
         return(this);
     }
     else
     {
         foreach (StyleTypeNode child in this.Children)
         {
             StyleTypeNode result = child.Find(t);
             if (result != null)
             {
                 return(result);
             }
         }
     }
     return(null);
 }