Class ArtModel
An editable, undoable ArtDocument with a selection.
Every change goes through this class so it can be undone: the operations below cover the
common edits, and edit wraps any other change to the nodes. Undo keeps whole snapshots
of the tree, so any change can be undone, however it was made. Nodes are found by id: every
node gets one when the model takes the document or a new node, and the selection is kept
as ids so it survives undo.
Snapshots share pixel data with the document, so never draw into a RasterNode's raster
in place: give the node a copy (setImage(getImage().copy())) after the checkpoint and
draw into that.
ArtModel model = new ArtModel(new ArtDocument(200, 120));
ShapeNode layer = ShapeNode.rect(10, 10, 80, 50, 6);
layer.fill(new SolidPaint(0xff1e88e5));
model.add(layer);
model.setOpacity(layer, 0.5f);
model.undo(); // back to fully opaque
-
Constructor Summary
ConstructorsConstructorDescriptionArtModel()Creates a model over an empty 800 by 600 document.ArtModel(ArtDocument document) Creates a model over a document. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a node on top of the document, or of the selected node's group, and selects it.voidAdds a listener.voidAdds an effect to a node.voidStarts a group of changes that undo as one.booleancanRedo()True when there is something to redo.booleancanUndo()True when there is something to undo.voidchanged()Tells the listeners the nodes changed, after a change made betweencheckpointand now, such as each step of a drag.voidSaves the current tree so the next change can be undone.voidClears the selection.voidMakes an arbitrary change undoable: saves the tree, runs the change and notifies the listeners.voidendGroup()Ends a group started bybeginGroup.The node with an id, or null.The document.The first selected node, or null.The selected nodes, in the order they were selected.Puts the nodes into a new group in place of the topmost of them, and selects it.voidInserts a node.booleanTrue when the node is selected.voidMoves a node to another group or position.The group holding a node, or null for the root.booleanredo()Redoes the last undone change; returns false when there is none.voidRemoves a node.voidRemoves a listener.voidremoveEffect(ArtNode n, int index) Removes an effect from a node.voidRemoves the selected nodes.voidMoves a node one step up (toward the top) or down among its siblings.voidSelects a node.voidsetBlendMode(ArtNode n, BlendMode mode) Sets a node's blend mode.voidsetDocument(ArtDocument doc) Replaces the document and clears the undo history and the selection.voidsetEffectParameter(ArtNode n, int index, String param, Object value) Sets a parameter of one of a node's effects.voidReplaces a shape's fills with one paint.voidLocks or unlocks a node.voidRenames a node.voidsetOpacity(ArtNode n, float opacity) Sets a node's opacity.voidsetStroke(ShapeNode s, StrokeStyle stroke) Replaces a shape's strokes with one stroke.voidReplaces a text node's characters, keeping the first run's style.voidsetTransform(ArtNode n, Matrix t) Sets a node's transform.voidsetUndoLimit(int limit) How many changes are kept for undo.voidsetVisible(ArtNode n, boolean visible) Shows or hides a node.The transform from a node's own space to the document, its parents' transforms multiplied in.voidtranslateSelection(double dx, double dy) Moves the selected nodes by an offset in document units.booleanundo()Undoes the last change; returns false when there is none.voidReplaces a group by its children, which keep their place visually, and selects them.
-
Constructor Details
-
ArtModel
public ArtModel()Creates a model over an empty 800 by 600 document. -
ArtModel
Creates a model over a document. The model edits that document in place.
Parameters
document: the document
-
-
Method Details
-
getDocument
The document. -
setDocument
Replaces the document and clears the undo history and the selection.
Parameters
doc: the new document
-
addArtListener
Adds a listener.
Parameters
l: the listener
-
removeArtListener
Removes a listener.
Parameters
l: the listener
-
checkpoint
public void checkpoint()Saves the current tree so the next change can be undone. Operations call this themselves; call it before changing nodes directly, or useedit. -
beginGroup
public void beginGroup()Starts a group of changes that undo as one. -
endGroup
public void endGroup()Ends a group started bybeginGroup. -
edit
Makes an arbitrary change undoable: saves the tree, runs the change and notifies the listeners.
Parameters
change: changes nodes of the document
-
changed
public void changed()Tells the listeners the nodes changed, after a change made betweencheckpointand now, such as each step of a drag. -
canUndo
public boolean canUndo()True when there is something to undo. -
canRedo
public boolean canRedo()True when there is something to redo. -
undo
public boolean undo()Undoes the last change; returns false when there is none. -
redo
public boolean redo()Redoes the last undone change; returns false when there is none. -
setUndoLimit
public void setUndoLimit(int limit) How many changes are kept for undo.
Parameters
limit: at least 1
-
getSelection
-
getSelected
The first selected node, or null. -
select
Selects a node.
Parameters
n: the node, or null to clear the selectionadd: true to add it to the selection, false to replace the selection
-
clearSelection
public void clearSelection()Clears the selection. -
isSelected
True when the node is selected.
Parameters
n: the node
-
findById
-
parentOf
-
toDocument
-
insert
-
add
Adds a node on top of the document, or of the selected node's group, and selects it.
Parameters
n: the node
-
remove
Removes a node.
Parameters
n: the node
-
removeSelection
public void removeSelection()Removes the selected nodes. -
move
-
reorder
Moves a node one step up (toward the top) or down among its siblings.
Parameters
n: the nodeup: true to bring it forward
-
group
-
ungroup
Replaces a group by its children, which keep their place visually, and selects them.
Parameters
g: the group
-
setOpacity
Sets a node's opacity.
Parameters
n: the nodeopacity: 0 to 1
-
setVisible
Shows or hides a node.
Parameters
n: the nodevisible: true to show it
-
setLocked
Locks or unlocks a node.
Parameters
n: the nodelocked: true to lock it
-
setName
-
setBlendMode
-
setTransform
-
translateSelection
public void translateSelection(double dx, double dy) Moves the selected nodes by an offset in document units.
Parameters
dx: x offsetdy: y offset
-
setFill
-
setStroke
Replaces a shape's strokes with one stroke.
Parameters
s: the shapestroke: the stroke, or null for none
-
addEffect
-
removeEffect
Removes an effect from a node.
Parameters
n: the nodeindex: the effect's position
-
setEffectParameter
-
setText
-