Skip to content

Commit

Permalink
Start working on user friendly game settings panel
Browse files Browse the repository at this point in the history
  • Loading branch information
fathzer committed Feb 14, 2024
1 parent 84df60a commit 8053f39
Show file tree
Hide file tree
Showing 4 changed files with 301 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ public Color getColor() {
}
}

public enum PlayerType {
HUMAN, ENGINE;
}

@NoArgsConstructor
@AllArgsConstructor
@Getter
Expand Down
137 changes: 137 additions & 0 deletions src/main/java/com/fathzer/jchess/swing/settings/GameSettingsPanel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package com.fathzer.jchess.swing.settings;

import static com.fathzer.jchess.swing.settings.GameSettings.Variant;

import javax.swing.JPanel;

import com.fathzer.jchess.swing.settings.clock.ClockSettingsPanel;

import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import javax.swing.JComboBox;
import java.awt.Insets;
import javax.swing.JCheckBox;
import javax.swing.border.TitledBorder;

public class GameSettingsPanel extends JPanel {
private static final long serialVersionUID = 1L;

private JComboBox<Variant> variantCombo;
private JCheckBox tabletModeCheckBox;
private JCheckBox showMovesCheckBox;
private JCheckBox touchMoveCheckBox;
private JPanel timePanel;
private JCheckBox timeControlCheckBox;
private JPanel timeDetailsPanel;
private JPanel player1Panel;
private JPanel player2Panel;

/**
* Create the panel.
*/
public GameSettingsPanel() {
GridBagLayout gridBagLayout = new GridBagLayout();
setLayout(gridBagLayout);

JLabel variantLabel = new JLabel("Variant: ");
GridBagConstraints variantLabelGbc = new GridBagConstraints();
variantLabelGbc.insets = new Insets(0, 0, 5, 5);
variantLabelGbc.anchor = GridBagConstraints.WEST;
variantLabelGbc.gridx = 0;
variantLabelGbc.gridy = 0;
add(variantLabel, variantLabelGbc);

variantCombo = new JComboBox<>();
for (Variant v : Variant.values()) {
variantCombo.addItem(v);
}
variantCombo.setSelectedIndex(0);
variantCombo.setEditable(true);
GridBagConstraints variantComboGbc = new GridBagConstraints();
variantComboGbc.insets = new Insets(0, 0, 5, 0);
variantComboGbc.fill = GridBagConstraints.HORIZONTAL;
variantComboGbc.gridx = 1;
variantComboGbc.gridy = 0;
add(variantCombo, variantComboGbc);

tabletModeCheckBox = new JCheckBox("Tablet mode");
GridBagConstraints tabletModeCheckBoxGbc = new GridBagConstraints();
tabletModeCheckBoxGbc.insets = new Insets(0, 0, 5, 0);
tabletModeCheckBoxGbc.anchor = GridBagConstraints.WEST;
tabletModeCheckBoxGbc.gridwidth = 2;
tabletModeCheckBoxGbc.gridx = 0;
tabletModeCheckBoxGbc.gridy = 1;
add(tabletModeCheckBox, tabletModeCheckBoxGbc);

showMovesCheckBox = new JCheckBox("Show possible moves");
GridBagConstraints showMovesCheckBoxGbc = new GridBagConstraints();
showMovesCheckBoxGbc.insets = new Insets(0, 0, 5, 0);
showMovesCheckBoxGbc.anchor = GridBagConstraints.WEST;
showMovesCheckBoxGbc.gridwidth = 2;
showMovesCheckBoxGbc.gridx = 0;
showMovesCheckBoxGbc.gridy = 2;
add(showMovesCheckBox, showMovesCheckBoxGbc);

touchMoveCheckBox = new JCheckBox("Always move touched piece");
GridBagConstraints touchMoveCheckBoxGbc = new GridBagConstraints();
touchMoveCheckBoxGbc.insets = new Insets(0, 0, 5, 0);
touchMoveCheckBoxGbc.anchor = GridBagConstraints.WEST;
touchMoveCheckBoxGbc.gridwidth = 2;
touchMoveCheckBoxGbc.gridx = 0;
touchMoveCheckBoxGbc.gridy = 3;
add(touchMoveCheckBox, touchMoveCheckBoxGbc);

player1Panel = new PlayerSelectionPanel();
player1Panel.setBorder(new TitledBorder(null, "Player 1", TitledBorder.LEADING, TitledBorder.TOP, null, null));
GridBagConstraints player1PanelGbc = new GridBagConstraints();
player1PanelGbc.gridwidth = 2;
player1PanelGbc.insets = new Insets(0, 0, 5, 5);
player1PanelGbc.fill = GridBagConstraints.BOTH;
player1PanelGbc.gridx = 0;
player1PanelGbc.gridy = 4;
add(player1Panel, player1PanelGbc);

player2Panel = new PlayerSelectionPanel();
player2Panel.setBorder(new TitledBorder(null, "Player 2", TitledBorder.LEADING, TitledBorder.TOP, null, null));
GridBagConstraints player2PanelGbc = new GridBagConstraints();
player2PanelGbc.gridwidth = 2;
player2PanelGbc.insets = new Insets(0, 0, 5, 5);
player2PanelGbc.fill = GridBagConstraints.BOTH;
player2PanelGbc.gridx = 0;
player2PanelGbc.gridy = 5;
add(player2Panel, player2PanelGbc);

timePanel = new JPanel();
timePanel.setBorder(new TitledBorder(null, "Time control", TitledBorder.LEADING, TitledBorder.TOP, null, null));
GridBagConstraints timePanelGridBagConstraints = new GridBagConstraints();
timePanelGridBagConstraints.gridwidth = 2;
timePanelGridBagConstraints.fill = GridBagConstraints.BOTH;
timePanelGridBagConstraints.gridx = 0;
timePanelGridBagConstraints.gridy = 6;
add(timePanel, timePanelGridBagConstraints);
GridBagLayout gbl_timePanel = new GridBagLayout();
gbl_timePanel.columnWidths = new int[]{0, 0};
gbl_timePanel.rowHeights = new int[]{0, 0, 0};
gbl_timePanel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gbl_timePanel.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
timePanel.setLayout(gbl_timePanel);

timeControlCheckBox = new JCheckBox("Limit time");
GridBagConstraints timeControlCheckBoxGbc = new GridBagConstraints();
timeControlCheckBoxGbc.insets = new Insets(0, 0, 5, 0);
timeControlCheckBoxGbc.anchor = GridBagConstraints.WEST;
timeControlCheckBoxGbc.gridx = 0;
timeControlCheckBoxGbc.gridy = 0;
timePanel.add(timeControlCheckBox, timeControlCheckBoxGbc);

timeDetailsPanel = new ClockSettingsPanel();
GridBagConstraints timeDetailsPanelGbc = new GridBagConstraints();
timeDetailsPanelGbc.fill = GridBagConstraints.BOTH;
timeDetailsPanelGbc.gridx = 0;
timeDetailsPanelGbc.gridy = 1;
timePanel.add(timeDetailsPanel, timeDetailsPanelGbc);

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.fathzer.jchess.swing.settings;

import static com.fathzer.jchess.swing.settings.GameSettings.PlayerType;

import javax.swing.JPanel;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;

import com.fathzer.soft.ajlib.swing.widget.TextWidget;

import java.awt.Insets;
import javax.swing.JComboBox;

public class PlayerSelectionPanel extends JPanel {
private static final long serialVersionUID = 1L;

private JLabel nameLabel;
private TextWidget txtName;
private JLabel typeLabel;
private JComboBox<PlayerType> comboBox;

/**
* Create the panel.
*/
public PlayerSelectionPanel() {
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{0, 0, 0};
gridBagLayout.rowHeights = new int[]{0, 0, 0};
gridBagLayout.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
gridBagLayout.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
setLayout(gridBagLayout);

typeLabel = new JLabel("Type: ");
GridBagConstraints typeLabelGbc = new GridBagConstraints();
typeLabelGbc.anchor = GridBagConstraints.EAST;
typeLabelGbc.insets = new Insets(0, 0, 5, 5);
typeLabelGbc.gridx = 0;
typeLabelGbc.gridy = 0;
add(typeLabel, typeLabelGbc);

comboBox = new JComboBox<>();
for (PlayerType type : PlayerType.values()) {
comboBox.addItem(type);
}
comboBox.setSelectedIndex(0);
comboBox.setToolTipText("Select player's type. Engines can be added and configured in Engines panel");
GridBagConstraints comboBoxGridBagConstraints = new GridBagConstraints();
comboBoxGridBagConstraints.insets = new Insets(0, 0, 5, 0);
comboBoxGridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
comboBoxGridBagConstraints.gridx = 1;
comboBoxGridBagConstraints.gridy = 0;
add(comboBox, comboBoxGridBagConstraints);

nameLabel = new JLabel("Name: ");
GridBagConstraints nameLabelGbc = new GridBagConstraints();
nameLabelGbc.insets = new Insets(0, 0, 0, 5);
nameLabelGbc.anchor = GridBagConstraints.EAST;
nameLabelGbc.gridx = 0;
nameLabelGbc.gridy = 1;
add(nameLabel, nameLabelGbc);

txtName = new TextWidget();
GridBagConstraints txtNameGridBagConstraints = new GridBagConstraints();
txtNameGridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
txtNameGridBagConstraints.gridx = 1;
txtNameGridBagConstraints.gridy = 1;
add(txtName, txtNameGridBagConstraints);
txtName.setColumns(10);

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.fathzer.jchess.swing.settings.clock;

import javax.swing.JPanel;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import javax.swing.JTextField;
import java.awt.Insets;
import java.math.BigInteger;

import javax.swing.JCheckBox;
import javax.swing.SwingConstants;

import com.fathzer.soft.ajlib.swing.widget.IntegerWidget;

import javax.swing.JButton;

public class ClockSettingsPanel extends JPanel {

private static final long serialVersionUID = 1L;
private IntegerWidget timeField;
private IntegerWidget incrementField;
private JLabel movesBeforeIncrementLabel;
private JTextField movesBeforeIncrementField;

/**
* Create the panel.
*/
public ClockSettingsPanel() {
GridBagLayout gridBagLayout = new GridBagLayout();
setLayout(gridBagLayout);

final JLabel timeLabel = new JLabel("Time: ");
timeLabel.setToolTipText("Maximum time in seconds");
GridBagConstraints timeLabelGbc = new GridBagConstraints();
timeLabelGbc.insets = new Insets(0, 0, 5, 5);
timeLabelGbc.anchor = GridBagConstraints.WEST;
timeLabelGbc.gridx = 0;
timeLabelGbc.gridy = 0;
add(timeLabel, timeLabelGbc);

timeField = new IntegerWidget(BigInteger.ZERO, BigInteger.valueOf(24L*3600));
GridBagConstraints timeFieldGbc = new GridBagConstraints();
timeFieldGbc.insets = new Insets(0, 0, 5, 5);
timeFieldGbc.fill = GridBagConstraints.HORIZONTAL;
timeFieldGbc.gridx = 1;
timeFieldGbc.gridy = 0;
add(timeField, timeFieldGbc);
timeField.setColumns(10);

final JLabel incrementLabel = new JLabel("Increment (s): ");
GridBagConstraints incrementLabelGbc = new GridBagConstraints();
incrementLabelGbc.anchor = GridBagConstraints.WEST;
incrementLabelGbc.insets = new Insets(0, 0, 5, 5);
incrementLabelGbc.gridx = 0;
incrementLabelGbc.gridy = 1;
add(incrementLabel, incrementLabelGbc);

incrementField = new IntegerWidget(BigInteger.ZERO, BigInteger.valueOf(3600));
incrementField.setToolTipText("Increment of time in seconds");
GridBagConstraints incrementFieldGbc = new GridBagConstraints();
incrementFieldGbc.insets = new Insets(0, 0, 5, 5);
incrementFieldGbc.fill = GridBagConstraints.HORIZONTAL;
incrementFieldGbc.gridx = 1;
incrementFieldGbc.gridy = 1;
add(incrementField, incrementFieldGbc);
incrementField.setColumns(10);

movesBeforeIncrementLabel = new JLabel("Moves before increment: ");
GridBagConstraints movesBeforeIncrementLabelGbc = new GridBagConstraints();
movesBeforeIncrementLabelGbc.anchor = GridBagConstraints.WEST;
movesBeforeIncrementLabelGbc.insets = new Insets(0, 0, 5, 5);
movesBeforeIncrementLabelGbc.gridx = 0;
movesBeforeIncrementLabelGbc.gridy = 2;
add(movesBeforeIncrementLabel, movesBeforeIncrementLabelGbc);

movesBeforeIncrementField = new JTextField();
GridBagConstraints movesBeforeIncrementFieldGbc = new GridBagConstraints();
movesBeforeIncrementFieldGbc.insets = new Insets(0, 0, 5, 5);
movesBeforeIncrementFieldGbc.fill = GridBagConstraints.HORIZONTAL;
movesBeforeIncrementFieldGbc.gridx = 1;
movesBeforeIncrementFieldGbc.gridy = 2;
add(movesBeforeIncrementField, movesBeforeIncrementFieldGbc);
movesBeforeIncrementField.setColumns(10);
}

}

0 comments on commit 8053f39

Please sign in to comment.