Skip to content

Commit

Permalink
#16: Fixed code layout according to conventions (now with 4 spaces wi…
Browse files Browse the repository at this point in the history
…de tabulator and up to 100 characters per line).
  • Loading branch information
christoff-buerger committed Oct 30, 2024
1 parent f9b8898 commit 88da635
Show file tree
Hide file tree
Showing 11 changed files with 1,527 additions and 1,170 deletions.
264 changes: 153 additions & 111 deletions sources/pmchess/gui/AboutFrame.java

Large diffs are not rendered by default.

145 changes: 81 additions & 64 deletions sources/pmchess/gui/GUI.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
This program and the accompanying materials are made available under the
terms of the MIT license (X11 license) which accompanies this distribution.
This program and the accompanying materials are made available under the terms of the MIT
license (X11 license) which accompanies this distribution.
Author: Christoff Bürger
*/
Expand All @@ -23,11 +23,14 @@ public final class GUI extends JFrame
static
{
// Initialize fonts:
final var keys = UIManager.getDefaults().keys();
final var keys =
UIManager.getDefaults().keys();
while (keys.hasMoreElements())
{
final var key = keys.nextElement();
final var value = UIManager.get(key);
final var key =
keys.nextElement();
final var value =
UIManager.get(key);
if (value != null && value instanceof javax.swing.plaf.FontUIResource)
{
UIManager.put(key, Resources.font_regular);
Expand Down Expand Up @@ -63,7 +66,8 @@ public GUI()
super("pmChess", Resources.graphics_configuration);

// Setup "About"-window:
final var about_action = new AboutAction();
final var about_action =
new AboutAction();
try
{
Desktop.getDesktop().setAboutHandler(about_action);
Expand All @@ -83,16 +87,20 @@ public GUI()
}

// Setup menu bar:
final var menu_bar = new JMenuBar();
final var game_menu = new JMenu("Game"); // Game menu:
final var menu_bar =
new JMenuBar();
final var game_menu =
new JMenu("Game"); // Game menu:
game_menu.setMnemonic(KeyEvent.VK_G);
game_menu.add(new NewGameAction());
final var computer_player = new ButtonGroup();
final var computer_player =
new ButtonGroup();
white_computer = new NonclosingRadioButtonMenuItem("White computer");
white_computer.setSelected(false);
black_computer = new NonclosingRadioButtonMenuItem("Black computer");
black_computer.setSelected(false);
final var no_computer = new NonclosingRadioButtonMenuItem("No computer");
final var no_computer =
new NonclosingRadioButtonMenuItem("No computer");
no_computer.setSelected(true);
computer_player.add(white_computer);
computer_player.add(black_computer);
Expand All @@ -103,7 +111,8 @@ public GUI()
game_menu.addSeparator();
game_menu.add(new ExitAction());
menu_bar.add(game_menu);
final var help_menu = new JMenu("Help"); // Help menu:
final var help_menu =
new JMenu("Help"); // Help menu:
help_menu.setMnemonic(KeyEvent.VK_H);
help_menu.add(about_action);
help_menu.add(new ContactAction());
Expand All @@ -122,24 +131,28 @@ public GUI()
setLocationRelativeTo(null); // center window

// Check window size fits:
final var os_scaling = switch (System.getProperty("os.name"))
final var os_scaling =
switch (System.getProperty("os.name"))
{
case String s when s.startsWith("Windows") ->
(int) Math.round(
(100.0f * ((float) Toolkit.getDefaultToolkit().getScreenResolution()))
/ 96.0f);
default -> 100;
};
final var display_mode = Resources.graphics_configuration
final var display_mode =
// Use real screen size, not DPI scaled size of .getBounds():
.getDevice().getDisplayMode();
final var screen_insets = Toolkit.getDefaultToolkit().getScreenInsets(
Resources.graphics_configuration);
final var width_max = (int) Math.floor(0.97f * (float)(
display_mode.getWidth() - screen_insets.left - screen_insets.right));
final var height_max = (int) Math.floor(0.97f * (float)(
display_mode.getHeight() - screen_insets.top - screen_insets.bottom));
final var frame_insets = Resources.compute_insets();
Resources.graphics_configuration.getDevice().getDisplayMode();
final var screen_insets =
Toolkit.getDefaultToolkit().getScreenInsets(Resources.graphics_configuration);
final var width_max =
(int) Math.floor(0.97f * (float)(
display_mode.getWidth() - screen_insets.left - screen_insets.right));
final var height_max =
(int) Math.floor(0.97f * (float)(
display_mode.getHeight() - screen_insets.top - screen_insets.bottom));
final var frame_insets =
Resources.compute_insets();
final var width_current =
os_scaling
* Math.max(
Expand All @@ -155,13 +168,14 @@ public GUI()
+ frame_insets.top
+ frame_insets.bottom)
/ 100;
final var width_original = (100 * width_current) / Resources.base_scale_in_percent();
final var height_original = (100 * height_current) / Resources.base_scale_in_percent();
final var width_original =
(100 * width_current) / Resources.base_scale_in_percent();
final var height_original =
(100 * height_current) / Resources.base_scale_in_percent();
if (width_current > width_max || height_current > height_max)
{
final var autoscale = (int) Math.floor(
96.0f
* Math.min(
final var autoscale =
(int) Math.floor(96.0f * Math.min(
((float) width_max) / ((float) width_original)
, ((float) height_max) / ((float) height_original)));
UIManager.put(
Expand All @@ -170,35 +184,36 @@ public GUI()
UIManager.put(
"OptionPane.buttonFont"
, Resources.font_regular.deriveFont(Resources.base_scale_default));
final var selection = JOptionPane.showOptionDialog(
this
, String.format(
"""
<html>
The configured scale of pmChess exceeds the resolution of<br>
the primary screen. The pmChess window likely will not fit.
<br>
<br>
The current scale configuration is %d%%.<br>
The recommended maximum is %d%%.
<br>
<br>
If you continue, pmChess is started with the configured scale.<br>
You can also reset to the default scale of 100%%.<br>
Or autoscale to the recommeded maximum.
<br>
<br>
Reset and autoscale will quit pmChess after updating the scale.<br>
The new scale will be applied next time you start pmChess.
</html>"""
, Resources.base_scale_in_percent()
, autoscale)
, "Warning: Scale exceeds primary screen resolution"
, JOptionPane.DEFAULT_OPTION
, JOptionPane.WARNING_MESSAGE
, null
, new String[]{ "Reset scale", "Autoscale", "Continue" }
, "Reset scale");
final var selection =
JOptionPane.showOptionDialog(
this
, String.format(
"""
<html>
The configured scale of pmChess exceeds the resolution of<br>
the primary screen. The pmChess window likely will not fit.
<br>
<br>
The current scale configuration is %d%%.<br>
The recommended maximum is %d%%.
<br>
<br>
If you continue, pmChess is started with the configured scale.<br>
You can also reset to the default scale of 100%%.<br>
Or autoscale to the recommeded maximum.
<br>
<br>
Reset and autoscale will quit pmChess after updating the scale.<br>
The new scale will be applied next time you start pmChess.
</html>"""
, Resources.base_scale_in_percent()
, autoscale)
, "Warning: Scale exceeds primary screen resolution"
, JOptionPane.DEFAULT_OPTION
, JOptionPane.WARNING_MESSAGE
, null
, new String[]{ "Reset scale", "Autoscale", "Continue" }
, "Reset scale");
switch (selection)
{
case 0: // Reset scale:
Expand Down Expand Up @@ -231,8 +246,7 @@ protected void exit()

public boolean save_game(final String game_file)
{
try (final var os = new ObjectOutputStream(
new FileOutputStream(game_file, false)))
try (final var os = new ObjectOutputStream(new FileOutputStream(game_file, false)))
{
main_panel.serialize_game(os);
return true;
Expand All @@ -252,10 +266,10 @@ public boolean save_game(final String game_file)

public void load_game(final String game_file)
{
try {
try
{
if (Files.exists(Paths.get(game_file)))
try (final var is = new ObjectInputStream(
new FileInputStream(game_file)))
try (final var is = new ObjectInputStream(new FileInputStream(game_file)))
{
main_panel.deserialize_game(is);
}
Expand All @@ -272,7 +286,9 @@ public void load_game(final String game_file)
Resources.configure_rendering(graphics);
}

/* *********************************** menu actions *********************************** */
/*
Menu actions:
*/

private final class NewGameAction extends AbstractAction
{
Expand Down Expand Up @@ -305,8 +321,8 @@ private ExitAction()

private static final class AboutAction extends AbstractAction implements AboutHandler
{
private static final AboutFrame about_frame = new AboutFrame(
Resources.graphics_configuration);
private static final AboutFrame about_frame =
new AboutFrame(Resources.graphics_configuration);

private AboutAction()
{
Expand Down Expand Up @@ -342,7 +358,8 @@ private static final class NonclosingRadioButtonMenuItem extends JRadioButtonMen
private static MenuElement[] path;

{ // Instance initialization:
getModel().addChangeListener(new ChangeListener()
getModel().addChangeListener(
new ChangeListener()
{
@Override public void stateChanged(final ChangeEvent e)
{
Expand Down
Loading

0 comments on commit 88da635

Please sign in to comment.