📄 mouseparser.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.gui;
import netwar.utils.*;
import netwar.utils.vectorgraphics.*;
import netwar.game.GameObject;
import netwar.game.Player;
import netwar.game.Hex;
import netwar.game.Command;
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 MouseParser implements MouseListener, MouseMotionListener {
int selarray[] = new int[0];
float selTime = 0.0f;
static int zoomLevel = 0;
private static Point3D p1 = new Point3D();
private static Point3D p2 = new Point3D();
private int downX;
private int downY;
/** Get the first object in the selection array */
public GameObject getSelection() {
if(selarray.length == 0) return null;
return GameObject.getObjectWithID(selarray[0]);
}
/** 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 = -2;
/** 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 = HexViewer.getHexViewer().getTransform().getPoint3D(new Point2D(e.getX(),e.getY()));
GameObject u = approximateClick(vr, 10.0f);
if(e.isAltDown()) {
HexViewer.getHexViewer().getTransform().translate(e.getComponent().getBounds().width / 2 - e.getX(), e.getComponent().getBounds().height / 2 - e.getY());
HexViewer.getHexViewer().updateBackground();
}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) {
HexViewer.getHexViewer().getTransform().zoom(0.5f, (int)e.getComponent().getBounds().width / 2, (int)e.getComponent().getBounds().height / 2);
zoomLevel--;
HexViewer.getHexViewer().updateBackground();
}
}else if(modifiers == (e.BUTTON1_MASK | e.CTRL_MASK)) {
if(zoomLevel < maxZoom) {
HexViewer.getHexViewer().getTransform().zoom(2.0f, (int)e.getComponent().getBounds().width / 2, (int)e.getComponent().getBounds().height / 2);
zoomLevel++;
HexViewer.getHexViewer().updateBackground();
}
}else if((e.BUTTON3_MASK & modifiers) > 0) {
((netwar.gui.NetwarPanel)netwar.Netwar.netwar.getDataViewer()).noUnitSelected();
selarray = new int[0];
}else if((e.SHIFT_MASK & modifiers) > 0) {
if((u != null) && (u.getPlayer() == Player.getLocal())) {
boolean selected = false;
boolean changedFirst = false;
for(int i = 0; i < selarray.length; i++) {
if(selarray[i] == u.getID()) {
selected = true;
changedFirst = (i == 0);
selarray[i] = selarray[selarray.length - 1];
break;
}
}
int max = selarray.length + 1;
if(selected) max -= 2;
if(max == 0)
((netwar.gui.NetwarPanel)netwar.Netwar.netwar.getDataViewer()).noUnitSelected();
int newarray[] = new int[max];
if(!selected) newarray[--max] = u.getID();
for(int i = 0; i < max; i++)
newarray[i] = selarray[i];
selarray = newarray;
if((max == 0 && !selected) || changedFirst) {
String[][] s = GameObject.getObjectWithID(selarray[0]).getProperties();
((netwar.gui.NetwarPanel)netwar.Netwar.netwar.getDataViewer()).updateDisplay(s,selarray[0]);
}
if(!selected) selTime = 0.0f;
}
}else{
if(((e.getX() - downX) * (e.getX() - downX) + (e.getY() - downY) * (e.getY() - downY)) < 100) {
Hex hex = Hex.getXY(vr);
if(hex != null) {
if(selarray.length > 0) {
if(u == null)
Command.pendingCommands.enqueue(new Command(2, Player.getLocal().getNumber(), selarray, (int)vr.x, (int)vr.y));
else
Command.pendingCommands.enqueue(new Command(3, Player.getLocal().getNumber(), selarray, u.getID(), 0));
}else{
if((u != null) && (u.getPlayer() == Player.getLocal())) {
selarray = new int[1];
selarray[0] = u.getID();
selTime = 0.0f;
String[][] s = u.getProperties();
((netwar.gui.NetwarPanel)netwar.Netwar.netwar.getDataViewer()).updateDisplay(s,selarray[0]);
//for(int t = 0; t< s[0].length; t++)
// System.out.println(s[0][t] + "\t" + s[1][t]);
}
}
}
}else{
int i, c = 0;
Player pl = Player.getLocal();
int sel[] = new int[pl.numObject()];
p2 = HexViewer.getHexViewer().getTransform().getPoint3D(new Point2D(downX,downY));
if(sel.length == 0) {
((netwar.gui.NetwarPanel)netwar.Netwar.netwar.getDataViewer()).noUnitSelected();
selarray = sel;
return;
}
GameObject go = pl.getFirstObject();
GameObject go0 = null;
Point3D loc = go.locate();
if(((loc.x >= p2.x) ^ (loc.x >= vr.x)) && ((loc.y >= p2.y) ^ (loc.y >= vr.y)))
sel[c++] = (go0 = go).getID();
for(i = 1; i < sel.length; i++) {
go = pl.getNextObject();
loc = go.locate();
if(((loc.x >= p2.x) ^ (loc.x >= vr.x)) && ((loc.y >= p2.y) ^ (loc.y >= vr.y)))
sel[c++] = (go0 = go).getID();
}
if(c == 0) {
((netwar.gui.NetwarPanel)netwar.Netwar.netwar.getDataViewer()).noUnitSelected();
selarray = new int[0];
return;
}
selTime = 0.0f;
selarray = new int[c];
for(i = 0; i < c; i++)
selarray[i] = sel[i];
String[][] s = go0.getProperties();
((netwar.gui.NetwarPanel)netwar.Netwar.netwar.getDataViewer()).updateDisplay(s,selarray[0]);
}
}
}
}
/** Record location of mouse click, to know if a real drag was done.*/
public void mousePressed(MouseEvent e) {
downX = e.getX();
downY = e.getY();
}
/** 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) {
}
/** Causes the lower portion of the selection box to be drawn on each selected GameObject. */
public void drawLow() {
HexViewer hv = HexViewer.getHexViewer();
//for each unit in selected
boolean mustShrink = false;
int deadUnit = 0;
for(int i = 0; i < selarray.length; i++) {
GameObject selected = GameObject.getObjectWithID(selarray[i]);
if(selected == null || selected.isDead()) {
mustShrink = true;
deadUnit = i;
break;
}
p1.set(selected.locate());
p1.z += (0.5f - selTime) * selected.getHeight();
drawBox2(selected.getWidth(), hv);
}
if(mustShrink) {
selarray[deadUnit] = selarray[selarray.length - 1];
int newarray[] = new int[selarray.length - 1];
for(int i = 0; i < selarray.length - 1; i++)
newarray[i] = selarray[i];
selarray = newarray;
if(selarray.length == 0){
((netwar.gui.NetwarPanel)netwar.Netwar.netwar.getDataViewer()).noUnitSelected();
}else{
String[][] s = GameObject.getObjectWithID(selarray[0]).getProperties();
((netwar.gui.NetwarPanel)netwar.Netwar.netwar.getDataViewer()).updateDisplay(s,selarray[0]);
}
}
}
/** Causes the upper portion of the selection box to be drawn on each selected GameObject. */
public void drawHigh() {
HexViewer hv = HexViewer.getHexViewer();
//for each unit in selected
for(int i = 0; i < selarray.length; i++) {
GameObject selected = GameObject.getObjectWithID(selarray[i]);
if(selected == null || selected.isDead())
break;
p1.set(selected.locate());
p1.z += (0.5f + selTime) * selected.getHeight();
drawBox(selected.getWidth());
}
if(selTime < 0.5f) selTime += 0.1f;
}
private void drawBox(float w) {
HexViewer hv = HexViewer.getHexViewer();
float w2 = (0.5f - selTime) * (w - 1.0f) + 1.0f;
hv.setColor(Color.white);
p2.set(p1);
p1.x -= w;
p1.y -= w;
p2.x = p1.x;
p2.y = p1.y + w2;
hv.drawLine(p1, p2);
p2.x = p1.x + w2;
p2.y = p1.y;
hv.drawLine(p1, p2);
p1.y += 2 * w;
p2.y += 2 * w;
hv.drawLine(p1, p2);
p2.x = p1.x;
p2.y = p1.y - w2;
hv.drawLine(p1, p2);
p1.x += 2 * w;
p2.x += 2 * w;
hv.drawLine(p1, p2);
p2.x = p1.x - w2;
p2.y = p1.y;
hv.drawLine(p1, p2);
p1.y -= 2 * w;
p2.y -= 2 * w;
hv.drawLine(p1, p2);
p2.x = p1.x;
p2.y = p1.y + w2;
hv.drawLine(p1, p2);
}
private void drawBox2(float w, HexViewer hv) {
float w2 = (0.5f + selTime) * (w - 1.0f) - 1.0f;
hv.setColor(Color.white);
p2.set(p1);
p1.x -= w;
p2.x -= w;
p1.y -= w - w2;
p2.y += w - w2;
hv.drawLine(p1, p2);
p1.x += 2 * w;
p2.x += 2 * w;
hv.drawLine(p1, p2);
p1.x -= 2 * w - w2;
p2.x -= w2;
p1.y -= w2;
p2.y -= 2 * w - w2;
hv.drawLine(p1, p2);
p1.y += 2 * w;
p2.y += 2 * w;
hv.drawLine(p1, p2);
}
private GameObject approximateClick(Point3D click, float radius) {
//Find the nearest GameObject to the click point, within the radius specified.
Point3D xy = new Point3D(click);
Hex hex;
GameObject u = null;
hex = Hex.getXY(xy);
if(hex != null)
u = closer(click, hex.getOccupant(), u);
xy.x++;
hex = Hex.getHex((int)xy.x, (int)xy.y);
if(hex != null)
u = closer(click, hex.getOccupant(), u);
xy.y++;
hex = Hex.getHex((int)xy.x, (int)xy.y);
if(hex != null)
u = closer(click, hex.getOccupant(), u);
xy.x--;
hex = Hex.getHex((int)xy.x, (int)xy.y);
if(hex != null)
u = closer(click, hex.getOccupant(), u);
xy.y-=2;
hex = Hex.getHex((int)xy.x, (int)xy.y);
if(hex != null)
u = closer(click, hex.getOccupant(), u);
xy.x--;
hex = Hex.getHex((int)xy.x, (int)xy.y);
if(hex != null)
u = closer(click, hex.getOccupant(), u);
xy.y++;
hex = Hex.getHex((int)xy.x, (int)xy.y);
if(hex != null)
u = closer(click, hex.getOccupant(), u);
//At this point, if u is null and radius > 20, should check another round out.
//But I don't know yet why we would need to ever check with radius > 20.
if(u == null) return null;
click.doProduct(-1);
p1.set(u.locate());
p1.doSum(click);
click.doProduct(-1);
if(p1.getLengthSquared() > radius * radius)
return null;
return u;
}
private GameObject closer(Point3D pt, GameObject u1, GameObject u2) {
if(u1 == null)
return u2;
if(u2 == null)
return u1;
pt.doProduct(-1);
p1.set(u1.locate());
p1.doSum(pt);
p2.set(u2.locate());
p2.doSum(pt);
pt.doProduct(-1);
if(p1.getLengthSquared() > p2.getLengthSquared())
return u2;
return u1;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -