Class ArtModel

java.lang.Object
com.codename1.art.ArtModel

public class ArtModel extends Object

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 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
      • document: 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
      • doc: the new document
    • addArtListener

      public void addArtListener(ArtListener l)

      Adds a listener.

      Parameters
      • l: the listener
    • removeArtListener

      public void removeArtListener(ArtListener l)

      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 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
      • change: 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
      • limit: 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
      • n: the node, or null to clear the selection
      • add: 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
      • n: the node
    • findById

      public ArtNode findById(String id)

      The node with an id, or null.

      Parameters
      • id: the id
    • parentOf

      public GroupNode parentOf(ArtNode n)

      The group holding a node, or null for the root.

      Parameters
      • n: 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
      • n: the node
    • insert

      public void insert(GroupNode parent, int index, ArtNode n)

      Inserts a node.

      Parameters
      • parent: the group, or null for the root
      • index: the position among its children, or -1 for the top
      • n: 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
      • n: the node
    • remove

      public void remove(ArtNode n)

      Removes a node.

      Parameters
      • n: 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
      • n: the node
      • parent: the new group, or null for the root
      • index: 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
      • n: the node
      • up: 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
      • nodes: 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
      • g: the group
    • setOpacity

      public void setOpacity(ArtNode n, float opacity)

      Sets a node's opacity.

      Parameters
      • n: the node
      • opacity: 0 to 1
    • setVisible

      public void setVisible(ArtNode n, boolean visible)

      Shows or hides a node.

      Parameters
      • n: the node
      • visible: true to show it
    • setLocked

      public void setLocked(ArtNode n, boolean locked)

      Locks or unlocks a node.

      Parameters
      • n: the node
      • locked: true to lock it
    • setName

      public void setName(ArtNode n, String name)

      Renames a node.

      Parameters
      • n: the node
      • name: the name
    • setBlendMode

      public void setBlendMode(ArtNode n, BlendMode mode)

      Sets a node's blend mode.

      Parameters
      • n: the node
      • mode: the mode
    • setTransform

      public void setTransform(ArtNode n, Matrix t)

      Sets a node's transform.

      Parameters
      • n: the node
      • t: 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
      • dx: x offset
      • dy: y offset
    • setFill

      public void setFill(ShapeNode s, ArtPaint paint)

      Replaces a shape's fills with one paint.

      Parameters
      • s: the shape
      • paint: the fill, or null for none
    • setStroke

      public void setStroke(ShapeNode s, StrokeStyle stroke)

      Replaces a shape's strokes with one stroke.

      Parameters
      • s: the shape
      • stroke: the stroke, or null for none
    • addEffect

      public void addEffect(ArtNode n, Effect e)

      Adds an effect to a node.

      Parameters
      • n: the node
      • e: the effect
    • removeEffect

      public void removeEffect(ArtNode n, int index)

      Removes an effect from a node.

      Parameters
      • n: the node
      • index: 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
      • n: the node
      • index: the effect's position
      • param: the parameter
      • value: its value
    • setText

      public void setText(TextNode t, String text)

      Replaces a text node's characters, keeping the first run's style.

      Parameters
      • t: the text node
      • text: the characters