diva.sketch
Class BasicInterpreter

java.lang.Object
  extended by diva.canvas.interactor.AbstractInteractor
      extended by diva.sketch.BasicInterpreter
All Implemented Interfaces:
LayerListener, LayerMotionListener, Interactor, EventListener
Direct Known Subclasses:
HighlightInterpreter, MultiStateInterpreter

public class BasicInterpreter
extends AbstractInteractor

A class that interprets changes to a stroke. BasicInterpreter is a sketch interpreter that gets called whenever a user sketches on the screen. It extends from diva.canvas.AbstractInteractor and should be added to the pane layer in order to receive mouse events. Since BasicInterpreter is a very simple implementation of a sketch interpreter, it doesn't do any interpretation on user input. In general, a sketch interpreter would receive mouse events and use a recognizer to process these events. The recognizer comes back with an answer, and depending on what the answer is, the interpreter tells the sketch controller to perform the corresponding application-specific actions. The recognizer can be arbitrarily complex (or simple). It could be a low-level recognizer that determines the geometric shape of a user stroke or a voting recognizer that uses a bunch of different recognizers. The recognizer is meant to be highly configurable.

Version:
$Revision: 1.38 $
Author:
Michael Shilman (michaels@eecs.berkeley.edu), Heloise (hwawen@eecs.berkeley.edu)

Field Summary
protected  SketchController _controller
          The sketch controller that uses this interpreter to interpret sketch input.
protected  TimedStroke _curStroke
          The current stroke that's being drawn, gets its data from _strokeBuffer and is reallocated at the end of a stroke.
protected  StrokeSymbol _curSymbol
          The current symbol which wraps the current stroke and keeps visual information about the stroke (color, line width).
protected  TimedStroke _strokeBuffer
          A buffer for the stroke that's currently being drawn.
 
Constructor Summary
BasicInterpreter(SketchController c)
          Create a BasicInterpreter which is used by the specified controller to interpret sketch input.
 
Method Summary
protected  void appendStroke(LayerEvent e)
          Append the given new point/timestamp to the current stroke.
protected  void finishStroke(LayerEvent e)
          Called at the end of the mouseReleased method to finish the drawing of a stroke.
 SketchController getController()
          Return the controller that uses this interpreter.
 TimedStroke getCurrentStroke()
          Return the current stroke being drawn.
 Symbol getCurrentSymbol()
          Return the symbol for current stroke being drawn.
 boolean isMotionEnabled()
          We're consuming motion events.
 void mouseDragged(LayerEvent e)
          Update the current stroke and its visual representation.
 void mouseEntered(LayerEvent e)
          Consume the event, for efficiency.
 void mouseExited(LayerEvent e)
          Consume the event, for efficiency.
 void mouseMoved(LayerEvent e)
          Consume the event, for efficiency.
 void mousePressed(LayerEvent e)
          Instantiate a new stroke and add its visual representation as a symbol in the pane.
 void mouseReleased(LayerEvent e)
          Update the current stroke and its visual representation, then reset the stroke to "null".
 void removeCurrentSymbol()
          Remove the current figure from the figure layer.
protected  void startStroke(LayerEvent e)
          This method is invoked upon mouse down.
 
Methods inherited from class diva.canvas.interactor.AbstractInteractor
accept, getMouseFilter, isConsuming, isEnabled, mouseClicked, setConsuming, setEnabled, setMotionEnabled, setMouseFilter
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_controller

protected SketchController _controller
The sketch controller that uses this interpreter to interpret sketch input.


_strokeBuffer

protected TimedStroke _strokeBuffer
A buffer for the stroke that's currently being drawn. This is to avoid allocating a TimedStroke object at the beginning of each new stroke, thus avoiding the overhead of memory allocation. The TimedStroke is initialized to have a capacity of 2000 points which should be plenty for a typical stroke.


_curStroke

protected TimedStroke _curStroke
The current stroke that's being drawn, gets its data from _strokeBuffer and is reallocated at the end of a stroke.


_curSymbol

protected StrokeSymbol _curSymbol
The current symbol which wraps the current stroke and keeps visual information about the stroke (color, line width).

Constructor Detail

BasicInterpreter

public BasicInterpreter(SketchController c)
Create a BasicInterpreter which is used by the specified controller to interpret sketch input.

Method Detail

appendStroke

