Split Display (gui) and Maze (work), prepare for another model

- prepare for a shorter storage model without any resolution
This commit is contained in:
philippe lhardy
2020-12-20 19:18:17 +01:00
parent a70ead877a
commit e146199ba0
15 changed files with 720 additions and 450 deletions

View File

@@ -32,13 +32,9 @@ public class Brick
char d;
short walls; // according to LabyModel.getWalls();
public Brick(char center, char right, char down, char downright, short walls)
protected Brick()
{
a = center;
b = right;
c = down;
d = downright;
this.walls = walls;
//
}
public Brick(String up, String low, short walls)
@@ -50,12 +46,6 @@ public class Brick
this.walls = walls;
}
private boolean check()
{
// Not Yet Implemented
return false;
}
public String getUpString()
{
return "" + a + b;
@@ -66,52 +56,6 @@ public class Brick
return "" + c + d;
}
public static String getDirLine()
{
char dir[] = new char[16];
String s = "";
/*
* dir[LEFT | DOWN | RIGHT | UP]='O'; dir[LEFT | DOWN | RIGHT]='U';
* dir[LEFT | UP | RIGHT]='M'; dir[LEFT | UP | DOWN]='['; dir[RIGHT | UP
* | DOWN]=']'; dir[UP | DOWN]='='; dir[LEFT | RIGHT]='|'; dir[RIGHT |
* DOWN]='J'; dir[LEFT | DOWN]='L'; dir [LEFT | UP]='T'; dir[UP |
* RIGHT]='7'; dir[LEFT] = '!'; dir[RIGHT] ='|'; dir[DOWN]= '_';
* dir[UP]= '¨'; dir[0]=' ';
*/
dir[LEFT | DOWN | RIGHT | UP] = 'O';
dir[LEFT | DOWN | RIGHT] = 'U';
dir[LEFT | UP | RIGHT] = 'M';
dir[LEFT | UP | DOWN] = '[';
dir[RIGHT | UP | DOWN] = ']';
dir[UP | DOWN] = '=';
dir[LEFT | RIGHT] = 226;
dir[RIGHT | DOWN] = 'J';
dir[LEFT | DOWN] = 'L';
dir[LEFT | UP] = 169;
dir[UP | RIGHT] = 170;
dir[LEFT] = 173;
dir[RIGHT] = '|';
dir[DOWN] = '_';
dir[UP] = '¨';
dir[0] = ' ';
for (int i = 0; i < 16; i++)
{
s = s + dir[i];
}
return s;
}
public char getChar()
{
// return getDirLine().charAt(walls & 0xFFF0);
return getDirLine().charAt(walls);
}
public short getWalls()
{
return walls;

View File

@@ -0,0 +1,60 @@
package org.artisanlogiciel.games.maze;
public class BrickTextMapping
extends Brick {
public static String getDirLine()
{
char dir[] = new char[16];
String s = "";
/*
* dir[LEFT | DOWN | RIGHT | UP]='O'; dir[LEFT | DOWN | RIGHT]='U';
* dir[LEFT | UP | RIGHT]='M'; dir[LEFT | UP | DOWN]='['; dir[RIGHT | UP
* | DOWN]=']'; dir[UP | DOWN]='='; dir[LEFT | RIGHT]='|'; dir[RIGHT |
* DOWN]='J'; dir[LEFT | DOWN]='L'; dir [LEFT | UP]='T'; dir[UP |
* RIGHT]='7'; dir[LEFT] = '!'; dir[RIGHT] ='|'; dir[DOWN]= '_';
* dir[UP]= '¨'; dir[0]=' ';
*/
dir[LEFT | DOWN | RIGHT | UP] = 'O';
dir[LEFT | DOWN | RIGHT] = 'U';
dir[LEFT | UP | RIGHT] = 'M';
dir[LEFT | UP | DOWN] = '[';
dir[RIGHT | UP | DOWN] = ']';
dir[UP | DOWN] = '=';
dir[LEFT | RIGHT] = 226;
dir[RIGHT | DOWN] = 'J';
dir[LEFT | DOWN] = 'L';
dir[LEFT | UP] = 169;
dir[UP | RIGHT] = 170;
dir[LEFT] = 173;
dir[RIGHT] = '|';
dir[DOWN] = '_';
dir[UP] = '¨';
dir[0] = ' ';
for (int i = 0; i < 16; i++)
{
s = s + dir[i];
}
return s;
}
public static char getChar(short walls)
{
// return getDirLine().charAt(walls & 0xFFF0);
return getDirLine().charAt(walls);
}
public static char getChar(Brick brick)
{
// return getDirLine().charAt(walls & 0xFFF0);
return getChar(brick.walls);
}
}

View File

@@ -89,7 +89,8 @@ public class LabyMap implements WallsProvider
}
return laby;
}
// FIXME UGLY and not usable, need a better mapping, and not be a specialized toString...
@Deprecated
public String toShortString()
{
int entryX = -1;
@@ -110,11 +111,12 @@ public class LabyMap implements WallsProvider
{
for (int x = 0; x < tileMap.length; x++)
{
laby += "" + tileMap[x][y].getChar();
laby += "" + BrickTextMapping.getChar(tileMap[x][y]);
}
laby += "\n";
}
return laby;
}
}

