From 940b8b72f943c4cb8b78efa2bd116dd8d08560e3 Mon Sep 17 00:00:00 2001 From: Anuken Date: Mon, 19 Mar 2018 23:09:38 -0400 Subject: [PATCH] Basic implementation of editor --- core/assets/version.properties | 4 +- .../io/anuke/mindustry/editor/EditorTool.java | 7 +- .../io/anuke/mindustry/editor/MapEditor.java | 6 +- .../mindustry/editor/MapEditorDialog.java | 38 ++++++++-- .../anuke/mindustry/editor/MapRenderer.java | 73 +++++++++++++++---- .../io/anuke/mindustry/editor/MapView.java | 9 ++- core/src/io/anuke/mindustry/world/Block.java | 2 + 7 files changed, 107 insertions(+), 32 deletions(-) diff --git a/core/assets/version.properties b/core/assets/version.properties index 52321f5850..fc2ca7841d 100644 --- a/core/assets/version.properties +++ b/core/assets/version.properties @@ -1,7 +1,7 @@ #Autogenerated file. Do not modify. -#Sat Mar 17 21:49:13 EDT 2018 +#Mon Mar 19 23:08:29 EDT 2018 version=release -androidBuildCode=538 +androidBuildCode=540 name=Mindustry code=3.4 build=custom build diff --git a/core/src/io/anuke/mindustry/editor/EditorTool.java b/core/src/io/anuke/mindustry/editor/EditorTool.java index 31c2cee5b5..a18a1d5d89 100644 --- a/core/src/io/anuke/mindustry/editor/EditorTool.java +++ b/core/src/io/anuke/mindustry/editor/EditorTool.java @@ -21,7 +21,8 @@ public enum EditorTool{ { edit = true; } - + + @Override public void touched(MapEditor editor, int x, int y){ editor.draw(x, y); } @@ -62,9 +63,9 @@ public enum EditorTool{ if((floor ? writer.floor : writer.wall) == dest){ if(floor) - writer.floor = dest; + writer.floor = (byte)editor.getDrawBlock().id; else - writer.wall = dest; + writer.wall = (byte)editor.getDrawBlock().id; editor.getMap().write(px, py, writer); editor.renderer().updatePoint(px, py); diff --git a/core/src/io/anuke/mindustry/editor/MapEditor.java b/core/src/io/anuke/mindustry/editor/MapEditor.java index 2f14dd0056..a2a8c2eaf3 100644 --- a/core/src/io/anuke/mindustry/editor/MapEditor.java +++ b/core/src/io/anuke/mindustry/editor/MapEditor.java @@ -29,6 +29,7 @@ public class MapEditor{ drawBlock = Blocks.stone; this.map = map; this.brushSize = 1; + renderer.resize(map.width(), map.height()); } public Block getDrawBlock(){ @@ -61,7 +62,10 @@ public class MapEditor{ for(int rx = -brushSize + 1; rx <= brushSize - 1; rx ++){ for(int ry = -brushSize + 1; ry <= brushSize - 1; ry ++){ - if(Mathf.dst(rx, ry) < brushSize){ + if(Mathf.dst(rx, ry) <= brushSize){ + if(dx + rx < 0 || dy + ry < 0 || dx + rx >= map.width() || dy + ry >= map.height()){ + continue; + } map.write(dx + rx, dy + ry, writer); renderer.updatePoint(dx + rx, dy + ry); } diff --git a/core/src/io/anuke/mindustry/editor/MapEditorDialog.java b/core/src/io/anuke/mindustry/editor/MapEditorDialog.java index 8988e71272..2eb33797c4 100644 --- a/core/src/io/anuke/mindustry/editor/MapEditorDialog.java +++ b/core/src/io/anuke/mindustry/editor/MapEditorDialog.java @@ -2,6 +2,7 @@ package io.anuke.mindustry.editor; import com.badlogic.gdx.Input.Keys; import com.badlogic.gdx.graphics.Color; +import com.badlogic.gdx.graphics.g2d.TextureRegion; import io.anuke.mindustry.io.MapTileData; import io.anuke.mindustry.io.Platform; import io.anuke.mindustry.world.Block; @@ -12,13 +13,13 @@ import io.anuke.ucore.core.Core; import io.anuke.ucore.core.Graphics; import io.anuke.ucore.core.Inputs; import io.anuke.ucore.core.Timers; -import io.anuke.ucore.graphics.Draw; import io.anuke.ucore.scene.Element; +import io.anuke.ucore.scene.actions.Actions; import io.anuke.ucore.scene.builders.build; -import io.anuke.ucore.scene.builders.imagebutton; import io.anuke.ucore.scene.builders.label; import io.anuke.ucore.scene.builders.table; import io.anuke.ucore.scene.ui.*; +import io.anuke.ucore.scene.ui.layout.Stack; import io.anuke.ucore.scene.ui.layout.Table; import io.anuke.ucore.util.Bundles; @@ -171,6 +172,12 @@ public class MapEditorDialog extends Dialog{ hidden(() -> Platform.instance.updateRPC()); } + + @Override + public Dialog show(){ + return super.show(Core.scene, Actions.sequence(Actions.alpha(0f), Actions.fadeIn(0.3f))); + } + public MapView getView() { return view; } @@ -200,7 +207,7 @@ public class MapEditorDialog extends Dialog{ new table(){{ float isize = 16*2f; aleft(); - + /* new table(){{ defaults().growY().width(130f).padBottom(-6); @@ -237,7 +244,7 @@ public class MapEditorDialog extends Dialog{ new imagebutton("icon-save-image", isize, () -> saveFile.show() - ).text("$text.editor.saveimage");*/ + ).text("$text.editor.saveimage"); row(); @@ -251,6 +258,7 @@ public class MapEditorDialog extends Dialog{ }).padBottom(0).text("$text.back"); }}.left().growY().end(); + */ new table("button"){{ add(view).grow(); @@ -388,7 +396,7 @@ public class MapEditorDialog extends Dialog{ private void addBlockSelection(Table table){ Table content = new Table(); pane = new ScrollPane(content, "volume"); - pane.setScrollingDisabled(true, false); + //pane.setScrollingDisabled(true, false); pane.setFadeScrollBars(false); pane.setOverscroll(true, false); ButtonGroup group = new ButtonGroup<>(); @@ -396,12 +404,26 @@ public class MapEditorDialog extends Dialog{ int i = 0; - for(BlockPair pair : ColorMapper.getPairs()){ - Block block = pair.wall == Blocks.air ? pair.floor : pair.wall; + for(Block block : Block.getAllBlocks()){ + TextureRegion[] regions; + try { + regions = block.getCompactIcon(); + }catch (Exception e){ + continue; + } + + Stack stack = new Stack(); + + for(TextureRegion region : regions){ + stack.add(new Image(region)); + } - ImageButton button = new ImageButton(Draw.hasRegion(block.name) ? Draw.region(block.name) : Draw.region(block.name + "1"), "toggle"); + ImageButton button = new ImageButton("white", "toggle"); button.clicked(() -> editor.setDrawBlock(block)); button.resizeImage(8*4f); + button.getImageCell().setActor(stack); + button.addChild(stack); + button.getImage().remove(); group.add(button); content.add(button).pad(4f).size(53f, 58f); diff --git a/core/src/io/anuke/mindustry/editor/MapRenderer.java b/core/src/io/anuke/mindustry/editor/MapRenderer.java index 857575ec66..bbed8eb9bc 100644 --- a/core/src/io/anuke/mindustry/editor/MapRenderer.java +++ b/core/src/io/anuke/mindustry/editor/MapRenderer.java @@ -7,9 +7,11 @@ import com.badlogic.gdx.utils.IntSet.IntSetIterator; import io.anuke.mindustry.io.MapTileData.TileDataWriter; import io.anuke.mindustry.world.Block; import io.anuke.mindustry.world.blocks.Blocks; +import io.anuke.ucore.core.Core; import io.anuke.ucore.core.Graphics; import io.anuke.ucore.graphics.CacheBatch; import io.anuke.ucore.graphics.Draw; +import io.anuke.ucore.util.Mathf; import static io.anuke.mindustry.Vars.tilesize; @@ -25,19 +27,27 @@ public class MapRenderer { } public void resize(int width, int height){ - batch = new CacheBatch(width * height * 3); + if(batch != null) batch.dispose(); + batch = new CacheBatch(width * height * 5); chunks = new int[width / chunksize][height / chunksize]; updates.clear(); + + for(int x = 0; x < width / chunksize; x ++){ + for(int y = 0; y < height / chunksize; y ++){ + chunks[x][y] = -1; + } + } + updateAll(); } - public void draw(){ + public void draw(float tx, float ty, float tw, float th){ Graphics.end(); Graphics.useBatch(batch); IntSetIterator it = updates.iterator(); - int i = it.next(); - for(; it.hasNext; i = it.next()){ + while(it.hasNext){ + int i = it.next(); int x = i % chunks.length; int y = i / chunks.length; render(x, y, chunks[x][y]); @@ -48,12 +58,19 @@ public class MapRenderer { Gdx.gl.glEnable(GL20.GL_BLEND); + + + batch.getTransformMatrix().setToTranslation(tx, ty, 0).scl(tw / (chunks.length * chunksize * tilesize), + th / (chunks[0].length * chunksize * tilesize), 1f); + batch.setProjectionMatrix(Core.batch.getProjectionMatrix()); batch.beginDraw(); for(int x = 0; x < chunks.length; x ++){ for(int y = 0; y < chunks[0].length; y ++){ int id = chunks[x][y]; - batch.drawCache(id); + if(id != -1){ + batch.drawCache(id); + } } } @@ -65,15 +82,20 @@ public class MapRenderer { public void updatePoint(int x, int y){ x /= chunksize; y /= chunksize; - updates.add(x + y * chunks.length); + if(Mathf.inBounds(x, y, chunks)) + updates.add(x + y * chunks.length); } public void updateAll(){ + Graphics.useBatch(batch); + for(int x = 0; x < chunks.length; x ++){ for(int y = 0; y < chunks[0].length; y ++){ render(x, y, chunks[x][y]); } } + + Graphics.popBatch(); } private void render(int chunkx, int chunky, int previousID){ @@ -83,21 +105,40 @@ public class MapRenderer { batch.begin(previousID); } - for(int x = 0; x < chunkx; x ++){ - for(int y = 0; y < chunky; y ++){ - int wx = chunkx*chunksize + x; - int wy = chunky*chunksize + y; + for(int i = 0; i < 2; i ++) { + for(int x = 0; x < chunksize; x ++){ + for(int y = 0; y < chunksize; y ++){ - TileDataWriter data = editor.getMap().readAt(wx, wy); - Block floor = Block.getByID(data.floor); - Block wall = Block.getByID(data.wall); + int wx = chunkx*chunksize + x; + int wy = chunky*chunksize + y; - if(floor != Blocks.air) Draw.rect(floor.name, wx * tilesize, wy * tilesize); - if(floor != Blocks.air) Draw.rect(wall.name, wx * tilesize, wy * tilesize); + TileDataWriter data = editor.getMap().readAt(wx, wy); + Block floor = Block.getByID(data.floor); + Block wall = Block.getByID(data.wall); + + if(i == 0) { + String fregion = Draw.hasRegion(floor.name) ? floor.name : floor.name + "1"; + + if (floor != Blocks.air && Draw.hasRegion(fregion)) { + Draw.crect(fregion, wx * tilesize, wy * tilesize); + } else { + Draw.rect("blank", wx * tilesize, wy * tilesize, 0, 0); + } + + }else{ + String wregion = Draw.hasRegion(wall.name) ? wall.name : wall.name + "1"; + + if (wall != Blocks.air && Draw.hasRegion(wregion)) { + Draw.crect(wregion, wx * tilesize, wy * tilesize); + } else { + Draw.rect("blank", wx * tilesize, wy * tilesize, 0, 0); + } + } + } } } batch.end(); - chunks[chunkx][chunky] = batch.getLastCache(); + if(previousID == -1) chunks[chunkx][chunky] = batch.getLastCache(); } } diff --git a/core/src/io/anuke/mindustry/editor/MapView.java b/core/src/io/anuke/mindustry/editor/MapView.java index 884fbdf067..e477e3be96 100644 --- a/core/src/io/anuke/mindustry/editor/MapView.java +++ b/core/src/io/anuke/mindustry/editor/MapView.java @@ -180,7 +180,7 @@ public class MapView extends Element implements GestureListener{ float sclheight = size * zoom * ratio; x = (x - getWidth()/2 + sclwidth/2 - offsetx*zoom) / sclwidth * editor.getMap().width(); y = (y - getHeight()/2 + sclheight/2 - offsety*zoom) / sclheight * editor.getMap().height(); - return Tmp.g1.set((int)x, editor.getMap().height() - 1 - (int)y); + return Tmp.g1.set((int)x, (int)y); } private Vector2 unproject(int x, int y){ @@ -189,7 +189,7 @@ public class MapView extends Element implements GestureListener{ float sclwidth = size * zoom; float sclheight = size * zoom * ratio; float px = ((float)x / editor.getMap().width()) * sclwidth + offsetx*zoom - sclwidth/2 + getWidth()/2; - float py = (float)((float)(editor.getMap().height() - 1 - y) / editor.getMap().height()) * sclheight + float py = (float)((float)(y) / editor.getMap().height()) * sclheight + offsety*zoom - sclheight/2 + getHeight()/2; return vec.set(px, py); } @@ -210,6 +210,11 @@ public class MapView extends Element implements GestureListener{ //batch.draw(editor.texture(), centerx - sclwidth/2, centery - sclheight/2, sclwidth, sclheight); //TODO actually render the map here? + Draw.color(Color.LIGHT_GRAY); + Lines.stroke(-2f); + Lines.rect(centerx - sclwidth/2 - 1, centery - sclheight/2 - 1, sclwidth + 2, sclheight + 2); + editor.renderer().draw(centerx - sclwidth/2, centery - sclheight/2, sclwidth, sclheight); + Draw.reset(); if(grid){ Draw.color(Color.GRAY); diff --git a/core/src/io/anuke/mindustry/world/Block.java b/core/src/io/anuke/mindustry/world/Block.java index 6632c86706..5e59a56854 100644 --- a/core/src/io/anuke/mindustry/world/Block.java +++ b/core/src/io/anuke/mindustry/world/Block.java @@ -184,6 +184,8 @@ public class Block extends BaseBlock { public TextureRegion[] getIcon(){ if(Draw.hasRegion(name + "-icon")){ return new TextureRegion[]{Draw.region(name + "-icon")}; + }else if(Draw.hasRegion(name + "1")){ + return new TextureRegion[]{Draw.region(name+ "1")}; }else{ return new TextureRegion[]{Draw.region(name)}; }