Skip to content

Commit

Permalink
Merge pull request #5571 from SJuliez/code-clean-towards-SBF-statusba…
Browse files Browse the repository at this point in the history
…rphasedisplays3

PhaseDisplay Code Cleanup
  • Loading branch information
HammerGS authored Jun 16, 2024
2 parents b30485f + 404691e commit 84c9469
Show file tree
Hide file tree
Showing 21 changed files with 253 additions and 375 deletions.
106 changes: 71 additions & 35 deletions megamek/src/megamek/client/ui/swing/AbstractPhaseDisplay.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
/*
* MegaMek - Copyright (C) 2000-2003 Ben Mazur ([email protected])
* Copyright (c) 2000-2003 Ben Mazur ([email protected])
* Copyright (c) 2024 - The MegaMek Team. All Rights Reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
* This file is part of MegaMek.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
* MegaMek is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MegaMek is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MegaMek. If not, see <http://www.gnu.org/licenses/>.
*/
package megamek.client.ui.swing;

Expand All @@ -20,53 +26,74 @@
import megamek.client.ui.swing.widget.*;
import megamek.common.event.*;
import megamek.common.util.Distractable;
import megamek.common.util.DistractableAdapter;
import megamek.common.util.DistractableDelegate;

import java.util.Objects;

import static megamek.client.ui.swing.util.UIUtil.guiScaledFontHTML;