View File

@@ -1,5 +1,7 @@
package org.artisanlogiciel.games.maze;
import org.artisanlogiciel.games.maze.model.LabyModelProvider;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Random;
@@ -12,10 +14,11 @@ import java.util.Random;
* tagged CLOSED when fully processed
**/
public class LabyModel
implements WallsProvider, MovesProvider {
implements LabyModelProvider {
/**
* WARNING don't change those values, they are used as it is for
* WARNING don't change those va public short getPath(int x, int y) {
lues, they are used as it is for
* optimisation
*/
private final static short FLAGLENGTH = 7;

View File

@@ -5,7 +5,12 @@ import org.artisanlogiciel.games.maze.solve.SolvingModel;
import java.util.LinkedList;
import java.util.Scanner;
public class Main {
public class Main
extends Maze {
public Main(LabyModel model) {
super(model);
}
public MazeParamEditor editor() {
MazeParamEditor editor = new MazeParamEditor(null);
System.out.println("enter width height and maxdepth");
@@ -18,6 +23,7 @@ public class Main {
params.setSeed(1024L);
SolvingModel model = new SolvingModel(params);
model.generateWithEntry(0, 0);
setModel(model);
final int width = params.getWidth();
final int height = params.getHeight();
@@ -35,11 +41,10 @@ public class Main {
}
public static void main(String pArgs[]) {
Main m = new Main();
Main m = new Main(null);
MazeParamEditor editor = m.editor();
LabyMap map = m.generate2(editor);
System.out.println(map.toShortString());
System.out.println(map.toString());
System.out.println(Brick.getDirLine());
// ­_L|âJU¨©=[ªM]O
}
}

View File

@@ -0,0 +1,375 @@
package org.artisanlogiciel.games.maze;
import org.artisanlogiciel.games.maze.persist.MazePersistRaw;
import org.artisanlogiciel.games.maze.persist.MazePersistWorldEdit;
import org.artisanlogiciel.games.stl.Maze3dParams;
import org.artisanlogiciel.games.stl.Wall3dStream;
import org.artisanlogiciel.graphics.Drawing;
import org.artisanlogiciel.graphics.SvgWriter;
import org.artisanlogiciel.osm.OsmReader;
import org.artisanlogiciel.osm.convert.OsmToDrawing;
import java.io.*;
public class Maze {
protected LabyLayers layers = new LabyLayers();
protected LabyModel model;
int layer = 0;
protected MazeParams params = null;
public Maze(LabyModel model)
{
this.model = model;
}
public MazeParams getParams() {
return params;
}
protected void refresh()
{
addStatus("refresh...");
}
public boolean isStatusEnable() {
return false;
}
public void addDrawing(Drawing drawing, boolean add)
{
// FIXME in non graphical case
// does nothing yet
}
public void setStatusEnable(boolean statusEnable) {
// does nothing
}
protected void addStatus(String status)
{
System.out.println(status);
}
// to allow to log / write somewher into screen...
public void writeSentence(String pSentence) {
// TODO
addStatus(pSentence);
}
void writeError(String pError) {
System.err.println(pError);
}
public void saveWorldEdit() {
File outfile = getFileForExtension("we");
if (!outfile.exists()) {
addStatus("Saving we to " + outfile + " ...");
try {
FileOutputStream out = new FileOutputStream(outfile);
new MazePersistWorldEdit(layers).streamOut("we", out);
out.close();
addStatus("... Done.");
} catch (IOException io) {
io.printStackTrace(System.err);
}
} else {
addStatus("we file " + outfile + " already exists");
}
}
public void saveStl(MazeParamsFixed params, Maze3dParams wallparams) {
File outfile = getFileForExtension(params,"stl");
if (!outfile.exists()) {
addStatus("Saving stl to " + outfile + " ...");
try {
FileOutputStream out = new FileOutputStream(outfile);
new Wall3dStream(params.getName(), model, out, wallparams).stream();
out.close();
addStatus("... Done.");
} catch (IOException io) {
io.printStackTrace(System.err);
}
} else {
addStatus("stl file " + outfile + " already exists");
}
}
public void saveRaw(MazeParamsFixed params)
{
save(params,model);
}
void save(MazeParamsFixed params, LabyModel model) {
File outfile = getFileForExtension(params,"raw");
if (!outfile.exists()) {
addStatus("Saving to " + outfile + " ...");
try {
FileOutputStream out = new FileOutputStream(outfile);
MazePersistRaw persist = new MazePersistRaw(model);
persist.streamOut("raw", out);
out.flush();
out.close();
addStatus("... Done.");
} catch (IOException io) {
io.printStackTrace(System.err);
}
} else {
addStatus("" + outfile + " already exists");
}
}
public void saveImc() {
Drawing d = createDrawing();
if (d != null) {
File outfile = getFileForExtension("imc");
writeSentence("Saving to " + outfile + " ...");
try {
DataOutputStream out = new DataOutputStream(new FileOutputStream(outfile));
d.saveLinesKompressed(out);
out.flush();
out.close();
writeSentence("... Done.");
} catch (IOException io) {
io.printStackTrace(System.err);
}
}
}
File getFileForExtension(MazeParams p, final String extension)
{
return new File(p.getSaveDir(), p.getName() + "." + extension);
}
protected File getFileForExtension(final String extension)
{
return getFileForExtension(params,extension);
}
public Drawing createDrawing() {
return new DrawingGenerator(model).createDrawing();
}
public void saveSvg() {
Drawing d = createDrawing();
if (d != null) {
File outfile = getFileForExtension("svg");
writeSentence("Saving to " + outfile + " ...");
try {
DataOutputStream out = new DataOutputStream(new FileOutputStream(outfile));
SvgWriter writer = new SvgWriter(d.getInternLines());
writer.writeTo(out);
out.flush();
out.close();
writeSentence("... Done.");
} catch (IOException io) {
io.printStackTrace(System.err);
}
} else {
writeError("drawing creation failed");
}
}
public void saveText() {
File outfile = getFileForExtension("txt");
writeSentence("Saving to " + outfile + " ...");
try {
DataOutputStream out = new DataOutputStream(new FileOutputStream(outfile));
out.write(model.toLabyMap().toString().getBytes());
out.flush();
out.close();
writeSentence("... Done.");
} catch (IOException io) {
io.printStackTrace(System.err);
}
}
protected void setModel( LabyModel model)
{
this.model = model;
layers.addLabyModel(layer, model);
}
public int setLayer(int x)
{
addStatus("set layer " + x);
LabyModel layermodel = layers.getLayer(x);
if ( layermodel == null )
{
// clone it
model = new LabyModel(model);
layers.addLabyModel(x, model);
}
else
{
model = layermodel;
}
layer = x;
refresh();
return layer;
}
public void loadWorldEdit(boolean add) {
File infile = new File(params.getSaveDir(), params.getName() + ".we");
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(infile);
LabyLayers newLayers = new MazePersistWorldEdit().parseInputStream("we",inputStream);
if ( ! newLayers.isEmpty()) {
int l = layer;
for (int i = newLayers.getMin(); i <= newLayers.getMax(); i++) {
LabyModel m = newLayers.getLayer(i);
if (m != null) {
System.out.println("add layer " + l);
layers.addLabyModel(l, m);
l++;
}
}
setModel(layers.getLayer(layer));
}
} catch (IOException io) {
io.printStackTrace(System.err);
addStatus("[ERROR] Can't load " + infile.getAbsolutePath());
} finally {
if (inputStream != null) {
// cleanup
try {
inputStream.close();
} catch (Exception any) {
// don't care really
}
}
}
}
public void loadRaw() {
File infile = new File(params.getSaveDir(), params.getName() + ".raw");
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(infile);
setModel(new MazePersistRaw().parseInputStream("raw",inputStream));
} catch (IOException io) {
io.printStackTrace(System.err);
addStatus("[ERROR] Can't load " + infile.getAbsolutePath());
}
finally
{
if (inputStream != null )
{
// cleanup
try {
inputStream.close();
}
catch (Exception any)
{
// don't care really
}
}
}
}
public void loadOsm(boolean add, int mulx, int muly) {
new Thread() {
@Override
public void run() {
File infile = new File(params.getSaveDir(), params.getName() + ".osm");
FileInputStream inputStream = null;
try {
// TODO really use InputStream and not pathname
OsmReader reader = new OsmReader(infile.getCanonicalPath());
reader.read();
OsmToDrawing converter = new OsmToDrawing(reader, mulx,muly);
Drawing drawing = converter.getDrawing(reader.getWays());
setStatusEnable(false);
addDrawing(drawing,add);
setStatusEnable(true);
} catch (IOException io) {
io.printStackTrace(System.err);
addStatus("[ERROR] Can't load " + infile.getAbsolutePath());
} finally {
if (inputStream != null) {
// cleanup
try {
inputStream.close();
} catch (Exception any) {
// don't care really
}
}
}
}
}.start();
}
public void loadImc(boolean add) {
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));
setStatusEnable(false);
addDrawing(drawing,add);
setStatusEnable(true);
} catch (IOException io) {
io.printStackTrace(System.err);
addStatus("[ERROR] Can't load " + infile.getAbsolutePath());
} finally {
if (inputStream != null) {
// cleanup
try {
inputStream.close();
} catch (Exception any) {
// don't care really
}
}
}
}
}.start();
}
public void loadDrawing(boolean add) {
new Thread() {
@Override
public void run() {
File infile = new File(params.getSaveDir(), params.getName() + ".drawing");
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(infile);
// TODO
// model = new MazePersistRaw().parseInputStream("raw",inputStream);
Drawing drawing = new Drawing();
drawing.loadLines(new DataInputStream(inputStream));
setStatusEnable(false);
addDrawing(drawing,add);
setStatusEnable(true);
} catch (IOException io) {
io.printStackTrace(System.err);
addStatus("[ERROR] Can't load " + infile.getAbsolutePath());
} finally {
if (inputStream != null) {
// cleanup
try {
inputStream.close();
} catch (Exception any) {
// don't care really
}
}
}
}
}.start();
}
}

