Skip to content

Commit

Permalink
Merge pull request MegaMek#5979 from SJuliez/recent-boaRDS
Browse files Browse the repository at this point in the history
Recent boards
  • Loading branch information
SJuliez authored Sep 8, 2024
2 parents 556af35 + a89c135 commit 47bce21
Show file tree
Hide file tree
Showing 8 changed files with 248 additions and 32 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ build/
#MegaMek Data Folders
# mmconf
/megamek/mmconf/clientsettings.xml
/megamek/mmconf/recent_boards.yml
/megamek/mmconf/*gameoptions.xml
/megamek/mmconf/*.properties
!/megamek/mmconf/shared.properties
Expand Down
2 changes: 2 additions & 0 deletions megamek/i18n/megamek/client/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ AdvancedOptions.ShowFPS.name=Show drawtime
#Board Editor
BoardEditor.BridgeBuildingElevError=Bridge/Building Elevation is an offset from the surface of a hex and hence must be a positive value!
BoardEditor.OpenFileError=Could not open file {0}.
BoardEditor.loadBoardError=Could not load the board file.
BoardEditor.butAddTerrain=Add/Set Terrain
BoardEditor.butBoardOpen=Open...
BoardEditor.butBoardNew=New...
Expand Down Expand Up @@ -954,6 +955,7 @@ CommonMenuBar.boardRemoveRoads=Remove Roads and Bridges
CommonMenuBar.boardRemoveBuildings=Remove Buildings and Fuel Tanks
CommonMenuBar.fileBoardNew=New
CommonMenuBar.fileBoardOpen=Open...
CommonMenuBar.fileBoardRecent=Recent Boards
CommonMenuBar.fileBoardSave=Save...
CommonMenuBar.fileBoardSaveAs=Save As...
CommonMenuBar.fileBoardSaveAsImage=Save As Image...
Expand Down
4 changes: 3 additions & 1 deletion megamek/i18n/megamek/client/messages_de.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1240,4 +1240,6 @@ CASCardPanel.printCard=Drucken
CASCardPanel.MUL=MUL öffnen
CASCardPanel.conversionReport=Umrechnungs-Bericht
CASCardPanel.font=Font:
CASCardPanel.cardSize=Kartengröße:
CASCardPanel.cardSize=Kartengröße:
Error=Fehler
BoardEditor.loadBoardError=Karte konnte nicht geladen werden.
94 changes: 64 additions & 30 deletions megamek/src/megamek/client/ui/swing/BoardEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,15 @@
import megamek.client.ui.Messages;
import megamek.client.ui.dialogs.helpDialogs.AbstractHelpDialog;
import megamek.client.ui.dialogs.helpDialogs.BoardEditorHelpDialog;
import megamek.client.ui.enums.DialogResult;
import megamek.client.ui.swing.boardview.*;
import megamek.client.ui.swing.dialog.FloodDialog;
import megamek.client.ui.swing.dialog.LevelChangeDialog;
import megamek.client.ui.swing.dialog.MMConfirmDialog;
import megamek.client.ui.swing.minimap.Minimap;
import megamek.client.ui.swing.tileset.HexTileset;
import megamek.client.ui.swing.tileset.TilesetManager;
import megamek.client.ui.swing.util.FontHandler;
import megamek.client.ui.swing.util.MegaMekController;
import megamek.client.ui.swing.util.StringDrawer;
import megamek.client.ui.swing.util.UIUtil;
import megamek.client.ui.swing.util.*;
import megamek.client.ui.swing.util.UIUtil.FixedYPanel;
import megamek.common.*;
import megamek.common.annotations.Nullable;
Expand Down Expand Up @@ -503,22 +501,9 @@ private void setupFrame() {
@Override
public void windowClosing(WindowEvent e) {
// When the board has changes, ask the user
if (hasChanges) {
ignoreHotKeys = true;
int savePrompt = JOptionPane.showConfirmDialog(null,
Messages.getString("BoardEditor.exitprompt"),
Messages.getString("BoardEditor.exittitle"),
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.WARNING_MESSAGE);
ignoreHotKeys = false;

// When the user cancels or did not actually save the board, don't close
if (((savePrompt == JOptionPane.YES_OPTION) && !boardSave(false)) ||
(savePrompt == JOptionPane.CANCEL_OPTION)) {
return;
}
if (hasChanges && (showSavePrompt() == DialogResult.CANCELLED)) {
return;
}

// otherwise: exit the Map Editor
minimapW.setVisible(false);
if (controller != null) {
Expand All @@ -535,6 +520,32 @@ public void windowClosed(WindowEvent e) {
});
}

/**
* Shows a prompt to save the current board. When the board is actually saved or the user presses
* "No" (don't want to save), returns DialogResult.CONFIRMED. In this case, the action (loading a board
* or leaving the board editor) that led to this prompt may be continued.
* In all other cases, returns DialogResult.CANCELLED, meaning the action should not be continued.
*
* @return DialogResult.CANCELLED (cancel action) or CONFIRMED (continue action)
*/
private DialogResult showSavePrompt() {
ignoreHotKeys = true;
int savePrompt = JOptionPane.showConfirmDialog(null,
Messages.getString("BoardEditor.exitprompt"),
Messages.getString("BoardEditor.exittitle"),
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.WARNING_MESSAGE);
ignoreHotKeys = false;
// When the user cancels or did not actually save the board, don't load anything
if (((savePrompt == JOptionPane.YES_OPTION) && !boardSave(false))
|| (savePrompt == JOptionPane.CANCEL_OPTION)
|| (savePrompt == JOptionPane.CLOSED_OPTION)) {
return DialogResult.CANCELLED;
} else {
return DialogResult.CONFIRMED;
}
}

