From 3690482728de52224a7f7ea7e39c3b1d26a3bb32 Mon Sep 17 00:00:00 2001 From: philippe lhardy Date: Tue, 20 Oct 2020 10:24:08 +0200 Subject: [PATCH] add drawing imc import as a path that will be openned in maze - importing an imc will create a open path folowwing the given drawing ( very slow due to addStatus string notification ? ) --- .../games/maze/gui/Display.java | 86 +++++++++++++++++-- 1 file changed, 78 insertions(+), 8 deletions(-) diff --git a/java/org/artisanlogiciel/games/maze/gui/Display.java b/java/org/artisanlogiciel/games/maze/gui/Display.java index 18f5e3a..c89b15f 100644 --- a/java/org/artisanlogiciel/games/maze/gui/Display.java +++ b/java/org/artisanlogiciel/games/maze/gui/Display.java @@ -17,10 +17,7 @@ import java.awt.event.InputEvent; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionListener; import java.io.*; -import java.util.Date; -import java.util.LinkedList; -import java.util.Locale; -import java.util.ResourceBundle; +import java.util.*; import javax.imageio.ImageIO; @@ -379,8 +376,27 @@ public class Display extends JFrame { }; JButton loadRawButton = new JButton(labels.getString("load" ) + " raw"); loadRawButton.addActionListener(loadRawAction); - loadMenu.add(loadRawButton); + + Action loadImcAction = new AbstractAction() { + public void actionPerformed(ActionEvent evt) { + // + addStatus("load Imc"); + + String filename = loadName.getText(); + + if ((filename.length() > 0)) { + setMazeName(filename); + loadImc(false); + refresh(); + } + + } + }; + JButton loadImcButton = new JButton(labels.getString("load" ) + " imc"); + loadImcButton.addActionListener(loadImcAction); + + loadMenu.add(loadImcButton); return loadMenu; } @@ -656,6 +672,38 @@ public class Display extends JFrame { } } + private void loadImc(boolean add) { + if ( maze != null ) { + new Thread() { + @Override + public void run() { + File infile = new File(params.getSaveDir(), params.getName() + ".imc"); + FileInputStream inputStream = null; + try { + inputStream = new FileInputStream(infile); + // TODO + // model = new MazePersistRaw().parseInputStream("raw",inputStream); + Drawing drawing = new Drawing(); + drawing.loadLinesExpanded(new DataInputStream(inputStream)); + maze.addDrawing(drawing,add); + } 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 + } + } + } + } + }.start(); + } + } + private JButton addDirection(final JPanel panel, final String direction, String key, Action goAction) { String actionName = "go" + direction; JButton button = new JButton(direction); @@ -843,13 +891,13 @@ public class Display extends JFrame { Date now = new Date(System.currentTimeMillis()); if (lastDrag == null ) { - drawingPath = null; + resetDrawingPath(); } else { if ( now.getTime() - lastDrag.getTime() > dragTimeout ) { - drawingPath = null; + resetDrawingPath(); addStatus("move timeout"); } } @@ -857,6 +905,28 @@ public class Display extends JFrame { addPosition(newPosition,add); } + public void resetDrawingPath() + { + drawingPath = null; + } + + public void addDrawing(Drawing drawing, boolean add) + { + for (DrawingLine line : drawing.getInternLines()) + { + addDrawingLine(line, add); + } + } + public void addDrawingLine(DrawingLine line, boolean add) + { + resetDrawingPath(); + ArrayList points = line.getPoints(); + for (Point p : points) + { + addPosition(new Position(p.x,p.y), add); + } + } + public void addPosition(Position newPosition, boolean add ) { // should find the cell ... DirectionPosition last = null; @@ -874,7 +944,7 @@ public class Display extends JFrame { while (!last.getPosition().equals(newPosition)) { path = LabyModel.getDirection(last.getPosition(), newPosition); last.setDirection(path); - // button 1 : add direction; button 2 : replace with direction. + // button 1 : add direction; button 2 : rep lace with direction. if (add) { map.addDirection(last.getPosition().getX(), last.getPosition().getY(), path); } else {