Skip to content

Commit f5402e1

Browse files
committed
Fix tile placement added tests
1 parent ad843a6 commit f5402e1

File tree

5 files changed

+105
-282
lines changed

5 files changed

+105
-282
lines changed

src/main/java/jcs/commandStation/autopilot/AutoPilot.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,16 @@ public static List<LocomotiveBean> getOnTrackLocomotives() {
369369
return new ArrayList<>(activeLocomotives);
370370
}
371371

372+
public static boolean isGostDetected() {
373+
List<BlockBean> blocks = PersistenceFactory.getService().getBlocks();
374+
for (BlockBean bb : blocks) {
375+
if (BlockBean.BlockState.GHOST == bb.getBlockState()) {
376+
return true;
377+
}
378+
}
379+
return false;
380+
}
381+
372382
private static void handleGhost(SensorEvent event) {
373383
Logger.warn("Ghost Detected! @ Sensor " + event.getId());
374384
//Switch power OFF!
@@ -552,7 +562,7 @@ public void run() {
552562
now = System.currentTimeMillis();
553563
}
554564

555-
Logger.trace((dispatchersRunning ? "Not " : "") + "All dispatchers stopped in " + ((now - start) / 1000) + " s. There are "+getRunningDispatcherCount()+" Still running...");
565+
Logger.trace((dispatchersRunning ? "Not " : "") + "All dispatchers stopped in " + ((now - start) / 1000) + " s. There are " + getRunningDispatcherCount() + " Still running...");
556566

557567
if (dispatchersRunning) {
558568
for (Dispatcher ld : dispatchers.values()) {

src/main/java/jcs/commandStation/autopilot/state/StateMachineThread.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import jcs.JCS;
1919
import jcs.commandStation.autopilot.AutoPilot;
20-
import jcs.commandStation.events.SensorEvent;
2120
import jcs.commandStation.events.SensorEventListener;
2221
import jcs.entities.BlockBean;
2322
import jcs.entities.LocomotiveBean;

src/main/java/jcs/ui/JCSFrame.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import javax.swing.KeyStroke;
3737
import javax.swing.SwingUtilities;
3838
import jcs.JCS;
39+
import jcs.commandStation.autopilot.AutoPilot;
3940
import jcs.commandStation.events.DisconnectionEvent;
4041
import jcs.commandStation.events.DisconnectionEventListener;
4142
import jcs.commandStation.events.PowerEvent;
@@ -97,7 +98,6 @@ private void initJCS() {
9798

9899
//Initialize the Touchbar for MacOS
99100
if (RunUtil.isMacOSX()) {
100-
//JCS.showTouchbar(this);
101101
this.setTitle("");
102102
}
103103

@@ -179,9 +179,11 @@ public void showSettings() {
179179
}
180180

181181
public void showDesignLayoutPanel() {
182-
CardLayout card = (CardLayout) this.centerPanel.getLayout();
183-
card.show(this.centerPanel, "designPanel");
184-
this.layoutPanel.loadLayout();
182+
if (!AutoPilot.isAutoModeActive()) {
183+
CardLayout card = (CardLayout) this.centerPanel.getLayout();
184+
card.show(this.centerPanel, "designPanel");
185+
this.layoutPanel.loadLayout();
186+
}
185187
}
186188

187189
public void stop() {

src/main/java/jcs/ui/layout/LayoutCanvas.java

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -453,11 +453,14 @@ private void mousePressedAction(MouseEvent evt) {
453453
if (MouseEvent.BUTTON1 == evt.getButton() && tile == null) {
454454
//Only add a new tile when there is no tile on the selected snapPoint
455455
Logger.trace("Adding a new tile: " + tileType + " @ (" + snapPoint.x + ", " + snapPoint.y + ")");
456+
456457
Tile addedTile = addTile(snapPoint);
457-
selectedTiles.addAll(addedTile.getAllPoints());
458+
if (addedTile != null) {
459+
selectedTiles.addAll(addedTile.getAllPoints());
458460

459-
if ("false".equals(System.getProperty("batch.tile.persist", "true"))) {
460-
this.saveTile(tile);
461+
if ("false".equals(System.getProperty("batch.tile.persist", "true"))) {
462+
this.saveTile(tile);
463+
}
461464
}
462465
} else {
463466
if (tile != null) {
@@ -882,26 +885,47 @@ private Tile addTile(Point p) {
882885

883886
Tile tile = TileFactory.createTile(tileType, orientation, direction, chkp, drawGrid);
884887

885-
tiles.put(chkp, tile);
886-
//Alternative point(s) to be able to find all points
888+
//Can the tile be placed, keeping in mind the extra points
889+
boolean canBeAdded = true;
887890
if (!tile.getAltPoints().isEmpty()) {
888-
Set<Point> alt = tile.getAltPoints();
889-
for (Point ap : alt) {
890-
this.altTiles.put(ap, tile);
891+
//Check if the extra point positions are not occupied
892+
Set<Point> tilePoints = tile.getAllPoints();
893+
894+
for (Point tp : tilePoints) {
895+
if (tiles.containsKey(tp) || altTiles.containsKey(tp)) {
896+
Logger.trace("Point " + p + " occupied by " + findTile(p).getId() + " Can't add new Tile: " + tile);
897+
canBeAdded = false;
898+
}
891899
}
892900
}
893901

894-
if ("false".equals(System.getProperty("batch.tile.persist", "true"))) {
895-
this.saveTile(tile);
896-
}
902+
if (canBeAdded) {
903+
tiles.put(chkp, tile);
904+
//Alternative point(s) to be able to find all points
905+
if (!tile.getAltPoints().isEmpty()) {
906+
Set<Point> alt = tile.getAltPoints();
907+
for (Point ap : alt) {
908+
this.altTiles.put(ap, tile);
909+
}
910+
}
897911

898-
Logger.trace("Added Tile " + tile.getClass().getSimpleName() + " " + tile.getOrientation() + " @ " + tile.getCenter() + " Full repaint: " + fullRepaint);
899-
Logger.trace("Added " + tile + " There are now " + this.tiles.size() + " tiles...");
912+
if ("false".equals(System.getProperty("batch.tile.persist", "true"))) {
913+
this.saveTile(tile);
914+
}
915+
916+
Logger.trace("Added Tile " + tile.getClass().getSimpleName() + " " + tile.getOrientation() + " @ " + tile.getCenter() + " Full repaint: " + fullRepaint);
917+
Logger.trace("Added " + tile + " There are now " + this.tiles.size() + " tiles...");
918+
}
900919

901920
if (fullRepaint) {
902921
this.repaint();
903922
}
904-
return tile;
923+
924+
if (canBeAdded) {
925+
return tile;
926+
} else {
927+
return null;
928+
}
905929
}
906930

907931
void removeTiles() {

0 commit comments

Comments
 (0)