Skip to content

Commit

Permalink
Merge pull request #207 from HenryCullom/puzzle-editor
Browse files Browse the repository at this point in the history
Puzzle editor: added tile images for nurikabe
  • Loading branch information
NMiklu authored Jul 22, 2022
2 parents f1a2d6a + 992688d commit 3184bd7
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 11 deletions.
59 changes: 58 additions & 1 deletion src/main/java/edu/rpi/legup/app/GameBoardFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,14 @@ public void initializeUI() {
public void setPuzzle(Puzzle puzzle) {
this.puzzle = puzzle;
this.puzzleSolver.setPuzzleView(puzzle);
this.puzzleEditor.setPuzzleView(puzzle);
this.history.clear();
}

public void setPuzzleEditor(Puzzle puzzle) {
this.puzzle = puzzle;
this.puzzleEditor.setPuzzleView(puzzle);
// this.history.clear();
//this.history.clear();
}

public void setConfig(Config config) {
Expand Down Expand Up @@ -184,6 +185,62 @@ public void loadPuzzle(String fileName) throws InvalidFileFormatException {
}
}

public void loadPuzzleEditor(String fileName) throws InvalidFileFormatException {
try {
loadPuzzleEditor(new FileInputStream(fileName));
curFileName = fileName;
setWindowTitle(puzzle.getName(), fileName);
} catch (IOException e) {
LOGGER.error("Invalid file " + fileName, e);
throw new InvalidFileFormatException("Could not find file");
}
}

public void loadPuzzleEditor(InputStream inputStream) throws InvalidFileFormatException {
Document document;
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(inputStream);
} catch (IOException | SAXException | ParserConfigurationException e) {
LOGGER.error("Invalid file", e);
throw new InvalidFileFormatException("Could not find file");
}

Element rootNode = document.getDocumentElement();
if (rootNode.getTagName().equals("Legup")) {
try {
Node node = rootNode.getElementsByTagName("puzzle").item(0);
String qualifiedClassName = config.getPuzzleClassForName(node.getAttributes().getNamedItem("name").getNodeValue());
if (qualifiedClassName == null) {
throw new InvalidFileFormatException("Puzzle creation error: cannot find puzzle with that name");
}
LOGGER.debug("Loading " + qualifiedClassName);

Class<?> c = Class.forName(qualifiedClassName);
Constructor<?> cons = c.getConstructor();
Puzzle puzzle = (Puzzle) cons.newInstance();

PuzzleImporter importer = puzzle.getImporter();
if (importer == null) {
LOGGER.error("Puzzle importer is null");
throw new InvalidFileFormatException("Puzzle importer null");
}
importer.initializePuzzle(node);
puzzle.initializeView();
puzzle.getBoardView().onTreeElementChanged(puzzle.getTree().getRootNode());
setPuzzleEditor(puzzle);
} catch (ClassNotFoundException | NoSuchMethodException | InvocationTargetException |
IllegalAccessException | InstantiationException e) {
LOGGER.error(e);
throw new InvalidFileFormatException("Puzzle creation error");
}
} else {
LOGGER.error("Invalid file");
throw new InvalidFileFormatException("Invalid file: must be a Legup file");
}
}

