Beispiel #1
0
        private static Exp OptimizeBnot(UnopExp exp)
        {
            switch (exp.Exp)
            {
            case IntegerExp ie:
            {
                ie.Val = ~ie.Val;
                return(ie);
            }

            case FloatExp fe:
            {
                if (LuaMath.FloatToInteger(fe.Val, out var ret))
                {
                    return(new IntegerExp
                        {
                            Line = exp.Line,
                            Val = ~ret
                        });
                }

                break;
            }
            }

            return(exp);
        }
Beispiel #2
0
        /// <summary>
        /// Gets the width allocated to the right block.
        /// </summary>
        /// <param name="width">The total width available.</param>
        /// <param name="leftPreferredWidth">The width preferred by the left block.</param>
        /// <param name="centerPreferredWidth">The width preferred by the center block.</param>
        /// <param name="rightPreferredWidth">The width preferred by the right block.</param>
        /// <returns>The allocated width.</returns>
        private double GetWidthAllocatedToRightBlock(double width, double?leftPreferredWidth, double?centerPreferredWidth, double?rightPreferredWidth)
        {
            if (this.rightBlock == null)
            {
                return(0);
            }

            // There is a right block.
            if (rightPreferredWidth != null)
            {
                return(rightPreferredWidth.Value);
            }

            // The right block is flexible.
            if (this.centerBlock == null)
            {
                // There is no center block
                if (this.leftBlock == null)
                {
                    // There is no left block.
                    return(width);
                }

                // There is a left block.
                if (leftPreferredWidth != null)
                {
                    // The left block is not flexible.
                    return(width - (leftPreferredWidth.Value + this.Layout.objectSpacing));
                }

                // The left block is flexible
                return((width - this.Layout.objectSpacing) / 2);
            }

            // There is a center block
            if (centerPreferredWidth != null)
            {
                // The center block is fixed.
                return(((width - centerPreferredWidth.Value) / 2) - this.Layout.objectSpacing);
            }

            // The center block is flexible
            if (this.leftBlock == null)
            {
                // There is no left block.
                // The flexible right block must share the right side with half of the center block.
                return(((width / 2) - this.Layout.objectSpacing) / 1.5);
            }

            // There is a left block.
            if (leftPreferredWidth != null)
            {
                // The left block is not flexible.
                return(LuaMath.max(leftPreferredWidth.Value, (((width / 2) - this.Layout.objectSpacing) / 3) * 2));
            }

            // The left block is flexible
            return((width - (this.Layout.objectSpacing * 2)) / 3);
        }
Beispiel #3
0
        public override double?GetPreferredWidth()
        {
            var innerPreferredWidth = this.Inner.GetPreferredWidth();

            if (innerPreferredWidth != null)
            {
                return(LuaMath.max((double)innerPreferredWidth, this.textLabel.GetWidth()));
            }
            return(null);
        }
Beispiel #4
0
        /// <summary>
        /// Gets the preferred height of the page.
        /// </summary>
        /// <returns>The preferred height. Null if it is flexible.</returns>
        public double?GetPreferredHeight()
        {
            var heights = this.Content.Select(line => line.GetPreferredHeight()).ToArray();

            if (!heights.Any(h => h == null))
            {
                return(heights.Sum() + (this.Layout.lineSpacing * LuaMath.max(this.Content.Count - 1, 0)));
            }

            return(null);
        }
Beispiel #5
0
        private bool StringToInteger(string s, out LuaInt ret)
        {
            if (Parser.ParseInteger(s, out ret))
            {
                return(true);
            }

            if (Parser.ParseFloat(s, out var f))
            {
                return(LuaMath.FloatToInteger(f, out ret));
            }

            ret = 0;
            return(false);
        }
Beispiel #6
0
        protected override void SizeChanged()
        {
            var    obj    = this.FirstChild;
            double width  = 0;
            double height = 0;

            while (obj != null)
            {
                width += obj.Object.GetWidth();
                height = LuaMath.max(height, obj.Object.GetHeight());
                obj    = obj.Next;
            }

            this.frame.SetWidth(width);
            this.frame.SetHeight(height);
        }
Beispiel #7
0
        private static Exp OptimizeBitwiseBinaryOp(BinopExp exp)
        {
            if (CastToInt(exp.Exp1, out var i1) &&
                CastToInt(exp.Exp2, out var i2))
            {
                switch (exp.Op)
                {
                case ETokenType.OpBAnd:
                    return(new IntegerExp
                    {
                        Line = exp.Line,
                        Val = i1 & i2
                    });

                case ETokenType.OpBOr:
                    return(new IntegerExp
                    {
                        Line = exp.Line,
                        Val = i1 | i2
                    });

                case ETokenType.OpBXor:
                    return(new IntegerExp
                    {
                        Line = exp.Line,
                        Val = i1 ^ i2
                    });

                case ETokenType.OpShl:
                    return(new IntegerExp
                    {
                        Line = exp.Line,
                        Val = LuaMath.ShiftLeft(i1, i2)
                    });

                case ETokenType.OpShr:
                    return(new IntegerExp
                    {
                        Line = exp.Line,
                        Val = LuaMath.ShiftRight(i1, i2)
                    });
                }
            }

            return(exp);
        }
