- Fix match music not stopping when moving Forge to background

- Fix quit warning not mentioning matches when one or more are active
- Some minor fixes and cleanup
This commit is contained in:
elcnesh
2015-02-22 00:18:50 +00:00
parent 215ccc4c78
commit ab1d24cc8a
8 changed files with 59 additions and 20 deletions

View File

@@ -274,6 +274,8 @@ public class GuiDesktop implements IGuiBase {
@Override
public HostedMatch hostMatch() {
return new HostedMatch();
final HostedMatch match = new HostedMatch();
Singletons.getControl().addMatch(match);
return match;
}
}

View File

@@ -27,6 +27,7 @@ import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.util.Collections;
import java.util.List;
import javax.swing.ImageIcon;
@@ -34,6 +35,7 @@ import javax.swing.JLayeredPane;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import forge.ImageCache;
@@ -86,6 +88,28 @@ public enum FControl implements KeyEventDispatcher {
EXIT_FORGE;
}
private boolean hasCurrentMatches() {
cleanMatches();
return !currentMatches.isEmpty();
}
public List<HostedMatch> getCurrentMatches() {
cleanMatches();
return Collections.unmodifiableList(currentMatches);
}
public void addMatch(final HostedMatch match) {
cleanMatches();
currentMatches.add(match);
}
private void cleanMatches() {
for (final HostedMatch match : ImmutableList.copyOf(currentMatches)) {
if (match.isMatchOver()) {
currentMatches.remove(match);
}
}
}
/**
* <p>
* FControl.
@@ -139,7 +163,7 @@ public enum FControl implements KeyEventDispatcher {
return closeAction;
}
public void setCloseAction(CloseAction closeAction0) {
public void setCloseAction(final CloseAction closeAction0) {
if (closeAction == closeAction0) { return; }
closeAction = closeAction0;
Singletons.getView().getNavigationBar().updateBtnCloseTooltip();
@@ -149,13 +173,14 @@ public enum FControl implements KeyEventDispatcher {
prefs.save();
}
public boolean canExitForge(boolean forRestart) {
String action = (forRestart ? "Restart" : "Exit");
public boolean canExitForge(final boolean forRestart) {
final String action = (forRestart ? "Restart" : "Exit");
String userPrompt = "Are you sure you wish to " + (forRestart ? "restart" : "exit") + " Forge?";
if (!currentMatches.isEmpty()) {
userPrompt = "A game is currently active. " + userPrompt;
final boolean hasCurrentMatches = hasCurrentMatches();
if (hasCurrentMatches) {
userPrompt = "One or more games are currently active. " + userPrompt;
}
if (!FOptionPane.showConfirmDialog(userPrompt, action + " Forge", action, "Cancel", currentMatches.isEmpty())) { //default Yes if no game active
if (!FOptionPane.showConfirmDialog(userPrompt, action + " Forge", action, "Cancel", !hasCurrentMatches)) { //default Yes if no game active
return false;
}
if (!CDeckEditorUI.SINGLETON_INSTANCE.canSwitchAway(true)) {

View File

@@ -36,7 +36,6 @@ import javax.swing.MenuElement;
import javax.swing.MenuSelectionManager;
import javax.swing.SwingUtilities;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import com.google.common.base.Function;
@@ -60,6 +59,7 @@ import forge.game.combat.CombatView;
import forge.game.phase.PhaseType;
import forge.game.player.DelayedReveal;
import forge.game.player.IHasIcon;
import forge.game.player.Player;
import forge.game.player.PlayerView;
import forge.game.spellability.SpellAbility;
import forge.game.zone.ZoneType;
@@ -105,7 +105,6 @@ import forge.toolbox.special.PhaseIndicator;
import forge.toolbox.special.PhaseLabel;
import forge.util.FCollectionView;
import forge.util.ITriggerEvent;
import forge.util.gui.SGuiChoose;
import forge.util.gui.SOptionPane;
import forge.view.FView;
import forge.view.arcane.CardPanel;
@@ -249,7 +248,7 @@ public final class CMatchUI
view.getLblAvatar().getResizeTimer().start();
}
public void initMatch(final FCollectionView<PlayerView> sortedPlayers, final FCollectionView<PlayerView> myPlayers) {
public void initMatch(final FCollectionView<PlayerView> sortedPlayers, final Iterable<PlayerView> myPlayers) {
this.sortedPlayers = sortedPlayers;
this.setLocalPlayers(myPlayers);
allHands = sortedPlayers.size() == getLocalPlayerCount();
@@ -766,7 +765,7 @@ public final class CMatchUI
}
@Override
public void openView(final FCollectionView<PlayerView> myPlayers) {
public void openView(final Iterable<PlayerView> myPlayers) {
final GameView gameView = getGameView();
gameView.getGameLog().addObserver(getCLog());

View File

@@ -19,8 +19,10 @@ import javax.swing.JRootPane;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
import forge.Singletons;
import forge.gui.framework.SDisplayUtil;
import forge.gui.framework.SResizingUtil;
import forge.match.HostedMatch;
import forge.model.FModel;
import forge.properties.ForgePreferences;
import forge.properties.ForgePreferences.FPref;
@@ -90,14 +92,18 @@ public class FFrame extends SkinnedFrame implements ITitleBarOwner {
private void pause() {
if (paused || !isMainFrame) { return; }
//HostedMatch.pause();
for (final HostedMatch hostedMatch : Singletons.getControl().getCurrentMatches()) {
hostedMatch.pause();
}
paused = true;
}
private void resume() {
if (!paused || !isMainFrame) { return; }
//HostedMatch.resume();
for (final HostedMatch hostedMatch : Singletons.getControl().getCurrentMatches()) {
hostedMatch.resume();
}
paused = false;
}

View File

@@ -111,7 +111,7 @@ public class MatchController extends AbstractGuiGame {
}
@Override
public void openView(final FCollectionView<PlayerView> myPlayers) {
public void openView(final Iterable<PlayerView> myPlayers) {
setLocalPlayers(myPlayers);
final boolean noHumans = !hasLocalPlayers();

View File

@@ -30,7 +30,7 @@ public interface IGuiGame {
void setGameView(GameView gameView);
void setGameController(PlayerView player, IGameController gameController);
boolean resetForNewGame();
void openView(FCollectionView<PlayerView> myPlayers);
void openView(Iterable<PlayerView> myPlayers);
void afterGameEnd();
void showCombat();
void showPromptMessage(PlayerView playerView, String message);

View File

@@ -28,15 +28,16 @@ import forge.interfaces.IButton;
import forge.interfaces.IGameController;
import forge.interfaces.IGuiGame;
import forge.interfaces.IMayViewCards;
import forge.util.FCollection;
import forge.util.FCollectionView;
public abstract class AbstractGuiGame implements IGuiGame, IMayViewCards {
private FCollectionView<PlayerView> localPlayers;
private FCollectionView<PlayerView> localPlayers = new FCollection<PlayerView>();
private PlayerView currentPlayer = null;
protected final void setLocalPlayers(final FCollectionView<PlayerView> players) {
this.localPlayers = players;
this.currentPlayer = players == null ? null : Iterables.getFirst(players, null);
protected final void setLocalPlayers(final Iterable<PlayerView> myPlayers) {
this.localPlayers = myPlayers == null ? new FCollection<PlayerView>() : new FCollection<PlayerView>(myPlayers);
this.currentPlayer = Iterables.getFirst(this.localPlayers, null);
}
public final boolean hasLocalPlayers() {
return localPlayers != null && !localPlayers.isEmpty();

View File

@@ -61,6 +61,7 @@ public class HostedMatch {
private FControlGamePlayback playbackControl = null;
private final MatchUiEventVisitor visitor = new MatchUiEventVisitor();
private final Map<PlayerControllerHuman, NextGameDecision> nextGameDecisions = Maps.newHashMap();
private boolean isMatchOver = false;
public HostedMatch() {
}
@@ -222,7 +223,7 @@ public class HostedMatch {
match.startGame(game);
// After game is over...
if (humanCount == 0) {
if (match.isMatchOver() && humanCount == 0) {
// ... if no human players, let AI decide next game
addNextGameDecision(null, NextGameDecision.CONTINUE);
}
@@ -256,6 +257,7 @@ public class HostedMatch {
humanController.getGui().afterGameEnd();
}
humanControllers.clear();
isMatchOver = true;
}
public void pause() {
@@ -273,6 +275,10 @@ public class HostedMatch {
SoundSystem.instance.resume();
}
public boolean isMatchOver() {
return isMatchOver;
}
/** Returns a random name from the supplied list. */
public static String getRandomName() {
final String playerName = GuiDisplayUtil.getPlayerName();