Added support for StaticEntity labels

Added support for StaticEntity labels
Refactored entity labels/names into Entity class -- needs optimization
Fixed issue where changing Label text wouldn't resize Label itself
Fixed issue where left padding on Label background Drawable was ignored
This commit is contained in:
Collin Smith 2019-02-16 15:34:36 -08:00
parent 1d1cf585b5
commit 0676660f44
4 changed files with 60 additions and 27 deletions

View File

@ -9,8 +9,10 @@ import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.Align;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import java.util.Arrays;
@ -21,8 +23,10 @@ import gdx.diablo.codec.COFD2;
import gdx.diablo.codec.DCC;
import gdx.diablo.codec.util.BBox;
import gdx.diablo.graphics.PaletteIndexedBatch;
import gdx.diablo.graphics.PaletteIndexedColorDrawable;
import gdx.diablo.map.DS1;
import gdx.diablo.map.DT1.Tile;
import gdx.diablo.widget.Label;
public class Entity {
private static final String TAG = "Entity";
@ -119,6 +123,8 @@ public class Entity {
Animation animation;
public boolean over = true;
Label label;
String name;
public static Entity create(DS1 ds1, DS1.Object obj) {
final int type = obj.type;
@ -143,6 +149,16 @@ public class Entity {
weaponClass = "HTH";
layers = DEFAULT_LAYERS;
invalidate();
// TODO: LabelStyle should be made static
label = new Label(Diablo.fonts.font16);
label.getStyle().background = new PaletteIndexedColorDrawable(Diablo.colors.modal75) {{
final float padding = 2;
setLeftWidth(padding);
setTopHeight(padding);
setRightWidth(padding);
setBottomHeight(padding);
}};
}
public void setMode(String mode) {
@ -347,7 +363,13 @@ public class Entity {
if (over) drawLabel(batch);
}
public void drawLabel(PaletteIndexedBatch batch) {}
public void drawLabel(PaletteIndexedBatch batch) {
if (label.getText().length == 0) return;
float x = +(position.x * Tile.SUBTILE_WIDTH50) - (position.y * Tile.SUBTILE_WIDTH50);
float y = -(position.x * Tile.SUBTILE_HEIGHT50) - (position.y * Tile.SUBTILE_HEIGHT50);
label.setPosition(x, y + animation.getMinHeight() + label.getHeight(), Align.center);
label.draw(batch, 1);
}
public boolean move() {
int x = Direction.getOffX(angle);
@ -364,4 +386,14 @@ public class Entity {
return x <= coords.x && coords.x <= x + box.width
&& y <= coords.y && coords.y <= y + box.height;
}
public void setName(String name) {
if (!StringUtils.equalsIgnoreCase(this.name, name)) {
label.setText(this.name = name);
}
}
public String getName() {
return name;
}
}

View File

@ -13,10 +13,8 @@ import gdx.diablo.Diablo;
import gdx.diablo.codec.excel.MonStats;
import gdx.diablo.codec.excel.MonStats2;
import gdx.diablo.graphics.PaletteIndexedBatch;
import gdx.diablo.graphics.PaletteIndexedColorDrawable;
import gdx.diablo.map.DS1;
import gdx.diablo.map.DT1.Tile;
import gdx.diablo.widget.Label;
public class Monster extends Entity {
private static final String TAG = "Monster";
@ -25,13 +23,12 @@ public class Monster extends Entity {
MonStats.Entry monstats;
MonStats2.Entry monstats2;
Label label;
public Monster(DS1.Object object, MonStats.Entry monstats) {
super(monstats.Code, EntType.MONSTER);
this.object = object;
this.monstats = monstats;
this.monstats2 = Diablo.files.monstats2.get(monstats.MonStatsEx);
setName(monstats.NameStr);
setWeaponClass(monstats2.BaseW);
setMode(monstats.spawnmode.isEmpty() ? "NU" : monstats.spawnmode);
for (int i = 0; i < monstats2.ComponentV.length; i++) {
@ -42,15 +39,6 @@ public class Monster extends Entity {
setArmType(Component.valueOf(i), v[random]);
}
}
label = new Label(monstats.NameStr, Diablo.fonts.font16);
label.getStyle().background = new PaletteIndexedColorDrawable(Diablo.colors.modal75) {{
final float padding = 3;
setLeftWidth(padding);
setTopHeight(padding);
setRightWidth(padding);
setBottomHeight(padding);
}};
}
public static Monster create(DS1 ds1, DS1.Object obj) {
@ -109,16 +97,8 @@ public class Monster extends Entity {
public void drawLabel(PaletteIndexedBatch batch) {
float x = +(position.x * Tile.SUBTILE_WIDTH50) - (position.y * Tile.SUBTILE_WIDTH50);
float y = -(position.x * Tile.SUBTILE_HEIGHT50) - (position.y * Tile.SUBTILE_HEIGHT50);
label.setPosition(x, y + monstats2.pixHeight/*animation.getMinHeight() + label.getHeight()*/, Align.center);
label.setPosition(x, y + monstats2.pixHeight + label.getHeight(), Align.center);
label.draw(batch, 1);
//BitmapFont font = Diablo.fonts.font16;
//GlyphLayout glyphs = new GlyphLayout(font, "Test", font.getColor(), 0, Align.center, false);
//batch.setBlendMode(BlendMode.SOLID, Diablo.colors.modal50);
//batch.draw(Diablo.textures.white, x - glyphs.width / 2, y + animation.getMinHeight(), glyphs.width, glyphs.height);
//batch.resetBlendMode();
//font.draw(batch, glyphs, x, y + animation.getMinHeight() + font.getLineHeight());
}
@Override

View File

@ -1,10 +1,14 @@
package gdx.diablo.entity;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.Align;
import gdx.diablo.Diablo;
import gdx.diablo.codec.excel.Objects;
import gdx.diablo.graphics.PaletteIndexedBatch;
import gdx.diablo.map.DS1;
import gdx.diablo.map.DT1.Tile;
public class StaticEntity extends Entity {
private static final String TAG = "StaticEntity";
@ -16,6 +20,7 @@ public class StaticEntity extends Entity {
super(base.Token, EntType.OBJECT);
this.object = object;
this.base = base;
setName(base.Name);
init();
}
@ -39,8 +44,19 @@ public class StaticEntity extends Entity {
animation.setFrameDelta(base.FrameDelta[mode]); // FIXME: anim framedelta looks too quick
}
public String getName() {
return base == null ? toString() : base.Name + "(" + base.Id + ")";
@Override
public void drawLabel(PaletteIndexedBatch batch) {
float x = +(position.x * Tile.SUBTILE_WIDTH50) - (position.y * Tile.SUBTILE_WIDTH50);
float y = -(position.x * Tile.SUBTILE_HEIGHT50) - (position.y * Tile.SUBTILE_HEIGHT50);
label.setPosition(x, y - base.NameOffset + label.getHeight(), Align.center);
label.draw(batch, 1);
}
@Override
public boolean contains(Vector3 coords) {
int mode = Diablo.files.ObjMode.index(this.mode);
if (!base.Selectable[mode]) return false;
return super.contains(coords);
}
private void init() {

View File

@ -45,9 +45,8 @@ public class Label extends com.badlogic.gdx.scenes.scene2d.ui.Label {
if (style != null) {
Drawable background = style.background;
if (background != null) {
// TODO: background.getLeftWidth() looks like it shifts background.getMinWidth() -- bug?
background.draw(batch,
getX() /*- background.getLeftWidth()*/, getY() - background.getBottomHeight(),
getX() - background.getLeftWidth(), getY() - background.getBottomHeight(),
getWidth() + background.getMinWidth(), getHeight() + background.getMinHeight());
}
}
@ -65,4 +64,10 @@ public class Label extends com.badlogic.gdx.scenes.scene2d.ui.Label {
setText(id == -1 ? "" : Diablo.string.lookup(id));
return true;
}
@Override
public void setText(CharSequence newText) {
super.setText(newText);
setSize(getPrefWidth(), getPrefHeight());
}
}