protected void appendStroke(LayerEvent e)
Append the given new point/timestamp to the current stroke. Consume the event when done. This appends the most recent point to the _strokeBuffer, not _curStroke. It then invokes appendStroke in SketchLayer (if we're using a SketchPane) which paints this last segment in the stroke (not the whole stroke). The prior segments of the stroke must have been drawn by the SketchLayer and they stay rendered (canvas does not get repainted, so they are still visible). Consume the event at the end so that other layers don't attempt to process the event. If we're not using a SketchPane, then call updateSymbol on the sketch model. This is because the stroke in _curSymbol has changed and must be rerendered. The model will send an event to the RepaintListener (in SketchController) telling it to update the symbol.


finishStroke

protected void finishStroke(LayerEvent e)
Called at the end of the mouseReleased method to finish the drawing of a stroke. Instantiate a new TimedStroke for _curStroke. The data is copied over from _strokeBuffer. Set _curSymbol's stroke to be _curStroke. If we're using a SketchPane, we would need to add the symbol to the sketch model at this point. This is because so far, we've been using the SketchLayer to render the last segment in the stroke, so the stroke figure doesn't exist, and if the canvas repaints, the rendered segments will go away. By adding the symbol to the sketch model, we're making the stroke permanent so that it will be redrawn every time the canvas repaints. The SketchLayer rendering is used to speed up the rendering process as the user draws, therefore it only paints the last segment of a stroke. Since we're not repainting the whole canvas nor the whole stroke, the prior drawing stays with the addition of the last piece of the stroke painted on the canvas. If we're not using a SketchPane, the symbol would have already been added to the model in startStroke, otherwise we wouldn't see the stroke as it is being drawn on the canvas.


getController

public final SketchController getController()
Return the controller that uses this interpreter.


getCurrentStroke

public final TimedStroke getCurrentStroke()
Return the current stroke being drawn.


getCurrentSymbol

public final Symbol getCurrentSymbol()
Return the symbol for current stroke being drawn.


isMotionEnabled

public boolean isMotionEnabled()
We're consuming motion events.

Specified by:
isMotionEnabled in interface Interactor
Overrides:
isMotionEnabled in class AbstractInteractor

mouseDragged

public void mouseDragged(LayerEvent e)
Update the current stroke and its visual representation.

Specified by:
mouseDragged in interface LayerListener
Overrides:
mouseDragged in class AbstractInteractor

mouseEntered

public void mouseEntered(LayerEvent e)
Consume the event, for efficiency.

Specified by:
mouseEntered in interface LayerMotionListener
Overrides:
mouseEntered in class AbstractInteractor

mouseExited

public void mouseExited(LayerEvent e)
Consume the event, for efficiency.

Specified by:
mouseExited in interface LayerMotionListener
Overrides:
mouseExited in class AbstractInteractor

mouseMoved

public void mouseMoved(LayerEvent e)
Consume the event, for efficiency.

Specified by:
mouseMoved in interface LayerMotionListener
Overrides:
mouseMoved in class AbstractInteractor

mousePressed

public void mousePressed(LayerEvent e)
Instantiate a new stroke and add its visual representation as a symbol in the pane.

Specified by:
mousePressed in interface LayerListener
Overrides:
mousePressed in class AbstractInteractor

mouseReleased

public void mouseReleased(LayerEvent e)
Update the current stroke and its visual representation, then reset the stroke to "null".

Specified by:
mouseReleased in interface LayerListener
Overrides:
mouseReleased in class AbstractInteractor

removeCurrentSymbol

public void removeCurrentSymbol()
Remove the current figure from the figure layer.


startStroke

protected void startStroke(LayerEvent e)
This method is invoked upon mouse down. Reset the _strokeBuffer to clear the previou stroke. Set the current stroke (_curStroke) to point to the stroke buffer, and instantiate a StrokeSymbol (_curSymbol) that paints the stroke with outline/fill color and line width specified in the controller. If a SketchPane is used, get the SketchLayer and call startStroke, this will set the starting point of the line segment to be drawn. SketchLayer will take care of drawing the last line segment in the current stroke, the prior segments stay on the canvas, they are not being rerendered. If a SketchPane is not being used, then add _curSymbol to the sketch model so that it can be rendered ('cause there will be no SketchLayer).



Copyright © 2015 Central Laboratory of the Research Councils. All Rights Reserved.