Beispiel #8
0
        public bool ToInteger(out LuaInt ret)
        {
            if (IsInt())
            {
                ret = (LuaInt)_numValue;
                return(true);
            }
            else if (IsFloat())
            {
                return(LuaMath.FloatToInteger(_numValue, out ret));
            }
            else if (IsString())
            {
                return(StringToInteger((string)_objValue, out ret));
            }

            ret = 0;
            return(false);
        }
Beispiel #9
0
        private static bool CastToInt(Exp exp, out LuaInt ret)
        {
            ret = 0;
            switch (exp)
            {
            case IntegerExp ie:
            {
                ret = ie.Val;
                return(true);
            }

            case FloatExp fe:
            {
                return(LuaMath.FloatToInteger(fe.Val, out ret));
            }

            default:
                return(false);
            }
        }
Beispiel #10
0
        public static void FrameBg(IFrame frame)
        {
            var bgFrame = (IFrame)Global.FrameProvider.CreateFrame(FrameType.Frame, null, frame);

            bgFrame.SetAllPoints(frame);
            bgFrame.SetFrameLevel(100);
            bgFrame.SetFrameStrata(FrameStrata.HIGH);
            var backdrop = new NativeLuaTable();

            backdrop["bgFile"]   = "Interface\\DialogFrame\\UI-DialogBox-Gold-Background";
            backdrop["tile"]     = false;
            backdrop["tileSize"] = 0;
            backdrop["edgeSize"] = 16;
            var inserts = new NativeLuaTable();

            backdrop["left"]   = 4;
            backdrop["right"]  = 4;
            backdrop["top"]    = 4;
            backdrop["bottom"] = 0;
            backdrop["insets"] = inserts;
            bgFrame.SetBackdrop(backdrop);
            bgFrame.SetBackdropColor(LuaMath.random(100) / 100, LuaMath.random(100) / 100, LuaMath.random(100) / 100, 0.8);
        }
Beispiel #11
0
        public LuaValue Get(LuaValue key)
        {
            LuaInt idx = 0;

            if (key.IsInt())
            {
                idx = key.GetIntValue();
            }
            else if (key.IsFloat())
            {
                if (LuaMath.FloatToInteger(key.GetFloatValue(), out var fi))
                {
                    idx = fi;
                }
            }

            if (idx >= 1 && idx <= Len())
            {
                return(_arr[(int)(idx - 1)]);
            }

            return(_map != null && _map.TryGetValue(key.GetValue(), out var ret) ? ret : LuaValue.Nil);
        }
Beispiel #12
0
        public void Put(LuaValue key, LuaValue val)
        {
            if (key is null || key.IsNil())
            {
                Debug.Panic("table index is nil!");
            }

            if (key.IsFloat() && LuaFloat.IsNaN(key.GetFloatValue()))
            {
                Debug.Panic("table index is Nan!");
            }

            _changed = true;

            LuaInt idx = 0;

            if (key.IsInt())
            {
                idx = key.GetIntValue();
            }
            else if (key.IsFloat())
            {
                if (LuaMath.FloatToInteger(key.GetFloatValue(), out var fi))
                {
                    idx = fi;
                }
            }

            if (idx >= 1)
            {
                var arrLen = Len();
                if (idx <= arrLen)
                {
                    _arr[(int)(idx - 1)] = val;
                    if (idx == arrLen && val.IsNil())
                    {
                        ShrinkArray();
                    }
                    return;
                }

                if (idx == arrLen + 1)
                {
                    if (_map != null && _map.ContainsKey(key.GetIntValue()))
                    {
                        _map.Remove(key.GetIntValue());
                    }

                    if (!val.IsNil())
                    {
                        if (_arr == null)
                        {
                            _arr = new List <LuaValue>();
                        }

                        _arr.Add(val);
                        ExpandArray();
                    }

                    return;
                }
            }

            if (!val.IsNil())
            {
                if (_map == null)
                {
                    _map = new Dictionary <object, LuaValue>();
                }

                _map[key.GetValue()] = val;
            }
            else
            {
                _map.Remove(key.GetValue());
            }
        }
Beispiel #13
0
        /// <summary>
        /// Gets the width allocated to the center block.
        /// </summary>
        /// <param name="width">The total width available.</param>
        /// <param name="leftPreferredWidth">The width preferred by the left block.</param>
        /// <param name="centerPreferredWidth">The width preferred by the center block.</param>
        /// <param name="rightPreferredWidth">The width preferred by the right block.</param>
        /// <returns>The allocated width.</returns>
        private double GetWidthAllocatedToCenterBlock(double width, double?leftPreferredWidth, double?centerPreferredWidth, double?rightPreferredWidth)
        {
            if (this.centerBlock == null)
            {
                return(0);
            }

            // There is a center block.
            if (centerPreferredWidth != null)
            {
                return(centerPreferredWidth.Value);
            }

            // The center block is flexible.
            if (this.leftBlock == null)
            {
                // There is no left block
                if (this.rightBlock == null)
                {
                    // There is no right block.
                    return(width);
                }

                // There is a right block.
                if (rightPreferredWidth != null)
                {
                    // The right block is not flexible.
                    return(((width / 2) - (rightPreferredWidth.Value + this.Layout.objectSpacing)) * 2);
                }

                // The right block is flexible
                return((((width / 2) - this.Layout.objectSpacing) / 3) * 2);
            }

            // There is a left block
            var halfWidthMinusOneObjectSpacing = (width / 2) - this.Layout.objectSpacing;

            if (leftPreferredWidth != null)
            {
                // The left block is fixed.
                if (this.rightBlock != null && rightPreferredWidth != null)
                {
                    // There is a fixed right block.
                    return((((width / 2) - LuaMath.max(leftPreferredWidth.Value, rightPreferredWidth.Value)) - this.Layout.objectSpacing) * 2);
                }

                if (this.rightBlock != null)
                {
                    // There is a flexible right block.
                    return(LuaMath.min((halfWidthMinusOneObjectSpacing / 3) * 2, (halfWidthMinusOneObjectSpacing - leftPreferredWidth.Value) * 2));
                }

                // There is no right block.
                return((halfWidthMinusOneObjectSpacing - leftPreferredWidth.Value) * 2);
            }

            // The left block is flexible
            if (this.rightBlock == null)
            {
                // There is no right block.
                // The flexible center block must share the center side with half of the left block.
                return((halfWidthMinusOneObjectSpacing / 3) * 2);
            }

            // There is a right block.
            if (rightPreferredWidth != null)
            {
                // The right block is not flexible.
                return(LuaMath.min((halfWidthMinusOneObjectSpacing / 3) * 2, (halfWidthMinusOneObjectSpacing - rightPreferredWidth.Value) * 2));
            }

            // The right block is flexible
            return((width - (this.Layout.objectSpacing * 2)) / 3);
        }
Beispiel #14
0
        private static Exp OptimizeArithBinaryOp(BinopExp exp)
        {
            if (exp.Exp1 is IntegerExp ie1 &&
                exp.Exp2 is IntegerExp ie2)
            {
                switch (exp.Op)
                {
                case ETokenType.OpAdd:
                    return(new IntegerExp
                    {
                        Line = exp.Line,
                        Val = ie1.Val + ie2.Val
                    });

                case ETokenType.OpSub:
                    return(new IntegerExp
                    {
                        Line = exp.Line,
                        Val = ie1.Val - ie2.Val
                    });

                case ETokenType.OpMul:
                    return(new IntegerExp
                    {
                        Line = exp.Line,
                        Val = ie1.Val * ie2.Val
                    });

                case ETokenType.OpIDiv:
                {
                    if (ie2.Val != 0)
                    {
                        return(new IntegerExp
                            {
                                Line = exp.Line,
                                Val = LuaMath.IFloorDiv(ie1.Val, ie2.Val)
                            });
                    }

                    break;
                }

                case ETokenType.OpMod:
                {
                    if (ie2.Val != 0)
                    {
                        return(new IntegerExp
                            {
                                Line = exp.Line,
                                Val = LuaMath.IMod(ie1.Val, ie2.Val)
                            });
                    }

                    break;
                }
                }
            }

            if (CastToFloat(exp.Exp1, out var f) &&
                CastToFloat(exp.Exp2, out var g))
            {
                switch (exp.Op)
                {
                case ETokenType.OpAdd:
                    return(new FloatExp
                    {
                        Line = exp.Line,
                        Val = f + g
                    });

                case ETokenType.OpSub:
                    return(new FloatExp
                    {
                        Line = exp.Line,
                        Val = f - g
                    });

                case ETokenType.OpMul:
                    return(new FloatExp
                    {
                        Line = exp.Line,
                        Val = f * g
                    });

                case ETokenType.OpDiv:
                {
                    if (g != 0)
                    {
                        return(new FloatExp
                            {
                                Line = exp.Line,
                                Val = f / g,
                            });
                    }

                    break;
                }

                case ETokenType.OpIDiv:
                {
                    if (g != 0)
                    {
                        return(new FloatExp
                            {
                                Line = exp.Line,
                                Val = LuaMath.FFloorDiv(f, g)
                            });
                    }

                    break;
                }

                case ETokenType.OpMod:
                {
                    if (g != 0)
                    {
                        return(new FloatExp
                            {
                                Line = exp.Line,
                                Val = LuaMath.FMod(f, g)
                            });
                    }

                    break;
                }

                case ETokenType.OpPow:
                    return(new FloatExp
                    {
                        Line = exp.Line,
                        Val = Math.Pow(f, g)
                    });
                }
            }

            return(exp);
        }
Beispiel #15
0
 private double[] GetCircularCoordinates(double degrees)
 {
     return(new [] { this.r * LuaMath.cos(degrees), this.r * LuaMath.sin(degrees) });
 }