/**
* This is the base class for all the "displays" which take control during the local player's turn.
* Only one display is shown at each time; the ChatLounge is also a display. The ChatLounge doesn't
* show the boardview but most other displays do. Typically the display itself is the button bar
* at the bottom of the GUI.
*
* Note that a display being active does not mean that it is also the player's turn. The display
* should allow inspecting units and other actions even when it's another player's turn. Also,
* even when it's the local player's turn, a unit is not necessarily selected. The unit that is
* selected to act (if there is one) is not necessarily the same as the one open in the unit display.
*/
public abstract class AbstractPhaseDisplay extends SkinnedJPanel implements
BoardViewListener, GameListener, Distractable {

public static final int DONE_BUTTON_WIDTH = 160;

protected DistractableAdapter distracted = new DistractableAdapter();
protected MegamekButton butDone;
protected final MegamekButton butDone;

private final DistractableDelegate distractableDelegate = new DistractableDelegate();

/** The IClientgui that this display is a part of. Cannot be null. */
private final IClientGUI clientgui;

/**
* Creates a phase display using the standard skin settings for phase displays.
*
* @param cg The IClientGUI parent of this display
*/
protected AbstractPhaseDisplay(IClientGUI cg) {
this(cg, SkinSpecification.UIComponents.PhaseDisplay.getComp(),
SkinSpecification.UIComponents.PhaseDisplayDoneButton.getComp());
}

protected AbstractPhaseDisplay(IClientGUI cg, String borderSkinComp, String buttonSkinComp) {
super(borderSkinComp, 0);
/**
* Creates a phase display using the given skin settings for the button panel and the buttons.
*
* @param cg The IClientGUI parent of this display
* @see SkinSpecification.UIComponents#getComp()
*/
protected AbstractPhaseDisplay(IClientGUI cg, String panelSkin, String buttonSkin) {
super(panelSkin, 0);
clientgui = Objects.requireNonNull(cg);
setBorder(new MegamekBorder(borderSkinComp));
butDone = new MegamekButton("DONE", buttonSkinComp);
setBorder(new MegamekBorder(panelSkin));

butDone = new MegamekButton("DONE", buttonSkin);
String f = guiScaledFontHTML(UIUtil.uiLightViolet()) + KeyCommandBind.getDesc(KeyCommandBind.DONE)+ "</FONT>";
butDone.setToolTipText("<html><body>" + f + "</body></html>");
butDone.setActionCommand("doneButton");
butDone.addActionListener(e -> {
if (shouldPerformKeyCommands()) {
done();
}
});

MegaMekGUI.getKeyDispatcher().registerCommandAction(KeyCommandBind.DONE, this::shouldPerformKeyCommands, this::done);
butDone.addActionListener(e -> done());

MegaMekGUI.getKeyDispatcher().registerCommandAction(KeyCommandBind.DONE, this::shouldperformDone, this::done);
}

private void done() {
// When the turn is ended, we could miss a key release event
// This will ensure no repeating keys are stuck down
MegaMekGUI.getKeyDispatcher().stopAllRepeating();
ready();
if (shouldperformDone()) {
// When the turn is ended, we could miss a key release event
// This will ensure no repeating keys are stuck down
MegaMekGUI.getKeyDispatcher().stopAllRepeating();
ready();
}
}

private boolean shouldPerformKeyCommands() {
private boolean shouldperformDone() {
return ((clientgui.getClient().isMyTurn()
|| (clientgui.getClient().getGame().getTurn() == null)
|| (clientgui.getClient().getGame().getPhase().isReport())))
Expand All @@ -78,21 +105,26 @@ && isVisible()

@Override
public final boolean isIgnoringEvents() {
return distracted.isIgnoringEvents();
return distractableDelegate.isIgnoringEvents();
}

@Override
public final void setIgnoringEvents(boolean distracted) {
this.distracted.setIgnoringEvents(distracted);
public final void setIgnoringEvents(boolean isDistracted) {
distractableDelegate.setIgnoringEvents(isDistracted);
}

public void ready() { }
/**
* Tells the display to finish the current player turn and send all planned actions to the server.
* Planned actions are e.g. movement, attacks or deployment. Usually, the planned actions are all
* actions that, together, make up a single unit's turn, e.g. all weapon attacks of one unit.
*/
public abstract void ready();

public IClientGUI getClientgui() {
return clientgui;
}

// BoardListener
//region Empty BoardListener

@Override
public void hexMoused(BoardViewEvent b) { }
Expand All @@ -118,7 +150,9 @@ public void finishedMovingUnits(BoardViewEvent b) { }
@Override
public void unitSelected(BoardViewEvent b) { }

// GameListener
//endregion

//region Empty GameListener

@Override
public void gamePlayerConnected(GamePlayerConnectedEvent e) { }
Expand Down Expand Up @@ -176,4 +210,6 @@ public void gameClientFeedbackRequest(GameCFREvent evt) { }

@Override
public void gameVictory(GameVictoryEvent e) { }

//endregion
}
3 changes: 2 additions & 1 deletion megamek/src/megamek/client/ui/swing/AttackPhaseDisplay.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import megamek.common.actions.*;

public abstract class AttackPhaseDisplay extends ActionPhaseDisplay {

// client list of attacks user has input
protected EntityActionLog attacks;

Expand Down Expand Up @@ -54,7 +55,7 @@ protected void updateDonePanel() {
}
}

protected void removeAttack(Object o) {
protected void removeAttack(EntityAction o) {
attacks.remove(o);
updateDonePanel();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -518,9 +518,6 @@ private void setRemoveMineEnabled(boolean enable) {
buttons.get(DeployMinefieldCommand.REMOVE_MINES).setEnabled(enable);
}

/**
* Stop just ignoring events and actually stop listening to them.
*/
@Override
public void removeAllListeners() {
clientgui.getClient().getGame().removeGameListener(this);
Expand Down
14 changes: 5 additions & 9 deletions megamek/src/megamek/client/ui/swing/DeploymentDisplay.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public enum DeployCommand implements PhaseCommand {
DEPLOY_ASSAULTDROP("assaultDrop"),
DEPLOY_DOCK("deployDock");

public String cmd;
public final String cmd;

/**
* Priority that determines this buttons order
Expand Down Expand Up @@ -90,13 +90,9 @@ public String getHotKeyDesc() {
String msg_next= Messages.getString("Next");
String msg_previous = Messages.getString("Previous");

switch (this) {
case DEPLOY_NEXT:
result += "&nbsp;&nbsp;" + msg_next + ": " + KeyCommandBind.getDesc(KeyCommandBind.NEXT_UNIT);
result += "&nbsp;&nbsp;" + msg_previous + ": " + KeyCommandBind.getDesc(KeyCommandBind.PREV_UNIT);
break;
default:
break;
if (this == DeployCommand.DEPLOY_NEXT) {
result += "&nbsp;&nbsp;" + msg_next + ": " + KeyCommandBind.getDesc(KeyCommandBind.NEXT_UNIT);
result += "&nbsp;&nbsp;" + msg_previous + ": " + KeyCommandBind.getDesc(KeyCommandBind.PREV_UNIT);
}

return result;
Expand Down Expand Up @@ -133,6 +129,7 @@ protected void setButtons() {
}
numButtonGroups = (int) Math.ceil((buttons.size() + 0.0) / buttonsPerGroup);
}

@Override
protected void setButtonsTooltips() {
for (DeployCommand cmd : DeployCommand.values()) {
Expand Down Expand Up @@ -341,7 +338,6 @@ private boolean checkNags() {
return false;
}

/** Sends a deployment to the server. */
@Override
public void ready() {
final Game game = clientgui.getClient().getGame();
Expand Down
10 changes: 0 additions & 10 deletions megamek/src/megamek/client/ui/swing/FiringDisplay.java
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,8 @@ public FiringDisplay(final ClientGUI clientgui) {

setupButtonPanel();

clientgui.getBoardView().getPanel().addKeyListener(this);

// mech display.
clientgui.getUnitDisplay().wPan.weaponList.addListSelectionListener(this);
clientgui.getUnitDisplay().wPan.weaponList.addKeyListener(this);

ash = new AimedShotHandler(this);

Expand Down Expand Up @@ -793,10 +790,6 @@ && ce().isCapitalFighter()) {
return false;
}

/**
* Called when the current entity is done firing. Send out our attack queue
* to the server.
*/
@Override
public void ready() {
if (checkNags()) {
Expand Down Expand Up @@ -2138,9 +2131,6 @@ public void valueChanged(ListSelectionEvent event) {
}
}

/**
* Stop just ignoring events and actually stop listening to them.
*/
@Override
public void removeAllListeners() {
clientgui.getClient().getGame().removeGameListener(this);
Expand Down
4 changes: 4 additions & 0 deletions megamek/src/megamek/client/ui/swing/IClientGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,8 @@ public interface IClientGUI {
IClient getClient();

JComponent turnTimerComponent();

default boolean isChatBoxActive() {
return false;
}
}
7 changes: 0 additions & 7 deletions megamek/src/megamek/client/ui/swing/MovementDisplay.java
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,6 @@ public MovementDisplay(final ClientGUI clientgui) {
clientgui.getClient().getGame().addGameListener(this);
clientgui.getBoardView().addBoardViewListener(this);
clientgui.getClient().getGame().setupTeams();
clientgui.getBoardView().getPanel().addKeyListener(this);
}

setupStatusBar(Messages.getString("MovementDisplay.waitingForMovementPhase"));
Expand Down Expand Up @@ -1634,9 +1633,6 @@ && ce().getMovementMode().isSubmarine()
return false;
}

/**
* Sends a data packet indicating the chosen movement.
*/
@Override
public synchronized void ready() {
if (ce() == null) {
Expand Down Expand Up @@ -5680,9 +5676,6 @@ private void setBombEnabled(boolean enabled) {
clientgui.getMenuBar().setEnabled(MoveCommand.MOVE_BOMB.getCmd(), enabled);
}

/**
* Stop just ignoring events and actually stop listening to them.
*/
@Override
public void removeAllListeners() {
if (clientgui != null) {
Expand Down
6 changes: 0 additions & 6 deletions megamek/src/megamek/client/ui/swing/PhysicalDisplay.java
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,6 @@ private boolean checkNags() {
return false;
}

/**
* Called when the current entity is done with physical attacks.
*/
@Override
public void ready() {
if (checkNags()) {
Expand Down Expand Up @@ -1643,9 +1640,6 @@ private void setSearchlightEnabled(boolean enabled) {
clientgui.getMenuBar().setEnabled(PhysicalCommand.PHYSICAL_SEARCHLIGHT.getCmd(), enabled);
}

/**
* Stop just ignoring events and actually stop listening to them.
*/
@Override
public void removeAllListeners() {
clientgui.getClient().getGame().removeGameListener(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,10 +406,6 @@ && ce().isCapitalFighter()) {
return false;
}

/**
* Called when the current entity is done firing. Send out our attack queue
* to the server.
*/
@Override
public void ready() {
if (checkNags()) {
Expand Down
9 changes: 1 addition & 8 deletions megamek/src/megamek/client/ui/swing/PrephaseDisplay.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
* PrephaseDisplay for revealing hidden units. This occurs before Move and Firing
*/
public class PrephaseDisplay extends StatusBarPhaseDisplay implements
KeyListener, ItemListener, ListSelectionListener {
ItemListener, ListSelectionListener {
private static final long serialVersionUID = 3441669419807288865L;

/**
Expand Down Expand Up @@ -180,11 +180,8 @@ public void initializeListeners() {
clientgui.getClient().getGame().addGameListener(this);
clientgui.getBoardView().addBoardViewListener(this);

clientgui.getBoardView().getPanel().addKeyListener(this);

// mech display.
clientgui.getUnitDisplay().wPan.weaponList.addListSelectionListener(this);
clientgui.getUnitDisplay().wPan.weaponList.addKeyListener(this);
}

@Override
Expand Down Expand Up @@ -269,10 +266,6 @@ private void refreshButtons() {
butDone.setEnabled(true);
}

/**
* Called when the current entity is done firing. Send out our attack queue
* to the server.
*/
@Override
public void ready() {
// stop further input (hopefully)
Expand Down
Loading

0 comments on commit 84c9469

Please sign in to comment.