A Selection represents the currently selected area in a sprite.
Constructor
Selection([rectangle])
Initial selection bounds.
local selection = Selection(Rectangle(10, 10, 20, 20))
local emptySelection = Selection()
Properties
Bounding rectangle of the selection.
True if the selection is empty.
Methods
selection:deselect()
Clears the selection.
selection:select(rectangle)
rectangle
Rectangle | Selection
required
Rectangle or selection to use.
Replaces the current selection.
selection:select(Rectangle(0, 0, 32, 32))
selection:select(otherSelection)
selection:selectAll()
Selects the entire sprite.
selection:add(rectangle)
rectangle
Rectangle | Selection
required
Area to add to selection.
Adds to the current selection.
selection:add(Rectangle(10, 10, 20, 20))
selection:add(otherSelection)
selection:subtract(rectangle)
rectangle
Rectangle | Selection
required
Area to subtract from selection.
Subtracts from the current selection.
selection:subtract(Rectangle(10, 10, 10, 10))
selection:subtract(otherSelection)
selection:intersect(rectangle)
rectangle
Rectangle | Selection
required
Area to intersect with.
Intersects the current selection.
selection:intersect(Rectangle(5, 5, 30, 30))
selection:intersect(otherSelection)
selection:contains(point)
Returns true if the point is inside the selection.
if selection:contains(Point(10, 10)) then
print("Point is selected")
end
Examples
Creating and Using Selections
local sprite = Sprite(64, 64)
local selection = sprite.selection
-- Select a rectangular area
selection:select(Rectangle(10, 10, 32, 32))
print("Selection bounds:", selection.bounds)
-- Add to selection
selection:add(Rectangle(40, 40, 20, 20))
-- Check if point is selected
if selection:contains(Point(20, 20)) then
print("Point 20,20 is selected")
end
Selection Operations
local sprite = app.sprite
local sel = sprite.selection
-- Create initial selection
sel:select(Rectangle(0, 0, 32, 32))
-- Add another area
sel:add(Rectangle(32, 32, 32, 32))
-- Subtract from selection
sel:subtract(Rectangle(16, 16, 32, 32))
-- Intersect with another area
sel:intersect(Rectangle(8, 8, 48, 48))
Working with Selection Bounds
local sprite = app.sprite
local sel = sprite.selection
if not sel.isEmpty then
local bounds = sel.bounds
print(string.format(
"Selection: x=%d y=%d w=%d h=%d",
bounds.x, bounds.y, bounds.width, bounds.height
))
-- Crop sprite to selection
sprite:crop(bounds)
else
print("No selection")
end
Moving Selection Origin
local sprite = app.sprite
local sel = sprite.selection
sel:select(Rectangle(10, 10, 20, 20))
print("Original origin:", sel.origin)
-- Move selection
sel.origin = Point(30, 30)
print("New origin:", sel.origin)
print("Bounds:", sel.bounds)
Selecting All and Deselecting
local sprite = app.sprite
local sel = sprite.selection
-- Select everything
sel:selectAll()
print("Selected all, bounds:", sel.bounds)
-- Clear selection
sel:deselect()
print("Is empty:", sel.isEmpty)
Testing Points in Selection
local sprite = Sprite(64, 64)
local sel = sprite.selection
sel:select(Rectangle(20, 20, 24, 24))
local points = {
Point(10, 10),
Point(30, 30),
Point(50, 50)
}
for i, pt in ipairs(points) do
local inside = sel:contains(pt)
print(string.format(
"Point (%d,%d): %s",
pt.x, pt.y,
inside and "inside" or "outside"
))
end