From b418afed630d392c35811e07b0dd0659570d7371 Mon Sep 17 00:00:00 2001 From: Anuken Date: Tue, 2 Nov 2021 10:03:44 -0400 Subject: [PATCH] Added WorldLabel entity for better server-side labels --- .../src/main/resources/classids.properties | 1 + .../resources/revisions/WorldLabelComp/0.json | 1 + core/src/mindustry/entities/GroupDefs.java | 1 + .../entities/comp/WorldLabelComp.java | 68 +++++++++++++++++++ core/src/mindustry/ui/Menus.java | 13 +++- 5 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 annotations/src/main/resources/revisions/WorldLabelComp/0.json create mode 100644 core/src/mindustry/entities/comp/WorldLabelComp.java diff --git a/annotations/src/main/resources/classids.properties b/annotations/src/main/resources/classids.properties index 2dc5b73284..d37431c7b0 100644 --- a/annotations/src/main/resources/classids.properties +++ b/annotations/src/main/resources/classids.properties @@ -20,6 +20,7 @@ mindustry.entities.comp.PlayerComp=12 mindustry.entities.comp.PosTeam=27 mindustry.entities.comp.PosTeamDef=28 mindustry.entities.comp.PuddleComp=13 +mindustry.entities.comp.WorldLabelComp=35 mindustry.type.Weather.WeatherStateComp=14 mindustry.world.blocks.campaign.LaunchPad.LaunchPayloadComp=15 mindustry.world.blocks.campaign.PayloadLaunchPad.LargeLaunchPayloadComp=34 diff --git a/annotations/src/main/resources/revisions/WorldLabelComp/0.json b/annotations/src/main/resources/revisions/WorldLabelComp/0.json new file mode 100644 index 0000000000..e3d056549b --- /dev/null +++ b/annotations/src/main/resources/revisions/WorldLabelComp/0.json @@ -0,0 +1 @@ +{fields:[{name:flags,type:byte},{name:fontSize,type:float},{name:text,type:java.lang.String},{name:x,type:float},{name:y,type:float},{name:z,type:float}]} \ No newline at end of file diff --git a/core/src/mindustry/entities/GroupDefs.java b/core/src/mindustry/entities/GroupDefs.java index 8f98a69442..b995d4c415 100644 --- a/core/src/mindustry/entities/GroupDefs.java +++ b/core/src/mindustry/entities/GroupDefs.java @@ -14,4 +14,5 @@ class GroupDefs{ @GroupDef(value = Firec.class) G fire; @GroupDef(value = Puddlec.class) G puddle; @GroupDef(value = WeatherStatec.class) G weather; + @GroupDef(value = WorldLabelc.class, mapping = true) G label; } diff --git a/core/src/mindustry/entities/comp/WorldLabelComp.java b/core/src/mindustry/entities/comp/WorldLabelComp.java new file mode 100644 index 0000000000..d33d95f1e0 --- /dev/null +++ b/core/src/mindustry/entities/comp/WorldLabelComp.java @@ -0,0 +1,68 @@ +package mindustry.entities.comp; + +import arc.graphics.*; +import arc.graphics.g2d.*; +import arc.scene.ui.layout.*; +import arc.util.*; +import arc.util.pooling.*; +import mindustry.annotations.Annotations.*; +import mindustry.gen.*; +import mindustry.graphics.*; +import mindustry.ui.*; + +/** Component/entity for labels in world space. Useful for servers. Does not save in files - create only on world load. */ +@EntityDef(value = {WorldLabelc.class}, serialize = false) +@Component(base = true) +public abstract class WorldLabelComp implements Posc, Drawc, Syncc{ + @Import int id; + @Import float x, y; + + public static final byte flagBackground = 1, flagOutline = 2; + + public String text = "sample text"; + public float fontSize = 1f, z = Layer.playerName + 1; + /** Flags are packed into a byte for sync efficiency; see the flag static values. */ + public byte flags = flagBackground | flagOutline; + + @Replace + public float clipSize(){ + return text.length() * 10f * fontSize; + } + + @Override + public void draw(){ + Draw.z(z); + float z = Drawf.text(); + + Font font = (flags & flagOutline) != 0 ? Fonts.outline : Fonts.def; + GlyphLayout layout = Pools.obtain(GlyphLayout.class, GlyphLayout::new); + + boolean ints = font.usesIntegerPositions(); + font.setUseIntegerPositions(false); + font.getData().setScale(0.25f / Scl.scl(1f) * fontSize); + layout.setText(font, text); + + if((flags & flagBackground) != 0){ + Draw.color(0f, 0f, 0f, 0.3f); + Fill.rect(x, y - layout.height / 2, layout.width + 2, layout.height + 3); + Draw.color(); + } + + font.setColor(Color.white); + font.draw(text, x, y, 0, Align.center, false); + + Draw.reset(); + Pools.free(layout); + font.getData().setScale(1f); + font.setColor(Color.white); + font.setUseIntegerPositions(ints); + + Draw.z(z); + } + + /** This MUST be called instead of remove()! */ + public void hide(){ + remove(); + Call.removeWorldLabel(id); + } +} diff --git a/core/src/mindustry/ui/Menus.java b/core/src/mindustry/ui/Menus.java index 2503e60535..7d4d601a8d 100644 --- a/core/src/mindustry/ui/Menus.java +++ b/core/src/mindustry/ui/Menus.java @@ -94,9 +94,7 @@ public class Menus{ @Remote(variants = Variant.both) public static void labelReliable(String message, float duration, float worldx, float worldy){ - if(message == null) return; - - ui.showLabel(message, duration, worldx, worldy); + label(message, duration, worldx, worldy); } @Remote(variants = Variant.both) @@ -113,6 +111,15 @@ public class Menus{ ui.hudfrag.showToast(Fonts.getGlyph(Fonts.icon, (char)unicode), text); } + //internal use only + @Remote + public static void removeWorldLabel(int id){ + var label = Groups.label.getByID(id); + if(label != null){ + label.remove(); + } + } + public interface MenuListener{ void get(Player player, int option); }