diff --git a/core/src/io/anuke/mindustry/ClientLauncher.java b/core/src/io/anuke/mindustry/ClientLauncher.java index 3234b1f9c5..527eaa3a68 100644 --- a/core/src/io/anuke/mindustry/ClientLauncher.java +++ b/core/src/io/anuke/mindustry/ClientLauncher.java @@ -81,6 +81,7 @@ public abstract class ClientLauncher extends ApplicationCore implements Platform add(netClient = new NetClient()); assets.load(mods); + assets.load(schematics); assets.loadRun("contentinit", ContentLoader.class, () -> { content.init(); diff --git a/core/src/io/anuke/mindustry/Vars.java b/core/src/io/anuke/mindustry/Vars.java index a045ef2af7..77475bb2c7 100644 --- a/core/src/io/anuke/mindustry/Vars.java +++ b/core/src/io/anuke/mindustry/Vars.java @@ -262,9 +262,6 @@ public class Vars implements Loadable{ mods.load(); maps.load(); - if(!headless){ - schematics.load(); - } } public static void loadSettings(){ diff --git a/core/src/io/anuke/mindustry/game/Schematic.java b/core/src/io/anuke/mindustry/game/Schematic.java index 4e82620bd2..24e3e1cae8 100644 --- a/core/src/io/anuke/mindustry/game/Schematic.java +++ b/core/src/io/anuke/mindustry/game/Schematic.java @@ -6,14 +6,21 @@ import io.anuke.mindustry.world.*; public class Schematic{ public final Array tiles; + public StringMap tags; public int width, height; + public boolean workshop; - public Schematic(Array tiles, int width, int height){ + public Schematic(Array tiles, StringMap tags, int width, int height){ this.tiles = tiles; + this.tags = tags; this.width = width; this.height = height; } + public String name(){ + return tags.get("name", "unknown"); + } + public static class Stile{ public @NonNull Block block; public short x, y; diff --git a/core/src/io/anuke/mindustry/game/Schematics.java b/core/src/io/anuke/mindustry/game/Schematics.java index 11c496988f..831fead321 100644 --- a/core/src/io/anuke/mindustry/game/Schematics.java +++ b/core/src/io/anuke/mindustry/game/Schematics.java @@ -1,6 +1,7 @@ package io.anuke.mindustry.game; import io.anuke.arc.*; +import io.anuke.arc.assets.*; import io.anuke.arc.collection.*; import io.anuke.arc.files.*; import io.anuke.arc.graphics.*; @@ -23,7 +24,7 @@ import java.util.zip.*; import static io.anuke.mindustry.Vars.*; /** Handles schematics.*/ -public class Schematics{ +public class Schematics implements Loadable{ private static final byte[] header = {'m', 's', 'c', 'h'}; private static final byte version = 0; @@ -43,6 +44,11 @@ public class Schematics{ }); } + @Override + public void loadSync(){ + load(); + } + /** Load all schematics in the folder immediately.*/ public void load(){ all.clear(); @@ -69,13 +75,15 @@ public class Schematics{ if(!previews.getOr(schematic, ObjectMap::new).containsKey(res)){ int resolution = res.resolution; Draw.blend(); - Draw.color(); + Draw.reset(); Time.mark(); - FrameBuffer buffer = new FrameBuffer((schematic.width + padding) * resolution, (schematic.height + padding) * resolution); Tmp.m1.set(Draw.proj()); + Tmp.m2.set(Draw.trans()); + FrameBuffer buffer = new FrameBuffer((schematic.width + padding) * resolution, (schematic.height + padding) * resolution); shadowBuffer.beginDraw(Color.clear); + Draw.trans().idt(); Draw.proj().setOrtho(0, 0, shadowBuffer.getWidth(), shadowBuffer.getHeight()); Draw.color(); @@ -108,11 +116,10 @@ public class Schematics{ Draw.rect(Tmp.tr1, buffer.getWidth()/2f, buffer.getHeight()/2f, buffer.getWidth(), -buffer.getHeight()); Draw.color(); - Array requests = schematic.tiles.map(t -> new BuildRequest(t.x, t.y, t.rotation, t.block).configure(t.config)); Draw.flush(); - Draw.trans().scale(4f, 4f).translate(tilesize*1.5f, tilesize*1.5f); + Draw.trans().scale(resolution / tilesize, resolution / tilesize).translate(tilesize*1.5f, tilesize*1.5f); requests.each(req -> { req.animScale = 1f; @@ -126,7 +133,8 @@ public class Schematics{ buffer.endDraw(); - Draw.proj(Tmp.m3); + Draw.proj(Tmp.m1); + Draw.trans(Tmp.m2); previews.getOr(schematic, ObjectMap::new).put(res, buffer); Log.info("Time taken: {0}", Time.elapsed()); @@ -172,7 +180,7 @@ public class Schematics{ for(int cy = y; cy <= y2; cy++){ Tile linked = world.ltile(cx, cy); - if(linked != null && linked.entity != null){ + if(linked != null && linked.entity != null && linked.entity.block.isVisible()){ int top = linked.block().size/2; int bot = linked.block().size % 2 == 1 ? -linked.block().size/2 : -(linked.block().size - 1)/2; minx = Math.min(linked.x + bot, minx); @@ -190,7 +198,7 @@ public class Schematics{ x2 = maxx; y2 = maxy; }else{ - return new Schematic(new Array<>(), 1, 1); + return new Schematic(new Array<>(), new StringMap(), 1, 1); } int width = x2 - x + 1, height = y2 - y + 1; @@ -210,7 +218,7 @@ public class Schematics{ } } - return new Schematic(tiles, width, height); + return new Schematic(tiles, new StringMap(), width, height); } /** Converts a schematic to base64. Note that the result of this will always start with 'bXNjaAB'.*/ @@ -243,14 +251,19 @@ public class Schematics{ } int ver; - //version, currently discarded if((ver = input.read()) != version){ throw new IOException("Unknown version: " + ver); } try(DataInputStream stream = new DataInputStream(new InflaterInputStream(input))){ - short width = stream.readShort(), height = stream.readShort(); + + StringMap map = new StringMap(); + byte tags = stream.readByte(); + for(int i = 0; i < tags; i++){ + map.put(stream.readUTF(), stream.readUTF()); + } + IntMap blocks = new IntMap<>(); byte length = stream.readByte(); for(int i = 0; i < length; i++){ @@ -258,6 +271,8 @@ public class Schematics{ blocks.put(i, block == null ? Blocks.air : block); } + Log.info(blocks); + int total = stream.readInt(); Array tiles = new Array<>(total); for(int i = 0; i < total; i++){ @@ -266,11 +281,11 @@ public class Schematics{ int config = stream.readInt(); byte rotation = stream.readByte(); if(block != Blocks.air){ - tiles.add(new Stile(block, Pos.x(position), Pos.y(rotation), config, rotation)); + tiles.add(new Stile(block, Pos.x(position), Pos.y(position), config, rotation)); } } - return new Schematic(tiles, width, height); + return new Schematic(tiles, map, width, height); } } @@ -286,6 +301,13 @@ public class Schematics{ stream.writeShort(schematic.width); stream.writeShort(schematic.height); + + stream.writeByte(schematic.tags.size); + for(ObjectMap.Entry e : schematic.tags.entries()){ + stream.writeUTF(e.key); + stream.writeUTF(e.value); + } + OrderedSet blocks = new OrderedSet<>(); schematic.tiles.each(t -> blocks.add(t.block)); @@ -309,7 +331,7 @@ public class Schematics{ //endregion public enum PreviewRes{ - low(8), high(32); + low(8), med(8), high(32); public final int resolution; diff --git a/core/src/io/anuke/mindustry/input/DesktopInput.java b/core/src/io/anuke/mindustry/input/DesktopInput.java index f251f9f897..141b4ca586 100644 --- a/core/src/io/anuke/mindustry/input/DesktopInput.java +++ b/core/src/io/anuke/mindustry/input/DesktopInput.java @@ -5,6 +5,7 @@ import io.anuke.arc.Graphics.*; import io.anuke.arc.Graphics.Cursor.*; import io.anuke.arc.graphics.*; import io.anuke.arc.graphics.g2d.*; +import io.anuke.arc.input.*; import io.anuke.arc.math.*; import io.anuke.arc.scene.*; import io.anuke.arc.scene.ui.*; @@ -109,7 +110,7 @@ public class DesktopInput extends InputHandler{ Draw.reset(); if(__REMOVE__ != null){ - Texture tex = schematics.getPreview(__REMOVE__, PreviewRes.high); + Texture tex = schematics.getPreview(__REMOVE__, PreviewRes.low); Draw.blend(Blending.disabled); Draw.rect(Draw.wrap(tex), Core.camera.position.x, Core.camera.position.y, tex.getWidth() / 4f, tex.getHeight() / 4f); Draw.blend(); @@ -235,10 +236,15 @@ public class DesktopInput extends InputHandler{ if(Core.input.keyRelease(Binding.schematic)){ Schematic schem = schematics.create(schemX, schemY, rawCursorX, rawCursorY); - __REMOVE__= schem; + __REMOVE__ = schem; schematics.add(schem); } + //TODO remove + if(Core.input.keyTap(KeyCode.T)){ + ui.schematics.show(); + } + if(sreq != null){ float offset = ((sreq.block.size + 2) % 2) * tilesize / 2f; float x = Core.input.mouseWorld().x + offset; diff --git a/core/src/io/anuke/mindustry/ui/dialogs/SchematicsDialog.java b/core/src/io/anuke/mindustry/ui/dialogs/SchematicsDialog.java index 37d49245a6..3beb7a2974 100644 --- a/core/src/io/anuke/mindustry/ui/dialogs/SchematicsDialog.java +++ b/core/src/io/anuke/mindustry/ui/dialogs/SchematicsDialog.java @@ -2,8 +2,10 @@ package io.anuke.mindustry.ui.dialogs; import io.anuke.arc.graphics.*; import io.anuke.arc.scene.ui.*; +import io.anuke.arc.util.*; import io.anuke.mindustry.game.*; import io.anuke.mindustry.game.Schematics.*; +import io.anuke.mindustry.graphics.*; import io.anuke.mindustry.ui.*; import static io.anuke.mindustry.Vars.schematics; @@ -13,6 +15,7 @@ public class SchematicsDialog extends FloatingDialog{ public SchematicsDialog(){ super("$schematics"); + addCloseButton(); shown(this::setup); } @@ -20,19 +23,33 @@ public class SchematicsDialog extends FloatingDialog{ cont.clear(); cont.pane(t -> { + t.top(); + t.margin(20f); int i = 0; for(Schematic s : schematics.all()){ - addButton(b -> { - Texture tex = schematics.getPreview(s, PreviewRes.low); - b.stack(new Image(tex), new BorderImage(tex)).size(100f); + Button sel = t.addButton(b -> { + Texture tex = schematics.getPreview(s, PreviewRes.high); + b.add(s.name()).center().color(Color.lightGray).fillY().get().setEllipsis(true); + b.row(); + b.add(new BorderImage(tex){ + @Override + public void draw(){ + //Draw.blend(Blending.disabled); + super.draw(); + //Draw.blend(); + } + }.setScaling(Scaling.fit).setName("border")); }, () -> { - }).size(110f).pad(4); + }).size(200f, 230f).pad(2).style(Styles.clearPartiali).get(); - if(++i % 3 == 0){ + BorderImage image = sel.find("border"); + image.update(() -> image.borderColor = (sel.isOver() ? Pal.accent : Pal.gray)); + + if(++i % 4 == 0){ t.row(); } } - }); + }).get().setScrollingDisabled(true, false); } }