com.transmote.nest
Class NestSpriteContainer

java.lang.Object
  extended by java.util.Observable
      extended by com.transmote.nest.events.EventDispatcher
          extended by com.transmote.nest.NestSprite
              extended by com.transmote.nest.NestSpriteContainer
All Implemented Interfaces:
Observer

public class NestSpriteContainer
extends NestSprite

A NestSpriteContainer sits at the top of the Nest display list. All NestSprite instances visible on-screen are descendants of a NestSpriteContainer. This means that traversal up the display list from a NestSprite instance's parent, to its parent, and so on, will ultimately reach the NestSpriteContainer.

updateDisplayList() should be called every frame by application code; updateDisplayList() then travels down the display list, from NestSprite to NestSprite, calling NestSprite.update(PApplet) on each, and then NestSprite.draw(PApplet) on each.

NestSpriteContainer acts as the conduit for input events (such as MouseEvents and KeyEvents) between PApplet and application code (NestSprite instances). Input events are dispatched by PApplet and handled by NestSpriteContainer, then sent down through the display list. See Event for more detail on the event flow.

If the developer wishes to eliminate the display list at runtime, call dispose().

Author:
Eric Socolofsky
+Example
/*
 * DisplayList
 * 
 * Nest provides a simple display list architecture
 * modeled on the ActionScript 3.0 display list.
 * 
 * A NestSpriteContainer lives at the top of a display list.
 * Adding NestSprite children to it will subject them to the
 * same transformations (translation, rotation, scale)
 * applied to their parents.
 * 
 * Press the up and down arrow keys and the backspace key
 * to add and remove NestSprite children.
 */

import com.transmote.nest.*;
import com.transmote.nest.events.*;

NestSpriteContainer spriteContainer;

void setup () {
  size(400, 400);
  
  // create a NestSpriteContainer to hold all other NestSprites,
  // and position it at the vertical center of the screen.
  spriteContainer = new NestSpriteContainer(this);
  spriteContainer.y = 0.5 * height;
}

void draw () {
  background(0);
  
  // calling NestSpriteContainer.update() will automatically
  // loop through all its descendant NestSprite instances,
  // call update() on each, and then call draw() on each.
  spriteContainer.updateDisplayList();
}

void keyPressed () {
  switch (keyCode) {
    case 38:
      // UP pressed
      addSprite();
      break;
    case 40:
      // DOWN pressed
      removeSprite();
      break;
    case 8:
      // delete pressed
      removeAllSprites();
      break;
  }
}

void addSprite () {
  // add a new SineSprite to the front of the display list.
  SineSprite sprite = new SineSprite();
  sprite.x = 10 + spriteContainer.numChildren() * 10;
  spriteContainer.addChild(sprite);
}

void removeSprite () {
  // remove the front-most SineSprite from the display list.
  if (spriteContainer.numChildren() > 0) {
    spriteContainer.removeChild(spriteContainer.numChildren()-1);
  }
}

void removeAllSprites () {
  // remove all SineSprites from the display list.
  while (spriteContainer.numChildren() > 0) {
    spriteContainer.removeChild(spriteContainer.numChildren()-1);
  }
}

Field Summary
 
Fields inherited from class com.transmote.nest.NestSprite
blendMode, boundsLeft, boundsTop, handleCaptureEvents, height, inFront, inputChildren, inputEnabled, NO_BLEND_MODE, rotationX, rotationY, rotationZ, scaleX, scaleY, scaleZ, visible, width, x, y, z
 
Constructor Summary
NestSpriteContainer(PApplet p)
          The NestSpriteContainer to be the top of the display list.
 
Method Summary
 void dispose()
          Frees up this NestSpriteContainer, and all child NestSprites, for garbage collection.
 void keyEvent(processing.event.KeyEvent pEvent)
           
 void mouseEvent(processing.event.MouseEvent pEvent)
           
 void update()
          Deprecated. 
 void updateDisplayList()
          Updates the Nest display list: Calls update() on self and all NestSprite descendants, then calls draw() on self and all NestSprite descendants.
 
Methods inherited from class com.transmote.nest.NestSprite
addChild, addChild, bounds, dispatchEvent, dispose, getChildAt, getChildIndex, globalToLocal, hitTest, hitTest, isDisposed, localToGlobal, mousePt, mouseX, mouseY, numChildren, pApplet, parent, removeChild, removeChild, renderer, screenPt, screenX, screenY, setBounds, setBounds, setChildIndex, setDraw, setDraw, setRenderer, setRenderer, setUpdate, setUpdate, swapChildren, swapChildren
 
Methods inherited from class com.transmote.nest.events.EventDispatcher
addObserver, deleteObserver, dispatchEvent, owner, update
 
Methods inherited from class java.util.Observable
countObservers, deleteObservers, hasChanged, notifyObservers, notifyObservers
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NestSpriteContainer

public NestSpriteContainer(PApplet p)
The NestSpriteContainer to be the top of the display list.

Parameters:
p - A reference to the PApplet running this Processing sketch.
Method Detail

updateDisplayList

public void updateDisplayList()
Updates the Nest display list: Calls update() on self and all NestSprite descendants, then calls draw() on self and all NestSprite descendants.


update

@Deprecated
public void update()
Deprecated. 


dispose

public void dispose()
Frees up this NestSpriteContainer, and all child NestSprites, for garbage collection. NOTE: not yet tested, nor verified in a profiler.

Overrides:
dispose in class NestSprite

mouseEvent

public void mouseEvent(processing.event.MouseEvent pEvent)

keyEvent

public void keyEvent(processing.event.KeyEvent pEvent)


Processing library Nest by Eric Socolofsky. (C) Eric Socolofsky 2011-2013