com.threerings.stage.util
Class PlacementConstraints

java.lang.Object
  extended by com.threerings.stage.util.PlacementConstraints
All Implemented Interfaces:
InvocationCodes, StageCodes, DirectionCodes

public class PlacementConstraints
extends Object
implements DirectionCodes, StageCodes

Maintains extra information on objects in a scene and checks proposed placement operations for constraint violations. When the constraints object is in use, all placement operations (object additions and removals) must go through the constraints object so that the object's internal state remains consistent.


Nested Class Summary
protected static class PlacementConstraints.DirectionType
          Contains the direction and height of a constraint.
protected  class PlacementConstraints.ObjectData
          Contains information about an object used in checking constraints.
 
Field Summary
protected static Rectangle _constrainRect
          One rectangle we'll re-use for all constraints ops.
protected  StageMisoSceneModel _mmodel
          The Miso scene model.
protected  HashMap<com.threerings.miso.data.ObjectInfo,PlacementConstraints.ObjectData> _objectData
          For all objects in the scene, maps ObjectInfos to PlacementConstraints.ObjectDatas.
protected  StageScene _scene
          The scene being checked for constraints.
protected  com.threerings.media.tile.TileManager _tilemgr
          The tile manager to use for object dimensions and constraints.
 
Fields inherited from interface com.threerings.util.DirectionCodes
CARDINAL_DIRECTIONS, CCW, CW, DIRECTION_COUNT, DOWN, EAST, EASTNORTHEAST, EASTSOUTHEAST, FINE_DIRECTION_COUNT, HORIZONTAL, LEFT, NONE, NORTH, NORTHEAST, NORTHNORTHEAST, NORTHNORTHWEST, NORTHWEST, RIGHT, SOUTH, SOUTHEAST, SOUTHSOUTHEAST, SOUTHSOUTHWEST, SOUTHWEST, UP, VERTICAL, WEST, WESTNORTHWEST, WESTSOUTHWEST
 
Fields inherited from interface com.threerings.stage.data.StageCodes
ERR_CANNOT_CLUSTER, ERR_NO_OVERLAP, MODIFY_SCENE_ACCESS, MUTILATE_SCENE_ACCESS, STAGE_MESSAGE_BUNDLE, TILESET_RSRC_SET
 
Fields inherited from interface com.threerings.presents.data.InvocationCodes
ACCESS_DENIED, E_ACCESS_DENIED, E_INTERNAL_ERROR, GLOBAL_GROUP, INTERNAL_ERROR
 
Constructor Summary
PlacementConstraints(com.threerings.media.tile.TileManager tilemgr, StageScene scene)
          Default constructor.
 
Method Summary
 void addObject(com.threerings.miso.data.ObjectInfo info)
          Adds the specified object through the constraints.
 String allowAddObject(com.threerings.miso.data.ObjectInfo info)
          Determines whether the constraints allow the specified object to be added to the scene.
 String allowModifyObjects(com.threerings.miso.data.ObjectInfo[] added, com.threerings.miso.data.ObjectInfo[] removed)
          Determines whether the constraints allow the specified objects to be added and removed simultaneously.
protected  String allowModifyObjects(PlacementConstraints.ObjectData[] added, PlacementConstraints.ObjectData[] removed)
          Determines whether the constraints allow the specified objects to be added and removed simultaneously.
 String allowRemoveObject(com.threerings.miso.data.ObjectInfo info)
          Determines whether the constraints allow the specified object to be removed from the scene.
protected  PlacementConstraints.ObjectData createObjectData(com.threerings.miso.data.ObjectInfo info)
          Using the tile manager, computes and returns the specified object's data.
protected  Rectangle getAdjacentEdge(Rectangle rect, int dir)
          Creates and returns a rectangle that covers the specified rectangle's adjacent edge (the squares one tile beyond the bounds) in the specified direction.
protected  int getConstraintDirection(PlacementConstraints.ObjectData data, String prefix)
          Returns the direction in which the specified object is constrained by appending "[NESW]" to the given constraint prefix.
protected  boolean getConstraintDirectionType(PlacementConstraints.ObjectData data, String prefix, PlacementConstraints.DirectionType dirtype)
          Populates the supplied PlacementConstraints.ObjectData object with the direction and height of the constraint identified by the given prefix.
protected  int getConstraintWallType(String constraint)
           
protected  String getDirectionalConstraint(String prefix, int dir)
          Given a constraint prefix and a direction, returns the directional constraint.
protected  String getDirectionalConstraint(String prefix, int dir, int type)
          Given a constraint prefix, direction, and height, returns the directional constraint.
protected  List<PlacementConstraints.ObjectData> getObjectData(Rectangle rect, PlacementConstraints.ObjectData[] added, PlacementConstraints.ObjectData[] removed)
          Finds all objects whose bounds intersect the given rectangle and returns a list containing their PlacementConstraints.ObjectData elements.
