Skip to content

Commit

Permalink
Merge pull request #6265 from Scoppio/game-options-interface
Browse files Browse the repository at this point in the history
feat: config interface extracted
  • Loading branch information
HammerGS authored Dec 15, 2024
2 parents c6de86e + 5eed67e commit ca68581
Show file tree
Hide file tree
Showing 78 changed files with 844 additions and 843 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
package megamek.client.ui.advancedsearch;

import megamek.common.options.AbstractOptions;
import megamek.common.options.IGameOptions;
import megamek.common.options.IOption;
import megamek.common.options.IOptionGroup;

Expand All @@ -34,7 +34,7 @@ class TriStateItemList {

private final JList<TriStateItem> list = new JList<>(new DefaultListModel<>());

TriStateItemList(AbstractOptions opts, int visibleRows) {
TriStateItemList(IGameOptions opts, int visibleRows) {
List<String> qs = new ArrayList<>();
for (final Enumeration<IOptionGroup> optionGroups = opts.getGroups(); optionGroups.hasMoreElements(); ) {
final IOptionGroup group = optionGroups.nextElement();
Expand Down
7 changes: 4 additions & 3 deletions megamek/src/megamek/client/ui/swing/ClientGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
import megamek.common.equipment.WeaponMounted;
import megamek.common.event.*;
import megamek.common.icons.Camouflage;
import megamek.common.options.GameOptions;
import megamek.common.preference.IPreferenceChangeListener;
import megamek.common.preference.PreferenceChangeEvent;
import megamek.common.util.AddBotUtil;
Expand Down Expand Up @@ -688,7 +689,7 @@ private void showSettings() {
private void showOptions() {
getGameOptionsDialog().setEditable(client.getGame().getPhase().isLounge());
// Display the game options dialog.
getGameOptionsDialog().update(client.getGame().getOptions());
getGameOptionsDialog().update((GameOptions) client.getGame().getOptions());
getGameOptionsDialog().setVisible(true);
}

Expand Down Expand Up @@ -1928,7 +1929,7 @@ protected void loadListFile(Player player, boolean reinforce) {

try {
// Read the units from the file.
final Vector<Entity> loadedUnits = new MULParser(unitFile, getClient().getGame().getOptions())
final Vector<Entity> loadedUnits = new MULParser(unitFile, (GameOptions) getClient().getGame().getOptions())
.getEntities();

// in the Lounge, set default deployment to "Before Game Start", round 0
Expand Down Expand Up @@ -2416,7 +2417,7 @@ public void gameEnd(GameEndEvent e) {
public void gameSettingsChange(GameSettingsChangeEvent evt) {
if ((gameOptionsDialog != null) && gameOptionsDialog.isVisible() &&
!evt.isMapSettingsOnlyChange()) {
gameOptionsDialog.update(getClient().getGame().getOptions());
gameOptionsDialog.update((GameOptions) getClient().getGame().getOptions());
}

if (curPanel instanceof ChatLounge) {
Expand Down
80 changes: 40 additions & 40 deletions megamek/src/megamek/client/ui/swing/CommonSettingsDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ private <T> void moveElement(DefaultListModel<T> srcModel, int srcIndex, int trg
private ClientGUI clientgui = null;

private static final GUIPreferences GUIP = GUIPreferences.getInstance();
private static final ClientPreferences CP = PreferenceManager.getClientPreferences();
private static final ClientPreferences CLIENT_PREFERENCES = PreferenceManager.getClientPreferences();
private static final UnitDisplayOrderPreferences UDOP = UnitDisplayOrderPreferences.getInstance();
private static final ButtonOrderPreferences BOP = ButtonOrderPreferences.getInstance();

Expand Down Expand Up @@ -1942,7 +1942,7 @@ public void setVisible(boolean visible) {
// Select the correct char set (give a nice default to start).
unitStartChar.setSelectedIndex(0);
for (int loop = 0; loop < unitStartChar.getItemCount(); loop++) {
if (unitStartChar.getItemAt(loop).charAt(0) == CP.getUnitStartChar()) {
if (unitStartChar.getItemAt(loop).charAt(0) == CLIENT_PREFERENCES.getUnitStartChar()) {
unitStartChar.setSelectedIndex(loop);
break;
}
Expand All @@ -1956,31 +1956,31 @@ public void setVisible(boolean visible) {
tfSoundMuteMyTurnFileName.setText(GUIP.getSoundBingFilenameMyTurn());
tfSoundMuteOthersFileName.setText(GUIP.getSoundBingFilenameOthersTurn());

maxPathfinderTime.setText(Integer.toString(CP.getMaxPathfinderTime()));
maxPathfinderTime.setText(Integer.toString(CLIENT_PREFERENCES.getMaxPathfinderTime()));

keepGameLog.setSelected(CP.keepGameLog());
keepGameLog.setSelected(CLIENT_PREFERENCES.keepGameLog());
gameLogFilename.setEnabled(keepGameLog.isSelected());
gameLogFilename.setText(CP.getGameLogFilename());
userDir.setText(CP.getUserDir());
mmlPath.setText(CP.getMmlPath());
stampFilenames.setSelected(CP.stampFilenames());
gameLogFilename.setText(CLIENT_PREFERENCES.getGameLogFilename());
userDir.setText(CLIENT_PREFERENCES.getUserDir());
mmlPath.setText(CLIENT_PREFERENCES.getMmlPath());
stampFilenames.setSelected(CLIENT_PREFERENCES.stampFilenames());
stampFormat.setEnabled(stampFilenames.isSelected());
stampFormat.setText(CP.getStampFormat());
reportKeywordsTextPane.setText(CP.getReportKeywords());
showIPAddressesInChat.setSelected(CP.getShowIPAddressesInChat());
startSearchlightsOn.setSelected(CP.getStartSearchlightsOn());
stampFormat.setText(CLIENT_PREFERENCES.getStampFormat());
reportKeywordsTextPane.setText(CLIENT_PREFERENCES.getReportKeywords());
showIPAddressesInChat.setSelected(CLIENT_PREFERENCES.getShowIPAddressesInChat());
startSearchlightsOn.setSelected(CLIENT_PREFERENCES.getStartSearchlightsOn());

defaultAutoejectDisabled.setSelected(CP.defaultAutoejectDisabled());
useAverageSkills.setSelected(CP.useAverageSkills());
useGPinUnitSelection.setSelected(CP.useGPinUnitSelection());
generateNames.setSelected(CP.generateNames());
showUnitId.setSelected(CP.getShowUnitId());
defaultAutoejectDisabled.setSelected(CLIENT_PREFERENCES.defaultAutoejectDisabled());
useAverageSkills.setSelected(CLIENT_PREFERENCES.useAverageSkills());
useGPinUnitSelection.setSelected(CLIENT_PREFERENCES.useGPinUnitSelection());
generateNames.setSelected(CLIENT_PREFERENCES.generateNames());
showUnitId.setSelected(CLIENT_PREFERENCES.getShowUnitId());

int index = 0;
if (CP.getLocaleString().startsWith("de")) {
if (CLIENT_PREFERENCES.getLocaleString().startsWith("de")) {
index = 1;
}
if (CP.getLocaleString().startsWith("ru")) {
if (CLIENT_PREFERENCES.getLocaleString().startsWith("ru")) {
index = 2;
}
displayLocale.setSelectedIndex(index);
Expand Down Expand Up @@ -2008,7 +2008,7 @@ public void setVisible(boolean visible) {
for (int i = 0; i < tileSets.size(); i++) {
String name = tileSets.get(i);
tileSetChoice.addItem(name.substring(0, name.length() - 8));
if (name.equals(CP.getMapTileset())) {
if (name.equals(CLIENT_PREFERENCES.getMapTileset())) {
tileSetChoice.setSelectedIndex(i);
}
}
Expand Down Expand Up @@ -2413,7 +2413,7 @@ protected void okAction() {
logger.error(ex, "okAction");
}
GUIP.setValue(GUIPreferences.GUI_SCALE, (float) (guiScale.getValue()) / 10);
CP.setUnitStartChar(((String) unitStartChar.getSelectedItem()).charAt(0));
CLIENT_PREFERENCES.setUnitStartChar(((String) unitStartChar.getSelectedItem()).charAt(0));

GUIP.setMouseWheelZoom(mouseWheelZoom.isSelected());
GUIP.setMouseWheelZoomFlip(mouseWheelZoomFlip.isSelected());
Expand All @@ -2430,33 +2430,33 @@ protected void okAction() {
GUIP.setSoundBingFilenameOthersTurn(tfSoundMuteOthersFileName.getText());

try {
CP.setMaxPathfinderTime(Integer.parseInt(maxPathfinderTime.getText()));
CLIENT_PREFERENCES.setMaxPathfinderTime(Integer.parseInt(maxPathfinderTime.getText()));
} catch (Exception ex) {
logger.error(ex, "okAction");
}

GUIP.setGetFocus(getFocus.isSelected());

CP.setKeepGameLog(keepGameLog.isSelected());
CP.setGameLogFilename(gameLogFilename.getText());
CP.setUserDir(userDir.getText());
CP.setMmlPath(mmlPath.getText());
CP.setStampFilenames(stampFilenames.isSelected());
CP.setStampFormat(stampFormat.getText());
CP.setReportKeywords(reportKeywordsTextPane.getText());
CP.setShowIPAddressesInChat(showIPAddressesInChat.isSelected());
CP.setStartSearchlightsOn(startSearchlightsOn.isSelected());

CP.setDefaultAutoejectDisabled(defaultAutoejectDisabled.isSelected());
CP.setUseAverageSkills(useAverageSkills.isSelected());
CP.setUseGpInUnitSelection(useGPinUnitSelection.isSelected());
CP.setGenerateNames(generateNames.isSelected());
CP.setShowUnitId(showUnitId.isSelected());
CLIENT_PREFERENCES.setKeepGameLog(keepGameLog.isSelected());
CLIENT_PREFERENCES.setGameLogFilename(gameLogFilename.getText());
CLIENT_PREFERENCES.setUserDir(userDir.getText());
CLIENT_PREFERENCES.setMmlPath(mmlPath.getText());
CLIENT_PREFERENCES.setStampFilenames(stampFilenames.isSelected());
CLIENT_PREFERENCES.setStampFormat(stampFormat.getText());
CLIENT_PREFERENCES.setReportKeywords(reportKeywordsTextPane.getText());
CLIENT_PREFERENCES.setShowIPAddressesInChat(showIPAddressesInChat.isSelected());
CLIENT_PREFERENCES.setStartSearchlightsOn(startSearchlightsOn.isSelected());

CLIENT_PREFERENCES.setDefaultAutoejectDisabled(defaultAutoejectDisabled.isSelected());
CLIENT_PREFERENCES.setUseAverageSkills(useAverageSkills.isSelected());
CLIENT_PREFERENCES.setUseGpInUnitSelection(useGPinUnitSelection.isSelected());
CLIENT_PREFERENCES.setGenerateNames(generateNames.isSelected());
CLIENT_PREFERENCES.setShowUnitId(showUnitId.isSelected());
if ((clientgui != null) && (clientgui.getBoardView() != null)) {
clientgui.getBoardView().updateEntityLabels();
}

CP.setLocale(CommonSettingsDialog.LOCALE_CHOICES[displayLocale.getSelectedIndex()]);
CLIENT_PREFERENCES.setLocale(CommonSettingsDialog.LOCALE_CHOICES[displayLocale.getSelectedIndex()]);

GUIP.setShowMapsheets(showMapsheets.isSelected());
GUIP.setAOHexShadows(aOHexShadows.isSelected());
Expand Down Expand Up @@ -2493,11 +2493,11 @@ protected void okAction() {

if (tileSetChoice.getSelectedIndex() >= 0) {
String tileSetFileName = tileSets.get(tileSetChoice.getSelectedIndex());
if (!CP.getMapTileset().equals(tileSetFileName) &&
if (!CLIENT_PREFERENCES.getMapTileset().equals(tileSetFileName) &&
(clientgui != null) && (clientgui.getBoardView() != null)) {
clientgui.getBoardView().clearShadowMap();
}
CP.setMapTileset(tileSetFileName);
CLIENT_PREFERENCES.setMapTileset(tileSetFileName);
}

ToolTipManager.sharedInstance().setInitialDelay(GUIP.getTooltipDelay());
Expand Down
4 changes: 2 additions & 2 deletions megamek/src/megamek/client/ui/swing/EquipChoicePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
import megamek.common.equipment.AmmoMounted;
import megamek.common.equipment.MiscMounted;
import megamek.common.equipment.WeaponMounted;
import megamek.common.options.AbstractOptions;
import megamek.common.options.IGameOptions;
import megamek.common.options.OptionsConstants;
import megamek.common.util.fileUtils.MegaMekFile;
import megamek.common.verifier.EntityVerifier;
Expand Down Expand Up @@ -586,7 +586,7 @@ private void setupMunitions() {
GridBagLayout gbl = new GridBagLayout();
panMunitions.setLayout(gbl);
Game game = clientgui.getClient().getGame();
AbstractOptions gameOpts = game.getOptions();
IGameOptions gameOpts = game.getOptions();
int gameYear = gameOpts.intOption(OptionsConstants.ALLOWED_YEAR);

if (entity.usesWeaponBays() || entity instanceof Dropship) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,7 @@ void exportMUL(ForceDescriptor fd) {
Player p = new Player(1, "Observer");
game.addPlayer(1, p);
game.setOptions(clientGui.getClient().getGame().getOptions());
list.stream().forEach(en -> {
list.forEach(en -> {
en.setOwner(p);
// If we don't set the id, the first unit will be left at -1, which in most
// cases is interpreted
Expand Down
2 changes: 1 addition & 1 deletion megamek/src/megamek/client/ui/swing/GameOptionsDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public class GameOptionsDialog extends AbstractButtonDialog implements ActionLis
public GameOptionsDialog(ClientGUI cg) {
super(cg.frame, "GameOptionsDialog", "GameOptionsDialog.title");
clientGui = cg;
init(cg.getFrame(), cg.getClient().getGame().getOptions());
init(cg.getFrame(), (GameOptions) cg.getClient().getGame().getOptions());
}

/**
Expand Down
97 changes: 43 additions & 54 deletions megamek/src/megamek/client/ui/swing/MegaMekGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,6 @@
*/
package megamek.client.ui.swing;

import static megamek.common.Compute.d6;

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BaseMultiResolutionImage;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.Vector;
import java.util.zip.GZIPInputStream;

import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.ToolTipManager;
import javax.swing.UIManager;
import javax.swing.filechooser.FileFilter;
import javax.xml.parsers.DocumentBuilder;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import megamek.MMConstants;
import megamek.MegaMek;
import megamek.SuiteConstants;
Expand Down Expand Up @@ -85,22 +48,45 @@
import megamek.common.*;
import megamek.common.annotations.Nullable;
import megamek.common.jacksonadapters.BotParser;
import megamek.common.options.GameOptions;
import megamek.common.options.IBasicOption;
import megamek.common.options.IOption;
import megamek.common.preference.IPreferenceChangeListener;
import megamek.common.preference.PreferenceChangeEvent;
import megamek.common.preference.PreferenceManager;
import megamek.common.scenario.Scenario;
import megamek.common.scenario.ScenarioLoader;
import megamek.server.sbf.SBFGameManager;
import megamek.common.util.EmailService;
import megamek.common.util.ImageUtil;
import megamek.common.util.fileUtils.MegaMekFile;
import megamek.logging.MMLogger;
import megamek.server.IGameManager;
import megamek.server.Server;
import megamek.server.sbf.SBFGameManager;
import megamek.server.totalwarfare.TWGameManager;
import megamek.utilities.xml.MMXMLUtility;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import javax.xml.parsers.DocumentBuilder;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BaseMultiResolutionImage;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.*;
import java.util.zip.GZIPInputStream;

import static megamek.common.Compute.d6;

public class MegaMekGUI implements IPreferenceChangeListener {
private static final MMLogger logger = MMLogger.create(MegaMekGUI.class);
Expand Down Expand Up @@ -438,7 +424,7 @@ public boolean startServer(@Nullable String serverPassword, int port, boolean is
serverPassword = Server.validatePassword(serverPassword);
port = Server.validatePort(port);
} catch (Exception ex) {
logger.error(ex, "Failed to start Server");
logger.error("Failed to start Server", ex);
frame.setVisible(true);
return false;
}
Expand Down Expand Up @@ -657,6 +643,19 @@ private boolean validateSaveVersion(final Node n) {
return false;
}

final Version version = getVersion(n);
if (SuiteConstants.VERSION.is(version)) {
return true;
} else {
final String message = String.format(
Messages.getString("MegaMek.LoadGameIncorrectVersion.message"),
version, SuiteConstants.VERSION);
logger.error(message, Messages.getString("MegaMek.LoadGameAlert.title"));
return false;
}
}

private static Version getVersion(Node n) {
final NodeList nl = n.getChildNodes();
String release = null;
String major = null;
Expand Down Expand Up @@ -686,16 +685,7 @@ private boolean validateSaveVersion(final Node n) {
}
}

final Version version = new Version(release, major, minor, snapshot);
if (SuiteConstants.VERSION.is(version)) {
return true;
} else {
final String message = String.format(
Messages.getString("MegaMek.LoadGameIncorrectVersion.message"),
version, SuiteConstants.VERSION);
logger.error(message, Messages.getString("MegaMek.LoadGameAlert.title"));
return false;
}
return new Version(release, major, minor, snapshot);
}

private void parsePlayerNames(final Node nodePlayers, final Vector<String> playerNames) {
Expand Down Expand Up @@ -775,9 +765,9 @@ void scenario() {
}

// popup options dialog
if (!scenario.hasFixedGameOptions() && game instanceof Game) {
GameOptionsDialog god = new GameOptionsDialog(frame, ((Game) game).getOptions(), false);
god.update(((Game) game).getOptions());
if (!scenario.hasFixedGameOptions() && game instanceof Game twGame) {
GameOptionsDialog god = new GameOptionsDialog(frame, (GameOptions) twGame.getOptions(), false);
god.update((GameOptions) twGame.getOptions());
god.setEditable(true);
god.setVisible(true);
for (IBasicOption opt : god.getOptions()) {
Expand Down Expand Up @@ -1182,8 +1172,7 @@ private BaseMultiResolutionImage getMultiResolutionSplashScreen(final List<Strin
for (String filename : filenames) {
File file = new MegaMekFile(Configuration.widgetsDir(), filename).getFile();
if (!file.exists()) {
logger
.error("MainMenu Error: background icon doesn't exist: " + file.getAbsolutePath());
logger.error("MainMenu Error: background icon doesn't exist: {}", file.getAbsolutePath());
} else {
BufferedImage img = (BufferedImage) ImageUtil.loadImageFromFile(file.toString());
images.add(img);
Expand Down
Loading

0 comments on commit ca68581

Please sign in to comment.