org.micropsi.comp.console.worldconsole
Class WorldRenderer

java.lang.Object
  extended byorg.micropsi.comp.console.worldconsole.WorldRenderer
All Implemented Interfaces:
IRenderInfo

public class WorldRenderer
extends java.lang.Object
implements IRenderInfo

This class represents all information needed to render a 2D view of the world at a sprecific resolution. It caches resolution-dependent information and can update parts of it with given world model data. It's main service is to render a 2d view of the world to a given graphics context. Objects of this class have to be disposed after usage using dispose().


Nested Class Summary
 class WorldRenderer.ObjectPainter
           
 class WorldRenderer.OverlayInfo
           
 
Field Summary
protected static double baseScale
           
protected  java.util.Map scaledImageCache
           
 
Constructor Summary
WorldRenderer(LocalWorld worldController, EditSession editSession)
          Creates a renderer that uses a given world model and - if not null - the given edit session.
WorldRenderer(LocalWorld worldController, EditSession editSession, IOverlayChangeNotifier changeNotifier)
          Creates a renderer that uses a given world model and - if not null - the given edit session and an object to request redraw.
 
Method Summary
protected  void clearImageCache()
           
 void dispose()
           
 Rectangle dragObjectPainterBy(WorldRenderer.ObjectPainter objPainter, int x, int y)
           
 void endDragging(WorldRenderer.ObjectPainter objPainter)
           
 WorldObject getClickedObject(int x, int y)
           
 WorldObject getClickedObjectUnderSelected(int x, int y)
           
 Position getDragPosition(WorldRenderer.ObjectPainter objPainter, int x, int y)
           
protected  EditSession getEditSession()
           
protected  LocalWorld getLocalWorld()
           
 Rectangle getObjectBounds(WorldObject obj)
          Returns a rectangle describing the image extends of the given object in screen coordinates.
protected  java.util.Map getObjectPainters()
           
protected  java.util.Collection getObjectPaintersSorted()
           
 java.util.Collection getObjectsInSelectionRectangle()
           
 WorldRenderer.OverlayInfo getOverlayInfo(java.lang.String name)
          Returns the WorldRenderer.OverlayInfo objects with the given name, null if none exists.
 java.util.List getOverlayInfoList()
          Returns a List of WorldRenderer.OverlayInfo objects.
 double getScaleX()
           
 double getScaleY()
           
 int getScreenX(double x)
           
 int getScreenX(Position pos)
           
 int getScreenY(double y)
           
 int getScreenY(Position pos)
           
 java.util.Set getSelectedObjectPainters()
           
 Rectangle getSelectionRectangle()
          Gets the rectangle shown while selecting areas.
 Point getSizeRenderedWorld()
           
 Position getWorldHighestCoords()
           
 Position getWorldLowestCoords()
           
 Position getWorldPosition(int screenX, int screenY)
           
protected  void initOverlayRenderer(LocalWorld world, EditSession editSession, IOverlayChangeNotifier changeNotifier)
           
 boolean isOverlayEnabled(java.lang.String name)
          Returns true if the overlay with the given name is enabled, false if it is not or if there is no such overlay.
protected  void paintBackground(GC gc, int offsetX, int offsetY)
           
protected  void paintObjects(GC gc, int offsetX, int offsetY)
           
protected  void paintOverlay(GC gc, int offsetX, int offsetY, WorldRenderer.OverlayInfo renderInfo)
           
protected  void paintSelectionRectangle(GC gc, int offsetX, int offsetY)
           
 void paintWorld(GC gc, int offsetX, int offsetY)
          Paints part of the current world on the given GC.
 void paintWorld(GC gc, long objectId)
          Convenience method.
 void paintWorld(GC gc, Position pos)
          Convenience method.
 void prepareDragging(WorldRenderer.ObjectPainter objPainter)
           
protected  void resetObjectPaintersSort()
           
 void setGroundmapArea(Area2D area)
           
 boolean setOverlayEnabled(java.lang.String name, boolean enabled)
          Sets the enabled state of the overlay with the given name.
 void setScale(double sX, double sY)
           
 void setWorldArea(Area2D area)
           
 Rectangle startDragging(WorldRenderer.ObjectPainter objPainter)
           
 void updateAllObjects()
           
 Rectangle updateMultipleObjects(java.util.Collection changedObjects)
          Creates or updates resolution specific data for a collection of given world objects.
 Rectangle updateObject(WorldObject changedObject)
          Creates or updates resolution specific data for a given world object.
 Rectangle updateObject(WorldObject changedObject, boolean forceRefresh)
           
 Rectangle updateSelectionRectangle(Rectangle selectionRectangle)
          Sets the rectangle shown while selecting areas.
 Rectangle updateSelectionState(WorldObject obj)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

