add functions to add and remove pixels from layer-tree
This commit is contained in:
@@ -48,7 +48,7 @@ struct
|
||||
, b = 0
|
||||
, a = 1
|
||||
, layer = 0
|
||||
, layerTree = LayerTree.emptyTree
|
||||
, layerTree = LayerTree.empty
|
||||
, modalNum = 0
|
||||
, undo = []
|
||||
, redo = []
|
||||
|
||||
@@ -34,7 +34,7 @@ struct
|
||||
, b: int
|
||||
, a: int
|
||||
, layer: int
|
||||
, layerTree: LayerTree.tree
|
||||
, layerTree: LayerTree.t
|
||||
, modalNum: int
|
||||
, undo: square list
|
||||
, redo: square list
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
structure Grid =
|
||||
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}
|
||||
|
||||
fun isBlank ({a, ...}: square) = a = 0
|
||||
fun isBlank ({a, ...}: pixel) = a = 0
|
||||
|
||||
fun changeGridSize maxSide grid =
|
||||
Vector.tabulate (maxSide, fn i =>
|
||||
@@ -20,4 +20,16 @@ struct
|
||||
end
|
||||
else
|
||||
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
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
structure LayerTree =
|
||||
struct
|
||||
datatype tree =
|
||||
NODE of {key: int, value: Grid.t, left: tree, right: tree}
|
||||
| LEAF
|
||||
datatype t = NODE of {key: int, value: Grid.t, left: t, right: t} | LEAF
|
||||
|
||||
val emptyTree = LEAF
|
||||
val empty = LEAF
|
||||
|
||||
fun insert (newKey, newValue, tree) =
|
||||
case tree of
|
||||
@@ -65,15 +63,26 @@ struct
|
||||
else
|
||||
valuePixel) valueYAxis) value
|
||||
|
||||
fun makeEmptyGrid maxSide =
|
||||
Vector.tabulate (maxSide, fn _ =>
|
||||
Vector.tabulate (maxSide, fn _ => Grid.emptyPixel))
|
||||
|
||||
fun flatten (maxSide, tree) =
|
||||
foldl (helpFlatten, tree, makeEmptyGrid maxSide)
|
||||
foldl (helpFlatten, tree, Grid.makeEmpty maxSide)
|
||||
|
||||
fun changeGridSize (maxSide, tree) =
|
||||
let val f = Grid.changeGridSize maxSide
|
||||
in map (f, tree)
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user