|
| 1 | +package io.github.techstreet.dfscript.screen.overlay; |
| 2 | + |
| 3 | +import io.github.techstreet.dfscript.DFScript; |
| 4 | +import io.github.techstreet.dfscript.loader.Loadable; |
| 5 | +import net.minecraft.client.util.math.MatrixStack; |
| 6 | +import net.minecraft.text.Text; |
| 7 | + |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.Date; |
| 10 | + |
| 11 | +public class OverlayManager implements Loadable { |
| 12 | + private static OverlayManager instance; |
| 13 | + |
| 14 | + private ArrayList<Overlay> overlayText; |
| 15 | + |
| 16 | + @Override |
| 17 | + public void load() { |
| 18 | + instance = this; |
| 19 | + overlayText = new ArrayList<>(); |
| 20 | + } |
| 21 | + |
| 22 | + private static class Overlay { |
| 23 | + public Text text; |
| 24 | + public Date time; |
| 25 | + public Date by; |
| 26 | + |
| 27 | + public Overlay(Text text) { |
| 28 | + this.text = text; |
| 29 | + this.time = new Date(); |
| 30 | + this.by = new Date(time.getTime() + 5000); |
| 31 | + } |
| 32 | + |
| 33 | + public boolean past() { |
| 34 | + return new Date().after(by); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + public void add(String text) { |
| 39 | + add(Text.literal(text)); |
| 40 | + } |
| 41 | + public void add(Text text) { |
| 42 | + overlayText.add(new Overlay(text)); |
| 43 | + } |
| 44 | + |
| 45 | + public void render(MatrixStack stack) { |
| 46 | + while (overlayText.size() > 10) { |
| 47 | + overlayText.remove(0); |
| 48 | + } |
| 49 | + int y = 10; |
| 50 | + for (Overlay text: this.overlayText) { |
| 51 | + if(text.past()) continue; |
| 52 | + long now = new Date().getTime(); |
| 53 | + long fadeout = text.by.getTime() - 980; |
| 54 | + int opacity = 0; |
| 55 | + if(fadeout < now) { |
| 56 | + opacity = (int) ((fadeout - now + 1000) * 255 / 1000); |
| 57 | + } |
| 58 | + DFScript.MC.textRenderer.drawWithShadow(stack, text.text, 10, y, 0xFF5555 + ((opacity) << 24)); |
| 59 | + y += 9; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + public static OverlayManager getInstance() { |
| 64 | + return instance; |
| 65 | + } |
| 66 | +} |
0 commit comments