⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mouseparserm.java

📁 用java开发的一个实施策略游戏源码 值得学习一下
💻 JAVA
字号:
/*
	Netwar
	Copyright (C) 2002  Daniel Grund, Kyle Kakligian, Jason Komutrattananon, & Brian Hibler.
 
	This file is part of Netwar.
 
	Netwar 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.
 
	Netwar 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 Netwar; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package netwar.mapper;
import netwar.utils.*;
import netwar.utils.vectorgraphics.*;
import netwar.game.*;
import java.awt.event.*;
import java.awt.Color;

/** This is the MouseListener and MouseMotionListener for HexViewer.
 * This enables the point and click interface for the game.
 * The game currently recognizes the following click inputs:
 * <BR> Alt + click = Center the HexViewer on the location of the click.
 * <BR> Ctrl + left click = Zoom in, preserving the center of the view.
 * <BR> Ctrl + right click = Zoom out, preserving the center of the view.
 * <BR> Shift + click = Add or remove a selectable GameObject to/from the set of selected objects.
 * <BR> Right click = Unselect all GameObjects.
 * <BR> Left Click (no objects selected) = Select a selectable GameObject.
 * <BR> Left Click (on a GameObject, with at least one selected) = Set this GameObject as the goal of the selected GameObjects.
 * <BR> Left Click (on an unoccupied Hex, with at least one object selected) = Set this Hex as the gola of the selected GameObjects.
 * <p> A selectable object is one which is owned by the local player, and is not dead.
 * Each GameObject may define its own behaviors when a goal is set. Typically, a Unit (mobile GameObject)
 * will move to a goal hex, or pursue/attack a goal GameObject.
 * @author Group N2 - Project Netwar
 * @author Daniel Grund
 */
public class MouseParserM implements MouseListener, MouseMotionListener {
	private float selTime = 0.0f;
	private static int zoomLevel = 0;
	private static Point3D p1 = new Point3D();
	private static Point3D p2 = new Point3D();
	/** The minimum zoom level.
	 * If the zoom level is at this level, everything will be shrunk to the minimum size.
	 * The default zoom level is 0.
	 */
	public static final int minZoom = -3;
	/** The maximum zoom level.
	 * If the zoom level is at this level, everything will be expanded to the maximum size.
	 * The default zoom level is 0.
	 */
	public static final int maxZoom = 2;
	/** The factor for zooming.
	 * When zooming in, each zoom level multiplies the sizes by this much.
	 * When zooming out, each zoom level divides the sizes by this much.
	 */
	public static final float zoomFactor = 2.0f;
	/** Accessor for Zoom Level.
	 * @return The current zoom level.
	 */
	public static int getZoomLevel() {return zoomLevel;}
	/** Required by MouseListener, but not currently used.
	 * Note that mouseClicked is never triggered if the mouse moves even a pixel between
	 * being pressed and being released. That is why mouseReleased is used instead.
	 */
	public void mouseClicked(MouseEvent e) {
	}
	
	/** Catches and interprets a mouse click.
	 * See the class discription for how this is interpretted.
	 * @param e The MouseEvent containing all the relevant click data.
	 */
	public void mouseReleased(MouseEvent e) {
		Point3D vr = HexViewerM.getHexViewer().getTransform().getPoint3D(new Point2D(e.getX(),e.getY()));
		if(e.isAltDown()) {
			HexViewerM.getHexViewer().getTransform().translate(e.getComponent().getBounds().width / 2 - e.getX(), e.getComponent().getBounds().height / 2 - e.getY());
			HexViewerM.getHexViewer().repaint();
		}else{
			int modifiers = e.getModifiers();
			//Might want to implement a control config
			//This could allow the user to click on a test pad
			//and determine the correct masks for his/her clicks.
			//That way, the interface adapts to combinations of
			//use/don't use shift/alt/ctrl as well as right/left/middle clicks
			if(modifiers == (e.BUTTON3_MASK | e.CTRL_MASK)) {
				if(zoomLevel > minZoom ) {
					HexViewerM.getHexViewer().getTransform().zoom(1/zoomFactor, (int)e.getComponent().getBounds().width / 2, (int)e.getComponent().getBounds().height / 2);
					zoomLevel--;
					HexViewerM.getHexViewer().repaint();
				}
			}else if(modifiers == (e.BUTTON1_MASK | e.CTRL_MASK)) {
				if(zoomLevel < maxZoom ) {
					HexViewerM.getHexViewer().getTransform().zoom(zoomFactor, (int)e.getComponent().getBounds().width / 2, (int)e.getComponent().getBounds().height / 2);
					zoomLevel++;
					HexViewerM.getHexViewer().repaint();
				}
			}
		}
	}
	/** Required by MouseListener, but not currently used.*/
	public void mousePressed(MouseEvent e) {
	}
	/** Required by MouseListener, but not currently used.*/
	public void mouseExited(MouseEvent e) {
	}
	/** Required by MouseListener, but not currently used.*/
	public void mouseEntered(MouseEvent e) {
	}
	/** Required by MouseMotionListener, but not currently used.*/
	public void mouseMoved(MouseEvent e) {
	}
	/** Required by MouseMotionListener, but not currently used.
	 * This is planned to be used for click-and-drag multiple Unit selection.
	 * However, that feature is not yet implemented.
	 */
        public void mouseDragged(MouseEvent e) {
            Point3D vr = HexViewerM.getHexViewer().getTransform().getPoint3D(new Point2D(e.getX(),e.getY()));
            
            int modifiers = e.getModifiers();
            HexM hex = HexM.getXY(vr);
            
            if(modifiers == e.BUTTON1_MASK) {
                if(hex != null && TileComponent.curr != null && TileComponent.curr.hexType != hex.getHexType()) {
                    hex.setType(TileComponent.curr.hexType);
                    hex.clear();
                    HexViewerM.getHexViewer().repaint();
                }
            } else if(modifiers == e.BUTTON3_MASK) {
                if(hex != null && ObjectComponent.curr != null)
                    if(hex.getOccupant() == null || ObjectComponent.curr.gameObject.getClass() != hex.getOccupant().getClass()) {
                      //GameObject toAdd = ObjectComponent.curr.gameObject;
                      hex.addGameObject(ObjectComponent.newSelectedObject(), (int)vr.x, (int)vr.y); // vr is changed into hex-coords in HexM.getXY(vr)

                      HexViewerM.getHexViewer().repaint();
                    }
            }
        }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -