From 0676660f44406972161e49633a2e21179fea2e46 Mon Sep 17 00:00:00 2001 From: Collin Smith Date: Sat, 16 Feb 2019 15:34:36 -0800 Subject: [PATCH] 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 --- core/src/gdx/diablo/entity/Entity.java | 34 +++++++++++++++++++- core/src/gdx/diablo/entity/Monster.java | 24 ++------------ core/src/gdx/diablo/entity/StaticEntity.java | 20 ++++++++++-- core/src/gdx/diablo/widget/Label.java | 9 ++++-- 4 files changed, 60 insertions(+), 27 deletions(-) diff --git a/core/src/gdx/diablo/entity/Entity.java b/core/src/gdx/diablo/entity/Entity.java index 209fd606..1052143a 100644 --- a/core/src/gdx/diablo/entity/Entity.java +++ b/core/src/gdx/diablo/entity/Entity.java @@ -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; + } } diff --git a/core/src/gdx/diablo/entity/Monster.java b/core/src/gdx/diablo/entity/Monster.java index 59ccadd7..5349bff7 100644 --- a/core/src/gdx/diablo/entity/Monster.java +++ b/core/src/gdx/diablo/entity/Monster.java @@ -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 diff --git a/core/src/gdx/diablo/entity/StaticEntity.java b/core/src/gdx/diablo/entity/StaticEntity.java index 27418fd4..2bb07141 100644 --- a/core/src/gdx/diablo/entity/StaticEntity.java +++ b/core/src/gdx/diablo/entity/StaticEntity.java @@ -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() { diff --git a/core/src/gdx/diablo/widget/Label.java b/core/src/gdx/diablo/widget/Label.java index eeb5da22..51f24445 100644 --- a/core/src/gdx/diablo/widget/Label.java +++ b/core/src/gdx/diablo/widget/Label.java @@ -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()); + } }