|
| 1 | +package cardillan.mlogassertions; |
| 2 | + |
| 3 | +import arc.Events; |
| 4 | +import arc.graphics.Color; |
| 5 | +import arc.graphics.g2d.Draw; |
| 6 | +import arc.graphics.g2d.Font; |
| 7 | +import arc.graphics.g2d.GlyphLayout; |
| 8 | +import arc.scene.ui.layout.Scl; |
| 9 | +import arc.struct.GridMap; |
| 10 | +import arc.util.Align; |
| 11 | +import arc.util.pooling.Pools; |
| 12 | +import mindustry.game.EventType; |
| 13 | +import mindustry.ui.Fonts; |
| 14 | +import mindustry.world.blocks.logic.LogicBlock.LogicBuild; |
| 15 | + |
| 16 | +import static mindustry.Vars.tilesize; |
| 17 | + |
| 18 | +public class Assertions { |
| 19 | + final static GridMap<LogicBuild> blocks = new GridMap<>(); |
| 20 | + final static GridMap<String> messages = new GridMap<>(); |
| 21 | + final static Color textColor = Color.brick; //.a(0.8f); |
| 22 | + |
| 23 | + static int removeX = -1; |
| 24 | + static int removeY = -1; |
| 25 | + |
| 26 | + public static void add(LogicBuild block, String message) { |
| 27 | + int x = (int) block.getX(); |
| 28 | + int y = (int) block.getY(); |
| 29 | + if (blocks.get(x, y) != block) { |
| 30 | + blocks.put(x, y, block); |
| 31 | + messages.put(x, y, message); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + public static void remove(int x, int y) { |
| 36 | + blocks.remove(x, y); |
| 37 | + messages.remove(x, y); |
| 38 | + } |
| 39 | + |
| 40 | + public static void init() { |
| 41 | + Events.on(EventType.ConfigEvent.class, e -> remove((int) e.tile.x, (int) e.tile.y)); |
| 42 | + |
| 43 | + Events.run(EventType.Trigger.drawOver, () -> { |
| 44 | + blocks.values().forEach(Assertions::draw); |
| 45 | + if (removeX != -1) { |
| 46 | + remove(removeX, removeY); |
| 47 | + removeX = -1; |
| 48 | + removeY = -1; |
| 49 | + } |
| 50 | + }); |
| 51 | + } |
| 52 | + |
| 53 | + private static void draw(LogicBuild block) { |
| 54 | + if (block.tile().build != block) { |
| 55 | + removeX = (int) block.getX(); |
| 56 | + removeY = (int) block.getY(); |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + String message = messages.get((int) block.getX(), (int) block.getY()); |
| 61 | + |
| 62 | + float x = block.getX(); |
| 63 | + float y = block.getY() + block.block.size * tilesize / 2f + 1f; |
| 64 | + |
| 65 | + Font font = Fonts.outline; |
| 66 | + GlyphLayout l = Pools.obtain(GlyphLayout.class, GlyphLayout::new); |
| 67 | + boolean ints = font.usesIntegerPositions(); |
| 68 | + font.getData().setScale(1 / 4f / Scl.scl(1f)); |
| 69 | + font.setUseIntegerPositions(false); |
| 70 | + |
| 71 | + l.setText(font, message, textColor, 90f, Align.left, false); |
| 72 | + |
| 73 | +// Draw.color(0f, 0f, 0f, 0.2f); |
| 74 | +// Fill.rect(x, y+2f, l.width+4f, l.height+2f); |
| 75 | + |
| 76 | + Draw.color(); |
| 77 | + font.setColor(textColor); |
| 78 | + font.draw(message, x - l.width/2f, y + l.height, 90f, Align.left, false); |
| 79 | + font.setUseIntegerPositions(ints); |
| 80 | + |
| 81 | + font.getData().setScale(1f); |
| 82 | + |
| 83 | + Pools.free(l); |
| 84 | + } |
| 85 | +} |
0 commit comments