baseScale

protected static final double baseScale
See Also:
Constant Field Values

scaledImageCache

protected java.util.Map scaledImageCache
Constructor Detail

WorldRenderer

public WorldRenderer(LocalWorld worldController,
                     EditSession editSession)
Creates a renderer that uses a given world model and - if not null - the given edit session. This object creates SWT resources and therefore has to be disposed after usage using dispose().

Parameters:
worldController - - the world model to use.
editSession - - an edit session that will be used. May be null.
See Also:
LocalWorld, @see EditSession

WorldRenderer

public WorldRenderer(LocalWorld worldController,
                     EditSession editSession,
                     IOverlayChangeNotifier changeNotifier)
Creates a renderer that uses a given world model and - if not null - the given edit session and an object to request redraw. Redraw is only requested by overlays, otherwise it has to be managed by the render target itself. This object creates SWT resources and therefore has to be disposed after usage using dispose().

Parameters:
worldController - - the world model to use.
editSession - - an edit session that will be used. May be null.
changeNotifier - - instance for overlays to request redraw of the render target. May be null.
See Also:
LocalWorld, @see EditSession
Method Detail

initOverlayRenderer

protected void initOverlayRenderer(LocalWorld world,
                                   EditSession editSession,
                                   IOverlayChangeNotifier changeNotifier)

updateAllObjects

public void updateAllObjects()

updateObject

public Rectangle updateObject(WorldObject changedObject)
Creates or updates resolution specific data for a given world object. Calculates the image area that needs to be updated to reflect this change.

Parameters:
changedObject - - the world object whose data should be updated
Returns:
a rectangle describing the image area that needs to be updated. Null if no update is necessary.

updateObject

public Rectangle updateObject(WorldObject changedObject,
                              boolean forceRefresh)

updateSelectionState

public Rectangle updateSelectionState(WorldObject obj)

updateMultipleObjects

public Rectangle updateMultipleObjects(java.util.Collection changedObjects)
Creates or updates resolution specific data for a collection of given world objects. Calculates the image area that needs to be updated to reflect this change.

Parameters:
changedObjects - - the world objects whose data should be updated
Returns:
a rectangle describing the image area that needs to be updated. Null if no update is necessary.

paintWorld

public void paintWorld(GC gc,
                       Position pos)
Convenience method. Paints current world on given GC, placing Position pos to the center and using gc.getClipping() to determin the requested image size.

Parameters:
gc - - target GC
pos - - Position to be placed in the center of the image

paintWorld

public void paintWorld(GC gc,
                       long objectId)
Convenience method. Paints current world on given GC, placing object with id objectId to the center and using gc.getClipping() to determin the requested image size. If no object with the given id exists, it will put the world center to the image center

Parameters:
gc - - target GC
objectId - - id of object to be placed in the center

paintWorld

public void paintWorld(GC gc,
                       int offsetX,
                       int offsetY)
Paints part of the current world on the given GC. The 'screen' coordinates (offsetX, offsetY) will be placed in the upper left corner of the GC's clipping, the size of the painted part is determined by the size of the GC's clipping.

Parameters:
gc - - GC to paint on
offsetX - - x 'screen' coordinate to be placed on the left border of the painted part
offsetY - - y 'screen' coordinate to be placed on the top border of the painted part

paintOverlay

protected void paintOverlay(GC gc,
                            int offsetX,
                            int offsetY,
                            WorldRenderer.OverlayInfo renderInfo)

paintObjects

protected void paintObjects(GC gc,
                            int offsetX,
                            int offsetY)

paintBackground

protected void paintBackground(GC gc,
                               int offsetX,
                               int offsetY)
Parameters:
gc -

paintSelectionRectangle

protected void paintSelectionRectangle(GC gc,
                                       int offsetX,
                                       int offsetY)

setScale

public void setScale(double sX,
                     double sY)

getClickedObject

public WorldObject getClickedObject(int x,
                                    int y)

getClickedObjectUnderSelected

public WorldObject getClickedObjectUnderSelected(int x,
                                                 int y)

dispose

public void dispose()

clearImageCache

protected void clearImageCache()

getScreenX

public int getScreenX(Position pos)
Specified by:
getScreenX in interface IRenderInfo

getScreenX

public int getScreenX(double x)
Specified by:
getScreenX in interface IRenderInfo

getScreenY

