add functions to add and remove pixels from layer-tree

This commit is contained in:
2025-08-09 09:13:03 +01:00
parent e634bb25d7
commit 34c29bcbba
5 changed files with 35 additions and 14 deletions

BIN
dotscape

Binary file not shown.

View File

@@ -48,7 +48,7 @@ struct
, b = 0 , b = 0
, a = 1 , a = 1
, layer = 0 , layer = 0
, layerTree = LayerTree.emptyTree , layerTree = LayerTree.empty
, modalNum = 0 , modalNum = 0
, undo = [] , undo = []
, redo = [] , redo = []

View File

@@ -34,7 +34,7 @@ struct
, b: int , b: int
, a: int , a: int
, layer: int , layer: int
, layerTree: LayerTree.tree , layerTree: LayerTree.t
, modalNum: int , modalNum: int
, undo: square list , undo: square list
, redo: square list , redo: square list

View File

@@ -1,12 +1,12 @@
structure Grid = structure Grid =
struct struct
type square = {r: int, g: int, b: int, a: int} type pixel = {r: int, g: int, b: int, a: int}
type t = square vector vector type t = pixel vector vector
val emptyPixel = {r = 0, g = 0, b = 0, a = 0} val emptyPixel = {r = 0, g = 0, b = 0, a = 0}
fun isBlank ({a, ...}: square) = a = 0 fun isBlank ({a, ...}: pixel) = a = 0
fun changeGridSize maxSide grid = fun changeGridSize maxSide grid =
Vector.tabulate (maxSide, fn i => Vector.tabulate (maxSide, fn i =>
@@ -20,4 +20,16 @@ struct
end end
else else
Vector.tabulate (maxSide, fn _ => emptyPixel)) Vector.tabulate (maxSide, fn _ => emptyPixel))
fun updateGrid (grid, newX, newY, pixel) =
let
val yAxis = Vector.sub (grid, newX)
val yAxis = Vector.update (yAxis, newY, pixel)
in
Vector.update (grid, newX, yAxis)
end
fun makeEmpty maxSide =
Vector.tabulate (maxSide, fn _ =>
Vector.tabulate (maxSide, fn _ => emptyPixel))
end end

View File

@@ -1,10 +1,8 @@
structure LayerTree = structure LayerTree =
struct struct
datatype tree = datatype t = NODE of {key: int, value: Grid.t, left: t, right: t} | LEAF
NODE of {key: int, value: Grid.t, left: tree, right: tree}
| LEAF
val emptyTree = LEAF val empty = LEAF
fun insert (newKey, newValue, tree) = fun insert (newKey, newValue, tree) =
case tree of case tree of
@@ -65,15 +63,26 @@ struct
else else
valuePixel) valueYAxis) value valuePixel) valueYAxis) value
fun makeEmptyGrid maxSide =
Vector.tabulate (maxSide, fn _ =>
Vector.tabulate (maxSide, fn _ => Grid.emptyPixel))
fun flatten (maxSide, tree) = fun flatten (maxSide, tree) =
foldl (helpFlatten, tree, makeEmptyGrid maxSide) foldl (helpFlatten, tree, Grid.makeEmpty maxSide)
fun changeGridSize (maxSide, tree) = fun changeGridSize (maxSide, tree) =
let val f = Grid.changeGridSize maxSide let val f = Grid.changeGridSize maxSide
in map (f, tree) in map (f, tree)
end end
fun addPixel (key, newX, newY, maxSide, pixel, tree) =
let
val grid =
case get (key, tree) of
SOME grid => grid
| NONE => Grid.makeEmpty maxSide
val grid = Grid.updateGrid (grid, newX, newY, pixel)
in
insert (key, grid, tree)
end
fun removePixel (key, newX, newY, maxSide, tree) =
addPixel (key, newX, newY, maxSide, Grid.emptyPixel, tree)
end end