Exemple #1
0
        private static void Regenerate()
        {
            var r = new List <Rectangle>();

            for (var i = 0; i < 20; i++)
            {
                int width  = Helpers.GenerateRandomNumber(1, 80);
                int height = Helpers.GenerateRandomNumber(1, 180);
                r.Add(new Rectangle(0, 0, width, height));
            }

            _rects        = r.ToArray();
            _boundingRect = new Rectangle(0, 0, Binning.FitRectangles(_rects));
        }
Exemple #2
0
        private void RenderSaveSection(RenderComposer composer)
        {
            // Saving
            ImGui.InputText("Name", ref _saveName, 100);
            ImGui.SameLine();
            if (string.IsNullOrEmpty(_saveName))
            {
                ImGui.TextDisabled("Save");
                ImGui.SameLine();
                ImGui.TextDisabled("SaveToFile");
            }
            else
            {
                ImGui.SameLine();
                if (ImGui.Button("SaveToFile"))
                {
                    string saveName = _saveName.ToLower();
                    if (!saveName.Contains(".anim")) saveName += ".anim";

                    // Fixups
                    if (AnimController?.MirrorXAnchors != null)
                    {
                        var emptyMirrorAnchors = true;
                        for (var i = 0; i < AnimController.MirrorXAnchors.Length; i++)
                        {
                            if (AnimController.MirrorXAnchors[i] == Vector2.Zero) continue;
                            emptyMirrorAnchors = false;
                            break;
                        }

                        if (emptyMirrorAnchors) AnimController.MirrorXAnchors = null;
                    }

                    try
                    {
                        string saveData;
                        // ReSharper disable once ConvertIfStatementToConditionalTernaryExpression
                        if (AnimController != null)
                            saveData = XMLFormat.To(AnimController);
                        else
                            saveData = XMLFormat.To(Animation);

                        Engine.AssetLoader.Save(Encoding.UTF8.GetBytes(saveData), saveName);
                    }
                    catch (Exception ex)
                    {
                        Engine.Log.Error(ex);
                    }
                }

                if (ImGui.Button("Save Packed Texture"))
                {
                    string saveName = _saveName.ToLower();
                    if (!saveName.Contains(".png")) saveName += ".png";
                    Rectangle[] frames = AnimController != null ? AnimController.AnimTex.Frames : Animation.Frames;
                    var preBinnedFrames = new Rectangle[frames.Length];
                    Array.Copy(frames, preBinnedFrames, frames.Length);
                    Texture spriteSheetTexture = Animation.Texture;

                    var spacing = 2;
                    for (var i = 0; i < frames.Length; i++)
                    {
                        Rectangle frame = frames[i];
                        frames[i] = frame.Inflate(spacing, spacing);
                    }

                    Vector2 totalSize = Binning.FitRectangles(frames, true);
                    FrameBuffer texture = new FrameBuffer(totalSize).WithColor();
                    composer.RenderTo(texture);
                    for (var i = 0; i < frames.Length; i++)
                    {
                        composer.RenderSprite(frames[i].Deflate(spacing, spacing), Color.White, spriteSheetTexture, preBinnedFrames[i]);
                    }

                    composer.RenderTo(null);

                    byte[] pixelsDownload = texture.Sample(new Rectangle(0, 0, totalSize), PixelFormat.Rgba);
                    ImageUtil.FlipImageY(pixelsDownload, (int) totalSize.Y);
                    byte[] pngFile = PngFormat.Encode(pixelsDownload, totalSize, PixelFormat.Rgba);
                    Engine.AssetLoader.Save(pngFile, saveName);
                }
            }
        }