Package squidpony.squidgrid.gui.gdx
Class SparseTextMap
java.lang.Object
squidpony.squidgrid.gui.gdx.SparseTextMap
- All Implemented Interfaces:
Iterable<SparseTextMap.Entry>
public class SparseTextMap extends Object implements Iterable<SparseTextMap.Entry>
An unordered map where the keys are two positive ints up to 16 bits (x and y, between 0 and 65535) and there are
multiple kinds of value per key, here just a char and a float for color. Can be rendered if given a running Batch
and a TextCellFactory using
draw(Batch, TextCellFactory, Frustum).
- Author:
- Nathan Sweet, Tommy Ettinger
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classSparseTextMap.CharValuesstatic classSparseTextMap.Entriesstatic classSparseTextMap.Entrystatic classSparseTextMap.FloatValuesstatic classSparseTextMap.Keys -
Field Summary
Fields Modifier and Type Field Description intsize -
Constructor Summary
Constructors Constructor Description SparseTextMap()Creates a new map with an initial capacity of 192 and a load factor of 0.75.SparseTextMap(int initialCapacity)Creates a new map with a load factor of 0.75.SparseTextMap(int initialCapacity, float loadFactor)Creates a new map with the specified initial capacity and load factor.SparseTextMap(SparseTextMap map)Creates a new map identical to the specified map. -
Method Summary
Modifier and Type Method Description SparseTextMap.CharValuescharValues()Returns an iterator for the values in the map.voidclear()voidclear(int maximumCapacity)Clears the map and reduces the size of the backing arrays to be the specified capacity if they are larger.booleancontainsCharValue(char value)Returns true if the specified char value is in the map.booleancontainsFloatValue(float value)Returns true if the specified char value is in the map.booleancontainsKey(int key)static intdecodeX(int encoded)Decodes a packed position and gets the x component from it, if it was produced byencodePosition(int, int)orplace(int, int, char, float).static intdecodeY(int encoded)Decodes a packed position and gets the y component from it, if it was produced byencodePosition(int, int)orplace(int, int, char, float).voiddraw(com.badlogic.gdx.graphics.g2d.Batch batch, TextCellFactory textFactory)Draws the contents of this SparseTextMap, using the keys as x,y pairs as they would be entered by callingplace(int, int, char, float)and drawing the associated char at that x,y position.voiddraw(com.badlogic.gdx.graphics.g2d.Batch batch, TextCellFactory textFactory, float screenOffsetX, float screenOffsetY)Draws the contents of this SparseTextMap, using the keys as x,y pairs as they would be entered by callingplace(int, int, char, float)and drawing the associated char at that x,y position, potentially with an offset on x and/or y.voiddraw(com.badlogic.gdx.graphics.g2d.Batch batch, TextCellFactory textFactory, com.badlogic.gdx.math.Frustum frustum)Draws the contents of this SparseTextMap, using the keys as x,y pairs as they would be entered by callingplace(int, int, char, float)and drawing the associated char at that x,y position.voiddraw(com.badlogic.gdx.graphics.g2d.Batch batch, TextCellFactory textFactory, com.badlogic.gdx.math.Frustum frustum, float screenOffsetX, float screenOffsetY)Draws the contents of this SparseTextMap, using the keys as x,y pairs as they would be entered by callingplace(int, int, char, float)and drawing the associated char at that x,y position, potentially with an offset on x and/or y.voiddraw(com.badlogic.gdx.graphics.g2d.Batch batch, TextCellFactory textFactory, com.badlogic.gdx.math.Frustum frustum, float screenOffsetX, float screenOffsetY, char replacement)Draws the contents of this SparseTextMap, using the keys as x,y pairs as they would be entered by callingplace(int, int, char, float)and drawing the char replacement at that x,y position, potentially with an offset on x and/or y.static intencodePosition(int x, int y)Packs the given x,y position into one int in the type that is used elsewhere in this class.voidensureCapacity(int additionalCapacity)Increases the size of the backing array to accommodate the specified number of additional items.SparseTextMap.Entriesentries()Returns an iterator for the entries in the map.booleanequals(Object obj)intfindKey(char value, int notFound)Returns the key for the specified char value, or notFound if it is not in the map.intfindKey(float value, int notFound)Returns the key for the specified float value, or notFound if it is not in the map.SparseTextMap.FloatValuesfloatValues()Returns an iterator for the values in the map.chargetChar(int key, char defaultValue)chargetChar(int x, int y, char defaultValue)floatgetFloat(int key, float defaultValue)floatgetFloat(int x, int y, float defaultValue)inthashCode()Iterator<SparseTextMap.Entry>iterator()SparseTextMap.Keyskeys()Returns an iterator for the keys in the map.intplace(int x, int y, char charValue, float encodedColor)Places a char in the given encoded color at the requested x, y position in grid cells, where x and y must each be between 0 and 65535, both inclusive, and the encoded color is one produced byColor.toFloatBits()or any of several methods inSColor, such asSColor.floatGetI(int, int, int),SColor.floatGetHSV(float, float, float, float), orSColor.lerpFloatColors(float, float, float).intplace(int x, int y, char charValue, com.badlogic.gdx.graphics.Color color)Places a char in the given libGDX Color at the requested x, y position in grid cells, where x and y must each be between 0 and 65535, both inclusive.voidput(int key, char charValue, float floatValue)voidputAll(SparseTextMap map)voidremove(int key)charremove(int key, char defaultValue)voidshrink(int maximumCapacity)Reduces the size of the backing arrays to be the specified capacity or less.StringtoString()voidupdateChar(int key, char charValue)If and only if key is already present, this changes the char associated with it while leaving the float the same.voidupdateFloat(int key, float floatValue)If and only if key is already present, this changes the float associated with it while leaving the char the same.
-
Field Details
-
Constructor Details
-
SparseTextMap
public SparseTextMap()Creates a new map with an initial capacity of 192 and a load factor of 0.75. -
SparseTextMap
Creates a new map with a load factor of 0.75.- Parameters:
initialCapacity- If not a power of two, it is increased to the next nearest power of two.
-
SparseTextMap
Creates a new map with the specified initial capacity and load factor. This map will hold initialCapacity items before growing the backing table.- Parameters:
initialCapacity- If not a power of two, it is increased to the next nearest power of two.
-
SparseTextMap
Creates a new map identical to the specified map.
-
-
Method Details
-
draw
Draws the contents of this SparseTextMap, using the keys as x,y pairs as they would be entered by callingplace(int, int, char, float)and drawing the associated char at that x,y position. Uses aFrustumobject, usually obtained from the current Camera withCamera.frustum, to cull anything that would be drawn out of view. Treats the float value as a color for the char, using the encoding for colors as floats thatColor.toFloatBits()uses. Relies on the sizing information from the given TextCellFactory (itsTextCellFactory.actualCellWidthandTextCellFactory.actualCellHeight, which may differ from its width and height if either ofTextCellFactory.tweakWidth(float)orTextCellFactory.tweakHeight(float)were called). It also, of course, uses the TextCellFactory to determine what its text will look like (font, size, and so on). The TextCellFactory must have been initialized, probably withTextCellFactory.initBySize()after setting the width and height as desired. This method should be called betweenbatch.begin()andbatch.end()with the Batch passed to this.- Parameters:
batch- theFilterBatchor other Batch used to draw this; should have already have had begin() calledtextFactory- used to determine the font, size, cell size, and other information; must be initialized
-
draw
public void draw(com.badlogic.gdx.graphics.g2d.Batch batch, TextCellFactory textFactory, float screenOffsetX, float screenOffsetY)Draws the contents of this SparseTextMap, using the keys as x,y pairs as they would be entered by callingplace(int, int, char, float)and drawing the associated char at that x,y position, potentially with an offset on x and/or y. Uses aFrustumobject, usually obtained from the current Camera withCamera.frustum, to cull anything that would be drawn out of view. Treats the float value as a color for the char, using the encoding for colors as floats thatColor.toFloatBits()uses. Relies on the sizing information from the given TextCellFactory (itsTextCellFactory.actualCellWidthandTextCellFactory.actualCellHeight, which may differ from its width and height if either ofTextCellFactory.tweakWidth(float)orTextCellFactory.tweakHeight(float)were called). It also, of course, uses the TextCellFactory to determine what its text will look like (font, size, and so on). The TextCellFactory must have been initialized, probably withTextCellFactory.initBySize()after setting the width and height as desired. This method should be called betweenbatch.begin()andbatch.end()with the Batch passed to this.- Parameters:
batch- theFilterBatchor other Batch used to draw this; should have already have had begin() calledtextFactory- used to determine the font, size, cell size, and other information; must be initializedscreenOffsetX- offset to apply to the x position of each char rendered; positive moves chars rightscreenOffsetY- offset to apply to the y position of each char rendered; positive moves chars up
-
draw
public void draw(com.badlogic.gdx.graphics.g2d.Batch batch, TextCellFactory textFactory, com.badlogic.gdx.math.Frustum frustum)Draws the contents of this SparseTextMap, using the keys as x,y pairs as they would be entered by callingplace(int, int, char, float)and drawing the associated char at that x,y position. Uses aFrustumobject, usually obtained from the current Camera withCamera.frustum, to cull anything that would be drawn out of view. Treats the float value as a color for the char, using the encoding for colors as floats thatColor.toFloatBits()uses. Relies on the sizing information from the given TextCellFactory (itsTextCellFactory.actualCellWidthandTextCellFactory.actualCellHeight, which may differ from its width and height if either ofTextCellFactory.tweakWidth(float)orTextCellFactory.tweakHeight(float)were called). It also, of course, uses the TextCellFactory to determine what its text will look like (font, size, and so on). The TextCellFactory must have been initialized, probably withTextCellFactory.initBySize()after setting the width and height as desired. This method should be called betweenbatch.begin()andbatch.end()with the Batch passed to this.- Parameters:
batch- theFilterBatchor other Batch used to draw this; should have already have had begin() calledtextFactory- used to determine the font, size, cell size, and other information; must be initializedfrustum- aFrustumobject to determine culling, almost always obtained fromCamera.frustum
-
draw
public void draw(com.badlogic.gdx.graphics.g2d.Batch batch, TextCellFactory textFactory, com.badlogic.gdx.math.Frustum frustum, float screenOffsetX, float screenOffsetY)Draws the contents of this SparseTextMap, using the keys as x,y pairs as they would be entered by callingplace(int, int, char, float)and drawing the associated char at that x,y position, potentially with an offset on x and/or y. Uses aFrustumobject, usually obtained from the current Camera withCamera.frustum, to cull anything that would be drawn out of view. Treats the float value as a color for the char, using the encoding for colors as floats thatColor.toFloatBits()uses. Relies on the sizing information from the given TextCellFactory (itsTextCellFactory.actualCellWidthandTextCellFactory.actualCellHeight, which may differ from its width and height if either ofTextCellFactory.tweakWidth(float)orTextCellFactory.tweakHeight(float)were called). It also, of course, uses the TextCellFactory to determine what its text will look like (font, size, and so on). The TextCellFactory must have been initialized, probably withTextCellFactory.initBySize()after setting the width and height as desired. This method should be called betweenbatch.begin()andbatch.end()with the Batch passed to this.- Parameters:
batch- theFilterBatchor other Batch used to draw this; should have already have had begin() calledtextFactory- used to determine the font, size, cell size, and other information; must be initializedfrustum- aFrustumobject to determine culling, almost always obtained fromCamera.frustumscreenOffsetX- offset to apply to the x position of each char rendered; positive moves chars rightscreenOffsetY- offset to apply to the y position of each char rendered; positive moves chars up
-
draw
public void draw(com.badlogic.gdx.graphics.g2d.Batch batch, TextCellFactory textFactory, com.badlogic.gdx.math.Frustum frustum, float screenOffsetX, float screenOffsetY, char replacement)Draws the contents of this SparseTextMap, using the keys as x,y pairs as they would be entered by callingplace(int, int, char, float)and drawing the char replacement at that x,y position, potentially with an offset on x and/or y. Uses aFrustumobject, usually obtained from the current Camera withCamera.frustum, to cull anything that would be drawn out of view. Treats the float value as a color for replacement, using the encoding for colors as floats thatColor.toFloatBits()uses. Relies on the sizing information from the given TextCellFactory (itsTextCellFactory.actualCellWidthandTextCellFactory.actualCellHeight, which may differ from its width and height if either ofTextCellFactory.tweakWidth(float)orTextCellFactory.tweakHeight(float)were called). It also, of course, uses the TextCellFactory to determine what its text will look like (font, size, and so on). The TextCellFactory must have been initialized, probably withTextCellFactory.initBySize()after setting the width and height as desired. This method should be called betweenbatch.begin()andbatch.end()with the Batch passed to this.- Parameters:
batch- theFilterBatchor other Batch used to draw this; should have already have had begin() calledtextFactory- used to determine the font, size, cell size, and other information; must be initializedfrustum- aFrustumobject to determine culling, almost always obtained fromCamera.frustumscreenOffsetX- offset to apply to the x position of each char rendered; positive moves chars rightscreenOffsetY- offset to apply to the y position of each char rendered; positive moves chars upreplacement- a char that will be used in place of the normal char values stored in this; often the char with Unicode value 0, which renders as a solid block.
-
encodePosition
Packs the given x,y position into one int in the type that is used elsewhere in this class. This requires x and y to each be between 0 and 65535, both inclusive.- Parameters:
x- the x component to encode; this must be between 0 and 65535, both inclusivey- the y component to encode; this must be between 0 and 65535, both inclusive- Returns:
- an encoded form of the x,y pair in the style used elsewhere in this class
-
decodeX
Decodes a packed position and gets the x component from it, if it was produced byencodePosition(int, int)orplace(int, int, char, float).- Parameters:
encoded- a packed position that holds x and y components- Returns:
- the x component packed in the parameter
-
decodeY
Decodes a packed position and gets the y component from it, if it was produced byencodePosition(int, int)orplace(int, int, char, float).- Parameters:
encoded- a packed position that holds x and y components- Returns:
- the y component packed in the parameter
-
place
Places a char in the given libGDX Color at the requested x, y position in grid cells, where x and y must each be between 0 and 65535, both inclusive. The color can also be an SColor, such as one of the many constants in that class. Returns the int code that can be used to locate the x,y pair as a key in this SparseTextMap.- Parameters:
x- the x position of the colorful char; this must be between 0 and 65535, both inclusivey- the y position of the colorful char; this must be between 0 and 65535, both inclusivecharValue- the char to put into the SparseTextMapcolor- the libGDX Color or SColor to use for the char- Returns:
- the int that the x,y pair will be stored at, and used as the single key associated with the colorful char
-
place
Places a char in the given encoded color at the requested x, y position in grid cells, where x and y must each be between 0 and 65535, both inclusive, and the encoded color is one produced byColor.toFloatBits()or any of several methods inSColor, such asSColor.floatGetI(int, int, int),SColor.floatGetHSV(float, float, float, float), orSColor.lerpFloatColors(float, float, float). Returns the int code that can be used to locate the x,y pair as a key in this SparseTextMap.- Parameters:
x- the x position of the colorful char; this must be between 0 and 65535, both inclusivey- the y position of the colorful char; this must be between 0 and 65535, both inclusivecharValue- the char to put into the SparseTextMapencodedColor- the encoded color to use for the char, as produced byColor.toFloatBits()- Returns:
- the int that the x,y pair will be stored at, and used as the single key associated with the colorful char
-
put
-
updateFloat
If and only if key is already present, this changes the float associated with it while leaving the char the same.- Parameters:
key- the encoded key as produced byencodePosition(int, int)floatValue- the float value to set at the given encoded key
-
updateChar
If and only if key is already present, this changes the char associated with it while leaving the float the same.- Parameters:
key- the encoded key as produced byencodePosition(int, int)charValue- the char value to set at the given encoded key
-
putAll
-
getChar
- Parameters:
x- the x-component of the position to look upy- the y-component of the position to look updefaultValue- Returned if the key was not associated with a value.- Returns:
- the char associated with the given position, or defaultValue if no char is associated
-
getChar
- Parameters:
key- the encoded key as produced byencodePosition(int, int)defaultValue- Returned if the key was not associated with a value.- Returns:
- the char associated with the given key, or defaultValue if no char is associated
-
getFloat
- Parameters:
x- the x-component of the position to look upy- the y-component of the position to look updefaultValue- Returned if the key was not associated with a value.- Returns:
- the float associated with the given position, or defaultValue if no float is associated
-
getFloat
- Parameters:
key- the encoded key as produced byencodePosition(int, int)defaultValue- Returned if the key was not associated with a value.- Returns:
- the float associated with the given key, or defaultValue if no float is associated
-
remove
-
remove
-
shrink
Reduces the size of the backing arrays to be the specified capacity or less. If the capacity is already less, nothing is done. If the map contains more items than the specified capacity, the next highest power of two capacity is used instead. -
clear
Clears the map and reduces the size of the backing arrays to be the specified capacity if they are larger. -
clear
-
containsCharValue
Returns true if the specified char value is in the map. Note: this traverses the entire map and compares every value, which may be an expensive operation. -
containsFloatValue
Returns true if the specified char value is in the map. Note: this traverses the entire map and compares every value, which may be an expensive operation. -
containsKey
-
findKey
Returns the key for the specified char value, or notFound if it is not in the map. Note this traverses the entire map and compares every value, which may be an expensive operation. -
findKey
Returns the key for the specified float value, or notFound if it is not in the map. Note this traverses the entire map and compares every value, which may be an expensive operation. -
ensureCapacity
Increases the size of the backing array to accommodate the specified number of additional items. Useful before adding many items to avoid multiple backing array resizes. -
hashCode
-
equals
-
toString
-
iterator
- Specified by:
iteratorin interfaceIterable<SparseTextMap.Entry>
-
entries
Returns an iterator for the entries in the map. Remove is supported. Note that the same iterator instance is returned each time this method is called. Use theSparseTextMap.Entriesconstructor for nested or multithreaded iteration. -
charValues
Returns an iterator for the values in the map. Remove is supported. Note that the same iterator instance is returned each time this method is called. Use theSparseTextMap.Entriesconstructor for nested or multithreaded iteration. -
floatValues
Returns an iterator for the values in the map. Remove is supported. Note that the same iterator instance is returned each time this method is called. Use theSparseTextMap.Entriesconstructor for nested or multithreaded iteration. -
keys
Returns an iterator for the keys in the map. Remove is supported. Note that the same iterator instance is returned each time this method is called. Use theSparseTextMap.Entriesconstructor for nested or multithreaded iteration.
-