diff --git a/core/assets/bundles/bundle.properties b/core/assets/bundles/bundle.properties index 6ebfddb815..ceaae059c5 100644 --- a/core/assets/bundles/bundle.properties +++ b/core/assets/bundles/bundle.properties @@ -176,6 +176,7 @@ text.editor.savemap=Save Map text.editor.saved=Saved! text.editor.save.noname=Your map does not have a name! Set one in the 'map info' menu. text.editor.save.overwrite=Your map overwrites a built-in map! Pick a different name in the 'map info' menu. +text.editor.import.exists=[scarlet]Unable to import:[] a built-in map named '{0}' already exists! text.editor.import=Import... text.editor.importmap=Import Map text.editor.importmap.description=Import an already existing map @@ -199,6 +200,7 @@ text.editor.resizemap=Resize Map text.editor.resizebig=[scarlet]Warning!\n[]Maps larger than 256 units may be laggy and unstable. text.editor.mapname=Map Name: text.editor.overwrite=[accent]Warning!\nThis overwrites an existing map. +text.editor.overwrite.confirm=[scarlet]Warning![] A map with this name already exists. Are you sure you want to overwrite it? text.editor.selectmap=Select a map to load: text.width=Width: text.height=Height: diff --git a/core/src/io/anuke/mindustry/ui/dialogs/MapsDialog.java b/core/src/io/anuke/mindustry/ui/dialogs/MapsDialog.java index 837e2122c4..46ae2cd62b 100644 --- a/core/src/io/anuke/mindustry/ui/dialogs/MapsDialog.java +++ b/core/src/io/anuke/mindustry/ui/dialogs/MapsDialog.java @@ -3,7 +3,11 @@ package io.anuke.mindustry.ui.dialogs; import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.utils.Scaling; import io.anuke.mindustry.Vars; +import io.anuke.mindustry.core.Platform; import io.anuke.mindustry.io.Map; +import io.anuke.mindustry.io.MapIO; +import io.anuke.mindustry.io.MapMeta; +import io.anuke.mindustry.io.MapTileData; import io.anuke.mindustry.ui.BorderImage; import io.anuke.ucore.scene.event.Touchable; import io.anuke.ucore.scene.ui.Image; @@ -11,15 +15,48 @@ import io.anuke.ucore.scene.ui.ScrollPane; import io.anuke.ucore.scene.ui.TextButton; import io.anuke.ucore.scene.ui.layout.Table; import io.anuke.ucore.util.Bundles; +import io.anuke.ucore.util.Log; +import io.anuke.ucore.util.Strings; -import static io.anuke.mindustry.Vars.ui; -import static io.anuke.mindustry.Vars.world; +import java.io.DataInputStream; + +import static io.anuke.mindustry.Vars.*; public class MapsDialog extends FloatingDialog { public MapsDialog() { super("$text.maps"); + addCloseButton(); + buttons().addImageTextButton("$text.editor.importmap", "icon-add", 14*2, () -> { + Platform.instance.showFileChooser("$text.editor.importmap", "Map File", file -> { + try{ + DataInputStream stream = new DataInputStream(file.read()); + MapMeta meta = MapIO.readMapMeta(stream); + MapTileData data = MapIO.readTileData(stream, meta, true); + stream.close(); + + String name = meta.tags.get("name", file.nameWithoutExtension()); + + if(world.maps().getByName(name) != null && !world.maps().getByName(name).custom){ + ui.showError(Bundles.format("text.editor.import.exists", name)); + }else if(world.maps().getByName(name) != null){ + ui.showConfirm("$text.confirm", "$text.editor.overwrite.confirm", () -> { + world.maps().saveMap(name, data, meta.tags); + setup(); + }); + }else{ + world.maps().saveMap(name, data, meta.tags); + setup(); + } + + }catch (Exception e){ + ui.showError(Bundles.format("text.editor.errorimageload", Strings.parseException(e, false))); + Log.err(e); + } + }, true, mapExtension); + }).size(230f, 64f); + shown(this::setup); } @@ -71,6 +108,7 @@ public class MapsDialog extends FloatingDialog { table.table("clear", desc -> { desc.top(); Table t = new Table(); + t.margin(6); ScrollPane pane = new ScrollPane(t, "clear-black"); desc.add(pane).grow(); @@ -93,7 +131,7 @@ public class MapsDialog extends FloatingDialog { t.add("$text.editor.oregen.info").padRight(10).color(Color.GRAY); t.row(); t.add(map.meta.hasOreGen() ? "$text.on" : "$text.off").padTop(2); - }).height(mapsize).width(mapsize).margin(6); + }).height(mapsize).width(mapsize); table.row(); diff --git a/core/src/io/anuke/mindustry/ui/fragments/MenuFragment.java b/core/src/io/anuke/mindustry/ui/fragments/MenuFragment.java index e38c2b4bd8..a89a723f5e 100644 --- a/core/src/io/anuke/mindustry/ui/fragments/MenuFragment.java +++ b/core/src/io/anuke/mindustry/ui/fragments/MenuFragment.java @@ -31,7 +31,7 @@ public class MenuFragment implements Fragment{ row(); - add(new MenuButton("icon-editor", "$text.editor", ui.editor::show)); + add(new MenuButton("icon-editor", "$text.editor", () -> ui.loadAnd(ui.editor::show))); add(new MenuButton("icon-menu", "$text.maps", ui.maps::show)); @@ -70,7 +70,7 @@ public class MenuFragment implements Fragment{ defaults().size(size).pad(5); - new imagebutton("icon-editor", isize, ui.editor::show).text("$text.editor").padTop(4f); + new imagebutton("icon-editor", isize, () -> ui.loadAnd(ui.editor::show)).text("$text.editor").padTop(4f); new imagebutton("icon-tools", isize, ui.settings::show).text("$text.settings").padTop(4f);