Ejemplo n.º 1
0
        private bool GetMatchingChildrenInner(
            UnityComponent component, bool pseudoElement, List <UnityComponent> list, UnityComponent scope, bool singleItem, List <RuleTreeNode <T> > leafList)
        {
            var matches = leafList.Any(x => x.Matches(component, scope));

            if (matches)
            {
                list.Add(component);
            }
            if (matches && singleItem)
            {
                return(true);
            }

            if (component is ContainerComponent cmp)
            {
                foreach (var child in cmp.Children)
                {
                    var childMatches = GetMatchingChildrenInner(child, pseudoElement, list, scope, singleItem, leafList);
                    if (childMatches && singleItem)
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
Ejemplo n.º 2
0
        public UnityComponent GetMatchingChild(UnityComponent component, bool pseudoElement = false)
        {
            var list = new List <UnityComponent>();

            GetMatchingChildrenInner(component, pseudoElement, list, component, true, LeafNodes);
            return(list.FirstOrDefault());
        }
Ejemplo n.º 3
0
        public List <UnityComponent> GetMatchingChildren(UnityComponent component, bool pseudoElement = false)
        {
            var list = new List <UnityComponent>();

            GetMatchingChildrenInner(component, pseudoElement, list, component, false, LeafNodes);
            return(list);
        }
Ejemplo n.º 4
0
        public override void Visit(UnityComponent component)
        {
            switch (component)
            {
            case TextComponent t:
                Text += t.Text.text;
                break;

            default:
                break;
            }
        }
Ejemplo n.º 5
0
        public void setEventListener(UnityComponent element, string eventType, JsValue value)
        {
            var hasValue = value != null && !value.IsNull() && !value.IsUndefined() && !value.IsBoolean();
            var callback = value.As <FunctionInstance>();

            if (hasValue && callback == null)
            {
                throw new Exception("The callback for an event must be a function.");
            }

            element.SetEventListener(eventType, new Callback(callback));
        }
Ejemplo n.º 6
0
        public static UnityComponent createElement(string tag, string text, HostComponent host)
        {
            UnityComponent res = null;

            if (ComponentCreators.TryGetValue(tag, out var creator))
            {
                res = creator(tag, text, host.Context);
            }
            else
            {
                throw new Exception($"Unknown component tag '{tag}' specified.");
            }
            res.GameObject.name = $"<{tag}>";
            return(res);
        }
Ejemplo n.º 7
0
        public UnityComponent createElement(string tag, string text, HostComponent host)
        {
            UnityComponent res = null;

            if (ComponentCreators.TryGetValue(tag, out var creator))
            {
                res = creator(tag, text, host.Context);
            }
            else
            {
                res = defaultCreator(tag, text, host.Context);
            }
            res.GameObject.name = $"<{tag}>";
            return(res);
        }
Ejemplo n.º 8
0
        public bool Matches(UnityComponent component, UnityComponent scope)
        {
            if (!ThisMatches(component, scope))
            {
                return(false);
            }

            // We are at root, all rules matched
            if (Parent == null)
            {
                return(true);
            }

            var relative = component;
            var runOnce  = RelationType == RuleRelationType.DirectSibling || RelationType == RuleRelationType.DirectParent || RelationType == RuleRelationType.Self;

            while (relative != null)
            {
                if (RelationType == RuleRelationType.Parent || RelationType == RuleRelationType.DirectParent)
                {
                    relative = relative.Parent;
                }
                else if (RelationType == RuleRelationType.Sibling || RelationType == RuleRelationType.DirectSibling)
                {
                    if (relative.Parent == null)
                    {
                        return(false);
                    }
                    var ind = relative.Parent.Children.IndexOf(relative);
                    if (ind == 0)
                    {
                        return(false);
                    }
                    relative = relative.Parent.Children[ind - 1];
                }

                if (Parent.Matches(relative, scope))
                {
                    return(true);
                }
                if (runOnce)
                {
                    return(false);
                }
            }

            return(false);
        }
Ejemplo n.º 9
0
        private bool ThisMatches(UnityComponent component, UnityComponent scope)
        {
            // We are at root, all rules matched
            if (ParsedSelector == null)
            {
                return(true);
            }

            // We reached the end of component hierarchy and there are still rules to process
            // This means the matching is incomplete
            if (component == null)
            {
                return(false);
            }

            return(ParsedSelector.All(x => x.Negated ^ x.Matches(component, scope)));
        }
Ejemplo n.º 10
0
 public string Get(UnityComponent component)
 {
     Text = "";
     component.Accept(this);
     return(Text);
 }
Ejemplo n.º 11
0
        public bool Matches(UnityComponent component, UnityComponent scope = null)
        {
            if (component.IsPseudoElement)
            {
                return(Type == RuleSelectorPartType.Tag && Name == component.Tag);
            }

            switch (Type)
            {
            case RuleSelectorPartType.None:
                return(false);

            case RuleSelectorPartType.All:
                return(true);

            case RuleSelectorPartType.Tag:
                if (Name != null && Name.StartsWith("_"))
                {
                    return(component.GameObject.name == Name);
                }
                return(Name == component.Tag);

            case RuleSelectorPartType.Id:
                return(Name == component.GameObject.name);

            case RuleSelectorPartType.ClassName:
                return(component.ClassList != null && component.ClassList.Contains(Name));

            case RuleSelectorPartType.Attribute:
                return(component.Data.TryGetValue(Name, out var val) && Equals(val, Parameter));

            case RuleSelectorPartType.DirectDescendant:
            case RuleSelectorPartType.AdjacentSibling:
            case RuleSelectorPartType.Sibling:
            case RuleSelectorPartType.Self:
                return(true);

            case RuleSelectorPartType.Not:
                break;

            case RuleSelectorPartType.FirstChild:
                return(component.Parent.Children[0] == component);

            case RuleSelectorPartType.LastChild:
                return(component.Parent.Children[component.Parent.Children.Count - 1] == component);

            case RuleSelectorPartType.NthChild:
                return(((NthChildParameter)Parameter).Matches(component.Parent.Children.IndexOf(component) + 1));

            case RuleSelectorPartType.NthLastChild:
                return(((NthChildParameter)Parameter).Matches(component.Parent.Children.Count - component.Parent.Children.IndexOf(component)));

            case RuleSelectorPartType.Empty:
                var cmp = component as ContainerComponent;
                return(cmp == null || cmp.Children.Count == 0);

            case RuleSelectorPartType.OnlyChild:
                return(component.Parent.Children.Count == 1);

            case RuleSelectorPartType.Root:
                return(component is HostComponent);

            case RuleSelectorPartType.Scope:
                return(scope != null && component == scope);

            case RuleSelectorPartType.Before:
            case RuleSelectorPartType.After:
                return(true);

            case RuleSelectorPartType.Hover:
            case RuleSelectorPartType.Focus:
            case RuleSelectorPartType.FocusVisible:
            case RuleSelectorPartType.FocusWithin:
            case RuleSelectorPartType.Active:
                return(true);

            case RuleSelectorPartType.Important:
            case RuleSelectorPartType.Special:
                return(true);

            case RuleSelectorPartType.State:
                return(component.StateStyles.GetState(Parameter as string));

            default:
                break;
            }

            return(false);
        }
Ejemplo n.º 12
0
 public static void insertBefore(ContainerComponent parent, UnityComponent child, UnityComponent beforeChild)
 {
     child.SetParent(parent, beforeChild);
     parent.ScheduleLayout();
 }
Ejemplo n.º 13
0
 public IEnumerable <RuleTreeNode <T> > GetMatchingAfter(UnityComponent component)
 {
     return(AfterNodes.Where(x => x.Matches(component, null)));
 }
Ejemplo n.º 14
0
 public static void removeChild(ContainerComponent parent, UnityComponent child)
 {
     child.Destroy();
     parent.ScheduleLayout();
 }
Ejemplo n.º 15
0
 public static void appendChildToContainer(HostComponent parent, UnityComponent child)
 {
     child.SetParent(parent);
     parent.ScheduleLayout();
 }
Ejemplo n.º 16
0
 public abstract void Visit(UnityComponent component);
Ejemplo n.º 17
0
 public static void setProperty(UnityComponent element, string property, object value)
 {
     element.SetProperty(property, value);
 }