View File

@@ -1,9 +1,13 @@
package org.artisanlogiciel.games.maze;
import org.artisanlogiciel.games.maze.model.WidthHeightProvider;
/**
* Get model represented by possible moves
*/
public interface MovesProvider {
public interface MovesProvider
extends WidthHeightProvider
{
/** return possible moves from this position */
short getMoves(int x, int y);

View File

@@ -1,15 +1,13 @@
package org.artisanlogiciel.games.maze;
import org.artisanlogiciel.games.maze.model.WidthHeightProvider;
/**
* WallsProvider provide a Walls representation
**/
public interface WallsProvider
extends WidthHeightProvider
{
int getWidth();
int getHeight();
/**
* See Brick
*

View File

@@ -1,68 +1,72 @@
package org.artisanlogiciel.games.maze.gui;
import org.artisanlogiciel.games.maze.*;
import org.artisanlogiciel.games.maze.LabyModel;
import org.artisanlogiciel.games.maze.Maze;
import org.artisanlogiciel.games.maze.MazeParams;
import org.artisanlogiciel.games.maze.MazeParamsFixed;
import org.artisanlogiciel.games.maze.persist.MazePersistRaw;
import org.artisanlogiciel.games.maze.persist.MazePersistWorldEdit;
import org.artisanlogiciel.games.maze.solve.SolvingModel;
import org.artisanlogiciel.games.stl.Maze3dParams;
import org.artisanlogiciel.games.stl.Wall3dStream;
import org.artisanlogiciel.graphics.Drawing;
import org.artisanlogiciel.graphics.SvgWriter;
import org.artisanlogiciel.osm.OsmReader;
import org.artisanlogiciel.osm.convert.OsmToDrawing;
import org.artisanlogiciel.xpm.Xpm;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.image.BufferedImage;
import java.io.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Locale;
/**
* Display is Main JFrame for this tool
**/
public class Display extends JFrame
public class Display extends Maze
implements StatusListener
{
// to please eclipse, not supposed to be serialized
private static final long serialVersionUID = 8500214871372184418L;
LabyLayers layers = new LabyLayers();
int layer = 0;
MazeComponent maze;
MazeControler controler;
LabyModel model;
boolean autoSize;
public boolean isStatusEnable() {
return statusEnable;
}
public void setStatusEnable(boolean statusEnable) {
this.statusEnable = statusEnable;
}
public void addDrawing(Drawing drawing, boolean add)
{
if (maze != null)
{
maze.addDrawing(drawing,add);
}
}
boolean statusEnable = true;
boolean mGrow = false;
MazeParams params = null;
JTextField statusField = null;
Maze3dSettings stlsettings;
Display(LabyModel model, int W, int H, MazeParams params) {
MazeFrame mazeFrame;
private class MazeFrame extends JFrame
{
MazeFrame(LabyModel model, int W, int H, MazeParams params) {
super(MazeDefault.labels.getString("title"));
if (params != null) {
// fixedParams = new MazeParamsFixed(params.getSaveDir(),params.getWidth(),params.getHeight(),params.getMaxDepth());
this.params = params;
}
this.model = model;
maze = createMazeComponent(model, W, H);
Container con = this.getContentPane();
JScrollPane scrollableMaze = new JScrollPane(maze);
con.add(scrollableMaze, BorderLayout.CENTER);
controler = new MazeControler(this,params);
controler = new MazeControler(Display.this, params);
con.add(controler.getMoveControl(), BorderLayout.NORTH);
con.add(controler.getGenerationControl(), BorderLayout.SOUTH);
@@ -84,6 +88,18 @@ implements StatusListener
setVisible(true);
}
}
public Display(LabyModel model, int W, int H, MazeParams params)
{
super(model);
mazeFrame = new MazeFrame(model,W,H,params);
if (params != null) {
// fixedParams = new MazeParamsFixed(params.getSaveDir(),params.getWidth(),params.getHeight(),params.getMaxDepth());
this.params = params;
}
}
private MazeComponent createMazeComponent(LabyModel model, int W, int H) {
MazeCellParameters cp = new MazeCellParameters(model.getWidth(), model.getHeight(), W, H, 0, 0);
MazeComponent comp = new MazeComponent(model, cp, this);
@@ -120,12 +136,6 @@ implements StatusListener
}
}
void setModel( LabyModel model)
{
this.model = model;
layers.addLabyModel(layer, model);
}
void resetModel() {
// recreate labyrinth
if (params != null) {
@@ -135,7 +145,8 @@ implements StatusListener
}
}
void refresh()
@Override
protected void refresh()
{
// reinit labyrinth view
if (params != null) {
@@ -195,97 +206,6 @@ implements StatusListener
}
}
// to allow to log / write somewher into screen...
void writeSentence(String pSentence) {
// TODO
addStatus(pSentence);
}
void writeError(String pError) {
System.err.println(pError);
}
public Drawing createDrawing() {
return new DrawingGenerator(model).createDrawing();
}
void saveImc() {
Drawing d = createDrawing();
if (d != null) {
File outfile = getFileForExtension("imc");
writeSentence("Saving to " + outfile + " ...");
try {
DataOutputStream out = new DataOutputStream(new FileOutputStream(outfile));
d.saveLinesKompressed(out);
out.flush();
out.close();
writeSentence("... Done.");
} catch (IOException io) {
io.printStackTrace(System.err);
}
}
}
File getFileForExtension(final String extension)
{
return new File(params.getSaveDir(), params.getName() + "." + extension);
}
void saveSvg() {
Drawing d = createDrawing();
if (d != null) {
File outfile = getFileForExtension("svg");
writeSentence("Saving to " + outfile + " ...");
try {
DataOutputStream out = new DataOutputStream(new FileOutputStream(outfile));
SvgWriter writer = new SvgWriter(d.getInternLines());
writer.writeTo(out);
out.flush();
out.close();
writeSentence("... Done.");
} catch (IOException io) {
io.printStackTrace(System.err);
}
} else {
writeError("drawing creation failed");
}
}
void savePng() {
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();
// this.paint(g);
maze.paint(g);
g.dispose();
try {
ImageIO.write(bi, "png", file);
} catch (Exception e) {
e.printStackTrace();
}
}
void saveText() {
File outfile = getFileForExtension("txt");
writeSentence("Saving to " + outfile + " ...");
try {
DataOutputStream out = new DataOutputStream(new FileOutputStream(outfile));
out.write(model.toLabyMap().toString().getBytes());
out.flush();
out.close();
writeSentence("... Done.");
} catch (IOException io) {
io.printStackTrace(System.err);
}
}
public void addStatus(String pStatus)
{
if ( statusEnable ) {
@@ -293,170 +213,6 @@ implements StatusListener
}
}
void loadRaw() {
File infile = new File(params.getSaveDir(), params.getName() + ".raw");
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(infile);
setModel(new MazePersistRaw().parseInputStream("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
}
}
}
}
void loadOsm(boolean add, int mulx, int muly) {
if ( maze != null ) {
new Thread() {
@Override
public void run() {
File infile = new File(params.getSaveDir(), params.getName() + ".osm");
FileInputStream inputStream = null;
try {
// TODO really use InputStream and not pathname
OsmReader reader = new OsmReader(infile.getCanonicalPath());
reader.read();
OsmToDrawing converter = new OsmToDrawing(reader, mulx,muly);
Drawing drawing = converter.getDrawing(reader.getWays());
statusEnable = false;
maze.addDrawing(drawing,add);
statusEnable = true;
} 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();
}
}
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));
statusEnable = false;
maze.addDrawing(drawing,add);
statusEnable = true;
} 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();
}
}
void loadDrawing(boolean add) {
if ( maze != null ) {
new Thread() {
@Override
public void run() {
File infile = new File(params.getSaveDir(), params.getName() + ".drawing");
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(infile);
// TODO
// model = new MazePersistRaw().parseInputStream("raw",inputStream);
Drawing drawing = new Drawing();
drawing.loadLines(new DataInputStream(inputStream));
statusEnable = false;
maze.addDrawing(drawing,add);
statusEnable = true;
} 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();
}
}
void loadWorldEdit(boolean add) {
if ( maze != null ) {
File infile = new File(params.getSaveDir(), params.getName() + ".we");
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(infile);
LabyLayers newLayers = new MazePersistWorldEdit().parseInputStream("we",inputStream);
if ( ! newLayers.isEmpty()) {
int l = layer;
for (int i = newLayers.getMin(); i <= newLayers.getMax(); i++) {
LabyModel m = newLayers.getLayer(i);
if (m != null) {
System.out.println("add layer " + l);
layers.addLabyModel(l, m);
l++;
}
}
setModel(layers.getLayer(layer));
}
} 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
}
}
}
}
}
JButton addDirection(final JPanel panel, final String direction, String key, Action goAction) {
String actionName = "go" + direction;
JButton button = new JButton(direction);
@@ -472,78 +228,23 @@ implements StatusListener
Display display = new Display(model, W, H, params);
}
void saveWorldEdit() {
File outfile = getFileForExtension("we");
if (!outfile.exists()) {
addStatus("Saving we to " + outfile + " ...");
/**
* Not part of generic Maze since using ImageIo and Graphics
*/
void savePng() {
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();
// this.paint(g);
maze.paint(g);
g.dispose();
try {
FileOutputStream out = new FileOutputStream(outfile);
new MazePersistWorldEdit(layers).streamOut("we", out);
out.close();
addStatus("... Done.");
} catch (IOException io) {
io.printStackTrace(System.err);
ImageIO.write(bi, "png", file);
} catch (Exception e) {
e.printStackTrace();
}
} else {
addStatus("we file " + outfile + " already exists");
}
}
public void saveStl(MazeParamsFixed params, LabyModel model, Maze3dParams wallparams) {
File outfile = getFileForExtension("stl");
if (!outfile.exists()) {
addStatus("Saving stl to " + outfile + " ...");
try {
FileOutputStream out = new FileOutputStream(outfile);
new Wall3dStream(params.getName(), model, out, wallparams).stream();
out.close();
addStatus("... Done.");
} catch (IOException io) {
io.printStackTrace(System.err);
}
} else {
addStatus("stl file " + outfile + " already exists");
}
}
public void save(MazeParamsFixed params, LabyModel model) {
File outfile = getFileForExtension("raw");
if (!outfile.exists()) {
addStatus("Saving to " + outfile + " ...");
try {
FileOutputStream out = new FileOutputStream(outfile);
MazePersistRaw persist = new MazePersistRaw(model);
persist.streamOut("raw", out);
out.flush();
out.close();
addStatus("... Done.");
} catch (IOException io) {
io.printStackTrace(System.err);
}
} else {
addStatus("" + outfile + " already exists");
}
}
int setLayer(int x)
{
addStatus("set layer " + x);
LabyModel layermodel = layers.getLayer(x);
if ( layermodel == null )
{
// clone it
model = new LabyModel(model);
layers.addLabyModel(x, model);
}
else
{
model = layermodel;
}
layer = x;
refresh();
return layer;
}
public static void main(String pArgs[]) {

View File

@@ -1,6 +1,7 @@
package org.artisanlogiciel.games.maze.gui;
import org.artisanlogiciel.games.maze.*;
import org.artisanlogiciel.games.maze.model.LabyModelProvider;
import org.artisanlogiciel.games.maze.solve.DirectionPosition;
import org.artisanlogiciel.games.maze.solve.MazeResolutionListener;
import org.artisanlogiciel.graphics.Drawing;
@@ -25,8 +26,7 @@ public class MazeComponent
{
private static final long serialVersionUID = 3163272907991176390L;
// WallsProvider map;
LabyModel map;
LabyModelProvider map;
final MazeCellParameters cp;
Position current = null;
LinkedList<DirectionPosition> solvedPath = null;

View File

@@ -24,7 +24,7 @@ public class MazeControler extends JPanel {
private void setMazeName(String pName) {
MazeParamsFixed p = (MazeParamsFixed) display.params;
MazeParamsFixed p = (MazeParamsFixed) display.getParams();
p.setName(pName);
}
@@ -158,8 +158,8 @@ public class MazeControler extends JPanel {
new AbstractAction() {
public void actionPerformed(ActionEvent evt) {
setMazeName(saveName.getText());
MazeParamsFixed p = (MazeParamsFixed) display.params;
display.save(p, display.model);
MazeParamsFixed p = (MazeParamsFixed) display.getParams();
display.saveRaw(p);
}
});
@@ -175,8 +175,8 @@ public class MazeControler extends JPanel {
public void actionPerformed(ActionEvent evt) {
//
setMazeName(saveName.getText());
MazeParamsFixed p = (MazeParamsFixed) display.params;
display.saveStl(p, display.model, display.stlsettings.createParams());
MazeParamsFixed p = (MazeParamsFixed) display.getParams();
display.saveStl(p, display.stlsettings.createParams());
}
};
final JButton saveStlButton = newSaveButton("stl", saveStlAction);

View File

@@ -0,0 +1,38 @@
package org.artisanlogiciel.games.maze.model;
import org.artisanlogiciel.games.maze.MovesProvider;
import org.artisanlogiciel.games.maze.WallsProvider;
public class HalfSquareModelCreator {
interface SetXY
{
void setXY(HalfSquareRasterModel model, int x, int y);
}
HalfSquareRasterModel createFromFunc(int width, int height, SetXY func)
{
HalfSquareRasterModel model = new HalfSquareRasterModel(width,height);
for (int y=0; y<model.getHeight();y++)
{
for (int x=0; x<model.getWidth();x++)
{
func.setXY(model,x,y);
}
}
return model;
}
public HalfSquareRasterModel createFromWallsProvider(WallsProvider provider)
{
SetXY setWalls = ( model, x, y ) -> { model.setWalls(x,y, provider.getWalls(x,y)); };
return createFromFunc( provider.getWidth(), provider.getHeight(), setWalls);
}
public HalfSquareRasterModel createFromMovesProvider(MovesProvider provider)
{
SetXY setMoves = ( model, x, y ) -> { model.setMoves(x,y,provider.getMoves(x,y)); };
return createFromFunc( provider.getWidth(), provider.getHeight(), setMoves);
}
}

View File

@@ -0,0 +1,105 @@
package org.artisanlogiciel.games.maze.model;
import org.artisanlogiciel.games.maze.MovesProvider;
import org.artisanlogiciel.games.maze.WallsProvider;
/**
* minimal model without repetition
* keep only left and down wall and rely on neightbor squeres to obtain right and up
* left down compatible with Bricks
*
* LEFT 01
* DOWN 10
*
* 2 bits for walls
*
* bit value :
* - set : there is a wall , no move in that direction
* - unset : move is possible , there is no wall
*/
public class HalfSquareRasterModel
implements WallsProvider,
MovesProvider,
WidthHeightProvider
{
int LEFT = 0x01;
int DOWN = 0x02;
// RIGHT 4
// UP 8
// 64 bits for long, 4 bits reserved
int USED_BITS = 60;
// 2 bits for LEFT UP, ( half square )
int BITSX = 2;
int width;
int height;
long lines[][];
public HalfSquareRasterModel(int width, int height)
{
this.width = width;
this.height = height;
lines = new long[height][(BITSX * width)/USED_BITS];
}
public void setLeftDown(int x, int y, short moves)
{
if (( x < width ) && ( y < height)) {
int index = x % (USED_BITS / BITSX);
long mask = 0xffffffff ^ ((moves & 0x3) << index);
lines[y][(BITSX * x) / USED_BITS] &= mask;
}
}
public void setWalls(int x, int y, short moves)
{
short walls = (short) ~ moves;
setMoves(x,y,moves);
}
public void setMoves(int x, int y, short moves)
{
setLeftDown(x,y,moves); // left up
setLeftDown(x+1,y,(short) (moves & (LEFT << BITSX))); // right
setLeftDown(x,y-1,(short) (moves & (DOWN << BITSX))); // up
}
public long getLeftDown(int x, int y)
{
if (( x < width ) && ( y < height)) {
// left up
long line = lines[y][(BITSX * x) / USED_BITS];
int index = x % (USED_BITS / BITSX);
return ((line >> index) & (LEFT | DOWN));
}
return 0;
}
public short getWalls(int x, int y)
{
return (short) (
getLeftDown(x,y) |
( getLeftDown(x+1,y) & LEFT ) << USED_BITS | // right
( getLeftDown( x, y-1) & DOWN) << USED_BITS // up
);
}
public short getMoves(int x, int y)
{
// moves are where there is no walls ...
return (short) ((~ getWalls(x,y)) & 0x0f);
}
@Override
public int getWidth() {
return width;
}
@Override
public int getHeight() {
return height;
}
}

View File

@@ -0,0 +1,26 @@
package org.artisanlogiciel.games.maze.model;
import org.artisanlogiciel.games.maze.MovesProvider;
import org.artisanlogiciel.games.maze.WallsProvider;
public interface LabyModelProvider
extends WallsProvider,
MovesProvider
{
/**
* add a new direction(s) exiting ones are kept
*/
void addDirection(int x, int y, short path);
/* set direction(s) existing ones are lost */
void setDirection(int x, int y, short path);
/**
* is there no wall in that direction ?
**/
boolean canMoveInDirection(int x, int y, short direction);
/** like getMoves but include resolved information */
short getPath(int x, int y);
}

View File

@@ -0,0 +1,9 @@
package org.artisanlogiciel.games.maze.model;
public interface WidthHeightProvider {
int getWidth();
int getHeight();
}