Ejemplo n.º 1
0
        static void Init()
        {
            Screen.ClearColor = 0x1e4e50ff;

            Texture.DefaultMinFilter = TextureFilter.Nearest;
            Texture.DefaultMagFilter = TextureFilter.Nearest;

            var builder = new AtlasBuilder(1024);

            //Pack a font
            var font   = new Font("Assets/NotoSans-Regular.ttf", FontCharSet.BasicLatin);
            var size32 = new FontSize(font, 32);

            builder.AddFont("font", size32, true);

            //Pack a bunch of icons
            builder.AddTiles("Assets/icons.png", 16, 16, true, true);

            atlas = builder.Build(1);
            if (atlas == null)
            {
                throw new Exception("Failed to build the atlas.");
            }

            icons = atlas.GetTiles("icons");

            batch = new DrawBatch2D();
        }
Ejemplo n.º 2
0
        static void Init()
        {
            var font = new Font("Assets/NotoSans-Regular.ttf", FontCharSet.BasicLatin);
            var size = new FontSize(font, 128f);

            var builder = new AtlasBuilder(2048);

            builder.AddBitmap("Assets/star.png", true, true);
            builder.AddBitmap("Assets/face.png", true, true);
            builder.AddBitmap("Assets/maritte.png", true, true);
            builder.AddFont("font", size, true);

            atlas = builder.Build(1);
            if (atlas == null)
            {
                throw new Exception("Failed to build atlas.");
            }

            batch  = new DrawBatch2D();
            shader = new Shader(Shader.Basic2D);

            view = new GuiView(shader, Screen.DrawWidth, Screen.DrawHeight, LayoutMode.Horizontal);
            view.BackgroundColor = Color4.Grey;
            view.Spacing         = 16;
            view.SetPadding(16);

            var left = view.AddChild(new GuiContainer());

            left.BackgroundColor = Color4.Black * 0.25f;
            left.FlexX           = left.FlexY = true;
            left.Spacing         = 16;

            var right = view.AddChild(new GuiContainer());

            right.BackgroundColor = Color4.Blue;
            right.FlexX           = right.FlexY = true;

            var items = left.AddChild(new GuiContainer());

            items.BackgroundColor = Color4.Black * 0.25f;
            items.FlexX           = items.FlexY = true;
            items.Spacing         = 1;
            items.ScrollableY     = true;

            var padding = left.AddChild(new GuiContainer(100, 100, LayoutMode.Vertical));

            padding.BackgroundColor = Color4.White * 0.25f;
            padding.FlexX           = true;
            padding.FlexY           = false;

            for (int i = 0; i < 50; ++i)
            {
                var item = items.AddChild(new GuiElement(16, 40));
                item.FlexY     = false;
                item.OnRender += b => b.DrawRect(item.RectX, item.RectY, item.RectW, item.RectH, Color4.Red);
            }

            Screen.ClearColor = Color4.Black;
            Screen.OnResized += () => view.SetSize(Screen.DrawWidth, Screen.DrawHeight);
        }
Ejemplo n.º 3
0
        internal override void DoRender(DrawBatch2D batch)
        {
            if (Color.A > 0)
            {
                //TODO: dumb to be calculating the alignment stuff here, move to layout phase
                var pos = new Rectangle(rect.X, rect.Y, texture.Width, texture.Height);

                if (FillMode == FillMode.ShrinkOnly)
                {
                    float s = Math.Min(1.0f, rect.W / pos.W);
                    s     = Math.Min(s, rect.H / pos.H);
                    pos.W = (int)(pos.W * s);
                    pos.H = (int)(pos.H * s);
                }
                else if (FillMode == FillMode.MaintainAspectRatio)
                {
                    float s = rect.W / pos.W;
                    s     = Math.Min(s, rect.H / pos.H);
                    pos.W = (int)(pos.W * s);
                    pos.H = (int)(pos.H * s);
                }
                else if (FillMode == FillMode.StretchToFill)
                {
                    pos.W = rect.W;
                    pos.H = rect.H;
                }
                else
                {
                    throw new Exception("Unhandled fill mode.");
                }

                if (AlignX == AlignX.Center)
                {
                    pos.X = (int)(rect.CenterX - (pos.W * 0.5f));
                }
                else if (AlignX == AlignX.Right)
                {
                    pos.X = rect.MaxX - pos.W;
                }

                if (AlignY == AlignY.Center)
                {
                    pos.Y = (int)(rect.CenterY - (pos.H * 0.5f));
                }
                else if (AlignY == AlignY.Bottom)
                {
                    pos.Y = rect.MaxY - pos.H;
                }

                batch.DrawImage(texture, ref pos, Color);
            }

            base.DoRender(batch);
        }
Ejemplo n.º 4
0
        static void Init()
        {
            batch = new DrawBatch2D();

            var bitmap = new Bitmap("Assets/ikenfell.png");

            bitmap.Premultiply();

            texture = new Texture2D(bitmap);

            var time = Time.ClockMilliseconds;

            poly = new Polygon();
            var tri = new BitmapTriangulator();

            tri.Triangulate(bitmap, 0.1f * Calc.Rad, 4f, poly);

            Console.WriteLine("Time: {0} ms", Time.ClockMilliseconds - time);
            Console.WriteLine("Points: {0}", poly.PointCount);

            tris = new List <Triangle>();
            var list = new List <Vector2>(poly.PointCount);

            for (int i = 0; i < poly.PointCount; ++i)
            {
                list.Add(poly.GetPoint(i));
            }
            while (list.Count > 2)
            {
                for (int i = 0; i < list.Count - 2; ++i)
                {
                    var   a    = list[i];
                    var   b    = list[(i + 1) % list.Count];
                    var   c    = list[(i + 2) % list.Count];
                    float side = (b.X - a.X) * (c.Y - a.Y) - (c.X - a.X) * (b.Y - a.Y);
                    if (side < 0f)
                    {
                        tris.Add(new Triangle(a, b, c));
                        list.RemoveAt(i + 1);
                    }
                }
            }

            Console.WriteLine("Triangles: {0}", tris.Count);

            Console.WriteLine("Bitmap Area: {0}", bitmap.Width * bitmap.Height);
            Console.WriteLine("Convex Area: {0}", poly.GetArea());
        }
Ejemplo n.º 5
0
        internal override void DoRender(DrawBatch2D batch)
        {
            void RenderChildren()
            {
                foreach (var child in Children)
                {
                    if (child.Enabled)
                    {
                        child.DoRender(batch);
                    }
                }
            }

            if (BackgroundColor.A > 0)
            {
                batch.DrawRect(rect.X, rect.Y, rect.W, rect.H, BackgroundColor);
            }

            if (children.Count > 0)
            {
                //if (AlwaysClip || !rect.Equals(ref contentRect))

                //Clip if your contents are larger than your size
                if (AlwaysClip || !rect.Contains(ref contentRect))
                {
                    //Make sure to retain the clip state as we enter/exit child containers
                    batch.PushClipRect(new RectangleI(rect.X, rect.Y, rect.W, rect.H));
                    RenderChildren();
                    batch.PopClipRect();
                }
                else
                {
                    RenderChildren();
                }
            }

            base.DoRender(batch);
        }
Ejemplo n.º 6
0
 internal virtual void DoRender(DrawBatch2D batch)
 {
     OnRender?.Invoke(batch);
 }
Ejemplo n.º 7
0
        internal override void DoRender(DrawBatch2D batch)
        {
            batch.DrawText(font, ref text, pos, Color);

            base.DoRender(batch);
        }
Ejemplo n.º 8
0
 protected abstract void Render(DrawBatch2D batch, RectangleI handle);
Ejemplo n.º 9
0
 internal override void DoRender(DrawBatch2D batch)
 {
     Render(batch, handle);
 }
Ejemplo n.º 10
0
 internal override void DoRender(DrawBatch2D batch)
 {
     batch.DrawRect(RectX, RectY, RectW, RectH, Color);
 }