Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.
public class ArtModel
- Object
- 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 opaqueConstructors
public ArtModel() | Creates a model over an empty 800 by 600 document. |
public ArtModel(ArtDocument document) | Creates a model over a document. |
Methods
Inherited methods
Constructor details
ArtModel
public ArtModel()Creates a model over an empty 800 by 600 document.
ArtModel
public ArtModel(ArtDocument document)Creates a model over a document. The model edits that document in place.
Parameters
documentArtDocument- the document
Method details
getDocument
public ArtDocument getDocument()The document.
setDocument
public void setDocument(ArtDocument doc)Replaces the document and clears the undo history and the selection.
Parameters
docArtDocument- the new document
addArtListener
public void addArtListener(ArtListener l)Adds a listener.
Parameters
lArtListener- the listener
removeArtListener
public void removeArtListener(ArtListener l)Removes a listener.
Parameters
lArtListener- 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 use
edit.beginGroup
public void beginGroup()Starts a group of changes that undo as one.
endGroup
public void endGroup()Ends a group started by
beginGroup.edit
public void edit(Runnable change)Makes an arbitrary change undoable: saves the tree, runs the change and notifies the
listeners.
Parameters
changeRunnable- changes nodes of the document
changed
public void changed()Tells the listeners the nodes changed, after a change made between
checkpoint and
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
limitint- at least 1
getSelection
public List<ArtNode> getSelection()The selected nodes, in the order they were selected.
getSelected
public ArtNode getSelected()The first selected node, or null.
select
public void select(ArtNode n, boolean add)Selects a node.
Parameters
nArtNode- the node, or null to clear the selection
addboolean- true to add it to the selection, false to replace the selection
clearSelection
public void clearSelection()Clears the selection.
isSelected
public boolean isSelected(ArtNode n)True when the node is selected.
Parameters
nArtNode- the node
findById
public ArtNode findById(String id)The node with an id, or null.
Parameters
idString- the id
parentOf
public GroupNode parentOf(ArtNode n)The group holding a node, or null for the root.
Parameters
nArtNode- the node
toDocument
public Matrix toDocument(ArtNode n)The transform from a node’s own space to the document, its parents’ transforms
multiplied in.
Parameters
nArtNode- the node
insert
public void insert(GroupNode parent, int index, ArtNode n)Inserts a node.
Parameters
parentGroupNode- the group, or null for the root
indexint- the position among its children, or -1 for the top
nArtNode- the node
add
public void add(ArtNode n)Adds a node on top of the document, or of the selected node’s group, and selects it.
Parameters
nArtNode- the node
remove
public void remove(ArtNode n)Removes a node.
Parameters
nArtNode- the node
removeSelection
public void removeSelection()Removes the selected nodes.
move
public void move(ArtNode n, GroupNode parent, int index)Moves a node to another group or position.
Parameters
nArtNode- the node
parentGroupNode- the new group, or null for the root
indexint- the position, or -1 for the top
reorder
public void reorder(ArtNode n, boolean up)Moves a node one step up (toward the top) or down among its siblings.
Parameters
nArtNode- the node
upboolean- true to bring it forward
group
public GroupNode group(List<ArtNode> nodes)Puts the nodes into a new group in place of the topmost of them, and selects it.
Parameters
nodesList<ArtNode>- siblings or not; each keeps its place in the document visually
ungroup
public void ungroup(GroupNode g)Replaces a group by its children, which keep their place visually, and selects them.
Parameters
gGroupNode- the group
setOpacity
public void setOpacity(ArtNode n, float opacity)Sets a node’s opacity.
Parameters
nArtNode- the node
opacityfloat- 0 to 1
setVisible
public void setVisible(ArtNode n, boolean visible)Shows or hides a node.
Parameters
nArtNode- the node
visibleboolean- true to show it
setLocked
public void setLocked(ArtNode n, boolean locked)Locks or unlocks a node.
Parameters
nArtNode- the node
lockedboolean- true to lock it
setName
public void setName(ArtNode n, String name)Renames a node.
Parameters
nArtNode- the node
nameString- the name
setBlendMode
public void setBlendMode(ArtNode n, BlendMode mode)Sets a node’s blend mode.
Parameters
nArtNode- the node
modeBlendMode- the mode
setTransform
public void setTransform(ArtNode n, Matrix t)Sets a node’s transform.
Parameters
nArtNode- the node
tMatrix- the transform, in its parent’s space
translateSelection
public void translateSelection(double dx, double dy)Moves the selected nodes by an offset in document units.
Parameters
dxdouble- x offset
dydouble- y offset
setFill
public void setFill(ShapeNode s, ArtPaint paint)Replaces a shape’s fills with one paint.
Parameters
sShapeNode- the shape
paintArtPaint- the fill, or null for none
setStroke
public void setStroke(ShapeNode s, StrokeStyle stroke)Replaces a shape’s strokes with one stroke.
Parameters
sShapeNode- the shape
strokeStrokeStyle- the stroke, or null for none
addEffect
public void addEffect(ArtNode n, Effect e)Adds an effect to a node.
Parameters
nArtNode- the node
eEffect- the effect
removeEffect
public void removeEffect(ArtNode n, int index)Removes an effect from a node.
Parameters
nArtNode- the node
indexint- the effect’s position
setEffectParameter
public void setEffectParameter(ArtNode n, int index, String param, Object value)Sets a parameter of one of a node’s effects.
Parameters
nArtNode- the node
indexint- the effect’s position
paramString- the parameter
valueObject- its value
setText
public void setText(TextNode t, String text)Replaces a text node’s characters, keeping the first run’s style.
Parameters
tTextNode- the text node
textString- the characters