/**
* Sets up Scaling Icon Buttons
*/
Expand Down Expand Up @@ -1009,10 +1020,9 @@ private void setupEditorPanel() {
addManyActionListeners(butBoardOpen, butExpandMap, butBoardNew);
addManyActionListeners(butDelTerrain, butAddTerrain, butSourceFile);

JPanel panButtons = new JPanel(new GridLayout(3, 2, 2, 2));
JPanel panButtons = new JPanel(new GridLayout(3, 3, 2, 2));
addManyButtons(panButtons, List.of(butBoardNew, butBoardSave, butBoardOpen,
butExpandMap, butBoardSaveAs, butBoardSaveAsImage));
panButtons.add(butBoardValidate);
butExpandMap, butBoardSaveAs, butBoardSaveAsImage, butBoardValidate));
if (Desktop.isDesktopSupported()) {
panButtons.add(butSourceFile);
}
Expand Down Expand Up @@ -1443,7 +1453,7 @@ public void updateMapSettings(MapSettings newSettings) {
mapSettings = newSettings;
}

public void boardLoad() {
public void loadBoard() {
JFileChooser fc = new JFileChooser(loadPath);
setDialogSize(fc);
fc.setDialogTitle(Messages.getString("BoardEditor.loadBoard"));
Expand All @@ -1454,10 +1464,11 @@ public void boardLoad() {
// I want a file, y'know!
return;
}
curBoardFile = fc.getSelectedFile();
loadPath = curBoardFile.getParentFile();
// load!
try (InputStream is = new FileInputStream(fc.getSelectedFile())) {
loadBoard(fc.getSelectedFile());
}

public void loadBoard(File file) {
try (InputStream is = new FileInputStream(file)) {
// tell the board to load!
board.load(is, null, true);
Set<String> boardTags = board.getTags();
Expand All @@ -1474,15 +1485,31 @@ public void boardLoad() {
}
cheRoadsAutoExit.setSelected(board.getRoadsAutoExit());
mapSettings.setBoardSize(board.getWidth(), board.getHeight());
curBoardFile = file;
RecentBoardList.addBoard(curBoardFile);
loadPath = curBoardFile.getParentFile();

// Now, *after* initialization of the board which will correct some errors,
// do a board validation
validateBoard(false);

refreshTerrainList();
setupUiFreshBoard();
} catch (IOException ex) {
LogManager.getLogger().error("", ex);
showBoardLoadError(ex);
initializeBoardIfEmpty();
}
}

private void showBoardLoadError(Exception ex) {
String message = Messages.getString("BoardEditor.loadBoardError") + System.lineSeparator() + ex.getMessage();
String title = Messages.getString("Error");
JOptionPane.showMessageDialog(frame, message, title, JOptionPane.ERROR_MESSAGE);
}

private void initializeBoardIfEmpty() {
if ((board == null) || (board.getWidth() == 0) || (board.getHeight() == 0)) {
boardNew(false);
}
}

Expand Down Expand Up @@ -1554,6 +1581,7 @@ private boolean boardSave(boolean saveAs) {
butSourceFile.setEnabled(true);
savedUndoStackSize = undoStack.size();
hasChanges = false;
RecentBoardList.addBoard(curBoardFile);
setFrameTitle();
return true;
} catch (IOException e) {
Expand Down Expand Up @@ -1772,7 +1800,13 @@ private void showBoardValidationReport(List<String> errors) {
//
@Override
public void actionPerformed(ActionEvent ae) {
if (ae.getActionCommand().equals(ClientGUI.BOARD_NEW)) {
if (ae.getActionCommand().startsWith(ClientGUI.BOARD_RECENT)) {
if (hasChanges && (showSavePrompt() == DialogResult.CANCELLED)) {
return;
}
String recentBoard = ae.getActionCommand().substring(ClientGUI.BOARD_RECENT.length() + 1);
loadBoard(new File(recentBoard));
} else if (ae.getActionCommand().equals(ClientGUI.BOARD_NEW)) {
ignoreHotKeys = true;
boardNew(true);
ignoreHotKeys = false;
Expand All @@ -1782,7 +1816,7 @@ public void actionPerformed(ActionEvent ae) {
ignoreHotKeys = false;
} else if (ae.getActionCommand().equals(ClientGUI.BOARD_OPEN)) {
ignoreHotKeys = true;
boardLoad();
loadBoard();
ignoreHotKeys = false;
} else if (ae.getActionCommand().equals(ClientGUI.BOARD_SAVE)) {
ignoreHotKeys = true;
Expand Down
1 change: 1 addition & 0 deletions megamek/src/megamek/client/ui/swing/ClientGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public class ClientGUI extends AbstractClientGUI implements BoardViewListener,
// board submenu
public static final String BOARD_NEW = "fileBoardNew";
public static final String BOARD_OPEN = "fileBoardOpen";
public static final String BOARD_RECENT = "recent";
public static final String BOARD_SAVE = "fileBoardSave";
public static final String BOARD_SAVE_AS = "fileBoardSaveAs";
public static final String BOARD_SAVE_AS_IMAGE = "fileBoardSaveAsImage";
Expand Down
24 changes: 24 additions & 0 deletions megamek/src/megamek/client/ui/swing/CommonMenuBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.*;

import static java.awt.event.KeyEvent.*;
Expand Down Expand Up @@ -87,6 +88,7 @@ public class CommonMenuBar extends JMenuBar implements ActionListener, IPreferen
// The Board menu
private final JMenuItem boardNew = new JMenuItem(getString("CommonMenuBar.fileBoardNew"));
private final JMenuItem boardOpen = new JMenuItem(getString("CommonMenuBar.fileBoardOpen"));
private final JMenu boardRecent = new JMenu(getString("CommonMenuBar.fileBoardRecent"));
private final JMenuItem boardSave = new JMenuItem(getString("CommonMenuBar.fileBoardSave"));
private final JMenuItem boardSaveAs = new JMenuItem(getString("CommonMenuBar.fileBoardSaveAs"));
private final JMenuItem boardSaveAsImage = new JMenuItem(getString("CommonMenuBar.fileBoardSaveAsImage"));
Expand Down Expand Up @@ -216,6 +218,8 @@ public CommonMenuBar() {
add(menu);
initMenuItem(boardNew, menu, BOARD_NEW);
initMenuItem(boardOpen, menu, BOARD_OPEN, VK_O);
initMenuItem(boardRecent, menu, BOARD_OPEN, VK_O);
initializeRecentBoardsMenu();
initMenuItem(boardSave, menu, BOARD_SAVE);
initMenuItem(boardSaveAs, menu, BOARD_SAVE_AS);
initMenuItem(boardValidate, menu, BOARD_VALIDATE);
Expand Down Expand Up @@ -335,6 +339,7 @@ public CommonMenuBar() {
setKeyBinds();
GUIP.addPreferenceChangeListener(this);
KeyBindParser.addPreferenceChangeListener(this);
RecentBoardList.addListener(this);
}

/** Sets/updates the accelerators from the KeyCommandBinds preferences. */
Expand Down Expand Up @@ -479,6 +484,7 @@ private synchronized void updateEnabledStates() {
boardSaveAsImage.setEnabled(isBoardEditor || isInGame); // TODO: should work in the lobby
boardNew.setEnabled(isBoardEditor || isMainMenu);
boardOpen.setEnabled(isBoardEditor || isMainMenu);
boardRecent.setEnabled((isBoardEditor || isMainMenu) && !RecentBoardList.getRecentBoards().isEmpty());
fileUnitsPaste.setEnabled(isLobby);
fileUnitsCopy.setEnabled(isLobby);
fileUnitsReinforce.setEnabled((isLobby || isInGame) && isNotVictory);
Expand Down Expand Up @@ -568,6 +574,8 @@ public void preferenceChange(PreferenceChangeEvent e) {
gameRoundReport.setSelected(GUIP.getMiniReportEnabled());
} else if (e.getName().equals(GUIPreferences.PLAYER_LIST_ENABLED)) {
gamePlayerList.setSelected(GUIP.getPlayerListEnabled());
} else if (e.getName().equals(RecentBoardList.RECENT_BOARDS_UPDATED)) {
initializeRecentBoardsMenu();
}
}

Expand All @@ -580,6 +588,7 @@ private void adaptToGUIScale() {
public void die() {
GUIP.removePreferenceChangeListener(this);
KeyBindParser.removePreferenceChangeListener(this);
RecentBoardList.removeListener(this);
}

private void initMenuItem(JMenuItem item, JMenu menu, String command) {
Expand All @@ -593,4 +602,19 @@ private void initMenuItem(JMenuItem item, JMenu menu, String command, int mnemon
initMenuItem(item, menu, command);
item.setMnemonic(mnemonic);
}

/**
* Updates the Recent Boards submenu with the current list of recent boards
*/
private void initializeRecentBoardsMenu() {
List<String> recentBoards = RecentBoardList.getRecentBoards();
boardRecent.removeAll();
for (String recentBoard : recentBoards) {
File boardFile = new File(recentBoard);
JMenuItem item = new JMenuItem(boardFile.getName());
initMenuItem(item, boardRecent, BOARD_RECENT + "|" + recentBoard);
}
boardRecent.setEnabled(!recentBoards.isEmpty());
adaptToGUIScale();
}
}
16 changes: 15 additions & 1 deletion megamek/src/megamek/client/ui/swing/MegaMekGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,16 @@ void showEditor() {
editor.boardNew(GUIPreferences.getInstance().getBoardEdRndStart());
}

/**
* Display the board editor and load the given board
*/
void showEditor(String boardFile) {
BoardEditor editor = new BoardEditor(controller);
controller.boardEditor = editor;
launch(editor.getFrame());
editor.loadBoard(new File(boardFile));
}

void showSkinEditor() {
int response = JOptionPane.showConfirmDialog(frame,
"The skin editor is currently "
Expand All @@ -368,7 +378,7 @@ void showEditorOpen() {
BoardEditor editor = new BoardEditor(controller);
controller.boardEditor = editor;
launch(editor.getFrame());
editor.boardLoad();
editor.loadBoard();
}

/**
Expand Down Expand Up @@ -1016,6 +1026,10 @@ void unlaunch() {
}

private final ActionListener actionListener = ev -> {
if (ev.getActionCommand().startsWith(ClientGUI.BOARD_RECENT)) {
String recentBoard = ev.getActionCommand().substring(ClientGUI.BOARD_RECENT.length() + 1);
showEditor(recentBoard);
}
switch (ev.getActionCommand()) {
case ClientGUI.BOARD_NEW:
showEditor();
Expand Down
Loading

0 comments on commit 47bce21

Please sign in to comment.