protected  PlacementConstraints.ObjectData[] getObjectDataFromInfo(com.threerings.miso.data.ObjectInfo[] info)
          Returns an ObjectData array that corresponds to the supplied ObjectInfo array.
protected  boolean hasAttached(PlacementConstraints.ObjectData data, PlacementConstraints.ObjectData[] added, PlacementConstraints.ObjectData[] removed, int dir)
          Determines whether the specified wall has anything attached to it that won't be held up if the wall is removed.
protected  boolean hasOnSurface(PlacementConstraints.ObjectData data, PlacementConstraints.ObjectData[] added, PlacementConstraints.ObjectData[] removed)
          Determines whether the specified surface has anything on it that won't be held up if the surface is removed.
protected  boolean hasOnWall(PlacementConstraints.ObjectData data, PlacementConstraints.ObjectData[] added, PlacementConstraints.ObjectData[] removed, int dir)
          Determines whether the specified wall has anything on it that won't be held up if the wall is removed.
protected  boolean hasSpace(PlacementConstraints.ObjectData data, PlacementConstraints.ObjectData[] added, PlacementConstraints.ObjectData[] removed, int dir)
          Determines whether the specified object has empty space in the specified direction.
protected  boolean hasSpaceConstrainedAdjacent(PlacementConstraints.ObjectData data, PlacementConstraints.ObjectData[] added, PlacementConstraints.ObjectData[] removed)
          Verifies that the objects adjacent to the given object will still have their space constraints met if the object is added.
protected  boolean isAttached(PlacementConstraints.ObjectData data, PlacementConstraints.ObjectData[] added, PlacementConstraints.ObjectData[] removed, int dir, int type)
          Determines whether the specified object is attached to another object in the specified direction and at the specified height.
protected  boolean isCovered(Rectangle rect, PlacementConstraints.ObjectData[] added, PlacementConstraints.ObjectData[] removed, String constraint, String altstraint)
          Given a rectangle, determines whether all of the tiles within the rectangle intersect an object.
protected  boolean isOnSurface(PlacementConstraints.ObjectData data, PlacementConstraints.ObjectData[] added, PlacementConstraints.ObjectData[] removed)
          Determines whether the specified object is on a surface.
protected  boolean isOnWall(PlacementConstraints.ObjectData data, PlacementConstraints.ObjectData[] added, PlacementConstraints.ObjectData[] removed, int dir, int type)
          Determines whether the specified object is on a wall facing the specified direction.
 void removeObject(com.threerings.miso.data.ObjectInfo info)
          Removes the specified object through the constraints.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_tilemgr

protected com.threerings.media.tile.TileManager _tilemgr
The tile manager to use for object dimensions and constraints.


_scene

protected StageScene _scene
The scene being checked for constraints.


_mmodel

protected StageMisoSceneModel _mmodel
The Miso scene model.


_objectData

protected HashMap<com.threerings.miso.data.ObjectInfo,PlacementConstraints.ObjectData> _objectData
For all objects in the scene, maps ObjectInfos to PlacementConstraints.ObjectDatas.


_constrainRect

protected static final Rectangle _constrainRect
One rectangle we'll re-use for all constraints ops.

Constructor Detail

PlacementConstraints

public PlacementConstraints(com.threerings.media.tile.TileManager tilemgr,
                            StageScene scene)
Default constructor.

Method Detail

allowAddObject

public String allowAddObject(com.threerings.miso.data.ObjectInfo info)
Determines whether the constraints allow the specified object to be added to the scene.

Returns:
null if the constraints allow the operation, otherwise a translatable string explaining why the object can't be added

addObject

public void addObject(com.threerings.miso.data.ObjectInfo info)
Adds the specified object through the constraints.


allowRemoveObject

public String allowRemoveObject(com.threerings.miso.data.ObjectInfo info)
Determines whether the constraints allow the specified object to be removed from the scene.

Returns:
null if the constraints allow the operation, otherwise a translatable string explaining why the object can't be removed

removeObject

public void removeObject(com.threerings.miso.data.ObjectInfo info)
Removes the specified object through the constraints.


allowModifyObjects

public String allowModifyObjects(com.threerings.miso.data.ObjectInfo[] added,
                                 com.threerings.miso.data.ObjectInfo[] removed)
Determines whether the constraints allow the specified objects to be added and removed simultaneously.

Returns:
null if the constraints allow the operation, otherwise a translatable string explaining why the objects can't be modified

getObjectDataFromInfo

protected PlacementConstraints.ObjectData[] getObjectDataFromInfo(com.threerings.miso.data.ObjectInfo[] info)
Returns an ObjectData array that corresponds to the supplied ObjectInfo array. Returns null on error.


allowModifyObjects

protected String allowModifyObjects(PlacementConstraints.ObjectData[] added,
                                    PlacementConstraints.ObjectData[] removed)
Determines whether the constraints allow the specified objects to be added and removed simultaneously. Subclasses that wish to define additional constraints should override this method.

Returns:
null if the constraints allow the operation, otherwise a qualified translatable string explaining why the objects can't be modified

hasOnSurface

protected boolean hasOnSurface(PlacementConstraints.ObjectData data,
                               PlacementConstraints.ObjectData[] added,
                               PlacementConstraints.ObjectData[] removed)
Determines whether the specified surface has anything on it that won't be held up if the surface is removed.


hasOnWall

protected boolean hasOnWall(PlacementConstraints.ObjectData data,
                            PlacementConstraints.ObjectData[] added,
                            PlacementConstraints.ObjectData[] removed,
                            int dir)
Determines whether the specified wall has anything on it that won't be held up if the wall is removed.


hasAttached

protected boolean hasAttached(PlacementConstraints.ObjectData data,
                              PlacementConstraints.ObjectData[] added,
                              PlacementConstraints.ObjectData[] removed,
                              int dir)
Determines whether the specified wall has anything attached to it that won't be held up if the wall is removed.


hasSpaceConstrainedAdjacent

protected boolean hasSpaceConstrainedAdjacent(PlacementConstraints.ObjectData data,
                                              PlacementConstraints.ObjectData[] added,
                                              PlacementConstraints.ObjectData[] removed)
Verifies that the objects adjacent to the given object will still have their space constraints met if the object is added.


hasSpace

protected boolean hasSpace(PlacementConstraints.ObjectData data,
                           PlacementConstraints.ObjectData[] added,
                           PlacementConstraints.ObjectData[] removed,
                           int dir)
Determines whether the specified object has empty space in the specified direction.


isOnSurface

protected boolean isOnSurface(PlacementConstraints.ObjectData data,
                              PlacementConstraints.ObjectData[] added,
                              PlacementConstraints.ObjectData[] removed)
Determines whether the specified object is on a surface.


isOnWall

protected boolean isOnWall(PlacementConstraints.ObjectData data,
                           PlacementConstraints.ObjectData[] added,
                           PlacementConstraints.ObjectData[] removed,
                           int dir,
                           int type)
Determines whether the specified object is on a wall facing the specified direction.


isAttached

protected boolean isAttached(PlacementConstraints.ObjectData data,
                             PlacementConstraints.ObjectData[] added,
                             PlacementConstraints.ObjectData[] removed,
                             int dir,
                             int type)
Determines whether the specified object is attached to another object in the specified direction and at the specified height.


isCovered

protected boolean isCovered(Rectangle rect,
                            PlacementConstraints.ObjectData[] added,
                            PlacementConstraints.ObjectData[] removed,
                            String constraint,
                            String altstraint)
Given a rectangle, determines whether all of the tiles within the rectangle intersect an object. If the constraint parameter is non-null, the intersected objects must have that constraint (or the alternate constraint, if specified).


getAdjacentEdge

protected Rectangle getAdjacentEdge(Rectangle rect,
                                    int dir)
Creates and returns a rectangle that covers the specified rectangle's adjacent edge (the squares one tile beyond the bounds) in the specified direction.


getConstraintDirection

protected int getConstraintDirection(PlacementConstraints.ObjectData data,
                                     String prefix)
Returns the direction in which the specified object is constrained by appending "[NESW]" to the given constraint prefix. Returns NONE if there is no such directional constraint.


getConstraintDirectionType

protected boolean getConstraintDirectionType(PlacementConstraints.ObjectData data,
                                             String prefix,
                                             PlacementConstraints.DirectionType dirtype)
Populates the supplied PlacementConstraints.ObjectData object with the direction and height of the constraint identified by the given prefix.

Returns:
true if the object was successfully populated, false if there is no such constraint

getConstraintWallType

protected int getConstraintWallType(String constraint)

getDirectionalConstraint

protected String getDirectionalConstraint(String prefix,
                                          int dir)
Given a constraint prefix and a direction, returns the directional constraint.


getDirectionalConstraint

protected String getDirectionalConstraint(String prefix,
                                          int dir,
                                          int type)
Given a constraint prefix, direction, and height, returns the directional constraint.


getObjectData

protected List<PlacementConstraints.ObjectData> getObjectData(Rectangle rect,
                                                              PlacementConstraints.ObjectData[] added,
                                                              PlacementConstraints.ObjectData[] removed)
Finds all objects whose bounds intersect the given rectangle and returns a list containing their PlacementConstraints.ObjectData elements.

Parameters:
added - an array of objects to add to the search
removed - an array of objects to exclude from the search

createObjectData

protected PlacementConstraints.ObjectData createObjectData(com.threerings.miso.data.ObjectInfo info)
Using the tile manager, computes and returns the specified object's data.