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 class
SparseTextMap.CharValues
static class
SparseTextMap.Entries
static class
SparseTextMap.Entry
static class
SparseTextMap.FloatValues
static class
SparseTextMap.Keys
-
Field Summary
Fields Modifier and Type Field Description int
size
-
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.CharValues
charValues()
Returns an iterator for the values in the map.void
clear()
void
clear(int maximumCapacity)
Clears the map and reduces the size of the backing arrays to be the specified capacity if they are larger.boolean
containsCharValue(char value)
Returns true if the specified char value is in the map.boolean
containsFloatValue(float value)
Returns true if the specified char value is in the map.boolean
containsKey(int key)
static int
decodeX(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 int
decodeY(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)
.void
draw(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.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.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.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.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.static int
encodePosition(int x, int y)
Packs the given x,y position into one int in the type that is used elsewhere in this class.void
ensureCapacity(int additionalCapacity)
Increases the size of the backing array to accommodate the specified number of additional items.SparseTextMap.Entries
entries()
Returns an iterator for the entries in the map.boolean
equals(Object obj)
int
findKey(char value, int notFound)
Returns the key for the specified char value, or notFound if it is not in the map.int
findKey(float value, int notFound)
Returns the key for the specified float value, or notFound if it is not in the map.SparseTextMap.FloatValues
floatValues()
Returns an iterator for the values in the map.char
getChar(int key, char defaultValue)
char
getChar(int x, int y, char defaultValue)
float
getFloat(int key, float defaultValue)
float
getFloat(int x, int y, float defaultValue)
int
hashCode()
Iterator<SparseTextMap.Entry>
iterator()
SparseTextMap.Keys
keys()
Returns an iterator for the keys in the map.int
place(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)
.int
place(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.void
put(int key, char charValue, float floatValue)
void
putAll(SparseTextMap map)
void
remove(int key)
char
remove(int key, char defaultValue)
void
shrink(int maximumCapacity)
Reduces the size of the backing arrays to be the specified capacity or less.String
toString()
void
updateChar(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.void
updateFloat(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 aFrustum
object, 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.actualCellWidth
andTextCellFactory.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
- theFilterBatch
or 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 aFrustum
object, 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.actualCellWidth
andTextCellFactory.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
- theFilterBatch
or 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 aFrustum
object, 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.actualCellWidth
andTextCellFactory.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
- theFilterBatch
or 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
- aFrustum
object 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 aFrustum
object, 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.actualCellWidth
andTextCellFactory.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
- theFilterBatch
or 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
- aFrustum
object to determine culling, almost always obtained fromCamera.frustum
screenOffsetX
- 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 aFrustum
object, 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.actualCellWidth
andTextCellFactory.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
- theFilterBatch
or 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
- aFrustum
object to determine culling, almost always obtained fromCamera.frustum
screenOffsetX
- 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:
iterator
in 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.Entries
constructor 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.Entries
constructor 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.Entries
constructor 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.Entries
constructor for nested or multithreaded iteration.
-