Input Control Class: InputManager

zhaozj2021-02-16  99

Package com.Input;

Import java.awt. *; import java.awt.event. *; import java.util.list; import java.util.Arraylist; import javax.swing.swingutilities;

/ ** The InputManager manages input of key and mouse events. Events are mapped to GameActions. * / Public class InputManager implements KeyListener, MouseListener, MouseMotionListener, MouseWheelListener {/ ** An invisible cursor. * / Public static final Cursor INVISIBLE_CURSOR = Toolkit. getDefaultToolkit (). CreateCustom Cursor (Toolkit.getDefaultToolkit (). getImage (""), New Point (0, 0), "Invisible");

// mouse codes public static final int MOUSE_MOVE_LEFT = 0; public static final int MOUSE_MOVE_RIGHT = 1; public static final int MOUSE_MOVE_UP = 2; public static final int MOUSE_MOVE_DOWN = 3; public static final int MOUSE_WHEEL_UP = 4; public static final int MOUSE_WHEEL_DOWN = 5; public static final int mouse_button_1 = 6; public static final int mouse_button_2 = 7; public static final int mouse_button_3 = 8;

Private static final int num_mouse_codes = 9;

// Key Codes Are Defined in java.awt.keyevent. // Most of the Codes (Except for Some Rare Ons Like // "Alt Graph") Are Less Than 600. Private Static Final Int Num_Key_codes = 600;

Private gameAction [] KeyAction = new gameAction [num_key_codes]; private gameAction [] mouseActions = new gameAction [num_mouse_codes];

PRIVATE POINT MOUSELOCATION; Private Component Comp; Private Robot Robot; Private Boolean IsRecentering;

. / ** Creates a new InputManager that listens to input from the specified component * / public InputManager (Component comp) {this.comp = comp; mouseLocation = new Point (); centerLocation = new Point (); // register key and Mouse Listener Comp.AddKeylistener (this); Comp.AddmouseListener (this); comp.addmousemotionListener (this); Comp.addmousewheellistener (this);

// Allow Input of the Tab Key and Other Keys Normal // Used for Focus Traversal Comp.setfocusTraversalKesenabled (false);

/ ** Sets the cursor on this infutmanager's infut company. * / Public void setcursor (cursor cursor) {comp.setcursor (cursor);

/ ** Sets whether realtive mouse mode is on or not. For relative mouse mode, the mouse is "locked" in the center of the screen, and only the changed in mouse movement is measured. In normal mode, the mouse is free to Move About The screen. * / public void setReLativeMouseMode (Boolean Mode) {if (Mode == isRelanchiMouseMode ()) {Return;}

IF (mode) {try {robot = new robot (); recectorMouse ();} catch (awtexception ex) {// couldn't create robot! robot = null;}} else {robot = null;}}

/ ** RETURns WHETER OR NOT Relative Mouse Mode Is on. * / Public Boolean IsRelativeMouseMode () {Return (Robot! = Null);}

/ ** Maps a GameAction to a specific key. The key codes are defined in java.awt.KeyEvent. If the key already has a GameAction mapped to it, the new GameAction overwrites it. * / Public void mapToKey (GameAction gameAction, int keyCode) {keyActions [keyCode] = gameAction;}.. / ** Maps a gameAction to a specific mouse action The mouse codes are defined herer in InputManager (MOUSE_MOVE_LEFT, MOUSE_BUTTON_1, etc) If the mouse action already has a gameAction mapped to it , The new gameAction overwrites it. * / public void maptomouse (gameAction game) {mouseAction;} = gameAction;

/ ** Clears all mapped keys and mouse actions to this gameaction. * / Public void clearmap (INT i = 0; i

For (int i = 0; i

GameAction.reset ();

/ ** Gets a list of names of the key the a list of names of the keyness. Each entry in the list is a string. * / Public list getmaps (arrise gamecode) {arraylist list = new arraylist ();

For (int i = 0; i

For (int i = 0; i

For (int i = 0; i

/ ** Gets the name of a key code. * / Public static string getKeyName (int keycode) {return keyevent.getKeyText (keycode);

. / ** Gets the name of a mouse code * / public static String getMouseName (int mouseCode) {switch (mouseCode) {case MOUSE_MOVE_LEFT: return "Mouse Left"; case MOUSE_MOVE_RIGHT: return "Mouse Right"; case MOUSE_MOVE_UP: return " Mouse Up "; case MOUSE_MOVE_DOWN: return" Mouse Down "; case MOUSE_WHEEL_UP: return" Mouse Wheel Up "; case MOUSE_WHEEL_DOWN: return" Mouse Wheel Down "; case MOUSE_BUTTON_1: return" Mouse Button 1 "; case MOUSE_BUTTON_2: return" Mouse Button 2 "; CASE MOUSE_BUTTON_3: Return" Mouse Button 3 "; Default: Return" Unknown Mouse Code " mousecode;}

/ ** Gets the x position of the mouse. * / Public int getmousex () {return mouselocation.x;}

/ ** Gets the y position of the mouse. * / Public int getMousey () {return mouselocation.y;} / ** buy the robot class to try to postion the mouse in the center of the screen.

Note That use of the Robot class may not be available on all platforms * / private synchronized void recenterMouse () {if (robot = null && comp.isShowing ()!) {centerLocation.x = comp.getWidth () / 2;. centerLocation. Y = comp.getHeight () / 2; swingutilities.convertpointtoscreen (centerlocation, comp); isrecentering = true; robot.mousemove (centerlocation.x, centerlocation.y);}}

Private gameaction getKeyAction (KeyEvent E) {int keycode = E.getKeycode (); if (keycode

. / ** Gets the mouse code for the button specified in this MouseEvent * / public static int getMouseButtonCode (MouseEvent e) {switch (e.getButton ()) {case MouseEvent.BUTTON1: return MOUSE_BUTTON_1; case MouseEvent.BUTTON2: return MOUSE_BUTTON_2 Case mouseevent.button3: return mouse_button_3; default: return -1;}}

Private gameAction getMouseButtonAction (MouseEvent E) {int mousecode = getMouseButtonCode (e); if (mousecode! = -1) {Return MouseActions [mousecode];} else {return null;}}

// from the KeyListener interface public void keyPressed (KeyEvent e) {GameAction gameAction = getKeyAction (e); if (gameAction = null!) {GameAction.press ();} // make sure the key is not processed for anything else E.Consume ();} // from the keylistener interface public void keyreleased (keyevent e) {gameAction gameAdition = getKeyAction (e); if (gameaction! = null) {GameAction.Release ();} // make Sure the Key IsoN't processed for anything else e.consume ();

// from the keyphants interface public void keytyped (keyevent e) {// make surething else E.CONSUME ();

// from the mouselistener interface public void mousepressed (mouseEvent e) {gameAction gameAction = getMouseButtonAction (e); if (gameAction! = null) {gameAction.press ();}}

// from the mouselistener interface public void mousereleased (mouseevent e) {gameAction gameAction = getMouseButtonAction (e); if (gameAction! = null) {gameAction.release ();}}

// from the mouselistener interface public void mouseclicked (mouseevent e) {// do nothing}

// from the mouselistener interface public void mouseentered (mouseevent e) {mousemoved (e);}

// from the mouselistener interface public void mouseexited (mouseevent e) {mousemoved (e);}

// from the mousemotionlistener interface public void mousedragged (mouseevent e) {mousemoved (e);}

// from the MouseMotionListener interface public synchronized void mouseMoved (MouseEvent e) {// this event is from re-centering the mouse - ignore it if (isRecentering && centerLocation.x == e.getX () && centerLocation.y == e .gety ()) {isrecentering = false;} else {int dx = E.GETX () - mouseLocation.x; int dy = E.GETY () - mouseelocation.y; mouseHelper (mouse_move_left, mouse_move_right, dx); mousehelper Mouse_move_up, mouse_move_down, dy; if (isRelanchiveMouseMode ()) {recnentermouse ();}}

MouseLocation.x = E.GETX (); mouseLocation.y = E.Gety ();

}

// from the mousewheelliistener interface public void mousewheelmoved (Mousewheelevent E) {mousehelper (mouse_wheel_up, mouse_wheel_down, e.Getwheelrotation ());}

private void mouseHelper (int codeNeg, int codePos, int amount) {GameAction gameAction; if (amount <0) {gameAction = mouseActions [codeNeg];} else {gameAction = mouseActions [codePos];} if (! gameAction = null) { GameAction.Press (Math.Abs ​​(Amount)); GameAction.Release ();}}

}

转载请注明原文地址:https://www.9cbs.com/read-10391.html

New Post(0)