📄 targetingphasedisplay.java
字号:
/* * MegaMek - Copyright (C) 2004 Ben Mazur (bmazur@sev.org) * * 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 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. */package megamek.client.ui.swing;import megamek.client.Client;import megamek.client.event.BoardViewEvent;import megamek.client.event.BoardViewListener;import megamek.common.AmmoType;import megamek.common.Compute;import megamek.common.Coords;import megamek.common.Entity;import megamek.common.GameTurn;import megamek.common.HexTarget;import megamek.common.IGame;import megamek.common.Mech;import megamek.common.Mounted;import megamek.common.Targetable;import megamek.common.ToHitData;import megamek.common.WeaponType;import megamek.common.actions.FlipArmsAction;import megamek.common.actions.SearchlightAttackAction;import megamek.common.actions.TorsoTwistAction;import megamek.common.actions.WeaponAttackAction;import megamek.common.event.GameListener;import megamek.common.event.GamePhaseChangeEvent;import megamek.common.event.GameTurnChangeEvent;import megamek.common.util.Distractable;import megamek.common.util.DistractableAdapter;import javax.swing.JButton;import javax.swing.JComponent;import javax.swing.JPanel;import javax.swing.event.ListSelectionEvent;import javax.swing.event.ListSelectionListener;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.GridLayout;import java.awt.Insets;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.InputEvent;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.awt.event.MouseEvent;import java.util.Comparator;import java.util.Enumeration;import java.util.Iterator;import java.util.TreeSet;import java.util.Vector;/*Targeting Phase Display. Breaks naming convention becauseTargetingDisplay is too easy to confuse with something else*/public class TargetingPhaseDisplay extends StatusBarPhaseDisplay implements GameListener, ActionListener, DoneButtoned, KeyListener, ItemListener, BoardViewListener, Distractable, ListSelectionListener { // Distraction implementation. private DistractableAdapter distracted = new DistractableAdapter(); // Action command names private static final String FIRE_FIRE = "fireFire"; //$NON-NLS-1$ private static final String FIRE_MODE = "fireMode"; //$NON-NLS-1$ private static final String FIRE_FLIP_ARMS = "fireFlipArms"; //$NON-NLS-1$ private static final String FIRE_NEXT = "fireNext"; //$NON-NLS-1$ private static final String FIRE_NEXT_TARG = "fireNextTarg"; //$NON-NLS-1$ private static final String FIRE_SKIP = "fireSkip"; //$NON-NLS-1$ private static final String FIRE_TWIST = "fireTwist"; //$NON-NLS-1$ private static final String FIRE_CANCEL = "fireCancel"; //$NON-NLS-1$ private static final String FIRE_SEARCHLIGHT = "fireSearchlight"; //$NON-NLS-1$ // parent game private ClientGUI clientgui; private Client client; // buttons private JComponent panButtons; private JButton butFire; private JButton butTwist; private JButton butSkip; private JButton butFlipArms; private JButton butFireMode; private JButton butSpace; private JButton butNext; private JButton butNextTarg; private JButton butDone; private JButton butSearchlight; // let's keep track of what we're shooting and at what, too private int cen = Entity.NONE; // current entity number private Targetable target; // target // shots we have so far. private Vector attacks; // is the shift key held? private boolean shiftheld; private boolean twisting; private final int phase; private Entity[] visibleTargets; private int lastTargetID = -1; /** * Creates and lays out a new targeting phase display * for the specified client. */ public TargetingPhaseDisplay(ClientGUI clientgui, boolean offboard) { this.clientgui = clientgui; client = clientgui.getClient(); phase = offboard ? IGame.PHASE_OFFBOARD : IGame.PHASE_TARGETING; shiftheld = false; // fire attacks = new Vector(); setupStatusBar(Messages.getString("TargetingPhaseDisplay.waitingForTargetingPhase")); //$NON-NLS-1$ butFire = new JButton(Messages.getString("TargetingPhaseDisplay.Fire")); //$NON-NLS-1$ butFire.addActionListener(this); butFire.setActionCommand(FIRE_FIRE); butFire.setEnabled(false); butSkip = new JButton(Messages.getString("TargetingPhaseDisplay.Skip")); //$NON-NLS-1$ butSkip.addActionListener(this); butSkip.setActionCommand(FIRE_SKIP); butSkip.setEnabled(false); butTwist = new JButton(Messages.getString("TargetingPhaseDisplay.Twist")); //$NON-NLS-1$ butTwist.addActionListener(this); butTwist.setActionCommand(FIRE_TWIST); butTwist.setEnabled(false); butFlipArms = new JButton(Messages.getString("TargetingPhaseDisplay.FlipArms")); //$NON-NLS-1$ butFlipArms.addActionListener(this); butFlipArms.setActionCommand(FIRE_FLIP_ARMS); butFlipArms.setEnabled(false); butFireMode = new JButton(Messages.getString("TargetingPhaseDisplay.Mode")); //$NON-NLS-1$ butFireMode.addActionListener(this); butFireMode.setActionCommand(FIRE_MODE); butFireMode.setEnabled(false); butNextTarg = new JButton(Messages.getString("FiringDisplay.NextTarget")); //$NON-NLS-1$ butNextTarg.addActionListener(this); butNextTarg.addKeyListener(this); butNextTarg.setActionCommand(FIRE_NEXT_TARG); butNextTarg.setEnabled(false); butSearchlight = new JButton(Messages.getString("FiringDisplay.Searchlight")); //$NON-NLS-1$ butSearchlight.addActionListener(this); butSearchlight.addKeyListener(this); butSearchlight.setActionCommand(FIRE_SEARCHLIGHT); butSearchlight.setEnabled(false); butSpace = new JButton("."); //$NON-NLS-1$ butSpace.setEnabled(false); butDone = new JButton(Messages.getString("TargetingPhaseDisplay.Done")); //$NON-NLS-1$ butDone.addActionListener(this); butDone.setEnabled(false); butNext = new JButton(Messages.getString("TargetingPhaseDisplay.NextUnit")); //$NON-NLS-1$ butNext.addActionListener(this); butNext.setActionCommand(FIRE_NEXT); butNext.setEnabled(false); // layout button grid panButtons = new JPanel(); setupButtonPanel(); // layout screen GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); setLayout(gridbag); c.fill = GridBagConstraints.BOTH; c.weightx = 1.0; c.weighty = 1.0; c.insets = new Insets(1, 1, 1, 1);// c.gridwidth = GridBagConstraints.REMAINDER;// addBag(clientgui.bv, gridbag, c);// c.weightx = 1.0; c.weighty = 0;// c.gridwidth = 1;// addBag(client.cb.getComponent(), gridbag, c); c.gridwidth = GridBagConstraints.REMAINDER; c.weightx = 0.0; c.weighty = 0.0; addBag(panButtons, gridbag, c); c.weightx = 1.0; c.weighty = 0.0; c.gridwidth = GridBagConstraints.REMAINDER; addBag(panStatus, gridbag, c); } /** * Have the panel register itself as a listener wherever it's needed. * <p/> * According to http://www-106.ibm.com/developerworks/java/library/j-jtp0618.html * it is a major bad no-no to perform these registrations before the * constructor finishes, so this function has to be called after the * panel is created. Please note, this restriction only applies to * listeners for objects that aren't on the panel itself. */ public void initializeListeners() { client.game.addGameListener(this); clientgui.getBoardView().addBoardViewListener(this); clientgui.bv.addKeyListener(this); addKeyListener(this); // mech display. clientgui.mechD.wPan.weaponList.addListSelectionListener(this); clientgui.mechD.wPan.weaponList.addKeyListener(this); } private void addBag(JComponent comp, GridBagLayout gridbag, GridBagConstraints c) { gridbag.setConstraints(comp, c); add(comp); comp.addKeyListener(this); } private void setupButtonPanel() { panButtons.removeAll(); panButtons.setLayout(new GridLayout(0, 8)); panButtons.add(butNext); panButtons.add(butFire); panButtons.add(butSkip); panButtons.add(butNextTarg); panButtons.add(butFlipArms); panButtons.add(butTwist); panButtons.add(butFireMode); panButtons.add(butSearchlight); validate(); } /** * Selects an entity, by number, for movement. */ private void selectEntity(int en) { // clear any previously considered attacks if (en != cen) { clearAttacks(); refreshAll(); } if (client.game.getEntity(en) != null) { cen = en; clientgui.setSelectedEntityNum(en); // If the selected entity is not on the board, use the next one. // ASSUMPTION: there will always be *at least one* entity on map. if (null == ce().getPosition()) { // Walk through the list of entities for this player. for (int nextId = client.getNextEntityNum(en); nextId != en; nextId = client.getNextEntityNum(nextId)) { if (null != client.game.getEntity(nextId).getPosition()) { cen = nextId; break; } } // Check the player's next entity. // We were *supposed* to have found an on-board entity. if (null == ce().getPosition()) { System.err.println ("FiringDisplay: could not find an on-board entity: " + //$NON-NLS-1$ en); return; } } // End ce()-not-on-board target(null); clientgui.getBoardView().highlight(ce().getPosition()); clientgui.getBoardView().select(null); clientgui.getBoardView().cursor(null); refreshAll(); cacheVisibleTargets(); if (!clientgui.bv.isMovingUnits()) { clientgui.bv.centerOnHex(ce().getPosition()); } // Update the menu bar. clientgui.getMenuBar().setEntity(ce()); // 2003-12-29, nemchenk -- only twist if crew conscious setTwistEnabled(ce().canChangeSecondaryFacing() && ce().getCrew().isActive()); setFlipArmsEnabled(ce().canFlipArms()); updateSearchlight(); setFireModeEnabled(true); } else { System.err.println("FiringDisplay: tried to select non-existant entity: " + en); //$NON-NLS-1$ } } /** * Does turn start stuff */ private void beginMyTurn() { target = null; selectEntity(client.getFirstEntityNum()); if (!clientgui.bv.isMovingUnits()) { clientgui.setDisplayVisible(true); } // There's special processing for triggering AP Pods. if (client.game.getTurn() instanceof GameTurn.TriggerAPPodTurn && null != ce()) { disableButtons(); TriggerAPPodDialog dialog = new TriggerAPPodDialog (clientgui.getFrame(), ce()); dialog.setVisible(true); attacks.removeAllElements(); Enumeration actions = dialog.getActions(); while (actions.hasMoreElements()) { attacks.addElement(actions.nextElement()); } ready(); } else { setNextEnabled(true); butDone.setEnabled(true); clientgui.getBoardView().select(null); } } /** * Does end turn stuff. */ private void endMyTurn() { // end my turn, then. Entity next = client.game.getNextEntity(client.game.getTurnIndex()); if (phase == client.game.getPhase() && null != next && null != ce() && next.getOwnerId() != ce().getOwnerId()) { clientgui.setDisplayVisible(false); } cen = Entity.NONE; target(null); clientgui.getBoardView().select(null); clientgui.getBoardView().highlight(null); clientgui.getBoardView().cursor(null); clientgui.bv.clearMovementData(); disableButtons(); } /** * Disables all buttons in the interface */ private void disableButtons() { setFireEnabled(false); setSkipEnabled(false); setTwistEnabled(false); setNextEnabled(false);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -