Class DefaultMapModel<G extends GameObject<G,A,R>,A extends MapArchObject<A>,R extends Archetype<G,A,R>>
- java.lang.Object
-
- net.sf.gridarta.model.mapmodel.DefaultMapModel<G,A,R>
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Iterable<MapSquare<G,A,R>>,MapModel<G,A,R>
public class DefaultMapModel<G extends GameObject<G,A,R>,A extends MapArchObject<A>,R extends Archetype<G,A,R>> extends java.lang.Object implements MapModel<G,A,R>
Implementation ofMapModelthat covers the similarities between crossfire and daimonin. The transaction system is not implemented in a way that supports overriding its method for change. Therefore all methods belonging to the transaction system are final.
-
-
Constructor Summary
Constructors Constructor Description DefaultMapModel(@NotNull AutojoinLists<G,A,R> autojoinLists, A mapArchObject, @NotNull ArchetypeChooserModel<G,A,R> archetypeChooserModel, @NotNull GameObjectFactory<G,A,R> gameObjectFactory, @NotNull GameObjectMatchers gameObjectMatchers, @NotNull InsertionMode topmostInsertionMode)Creates a new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddActiveEditType(int editType)Add edit type to the bitmask of active types.voidaddGameObjectToMap(G gameObject, @NotNull java.awt.Point pos, @NotNull InsertionMode insertionMode)Add a gameObject to the map.voidaddMapModelListener(@NotNull MapModelListener<G,A,R> listener)Register a map listener.voidaddMapTransactionListener(@NotNull MapTransactionListener<G,A,R> listener)Registers a map transaction listener.voidaddObjectListToMap(@NotNull java.lang.Iterable<G> objects)Adds a list ofGameObjectsto this map.voidbeginGameObjectChange(G gameObject)Method to notify the model that a game object is about to change.voidbeginSquareChange(@NotNull MapSquare<G,A,R> mapSquare)Method to notify the model that a map square is about to change.voidbeginTransaction(@NotNull java.lang.String name)Starts a new transaction.voidclearMap()Clears this map completely.voidendAllTransactions()Ends all transaction.voidendGameObjectChange(G gameObject)Method to notify the model that a game object was changed.voidendSquareChange(@NotNull MapSquare<G,A,R> mapSquare)Method to notify the model that a map square was changed.voidendTransaction()End a transaction.voidendTransaction(boolean fireEvent)End a transaction.voidfacesReloaded()Will be called whenever the archetype faces have been reloaded.@NotNull java.util.List<G>getAllGameObjects()Returns all game objects.@NotNull ErrorCollector<G,A,R>getErrors()Gets the errors in this map.AgetMapArchObject()Returns the Map Arch Object with the meta information about the map.@Nullable MapFilegetMapFile()Returns the map file.@NotNull MapFilegetMapFile(@NotNull MapPath mapPath)Returns the map file for a map that can be reached by following aMapPathfrom this map.@NotNull MapSquare<G,A,R>getMapSquare(@NotNull java.awt.Point pos)Get the square at a specified location.intgetTransactionDepth()Get the transaction depth, which is the number ofMapModel.beginTransaction(String)invocations without matchingMapModel.endTransaction()invocations.GinsertArchToMap(@NotNull BaseObject<G,A,R,?> templateBaseObject, G nextGameObject, @NotNull java.awt.Point pos, boolean join)Insert a game object to the map at a specified position.GinsertBaseObject(@NotNull BaseObject<G,A,R,?> baseObject, @NotNull java.awt.Point pos, boolean allowMany, boolean join, @NotNull InsertionMode insertionMode)Inserts aBaseObjectto a map.booleanisAnyTransactionActive()Returns whether a transaction is currently active.booleanisAreaEmpty(int left, int top, int width, int height)Checks whether an area of a map is completely empty.booleanisEmpty()Returns whether the map is empty.booleanisModified()Return whether the map has been modified from the on-disk state.booleanisMultiArchFittingToMap(@NotNull Archetype<G,A,R> archetype, @NotNull java.awt.Point pos, boolean allowDouble)Checks whether a GameObject (multi-arch) would still fit on this map.java.util.Iterator<MapSquare<G,A,R>>iterator()voidmapClosed()This function must be called if this instance is freed.voidmoveEnv(G gameObject, @NotNull java.awt.Point pos, G nextGameObject)Moves aGameObjectto its environment.voidmoveInv(G gameObject, @NotNull GameObject<G,A,R> prevGameObject)Moves aGameObjectto the inventory of another game object.voidnextPoint(java.awt.Point point, int direction)Moves the given point forward or backward one map square.voidprependMapModelListener(@NotNull MapModelListener<G,A,R> listener)Register a map listener and call it before all other listeners.voidremoveGameObject(G gameObject, boolean join)Delete an existingGameObjectfrom the map.voidremoveMapModelListener(@NotNull MapModelListener<G,A,R> listener)Unregister a map listener.voidremoveMapTransactionListener(@NotNull MapTransactionListener<G,A,R> listener)Unregisters a map transaction listener.voidresetModified()Resets the modified flag to false.voidsetErrors(@NotNull ErrorCollector<G,A,R> errors)Sets the errors in this map.voidsetMapFile(@Nullable MapFile mapFile)Sets the map file.voidtransientGameObjectChange(G gameObject)Method to notify the model that a game object was changed but need not be restored by undo/redo.
-
-
-
Constructor Detail
-
DefaultMapModel
public DefaultMapModel(@NotNull @NotNull AutojoinLists<G,A,R> autojoinLists, @NotNull A mapArchObject, @NotNull @NotNull ArchetypeChooserModel<G,A,R> archetypeChooserModel, @NotNull @NotNull GameObjectFactory<G,A,R> gameObjectFactory, @NotNull @NotNull GameObjectMatchers gameObjectMatchers, @NotNull @NotNull InsertionMode topmostInsertionMode)Creates a new instance.- Parameters:
autojoinLists- the autojoin lists instance to usemapArchObject- the map arch object to associate with this modelarchetypeChooserModel- the archetype chooser controlgameObjectFactory- the game object factory for creating game objectsgameObjectMatchers- the game object matchers to usetopmostInsertionMode- the "topmost" insertion mode
-
-
Method Detail
-
mapClosed
public void mapClosed()
Description copied from interface:MapModelThis function must be called if this instance is freed.
-
getMapArchObject
@NotNull public A getMapArchObject()
Description copied from interface:MapModelReturns the Map Arch Object with the meta information about the map.- Specified by:
getMapArchObjectin interfaceMapModel<G extends GameObject<G,A,R>,A extends MapArchObject<A>,R extends Archetype<G,A,R>>- Returns:
- the map arch object with the meta information about the map
-
iterator
public java.util.Iterator<MapSquare<G,A,R>> iterator()
- Specified by:
iteratorin interfacejava.lang.Iterable<G extends GameObject<G,A,R>>
-
getMapSquare
@NotNull public @NotNull MapSquare<G,A,R> getMapSquare(@NotNull @NotNull java.awt.Point pos)
Description copied from interface:MapModelGet the square at a specified location.- Specified by:
getMapSquarein interfaceMapModel<G extends GameObject<G,A,R>,A extends MapArchObject<A>,R extends Archetype<G,A,R>>- Parameters:
pos- location to get square at- Returns:
- square at
p
-
addObjectListToMap
public void addObjectListToMap(@NotNull @NotNull java.lang.Iterable<G> objects)Description copied from interface:MapModelAdds a list ofGameObjectsto this map.- Specified by:
addObjectListToMapin interfaceMapModel<G extends GameObject<G,A,R>,A extends MapArchObject<A>,R extends Archetype<G,A,R>>- Parameters:
objects- the game objects to add
-
clearMap
public void clearMap()
Description copied from interface:MapModelClears this map completely. After invoking this method, no objects will remain on this map.
-
isEmpty
public boolean isEmpty()
Description copied from interface:MapModelReturns whether the map is empty.
-
addMapModelListener
public void addMapModelListener(@NotNull @NotNull MapModelListener<G,A,R> listener)Description copied from interface:MapModelRegister a map listener.- Specified by:
addMapModelListenerin interfaceMapModel<G extends GameObject<G,A,R>,A extends MapArchObject<A>,R extends Archetype<G,A,R>>- Parameters:
listener- the listener to register
-
prependMapModelListener
public void prependMapModelListener(@NotNull @NotNull MapModelListener<G,A,R> listener)Description copied from interface:MapModelRegister a map listener and call it before all other listeners.- Specified by:
prependMapModelListenerin interfaceMapModel<G extends GameObject<G,A,R>,A extends MapArchObject<A>,R extends Archetype<G,A,R>>- Parameters:
listener- the listener to register
-
removeMapModelListener
public void removeMapModelListener(@NotNull @NotNull MapModelListener<G,A,R> listener)Description copied from interface:MapModelUnregister a map listener.- Specified by:
removeMapModelListenerin interfaceMapModel<G extends GameObject<G,A,R>,A extends MapArchObject<A>,R extends Archetype<G,A,R>>- Parameters:
listener- the listener to unregister
-
addMapTransactionListener
public void addMapTransactionListener(@NotNull @NotNull MapTransactionListener<G,A,R> listener)Description copied from interface:MapModelRegisters a map transaction listener.- Specified by:
addMapTransactionListenerin interfaceMapModel<G extends GameObject<G,A,R>,A extends MapArchObject<A>,R extends Archetype<G,A,R>>- Parameters:
listener- the the listener to register
-
removeMapTransactionListener
public void removeMapTransactionListener(@NotNull @NotNull MapTransactionListener<G,A,R> listener)Description copied from interface:MapModelUnregisters a map transaction listener.- Specified by:
removeMapTransactionListenerin interfaceMapModel<G extends GameObject<G,A,R>,A extends MapArchObject<A>,R extends Archetype<G,A,R>>- Parameters:
listener- the listener to unregister
-
beginSquareChange
public void beginSquareChange(@NotNull @NotNull MapSquare<G,A,R> mapSquare)Description copied from interface:MapModelMethod to notify the model that a map square is about to change.- Specified by:
beginSquareChangein interfaceMapModel<G extends GameObject<G,A,R>,A extends MapArchObject<A>,R extends Archetype<G,A,R>>- Parameters:
mapSquare- the map square that is about to change
-
endSquareChange
public void endSquareChange(@NotNull @NotNull MapSquare<G,A,R> mapSquare)Description copied from interface:MapModelMethod to notify the model that a map square was changed. A change to a square is atomic ifMapModel.getTransactionDepth()returns 0, otherwise it is transactional. The model then notifies the registered listeners of the changes.- Specified by:
endSquareChangein interfaceMapModel<G extends GameObject<G,A,R>,A extends MapArchObject<A>,R extends Archetype<G,A,R>>- Parameters:
mapSquare- the map square that has changed
-
beginGameObjectChange
public void beginGameObjectChange(@NotNull G gameObject)Description copied from interface:MapModelMethod to notify the model that a game object is about to change.- Specified by:
beginGameObjectChangein interfaceMapModel<G extends GameObject<G,A,R>,A extends MapArchObject<A>,R extends Archetype<G,A,R>>- Parameters:
gameObject- the game object that is about to change
-
endGameObjectChange
public void endGameObjectChange(@NotNull G gameObject)Description copied from interface:MapModelMethod to notify the model that a game object was changed. A change to a game object is atomic ifMapModel.getTransactionDepth()returns 0, otherwise it is transactional. The model then notifies the registered listeners of the changes.- Specified by:
endGameObjectChangein interfaceMapModel<G extends GameObject<G,A,R>,A extends MapArchObject<A>,R extends Archetype<G,A,R>>- Parameters:
gameObject- the game object that has changed
-
transientGameObjectChange
public void transientGameObjectChange(@NotNull G gameObject)Description copied from interface:MapModelMethod to notify the model that a game object was changed but need not be restored by undo/redo. A change to a game object is atomic ifMapModel.getTransactionDepth()returns 0, otherwise it is transactional. The model then notifies the registered listeners of the changes.- Specified by:
transientGameObjectChangein interfaceMapModel<G extends GameObject<G,A,R>,A extends MapArchObject<A>,R extends Archetype<G,A,R>>- Parameters:
gameObject- the game object that has changed
-
beginTransaction
public void beginTransaction(@NotNull @NotNull java.lang.String name)Description copied from interface:MapModelStarts a new transaction. Transactions may be nested. Transactions serve the purpose of firing events to the views when more changes are known to come before the view is really required to update. Each invocation of this function requires its own invocation ofMapModel.endTransaction().A transaction has a name. The name of the outermost transaction is used as a String presented to the user for undoing the operation enclosed by that transaction.
Beginning a nested transaction is a cheap operation.
- Specified by:
beginTransactionin interfaceMapModel<G extends GameObject<G,A,R>,A extends MapArchObject<A>,R extends Archetype<G,A,R>>- Parameters:
name- the name of the transaction- See Also:
MapModel.endTransaction(),MapModel.endTransaction(boolean),MapModel.endAllTransactions(),MapModel.getTransactionDepth(),MapModel.isAnyTransactionActive()
-
endTransaction
public void endTransaction()
Description copied from interface:MapModelEnd a transaction. Invoking this method will reduce the transaction depth by only 1.Ending a nested operation is a cheap operation. Ending a transaction without changes also is a cheap operation.
If the last transaction is ended, the changes are committed.
Same as
endTransaction(false).- Specified by:
endTransactionin interfaceMapModel<G extends GameObject<G,A,R>,A extends MapArchObject<A>,R extends Archetype<G,A,R>>- See Also:
MapModel.beginTransaction(String),MapModel.endTransaction(boolean),MapModel.endAllTransactions(),MapModel.getTransactionDepth(),MapModel.isAnyTransactionActive()
-
endTransaction
public void endTransaction(boolean fireEvent)
Description copied from interface:MapModelEnd a transaction. Invoking this method will reduce the transaction depth by only 1.Ending a nested operation is a cheap operation. Ending a transaction without changes also is a cheap operation.
If the last transaction is ended, the changes are committed.
An example where setting
fireEventtotrueis useful even though the outermost transaction is not ended is when during painting the UI should be updated though painting is not finished.- Specified by:
endTransactionin interfaceMapModel<G extends GameObject<G,A,R>,A extends MapArchObject<A>,R extends Archetype<G,A,R>>- Parameters:
fireEvent-trueif an event should be fired even in case this doesn't end the outermost transaction.- See Also:
MapModel.beginTransaction(String),MapModel.endTransaction(),MapModel.endAllTransactions(),MapModel.getTransactionDepth(),MapModel.isAnyTransactionActive()
-
endAllTransactions
public void endAllTransactions()
Description copied from interface:MapModelEnds all transaction. Invoking this method will reduce set the transaction depth to 0. You shouldn't invoke this method regularly. It is meant as a fallback in high level methods / exception handlers to prevent errors from causing unclosed transactions.- Specified by:
endAllTransactionsin interfaceMapModel<G extends GameObject<G,A,R>,A extends MapArchObject<A>,R extends Archetype<G,A,R>>- See Also:
MapModel.beginTransaction(String),MapModel.endTransaction(),MapModel.endTransaction(boolean),MapModel.getTransactionDepth(),MapModel.isAnyTransactionActive()
-
getTransactionDepth
public int getTransactionDepth()
Description copied from interface:MapModelGet the transaction depth, which is the number ofMapModel.beginTransaction(String)invocations without matchingMapModel.endTransaction()invocations. A transaction depth of 0 means there is no ongoing transaction.- Specified by:
getTransactionDepthin interfaceMapModel<G extends GameObject<G,A,R>,A extends MapArchObject<A>,R extends Archetype<G,A,R>>- Returns:
- transaction depth
- See Also:
MapModel.beginTransaction(String),MapModel.endTransaction(),MapModel.endTransaction(boolean),MapModel.endAllTransactions(),MapModel.isAnyTransactionActive()
-
isAnyTransactionActive
public boolean isAnyTransactionActive()
Description copied from interface:MapModelReturns whether a transaction is currently active. This method will returntrueif and only ifMapModel.getTransactionDepth()returns a value greater than zero.- Specified by:
isAnyTransactionActivein interfaceMapModel<G extends GameObject<G,A,R>,A extends MapArchObject<A>,R extends Archetype<G,A,R>>- Returns:
trueif there's a transaction going on, otherwisefalse.- See Also:
MapModel.beginTransaction(String),MapModel.endTransaction(),MapModel.endTransaction(boolean),MapModel.endAllTransactions(),MapModel.getTransactionDepth()
-
removeGameObject
public void removeGameObject(@NotNull G gameObject, boolean join)Description copied from interface:MapModelDelete an existingGameObjectfrom the map.- Specified by:
removeGameObjectin interfaceMapModel<G extends GameObject<G,A,R>,A extends MapArchObject<A>,R extends Archetype<G,A,R>>- Parameters:
gameObject- the game object to removejoin- if set, auto-joining is supported
-
isMultiArchFittingToMap
public boolean isMultiArchFittingToMap(@NotNull @NotNull Archetype<G,A,R> archetype, @NotNull @NotNull java.awt.Point pos, boolean allowDouble)Description copied from interface:MapModelChecks whether a GameObject (multi-arch) would still fit on this map.- Specified by:
isMultiArchFittingToMapin interfaceMapModel<G extends GameObject<G,A,R>,A extends MapArchObject<A>,R extends Archetype<G,A,R>>- Parameters:
archetype- the archetype to checkpos- position of multi-square headallowDouble- whether overlapping multi-square arches should be allowed (check is done using the archetype name)- Returns:
- whether the multi-arch would still fit on this map
-
setErrors
public void setErrors(@NotNull @NotNull ErrorCollector<G,A,R> errors)Description copied from interface:MapModelSets the errors in this map.
-
getErrors
@NotNull public @NotNull ErrorCollector<G,A,R> getErrors()
Description copied from interface:MapModelGets the errors in this map.
-
insertArchToMap
@Nullable public G insertArchToMap(@NotNull @NotNull BaseObject<G,A,R,?> templateBaseObject, @Nullable G nextGameObject, @NotNull @NotNull java.awt.Point pos, boolean join)
Description copied from interface:MapModelInsert a game object to the map at a specified position. This function allows either to choose from the archetypes or to insert a copy from an existing game object. It also works for container-inventory.- Specified by:
insertArchToMapin interfaceMapModel<G extends GameObject<G,A,R>,A extends MapArchObject<A>,R extends Archetype<G,A,R>>- Parameters:
templateBaseObject- a clone copy of this game object gets inserted to the map; it can be an archetype of a game objectnextGameObject- the new game object gets inserted before this; ifnull, the game object gets inserted at bottompos- the map position to insert the new game objectjoin- if set, auto-joining is supported- Returns:
- the inserted game object, or
nullif an error occurred - XXX:
- I'm too complex
-
insertBaseObject
@Nullable public G insertBaseObject(@NotNull @NotNull BaseObject<G,A,R,?> baseObject, @NotNull @NotNull java.awt.Point pos, boolean allowMany, boolean join, @NotNull @NotNull InsertionMode insertionMode)
Description copied from interface:MapModelInserts aBaseObjectto a map. Archetypes are instantiated, game objects are cloned. The direction of the inserted game object is set to the direction of the archetype chooser. This function allows multi-square game objects.- Specified by:
insertBaseObjectin interfaceMapModel<G extends GameObject<G,A,R>,A extends MapArchObject<A>,R extends Archetype<G,A,R>>- Parameters:
baseObject- the base objectpos- the insert-location on this mapallowMany- whether duplicates are allowedjoin- whether to auto-join the inserted game objectinsertionMode- the insertion mode to use- Returns:
- the inserted game object or
nullif nothing was inserted
-
addGameObjectToMap
public void addGameObjectToMap(@NotNull G gameObject, @NotNull @NotNull java.awt.Point pos, @NotNull @NotNull InsertionMode insertionMode)Description copied from interface:MapModelAdd a gameObject to the map. Including multi square objects. This function allows to insert any given GameObject. Make sure that the givengameObjectis a new and unlinked object.- Specified by:
addGameObjectToMapin interfaceMapModel<G extends GameObject<G,A,R>,A extends MapArchObject<A>,R extends Archetype<G,A,R>>- Parameters:
gameObject- the new GameObject with set destination coordinates to be linked onto the mappos- the insert locationinsertionMode- the insertion mode to use
-
moveEnv
public void moveEnv(@NotNull G gameObject, @NotNull @NotNull java.awt.Point pos, @NotNull G nextGameObject)Description copied from interface:MapModelMoves aGameObjectto its environment.
-
moveInv
public void moveInv(@NotNull G gameObject, @NotNull @NotNull GameObject<G,A,R> prevGameObject)Description copied from interface:MapModelMoves aGameObjectto the inventory of another game object.
-
isAreaEmpty
public boolean isAreaEmpty(int left, int top, int width, int height)Description copied from interface:MapModelChecks whether an area of a map is completely empty.- Specified by:
isAreaEmptyin interfaceMapModel<G extends GameObject<G,A,R>,A extends MapArchObject<A>,R extends Archetype<G,A,R>>- Parameters:
left- the left border of the areatop- the top border of the areawidth- the with of the areaheight- the height of the area- Returns:
- whether the area is completely empty
-
addActiveEditType
public void addActiveEditType(int editType)
Description copied from interface:MapModelAdd edit type to the bitmask of active types. If this is a new type, it gets calculated for every arch on the map. Once it is calculated, we save that state in 'activeEditType' so we don't need to do it again.- Specified by:
addActiveEditTypein interfaceMapModel<G extends GameObject<G,A,R>,A extends MapArchObject<A>,R extends Archetype<G,A,R>>- Parameters:
editType- new edit type
-
setMapFile
public void setMapFile(@Nullable @Nullable MapFile mapFile)Description copied from interface:MapModelSets the map file.- Specified by:
setMapFilein interfaceMapModel<G extends GameObject<G,A,R>,A extends MapArchObject<A>,R extends Archetype<G,A,R>>- Parameters:
mapFile- the map file ornullif the map has not yet been saved
-
getMapFile
@Nullable public @Nullable MapFile getMapFile()
Description copied from interface:MapModelReturns the map file.- Specified by:
getMapFilein interfaceMapModel<G extends GameObject<G,A,R>,A extends MapArchObject<A>,R extends Archetype<G,A,R>>- Returns:
- the map file or
nullif the map has not yet been saved
-
getMapFile
@NotNull public @NotNull MapFile getMapFile(@NotNull @NotNull MapPath mapPath) throws SameMapException, UnsavedMapException
Description copied from interface:MapModelReturns the map file for a map that can be reached by following aMapPathfrom this map.- Specified by:
getMapFilein interfaceMapModel<G extends GameObject<G,A,R>,A extends MapArchObject<A>,R extends Archetype<G,A,R>>- Parameters:
mapPath- the map path to follow- Returns:
- the map file
- Throws:
SameMapException- of the destination map file is the same as this map fileUnsavedMapException- if this map is not saved and therefore its map file is unknown
-
getAllGameObjects
@NotNull public @NotNull java.util.List<G> getAllGameObjects()
Description copied from interface:MapModelReturns all game objects. Only top-level head parts are returned; tail parts are ignored as are objects in inventories.- Specified by:
getAllGameObjectsin interfaceMapModel<G extends GameObject<G,A,R>,A extends MapArchObject<A>,R extends Archetype<G,A,R>>- Returns:
- all game objects
-
isModified
public boolean isModified()
Description copied from interface:MapModelReturn whether the map has been modified from the on-disk state.- Specified by:
isModifiedin interfaceMapModel<G extends GameObject<G,A,R>,A extends MapArchObject<A>,R extends Archetype<G,A,R>>- Returns:
trueif the map has been modified from the on-disk state
-
resetModified
public void resetModified()
Description copied from interface:MapModelResets the modified flag to false.- Specified by:
resetModifiedin interfaceMapModel<G extends GameObject<G,A,R>,A extends MapArchObject<A>,R extends Archetype<G,A,R>>
-
facesReloaded
public void facesReloaded()
Description copied from interface:MapModelWill be called whenever the archetype faces have been reloaded.- Specified by:
facesReloadedin interfaceMapModel<G extends GameObject<G,A,R>,A extends MapArchObject<A>,R extends Archetype<G,A,R>>
-
nextPoint
public void nextPoint(java.awt.Point point, int direction)Description copied from interface:MapModelMoves the given point forward or backward one map square.
-
-