From 18ed359af03fd22cb772ff5485be6bfaee61cf41 Mon Sep 17 00:00:00 2001 From: Brendan Robert Date: Sun, 3 Jul 2016 01:00:44 -0500 Subject: [PATCH] Application state is now an OSGi concern and SCR has been fully integrated to simplify plugin registration --- OutlawEditor/OutlawPluginExample/pom.xml | 39 ++++-- .../outlaw/plugin/example/Activator.java | 48 ++++--- .../outlaw/plugin/example/ExamplePlugin.java | 65 +++++++++ OutlawEditor/pom.xml | 24 +++- .../badvision/outlaweditor/Application.java | 125 +++++++++++------- .../org/badvision/outlaweditor/FileUtils.java | 5 +- .../badvision/outlaweditor/GlobalEditor.java | 15 ++- .../org/badvision/outlaweditor/MapEditor.java | 41 +++--- .../badvision/outlaweditor/MythosEditor.java | 3 +- .../outlaweditor/api/ApplicationState.java | 29 +++- .../outlaweditor/api/MenuAction.java | 31 +++++ .../outlaweditor/apple/AppleImageEditor.java | 10 +- .../outlaweditor/data/DataUtilities.java | 10 +- .../badvision/outlaweditor/data/TileMap.java | 7 +- .../outlaweditor/data/TilesetUtils.java | 13 +- .../ui/ApplicationMenuController.java | 7 +- .../ui/ApplicationUIController.java | 4 +- .../badvision/outlaweditor/ui/UIAction.java | 34 ++--- .../impl/ApplicationMenuControllerImpl.java | 32 ++++- .../ui/impl/ApplicationUIControllerImpl.java | 7 +- .../impl/GlobalEditorTabControllerImpl.java | 48 +++---- .../ui/impl/ImageEditorTabControllerImpl.java | 12 +- .../ui/impl/MapEditorTabControllerImpl.java | 17 ++- .../ui/impl/TileEditorTabControllerImpl.java | 13 +- OutlawEditor/src/main/resources/Menubar.fxml | 13 +- 25 files changed, 447 insertions(+), 205 deletions(-) create mode 100644 OutlawEditor/OutlawPluginExample/src/main/java/org/badvision/outlaw/plugin/example/ExamplePlugin.java create mode 100644 OutlawEditor/src/main/java/org/badvision/outlaweditor/api/MenuAction.java diff --git a/OutlawEditor/OutlawPluginExample/pom.xml b/OutlawEditor/OutlawPluginExample/pom.xml index 411b7ff8..f1c459c0 100644 --- a/OutlawEditor/OutlawPluginExample/pom.xml +++ b/OutlawEditor/OutlawPluginExample/pom.xml @@ -7,10 +7,12 @@ 0.1 bundle - OutlawPluginExample OSGi Bundle + Outlaw Plugin Example UTF-8 + 1.8 + 1.8 @@ -22,24 +24,36 @@ true - org.osgi.framework,org.badvision.outlaweditor.api,org.badvision.outlaweditor.data,org.badvision.outlaweditor.data.xml,org.badvision.outlaweditor.ui - org.badvision.outlaw.plugin.example.* + org.badvision.outlaw.plugin.example org.badvision.outlaw.plugin.example.Activator + * + + org.apache.felix + maven-scr-plugin + 1.22.0 + + + generate-scr-scrdescriptor + + scr + + + + 8-Bit Bunch + + + + + - org.apache.felix - org.apache.felix.framework - 5.4.0 - jar - - - ${project.groupId} + org.badvision OutlawEditor 1.0-SNAPSHOT @@ -48,5 +62,10 @@ org.apache.felix.scr.annotations 1.10.0 + + org.apache.felix + org.apache.felix.main + 5.4.0 + diff --git a/OutlawEditor/OutlawPluginExample/src/main/java/org/badvision/outlaw/plugin/example/Activator.java b/OutlawEditor/OutlawPluginExample/src/main/java/org/badvision/outlaw/plugin/example/Activator.java index b34c249b..ba30b341 100644 --- a/OutlawEditor/OutlawPluginExample/src/main/java/org/badvision/outlaw/plugin/example/Activator.java +++ b/OutlawEditor/OutlawPluginExample/src/main/java/org/badvision/outlaw/plugin/example/Activator.java @@ -1,33 +1,47 @@ package org.badvision.outlaw.plugin.example; -import org.apache.felix.scr.annotations.Reference; -import org.apache.felix.scr.annotations.ReferenceCardinality; import org.badvision.outlaweditor.api.ApplicationState; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; /** - * + * This is an example activator class, provided to demonstrate basic bundle lifecycle events. + * Since we're using declarative servies (Felix SCR), all the messy bits of service registration + * and reference passing are managed for us. Otherwise we'd be doing that here. Fortunately, + * we don't have to do all that. + * + * Still, this is a useful mechanism if you have some one-time setup or shutdown concerns that apply + * to your whole bundle, such as reading configuration data from a file or whatever. + * * @author blurry */ public class Activator implements BundleActivator { - - + @Override public void start(BundleContext bc) throws Exception { - System.out.println("Hello, world!"); - ApplicationState app = bc.getService(bc.getServiceReference(ApplicationState.class)); + System.out.println("Hello, plugin!"); + checkReferences(); + } + + @Override + public void stop(BundleContext bc) throws Exception { + System.out.println("Goodbye, plugin!"); + } + + private void checkReferences() { + // Note that our activator is not a component, so we can't use the @Reference + // annotation to inject app automatically. ApplicationState has a convenience + // method to get around this in just such events, but it's a hack. + // Ultimately it's not a good idea to rely on this too much as it follows + // some bad practices behind the scene that leave unclosed references, etc. + // I'll have to come up with a safer way to inject dependencies without + // causing housekeeping issues for OSGi. + ApplicationState app = ApplicationState.getInstance(); if (app == null) { System.out.println("App is null?!?!"); + } else if (app.getCurrentPlatform() == null) { + System.out.println("Current platform is null?"); } else { - if (app.getCurrentPlatform() == null) { - System.out.println("Current platform is null?"); - } else { - System.out.println("Current platform is "+app.getCurrentPlatform()); - } - } + System.out.println("Current platform is " + app.getCurrentPlatform()); + } } - - public void stop(BundleContext bc) throws Exception { - } - } diff --git a/OutlawEditor/OutlawPluginExample/src/main/java/org/badvision/outlaw/plugin/example/ExamplePlugin.java b/OutlawEditor/OutlawPluginExample/src/main/java/org/badvision/outlaw/plugin/example/ExamplePlugin.java new file mode 100644 index 00000000..1d731b45 --- /dev/null +++ b/OutlawEditor/OutlawPluginExample/src/main/java/org/badvision/outlaw/plugin/example/ExamplePlugin.java @@ -0,0 +1,65 @@ +package org.badvision.outlaw.plugin.example; + +import javafx.event.ActionEvent; +import org.apache.felix.scr.annotations.Activate; +import org.apache.felix.scr.annotations.Component; +import org.apache.felix.scr.annotations.Deactivate; +import org.apache.felix.scr.annotations.Reference; +import org.apache.felix.scr.annotations.Service; +import org.badvision.outlaweditor.api.ApplicationState; +import org.badvision.outlaweditor.api.MenuAction; +import org.osgi.framework.BundleContext; + + +/** + * This + * @author blurry + */ +@Component(immediate = true) +@Service(MenuAction.class) +public class ExamplePlugin implements MenuAction { + + // Note: Because ApplicationState is already a defined service, this will automatically be bound. + // Hence, it is not necessary to worry about passing it it. + @Reference + ApplicationState app; + + // This is called when our plugin is starting + @Activate + public void activate() throws Exception { + System.out.println("Hello, menu!"); + checkReferences(); + } + + // This is called when our plugin is stopping + @Deactivate + public void stop(BundleContext bc) throws Exception { + System.out.println("Goodbye, menu!"); + } + + // This identifies the menu item label + @Override + public String getName() { + return "Example action"; + } + + // This method is called when the user selects the menu item + @Override + public void handle(ActionEvent event) { + System.out.println("Clicked!"); + checkReferences(); + } + + private void checkReferences() { +// app = ApplicationState.getInstance(); + if (app == null) { + System.out.println("App is null?!?!"); + } else { + if (app.getCurrentPlatform() == null) { + System.out.println("Current platform is null?"); + } else { + System.out.println("Current platform is "+app.getCurrentPlatform()); + } + } + } +} diff --git a/OutlawEditor/pom.xml b/OutlawEditor/pom.xml index 5d23a9cc..dfb98eaa 100644 --- a/OutlawEditor/pom.xml +++ b/OutlawEditor/pom.xml @@ -9,6 +9,7 @@ UTF-8 org.badvision.outlaweditor.Application + apache20 @@ -23,6 +24,19 @@ org.apache.felix maven-scr-plugin 1.22.0 + + + generate-scr-scrdescriptor + + scr + + + + 8-Bit Bunch + + + + org.apache.felix @@ -158,15 +172,15 @@ org.apache.felix.main 5.4.0 - - org.osgi - org.osgi.core - 4.3.0 - org.apache.felix org.apache.felix.scr.annotations 1.10.0 + + org.apache.felix + org.apache.felix.scr + 2.0.2 + diff --git a/OutlawEditor/src/main/java/org/badvision/outlaweditor/Application.java b/OutlawEditor/src/main/java/org/badvision/outlaweditor/Application.java index 6fcd6ff6..f7e91d58 100644 --- a/OutlawEditor/src/main/java/org/badvision/outlaweditor/Application.java +++ b/OutlawEditor/src/main/java/org/badvision/outlaweditor/Application.java @@ -11,71 +11,98 @@ package org.badvision.outlaweditor; import java.io.IOException; import java.util.HashMap; -import java.util.Hashtable; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; import javafx.fxml.FXMLLoader; import javafx.scene.Scene; -import javafx.scene.canvas.Canvas; import javafx.scene.layout.AnchorPane; import javafx.stage.Stage; import javafx.stage.WindowEvent; import org.apache.felix.framework.Felix; +import org.apache.felix.framework.util.FelixConstants; import org.apache.felix.main.AutoProcessor; import org.apache.felix.scr.annotations.Component; +import org.apache.felix.scr.annotations.Service; +import org.apache.felix.scr.impl.Activator; import org.badvision.outlaweditor.api.ApplicationState; +import org.badvision.outlaweditor.api.MenuAction; import org.badvision.outlaweditor.api.Platform; import org.badvision.outlaweditor.data.xml.GameData; import org.badvision.outlaweditor.ui.ApplicationUIController; +import org.osgi.framework.BundleActivator; +import org.osgi.framework.BundleContext; import org.osgi.framework.BundleException; +import org.osgi.framework.ServiceEvent; +import org.osgi.framework.launch.Framework; +import org.osgi.util.tracker.ServiceTracker; /** * * @author brobert */ -@Component(name = "org.badvision.outlaweditor.api.ApplicationState") -public class Application extends javafx.application.Application implements ApplicationState { +@Component +@Service(org.badvision.outlaweditor.api.ApplicationState.class) +public class Application extends javafx.application.Application implements ApplicationState, BundleActivator { - public static GameData gameData = new GameData(); - public static Platform currentPlatform = Platform.AppleII; - static Application instance; - - public static Application getInstance() { - return instance; - } + public static Framework felix; + private GameData gameData = new GameData(); + private Platform currentPlatform = Platform.AppleII; + private ApplicationUIController controller; + private Stage primaryStage; public static void shutdown() { try { - instance.pluginContainer.stop(); - instance.pluginContainer.waitForStop(0L); + felix.stop(); + felix.waitForStop(0L); } catch (BundleException | InterruptedException ex) { Logger.getLogger(Application.class.getName()).log(Level.SEVERE, null, ex); } } - private ApplicationUIController controller; - private Felix pluginContainer; + @Override + public GameData getGameData() { + return gameData; + } + @Override + public ApplicationUIController getApplicationUI() { + return controller; + } + + @Override + public Platform getCurrentPlatform() { + return currentPlatform; + } + + @Override + public void setGameData(GameData newData) { + gameData = newData; + } + + @Override + public void setCurrentPlatform(Platform newPlatform) { + currentPlatform = newPlatform; + } + + @Override public ApplicationUIController getController() { return controller; } - public Stage primaryStage; - - public static Stage getPrimaryStage() { - return instance.primaryStage; + @Override + public Stage getPrimaryStage() { + return primaryStage; } @Override public void start(Stage primaryStage) { - instance = this; this.primaryStage = primaryStage; javafx.application.Platform.setImplicitExit(true); try { startPluginContainer(); - } catch (BundleException ex) { + } catch (Exception ex) { Logger.getLogger(Application.class.getName()).log(Level.SEVERE, null, ex); throw new RuntimeException(ex); } @@ -96,7 +123,6 @@ public class Application extends javafx.application.Application implements Appli }); primaryStage.show(); } - Canvas tilePreview; /** * The main() method is ignored in correctly deployed JavaFX application. @@ -110,35 +136,36 @@ public class Application extends javafx.application.Application implements Appli launch(args); } - private void startPluginContainer() throws BundleException { - Map pluginConfiguration = new HashMap<>(); - pluginConfiguration.put("felix.cache.locking", "false"); - pluginConfiguration.put("felix.auto.deploy.action", "install,start"); - pluginConfiguration.put("felix.auto.deploy.dir", "install"); - pluginConfiguration.put("org.osgi.framework.system.packages.extra", - "org.badvision.outlaweditor.api," - + "org.badvision.outlaweditor.data," - + "org.badvision.outlaweditor.data.xml," - + "org.badvision.outlaweditor.ui," - + "org.osgi.framework"); - pluginContainer = new Felix(pluginConfiguration); - pluginContainer.start(); - pluginContainer.getBundleContext().registerService(ApplicationState.class, this, new Hashtable<>()); - AutoProcessor.process(pluginConfiguration, pluginContainer.getBundleContext()); + ServiceTracker tracker; + private void startPluginContainer() throws BundleException, Exception { + Map config = new HashMap<>(); + config.put(FelixConstants.ACTIVATION_LAZY, "false"); + config.put("felix.cache.locking", "false"); + config.put("felix.auto.deploy.dir", "install"); + config.put("felix.auto.deploy.action", "install,start"); + config.put("org.osgi.framework.system.packages.extra", + "javafx.event," + + "org.badvision.outlaweditor.api," + + "org.badvision.outlaweditor.data," + + "org.badvision.outlaweditor.data.xml," + + "org.badvision.outlaweditor.ui," + + "org.osgi.framework"); + felix = new Felix(config); + felix.start(); + felix.getBundleContext().registerService(ApplicationState.class, this, null); + tracker = new ServiceTracker(felix.getBundleContext(), MenuAction.class, null); + tracker.open(); + Activator scrActivator = new Activator(); + scrActivator.start(felix.getBundleContext()); + AutoProcessor.process(config, felix.getBundleContext()); + } + + @Override + public void start(BundleContext bc) throws Exception { + launch(); } @Override - public GameData getGameData() { - return gameData; - } - - @Override - public ApplicationUIController getApplicationUI() { - return controller; - } - - @Override - public Platform getCurrentPlatform() { - return currentPlatform; + public void stop(BundleContext bc) throws Exception { } } diff --git a/OutlawEditor/src/main/java/org/badvision/outlaweditor/FileUtils.java b/OutlawEditor/src/main/java/org/badvision/outlaweditor/FileUtils.java index 0ef979f3..6fe5eeef 100644 --- a/OutlawEditor/src/main/java/org/badvision/outlaweditor/FileUtils.java +++ b/OutlawEditor/src/main/java/org/badvision/outlaweditor/FileUtils.java @@ -24,6 +24,7 @@ import java.util.zip.DataFormatException; import java.util.zip.Deflater; import java.util.zip.Inflater; import javafx.stage.FileChooser; +import org.badvision.outlaweditor.api.ApplicationState; /** * @@ -67,7 +68,7 @@ public class FileUtils { f.getExtensionFilters().add(e.getExtensionFilter()); } if (create) { - File file = f.showSaveDialog(Application.getPrimaryStage()); + File file = f.showSaveDialog(ApplicationState.getInstance().getPrimaryStage()); if (file == null) { return null; } @@ -77,7 +78,7 @@ public class FileUtils { return file; } } else { - return f.showOpenDialog(Application.getPrimaryStage()); + return f.showOpenDialog(ApplicationState.getInstance().getPrimaryStage()); } } diff --git a/OutlawEditor/src/main/java/org/badvision/outlaweditor/GlobalEditor.java b/OutlawEditor/src/main/java/org/badvision/outlaweditor/GlobalEditor.java index 1bfbc9c6..b374c22d 100644 --- a/OutlawEditor/src/main/java/org/badvision/outlaweditor/GlobalEditor.java +++ b/OutlawEditor/src/main/java/org/badvision/outlaweditor/GlobalEditor.java @@ -11,6 +11,8 @@ package org.badvision.outlaweditor; import javafx.scene.layout.Pane; +import org.badvision.outlaweditor.api.ApplicationState; +import org.badvision.outlaweditor.data.xml.GameData; import org.badvision.outlaweditor.data.xml.Global; import org.badvision.outlaweditor.data.xml.Script; import org.badvision.outlaweditor.data.xml.Scripts; @@ -19,17 +21,18 @@ public class GlobalEditor extends Editor{ @Override public void addScript(Script script) { - Scripts scripts = Application.gameData.getGlobal().getScripts(); + GameData gameData = getGameData(); + Scripts scripts = gameData.getGlobal().getScripts(); if (scripts == null) { - Application.gameData.getGlobal().setScripts(new Scripts()); - scripts = Application.gameData.getGlobal().getScripts(); + gameData.getGlobal().setScripts(new Scripts()); + scripts = gameData.getGlobal().getScripts(); } scripts.getScript().add(script); } public void removeScript(Script script) { - Scripts scripts = Application.gameData.getGlobal().getScripts(); + Scripts scripts = getGameData().getGlobal().getScripts(); scripts.getScript().remove(script); } @@ -82,5 +85,9 @@ public class GlobalEditor extends Editor{ public void observedObjectChanged(Global object) { throw new UnsupportedOperationException("Not supported."); //To change body of generated methods, choose Tools | Templates. } + + private GameData getGameData() { + return ApplicationState.getInstance().getGameData(); + } } diff --git a/OutlawEditor/src/main/java/org/badvision/outlaweditor/MapEditor.java b/OutlawEditor/src/main/java/org/badvision/outlaweditor/MapEditor.java index 29c55288..3d8a9470 100644 --- a/OutlawEditor/src/main/java/org/badvision/outlaweditor/MapEditor.java +++ b/OutlawEditor/src/main/java/org/badvision/outlaweditor/MapEditor.java @@ -9,7 +9,6 @@ */ package org.badvision.outlaweditor; -import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -43,7 +42,7 @@ import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; import javafx.stage.Stage; -import static org.badvision.outlaweditor.Application.currentPlatform; +import org.badvision.outlaweditor.api.ApplicationState; import org.badvision.outlaweditor.data.TileMap; import org.badvision.outlaweditor.data.TileUtils; import org.badvision.outlaweditor.data.xml.Map; @@ -67,8 +66,8 @@ public class MapEditor extends Editor implements EventH double zoom = 1.0; DrawMode drawMode = DrawMode.Pencil1px; TileMap currentMap; - double tileWidth = currentPlatform.tileRenderer.getWidth() * zoom; - double tileHeight = currentPlatform.tileRenderer.getHeight() * zoom; + double tileWidth = getCurrentPlatform().tileRenderer.getWidth() * zoom; + double tileHeight = getCurrentPlatform().tileRenderer.getHeight() * zoom; @Override protected void onEntityUpdated() { @@ -115,7 +114,7 @@ public class MapEditor extends Editor implements EventH @Override public void buildEditorUI(Pane tileEditorAnchorPane) { anchorPane = tileEditorAnchorPane; - Application.getPrimaryStage().getScene().addEventHandler(KeyEvent.KEY_PRESSED, this::keyPressed); + ApplicationState.getInstance().getPrimaryStage().getScene().addEventHandler(KeyEvent.KEY_PRESSED, this::keyPressed); initCanvas(); redraw(); } @@ -127,7 +126,7 @@ public class MapEditor extends Editor implements EventH if (currentTile != null) { category = currentTile.getCategory(); } - if (this.equals(Application.getInstance().getController().getVisibleEditor())) { + if (this.equals(ApplicationState.getInstance().getController().getVisibleEditor())) { UIAction.showTileSelectModal(anchorPane, category, this::setCurrentTile); } } @@ -138,8 +137,8 @@ public class MapEditor extends Editor implements EventH anchorPane.getChildren().remove(drawCanvas); } drawCanvas = new Canvas(); - drawCanvas.heightProperty().bind(Application.getPrimaryStage().heightProperty().subtract(120)); - drawCanvas.widthProperty().bind(Application.getPrimaryStage().widthProperty().subtract(200)); + drawCanvas.heightProperty().bind(ApplicationState.getInstance().getPrimaryStage().heightProperty().subtract(120)); + drawCanvas.widthProperty().bind(ApplicationState.getInstance().getPrimaryStage().widthProperty().subtract(200)); // drawCanvas.widthProperty().bind(anchorPane.widthProperty()); drawCanvas.widthProperty().addListener((ObservableValue ov, Number t, Number t1) -> { redraw(); @@ -226,8 +225,8 @@ public class MapEditor extends Editor implements EventH // // double newLeft = (left + pointerX) * ratio - pointerX; // double newTop = (top + pointerY) * ratio - pointerY; - tileWidth = currentPlatform.tileRenderer.getWidth() * zoom; - tileHeight = currentPlatform.tileRenderer.getHeight() * zoom; + tileWidth = getCurrentPlatform().tileRenderer.getWidth() * zoom; + tileHeight = getCurrentPlatform().tileRenderer.getHeight() * zoom; redraw(); } @@ -300,7 +299,7 @@ public class MapEditor extends Editor implements EventH double xx = x * tileWidth; double yy = y * tileHeight; if (tile != null) { - drawCanvas.getGraphicsContext2D().drawImage(TileUtils.getImage(tile, currentPlatform), xx, yy, tileWidth, tileHeight); + drawCanvas.getGraphicsContext2D().drawImage(TileUtils.getImage(tile, getCurrentPlatform()), xx, yy, tileWidth, tileHeight); } } @@ -398,7 +397,7 @@ public class MapEditor extends Editor implements EventH drawCanvas.heightProperty().unbind(); anchorPane.getChildren().remove(drawCanvas); currentMap.updateBackingMap(); - Application.getPrimaryStage().getScene().removeEventHandler(KeyEvent.KEY_PRESSED, this::keyPressed); + ApplicationState.getInstance().getPrimaryStage().getScene().removeEventHandler(KeyEvent.KEY_PRESSED, this::keyPressed); } /** @@ -417,14 +416,14 @@ public class MapEditor extends Editor implements EventH drawMode = DrawMode.Pencil1px; } this.currentTile = currentTile; - ImageCursor cursor = new ImageCursor(TileUtils.getImage(currentTile, currentPlatform), 2, 2); + ImageCursor cursor = new ImageCursor(TileUtils.getImage(currentTile, getCurrentPlatform()), 2, 2); drawCanvas.setCursor(cursor); return currentTile; } public void showPreview() { - byte[] data = currentPlatform.imageRenderer.renderPreview(currentMap, posX, posY, currentPlatform.maxImageWidth, currentPlatform.maxImageHeight); - WritableImage img = currentPlatform.imageRenderer.renderImage(null, data, currentPlatform.maxImageWidth, currentPlatform.maxImageHeight); + byte[] data = getCurrentPlatform().imageRenderer.renderPreview(currentMap, posX, posY, getCurrentPlatform().maxImageWidth, getCurrentPlatform().maxImageHeight); + WritableImage img = getCurrentPlatform().imageRenderer.renderImage(null, data, getCurrentPlatform().maxImageWidth, getCurrentPlatform().maxImageHeight); Stage stage = new Stage(); stage.setTitle("Preview"); ImageView imgView = new ImageView(img); @@ -435,12 +434,12 @@ public class MapEditor extends Editor implements EventH @Override public void copy() { - byte[] data = currentPlatform.imageRenderer.renderPreview(currentMap, posX, posY, currentPlatform.maxImageWidth, currentPlatform.maxImageHeight); - WritableImage img = currentPlatform.imageRenderer.renderImage(null, data, currentPlatform.maxImageWidth, currentPlatform.maxImageHeight); + byte[] data = getCurrentPlatform().imageRenderer.renderPreview(currentMap, posX, posY, getCurrentPlatform().maxImageWidth, getCurrentPlatform().maxImageHeight); + WritableImage img = getCurrentPlatform().imageRenderer.renderImage(null, data, getCurrentPlatform().maxImageWidth, getCurrentPlatform().maxImageHeight); java.util.Map clip = new HashMap<>(); clip.put(DataFormat.IMAGE, img); if (drawMode != DrawMode.Select || startX >= 0) { - clip.put(DataFormat.PLAIN_TEXT, "selection/map/" + Application.gameData.getMap().indexOf(getEntity()) + "/" + getSelectionInfo()); + clip.put(DataFormat.PLAIN_TEXT, "selection/map/" + ApplicationState.getInstance().getGameData().getMap().indexOf(getEntity()) + "/" + getSelectionInfo()); } Clipboard.getSystemClipboard().setContent(clip); clearSelection(); @@ -460,7 +459,7 @@ public class MapEditor extends Editor implements EventH java.util.Map selection = TransferHelper.getSelectionDetails(clipboardInfo); if (selection.containsKey("map")) { trackState(); - Map sourceMap = Application.gameData.getMap().get(selection.get("map")); + Map sourceMap = ApplicationState.getInstance().getGameData().getMap().get(selection.get("map")); TileMap source = getCurrentMap(); if (!sourceMap.equals(getCurrentMap().getBackingMap())) { source = new TileMap(sourceMap); @@ -507,6 +506,10 @@ public class MapEditor extends Editor implements EventH redraw(); } + private org.badvision.outlaweditor.api.Platform getCurrentPlatform() { + return ApplicationState.getInstance().getCurrentPlatform(); + } + public static enum DrawMode { Pencil1px, Pencil3px, Pencil5px, FilledRect, Eraser(false), Select(false); diff --git a/OutlawEditor/src/main/java/org/badvision/outlaweditor/MythosEditor.java b/OutlawEditor/src/main/java/org/badvision/outlaweditor/MythosEditor.java index ff1fee4e..f4c6f475 100644 --- a/OutlawEditor/src/main/java/org/badvision/outlaweditor/MythosEditor.java +++ b/OutlawEditor/src/main/java/org/badvision/outlaweditor/MythosEditor.java @@ -35,6 +35,7 @@ import javax.xml.namespace.QName; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; +import org.badvision.outlaweditor.api.ApplicationState; import org.badvision.outlaweditor.data.xml.Block; import org.badvision.outlaweditor.data.xml.Global; import org.badvision.outlaweditor.data.xml.Mutation; @@ -192,7 +193,7 @@ public class MythosEditor { } public static Scope getGlobalScope() { - return Application.gameData.getGlobal(); + return ApplicationState.getInstance().getGameData().getGlobal(); } private boolean isGlobalScope() { diff --git a/OutlawEditor/src/main/java/org/badvision/outlaweditor/api/ApplicationState.java b/OutlawEditor/src/main/java/org/badvision/outlaweditor/api/ApplicationState.java index cee44872..0537b88a 100644 --- a/OutlawEditor/src/main/java/org/badvision/outlaweditor/api/ApplicationState.java +++ b/OutlawEditor/src/main/java/org/badvision/outlaweditor/api/ApplicationState.java @@ -15,16 +15,43 @@ */ package org.badvision.outlaweditor.api; +import javafx.stage.Stage; +import org.badvision.outlaweditor.Application; import org.badvision.outlaweditor.data.xml.GameData; import org.badvision.outlaweditor.ui.ApplicationUIController; +import org.osgi.framework.BundleContext; +import org.osgi.framework.FrameworkUtil; /** - * + * * @author blurry */ public interface ApplicationState { public GameData getGameData(); + + public void setGameData(GameData newData); + public ApplicationUIController getApplicationUI(); + public Platform getCurrentPlatform(); + + public void setCurrentPlatform(Platform p); + + public ApplicationUIController getController(); + + public Stage getPrimaryStage(); + + static public BundleContext getBundleContext() { + if (Application.felix != null) { + return Application.felix.getBundleContext(); + } else { + return FrameworkUtil.getBundle(ApplicationState.class).getBundleContext(); + } + } + public static ApplicationState getInstance() { + BundleContext bc = getBundleContext(); + return bc.getService(bc.getServiceReference(ApplicationState.class)); + } + } diff --git a/OutlawEditor/src/main/java/org/badvision/outlaweditor/api/MenuAction.java b/OutlawEditor/src/main/java/org/badvision/outlaweditor/api/MenuAction.java new file mode 100644 index 00000000..d936ae04 --- /dev/null +++ b/OutlawEditor/src/main/java/org/badvision/outlaweditor/api/MenuAction.java @@ -0,0 +1,31 @@ +/* + * Copyright 2016 org.badvision. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.badvision.outlaweditor.api; + +import javafx.event.ActionEvent; +import javafx.event.EventHandler; + +/** + * Any ser + * @author blurry + */ +public interface MenuAction extends EventHandler { + public String getName(); + + default public String getDescription() { + return getName(); + } +} diff --git a/OutlawEditor/src/main/java/org/badvision/outlaweditor/apple/AppleImageEditor.java b/OutlawEditor/src/main/java/org/badvision/outlaweditor/apple/AppleImageEditor.java index dade59cc..5f0afe82 100644 --- a/OutlawEditor/src/main/java/org/badvision/outlaweditor/apple/AppleImageEditor.java +++ b/OutlawEditor/src/main/java/org/badvision/outlaweditor/apple/AppleImageEditor.java @@ -28,11 +28,11 @@ import javafx.scene.input.MouseEvent; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; -import org.badvision.outlaweditor.Application; import org.badvision.outlaweditor.FileUtils; import org.badvision.outlaweditor.ImageEditor; -import org.badvision.outlaweditor.api.Platform; import org.badvision.outlaweditor.TransferHelper; +import org.badvision.outlaweditor.api.ApplicationState; +import org.badvision.outlaweditor.api.Platform; import org.badvision.outlaweditor.data.TileMap; import org.badvision.outlaweditor.data.xml.Image; import org.badvision.outlaweditor.data.xml.PlatformData; @@ -435,7 +435,7 @@ public class AppleImageEditor extends ImageEditor implements EventHandler clip = new HashMap<>(); clip.put(DataFormat.IMAGE, currentImage); - clip.put(DataFormat.PLAIN_TEXT, "selection/image/" + Application.gameData.getImage().indexOf(getEntity()) + "/" + getSelectionInfo()); + clip.put(DataFormat.PLAIN_TEXT, "selection/image/" + ApplicationState.getInstance().getGameData().getImage().indexOf(getEntity()) + "/" + getSelectionInfo()); Clipboard.getSystemClipboard().setContent(clip); copyData = Arrays.copyOf(getImageData(), getImageData().length); } @@ -460,7 +460,7 @@ public class AppleImageEditor extends ImageEditor implements EventHandler> " + contentPath); Map selection = TransferHelper.getSelectionDetails(contentPath); if (selection.containsKey("map")) { - TileMap map = new TileMap(Application.gameData.getMap().get(selection.get("map"))); + TileMap map = new TileMap(ApplicationState.getInstance().getGameData().getMap().get(selection.get("map"))); byte[] buf = getPlatform().imageRenderer.renderPreview( map, selection.get("x1"), @@ -471,7 +471,7 @@ public class AppleImageEditor extends ImageEditor implements EventHandler> implements Serializable }); } locationScripts.remove(loc); - Application.getInstance().getController().redrawScripts(); + ApplicationState.getInstance().getController().redrawScripts(); } private void registerLocationScript(int x, int y, Script s) { @@ -104,7 +105,7 @@ public class TileMap extends ArrayList> implements Serializable locationScripts.put(loc, list); } list.add(s); - Application.getInstance().getController().redrawScripts(); + ApplicationState.getInstance().getController().redrawScripts(); } private int getMortonNumber(int x, int y) { @@ -195,7 +196,7 @@ public class TileMap extends ArrayList> implements Serializable if (t == null) { t = new Tile(); unknownTiles.add(t); - Platform p = Application.currentPlatform; + Platform p = ApplicationState.getInstance().getCurrentPlatform(); WritableImage img = UIAction.getBadImage(p.tileRenderer.getWidth(), p.tileRenderer.getHeight()); TileUtils.setImage(t, p, img); } diff --git a/OutlawEditor/src/main/java/org/badvision/outlaweditor/data/TilesetUtils.java b/OutlawEditor/src/main/java/org/badvision/outlaweditor/data/TilesetUtils.java index 0b9e3a87..c6d07d07 100644 --- a/OutlawEditor/src/main/java/org/badvision/outlaweditor/data/TilesetUtils.java +++ b/OutlawEditor/src/main/java/org/badvision/outlaweditor/data/TilesetUtils.java @@ -13,7 +13,7 @@ package org.badvision.outlaweditor.data; import java.io.Serializable; import java.util.HashMap; import java.util.Map; -import org.badvision.outlaweditor.Application; +import org.badvision.outlaweditor.api.ApplicationState; import org.badvision.outlaweditor.data.xml.Tile; /** @@ -27,18 +27,18 @@ public class TilesetUtils implements Serializable { } public static boolean add(Tile e) { - boolean output = Application.gameData.getTile().add(e); + boolean output = ApplicationState.getInstance().getGameData().getTile().add(e); DataProducer.notifyObservers(TilesetUtils.class); return output; } public static void clear() { - Application.gameData.getTile().clear(); + ApplicationState.getInstance().getGameData().getTile().clear(); DataProducer.notifyObservers(TilesetUtils.class); } public static void remove(Tile t) { - Application.gameData.getTile().remove(t); + ApplicationState.getInstance().getGameData().getTile().remove(t); DataProducer.notifyObservers(TilesetUtils.class); } // The tileset should have been a map in retrospect but now it has to @@ -48,7 +48,7 @@ public class TilesetUtils implements Serializable { public static Tile getTileById(String tileId) { if (lookup == null || (lookup.get(tileId) == null && !lookup.containsKey(tileId))) { lookup = new HashMap<>(); - for (Tile t : Application.gameData.getTile()) { + for (Tile t : ApplicationState.getInstance().getGameData().getTile()) { lookup.put(TileUtils.getId(t), t); } if (lookup.get(tileId) == null) { @@ -57,4 +57,7 @@ public class TilesetUtils implements Serializable { } return lookup.get(tileId); } + + private TilesetUtils() { + } } diff --git a/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/ApplicationMenuController.java b/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/ApplicationMenuController.java index 08087df2..0b2b8605 100644 --- a/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/ApplicationMenuController.java +++ b/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/ApplicationMenuController.java @@ -12,6 +12,7 @@ package org.badvision.outlaweditor.ui; import javafx.event.ActionEvent; import javafx.fxml.FXML; +import javafx.scene.control.Menu; /** * @@ -19,6 +20,9 @@ import javafx.fxml.FXML; */ public abstract class ApplicationMenuController { + @FXML + protected Menu extraMenu; + @FXML abstract public void onChangePlatformAppleDHGRSolid(ActionEvent event); @@ -60,5 +64,6 @@ public abstract class ApplicationMenuController { @FXML abstract public void performUndo(ActionEvent event); - + + abstract public void initalize(); } diff --git a/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/ApplicationUIController.java b/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/ApplicationUIController.java index 582c86da..6d0ab7f3 100644 --- a/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/ApplicationUIController.java +++ b/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/ApplicationUIController.java @@ -14,13 +14,13 @@ import java.net.URL; import java.util.ResourceBundle; import javafx.event.Event; import javafx.fxml.FXML; -import org.badvision.outlaweditor.Application; import org.badvision.outlaweditor.Editor; +import org.badvision.outlaweditor.api.ApplicationState; public abstract class ApplicationUIController { public static ApplicationUIController getController() { - return Application.getInstance().getController(); + return ApplicationState.getInstance().getController(); } abstract public void rebuildTileSelectors(); diff --git a/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/UIAction.java b/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/UIAction.java index 3751e1fc..977a0d77 100644 --- a/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/UIAction.java +++ b/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/UIAction.java @@ -62,10 +62,10 @@ import javafx.util.Duration; import javafx.util.converter.DefaultStringConverter; import javax.xml.bind.JAXB; import org.badvision.outlaweditor.Application; -import static org.badvision.outlaweditor.Application.currentPlatform; import org.badvision.outlaweditor.FileUtils; import org.badvision.outlaweditor.MythosEditor; import org.badvision.outlaweditor.SheetEditor; +import org.badvision.outlaweditor.api.ApplicationState; import org.badvision.outlaweditor.apple.ImageDitherEngine; import org.badvision.outlaweditor.data.DataUtilities; import org.badvision.outlaweditor.data.TileUtils; @@ -131,7 +131,7 @@ public class UIAction { GameData newData = JAXB.unmarshal(currentSaveFile, GameData.class); ApplicationUIController.getController().clearData(); TilesetUtils.clear(); - Application.gameData = newData; + ApplicationState.getInstance().setGameData(newData); DataUtilities.ensureGlobalExists(); DataUtilities.cleanupAllScriptNames(); ApplicationUIController.getController().updateSelectors(); @@ -151,7 +151,7 @@ public class UIAction { } if (currentSaveFile != null) { currentSaveFile.delete(); - JAXB.marshal(Application.gameData, currentSaveFile); + JAXB.marshal(ApplicationState.getInstance().getGameData(), currentSaveFile); } break; } @@ -299,10 +299,10 @@ public class UIAction { public static void createAndEditUserType() throws IntrospectionException { UserType type = new UserType(); if (editAndGetUserType(type).isPresent()) { - if (Application.gameData.getGlobal().getUserTypes() == null) { - Application.gameData.getGlobal().setUserTypes(new Global.UserTypes()); + if (ApplicationState.getInstance().getGameData().getGlobal().getUserTypes() == null) { + ApplicationState.getInstance().getGameData().getGlobal().setUserTypes(new Global.UserTypes()); } - Application.gameData.getGlobal().getUserTypes().getUserType().add(type); + ApplicationState.getInstance().getGameData().getGlobal().getUserTypes().getUserType().add(type); } } @@ -345,10 +345,10 @@ public class UIAction { public static Sheet createAndEditSheet() throws IntrospectionException { Sheet sheet = new Sheet(); sheet.setName("New Sheet"); - if (Application.gameData.getGlobal().getSheets() == null) { - Application.gameData.getGlobal().setSheets(new Global.Sheets()); + if (ApplicationState.getInstance().getGameData().getGlobal().getSheets() == null) { + ApplicationState.getInstance().getGameData().getGlobal().setSheets(new Global.Sheets()); } - Application.gameData.getGlobal().getSheets().getSheet().add(sheet); + ApplicationState.getInstance().getGameData().getGlobal().getSheets().getSheet().add(sheet); return editSheet(sheet); } @@ -392,10 +392,10 @@ public class UIAction { } currentTileSelector = new AnchorPane(); - int TILE_WIDTH = Application.currentPlatform.tileRenderer.getWidth(); - int TILE_HEIGHT = Application.currentPlatform.tileRenderer.getHeight(); + int TILE_WIDTH = ApplicationState.getInstance().getCurrentPlatform().tileRenderer.getWidth(); + int TILE_HEIGHT = ApplicationState.getInstance().getCurrentPlatform().tileRenderer.getHeight(); - List tiles = Application.gameData.getTile().stream().filter((Tile t) -> { + List tiles = ApplicationState.getInstance().getGameData().getTile().stream().filter((Tile t) -> { return category == null || t.getCategory().equals(category); }).collect(Collectors.toList()); @@ -406,7 +406,7 @@ public class UIAction { currentTileSelector.setPrefHeight(Math.min(numRows * (TILE_HEIGHT + GRID_SPACING) + GRID_SPACING, prefWidth)); for (int i = 0; i < tiles.size(); i++) { final Tile tile = tiles.get(i); - ImageView tileIcon = new ImageView(TileUtils.getImage(tile, currentPlatform)); + ImageView tileIcon = new ImageView(TileUtils.getImage(tile, ApplicationState.getInstance().getCurrentPlatform())); currentTileSelector.getChildren().add(tileIcon); tileIcon.setOnMouseClicked((e) -> { e.consume(); @@ -442,8 +442,8 @@ public class UIAction { null))); currentTileSelector.setEffect(new DropShadow(5.0, 1.0, 1.0, Color.BLACK)); anchorPane.getChildren().add(currentTileSelector); - Application.getPrimaryStage().getScene().addEventHandler(KeyEvent.KEY_PRESSED, cancelTileSelectKeyHandler); - Application.getPrimaryStage().getScene().addEventFilter(MouseEvent.MOUSE_PRESSED, cancelTileSelectMouseHandler); + ApplicationState.getInstance().getPrimaryStage().getScene().addEventHandler(KeyEvent.KEY_PRESSED, cancelTileSelectKeyHandler); + ApplicationState.getInstance().getPrimaryStage().getScene().addEventFilter(MouseEvent.MOUSE_PRESSED, cancelTileSelectMouseHandler); } private static final EventHandler cancelTileSelectMouseHandler = (MouseEvent e) -> { @@ -460,8 +460,8 @@ public class UIAction { }; public static void closeCurrentTileSelector() { - Application.getPrimaryStage().getScene().removeEventHandler(KeyEvent.KEY_PRESSED, cancelTileSelectKeyHandler); - Application.getPrimaryStage().getScene().removeEventFilter(MouseEvent.MOUSE_PRESSED, cancelTileSelectMouseHandler); + ApplicationState.getInstance().getPrimaryStage().getScene().removeEventHandler(KeyEvent.KEY_PRESSED, cancelTileSelectKeyHandler); + ApplicationState.getInstance().getPrimaryStage().getScene().removeEventFilter(MouseEvent.MOUSE_PRESSED, cancelTileSelectMouseHandler); fadeOut(currentTileSelector, (ActionEvent ev) -> { if (currentTileSelector != null) { diff --git a/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/impl/ApplicationMenuControllerImpl.java b/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/impl/ApplicationMenuControllerImpl.java index 160efb42..1767900d 100644 --- a/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/impl/ApplicationMenuControllerImpl.java +++ b/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/impl/ApplicationMenuControllerImpl.java @@ -14,45 +14,54 @@ import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; import javafx.event.ActionEvent; +import javafx.scene.control.MenuItem; import org.badvision.outlaweditor.Application; import org.badvision.outlaweditor.Editor; +import org.badvision.outlaweditor.api.ApplicationState; +import org.badvision.outlaweditor.api.MenuAction; import org.badvision.outlaweditor.api.Platform; import org.badvision.outlaweditor.apple.AppleTileRenderer; import org.badvision.outlaweditor.ui.ApplicationMenuController; import org.badvision.outlaweditor.ui.ApplicationUIController; import org.badvision.outlaweditor.ui.UIAction; +import org.osgi.framework.BundleContext; +import org.osgi.framework.InvalidSyntaxException; /** * * @author blurry */ public class ApplicationMenuControllerImpl extends ApplicationMenuController { + @Override + public void initalize() { + setupPluginMenu(); + } @Override public void onChangePlatformAppleSolid(ActionEvent event) { AppleTileRenderer.useSolidPalette = true; - Application.currentPlatform = Platform.AppleII; + ApplicationState.getInstance().setCurrentPlatform(Platform.AppleII); ApplicationUIController.getController().platformChange(); } @Override public void onChangePlatformAppleText(ActionEvent event) { AppleTileRenderer.useSolidPalette = false; - Application.currentPlatform = Platform.AppleII; + ApplicationState.getInstance().setCurrentPlatform(Platform.AppleII); ApplicationUIController.getController().platformChange(); } @Override public void onChangePlatformAppleDHGRSolid(ActionEvent event) { AppleTileRenderer.useSolidPalette = true; - Application.currentPlatform = Platform.AppleII_DHGR; + ApplicationState.getInstance().setCurrentPlatform(Platform.AppleII_DHGR); ApplicationUIController.getController().platformChange(); } @Override public void onChangePlatformAppleDHGRText(ActionEvent event) { AppleTileRenderer.useSolidPalette = false; - Application.currentPlatform = Platform.AppleII_DHGR; + ApplicationState.getInstance().setCurrentPlatform(Platform.AppleII_DHGR); ApplicationUIController.getController().platformChange(); } @@ -136,4 +145,19 @@ public class ApplicationMenuControllerImpl extends ApplicationMenuController { } } + private void setupPluginMenu() { + System.out.println("Setting up extras menu"); + + BundleContext bc = ApplicationState.getBundleContext(); + try { + bc.getServiceReferences(MenuAction.class, null).stream().map(bc::getService).forEach((MenuAction a) -> { + System.out.println("Adding menu item " + a.getName()); + MenuItem item = new MenuItem(a.getName()); + item.setOnAction(a); + extraMenu.getItems().add(item); + }); + } catch (InvalidSyntaxException ex) { + Logger.getLogger(ApplicationUIControllerImpl.class.getName()).log(Level.SEVERE, null, ex); + } + } } diff --git a/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/impl/ApplicationUIControllerImpl.java b/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/impl/ApplicationUIControllerImpl.java index 82d52398..534bf435 100644 --- a/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/impl/ApplicationUIControllerImpl.java +++ b/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/impl/ApplicationUIControllerImpl.java @@ -12,8 +12,8 @@ package org.badvision.outlaweditor.ui.impl; import javafx.event.Event; import javafx.scene.input.DataFormat; -import org.badvision.outlaweditor.Application; import org.badvision.outlaweditor.Editor; +import org.badvision.outlaweditor.api.ApplicationState; import org.badvision.outlaweditor.data.TileUtils; import org.badvision.outlaweditor.data.TilesetUtils; import org.badvision.outlaweditor.data.xml.Tile; @@ -30,18 +30,19 @@ public class ApplicationUIControllerImpl extends ApplicationUIController { public void initialize() { super.initialize(); - TilesetUtils.addObserver((org.badvision.outlaweditor.data.DataObserver) (Object object) -> { + TilesetUtils.addObserver((Object object) -> { rebuildTileSelectors(); }); tileController.initalize(); mapController.initalize(); imageController.initalize(); globalController.initialize(); + menuController.initalize(); } @Override public void platformChange() { - Application.gameData.getTile().stream().forEach((t) -> { + ApplicationState.getInstance().getGameData().getTile().stream().forEach((t) -> { TileUtils.redrawTile(t); }); Tile tile = tileController.getCurrentTile(); diff --git a/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/impl/GlobalEditorTabControllerImpl.java b/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/impl/GlobalEditorTabControllerImpl.java index 4f579f24..5bf125e9 100644 --- a/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/impl/GlobalEditorTabControllerImpl.java +++ b/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/impl/GlobalEditorTabControllerImpl.java @@ -19,8 +19,8 @@ import javafx.scene.control.Tooltip; import javafx.scene.text.Font; import javafx.scene.text.FontWeight; import javax.xml.bind.JAXBException; -import org.badvision.outlaweditor.Application; import org.badvision.outlaweditor.TransferHelper; +import org.badvision.outlaweditor.api.ApplicationState; import org.badvision.outlaweditor.data.DataUtilities; import org.badvision.outlaweditor.data.xml.Script; import org.badvision.outlaweditor.data.xml.Sheet; @@ -52,7 +52,7 @@ public class GlobalEditorTabControllerImpl extends GlobalEditorTabController { @Override public void startEdit() { - UIAction.editVariable(getItem(), Application.gameData.getGlobal()); + UIAction.editVariable(getItem(), ApplicationState.getInstance().getGameData().getGlobal()); cancelEdit(); updateItem(getItem(), false); } @@ -71,7 +71,7 @@ public class GlobalEditorTabControllerImpl extends GlobalEditorTabController { @Override public void startEdit() { - UIAction.editScript(getItem(), Application.gameData.getGlobal()); + UIAction.editScript(getItem(), ApplicationState.getInstance().getGameData().getGlobal()); cancelEdit(); updateItem(getItem(), false); } @@ -124,7 +124,7 @@ public class GlobalEditorTabControllerImpl extends GlobalEditorTabController { @Override protected void onScriptAddPressed(ActionEvent event) { - UIAction.createAndEditScript(Application.gameData.getGlobal()); + UIAction.createAndEditScript(ApplicationState.getInstance().getGameData().getGlobal()); } @Override @@ -153,7 +153,7 @@ public class GlobalEditorTabControllerImpl extends GlobalEditorTabController { Script script = TransferHelper.cloneObject(source, Script.class, "script"); script.setName(source.getName() + " CLONE"); getCurrentEditor().addScript(script); - editScript(script, Application.gameData.getGlobal()); + editScript(script, ApplicationState.getInstance().getGameData().getGlobal()); } catch (JAXBException ex) { Logger.getLogger(MapEditorTabControllerImpl.class.getName()).log(Level.SEVERE, null, ex); UIAction.alert("Error occured when attempting clone operation:\n" + ex.getMessage()); @@ -180,7 +180,7 @@ public class GlobalEditorTabControllerImpl extends GlobalEditorTabController { + type.getName() + "? There is no undo for this!", () -> { - Application.gameData.getGlobal().getUserTypes().getUserType().remove(type); + ApplicationState.getInstance().getGameData().getGlobal().getUserTypes().getUserType().remove(type); redrawGlobalDataTypes(); }, null); } @@ -197,7 +197,7 @@ public class GlobalEditorTabControllerImpl extends GlobalEditorTabController { UserType newType = TransferHelper.cloneObject(source, UserType.class, "userType"); newType.setName(source.getName() + " CLONE"); if (UIAction.editAndGetUserType(newType) != null) { - Application.gameData.getGlobal().getUserTypes().getUserType().add(newType); + ApplicationState.getInstance().getGameData().getGlobal().getUserTypes().getUserType().add(newType); redrawGlobalDataTypes(); } } catch (JAXBException ex) { @@ -229,7 +229,7 @@ public class GlobalEditorTabControllerImpl extends GlobalEditorTabController { + sheet.getName() + "? There is no undo for this!", () -> { - Application.gameData.getGlobal().getSheets().getSheet().remove(sheet); + ApplicationState.getInstance().getGameData().getGlobal().getSheets().getSheet().remove(sheet); redrawGlobalSheets(); }, null); } @@ -246,7 +246,7 @@ public class GlobalEditorTabControllerImpl extends GlobalEditorTabController { Sheet sheet = TransferHelper.cloneObject(source, Sheet.class, "sheet"); sheet.setName(source.getName() + " CLONE"); Sheet newVar = UIAction.editSheet(sheet); - Application.gameData.getGlobal().getSheets().getSheet().add(newVar); + ApplicationState.getInstance().getGameData().getGlobal().getSheets().getSheet().add(newVar); redrawGlobalSheets(); } catch (JAXBException ex) { Logger.getLogger(GlobalEditorTabControllerImpl.class.getName()).log(Level.SEVERE, null, ex); @@ -258,7 +258,7 @@ public class GlobalEditorTabControllerImpl extends GlobalEditorTabController { @Override protected void onVariableAddPressed(ActionEvent event) { try { - UIAction.createAndEditVariable(Application.gameData.getGlobal()); + UIAction.createAndEditVariable(ApplicationState.getInstance().getGameData().getGlobal()); redrawGlobalVariables(); } catch (IntrospectionException ex) { Logger.getLogger(GlobalEditorTabControllerImpl.class.getName()).log(Level.SEVERE, null, ex); @@ -274,7 +274,7 @@ public class GlobalEditorTabControllerImpl extends GlobalEditorTabController { + var.getName() + "? There is no undo for this!", () -> { - Application.gameData.getGlobal().getVariables().getVariable().remove(var); + ApplicationState.getInstance().getGameData().getGlobal().getVariables().getVariable().remove(var); redrawGlobalVariables(); }, null); } @@ -292,7 +292,7 @@ public class GlobalEditorTabControllerImpl extends GlobalEditorTabController { variable.setName(source.getName() + " CLONE"); Optional newVar = UIAction.editAndGetVariable(variable); if (newVar.isPresent()) { - Application.gameData.getGlobal().getVariables().getVariable().add(newVar.get()); + ApplicationState.getInstance().getGameData().getGlobal().getVariables().getVariable().add(newVar.get()); redrawGlobalVariables(); } } catch (JAXBException ex) { @@ -307,9 +307,9 @@ public class GlobalEditorTabControllerImpl extends GlobalEditorTabController { @Override public void redrawGlobalScripts() { DataUtilities.ensureGlobalExists(); - if (globalScriptList.getItems() != null && Application.gameData.getGlobal().getScripts() != null) { - DataUtilities.sortNamedEntities(Application.gameData.getGlobal().getScripts().getScript()); - globalScriptList.getItems().setAll(Application.gameData.getGlobal().getScripts().getScript()); + if (globalScriptList.getItems() != null && ApplicationState.getInstance().getGameData().getGlobal().getScripts() != null) { + DataUtilities.sortNamedEntities(ApplicationState.getInstance().getGameData().getGlobal().getScripts().getScript()); + globalScriptList.getItems().setAll(ApplicationState.getInstance().getGameData().getGlobal().getScripts().getScript()); } else { globalScriptList.getItems().clear(); } @@ -318,9 +318,9 @@ public class GlobalEditorTabControllerImpl extends GlobalEditorTabController { @Override public void redrawGlobalVariables() { DataUtilities.ensureGlobalExists(); - if (variableList.getItems() != null && Application.gameData.getGlobal().getVariables() != null) { - DataUtilities.sortNamedEntities(Application.gameData.getGlobal().getVariables().getVariable()); - variableList.getItems().setAll(Application.gameData.getGlobal().getVariables().getVariable()); + if (variableList.getItems() != null && ApplicationState.getInstance().getGameData().getGlobal().getVariables() != null) { + DataUtilities.sortNamedEntities(ApplicationState.getInstance().getGameData().getGlobal().getVariables().getVariable()); + variableList.getItems().setAll(ApplicationState.getInstance().getGameData().getGlobal().getVariables().getVariable()); } else { variableList.getItems().clear(); } @@ -329,9 +329,9 @@ public class GlobalEditorTabControllerImpl extends GlobalEditorTabController { @Override public void redrawGlobalDataTypes() { DataUtilities.ensureGlobalExists(); - if (dataTypeList.getItems() != null && Application.gameData.getGlobal().getUserTypes() != null) { - DataUtilities.sortNamedEntities(Application.gameData.getGlobal().getUserTypes().getUserType()); - dataTypeList.getItems().setAll(Application.gameData.getGlobal().getUserTypes().getUserType()); + if (dataTypeList.getItems() != null && ApplicationState.getInstance().getGameData().getGlobal().getUserTypes() != null) { + DataUtilities.sortNamedEntities(ApplicationState.getInstance().getGameData().getGlobal().getUserTypes().getUserType()); + dataTypeList.getItems().setAll(ApplicationState.getInstance().getGameData().getGlobal().getUserTypes().getUserType()); } else { dataTypeList.getItems().clear(); } @@ -340,9 +340,9 @@ public class GlobalEditorTabControllerImpl extends GlobalEditorTabController { @Override public void redrawGlobalSheets() { DataUtilities.ensureGlobalExists(); - if (sheetList.getItems() != null && Application.gameData.getGlobal().getSheets() != null) { - DataUtilities.sortNamedEntities(Application.gameData.getGlobal().getSheets().getSheet()); - sheetList.getItems().setAll(Application.gameData.getGlobal().getSheets().getSheet()); + if (sheetList.getItems() != null && ApplicationState.getInstance().getGameData().getGlobal().getSheets() != null) { + DataUtilities.sortNamedEntities(ApplicationState.getInstance().getGameData().getGlobal().getSheets().getSheet()); + sheetList.getItems().setAll(ApplicationState.getInstance().getGameData().getGlobal().getSheets().getSheet()); } else { sheetList.getItems().clear(); } diff --git a/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/impl/ImageEditorTabControllerImpl.java b/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/impl/ImageEditorTabControllerImpl.java index 94298ffb..3b0fb6b4 100644 --- a/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/impl/ImageEditorTabControllerImpl.java +++ b/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/impl/ImageEditorTabControllerImpl.java @@ -20,10 +20,10 @@ import javafx.scene.control.ListView; import javafx.util.StringConverter; import javax.xml.bind.JAXBException; import org.badvision.outlaweditor.Application; -import static org.badvision.outlaweditor.Application.currentPlatform; import org.badvision.outlaweditor.Editor; import org.badvision.outlaweditor.ImageEditor; import org.badvision.outlaweditor.TransferHelper; +import org.badvision.outlaweditor.api.ApplicationState; import static org.badvision.outlaweditor.data.PropertyHelper.bind; import static org.badvision.outlaweditor.data.PropertyHelper.stringProp; import org.badvision.outlaweditor.data.xml.Image; @@ -149,7 +149,7 @@ public class ImageEditorTabControllerImpl extends ImageEditorTabController { } Image clone = TransferHelper.cloneObject(getCurrentImage(), Image.class, "image"); clone.setName(clone.getName()+" clone"); - Application.gameData.getImage().add(clone); + ApplicationState.getInstance().getGameData().getImage().add(clone); setCurrentImage(clone); rebuildImageSelector(); } catch (JAXBException ex) { @@ -161,7 +161,7 @@ public class ImageEditorTabControllerImpl extends ImageEditorTabController { public void onImageCreatePressed(ActionEvent event) { Image i = new Image(); i.setName("Untitled"); - Application.gameData.getImage().add(i); + ApplicationState.getInstance().getGameData().getImage().add(i); setCurrentImage(i); rebuildImageSelector(); } @@ -174,7 +174,7 @@ public class ImageEditorTabControllerImpl extends ImageEditorTabController { confirm("Delete image '" + currentImage.getName() + "'. Are you sure?", () -> { Image del = currentImage; setCurrentImage(null); - Application.gameData.getImage().remove(del); + ApplicationState.getInstance().getGameData().getImage().remove(del); rebuildImageSelector(); }, null); } @@ -231,7 +231,7 @@ public class ImageEditorTabControllerImpl extends ImageEditorTabController { Logger.getLogger(ApplicationUIControllerImpl.class.getName()).log(Level.SEVERE, null, ex); } try { - currentImageEditor = currentPlatform.imageEditor.newInstance(); + currentImageEditor = ApplicationState.getInstance().getCurrentPlatform().imageEditor.newInstance(); } catch (InstantiationException | IllegalAccessException ex) { Logger.getLogger(ApplicationUIControllerImpl.class.getName()).log(Level.SEVERE, null, ex); } @@ -262,7 +262,7 @@ public class ImageEditorTabControllerImpl extends ImageEditorTabController { public void rebuildImageSelector() { Image i = getCurrentImage(); imageSelector.getItems().clear(); - List allImages = Application.gameData.getImage(); + List allImages = ApplicationState.getInstance().getGameData().getImage(); allImages.sort((Image o1, Image o2) -> { int c1 = String.valueOf(o1.getCategory()).compareTo(String.valueOf(o2.getCategory())); if (c1 != 0) { diff --git a/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/impl/MapEditorTabControllerImpl.java b/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/impl/MapEditorTabControllerImpl.java index dde8f555..b087ca65 100644 --- a/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/impl/MapEditorTabControllerImpl.java +++ b/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/impl/MapEditorTabControllerImpl.java @@ -26,10 +26,9 @@ import javafx.scene.text.Font; import javafx.scene.text.FontWeight; import javax.xml.bind.JAXBException; import org.badvision.outlaweditor.Application; -import static org.badvision.outlaweditor.Application.currentPlatform; -import static org.badvision.outlaweditor.Application.gameData; import org.badvision.outlaweditor.MapEditor; import org.badvision.outlaweditor.TransferHelper; +import org.badvision.outlaweditor.api.ApplicationState; import org.badvision.outlaweditor.data.DataUtilities; import static org.badvision.outlaweditor.data.PropertyHelper.bind; import static org.badvision.outlaweditor.data.PropertyHelper.stringProp; @@ -121,7 +120,7 @@ public class MapEditorTabControllerImpl extends MapEditorTabController { m.setName("Untitled"); m.setWidth(512); m.setHeight(512); - gameData.getMap().add(m); + ApplicationState.getInstance().getGameData().getMap().add(m); rebuildMapSelectors(); setCurrentMap(m); } @@ -135,7 +134,7 @@ public class MapEditorTabControllerImpl extends MapEditorTabController { confirm("Delete map '" + currentMap.getName() + "'. Are you sure?", () -> { org.badvision.outlaweditor.data.xml.Map del = currentMap; setCurrentMap(null); - Application.gameData.getMap().remove(del); + ApplicationState.getInstance().getGameData().getMap().remove(del); rebuildMapSelectors(); }, null); } @@ -337,8 +336,8 @@ public class MapEditorTabControllerImpl extends MapEditorTabController { public void rebuildMapSelectors() { Map m = mapSelect.getSelectionModel().getSelectedItem(); mapSelect.getItems().clear(); - DataUtilities.sortMaps(Application.gameData.getMap()); - mapSelect.getItems().addAll(Application.gameData.getMap()); + DataUtilities.sortMaps(ApplicationState.getInstance().getGameData().getMap()); + mapSelect.getItems().addAll(ApplicationState.getInstance().getGameData().getMap()); mapSelect.getSelectionModel().select(m); } @@ -375,8 +374,8 @@ public class MapEditorTabControllerImpl extends MapEditorTabController { ToggleGroup tileGroup = new ToggleGroup(); HashMap submenus = new HashMap<>(); - Application.gameData.getTile().stream().forEach((Tile t) -> { - WritableImage img = TileUtils.getImage(t, currentPlatform); + ApplicationState.getInstance().getGameData().getTile().stream().forEach((Tile t) -> { + WritableImage img = TileUtils.getImage(t, ApplicationState.getInstance().getCurrentPlatform()); ImageView iv = new ImageView(img); String category = String.valueOf(t.getCategory()); Menu categoryMenu = submenus.get(category); @@ -391,7 +390,7 @@ public class MapEditorTabControllerImpl extends MapEditorTabController { tileGroup.selectToggle(tileSelection); theMenu.setStyle("-fx-font-weight:bold; -fx-text-fill:blue"); } - tileSelection.setGraphic(new ImageView(TileUtils.getImage(t, currentPlatform))); + tileSelection.setGraphic(new ImageView(TileUtils.getImage(t, ApplicationState.getInstance().getCurrentPlatform()))); tileSelection.setOnAction((event) -> { if (getCurrentEditor() != null) { getCurrentEditor().setCurrentTile(t); diff --git a/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/impl/TileEditorTabControllerImpl.java b/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/impl/TileEditorTabControllerImpl.java index fbd3d73c..86f010fb 100644 --- a/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/impl/TileEditorTabControllerImpl.java +++ b/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/impl/TileEditorTabControllerImpl.java @@ -10,7 +10,6 @@ package org.badvision.outlaweditor.ui.impl; -import org.badvision.outlaweditor.ui.EntitySelectorCell; import java.util.Arrays; import java.util.List; import java.util.logging.Level; @@ -23,7 +22,7 @@ import javafx.scene.image.ImageView; import javafx.util.StringConverter; import org.badvision.outlaweditor.Application; import org.badvision.outlaweditor.TileEditor; -import static org.badvision.outlaweditor.ui.UIAction.confirm; +import org.badvision.outlaweditor.api.ApplicationState; import static org.badvision.outlaweditor.data.PropertyHelper.bind; import static org.badvision.outlaweditor.data.PropertyHelper.boolProp; import static org.badvision.outlaweditor.data.PropertyHelper.stringProp; @@ -32,7 +31,9 @@ import org.badvision.outlaweditor.data.TilesetUtils; import org.badvision.outlaweditor.data.xml.PlatformData; import org.badvision.outlaweditor.data.xml.Tile; import org.badvision.outlaweditor.ui.ApplicationUIController; +import org.badvision.outlaweditor.ui.EntitySelectorCell; import org.badvision.outlaweditor.ui.TileEditorTabController; +import static org.badvision.outlaweditor.ui.UIAction.confirm; /** * FXML Controller class for tile editor tab @@ -95,7 +96,7 @@ public class TileEditorTabControllerImpl extends TileEditorTabController { confirm("Delete tile '" + getCurrentTile().getName() + "'. Are you sure?", () -> { Tile del = getCurrentTile(); setCurrentTile(null); - Application.gameData.getTile().remove(del); + ApplicationState.getInstance().getGameData().getTile().remove(del); mainController.rebuildTileSelectors(); }, null); } @@ -150,7 +151,7 @@ public class TileEditorTabControllerImpl extends TileEditorTabController { return new EntitySelectorCell(tileNameField, tileCategoryField) { @Override public void finishUpdate(Tile item) { - setGraphic(new ImageView(TileUtils.getImage(item, Application.currentPlatform))); + setGraphic(new ImageView(TileUtils.getImage(item, ApplicationState.getInstance().getCurrentPlatform()))); } }; }); @@ -223,7 +224,7 @@ public class TileEditorTabControllerImpl extends TileEditorTabController { bind(tileSpriteField.selectedProperty(), boolProp(t, "sprite")); bind(tileBlockerField.selectedProperty(), boolProp(t, "blocker")); bind(tileNameField.textProperty(), stringProp(t, "name")); - TileEditor editor = Application.currentPlatform.tileEditor.newInstance(); + TileEditor editor = ApplicationState.getInstance().getCurrentPlatform().tileEditor.newInstance(); editor.setEntity(t); setCurrentTileEditor(editor); tileNameField.textProperty().addListener(rebuildListener); @@ -241,7 +242,7 @@ public class TileEditorTabControllerImpl extends TileEditorTabController { public void rebuildTileSelectors() { Tile t = getCurrentTile(); tileSelector.getItems().clear(); - List allTiles = Application.gameData.getTile(); + List allTiles = ApplicationState.getInstance().getGameData().getTile(); allTiles.sort((Tile o1, Tile o2) -> { int c1 = String.valueOf(o1.getCategory()).compareTo(String.valueOf(o2.getCategory())); if (c1 != 0) { diff --git a/OutlawEditor/src/main/resources/Menubar.fxml b/OutlawEditor/src/main/resources/Menubar.fxml index 040fd934..446cfc69 100644 --- a/OutlawEditor/src/main/resources/Menubar.fxml +++ b/OutlawEditor/src/main/resources/Menubar.fxml @@ -1,13 +1,11 @@ - - - - - - + + + + - + @@ -38,6 +36,7 @@ +