Esempio n. 1
0
    void Rebuild()
    {
        Debug.Log("Rebuilding");
        solver.reset();

        if (canvas != null)
        {
            if (!vars.ContainsKey(canvas))
            {
                vars[canvas] = new WrappedRect(canvas);
            }

            updateScreenConstraints();
            IUIConstraint[] cons = canvas.gameObject.GetComponentsInChildren <IUIConstraint>(true);
            foreach (IUIConstraint con in cons)
            {
                cassowary.Constraint st = con.getConstraint(vars);
                if (st == null)
                {
                    continue;
                }
                try { solver.addConstraint(st); } catch { }
            }

            foreach (UIConnection con in connections)
            {
                cassowary.Constraint st = con.getConstraint(vars);
                try { solver.addConstraint(st); } catch { }
            }
            solver.updateVariables();
            needs_rebuild = true;
        }
    }
Esempio n. 2
0
    public cassowary.Constraint getConstraint(Dictionary <RectTransform, WrappedRect> dct)
    {
        WrappedRect f1;
        WrappedRect f2;

        cassowary.Expression ex1;
        cassowary.Expression ex2;
        if (first == null && second == null)
        {
            return(null);
        }
        if (first != null)
        {
            if (!dct.TryGetValue(first, out f1))
            {
                f1         = new WrappedRect(first);
                dct[first] = f1;
            }
            ex1 = f1.getTermFromSide(firstSide) * firstcoef;
        }
        else
        {
            ex1 = new cassowary.Expression(firstcoef);
        }
        if (second != null)
        {
            if (!dct.TryGetValue(second, out f2))
            {
                f2          = new WrappedRect(second);
                dct[second] = f2;
            }

            ex2 = f2.getTermFromSide(secondSide) * secondcoef;
        }
        else
        {
            ex2 = new cassowary.Expression(secondcoef);
        }
        switch (relation)
        {
        case Relation.Equal:
        {
            return(cassowary.Constraint.Eq(ex1 + freecoef, ex2) | cassowary.Strength.fromEnum(strength));
        }

        case Relation.GreaterThan:
        {
            return(cassowary.Constraint.Ge(ex1 + freecoef, ex2) | cassowary.Strength.fromEnum(strength));
        }

        case Relation.LessThan:
        {
            return(cassowary.Constraint.Le(ex1 + freecoef, ex2) | cassowary.Strength.fromEnum(strength));
        }
        }

        return(null);
    }
Esempio n. 3
0
 void Start()
 {
     if (canvas != null && !vars.ContainsKey(canvas))
     {
         vars[canvas] = new WrappedRect(canvas);
     }
     if (Screen.width != prevScreen.x)
     {
         Rebuild();
         //   updateScreenConstraints();
     }
     instance = this;
 }
Esempio n. 4
0
    // Start is called before the first frame update
    void updateScreenConstraints()
    {
        if (canvas == null)
        {
            return;
        }



        solver.addConstraint(cassowary.Constraint.Eq(vars[canvas].getTermFromSide(rectSide.Top), 0));
        solver.addConstraint(cassowary.Constraint.Eq(vars[canvas].getTermFromSide(rectSide.Left), 0));
        cassowary.Constraint newx = cassowary.Constraint.Eq(vars[canvas].getTermFromSide(rectSide.Right), Screen.width);
        cassowary.Constraint newy = cassowary.Constraint.Eq(vars[canvas].getTermFromSide(rectSide.Bottom), Screen.height);
        try
        {
            solver.addConstraint(newx);
        }
        catch { }
        try
        {
            solver.addConstraint(newy);
        }
        catch { }

        if (safeArea != null)
        {
            if (!vars.ContainsKey(safeArea))
            {
                vars[safeArea] = new WrappedRect(safeArea);
            }
            Rect rs = new Rect(Screen.safeArea.xMin, Screen.height - Screen.safeArea.yMax, Screen.safeArea.width, Screen.safeArea.height);
            Debug.LogFormat("Safe Area: {0}", rs);
            solver.addConstraint(cassowary.Constraint.Eq(vars[safeArea].getTermFromSide(rectSide.Top), rs.yMin));
            solver.addConstraint(cassowary.Constraint.Eq(vars[safeArea].getTermFromSide(rectSide.Left), rs.xMin));
            cassowary.Constraint snewx = cassowary.Constraint.Eq(vars[safeArea].getTermFromSide(rectSide.Right), rs.xMax);
            cassowary.Constraint snewy = cassowary.Constraint.Eq(vars[safeArea].getTermFromSide(rectSide.Bottom), rs.yMax);
            try
            {
                solver.addConstraint(snewx);
            }
            catch { }
            try
            {
                solver.addConstraint(snewy);
            }
            catch { }
        }
        prevScreen = new Vector2(Screen.width, Screen.height);
        Debug.Log(prevScreen);
        Debug.Log(Screen.width);
    }
