Skip to content

Base module template

oleegholm edited this page May 9, 2012 · 16 revisions

This is an example module that writes a text dynamically, following the mouse position. It also sets a line at the cursor position when the mouse is clicked. The code could be copy-pasted and used as a template for new modules. The lines starting with // are comments, and not a part of the code.

see also

Home

Create a new module

If you are pasting this code, please remember to include the copyright statements as well. (The first part)

 //********* IMPORT STATEMENTS ***********
 package com.siigna.module.base.create
 import com.siigna._
 
 //**********OBJEKT DEFINITION ************
 object Template extends Module {
 
 //********* A DESCRIPTIVE TEXT (helpful for others browsing the code ***********
 
 /**
 * A template module that createa a line where the mouse is clicked
 * and writes the text "Hello Siigna World" dynamically while moving the mouse, 
 * and in a display after the mouse is  clicked.
 * by Ole Egholm
 */
 
 //********* VARIABLES DECLARATION ***********
 val eventHandler = EventHandler(stateMap, stateMachine)
 private var point : Option[Vector2D] = None
 private var shape : Option[LineShape] = None
 
 //the text string to be created
  private var text = "Click to create a line in the Siigna World here!"
 
  def stateMap = DirectedGraph(
    'Start    ->   'KeyEscape ->    'End
  )
 
  def stateMachine = Map(
 
    //********* START STATE ***********
 
    //'Start is always the first state, and always present in a module.
    'Start -> ((events : List[Event]) => {
    
     //declaration of a 'Point Guide, that will send the text string to 'Point
    //to draw it dynamically while looking for a MouseDown (waiting for the user to click) in 'Point.  
    def getPointGuide = (p : Vector2D) =>
      TextShape(text,p, 10 * Siigna.paperScale, Attributes())
 
      // the line below can be commented in to help debugging
      //Log.level += Log.DEBUG + Log.SUCCESS
 
      // match on events
      events match {
        case MouseDown(_, MouseButtonRight, _) :: tail => Goto('End)
 
        //if point returns a Message with a point, goto the 'End state to draw a line and end the module.
        case Message(p : Vector2D) :: tail => {
          point = Some(p)
          Goto('End)
        }
        // if everything but a right mouse click is registered, forward to the point module to look for a point.
        case _ => {
          //send the Point Guide ahead before Forwarding to 'Point.
          Controller ! Message(PointGuide(getPointGuide))
          ForwardTo('Point, false)
        }
      }
    }),
 
    //********* MORE STATES COULD BE INSERTED HERE ***********
 
    //********* 'END STATE STARTS HERE ***********
 
 
    //'Start will go to 'SetPoint once 'Point returns with a 'Message.:
    'End -> ((events : List[Event]) => {
      println(events)
      //match on incoming events:
      events match {
        // exit strategy
        case (MouseDown(_, MouseButtonRight, _) | MouseUp(_, MouseButtonRight, _) | KeyDown(Key.Esc, _)) :: tail => Goto('End, false)
        //write the text message if 'Points returns a Message with a 'Point (the point is returned as (p).
        case Message(p : Vector2D) :: tail => {
 
          //CREATE THE LINE:
          shape = Some(LineShape(point.get + Vector2D(0,-50), point.get + Vector2D(0, 50)))
          Create(shape)
        }
 
        // match on everything else
        case _ =>
      }
    })
  )
 //if something needs to be drawn dynamically within the module, it should be done below:
 //override def paint(g : Graphics, transform : TransformationMatrix) = {
 //  ** CODE HERE ** 
 //g draw ... something something
 //}
 }

see also

Home

Create a new module

Clone this wiki locally