Move (most) hardcoded defaults to MazeDefault

- harcoded values centralized
- IntegerField for JTextField containing numbers
- fix, can draw even if topleft position is not (0,0) ( ie if scrollbar were used )
This commit is contained in:
philippe lhardy
2020-12-15 22:26:57 +01:00
parent 14b6d9ff1d
commit b8cb7394cd
8 changed files with 274 additions and 110 deletions

View File

@@ -1,8 +1,8 @@
package org.artisanlogiciel.games.maze.gui;
import org.artisanlogiciel.games.maze.*;
import org.artisanlogiciel.games.maze.persist.MazePersistWorldEdit;
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;
@@ -10,7 +10,6 @@ import org.artisanlogiciel.graphics.Drawing;
import org.artisanlogiciel.graphics.SvgWriter;
import org.artisanlogiciel.osm.OsmReader;
import org.artisanlogiciel.osm.convert.OsmToDrawing;
import org.artisanlogiciel.util.UTF8Control;
import org.artisanlogiciel.xpm.Xpm;
import javax.imageio.ImageIO;
@@ -25,7 +24,6 @@ import java.awt.event.ComponentEvent;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Locale;
import java.util.ResourceBundle;
/**
* Display is Main JFrame for this tool
@@ -36,7 +34,6 @@ implements StatusListener
// to please eclipse, not supposed to be serialized
private static final long serialVersionUID = 8500214871372184418L;
public final static ResourceBundle labels = ResourceBundle.getBundle("LabelsBundle", Locale.getDefault(), Display.class.getClassLoader(), new UTF8Control());
LabyLayers layers = new LabyLayers();
int layer = 0;
@@ -54,7 +51,7 @@ implements StatusListener
Maze3dSettings stlsettings;
Display(LabyModel model, int W, int H, MazeParams params) {
super(labels.getString("title"));
super(MazeDefault.labels.getString("title"));
if (params != null) {
// fixedParams = new MazeParamsFixed(params.getSaveDir(),params.getWidth(),params.getHeight(),params.getMaxDepth());
this.params = params;
@@ -64,13 +61,13 @@ implements StatusListener
Container con = this.getContentPane();
JScrollPane scrollableMaze = new JScrollPane(maze);
scrollableMaze.addMouseMotionListener(maze);
con.add(scrollableMaze, BorderLayout.CENTER);
controler = new MazeControler(params);
con.add(controler.getMoveControl(), BorderLayout.NORTH);
con.add(controler.getGenerationControl(), BorderLayout.SOUTH);
addComponentListener(new ComponentAdapter() {
/*
scrollableMaze.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
if (autoSize) {
@@ -78,6 +75,8 @@ implements StatusListener
}
}
});
*/
model.setMazeListener(maze);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
@@ -86,17 +85,16 @@ implements StatusListener
}
private MazeComponent createMazeComponent(LabyModel model, int W, int H) {
MazeCellParameters cp = new MazeCellParameters(model.getWidth(), model.getHeight(), W, H, 3, 3);
MazeCellParameters cp = new MazeCellParameters(model.getWidth(), model.getHeight(), W, H, 0, 0);
MazeComponent comp = new MazeComponent(model, cp, this);
Xpm xpm = new Xpm();
// HARDCODED FOR FIRST TEST FIXME !!!
try {
xpm.parse(new FileInputStream("lab/titou20.xpm"));
xpm.parse(new FileInputStream(MazeDefault.getInstance().getXpmBackground()));
comp.setXpm(xpm);
}
catch(Exception e)
{
System.err.println("Hardcoded background to fix");
System.err.println("Missing background file " + e);
}
return comp;
}
@@ -310,7 +308,7 @@ implements StatusListener
}
private JMenu createLoadingMenu() {
JMenu loadMenu = new JMenu(labels.getString("load") );
JMenu loadMenu = new JMenu(MazeDefault.labels.getString("load") );
final JTextField loadName = new JTextField("newlaby");
@@ -331,7 +329,7 @@ implements StatusListener
}
};
JButton loadRawButton = new JButton(labels.getString("load" ) + " raw");
JButton loadRawButton = new JButton(MazeDefault.labels.getString("load" ) + " raw");
loadRawButton.addActionListener(loadRawAction);
loadMenu.add(loadRawButton);
@@ -350,7 +348,7 @@ implements StatusListener
}
};
JButton loadImcButton = new JButton(labels.getString("load" ) + " imc");
JButton loadImcButton = new JButton(MazeDefault.labels.getString("load" ) + " imc");
loadImcButton.addActionListener(loadImcAction);
loadMenu.add(loadImcButton);
@@ -370,7 +368,7 @@ implements StatusListener
}
};
JButton loadDrawingButton = new JButton(labels.getString("load" ) + " Drawing");
JButton loadDrawingButton = new JButton(MazeDefault.labels.getString("load" ) + " Drawing");
loadDrawingButton.addActionListener(loadDrawingAction);
loadMenu.add(loadDrawingButton);
@@ -481,7 +479,7 @@ implements StatusListener
stlsettings = new Maze3dSettings(new Maze3dParams());
JMenu saveMenu = new JMenu(labels.getString("save") );
JMenu saveMenu = new JMenu(MazeDefault.labels.getString("save") );
saveMenu.add(saveName);
saveMenu.add(saveSvgButton);
saveMenu.add(savePngButton);
@@ -497,7 +495,7 @@ implements StatusListener
}
private JButton newActionButton(String actionName, String format, Action action) {
final JButton saveTextButton = new JButton(labels.getString(actionName) + " " + format);
final JButton saveTextButton = new JButton(MazeDefault.labels.getString(actionName) + " " + format);
saveTextButton.addActionListener(action);
return saveTextButton;
}
@@ -513,7 +511,7 @@ implements StatusListener
private JPanel createResolveQuitBar() {
JPanel resolveQuitBar = new JPanel(new FlowLayout());
JButton buttonCreate = new JButton(labels.getString("create"));
JButton buttonCreate = new JButton(MazeDefault.labels.getString("create"));
buttonCreate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
recreateModel();
@@ -521,7 +519,7 @@ implements StatusListener
});
resolveQuitBar.add(buttonCreate);
JButton resolveButton = new JButton(labels.getString("resolve"));
JButton resolveButton = new JButton(MazeDefault.labels.getString("resolve"));
resolveButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
//
@@ -532,7 +530,7 @@ implements StatusListener
resolveQuitBar.add(resolveButton);
JButton reverseButton = new JButton(labels.getString("reverse"));
JButton reverseButton = new JButton(MazeDefault.labels.getString("reverse"));
reverseButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
//
@@ -544,7 +542,7 @@ implements StatusListener
resolveQuitBar.add(reverseButton);
final JButton quitButton = new JButton(labels.getString("quit"));
final JButton quitButton = new JButton(MazeDefault.labels.getString("quit"));
Action quitAction = new AbstractAction() {
public void actionPerformed(ActionEvent evt) {
//
@@ -556,7 +554,7 @@ implements StatusListener
resolveQuitBar.add(quitButton);
JButton buttonReset = new JButton("reset");//labels.getString("reset"));
JButton buttonReset = new JButton("reset");//MazeDefault.labels.getString("reset"));
buttonReset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
resetModel();
@@ -591,7 +589,7 @@ implements StatusListener
goNorth();
}
};
JButton north = addDirection(this, labels.getString("north"), "UP", goNorth);
JButton north = addDirection(this, MazeDefault.labels.getString("north"), "UP", goNorth);
@SuppressWarnings("serial")
Action goEast = new AbstractAction() {
@@ -602,7 +600,7 @@ implements StatusListener
}
};
JButton east = addDirection(this, labels.getString("east"), "RIGHT", goEast);
JButton east = addDirection(this, MazeDefault.labels.getString("east"), "RIGHT", goEast);
@SuppressWarnings("serial")
Action goWest = new AbstractAction() {
@@ -612,7 +610,7 @@ implements StatusListener
goWest();
}
};
JButton west = addDirection(this, labels.getString("west"), "LEFT", goWest);
JButton west = addDirection(this, MazeDefault.labels.getString("west"), "LEFT", goWest);
@SuppressWarnings("serial")
Action goSouth = new AbstractAction() {
@@ -622,7 +620,7 @@ implements StatusListener
goSouth();
}
};
JButton south = addDirection(this, labels.getString("south"), "DOWN", goSouth);
JButton south = addDirection(this, MazeDefault.labels.getString("south"), "DOWN", goSouth);
controlMovesPanel.add(north, BorderLayout.NORTH);
controlMovesPanel.add(west, BorderLayout.WEST);
@@ -988,7 +986,7 @@ implements StatusListener
setupDisplay(model, W, H, null);
} else {
MazeParamsFixed params = new MazeParamsFixed(new File("lab"), 20, 20, 12,1024L);
MazeParamsFixed params = new MazeParamsFixed(MazeDefault.getInstance().getParams());
model = new LabyModel(params);
setupDisplay(model, W, H, params);

View File

@@ -1,20 +1,22 @@
package org.artisanlogiciel.games.maze.gui;
import org.artisanlogiciel.games.maze.gui.component.IntegerField;
import org.artisanlogiciel.games.maze.gui.component.Panel;
import org.artisanlogiciel.games.stl.Maze3dParams;
import javax.swing.*;
public class Maze3dSettings
extends JPanel
extends Panel
{
// grid size
JTextField xl;
JTextField yl;
JTextField zl;
IntegerField xl;
IntegerField yl;
IntegerField zl;
JTextField w;
JTextField lg;
JTextField hg;
IntegerField w;
IntegerField lg;
IntegerField hg;
JCheckBox reverse;
@@ -28,42 +30,39 @@ public class Maze3dSettings
void createSettingsGui() {
if (params != null) {
JLabel widthLabel = new JLabel(Display.labels.getString("width"));
xl = new JTextField("0" + params.getXl());
add(widthLabel);
add(xl);
JLabel heightLabel = new JLabel(Display.labels.getString("height"));
zl = new JTextField("0" + params.getZl());
add(heightLabel);
add(zl);
JLabel depthLabel = new JLabel(Display.labels.getString("depth"));
yl = new JTextField("0" + params.getYl());
add(depthLabel);
add(yl);
IntegerField xl = new IntegerField("width",params.getXl());
addField(xl);
IntegerField zl = new IntegerField("height",params.getZl());
addField(zl);
IntegerField yl = new IntegerField("depth",params.getYl());
addField(yl);
reverse = new JCheckBox("reverse",params.isReverse());
add(reverse);
w = new JTextField("0" + params.getW());
add(w);
w = new IntegerField(params.getW());
addField(w);
// lowground hightground
lg = new JTextField("0" + params.getLg());
add(lg);
hg = new JTextField("0" + params.getHg());
add(hg);
lg = new IntegerField(params.getLg());
addField(lg);
hg = new IntegerField(params.getHg());
addField(hg);
}
}
Maze3dParams createParams()
{
return new Maze3dParams(
Integer.parseInt(xl.getText()),
Integer.parseInt(yl.getText()),
Integer.parseInt(zl.getText()),
Integer.parseInt(w.getText()),
Integer.parseInt(lg.getText()),
Integer.parseInt(hg.getText()),
xl.getValue(),
yl.getValue(),
zl.getValue(),
w.getValue(),
lg.getValue(),
hg.getValue(),
reverse.isSelected());
}

View File

@@ -8,26 +8,22 @@ import org.artisanlogiciel.games.maze.solve.DirectionPosition;
import java.awt.*;
public class MazeCellParameters {
double width = 10; // width of one cell
double height = 10; // height of one cell
int offsetX = 5; // x offset of upper corner left
int offsetY = 5; // y offset of upper corner left
int offsetX; // x offset of upper corner left in pixels
int offsetY; // y offset of upper corner left in pixers
int mapWidth = 0;
int mapHeight = 0;
int mapWidth;
int mapHeight;
// computed see adaptTo
double width; // width of one cell in pixels
double height; // height of one cell in pixels
public MazeCellParameters(int mapw, int maph, int W, int H, int x, int y) {
double w = (W - x) / mapw;
double h = (H - y) / maph;
mapWidth = mapw;
mapHeight = maph;
if (w < 5)
w = 5;
if (h < 5)
h = 5;
setCellSize(w, h);
offsetX = x;
offsetY = y;
adaptTo(W,H);
}
public void resetMazeWidthHeight(int mapw, int maph) {
@@ -38,8 +34,6 @@ public class MazeCellParameters {
public void adaptTo(double W, double H) {
double w = (W - offsetX) / mapWidth;
double h = (H - offsetY) / mapHeight;
mapWidth = mapWidth;
mapHeight = mapHeight;
if (w < 5)
w = 5;
if (h < 5)
@@ -47,6 +41,13 @@ public class MazeCellParameters {
setCellSize(w, h);
}
// for a given (x,y) pixel return cell position.
Position toMazeCoordinates(int x, int y) {
int pX = (int) ((double) (x - getOffsetX()) / getWidth());
int pY = (int) ((double) (y - getOffsetY()) / getHeight());
return new Position(pX, pY);
}
public void setCellSize(double w, double h) {
width = w;
height = h;

View File

@@ -20,7 +20,9 @@ public class MazeComponent
extends JComponent
implements MazeCreationListener,
MazeResolutionListener,
MouseMotionListener {
MouseMotionListener,
Scrollable
{
private static final long serialVersionUID = 3163272907991176390L;
// WallsProvider map;
@@ -41,8 +43,8 @@ public class MazeComponent
Xpm xpm = null;
Date lastDrag = null;
// FIXME HARCDODED delay after which a draging to draw a continous line is ended, a new line will then start.
long dragTimeout = 200;
// delay after which a draging to draw a continuous line is ended, a new line will then start.
long dragTimeout = MazeDefault.dragTimeout;
// not set by default for debug, will show any path
boolean showAll = false;
@@ -72,17 +74,19 @@ public class MazeComponent
{
this.xpm = xpm;
}
// for a given (x,y) pixel return cell position.
// if rightmost position of view not (0,0) this is not working.
Position getPosition(int x, int y) {
int pX = (int) ((double) (x - cp.getOffsetX()) / cp.getWidth());
int pY = (int) ((double) (y - cp.getOffsetY()) / cp.getHeight());
return new Position(pX, pY);
// if rightmost position of this view not (0,0) this is not working.
// that's why MousListener should be attache to this MazeComponent and not to Scrollable one...
return cp.toMazeCoordinates(x,y);
}
@Override
public void mouseDragged(MouseEvent e) {
boolean add = ((e.getModifiersEx() & InputEvent.BUTTON1_DOWN_MASK) != 0);
// where pixel -> maze coordinate is done
Position newPosition = getPosition(e.getX(), e.getY());
Date now = new Date(System.currentTimeMillis());
if (lastDrag == null) {
@@ -117,6 +121,8 @@ public class MazeComponent
}
}
// button 1 : add direction; button 2 : replace with direction.
// Position is Already in maze coordinates (ie not pixels)
public void addPosition(Position newPosition, boolean add) {
// should find the cell ...
DirectionPosition last = null;
@@ -134,7 +140,6 @@ public class MazeComponent
while (!last.getPosition().equals(newPosition)) {
path = LabyModel.getDirection(last.getPosition(), newPosition);
last.setDirection(path);
// button 1 : add direction; button 2 : rep lace with direction.
if (add) {
map.addDirection(last.getPosition().getX(), last.getPosition().getY(), path);
} else {
@@ -219,6 +224,7 @@ public class MazeComponent
gX = map.getWidth() - 1;
gY = map.getHeight() - 1;
this.statusListener = statusListener;
addMouseMotionListener(this);
}
// public void resetWallsProvider(WallsProvider map)
@@ -242,7 +248,8 @@ public class MazeComponent
public int getAutoSize() {
Rectangle r = getBounds();
cp.adaptTo((int) r.getWidth(), (int) r.getHeight());
// cp.resetMazeWidthHeight( (int)r.getWidth(), (int) r.getHeight());
cp.adaptTo(r.getWidth(), r.getHeight());
// should redraw ...
invalidate();
repaint();
@@ -412,4 +419,29 @@ public class MazeComponent
public void resetResolution() {
solvedPath = null;
}
@Override
public Dimension getPreferredScrollableViewportSize() {
return cp.getDimension();
}
@Override
public int getScrollableUnitIncrement(Rectangle rectangle, int i, int i1) {
return ( i == SwingConstants.VERTICAL ) ? (int) cp.getHeight() : (int) cp.getWidth();
}
@Override
public int getScrollableBlockIncrement(Rectangle rectangle, int i, int i1) {
return ( i == SwingConstants.VERTICAL ) ? (int) cp.getHeight() : (int) cp.getWidth();
}
@Override
public boolean getScrollableTracksViewportWidth() {
return false;
}
@Override
public boolean getScrollableTracksViewportHeight() {
return false;
}
}

View File

@@ -0,0 +1,49 @@
package org.artisanlogiciel.games.maze.gui;
import org.artisanlogiciel.games.maze.MazeParams;
import org.artisanlogiciel.games.maze.MazeParamsFixed;
import org.artisanlogiciel.util.UTF8Control;
import java.io.File;
import java.util.Locale;
import java.util.ResourceBundle;
/**
*
* All defaults
* Any Hardcoded value should end here
*
*/
public class MazeDefault {
public final static ResourceBundle labels = ResourceBundle.getBundle("LabelsBundle", Locale.getDefault(), Display.class.getClassLoader(), new UTF8Control());
private final static MazeDefault instance;
static {
instance = new MazeDefault();
}
// HARDCODED Default
private MazeParamsFixed mParams = new MazeParamsFixed(new File("lab"), 300, 300, 20,1024L);
// private MazeCellParameters mazeCellParameters = new MazeCellParameters();
// delay after which a draging to draw a continuous line is ended, a new line will then start.
public static final long dragTimeout = 200;
public static MazeDefault getInstance()
{
return instance;
}
public MazeParams getParams()
{
return mParams;
}
public File getXpmBackground()
{
return new File("lab/titou20.xpm");
}
}

View File

@@ -2,19 +2,21 @@ package org.artisanlogiciel.games.maze.gui;
import org.artisanlogiciel.games.maze.MazeParams;
import org.artisanlogiciel.games.maze.MazeParamsFixed;
import org.artisanlogiciel.games.maze.gui.component.IntegerField;
import org.artisanlogiciel.games.maze.gui.component.Panel;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.util.Random;
public class MazeSettings extends JPanel {
public class MazeSettings extends Panel {
MazeParams params;
JTextField textWidth = null;
JTextField textHeight = null;
JTextField textDepth = null;
JTextField textSeed = null;
IntegerField fieldWidth = null;
IntegerField fieldHeight = null;
IntegerField fieldDepth = null;
IntegerField fieldSeed = null;
JCheckBox onewaywallCB = null;
// TODO set width and height and depth of maze with gui
@@ -25,35 +27,39 @@ public class MazeSettings extends JPanel {
}
void createSettingsGui() {
MazeParams defaultParams = MazeDefault.getInstance().getParams();
if (params != null) {
final JSlider slider = new JSlider(1, 100);
final JSlider slider = new JSlider(1,
Math.max(
Math.max(defaultParams.getHeight(),defaultParams.getHeight()),
Math.max(defaultParams.getWidth(),params.getWidth())));
slider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
// settextWidthWallSize(slider.getValue());
String valueStr = "" + slider.getValue();
textWidth.setText(valueStr);
textHeight.setText(valueStr);
fieldWidth.getTextField().setText(valueStr);
fieldHeight.getTextField().setText(valueStr);
}
});
add(slider);
JLabel widthLabel = new JLabel(Display.labels.getString("width"));
textWidth = new JTextField("0" + params.getWidth());
add(widthLabel);
add(textWidth);
JLabel heightLabel = new JLabel(Display.labels.getString("height"));
textHeight = new JTextField("0" + params.getHeight());
add(heightLabel);
add(textHeight);
JLabel depthLabel = new JLabel(Display.labels.getString("depth"));
textDepth = new JTextField("0" + params.getMaxDepth());
add(depthLabel);
add(textDepth);
JLabel seedLabel = new JLabel(Display.labels.getString("seed"));
textSeed = new JTextField( "" + params.getSeed(),16);
add(seedLabel);
add(textSeed);
fieldWidth = new IntegerField("width",params.getWidth());
addField(fieldWidth);
fieldHeight = new IntegerField("height",params.getHeight());
addField(fieldHeight);
fieldDepth = new IntegerField("depth",params.getMaxDepth());
addField(fieldDepth);
JLabel seedLabel = new JLabel();
fieldSeed = new IntegerField("seed", params.getSeed());
addField(fieldSeed);
onewaywallCB = new JCheckBox("one way wall", false);
add(onewaywallCB);
}
@@ -61,12 +67,12 @@ public class MazeSettings extends JPanel {
public MazeParams resetParams() {
params = new MazeParamsFixed(params.getSaveDir(),
Integer.parseInt(textWidth.getText()),
Integer.parseInt(textHeight.getText()),
Integer.parseInt(textDepth.getText()),
fieldWidth.getValue(),
fieldHeight.getValue(),
fieldDepth.getValue(),
new Random().nextLong()
);
textSeed.setText("" + params.getSeed());
fieldSeed.getTextField().setText("" + params.getSeed());
return params;
}

View File

@@ -0,0 +1,65 @@
package org.artisanlogiciel.games.maze.gui.component;
import org.artisanlogiciel.games.maze.gui.Display;
import org.artisanlogiciel.games.maze.gui.MazeDefault;
import javax.swing.*;
public class IntegerField
{
JLabel mLabel;
JTextField mTextField;
long mDefaultValue;
public IntegerField(String label, long defaultValue)
{
mDefaultValue = defaultValue;
// DEPENDENCY
mLabel = new JLabel(MazeDefault.labels.getString(label));
mTextField = new JTextField("" + defaultValue);
}
public IntegerField(int defaultValue)
{
mDefaultValue = defaultValue;
mLabel = new JLabel("");
mTextField = new JTextField("" + defaultValue);
}
public JLabel getLabel()
{
return mLabel;
}
public JTextField getTextField()
{
return mTextField;
}
public int getValue()
{
try {
return Integer.parseInt(mTextField.getText());
}
catch (Exception e)
{
mTextField.setText("" + mDefaultValue);
}
return (int) mDefaultValue;
}
public long getLongValue()
{
try {
return Long.parseLong(mTextField.getText());
}
catch (Exception e)
{
mTextField.setText("" + mDefaultValue);
}
return (int) mDefaultValue;
}
}

View File

@@ -0,0 +1,14 @@
package org.artisanlogiciel.games.maze.gui.component;
import javax.swing.*;
public class Panel
extends JPanel {
protected void addField(IntegerField pField)
{
add(pField.getLabel());
add(pField.getTextField());
}
}