Skip to content

Commit

Permalink
Merge pull request #237 from charlestian23/puzzle-editor
Browse files Browse the repository at this point in the history
Home panel prompts for file when appropriate
  • Loading branch information
HazelCullom authored Aug 5, 2022
2 parents f8ccad2 + f0813d8 commit b28ba6c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 8 deletions.
30 changes: 28 additions & 2 deletions src/main/java/edu/rpi/legup/ui/HomePanel.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package edu.rpi.legup.ui;

import edu.rpi.legup.app.GameBoardFacade;
import edu.rpi.legup.controller.CursorController;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

public class HomePanel extends LegupPanel {
private LegupUI legupUI;
Expand All @@ -14,6 +18,28 @@ public class HomePanel extends LegupPanel {

private final int buttonSize = 100;

private ActionListener openProofListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Object[] items = legupUI.getProofEditor().promptPuzzle();
String fileName = (String) items[0];
File puzzleFile = (File) items[1];
legupUI.displayPanel(1);
legupUI.getProofEditor().loadPuzzle(fileName, puzzleFile);
}
};

private ActionListener openPuzzleListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Object[] items = legupUI.getPuzzleEditor().promptPuzzle();
String fileName = (String) items[0];
File puzzleFile = (File) items[1];
legupUI.displayPanel(2);
legupUI.getPuzzleEditor().loadPuzzle(fileName, puzzleFile);
}
};

public HomePanel(FileDialog fileDialog, JFrame frame, LegupUI legupUI) {
this.legupUI = legupUI;
this.frame = frame;
Expand Down Expand Up @@ -79,7 +105,7 @@ private void initButtons() {
this.buttons[0].setIcon(resizeButtonIcon(button0Icon, this.buttonSize, this.buttonSize));
this.buttons[0].setHorizontalTextPosition(AbstractButton.CENTER);
this.buttons[0].setVerticalTextPosition(AbstractButton.BOTTOM);
this.buttons[0].addActionListener(l -> this.legupUI.displayPanel(1));
this.buttons[0].addActionListener(CursorController.createListener(this, openProofListener));

this.buttons[1] = new JButton("New Puzzle") {
{
Expand All @@ -103,7 +129,7 @@ private void initButtons() {
this.buttons[2].setIcon(resizeButtonIcon(button2Icon, this.buttonSize, this.buttonSize));
this.buttons[2].setHorizontalTextPosition(AbstractButton.CENTER);
this.buttons[2].setVerticalTextPosition(AbstractButton.BOTTOM);
this.buttons[2].addActionListener(l -> this.legupUI.displayPanel(2)); // PLACEHOLDER
this.buttons[2].addActionListener(CursorController.createListener(this, openPuzzleListener)); // PLACEHOLDER

for (int i = 0; i < this.buttons.length - 1; i++) { // -1 to avoid the batch grader button
//this.buttons[i].setPreferredSize(new Dimension(100, 100));
Expand Down
20 changes: 17 additions & 3 deletions src/main/java/edu/rpi/legup/ui/ProofEditorPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,12 @@ public JMenuBar getMenuBar() {
return mBar;
}

public void promptPuzzle() {
public Object[] promptPuzzle() {
GameBoardFacade facade = GameBoardFacade.getInstance();
if (facade.getBoard() != null) {
if (noquit("Opening a new puzzle?")) { // !noquit or noquit?
return;
if (noquit("Opening a new puzzle?")) // !noquit or noquit?
{
return new Object[0];
}
}

Expand All @@ -331,6 +332,19 @@ public void promptPuzzle() {
puzzleFile = new File(fileName);
}

return new Object[]{fileName, puzzleFile};
}

public void loadPuzzle()
{
Object[] items = promptPuzzle();
String fileName = (String) items[0];
File puzzleFile = (File) items[1];
loadPuzzle(fileName, puzzleFile);
}

public void loadPuzzle(String fileName, File puzzleFile)
{
if (puzzleFile != null && puzzleFile.exists()) {
try {
GameBoardFacade.getInstance().loadPuzzle(fileName);
Expand Down
20 changes: 17 additions & 3 deletions src/main/java/edu/rpi/legup/ui/PuzzleEditorPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,12 @@ public void loadPuzzleFromHome(String game, int rows, int columns) throws Illega
}
}

public void promptPuzzle() {
public Object[] promptPuzzle() {
GameBoardFacade facade = GameBoardFacade.getInstance();
if (facade.getBoard() != null) {
if (noQuit("Opening a new puzzle to edit?")) { // !noquit or noquit?
return;
if (noQuit("Opening a new puzzle to edit?")) // !noquit or noquit?
{
return new Object[0];
}
}
if (fileDialog == null) {
Expand All @@ -231,6 +232,19 @@ public void promptPuzzle() {
puzzleFile = new File(fileName);
}

return new Object[]{fileName, puzzleFile};
}

public void loadPuzzle()
{
Object[] items = promptPuzzle();
String fileName = (String) items[0];
File puzzleFile = (File) items[1];
loadPuzzle(fileName, puzzleFile);
}

public void loadPuzzle(String fileName, File puzzleFile)
{
if (puzzleFile != null && puzzleFile.exists()) {
try {
GameBoardFacade.getInstance().loadPuzzleEditor(fileName);
Expand Down

0 comments on commit b28ba6c

Please sign in to comment.