TiledMapEditor supports the placement of text-labels nowdays, but I'm not able to load the text in a String.
I've tried iterating through the MapProperties
of a MapObject
, but the text is not part of them.
for(MapObject object : map.getLayers().get("id of textlabels").getObjects())
{
MapProperties props = object.getProperties();
Iterator<String> valueIta = props.getValues();
while (valueIta.hasNext()){
String currentValue = String.valueOf(valueIta.next());
sout(currentValue); //prints id,x,y,width,height
}
}
I also looked at the usages of the MapObject interface if there is anything like a ''TextLabelMapObject'' (like RectangularMapObject
), but found none.
Looking in my .tmx file I can find this description of my object, but I have no idea how to parse that text attribute.
<object id="1594" x="1512.5" y="7170.5" width="83" height="19">
<text wrap="1">Hello World</text>
</object>
My idea was to iterate through the layer's objects, parsing the current object to a TextLabelMapObject (which not exists) that implements a method getText() that returns the String inside <text>
.
I'm a missing something here?
Any hint is greatly appreciated.
EDIT:
So, as @bornander suggested I've created the following classes:
public class CustomMapLoader extends CustomBaseTmxMapLoader<TmxMapLoader.Parameters> {
https://github.com/libgdx/libgdx/blob/master/gdx/src/com/badlogic/gdx/maps/tiled/TmxMapLoader.java
public abstract class CustomBaseTmxMapLoader<P extends AssetLoaderParameters<TiledMap>> extends AsynchronousAssetLoader<TiledMap, P> {
//added to loadObject(TiledMap map, MapLayer layer, XmlReader.Element element)
...
} else if ((child = element.getChildByName("ellipse")) != null) {
object = new EllipseMapObject(x, flipY ? y - height : y, width, height);
} else if ((child = element.getChildByName("text")) != null) {
object = new TextLabelMapObject(child.getText(), x, flipY ? y - height : y, width, height); //this line will create a text object with position data
}
so that I can load my map like this:
gameWorld = new CustomMapLoader().load(Constants.MAP);
Ans here is the TextLabelMapObject
import com.badlogic.gdx.maps.MapObject;
import com.badlogic.gdx.math.Rectangle;
public class TextLabelMapObject extends MapObject {
private Rectangle rectangle;
private String text;
/** @return rectangle shape */
public Rectangle getRectangle () {
return rectangle;
}
/** @return text as string */
public String getText () {
return text;
}
/** Creates a rectangle object which lower left corner is at (0, 0) with width=1 and height=1 */
public TextLabelMapObject () {
this("", 0.0f, 0.0f, 1.0f, 1.0f);
}
/** Creates a {@link Rectangle} object with the given X and Y coordinates along with a given width and height.
*
* @param x X coordinate
* @param y Y coordinate
* @param width Width of the {@link Rectangle} to be created.
* @param height Height of the {@link Rectangle} to be created. */
public TextLabelMapObject (String text, float x, float y, float width, float height) {
super();
rectangle = new Rectangle(x, y, width, height);
this.text = text;
}
}
In my B2DWorldCreator I do
for (MapObject object : GameEngine.mapLoader.gameWorld.getLayers().get(Constants.LAYER_TEXTLABELS).getObjects()) {
Rectangle rect = ((TextLabelMapObject) object).getRectangle();
String text = ((TextLabelMapObject) object).getText();
infoList.add(new InfoSign(main, text, rect.getX()/ Constants.PPM, rect.getY() / Constants.PPM));
}
with the InfoSign (thats later going to be a Sprite
) being
public class InfoSign {
private String text;
private float x;
private float y;
public InfoSign(String text, float x, float y){
this.main = main;
this.text = text;
this.x = x;
this.y = y;
}
public void draw(Batch batch){
main.assets.infoFont.draw(batch, text , x,y);
}
}
Thanks a bunch!