Skip to content

Commit

Permalink
Merge pull request #5383 from SJuliez/boardview-remove-super-subclass
Browse files Browse the repository at this point in the history
Boardview subclassing
  • Loading branch information
SJuliez authored Apr 20, 2024
2 parents e8b024b + 661c8fa commit 98284ce
Show file tree
Hide file tree
Showing 14 changed files with 362 additions and 270 deletions.
6 changes: 2 additions & 4 deletions megamek/src/megamek/client/event/BoardViewListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

package megamek.client.event;

import megamek.common.Coords;

/**
* Classes which implement this interface provide methods that deal with the
* events that are generated when the BoardView is changed.
Expand Down Expand Up @@ -68,10 +66,10 @@ public interface BoardViewListener extends java.util.EventListener {

/**
* Sent when secondLOS is set.
*
*
* @param b an event
*/
public void secondLOSHex(BoardViewEvent b, Coords c);
public void secondLOSHex(BoardViewEvent b);

/**
* Sent when moving units is finished.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

package megamek.client.event;

import megamek.common.Coords;

/**
* This adapter class provides default implementations for the methods described
* by the <code>BoardViewListener</code> interface.
Expand Down Expand Up @@ -79,11 +77,11 @@ public void firstLOSHex(BoardViewEvent b) {

/**
* Sent when secondLOS is set. The default behavior is to do nothing.
*
*
* @param b an event
*/
@Override
public void secondLOSHex(BoardViewEvent b, Coords c) {
public void secondLOSHex(BoardViewEvent b) {
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import megamek.client.ui.swing.util.UIUtil;
import megamek.client.ui.swing.widget.*;
import megamek.common.Configuration;
import megamek.common.Coords;
import megamek.common.event.*;
import megamek.common.util.Distractable;
import megamek.common.util.DistractableAdapter;
Expand Down Expand Up @@ -166,7 +165,7 @@ public void firstLOSHex(BoardViewEvent b) {
}

@Override
public void secondLOSHex(BoardViewEvent b, Coords c) {
public void secondLOSHex(BoardViewEvent b) {
//noaction default
}

Expand Down
6 changes: 4 additions & 2 deletions megamek/src/megamek/client/ui/swing/ActionPhaseDisplay.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package megamek.client.ui.swing;

import megamek.client.ui.swing.boardview.TurnDetailsOverlay;
import megamek.client.ui.swing.util.KeyCommandBind;
import megamek.client.ui.swing.util.UIUtil;
import megamek.client.ui.swing.widget.MegamekButton;
Expand Down Expand Up @@ -341,8 +342,9 @@ protected void updateDonePanelButtons(final String doneButtonLabel, final String
butSkipTurn.setEnabled(true);
}

if (clientgui.getBoardView().turnDetailsOverlay != null) {
clientgui.getBoardView().turnDetailsOverlay.setLines(turnDetails);
TurnDetailsOverlay turnDetailsOverlay = clientgui.getBoardView().getTurnDetailsOverlay();
if (turnDetailsOverlay != null) {
turnDetailsOverlay.setLines(turnDetails);
}
}

Expand Down
9 changes: 6 additions & 3 deletions megamek/src/megamek/client/ui/swing/BoardEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
import megamek.client.ui.dialogs.helpDialogs.AbstractHelpDialog;
import megamek.client.ui.dialogs.helpDialogs.BoardEditorHelpDialog;
import megamek.client.ui.swing.boardview.BoardView;
import megamek.client.ui.swing.boardview.KeyBindingsOverlay;
import megamek.client.ui.swing.boardview.PlanetaryConditionsOverlay;
import megamek.client.ui.swing.boardview.TurnDetailsOverlay;
import megamek.client.ui.swing.dialog.FloodDialog;
import megamek.client.ui.swing.dialog.LevelChangeDialog;
import megamek.client.ui.swing.dialog.MMConfirmDialog;
Expand Down Expand Up @@ -339,9 +342,10 @@ public BoardEditor(MegaMekController c) {
controller = c;
try {
bv = new BoardView(game, controller, null);
bv.planetaryConditionsOverlay.setVisible(false);
bv.addOverlay(new KeyBindingsOverlay(bv));
bv.setUseLosTool(false);
bv.setDisplayInvalidFields(true);
bvc = bv.getComponent(true);
bv.setDisplayInvalidHexInfo(true);
} catch (IOException e) {
JOptionPane.showMessageDialog(frame,
Messages.getString("BoardEditor.CouldntInitialize") + e,
Expand Down Expand Up @@ -457,7 +461,6 @@ public void hexMoused(BoardViewEvent b) {
}
});

bv.setUseLOSTool(false);
setupEditorPanel();
setupFrame();
frame.setVisible(true);
Expand Down
18 changes: 12 additions & 6 deletions megamek/src/megamek/client/ui/swing/ClientGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
import megamek.client.ui.swing.audio.SoundManager;
import megamek.client.ui.swing.audio.SoundType;
import megamek.client.ui.swing.boardview.BoardView;
import megamek.client.ui.swing.boardview.KeyBindingsOverlay;
import megamek.client.ui.swing.boardview.PlanetaryConditionsOverlay;
import megamek.client.ui.swing.boardview.TurnDetailsOverlay;
import megamek.client.ui.swing.dialog.AbstractUnitSelectorDialog;
import megamek.client.ui.swing.dialog.MegaMekUnitSelectorDialog;
import megamek.client.ui.swing.forceDisplay.ForceDisplayDialog;
Expand Down Expand Up @@ -522,8 +525,11 @@ public void initialize() {
initializeFrame();
try {
client.getGame().addGameListener(gameListener);
// Create the board viewer.

bv = new BoardView(client.getGame(), controller, this);
bv.addOverlay(new KeyBindingsOverlay(bv));
bv.addOverlay(new PlanetaryConditionsOverlay(bv));
bv.addOverlay(new TurnDetailsOverlay(bv));
bv.getPanel().setPreferredSize(getSize());
bvc = bv.getComponent();
bvc.setName(CG_BOARDVIEW);
Expand Down Expand Up @@ -588,7 +594,7 @@ public void windowClosing(WindowEvent e) {
}
});
cb2 = new ChatterBox2(this, bv, controller);
bv.addDisplayable(cb2);
bv.addOverlay(cb2);
bv.getPanel().addKeyListener(cb2);
uo = new UnitOverview(this);
offBoardOverlay = new OffBoardTargetOverlay(this);
Expand All @@ -597,8 +603,8 @@ public void windowClosing(WindowEvent e) {
aw.setLocation(0, 0);
aw.setSize(300, 300);

bv.addDisplayable(uo);
bv.addDisplayable(offBoardOverlay);
bv.addOverlay(uo);
bv.addOverlay(offBoardOverlay);

setUnitDisplay(new UnitDisplay(this, controller));
getUnitDisplay().addMechDisplayListener(bv);
Expand Down Expand Up @@ -1110,7 +1116,7 @@ public void die() {
boolean reportHandled = false;
if (bv != null) {
// cleanup our timers first
bv.die();
bv.dispose();
}

for (String s : phaseComponents.keySet()) {
Expand Down Expand Up @@ -2816,7 +2822,7 @@ public void firstLOSHex(BoardViewEvent b) {
}

@Override
public void secondLOSHex(BoardViewEvent b, Coords c) {
public void secondLOSHex(BoardViewEvent b) {
// ignored
}

Expand Down
2 changes: 1 addition & 1 deletion megamek/src/megamek/client/ui/swing/Ruler.java
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ public void firstLOSHex(BoardViewEvent b) {
}

@Override
public void secondLOSHex(BoardViewEvent b, Coords c) {
public void secondLOSHex(BoardViewEvent b) {
//ignored
}

Expand Down
110 changes: 110 additions & 0 deletions megamek/src/megamek/client/ui/swing/boardview/AbstractBoardView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Copyright (c) 2024 - The MegaMek Team. All Rights Reserved.
*
* This file is part of MegaMek.
*
* 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.boardview;

import megamek.client.event.BoardViewEvent;
import megamek.client.event.BoardViewListener;
import megamek.client.ui.IDisplayable;

import java.util.ArrayList;
import java.util.LinkedHashSet;

public abstract class AbstractBoardView implements IBoardView {

protected final ArrayList<BoardViewListener> boardViewListeners = new ArrayList<>();
protected final LinkedHashSet<IDisplayable> overlays = new LinkedHashSet<>();

/**
* Notifies attached BoardViewListeners of the event.
*
* @param event the board event.
*/
public void processBoardViewEvent(BoardViewEvent event) {
// Copy the listener list to allow concurrent modification
for (BoardViewListener l : new ArrayList<>(boardViewListeners)) {
switch (event.getType()) {
case BoardViewEvent.BOARD_HEX_CLICKED:
case BoardViewEvent.BOARD_HEX_DOUBLECLICKED:
case BoardViewEvent.BOARD_HEX_DRAGGED:
case BoardViewEvent.BOARD_HEX_POPUP:
l.hexMoused(event);
break;
case BoardViewEvent.BOARD_HEX_CURSOR:
l.hexCursor(event);
break;
case BoardViewEvent.BOARD_HEX_HIGHLIGHTED:
l.boardHexHighlighted(event);
break;
case BoardViewEvent.BOARD_HEX_SELECTED:
l.hexSelected(event);
break;
case BoardViewEvent.BOARD_FIRST_LOS_HEX:
l.firstLOSHex(event);
break;
case BoardViewEvent.BOARD_SECOND_LOS_HEX:
l.secondLOSHex(event);
break;
case BoardViewEvent.FINISHED_MOVING_UNITS:
l.finishedMovingUnits(event);
break;
case BoardViewEvent.SELECT_UNIT:
l.unitSelected(event);
break;
}
}
}

/**
* Adds the specified BoardViewListener to receive board events from this boardview. A listener that is
* already present is not added again.
*
* @param listener the BoardViewListener
*/
@Override
public final void addBoardViewListener(BoardViewListener listener) {
if (!boardViewListeners.contains(listener)) {
boardViewListeners.add(listener);
}
}

/**
* Removes the specified BoardViewListener.
*
* @param listener the BoardViewListener
*/
@Override
public final void removeBoardViewListener(BoardViewListener listener) {
boardViewListeners.remove(listener);
}

@Override
public void dispose() {
boardViewListeners.clear();
}

@Override
public final void addOverlay(IDisplayable overlay) {
overlays.add(overlay);
}

@Override
public final void removeOverlay(IDisplayable overlay) {
overlays.remove(overlay);
}
}
Loading

0 comments on commit 98284ce

Please sign in to comment.