public int getScreenY(Position pos)
Specified by:
getScreenY in interface IRenderInfo

getScreenY

public int getScreenY(double y)
Specified by:
getScreenY in interface IRenderInfo

getWorldPosition

public Position getWorldPosition(int screenX,
                                 int screenY)
Specified by:
getWorldPosition in interface IRenderInfo

getObjectBounds

public Rectangle getObjectBounds(WorldObject obj)
Description copied from interface: IRenderInfo
Returns a rectangle describing the image extends of the given object in screen coordinates. May return null.

Specified by:
getObjectBounds in interface IRenderInfo
Parameters:
obj - - the object
Returns:
- rectangle of object icon, null if object doesnot exist.

getObjectPaintersSorted

protected java.util.Collection getObjectPaintersSorted()

resetObjectPaintersSort

protected void resetObjectPaintersSort()

getLocalWorld

protected LocalWorld getLocalWorld()
Returns:
Returns the localWorld.

getObjectPainters

protected java.util.Map getObjectPainters()
Returns:
Returns the objectPainters.

getSizeRenderedWorld

public Point getSizeRenderedWorld()
Specified by:
getSizeRenderedWorld in interface IRenderInfo

getWorldHighestCoords

public Position getWorldHighestCoords()
Specified by:
getWorldHighestCoords in interface IRenderInfo
Returns:
Returns the worldHighestCoords.

getWorldLowestCoords

public Position getWorldLowestCoords()
Specified by:
getWorldLowestCoords in interface IRenderInfo
Returns:
Returns the worldLowestCoords.

getScaleX

public double getScaleX()
Specified by:
getScaleX in interface IRenderInfo
Returns:
Returns the scaleX.

getScaleY

public double getScaleY()
Specified by:
getScaleY in interface IRenderInfo
Returns:
Returns the scaleY.

setWorldArea

public void setWorldArea(Area2D area)

setGroundmapArea

public void setGroundmapArea(Area2D area)

getEditSession

protected EditSession getEditSession()
Returns:
Returns the editSession.

getDragPosition

public Position getDragPosition(WorldRenderer.ObjectPainter objPainter,
                                int x,
                                int y)

prepareDragging

public void prepareDragging(WorldRenderer.ObjectPainter objPainter)

startDragging

public Rectangle startDragging(WorldRenderer.ObjectPainter objPainter)

endDragging

public void endDragging(WorldRenderer.ObjectPainter objPainter)

dragObjectPainterBy

public Rectangle dragObjectPainterBy(WorldRenderer.ObjectPainter objPainter,
                                     int x,
                                     int y)

getSelectedObjectPainters

public java.util.Set getSelectedObjectPainters()
Returns:
Returns the selectedObjectPainters.

getSelectionRectangle

public Rectangle getSelectionRectangle()
Gets the rectangle shown while selecting areas. Returns null when no selection rectangle is shown.

Returns:
Returns the selectionRectangle. May be null

updateSelectionRectangle

public Rectangle updateSelectionRectangle(Rectangle selectionRectangle)
Sets the rectangle shown while selecting areas. Set null when no selection rectangle should be shown. Returns the image area that needs to be updated to reflect this change.

Parameters:
selectionRectangle - The selectionRectangle to set. May be null.
Returns:
a rectangle describing the image area that needs to be updated. Null if no update is necessary.

getObjectsInSelectionRectangle

public java.util.Collection getObjectsInSelectionRectangle()

getOverlayInfoList

public java.util.List getOverlayInfoList()
Returns a List of WorldRenderer.OverlayInfo objects. Instances can be modified. Modification does not trigger redraw.

Returns:
Returns the overlayInfoList.

getOverlayInfo

public WorldRenderer.OverlayInfo getOverlayInfo(java.lang.String name)
Returns the WorldRenderer.OverlayInfo objects with the given name, null if none exists. Instance can be modified. Modification does not trigger redraw.

Returns:
the overlayInfo.

isOverlayEnabled

public boolean isOverlayEnabled(java.lang.String name)
Returns true if the overlay with the given name is enabled, false if it is not or if there is no such overlay.

Parameters:
name - - the name of the overlay
Returns:
true if overlay is enabled

setOverlayEnabled

public boolean setOverlayEnabled(java.lang.String name,
                                 boolean enabled)
Sets the enabled state of the overlay with the given name. Does not trigger redraw. Returns true if something has changed.

Parameters:
name - - the name of the overlay
enabled - - the new state
Returns:
- true, if something has changed

(c) by Micropsi Project: http://www.cognitive-agents.org

.