Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zooming in on a specific part of the canvas from a script #4722

Closed
thkwznk opened this issue Oct 24, 2024 · 3 comments · Fixed by #4786
Closed

Zooming in on a specific part of the canvas from a script #4722

thkwznk opened this issue Oct 24, 2024 · 3 comments · Fixed by #4786
Assignees
Labels
feature Feature request, or something should be improved scripting Related to scripting API
Milestone

Comments

@thkwznk
Copy link
Contributor

thkwznk commented Oct 24, 2024

This is purely a feature request, ot seems that there's no working option to zoom in on a specific part of a canvas using the Lua API.

The Zoom command doesn't allow for specifing a point to zoom to (or from) other then the center. And the app.useTool method for the zoom tool produces no results (in the v1.3.9-2).

@dacap dacap added feature Feature request, or something should be improved scripting Related to scripting API labels Oct 24, 2024
@dacap dacap moved this to Todo in Aseprite v1.3.x Oct 24, 2024
@dacap dacap added this to the v1.3.10 milestone Oct 24, 2024
@thkwznk
Copy link
Contributor Author

thkwznk commented Oct 25, 2024

I actually found a solution to my own problem, it was a combination of a zoom and scroll. Here's a sample function that can scroll to a slice with a little heuristic for calculating an appropriate zoom:

function ScrollToSlice(slice, autoZoom)
    -- Center the canvas first
    app.command.ScrollCenter()

    local sliceCenterX = slice.bounds.x + slice.bounds.width / 2
    local sliceCenterY = slice.bounds.y + slice.bounds.height / 2

    local centerX = slice.sprite.width / 2
    local centerY = slice.sprite.height / 2

    if sliceCenterX < centerX then
        app.command.Scroll {
            direction = "left",
            units = "zoomed-pixel",
            quantity = tostring(centerX - sliceCenterX)
        }
    else
        app.command.Scroll {
            direction = "right",
            units = "zoomed-pixel",
            quantity = tostring(sliceCenterX - centerX)
        }
    end

    if sliceCenterY < centerY then
        app.command.Scroll {
            direction = "up",
            units = "zoomed-pixel",
            quantity = tostring(centerY - sliceCenterY)
        }
    else
        app.command.Scroll {
            direction = "down",
            units = "zoomed-pixel",
            quantity = tostring(sliceCenterY - centerY)
        }
    end

    if autoZoom then
        local sizes = {
            64, 48, 32, 24, 16, 12, 8, 6, 5, 4, 3, 2, 1, 0.5, 0.333, 0.25, 0.20,
            0.167, 0.125 -- There are a few skipped zoom options
        }

        for _, size in ipairs(sizes) do
            -- The window width and height percentages are just heuristic values to make the zoom not bee too harsh
            if slice.bounds.width * size < app.window.width * 0.8 and
                slice.bounds.height * size < app.window.height * 0.6 then
                app.command.Zoom {
                    percentage = tostring(size * 100),
                    focus = "center"
                }
                break
            end
        end
    end
end

@dacap dacap self-assigned this Nov 14, 2024
@dacap dacap moved this from Todo to In Progress in Aseprite v1.3.x Nov 14, 2024
dacap added a commit to dacap/aseprite that referenced this issue Nov 14, 2024
@dacap
Copy link
Member

dacap commented Nov 14, 2024

@thkwznk we'll add app.editor.zoom and app.editor.scroll properties, but I'm not sure if they will be enough for your case. The zoom property will be a number (floating-point number), and the scroll a table (not a Point) with two x and y fields (floating-point numbers), which will indicate the specific sprite pixel that is showing the center of the editor. We'll be able to get/set these properties.

@thkwznk
Copy link
Contributor Author

thkwznk commented Nov 14, 2024

@dacap It should be more than enough, just knowing the zoom value will allow for restoting zoom after moving the view.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Feature request, or something should be improved scripting Related to scripting API
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants