Example #1
0
        private void OpenFile(PixelArt target)
        {
            if (Workspace?.Target == target)
            {
                return;
            }

            if (Workspace != null)
            {
                CloseFile();
            }

            Workspace = new PAWorkspace(this)
            {
                name = "Workspace"
            };
            rootVisualElement.Add(Workspace);

            // Load the Saved preferences
            Workspace.Canvas.ForegroundColor = ColorUtility.TryParseHtmlString(EditorPrefs.GetString("com.noz.pixelart.ForegroundColor"), out var foregroundColor) ?
                                               foregroundColor :
                                               Color.white;
            Workspace.Canvas.BackgroundColor = ColorUtility.TryParseHtmlString(EditorPrefs.GetString("com.noz.pixelart.BackgroundColor"), out var backgroundColor) ?
                                               backgroundColor :
                                               Color.white;

            Workspace.OpenFile(target);

            RefreshTitle();
        }
        public override void OnImportAsset(AssetImportContext ctx)
        {
            // Load the raw file.
            var file = PAFile.Load(ctx.assetPath);

            var texture = new Texture2D(64, 64, TextureFormat.RGBA32, false);

            texture.filterMode = FilterMode.Point;
            texture.name       = "atlas";

            var textures = file.frames.Select(f => file.RenderFrame(f)).ToArray();
            var rects    = texture.PackTextures(textures, 1).Select(r => new Rect(r.xMin * texture.width, r.yMin * texture.height, r.width * texture.width, r.height * texture.height)).ToArray();

            texture.Apply();

            // TODO: look for duplicates by checking imageContentsHash for all textures


            var sprites = new Dictionary <string, Sprite>();

            for (int frameIndex = 0; frameIndex < file.frames.Count; frameIndex++)
            {
                var frame  = file.frames[frameIndex];
                var sprite = Sprite.Create(texture, rects[frameIndex], new Vector2(0.5f, 0.5f));
                sprite.name = $"{frame.animation.name}.{frame.order:D03}";
                ctx.AddObjectToAsset(frame.id, sprite);
                sprites[frame.id] = sprite;
            }

            ctx.AddObjectToAsset("_atlas", texture);

            var pixelArt = PixelArt.Create(
                file.animations.Select(a => new PixelArtAnimation(
                                           a.name,
                                           file.frames.Where(f => f.animation == a).OrderBy(f => f.order).Select(f => new PixelArtAnimationFrame {
                sprite = sprites[f.id]
            }).ToArray()
                                           )).ToArray(),
                defaultAnimationIndex);

            var defaultFrame = file.frames.Where(f => f.animation == file.animations[defaultAnimationIndex]).OrderBy(f => f.order).First();

#if false
            var go = new GameObject();
            var sr = go.AddComponent <SpriteRenderer>();
            sr.sprite = sprites[defaultFrame.id];;
            go.name   = "Prefab";
            var animator = go.AddComponent <PixelArtAnimator>();
            animator.PixelArt = pixelArt;
            animator.layers   = new SpriteRenderer[] { sr };

            ctx.AddObjectToAsset("prefab", go, PAUtils.LoadImage("PixelArtIcon.psd"));
#endif

            pixelArt.name = "PixelArt";
            ctx.AddObjectToAsset("pixelArt", pixelArt, PAUtils.LoadImage("PixelArtIcon.psd"));

            //ctx.AddObjectToAsset("main", pixelArt, file.RenderFrame(file.frames[0]));
            ctx.SetMainObject(pixelArt);
        }
Example #3
0
        private void OnDisable()
        {
            _previousTarget = Workspace?.Target;

            CloseFile();

            Undo.undoRedoPerformed     -= OnUndoRedo;
            EditorApplication.quitting -= CloseFile;
        }
Example #4
0
        public void OnEnable()
        {
            RefreshTitle();

            // Add style sheet
            rootVisualElement.AddStyleSheetPath("PAEditor");
            rootVisualElement.name      = "Editor";
            rootVisualElement.focusable = true;
            //rootVisualElement.RegisterCallback<FocusInEvent>((e) => Workspace?.schedule.Execute(Workspace.Canvas.Focus));

            rootVisualElement.Add(CreateToolbar());

            rootVisualElement.RegisterCallback <KeyDownEvent>((evt) => Workspace?.OnKeyDown(evt));

            if (_previousTarget != null)
            {
                OpenFile(_previousTarget);
                _previousTarget = null;
            }

            Undo.undoRedoPerformed     += OnUndoRedo;
            EditorApplication.quitting += CloseFile;
        }