/**
* Loads a puzzle file from the input stream
*
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/edu/rpi/legup/controller/ElementController.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ public void mouseReleased(MouseEvent e) {
TreePanel treePanel = GameBoardFacade.getInstance().getLegupUI().getTreePanel();
TreeView treeView = treePanel.getTreeView();
BoardView boardView = getInstance().getLegupUI().getBoardView();
if (boardView == null) {
boardView = getInstance().getLegupUI().getEditorBoardView();
}
Board board = boardView.getBoard();
ElementView elementView = boardView.getElement(e.getPoint());
TreeViewSelection selection = treeView.getSelection();
Expand Down Expand Up @@ -112,6 +115,12 @@ public void mouseEntered(MouseEvent e) {
TreeElement treeElement = boardView.getTreeElement();
DynamicView dynamicView = getInstance().getLegupUI().getDynamicBoardView();
BoardView boardView = getInstance().getLegupUI().getBoardView();
if (boardView == null) {
boardView = getInstance().getLegupUI().getEditorBoardView();
}
if (dynamicView == null) {
dynamicView = getInstance().getLegupUI().getEditorDynamicBoardView();
}
Board board = boardView.getBoard();
ElementView elementView = boardView.getElement(e.getPoint());
ElementSelection selection = boardView.getSelection();
Expand Down Expand Up @@ -146,6 +155,12 @@ public void mouseExited(MouseEvent e) {
boardView.setFocusable(false);
DynamicView dynamicView = getInstance().getLegupUI().getDynamicBoardView();
BoardView boardView = getInstance().getLegupUI().getBoardView();
if (boardView == null) {
boardView = getInstance().getLegupUI().getEditorBoardView();
}
if (dynamicView == null) {
dynamicView = getInstance().getLegupUI().getEditorDynamicBoardView();
}
ElementView element = boardView.getElement(e.getPoint());
if (element != null) {
boardView.getSelection().clearHover();
Expand All @@ -172,9 +187,15 @@ public void mouseDragged(MouseEvent e) {
@Override
public void mouseMoved(MouseEvent e) {
BoardView boardView = getInstance().getLegupUI().getBoardView();
if (boardView == null) {
boardView = getInstance().getLegupUI().getEditorBoardView();
}
Board board = boardView.getBoard();
TreeElement treeElement = boardView.getTreeElement();
DynamicView dynamicView = getInstance().getLegupUI().getDynamicBoardView();
if (dynamicView == null) {
dynamicView = getInstance().getLegupUI().getEditorDynamicBoardView();
}
ElementView elementView = boardView.getElement(e.getPoint());
ElementSelection selection = boardView.getSelection();
String error = null;
Expand Down Expand Up @@ -211,6 +232,9 @@ public void changeCell(MouseEvent e, PuzzleElement data) {
@Override
public void actionPerformed(ActionEvent e) {
BoardView boardView = getInstance().getLegupUI().getBoardView();
if (boardView == null) {
boardView = getInstance().getLegupUI().getEditorBoardView();
}
ElementView selectedElement = boardView.getSelection().getFirstSelection();
PuzzleElement puzzleElement = selectedElement.getPuzzleElement();

Expand Down Expand Up @@ -271,6 +295,9 @@ public void keyPressed(KeyEvent e) {
public void keyReleased(KeyEvent e) {
Puzzle puzzle = GameBoardFacade.getInstance().getPuzzleModule();
BoardView boardView = getInstance().getLegupUI().getBoardView();
if (boardView == null) {
boardView = getInstance().getLegupUI().getEditorBoardView();
}
Board board = boardView.getBoard();
if (board instanceof CaseBoard) {
CaseBoard caseBoard = (CaseBoard) board;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

public class BlackTile extends PlaceableElement {
public BlackTile() {
super("NURI-PLAC-0001", "Black Tile", "The black tile", "edu/rpi/legup/images/nurikabe/rules/BetweenRegions.png");
super("NURI-PLAC-0001", "Black Tile", "The black tile", "edu/rpi/legup/images/nurikabe/tiles/BlackTile.png");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
public class NumberTile extends NonPlaceableElement {
private int object_num;
public NumberTile( int num ) {
super("NURI-UNPL-0001", "Number Tile", "A numbered tile", "edu/rpi/legup/images/nurikabe/rules/BetweenRegions.png");
super("NURI-UNPL-0001", "Number Tile", "A numbered tile", "edu/rpi/legup/images/nurikabe/tiles/NumberTile.png");
object_num = num;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

public class UnknownTile extends NonPlaceableElement {
public UnknownTile() {
super("NURI-UNPL-0002", "Unknown Tile", "A blank tile", "edu/rpi/legup/images/nurikabe/rules/BetweenRegions.png");
super("NURI-UNPL-0002", "Unknown Tile", "A blank tile", "edu/rpi/legup/images/nurikabe/tiles/UnknownTile.png");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

public class WhiteTile extends PlaceableElement {
public WhiteTile() {
super("NURI-PLAC-0002", "White Tile", "The white tile", "edu/rpi/legup/images/nurikabe/rules/BetweenRegions.png");
super("NURI-PLAC-0002", "White Tile", "The white tile", "edu/rpi/legup/images/nurikabe/tiles/WhiteTile.png");
}
}
6 changes: 6 additions & 0 deletions src/main/java/edu/rpi/legup/ui/LegupUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,16 @@ public void windowDeactivated(WindowEvent e) {
public BoardView getBoardView() {
return getProofEditor().getBoardView();
}
public BoardView getEditorBoardView() {
return getPuzzleEditor().getBoardView();
}

public DynamicView getDynamicBoardView() {
return getProofEditor().getDynamicBoardView();
}
public DynamicView getEditorDynamicBoardView() {
return getPuzzleEditor().getDynamicBoardView();
}

public TreePanel getTreePanel() {
return getProofEditor().getTreePanel();
Expand Down
20 changes: 15 additions & 5 deletions src/main/java/edu/rpi/legup/ui/PuzzleEditorPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public void setMenuBar() {

// FILE
menus[0] = new JMenu("File");

// file>new
JMenuItem newPuzzle = new JMenuItem("New");
newPuzzle.addActionListener((ActionEvent) -> promptPuzzle());
Expand Down Expand Up @@ -205,7 +206,9 @@ public void promptPuzzle() {
return;
}
}

if (fileDialog == null) {
fileDialog = new FileDialog(this.frame);
}
fileDialog.setMode(FileDialog.LOAD);
fileDialog.setTitle("Select Puzzle");
fileDialog.setVisible(true);
Expand All @@ -218,7 +221,7 @@ public void promptPuzzle() {

if (puzzleFile != null && puzzleFile.exists()) {
try {
GameBoardFacade.getInstance().loadPuzzle(fileName);
GameBoardFacade.getInstance().loadPuzzleEditor(fileName);
String puzzleName = GameBoardFacade.getInstance().getPuzzleModule().getName();
frame.setTitle(puzzleName + " - " + puzzleFile.getName());
} catch (InvalidFileFormatException e) {
Expand Down Expand Up @@ -251,6 +254,10 @@ public void onClearHistory() {

}

public BoardView getBoardView() {
return boardView;
}

public JButton[] getToolBarButtons() {
return toolBarButtons;
}
Expand All @@ -274,12 +281,15 @@ public void setPuzzleView(Puzzle puzzle) {
dynamicBoardView.setBorder(titleBoard);

puzzle.addBoardListener(puzzle.getBoardView());

elementFrame.getNonPlaceableElementPanel().setElements(puzzle.getNonPlaceableElements());
elementFrame.getPlaceableElementPanel().setElements(puzzle.getPlaceableElements());
System.out.println("Setting elements");
elementFrame.setElements(puzzle);

toolBarButtons[ToolbarName.CHECK.ordinal()].setEnabled(true);
// toolBarButtons[ToolbarName.SAVE.ordinal()].setEnabled(true);
}

public DynamicView getDynamicBoardView() {
return dynamicBoardView;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ public void setElements(List<? extends Element> elements) {
clearButtons();

elementButtons = new ElementButton[elements.size()];

System.out.println("adding " + elements.size() + " elements to panel");
for (int i = 0; i < elements.size(); i++) {
Element element = elements.get(i);
elementButtons[i] = new ElementButton(element);
elementFrame.getButtonGroup().add(elementButtons[i]);
System.out.printf("added button: %d, element %s\n", i, element.getElementName());

elementButtons[i].setToolTipText(element.getElementName() + ": " + element.getDescription());
elementButtons[i].addActionListener(elementFrame.getController());
Expand Down

0 comments on commit 3184bd7

Please sign in to comment.