mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-18 11:48:02 +00:00
Avatar rebuild, also fixing indexing bug #2.
This commit is contained in:
@@ -3,7 +3,6 @@ package forge.view.home;
|
|||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
import java.awt.Dimension;
|
import java.awt.Dimension;
|
||||||
import java.awt.Font;
|
import java.awt.Font;
|
||||||
import java.awt.Graphics;
|
|
||||||
import java.awt.Image;
|
import java.awt.Image;
|
||||||
import java.awt.event.FocusAdapter;
|
import java.awt.event.FocusAdapter;
|
||||||
import java.awt.event.FocusEvent;
|
import java.awt.event.FocusEvent;
|
||||||
@@ -16,6 +15,7 @@ import java.util.Arrays;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import javax.swing.ImageIcon;
|
||||||
import javax.swing.JCheckBox;
|
import javax.swing.JCheckBox;
|
||||||
import javax.swing.JLabel;
|
import javax.swing.JLabel;
|
||||||
import javax.swing.JList;
|
import javax.swing.JList;
|
||||||
@@ -30,7 +30,7 @@ import net.miginfocom.swing.MigLayout;
|
|||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.apache.commons.lang3.text.WordUtils;
|
import org.apache.commons.lang3.text.WordUtils;
|
||||||
|
|
||||||
import forge.PlayerType;
|
import forge.Command;
|
||||||
import forge.Singletons;
|
import forge.Singletons;
|
||||||
import forge.control.KeyboardShortcuts;
|
import forge.control.KeyboardShortcuts;
|
||||||
import forge.control.KeyboardShortcuts.Shortcut;
|
import forge.control.KeyboardShortcuts.Shortcut;
|
||||||
@@ -64,8 +64,7 @@ public class ViewSettings extends JPanel {
|
|||||||
|
|
||||||
private String sectionConstraints, regularConstraints, tabberConstraints;
|
private String sectionConstraints, regularConstraints, tabberConstraints;
|
||||||
|
|
||||||
private AvatarLabel avatarHuman, avatarAI;
|
private final FLabel lblAvatarHuman, lblAvatarAI;
|
||||||
private List<AvatarLabel> lstAvatars;
|
|
||||||
|
|
||||||
/** Assembles swing components for "Settings" mode menu. */
|
/** Assembles swing components for "Settings" mode menu. */
|
||||||
public ViewSettings() {
|
public ViewSettings() {
|
||||||
@@ -114,6 +113,11 @@ public class ViewSettings extends JPanel {
|
|||||||
lblTitleSkin = new FLabel.Builder().text("Choose Skin").fontScaleAuto(false).build();
|
lblTitleSkin = new FLabel.Builder().text("Choose Skin").fontScaleAuto(false).build();
|
||||||
lblTitleSkin.setFont(FSkin.getBoldFont(14));
|
lblTitleSkin.setFont(FSkin.getBoldFont(14));
|
||||||
|
|
||||||
|
lblAvatarHuman = new FLabel.Builder().hoverable(true).selectable(true)
|
||||||
|
.iconScaleFactor(0.99f).iconInBackground(true).build();
|
||||||
|
lblAvatarAI = new FLabel.Builder().hoverable(true).selectable(true)
|
||||||
|
.iconScaleFactor(0.99f).iconInBackground(true).build();
|
||||||
|
|
||||||
populateTabber();
|
populateTabber();
|
||||||
populatePrefs();
|
populatePrefs();
|
||||||
populateAvatars();
|
populateAvatars();
|
||||||
@@ -129,8 +133,8 @@ public class ViewSettings extends JPanel {
|
|||||||
|
|
||||||
/** */
|
/** */
|
||||||
public void updateSkinNames() {
|
public void updateSkinNames() {
|
||||||
String[] uglyNames = FSkin.getSkins().toArray(new String[0]);
|
final String[] uglyNames = FSkin.getSkins().toArray(new String[0]);
|
||||||
String[] prettyNames = new String[uglyNames.length];
|
final String[] prettyNames = new String[uglyNames.length];
|
||||||
|
|
||||||
for (int i = 0; i < uglyNames.length; i++) {
|
for (int i = 0; i < uglyNames.length; i++) {
|
||||||
prettyNames[i] = WordUtils.capitalize(uglyNames[i].replace('_', ' '));
|
prettyNames[i] = WordUtils.capitalize(uglyNames[i].replace('_', ' '));
|
||||||
@@ -228,101 +232,90 @@ public class ViewSettings extends JPanel {
|
|||||||
|
|
||||||
private void populateAvatars() {
|
private void populateAvatars() {
|
||||||
final Map<Integer, Image> avatarMap = FSkin.getAvatars();
|
final Map<Integer, Image> avatarMap = FSkin.getAvatars();
|
||||||
|
final JPanel pnlAvatarPics = new JPanel(new WrapLayout());
|
||||||
|
final JPanel pnlAvatarUsers = new JPanel(new MigLayout("insets 0, gap 0, align center"));
|
||||||
|
|
||||||
pnlAvatars.setLayout(new WrapLayout());
|
pnlAvatarUsers.setOpaque(false);
|
||||||
|
pnlAvatarPics.setOpaque(false);
|
||||||
|
|
||||||
lstAvatars = new ArrayList<AvatarLabel>();
|
pnlAvatarUsers.add(new FLabel.Builder().fontSize(12).text("Human").build(),
|
||||||
|
"w 100px!, h 20px!, gap 0 20px 0 0");
|
||||||
|
pnlAvatarUsers.add(new FLabel.Builder().fontSize(12).text("AI").build(),
|
||||||
|
"w 100px!, h 20px!, wrap");
|
||||||
|
|
||||||
|
pnlAvatarUsers.add(lblAvatarHuman, "w 100px!, h 100px!, gap 0 20px 0 0");
|
||||||
|
pnlAvatarUsers.add(lblAvatarAI, "w 100px!, h 100px!");
|
||||||
|
|
||||||
for (final Integer i : avatarMap.keySet()) {
|
for (final Integer i : avatarMap.keySet()) {
|
||||||
lstAvatars.add(new AvatarLabel(avatarMap.get(i), i));
|
pnlAvatarPics.add(makeAvatarLabel(avatarMap.get(i), i));
|
||||||
pnlAvatars.add(lstAvatars.get(lstAvatars.size() - 1));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pnlAvatars.removeAll();
|
||||||
|
pnlAvatars.setLayout(new MigLayout("insets 0, gap 0"));
|
||||||
|
pnlAvatars.add(pnlAvatarUsers, "w 90%!, h 150px!, wrap");
|
||||||
|
pnlAvatars.add(new FScrollPane(pnlAvatarPics,
|
||||||
|
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER),
|
||||||
|
"w 90%!, pushy, growy, gap 5% 0 0 0");
|
||||||
|
|
||||||
|
final Command cmdHuman = new Command() { @Override
|
||||||
|
public void execute() { lblAvatarAI.setSelected(false); } };
|
||||||
|
|
||||||
|
final Command cmdAI = new Command() { @Override
|
||||||
|
public void execute() { lblAvatarHuman.setSelected(false); } };
|
||||||
|
|
||||||
|
lblAvatarHuman.setCommand(cmdHuman);
|
||||||
|
lblAvatarAI.setCommand(cmdAI);
|
||||||
|
|
||||||
|
lblAvatarHuman.setSelected(true);
|
||||||
|
|
||||||
final String[] indexes = Singletons.getModel().getPreferences().getPref(FPref.UI_AVATARS).split(",");
|
final String[] indexes = Singletons.getModel().getPreferences().getPref(FPref.UI_AVATARS).split(",");
|
||||||
int humanIndex = Integer.parseInt(indexes[0]);
|
int humanIndex = Integer.parseInt(indexes[0]);
|
||||||
int aiIndex = Integer.parseInt(indexes[1]);
|
int aiIndex = Integer.parseInt(indexes[1]);
|
||||||
|
|
||||||
// Set human avatar from preferences
|
if (humanIndex >= FSkin.getAvatars().size()) { humanIndex = 0; }
|
||||||
if (humanIndex >= lstAvatars.size()) {
|
if (aiIndex >= FSkin.getAvatars().size()) { aiIndex = 0; }
|
||||||
humanIndex = (int) (Math.random() * (lstAvatars.size() - 1));
|
|
||||||
|
lblAvatarHuman.setIcon(new ImageIcon(FSkin.getAvatars().get(humanIndex)));
|
||||||
|
lblAvatarAI.setIcon(new ImageIcon(FSkin.getAvatars().get(aiIndex)));
|
||||||
|
|
||||||
|
Singletons.getModel().getPreferences().setPref(FPref.UI_AVATARS, aiIndex + "," + humanIndex);
|
||||||
|
Singletons.getModel().getPreferences().save();
|
||||||
}
|
}
|
||||||
|
|
||||||
avatarHuman = lstAvatars.get(humanIndex);
|
private FLabel makeAvatarLabel(final Image img0, final int index0) {
|
||||||
avatarHuman.setOwner(PlayerType.HUMAN);
|
final FLabel lbl = new FLabel.Builder().icon(new ImageIcon(img0)).iconScaleFactor(1.0)
|
||||||
avatarHuman.repaintOnlyThisLabel();
|
.iconAlpha(0.7f).iconInBackground(true).hoverable(true).build();
|
||||||
|
|
||||||
if (humanIndex == aiIndex || aiIndex >= lstAvatars.size()) {
|
final Dimension size = new Dimension(100, 100);
|
||||||
aiIndex = humanIndex;
|
lbl.setPreferredSize(size);
|
||||||
while (aiIndex == humanIndex) {
|
lbl.setMaximumSize(size);
|
||||||
aiIndex = (int) (Math.random() * (lstAvatars.size() - 1));
|
lbl.setMinimumSize(size);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
avatarAI = lstAvatars.get(aiIndex);
|
final Command cmd = new Command() {
|
||||||
avatarAI.setOwner(PlayerType.COMPUTER);
|
@Override
|
||||||
avatarAI.repaintOnlyThisLabel();
|
public void execute() {
|
||||||
}
|
String[] indices = Singletons.getModel().getPreferences()
|
||||||
|
.getPref(FPref.UI_AVATARS).split(",");
|
||||||
|
|
||||||
/** Surprisingly complicated - be careful when modifying! */
|
if (lblAvatarAI.isSelected()) {
|
||||||
private void cycleOwner(final AvatarLabel lbl0) {
|
lblAvatarAI.setIcon(new ImageIcon(FSkin.getAvatars().get(index0)));
|
||||||
if (lbl0.getOwner() == null) {
|
lblAvatarAI.repaintOnlyThisLabel();
|
||||||
lbl0.setOwner(PlayerType.HUMAN);
|
indices[0] = String.valueOf(index0);
|
||||||
lbl0.repaintOnlyThisLabel();
|
|
||||||
|
|
||||||
if (avatarHuman != null) {
|
|
||||||
avatarHuman.setOwner(null);
|
|
||||||
avatarHuman.repaintOnlyThisLabel();
|
|
||||||
}
|
|
||||||
|
|
||||||
avatarHuman = lbl0;
|
|
||||||
}
|
|
||||||
else if (lbl0.getOwner() == PlayerType.HUMAN) {
|
|
||||||
// Re-assign avatar to human
|
|
||||||
avatarHuman.setOwner(null);
|
|
||||||
avatarHuman.repaintOnlyThisLabel();
|
|
||||||
|
|
||||||
for (int i = 0; i < lstAvatars.size(); i++) {
|
|
||||||
if (lstAvatars.get(i) != lbl0) {
|
|
||||||
avatarHuman = lstAvatars.get(i);
|
|
||||||
avatarHuman.setOwner(PlayerType.HUMAN);
|
|
||||||
avatarHuman.repaintOnlyThisLabel();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Assign computer
|
|
||||||
lbl0.setOwner(PlayerType.COMPUTER);
|
|
||||||
lbl0.repaintOnlyThisLabel();
|
|
||||||
|
|
||||||
if (avatarAI != null) {
|
|
||||||
avatarAI.setOwner(null);
|
|
||||||
avatarAI.repaintOnlyThisLabel();
|
|
||||||
}
|
|
||||||
|
|
||||||
avatarAI = lbl0;
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
lbl0.setOwner(null);
|
lblAvatarHuman.setIcon(new ImageIcon(FSkin.getAvatars().get(index0)));
|
||||||
lbl0.repaintOnlyThisLabel();
|
lblAvatarHuman.repaintOnlyThisLabel();
|
||||||
|
indices[1] = String.valueOf(index0);
|
||||||
// Re-assign avatar to computer
|
|
||||||
avatarAI.setOwner(null);
|
|
||||||
avatarAI.repaintOnlyThisLabel();
|
|
||||||
|
|
||||||
for (int i = 0; i < lstAvatars.size(); i++) {
|
|
||||||
if (lstAvatars.get(i) != avatarHuman) {
|
|
||||||
avatarAI = lstAvatars.get(i);
|
|
||||||
avatarAI.setOwner(PlayerType.COMPUTER);
|
|
||||||
avatarAI.repaintOnlyThisLabel();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Singletons.getModel().getPreferences().setPref(
|
Singletons.getModel().getPreferences().setPref(FPref.UI_AVATARS, indices[0] + "," + indices[1]);
|
||||||
FPref.UI_AVATARS, avatarAI.getIndex() + "," + avatarHuman.getIndex());
|
|
||||||
Singletons.getModel().getPreferences().save();
|
Singletons.getModel().getPreferences().save();
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
lbl.setCommand(cmd);
|
||||||
|
return lbl;
|
||||||
|
}
|
||||||
|
|
||||||
/** Consolidates checkbox styling in one place. */
|
/** Consolidates checkbox styling in one place. */
|
||||||
private class OptionsCheckBox extends JCheckBox {
|
private class OptionsCheckBox extends JCheckBox {
|
||||||
@@ -368,65 +361,6 @@ public class ViewSettings extends JPanel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class AvatarLabel extends JLabel {
|
|
||||||
private final Image img;
|
|
||||||
private final int index;
|
|
||||||
private PlayerType owner;
|
|
||||||
private boolean hovered = false;
|
|
||||||
|
|
||||||
public AvatarLabel(final Image img0, final int index0) {
|
|
||||||
super();
|
|
||||||
img = img0;
|
|
||||||
index = index0;
|
|
||||||
setMaximumSize(new Dimension(100, 120));
|
|
||||||
setMinimumSize(new Dimension(100, 120));
|
|
||||||
setPreferredSize(new Dimension(100, 120));
|
|
||||||
|
|
||||||
this.addMouseListener(new MouseAdapter() {
|
|
||||||
@Override
|
|
||||||
public void mouseEntered(final MouseEvent evt) { hovered = true; repaintOnlyThisLabel(); }
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void mouseExited(final MouseEvent evt) { hovered = false; repaintOnlyThisLabel(); }
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void mouseClicked(final MouseEvent evt) { cycleOwner(AvatarLabel.this); repaintOnlyThisLabel(); }
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setOwner(final PlayerType player0) {
|
|
||||||
this.owner = player0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public PlayerType getOwner() {
|
|
||||||
return this.owner;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getIndex() {
|
|
||||||
return this.index;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void repaintOnlyThisLabel() {
|
|
||||||
final Dimension d = AvatarLabel.this.getSize();
|
|
||||||
repaint(0, 0, d.width, d.height);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void paintComponent(final Graphics graphics0) {
|
|
||||||
if (hovered) {
|
|
||||||
graphics0.setColor(FSkin.getColor(FSkin.Colors.CLR_HOVER));
|
|
||||||
graphics0.fillRect(0, 0, 100, 120);
|
|
||||||
}
|
|
||||||
|
|
||||||
graphics0.drawImage(img, 0, 20, null);
|
|
||||||
if (owner == null) { return; }
|
|
||||||
|
|
||||||
graphics0.setColor(FSkin.getColor(FSkin.Colors.CLR_TEXT));
|
|
||||||
graphics0.drawRect(0, 0, 99, 119);
|
|
||||||
graphics0.setFont(FSkin.getBoldFont(14));
|
|
||||||
graphics0.drawString(owner.toString(), 5, 15);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A JTextField plus a "codeString" property, that stores keycodes for the
|
* A JTextField plus a "codeString" property, that stores keycodes for the
|
||||||
* shortcut. Also, an action listener that handles translation of keycodes
|
* shortcut. Also, an action listener that handles translation of keycodes
|
||||||
|
|||||||
@@ -316,6 +316,11 @@ public class FLabel extends JLabel {
|
|||||||
return this.resizeTimer;
|
return this.resizeTimer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @return boolean */
|
||||||
|
public boolean isSelected() {
|
||||||
|
return selected;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
// Must be public.
|
// Must be public.
|
||||||
public void setIcon(final Icon i0) {
|
public void setIcon(final Icon i0) {
|
||||||
|
|||||||
Reference in New Issue
Block a user