Avatar icons now update in the player field without restarting Forge.

This commit is contained in:
Doublestrike
2012-02-19 01:55:48 +00:00
parent 056885069a
commit b724f39386
3 changed files with 62 additions and 52 deletions

View File

@@ -17,9 +17,13 @@
*/ */
package forge.control; package forge.control;
import java.awt.Image;
import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.swing.ImageIcon;
import forge.AllZone; import forge.AllZone;
import forge.Card; import forge.Card;
import forge.CardContainer; import forge.CardContainer;
@@ -37,8 +41,14 @@ import forge.control.match.ControlHand;
import forge.control.match.ControlMessage; import forge.control.match.ControlMessage;
import forge.control.match.ControlPicture; import forge.control.match.ControlPicture;
import forge.control.match.ControlTabber; import forge.control.match.ControlTabber;
import forge.game.GameType;
import forge.properties.ForgePreferences.FPref;
import forge.properties.ForgeProps;
import forge.properties.NewConstants;
import forge.view.ViewMatchUI; import forge.view.ViewMatchUI;
import forge.view.match.ViewField; import forge.view.match.ViewField;
import forge.view.toolbox.FLabel;
import forge.view.toolbox.FSkin;
/** /**
* <p> * <p>
@@ -93,6 +103,34 @@ public class ControlMatchUI implements CardContainer {
f.getPlayer().updateObservers(); f.getPlayer().updateObservers();
} }
// Update avatars
final String[] indices = Singletons.getModel().getPreferences().getPref(FPref.UI_AVATARS).split(",");
final Object[] views = Singletons.getView().getMatchView().getFieldViews().toArray();
for (int i = 0; i < views.length; i++) {
final Image img;
// Update AI quest icon
if (i > 0 && Constant.Runtime.getGameType() == GameType.Quest) {
String filename = ForgeProps.getFile(NewConstants.IMAGE_ICON) + File.separator;
if (Constant.Quest.OPP_ICON_NAME[0] != null) {
filename += Constant.Quest.OPP_ICON_NAME[0];
final File f = new File(filename);
img = (f.exists()
? new ImageIcon(filename).getImage()
: FSkin.getAvatars().get(Integer.parseInt(indices[i])));
}
else {
img = FSkin.getAvatars().get(Integer.parseInt(indices[i]));
}
}
else {
img = FSkin.getAvatars().get(Integer.parseInt(indices[i]));
}
((ViewField) views[i]).getLblAvatar().setIcon(new ImageIcon(img));
((FLabel) ((ViewField) views[i]).getLblAvatar()).getResizeTimer().start();
}
AllZone.getHumanPlayer().getZone(Zone.Hand).updateObservers(); AllZone.getHumanPlayer().getZone(Zone.Hand).updateObservers();
AllZone.getComputerPlayer().getZone(Zone.Hand).updateObservers(); AllZone.getComputerPlayer().getZone(Zone.Hand).updateObservers();
AllZone.getStack().updateObservers(); AllZone.getStack().updateObservers();

View File

@@ -19,13 +19,10 @@ package forge.view.match;
import java.awt.Color; import java.awt.Color;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseAdapter; import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.io.File;
import javax.swing.ImageIcon; import javax.swing.ImageIcon;
import javax.swing.JLabel; import javax.swing.JLabel;
@@ -39,17 +36,12 @@ import javax.swing.border.MatteBorder;
import net.miginfocom.swing.MigLayout; import net.miginfocom.swing.MigLayout;
import arcane.ui.PlayArea; import arcane.ui.PlayArea;
import forge.AllZone; import forge.AllZone;
import forge.Constant;
import forge.Constant.Zone; import forge.Constant.Zone;
import forge.Player; import forge.Player;
import forge.Singletons; import forge.Singletons;
import forge.card.cardfactory.CardFactoryUtil; import forge.card.cardfactory.CardFactoryUtil;
import forge.card.mana.ManaPool; import forge.card.mana.ManaPool;
import forge.control.match.ControlField; import forge.control.match.ControlField;
import forge.game.GameType;
import forge.properties.ForgePreferences.FPref;
import forge.properties.ForgeProps;
import forge.properties.NewConstants;
import forge.view.toolbox.FLabel; import forge.view.toolbox.FLabel;
import forge.view.toolbox.FPanel; import forge.view.toolbox.FPanel;
import forge.view.toolbox.FSkin; import forge.view.toolbox.FSkin;
@@ -75,8 +67,7 @@ public class ViewField extends FPanel {
lblEndCombat, lblMain2, lblEndTurn, lblCleanup; lblEndCombat, lblMain2, lblEndTurn, lblCleanup;
private final JPanel avatarArea, phaseArea, pnlDetails; private final JPanel avatarArea, phaseArea, pnlDetails;
private JLabel lblAvatar, lblLife; private final JLabel lblAvatar, lblLife;
private final Image img;
private final Color clrHover, clrPhaseActiveEnabled, clrPhaseActiveDisabled, private final Color clrHover, clrPhaseActiveEnabled, clrPhaseActiveDisabled,
clrPhaseInactiveEnabled, clrPhaseInactiveDisabled; clrPhaseInactiveEnabled, clrPhaseInactiveDisabled;
/** /**
@@ -99,42 +90,18 @@ public class ViewField extends FPanel {
this.clrPhaseActiveDisabled = FSkin.getColor(FSkin.Colors.CLR_PHASE_ACTIVE_DISABLED); this.clrPhaseActiveDisabled = FSkin.getColor(FSkin.Colors.CLR_PHASE_ACTIVE_DISABLED);
this.clrPhaseInactiveDisabled = FSkin.getColor(FSkin.Colors.CLR_PHASE_INACTIVE_DISABLED); this.clrPhaseInactiveDisabled = FSkin.getColor(FSkin.Colors.CLR_PHASE_INACTIVE_DISABLED);
// Player icon logic
final String[] indexes = Singletons.getModel().getPreferences().getPref(FPref.UI_AVATARS).split(",");
if (player.isHuman()) {
img = FSkin.getAvatars().get(Integer.parseInt(indexes[0]));
}
else if (Constant.Runtime.getGameType() == GameType.Quest) {
String filename = ForgeProps.getFile(NewConstants.IMAGE_ICON) + File.separator;
if (Constant.Quest.OPP_ICON_NAME[0] != null) {
filename += Constant.Quest.OPP_ICON_NAME[0];
final File f = new File(filename);
img = (f.exists()
? new ImageIcon(filename).getImage()
: FSkin.getAvatars().get(Integer.parseInt(indexes[1])));
}
else {
img = FSkin.getAvatars().get(Integer.parseInt(indexes[1]));
}
}
else {
img = FSkin.getAvatars().get(Integer.parseInt(indexes[1]));
}
// Avatar and life // Avatar and life
avatarArea = new JPanel(); avatarArea = new JPanel();
avatarArea.setOpaque(false); avatarArea.setOpaque(false);
avatarArea.setBackground(FSkin.getColor(FSkin.Colors.CLR_HOVER)); avatarArea.setBackground(FSkin.getColor(FSkin.Colors.CLR_HOVER));
avatarArea.setLayout(new MigLayout("insets 0, gap 0")); avatarArea.setLayout(new MigLayout("insets 0, gap 0"));
lblAvatar = new JLabel(); lblAvatar = new FLabel.Builder().fontAlign(SwingConstants.CENTER)
lblAvatar.setHorizontalAlignment(SwingConstants.CENTER); .iconScaleFactor(1.0f).build();
avatarArea.add(lblAvatar, "w 100%!, wrap, gaptop 4%"); avatarArea.add(lblAvatar, "w 100%!, h 70%!, wrap, gaptop 4%");
lblLife = new JLabel(); lblLife = new FLabel.Builder().fontAlign(SwingConstants.CENTER)
lblLife.setHorizontalAlignment(SwingConstants.CENTER); .fontStyle(Font.BOLD).build();
lblLife.setForeground(FSkin.getColor(FSkin.Colors.CLR_TEXT));
avatarArea.add(lblLife, "w 100%!, h 30%!, gaptop 4%"); avatarArea.add(lblLife, "w 100%!, h 30%!, gaptop 4%");
this.add(avatarArea, "w 10%!, h 30%!"); this.add(avatarArea, "w 10%!, h 30%!");
@@ -167,17 +134,6 @@ public class ViewField extends FPanel {
populateDetails(); populateDetails();
this.add(pnlDetails, "w 10%!, h 69%!, gapleft 1px"); this.add(pnlDetails, "w 10%!, h 69%!, gapleft 1px");
// Resize adapter
this.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
int side = (int) (avatarArea.getHeight() * 0.7);
int size = (int) (avatarArea.getHeight() * 0.24);
lblLife.setFont(FSkin.getBoldFont(size));
lblAvatar.setIcon(new ImageIcon(img.getScaledInstance(side, side, java.awt.Image.SCALE_SMOOTH)));
}
});
// Player hover effect // Player hover effect
avatarArea.addMouseListener(new MouseAdapter() { avatarArea.addMouseListener(new MouseAdapter() {
@Override @Override
@@ -411,6 +367,11 @@ public class ViewField extends FPanel {
return this.avatarArea; return this.avatarArea;
} }
/** @return {@link javax.swing.JLabel} */
public JLabel getLblAvatar() {
return this.lblAvatar;
}
/** @return {@link javax.swing.JLabel} */ /** @return {@link javax.swing.JLabel} */
public JLabel getLblLife() { public JLabel getLblLife() {
return this.lblLife; return this.lblLife;

View File

@@ -291,6 +291,17 @@ public class FLabel extends JLabel {
this.setHorizontalAlignment(i0); this.setHorizontalAlignment(i0);
} }
/** Resizing in MigLayout "slides" between the original and destination sizes.
* To prevent this label from recalculating on each increment, a timer
* is run to check that the the "sliding" is finished. To resize this label
* explicitly, retrieve this timer and start it. It will stop automatically.
*
* @return {@link javax.swing.Timer}
*/
public Timer getResizeTimer() {
return this.resizeTimer;
}
@Override @Override
// Must be public. // Must be public.
public void setIcon(final Icon i0) { public void setIcon(final Icon i0) {
@@ -335,7 +346,7 @@ public class FLabel extends JLabel {
// Opacity, select // Opacity, select
if (this.opaque && !selected) { if (this.opaque && !selected) {
g2d.setColor(clrInactive); g2d.setColor(getBackground());
g2d.fillRect(0, 0, w, h); g2d.fillRect(0, 0, w, h);
} }
else if (selectable && selected) { else if (selectable && selected) {