From 6dfcea56479d2cc2bbf8102abeba4115de7281f4 Mon Sep 17 00:00:00 2001 From: philippe lhardy Date: Wed, 14 Oct 2020 22:21:46 +0200 Subject: [PATCH] add load menu and a status line - don't print to console but add a status line - add a load menu to load raw --- .../games/maze/gui/Display.java | 179 ++++++++++++------ lang/LabelsBundle.properties | 3 +- lang/LabelsBundle_fr.properties | 3 +- 3 files changed, 130 insertions(+), 55 deletions(-) diff --git a/java/org/artisanlogiciel/games/maze/gui/Display.java b/java/org/artisanlogiciel/games/maze/gui/Display.java index 5da68ce..79893e5 100644 --- a/java/org/artisanlogiciel/games/maze/gui/Display.java +++ b/java/org/artisanlogiciel/games/maze/gui/Display.java @@ -16,11 +16,7 @@ import java.awt.event.ComponentEvent; import java.awt.event.InputEvent; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionListener; -import java.io.DataOutputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; +import java.io.*; import java.util.LinkedList; import java.util.Locale; import java.util.ResourceBundle; @@ -53,10 +49,10 @@ public class Display extends JFrame { MazeComponent maze; MazeControler controler; LabyModel model; - JPanel controlPanel; boolean autoSize; MazeParams params = null; + JTextField statusField = null; Maze3dSettings stlsettings; @@ -92,7 +88,7 @@ public class Display extends JFrame { setVisible(true); } - private static MazeComponent createMazeComponent(LabyModel model, int W, int H) { + private MazeComponent createMazeComponent(LabyModel model, int W, int H) { MazeCellParameters cp = new MazeCellParameters(model.getWidth(), model.getHeight(), W, H, 3, 3); MazeComponent comp = new MazeComponent(model, cp); return comp; @@ -101,10 +97,8 @@ public class Display extends JFrame { void recreateModel() { // recreate labyrinth if (params != null) { - params = controler.getSettings().resetParams(); - // DON'T keep current model... - // TODO control SEED. - model = new LabyModel(params); + + // Keep current model to be able to complete a manualy edited one maze.resetWallsProvider(model); model.setMazeListener(maze); @@ -126,10 +120,18 @@ public class Display extends JFrame { if (params != null) { params = controler.getSettings().resetParams(); model = new LabyModel(params); + + refresh(); + } + } + + void refresh() + { + // reinit labyrinth view + if (params != null) { maze.resetWallsProvider(model); model.setMazeListener(maze); - // we are in GUI event thread, need to release it and do it outside. new Thread() { public void run() { @@ -137,7 +139,6 @@ public class Display extends JFrame { } }.start(); - } } @@ -243,7 +244,7 @@ public class Display extends JFrame { // to allow to log / write somewher into screen... void writeSentence(String pSentence) { // TODO - System.out.println(pSentence); + addStatus(pSentence); } void writeError(String pError) { @@ -254,7 +255,7 @@ public class Display extends JFrame { Drawing d = createDrawing(); if (d != null) { - File outfile = new File(params.getSaveDir(), params.getName() + ".imc"); + File outfile = getFileForExtension("imc"); writeSentence("Saving to " + outfile + " ..."); try { DataOutputStream out = new DataOutputStream(new FileOutputStream(outfile)); @@ -268,11 +269,16 @@ public class Display extends JFrame { } } + File getFileForExtension(final String extension) + { + return new File(params.getSaveDir(), params.getName() + "." + extension); + } + void saveSvg() { Drawing d = createDrawing(); if (d != null) { - File outfile = new File(params.getSaveDir(), params.getName() + ".svg"); + File outfile = getFileForExtension("svg"); writeSentence("Saving to " + outfile + " ..."); try { DataOutputStream out = new DataOutputStream(new FileOutputStream(outfile)); @@ -291,7 +297,7 @@ public class Display extends JFrame { void savePng() { - File file = new File("snapshot.png"); + File file = getFileForExtension("png"); // BufferedImage bi = new BufferedImage(this.getSize().width, this.getSize().height, BufferedImage.TYPE_INT_ARGB); BufferedImage bi = new BufferedImage(maze.getSize().width, maze.getSize().height, BufferedImage.TYPE_INT_ARGB); Graphics g = bi.createGraphics(); @@ -305,6 +311,11 @@ public class Display extends JFrame { } } + void addStatus(String pStatus) + { + statusField.setText(pStatus); + } + private class MazeControler extends JPanel { /** @@ -321,13 +332,42 @@ public class Display extends JFrame { p.setName(pName); } + private JMenu createLoadingMenu() { + JMenu loadMenu = new JMenu(labels.getString("load") ); + + final JTextField loadName = new JTextField("newlaby"); + + loadMenu.add(loadName); + + Action loadRawAction = new AbstractAction() { + public void actionPerformed(ActionEvent evt) { + // + addStatus("load raw"); + + String filename = loadName.getText(); + + if ((filename.length() > 0)) { + setMazeName(filename); + loadRaw(); + refresh(); + } + + } + }; + JButton loadRawButton = new JButton(labels.getString("load" ) + " raw"); + loadRawButton.addActionListener(loadRawAction); + + loadMenu.add(loadRawButton); + return loadMenu; + } + private JMenu createSavingMenu() { - final JTextField saveName = new JTextField("newlaby "); + final JTextField saveName = new JTextField("newlaby"); final JButton savePngButton = new JButton(labels.getString("save") + " png"); Action savePngAction = new AbstractAction() { public void actionPerformed(ActionEvent evt) { // - System.out.println("save png"); + addStatus("save png"); setMazeName(saveName.getText()); savePng(); } @@ -346,7 +386,7 @@ public class Display extends JFrame { Action saveAction = new AbstractAction() { public void actionPerformed(ActionEvent evt) { // - System.out.println("save"); + addStatus("save"); setMazeName(saveName.getText()); MazeParamsFixed p = (MazeParamsFixed) params; save(p, model); @@ -357,7 +397,7 @@ public class Display extends JFrame { Action saveImcAction = new AbstractAction() { public void actionPerformed(ActionEvent evt) { // - System.out.println("save imc"); + addStatus("save imc"); setMazeName(saveName.getText()); saveImc(); } @@ -367,7 +407,7 @@ public class Display extends JFrame { Action saveStlAction = new AbstractAction() { public void actionPerformed(ActionEvent evt) { // - System.out.println("save stl"); + addStatus("save stl"); setMazeName(saveName.getText()); MazeParamsFixed p = (MazeParamsFixed) params; saveStl(p, model,stlsettings.createParams()); @@ -377,7 +417,7 @@ public class Display extends JFrame { stlsettings = new Maze3dSettings(new Maze3dParams()); - JMenu saveMenu = new JMenu("Save"); + JMenu saveMenu = new JMenu(labels.getString("save") ); saveMenu.add(saveName); saveMenu.add(saveSvgButton); saveMenu.add(savePngButton); @@ -405,7 +445,7 @@ public class Display extends JFrame { resolveButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { // - System.out.println("Resolving"); + addStatus("Resolving"); resolve(); } }); @@ -416,7 +456,7 @@ public class Display extends JFrame { Action quitAction = new AbstractAction() { public void actionPerformed(ActionEvent evt) { // - System.out.println("quit"); + addStatus("quit"); System.exit(0); } }; @@ -444,17 +484,18 @@ public class Display extends JFrame { JMenuBar menuBar = new JMenuBar(); menuBar.add(createSavingMenu()); + menuBar.add(createLoadingMenu()); add(menuBar); add(createResolveQuitBar()); - controlPanel = new JPanel(new BorderLayout()); + JPanel controlMovesPanel = new JPanel(new BorderLayout()); settings = new MazeSettings(params); @SuppressWarnings("serial") Action goNorth = new AbstractAction() { public void actionPerformed(ActionEvent evt) { // - System.out.println("go North"); + addStatus("go North"); goNorth(); } }; @@ -464,7 +505,7 @@ public class Display extends JFrame { Action goEast = new AbstractAction() { public void actionPerformed(ActionEvent evt) { // - System.out.println("go East"); + addStatus("go East"); goEast(); } }; @@ -475,7 +516,7 @@ public class Display extends JFrame { Action goWest = new AbstractAction() { public void actionPerformed(ActionEvent evt) { // - System.out.println("go West"); + addStatus("go West"); goWest(); } }; @@ -485,16 +526,22 @@ public class Display extends JFrame { Action goSouth = new AbstractAction() { public void actionPerformed(ActionEvent evt) { // - System.out.println("go South"); + addStatus("go South"); goSouth(); } }; JButton south = addDirection(this, labels.getString("south"), "DOWN", goSouth); - controlPanel.add(north, BorderLayout.NORTH); - controlPanel.add(west, BorderLayout.WEST); - controlPanel.add(east, BorderLayout.EAST); - controlPanel.add(south, BorderLayout.SOUTH); + controlMovesPanel.add(north, BorderLayout.NORTH); + controlMovesPanel.add(west, BorderLayout.WEST); + controlMovesPanel.add(east, BorderLayout.EAST); + controlMovesPanel.add(south, BorderLayout.SOUTH); + + JPanel controlPanel = new JPanel(); + + controlPanel.add(controlMovesPanel,BorderLayout.LINE_START); + statusField = new JTextField("",20); + controlPanel.add(statusField, BorderLayout.CENTER); add(controlPanel, BorderLayout.NORTH); // control display panel contains controls for display @@ -549,6 +596,32 @@ public class Display extends JFrame { } + private void loadRaw() { + File infile = new File(params.getSaveDir(), params.getName() + ".raw"); + FileInputStream inputStream = null; + try { + inputStream = new FileInputStream(infile); + model = new LabyModel("raw", inputStream); + } catch (IOException io) { + io.printStackTrace(System.err); + statusField.setText("[ERROR] Can't load " + infile.getAbsolutePath()); + } + finally + { + if (inputStream != null ) + { + // cleanup + try { + inputStream.close(); + } + catch (Exception any) + { + // don't care really + } + } + } + } + private JButton addDirection(final JPanel panel, final String direction, String key, Action goAction) { String actionName = "go" + direction; JButton button = new JButton(direction); @@ -694,7 +767,7 @@ public class Display extends JFrame { } - private static class MazeComponent + private class MazeComponent extends JComponent implements MazeCreationListener, MazeResolutionListener, @@ -734,7 +807,7 @@ public class Display extends JFrame { if (drawingPath == null) { drawingPath = new LinkedList<>(); last = new DirectionPosition((short) 0, newPosition); - System.out.println("Mouse dragged Cell " + newPosition + " Button " + e.getModifiersEx() + " " + InputEvent.BUTTON1_DOWN_MASK); + addStatus("Mouse dragged Cell " + newPosition + " Button " + e.getModifiersEx() + " " + InputEvent.BUTTON1_DOWN_MASK); drawingPath.addLast(last); } else { // setShowAll(true); @@ -753,14 +826,14 @@ public class Display extends JFrame { last = last.moveToAdjacentDirection(); drawingPath.addLast(last); } - System.out.println("Mouse dragged from Cell " + first.getPosition() + "To" + newPosition + " Button " + e.getModifiersEx() + " " + InputEvent.BUTTON1_DOWN_MASK); + addStatus("Mouse dragged from Cell " + first.getPosition() + "To" + newPosition + " Button " + e.getModifiersEx() + " " + InputEvent.BUTTON1_DOWN_MASK); } changed(null, null, map); } @Override public void mouseMoved(MouseEvent e) { - // System.out.println("Mouse moved (" + e.getX() + ',' + e.getY() + ')'); + // addStatus("Mouse moved (" + e.getX() + ',' + e.getY() + ')'); Position newPosition = getPosition(e.getX(), e.getY()); if ((newPosition.getX() >= 0) && (newPosition.getY() >= 0)) { requestFocus(); @@ -769,12 +842,12 @@ public class Display extends JFrame { public void setShowAll(boolean showall) { this.showAll = showall; - System.out.println(showAll ? "show all" : "X"); + addStatus(showAll ? "show all" : "X"); } void checkExit() { if ((sX == gX) && (sY == gY)) { - System.out.println("Exit found by human !"); + addStatus("Exit found by human !"); } } @@ -958,7 +1031,7 @@ public class Display extends JFrame { } if (this.showAll) { - System.out.println("*"); + addStatus("*"); pX = aX; pY = aY; @@ -998,7 +1071,7 @@ public class Display extends JFrame { public void notifyCompletion(LinkedList solvedPath) { LinkedList newPath = new LinkedList<>(solvedPath); - System.out.println("resolution completed"); + addStatus("resolution completed"); synchronized (lockChange) { this.solvedPath = newPath; } @@ -1016,39 +1089,39 @@ public class Display extends JFrame { Display display = new Display(model, W, H, params); } - public static void saveStl(MazeParamsFixed params, LabyModel model, Maze3dParams wallparams) { - File outfile = new File(params.getSaveDir(), params.getName() + ".stl"); + public void saveStl(MazeParamsFixed params, LabyModel model, Maze3dParams wallparams) { + File outfile = getFileForExtension("stl"); if (!outfile.exists()) { - System.out.println("Saving to " + outfile + " ..."); + addStatus("Saving to " + outfile + " ..."); try { FileOutputStream out = new FileOutputStream(outfile); new Wall3dStream(params.getName(), model, out, wallparams).stream(); out.close(); - System.out.println("... Done."); + addStatus("... Done."); } catch (IOException io) { io.printStackTrace(System.err); } } else { - System.out.println("" + outfile + " already exists"); + addStatus("" + outfile + " already exists"); } } - public static void save(MazeParamsFixed params, LabyModel model) { - File outfile = new File(params.getSaveDir(), params.getName() + ".raw"); + public void save(MazeParamsFixed params, LabyModel model) { + File outfile = getFileForExtension("raw"); if (!outfile.exists()) { - System.out.println("Saving to " + outfile + " ..."); + addStatus("Saving to " + outfile + " ..."); try { FileOutputStream out = new FileOutputStream(outfile); model.streamOut("raw", out); out.flush(); out.close(); - System.out.println("... Done."); + addStatus("... Done."); } catch (IOException io) { io.printStackTrace(System.err); } } else { - System.out.println("" + outfile + " already exists"); + addStatus("" + outfile + " already exists"); } } @@ -1081,7 +1154,7 @@ public class Display extends JFrame { model.addEntryOrExit(-1, 0); model.addEntryOrExit(params.getWidth(), params.getHeight() - 1); - System.out.println("Generation completed"); + addStatus("Generation completed"); */ /* */ diff --git a/lang/LabelsBundle.properties b/lang/LabelsBundle.properties index c63ec24..de03b04 100644 --- a/lang/LabelsBundle.properties +++ b/lang/LabelsBundle.properties @@ -10,4 +10,5 @@ create = Create save = Save quit = Quit title = A Maz ing -seed = seed \ No newline at end of file +seed = seed +load = Load \ No newline at end of file diff --git a/lang/LabelsBundle_fr.properties b/lang/LabelsBundle_fr.properties index 118f33f..cd279bf 100644 --- a/lang/LabelsBundle_fr.properties +++ b/lang/LabelsBundle_fr.properties @@ -10,4 +10,5 @@ create = Créer save = Sauver quit = Quitter title = La Bireinte -seed = graine \ No newline at end of file +seed = graine +load = Charger \ No newline at end of file