From f09e5afbe9fb3d9c7984625879b4dfcadc3b7ffd Mon Sep 17 00:00:00 2001 From: Anuken Date: Sat, 12 Feb 2022 18:50:15 -0500 Subject: [PATCH] More logic message types --- core/assets/bundles/bundle.properties | 2 +- core/src/mindustry/core/UI.java | 9 ++++++- core/src/mindustry/logic/LExecutor.java | 28 +++++++++++++++++++--- core/src/mindustry/logic/LStatements.java | 29 ++++++++++++++++++++--- core/src/mindustry/logic/MessageType.java | 9 +++++++ core/src/mindustry/ui/ItemImage.java | 4 ++-- 6 files changed, 71 insertions(+), 10 deletions(-) create mode 100644 core/src/mindustry/logic/MessageType.java diff --git a/core/assets/bundles/bundle.properties b/core/assets/bundles/bundle.properties index 06321bf5e5..74cc3ee417 100644 --- a/core/assets/bundles/bundle.properties +++ b/core/assets/bundles/bundle.properties @@ -1709,7 +1709,7 @@ lst.setblock = Set tile data at any location. lst.spawnunit = Spawn unit at a location. lst.packcolor = Pack [0, 1] RGBA components into a single number for drawing or rule-setting. lst.setrule = Set a game rule. -lst.flushnotification = Display a notification at the top of the screen from the text buffer.\nWill wait until the previous message finishes. +lst.flushmessage = Display a message on the screen from the text buffer.\nWill wait until the previous message finishes. logic.nounitbuild = [red]Unit building logic is not allowed here. diff --git a/core/src/mindustry/core/UI.java b/core/src/mindustry/core/UI.java index 4ef9d0883a..8340a6cff6 100644 --- a/core/src/mindustry/core/UI.java +++ b/core/src/mindustry/core/UI.java @@ -75,6 +75,8 @@ public class UI implements ApplicationListener, Loadable{ public Cursor drillCursor, unloadCursor; + private @Nullable Element lastAnnouncement; + public UI(){ Fonts.loadFonts(); } @@ -323,9 +325,9 @@ public class UI implements ApplicationListener, Loadable{ table.actions(Actions.delay(duration * 0.9f), Actions.fadeOut(duration * 0.1f, Interp.fade), Actions.remove()); table.top().table(Styles.black3, t -> t.margin(4).add(info).style(Styles.outlineLabel)).padTop(10); Core.scene.add(table); + lastAnnouncement = table; } - /** Shows a label at some position on the screen. Does not fade. */ public void showInfoPopup(String info, float duration, int align, int top, int left, int bottom, int right){ Table table = new Table(); @@ -516,6 +518,10 @@ public class UI implements ApplicationListener, Loadable{ dialog.show(); } + public boolean hasAnnouncement(){ + return lastAnnouncement != null && lastAnnouncement.parent != null; + } + /** Display text in the middle of the screen, then fade out. */ public void announce(String text){ announce(text, 3); @@ -531,6 +537,7 @@ public class UI implements ApplicationListener, Loadable{ t.pack(); t.act(0.1f); Core.scene.add(t); + lastAnnouncement = t; } public void showOkText(String title, String text, Runnable confirmed){ diff --git a/core/src/mindustry/logic/LExecutor.java b/core/src/mindustry/logic/LExecutor.java index 30d2a4c793..576625900a 100644 --- a/core/src/mindustry/logic/LExecutor.java +++ b/core/src/mindustry/logic/LExecutor.java @@ -1288,17 +1288,39 @@ public class LExecutor{ } } - public static class FlushNotificationI implements LInstruction{ + public static class FlushMessageI implements LInstruction{ + public MessageType type = MessageType.announce; + public int duration; + + public FlushMessageI(MessageType type, int duration){ + this.type = type; + this.duration = duration; + } + + public FlushMessageI(){ + } @Override public void run(LExecutor exec){ + if(headless) return; + //skip back to self until possible - if(ui.hudfrag.hasToast()){ + if( + type == MessageType.announce && ui.hudfrag.hasToast() || + type == MessageType.notify && ui.hasAnnouncement() || + type == MessageType.toast && ui.hasAnnouncement() + ){ exec.var(varCounter).numval --; return; } - ui.hudfrag.showToast(Icon.info, exec.textBuffer.toString()); + String text = exec.textBuffer.toString(); + switch(type){ + case notify -> ui.hudfrag.showToast(Icon.info, text); + case announce -> ui.announce(text, exec.numf(duration)); + case toast -> ui.showInfoToast(text, exec.numf(duration)); + } + exec.textBuffer.setLength(0); } } diff --git a/core/src/mindustry/logic/LStatements.java b/core/src/mindustry/logic/LStatements.java index c7dac168c5..53e6805d72 100644 --- a/core/src/mindustry/logic/LStatements.java +++ b/core/src/mindustry/logic/LStatements.java @@ -1272,11 +1272,34 @@ public class LStatements{ } } - @RegisterStatement("notification") - public static class FlushNotificationStatement extends LStatement{ + @RegisterStatement("message") + public static class FlushMessageStatement extends LStatement{ + public MessageType type = MessageType.announce; + public String duration = "3"; @Override public void build(Table table){ + rebuild(table); + } + + void rebuild(Table table){ + table.clearChildren(); + + table.button(b -> { + b.label(() -> type.name()).growX().wrap().labelAlign(Align.center); + b.clicked(() -> showSelect(b, MessageType.all, type, o -> { + type = o; + rebuild(table); + }, 2, c -> c.width(150f))); + }, Styles.logict, () -> {}).size(160f, 40f).padLeft(2).color(table.color); + + switch(type){ + case announce, toast -> { + table.add(" for "); + fields(table, duration, str -> duration = str); + table.add(" secs "); + } + } } @Override @@ -1291,7 +1314,7 @@ public class LStatements{ @Override public LInstruction build(LAssembler builder){ - return new FlushNotificationI(); + return new FlushMessageI(type, builder.var(duration)); } } diff --git a/core/src/mindustry/logic/MessageType.java b/core/src/mindustry/logic/MessageType.java new file mode 100644 index 0000000000..46eef156f3 --- /dev/null +++ b/core/src/mindustry/logic/MessageType.java @@ -0,0 +1,9 @@ +package mindustry.logic; + +public enum MessageType{ + notify, + announce, + toast; + + public static final MessageType[] all = values(); +} diff --git a/core/src/mindustry/ui/ItemImage.java b/core/src/mindustry/ui/ItemImage.java index 748eabbda4..09add15c73 100644 --- a/core/src/mindustry/ui/ItemImage.java +++ b/core/src/mindustry/ui/ItemImage.java @@ -17,7 +17,7 @@ public class ItemImage extends Stack{ add(new Table(t -> { t.left().bottom(); - t.add(amount > 1000 ? UI.formatAmount(amount) : amount + "").style(Styles.outlineLabel); + t.add(amount >= 1000 ? UI.formatAmount(amount) : amount + "").style(Styles.outlineLabel); t.pack(); })); } @@ -32,7 +32,7 @@ public class ItemImage extends Stack{ if(stack.amount != 0){ add(new Table(t -> { t.left().bottom(); - t.add(stack.amount > 1000 ? UI.formatAmount(stack.amount) : stack.amount + "").style(Styles.outlineLabel); + t.add(stack.amount >= 1000 ? UI.formatAmount(stack.amount) : stack.amount + "").style(Styles.outlineLabel); t.pack(); })); }