Esempio n. 5
0
    public cassowary.Constraint getConstraint(Dictionary <RectTransform, WrappedRect> dct)
    {
        cassowary.Expression ex = new cassowary.Expression();
        foreach (SingleTerm term in terms)
        {
            if (term.rect == null)
            {
                ex = ex + new cassowary.Expression(term.coef);
            }
            else
            {
                WrappedRect f1;
                if (!dct.TryGetValue(term.rect, out f1))
                {
                    f1             = new WrappedRect(term.rect);
                    dct[term.rect] = f1;
                }
                ex = ex + f1.getTermFromSide(term.side) * term.coef;
            }
        }

        switch (relation)
        {
        case Relation.Equal:
        {
            return(cassowary.Constraint.Eq(ex, 0) | cassowary.Strength.fromEnum(strength));
        }

        case Relation.GreaterThan:
        {
            return(cassowary.Constraint.Ge(ex, 0) | cassowary.Strength.fromEnum(strength));
        }

        case Relation.LessThan:
        {
            return(cassowary.Constraint.Le(ex, 0) | cassowary.Strength.fromEnum(strength));
        }
        }

        return(null);
    }
Esempio n. 6
0
    // Update is called once per frame
    void Update()
    {
        if (Screen.width != prevScreen.x || Screen.height != prevScreen.y)
        {
            needs_rebuild = true;
        }
        //
        if (needs_rebuild)
        {
            Rebuild();
        }
        if (needs_reassign || needs_rebuild)
        {
            Dictionary <RectTransform, WrappedRect> newVars = new Dictionary <RectTransform, WrappedRect>();
            foreach (var e in vars)
            {
                if (e.Value.linkedTransform != null)
                {
                    newVars.Add(e.Key, e.Value);
                }
            }
            vars = newVars;
            WrappedRect[] transfs = new WrappedRect[vars.Count];
            vars.Values.CopyTo(transfs, 0);
            Array.Sort <WrappedRect>(transfs, delegate(WrappedRect u1, WrappedRect u2)
            {
                return(u1.DistToCanvas(canvas).CompareTo(u2.DistToCanvas(canvas)));
            });
            foreach (var v in transfs)
            {
                if (v.linkedTransform != canvas)
                {
                    Vector3[]     cor = new Vector3[4];
                    RectTransform par = v.linkedTransform.parent.GetComponent <RectTransform>();
                    if (par != null)
                    {
                        par.GetWorldCorners(cor);
                    }
                    else
                    {
                        canvas.GetWorldCorners(cor);
                    }

                    float wdth   = cor[2].x - cor[0].x;
                    float height = cor[2].y - cor[0].y;
                    if (wdth == 0 || height == 0)
                    {
                        continue;
                    }
                    //Debug.Log(wdth);
                    //Debug.Log(height);
                    v.linkedTransform.offsetMax = new Vector2(0, 0);
                    v.linkedTransform.offsetMin = Vector2.zero;
                    float proper_y = Screen.height - cor[1].y;
                    v.linkedTransform.anchorMax = new Vector2((float)(v.right.value() - cor[0].x) / wdth, (float)(height + proper_y - v.top.value()) / height);
                    v.linkedTransform.anchorMin = new Vector2((float)(v.left.value() - cor[0].x) / wdth, (float)(height + proper_y - v.bottom.value()) / height);
                    //Debug.LogFormat("Screen w:{0} h:{1}, tr: {2}, left: {3} right: {4} top: {5} bottom:{6}",Screen.width,Screen.height,v.linkedTransform,
                    //v.left.value(), v.right.value(), v.top.value(), v.bottom.value());
                    //Debug.LogFormat("Tr: {0} amin: {1} amax: {2} cor: {3} {4} {5} {6} proper:{7}",v.linkedTransform,v.linkedTransform.anchorMin,v.linkedTransform.anchorMax,cor[0],cor[1],cor[2],cor[3],proper_y);
                }
            }
            needs_rebuild  = false;
            needs_reassign = false;
        }
    }