diff --git a/forge-adventure/pom.xml b/forge-adventure/pom.xml index 3d696393376..2ab05277ddc 100644 --- a/forge-adventure/pom.xml +++ b/forge-adventure/pom.xml @@ -233,11 +233,6 @@ - - com.github.jetopto1 - cling - 1.0.0 - com.badlogicgames.gdx gdx diff --git a/forge-gui-mobile/src/forge/adventure/scene/SettingsScene.java b/forge-gui-mobile/src/forge/adventure/scene/SettingsScene.java index 0a08d742717..760acf9dc26 100644 --- a/forge-gui-mobile/src/forge/adventure/scene/SettingsScene.java +++ b/forge-gui-mobile/src/forge/adventure/scene/SettingsScene.java @@ -3,45 +3,117 @@ package forge.adventure.scene; import com.badlogic.gdx.Input; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.scenes.scene2d.Actor; -import com.badlogic.gdx.scenes.scene2d.Stage; +import com.badlogic.gdx.scenes.scene2d.InputEvent; import com.badlogic.gdx.scenes.scene2d.ui.*; import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener; +import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; import com.badlogic.gdx.utils.Align; import com.github.tommyettinger.textra.TextraButton; import com.github.tommyettinger.textra.TextraLabel; import forge.Forge; import forge.adventure.util.Config; import forge.adventure.util.Controls; +import forge.adventure.util.UIActor; import forge.gui.GuiBase; import forge.localinstance.properties.ForgePreferences; import forge.model.FModel; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.stream.Stream; + +import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; + /** * Scene to handle settings of the base forge and adventure mode */ public class SettingsScene extends UIScene { static public ForgePreferences Preference; - Stage stage; Texture Background; private final Table settingGroup; TextraButton backButton; + TextraButton newPlane; ScrollPane scrollPane; + SelectBox selectSourcePlane; + TextField newPlaneName; + private void copyNewPlane() { + String plane=(String) selectSourcePlane.getSelected(); + Path source= Paths.get(Config.instance().getPlanePath(plane)); + Path destination= Paths.get(Config.instance().getPlanePath(""+newPlaneName.getText())); + AtomicBoolean somethingWentWrong= new AtomicBoolean(false); + try (Stream stream = Files.walk(source)) + { + Files.createDirectories(destination); + stream.forEach(s -> { + try { Files.copy(s, destination.resolve(source.relativize(s)), REPLACE_EXISTING); } + catch (IOException e) { + somethingWentWrong.set(true); + } + }); + } catch (IOException e) { + somethingWentWrong.set(true); + } + if(somethingWentWrong.get()) + { + Dialog dialog=ui.showDialog(stage,"Something went wrong", UIActor.ButtonOk|UIActor.ButtonAbort,null); + dialog.text("Copy was not successful check your access right\n and if the folder is in use"); + dialog.show(stage); + } + else + { + Dialog dialog=ui.showDialog(stage,"Copied plane", UIActor.ButtonOk|UIActor.ButtonAbort,null); + dialog.text("New plane "+newPlaneName.getText()+" was created\nYou can now start the editor to change the plane\n" + + "or edit it manually from the folder\n" + + Config.instance().getPlanePath(""+newPlaneName.getText())); + Config.instance().getSettingData().plane = ""+newPlaneName.getText(); + Config.instance().saveSettings(); + dialog.show(stage); + } + + } + private void createNewPlane() { + Dialog dialog=ui.showDialog(stage,"Create your own Plane", UIActor.ButtonOk|UIActor.ButtonAbort,()->copyNewPlane()); + dialog.text("Select a plane to copy"); + dialog.getContentTable().row(); + dialog.getContentTable().add(selectSourcePlane); + dialog.getContentTable().row(); + dialog.text("Set new plane name"); + dialog.getContentTable().row(); + dialog.getContentTable().add(newPlaneName); + newPlaneName.setText(selectSourcePlane.getSelected().toString()+"_copy"); + dialog.show(stage); + } + private SettingsScene() { super(Forge.isLandscapeMode() ? "ui/settings.json" : "ui/settings_portrait.json"); + selectSourcePlane = new SelectBox(Controls.getSkin()); + newPlaneName = Controls.newTextField(""); settingGroup = new Table(); if (Preference == null) { Preference = new ForgePreferences(); } - + selectSourcePlane.setItems(Config.instance().getAllAdventures()); SelectBox plane = Controls.newComboBox(Config.instance().getAllAdventures(), Config.instance().getSettingData().plane, o -> { Config.instance().getSettingData().plane = (String) o; Config.instance().saveSettings(); return null; }); + newPlane=Controls.newTextButton("Create own plane"); + newPlane.addListener(new ClickListener() { + @Override + public void clicked(InputEvent event, float x, float y) { + createNewPlane(); + } + }); addLabel(Forge.getLocalizer().getMessage("lblWorld")); settingGroup.add(plane).align(Align.right).pad(2); + addLabel(Forge.getLocalizer().getMessage("lblCreate")+Forge.getLocalizer().getMessage("lblWorld")); + settingGroup.add(newPlane).align(Align.right).pad(2); if (!GuiBase.isAndroid()) { SelectBox videomode = Controls.newComboBox(new String[]{"720p", "768p", "900p", "1080p"}, Config.instance().getSettingData().videomode, o -> { diff --git a/forge-gui-mobile/src/forge/adventure/scene/UIScene.java b/forge-gui-mobile/src/forge/adventure/scene/UIScene.java index b8fa9d2a14a..419f57584c7 100644 --- a/forge-gui-mobile/src/forge/adventure/scene/UIScene.java +++ b/forge-gui-mobile/src/forge/adventure/scene/UIScene.java @@ -28,7 +28,7 @@ import forge.adventure.util.UIActor; */ public class UIScene extends Scene { protected UIActor ui; - Stage stage; + protected Stage stage; String uiFile; private final Dialog keyboardDialog; diff --git a/forge-gui-mobile/src/forge/adventure/util/Config.java b/forge-gui-mobile/src/forge/adventure/util/Config.java index 305e0cd4c6f..3da34a9351d 100644 --- a/forge-gui-mobile/src/forge/adventure/util/Config.java +++ b/forge-gui-mobile/src/forge/adventure/util/Config.java @@ -2,6 +2,7 @@ package forge.adventure.util; import com.badlogic.gdx.files.FileHandle; import com.badlogic.gdx.graphics.g2d.TextureAtlas; +import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.Json; import com.badlogic.gdx.utils.JsonWriter; import com.badlogic.gdx.utils.ObjectMap; @@ -43,7 +44,7 @@ public class Config { } private Config() { - String path= GuiBase.isAndroid() ? ForgeConstants.ASSETS_DIR : Files.exists(Paths.get("./res"))?"./":"../forge-gui/"; + String path= resPath(); adventures = new File(GuiBase.isAndroid() ? ForgeConstants.ADVENTURE_DIR : path + "/res/adventure").list(); try { @@ -59,6 +60,7 @@ public class Config { if(adventures!=null&&adventures.length>=1) settingsData.plane=adventures[0]; } + plane=settingsData.plane; if(settingsData.width==0||settingsData.height==0) { @@ -80,10 +82,9 @@ public class Config { if(settingsData.cardTooltipAdjLandscape == null || settingsData.cardTooltipAdjLandscape == 0f) settingsData.cardTooltipAdjLandscape=1f; - this.plane = settingsData.plane; - currentConfig = this; + prefix = getPlanePath(settingsData.plane); - prefix = path + "/res/adventure/" + plane + "/"; + currentConfig = this; if (FModel.getPreferences() != null) Lang = FModel.getPreferences().getPref(ForgePreferences.FPref.UI_LANGUAGE); try @@ -99,6 +100,22 @@ public class Config { } + private String resPath() { + + return GuiBase.isAndroid() ? ForgeConstants.ASSETS_DIR : Files.exists(Paths.get("./res"))?"./":"../forge-gui/"; + } + + public String getPlanePath(String plane) { + if(plane.startsWith("")) + { + return ForgeConstants.USER_ADVENTURE_DIR + "/userplanes/" + plane.substring("".length()) + "/"; + } + else + { + return resPath() + "/res/adventure/" + plane + "/"; + } + } + public ConfigData getConfigData() { return configData; } @@ -130,7 +147,7 @@ public class Config { public String getPlane() { - return plane; + return plane.replace("","user_"); } public String[] colorIdNames() { @@ -174,8 +191,17 @@ public class Config { { return settingsData; } - public String[] getAllAdventures() + public Array getAllAdventures() { + String path=ForgeConstants.USER_ADVENTURE_DIR + "/userplanes/"; + Array adventures = new Array(); + if(new File(path).exists()) + adventures.addAll(new File(path).list()); + for(int i=0;i"+adventures.get(i)); + } + adventures.addAll(this.adventures); return adventures; } diff --git a/forge-gui-mobile/src/forge/adventure/util/Controls.java b/forge-gui-mobile/src/forge/adventure/util/Controls.java index 1fb6f3cc08e..3472b8089e5 100644 --- a/forge-gui-mobile/src/forge/adventure/util/Controls.java +++ b/forge-gui-mobile/src/forge/adventure/util/Controls.java @@ -12,6 +12,7 @@ import com.badlogic.gdx.scenes.scene2d.ui.*; import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener; import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; import com.badlogic.gdx.utils.Align; +import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.Null; import com.github.tommyettinger.textra.Font; import com.github.tommyettinger.textra.TextraButton; @@ -99,6 +100,26 @@ public class Controls { return ret; } + static public SelectBox newComboBox(Array text, String item, Function func) { + SelectBox ret = new SelectBox(getSkin()); + ret.getStyle().listStyle.selection.setTopHeight(4); + ret.setItems(text); + ret.addListener(new ChangeListener() { + @Override + public void changed(ChangeEvent event, Actor actor) { + try { + func.apply(((SelectBox) actor).getSelected()); + } catch (Exception e) { + e.printStackTrace(); + } + } + }); + func.apply(item); + ret.getList().setAlignment(Align.center); + ret.setSelected(item); + ret.setAlignment(Align.right); + return ret; + } static public SelectBox newComboBox(Float[] text, float item, Function func) { SelectBox ret = new SelectBox(getSkin()); ret.getStyle().listStyle.selection.setTopHeight(4); diff --git a/forge-gui-mobile/src/forge/adventure/util/UIActor.java b/forge-gui-mobile/src/forge/adventure/util/UIActor.java index 4d67a1d35f8..d0ed76b9abb 100644 --- a/forge-gui-mobile/src/forge/adventure/util/UIActor.java +++ b/forge-gui-mobile/src/forge/adventure/util/UIActor.java @@ -7,6 +7,7 @@ import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.scenes.scene2d.Actor; import com.badlogic.gdx.scenes.scene2d.Group; import com.badlogic.gdx.scenes.scene2d.InputEvent; +import com.badlogic.gdx.scenes.scene2d.Stage; import com.badlogic.gdx.scenes.scene2d.ui.*; import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable; @@ -286,4 +287,31 @@ public class UIActor extends Group { } }); } + static final public int ButtonYes=0x1; + static final public int ButtonNo=0x2; + static final public int ButtonOk=0x4; + static final public int ButtonAbort=0x8; + public Dialog showDialog(Stage stage, String header, int buttons, Runnable onOkOrYes) { + Dialog dialog =new Dialog(header, Controls.getSkin()) + { + protected void result(Object object) + { + if(onOkOrYes!=null&&object!=null&&object.equals(true)) + onOkOrYes.run(); + this.hide(); + removeActor(this); + } + }; + if((buttons&ButtonYes)!=0) + dialog.button(Forge.getLocalizer().getMessage("lblYes"), true); + if((buttons&ButtonNo)!=0) + dialog.button(Forge.getLocalizer().getMessage("lblNo"), false); + if((buttons&ButtonOk)!=0) + dialog.button(Forge.getLocalizer().getMessage("lblOk"), true); + if((buttons&ButtonAbort)!=0) + dialog.button(Forge.getLocalizer().getMessage("lblAbort"), false); + addActor(dialog); + dialog.show(stage); + return dialog; + } } diff --git a/forge-gui/pom.xml b/forge-gui/pom.xml index c7da09dd551..6baac0096a9 100644 --- a/forge-gui/pom.xml +++ b/forge-gui/pom.xml @@ -73,9 +73,9 @@ compile - com.github.jetopto1.cling + org.fourthline.cling cling-core - 1.0.0 + 2.1.2 compile