diff options
author | olamedia <[email protected]> | 2012-09-28 18:46:42 +0600 |
---|---|---|
committer | olamedia <[email protected]> | 2012-09-28 18:46:42 +0600 |
commit | b4192c7a88bad111bebbd42d391d6e729c8617d6 (patch) | |
tree | b05a586dbc60c5427fcc7f3fe01cf2fdcb970272 /src/ru/olamedia/input |
initial
Diffstat (limited to 'src/ru/olamedia/input')
-rw-r--r-- | src/ru/olamedia/input/AWTRobotUtil.java | 97 | ||||
-rw-r--r-- | src/ru/olamedia/input/KeyListener.java | 10 | ||||
-rw-r--r-- | src/ru/olamedia/input/Keyboard.java | 64 | ||||
-rw-r--r-- | src/ru/olamedia/input/MouseJail.java | 113 | ||||
-rw-r--r-- | src/ru/olamedia/input/MouseListener.java | 6 | ||||
-rw-r--r-- | src/ru/olamedia/input/package-info.java | 8 |
6 files changed, 298 insertions, 0 deletions
diff --git a/src/ru/olamedia/input/AWTRobotUtil.java b/src/ru/olamedia/input/AWTRobotUtil.java new file mode 100644 index 0000000..9c6778c --- /dev/null +++ b/src/ru/olamedia/input/AWTRobotUtil.java @@ -0,0 +1,97 @@ +package ru.olamedia.input; + +/** + * Copyright 2010 JogAmp Community. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied, of JogAmp Community. + */ + +import java.lang.reflect.InvocationTargetException; +import java.awt.AWTException; +import java.awt.Component; +import java.awt.Point; +import java.awt.Robot; + +public class AWTRobotUtil { + + private static final int ROBOT_DELAY = 100; // ms + + public static Point getCenterLocation(Object obj, boolean onTitleBarIfWindow) throws InterruptedException, + InvocationTargetException { + Component comp = null; + com.jogamp.newt.Window win = null; + + if (obj instanceof com.jogamp.newt.Window) { + win = (com.jogamp.newt.Window) obj; + } else if (obj instanceof Component) { + comp = (Component) obj; + } else { + throw new RuntimeException("Neither AWT nor NEWT: " + obj); + } + + int x0, y0; + if (null != comp) { + java.awt.Point p0 = comp.getLocationOnScreen(); + java.awt.Rectangle r0 = comp.getBounds(); + if (onTitleBarIfWindow && comp instanceof java.awt.Window) { + java.awt.Window window = (java.awt.Window) comp; + java.awt.Insets insets = window.getInsets(); + y0 = (int) (p0.getY() + insets.top / 2.0 + .5); + } else { + y0 = (int) (p0.getY() + r0.getHeight() / 2.0 + .5); + } + x0 = (int) (p0.getX() + r0.getWidth() / 2.0 + .5); + } else { + javax.media.nativewindow.util.Point p0 = win.getLocationOnScreen(null); + if (onTitleBarIfWindow) { + javax.media.nativewindow.util.InsetsImmutable insets = win.getInsets(); + p0.translate(win.getWidth() / 2, insets.getTopHeight() / 2); + } else { + javax.media.nativewindow.util.InsetsImmutable insets = win.getInsets(); + p0.translate(win.getWidth() / 2, (win.getHeight() - insets.getTopHeight()) / 2); + } + x0 = p0.getX(); + y0 = p0.getY(); + } + + return new Point(x0, y0); + } + + /** + * centerMouse + */ + public static Point centerMouse(Robot robot, Object obj, boolean onTitleBarIfWindow) throws AWTException, + InterruptedException, InvocationTargetException { + + Point p0 = getCenterLocation(obj, onTitleBarIfWindow); + // System.err.println("centerMouse: robot pos: " + p0 + + // ", onTitleBarIfWindow: " + onTitleBarIfWindow); + + robot.mouseMove((int) p0.getX(), (int) p0.getY()); + // robot.delay(ROBOT_DELAY); + return p0; + } + +} diff --git a/src/ru/olamedia/input/KeyListener.java b/src/ru/olamedia/input/KeyListener.java new file mode 100644 index 0000000..8fb5d49 --- /dev/null +++ b/src/ru/olamedia/input/KeyListener.java @@ -0,0 +1,10 @@ +package ru.olamedia.input; + +import com.jogamp.newt.event.KeyEvent; + +public interface KeyListener { + public void onKeyPressed(String name, KeyEvent e); + + public void onKeyReleased(String name, KeyEvent e); + +} diff --git a/src/ru/olamedia/input/Keyboard.java b/src/ru/olamedia/input/Keyboard.java new file mode 100644 index 0000000..6a9ecbd --- /dev/null +++ b/src/ru/olamedia/input/Keyboard.java @@ -0,0 +1,64 @@ +package ru.olamedia.input; + +import java.awt.event.KeyEvent; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.collections.BidiMap; +import org.apache.commons.collections.bidimap.DualHashBidiMap; + +public class Keyboard implements com.jogamp.newt.event.KeyListener { + public static Keyboard instance = new Keyboard(); + private static boolean[] downState = new boolean[256]; + private static BidiMap names = new DualHashBidiMap(); + + public static void setName(String name, int keyCode) { + names.put(name, keyCode); + } + + public static boolean isKeyDown(int keyCode) { + return downState[keyCode]; + } + + public static boolean isKeyDown(String name) { + if (names.containsKey(name)) { + return downState[((Integer) names.get(name)).intValue()]; + } + return false; + } + + private static List<ru.olamedia.input.KeyListener> listeners = new ArrayList<ru.olamedia.input.KeyListener>(); + + public static void attach(ru.olamedia.input.KeyListener l) { + listeners.add(l); + } + + @Override + public void keyPressed(com.jogamp.newt.event.KeyEvent e) { + downState[e.getKeyCode()] = true; + if (names.containsValue(e.getKeyCode())) { + String name = (String) names.getKey(e.getKeyCode()); + for (ru.olamedia.input.KeyListener l : listeners) { + l.onKeyPressed(name, e); + } + } + } + + @Override + public void keyReleased(com.jogamp.newt.event.KeyEvent e) { + downState[e.getKeyCode()] = false; + if (names.containsValue(e.getKeyCode())) { + String name = (String) names.getKey(e.getKeyCode()); + for (ru.olamedia.input.KeyListener l : listeners) { + l.onKeyReleased(name, e); + } + } + } + + @Override + public void keyTyped(com.jogamp.newt.event.KeyEvent arg0) { + // TODO Auto-generated method stub + + } +} diff --git a/src/ru/olamedia/input/MouseJail.java b/src/ru/olamedia/input/MouseJail.java new file mode 100644 index 0000000..bd12b0a --- /dev/null +++ b/src/ru/olamedia/input/MouseJail.java @@ -0,0 +1,113 @@ +package ru.olamedia.input; + +import java.awt.AWTException; +import java.awt.Point; +import java.awt.Robot; +import java.lang.reflect.InvocationTargetException; +import java.util.ArrayList; +import java.util.List; + +import com.jogamp.newt.event.MouseAdapter; +import com.jogamp.newt.event.MouseEvent; + +import ru.olamedia.game.GameFrame; + +public class MouseJail extends MouseAdapter { + public static MouseJail instance = new MouseJail(); + + public MouseJail() { + } + + private static boolean isActive = false; + + /** + * @return the isActive + */ + public static boolean isActive() { + return isActive; + } + + /** + * @param isActive + * the isActive to set + */ + public static void setActive(boolean isActive) { + System.out.println("Mouse jail " + (isActive ? "active" : "not active")); + MouseJail.isActive = isActive; + GameFrame.confinePointer(isActive); + GameFrame.setPointerVisible(!isActive); + } + + @Override + public void mouseClicked(MouseEvent e) { + if (e.isAltDown()) { + setActive(false); + } else { + setActive(true); + } + for (ru.olamedia.input.MouseListener l : listeners) { + l.onMouseClick(); + } + } + + @Override + public void mouseEntered(MouseEvent e) { + System.out.println("Entered"); + + } + + @Override + public void mouseExited(MouseEvent e) { + System.out.println("Exited"); + isActive = false; + if (isActive) { + //moveToCenter(); + } + } + + @Override + public void mousePressed(MouseEvent e) { + + } + + @Override + public void mouseReleased(MouseEvent e) { + + } + + @Override + public void mouseDragged(MouseEvent e) { + onMove(e); + } + + private float sensitivity = 2f; + + private void onMove(MouseEvent e) { + if (isActive) { + int cx = GameFrame.getWidth() / 2; + int cy = GameFrame.getHeight() / 2; + float dx = e.getX() - cx; + float dy = e.getY() - cy; + dx *= sensitivity / 10; + dy *= sensitivity / 10; + // System.out.println("Mouse moved " + " dx:" + dx + " dy:" + dy + // + " x:" + e.getX() + " y:" + e.getY()); + for (ru.olamedia.input.MouseListener l : listeners) { + l.onMouseMove(dx, dy); + } + GameFrame.getWindow().warpPointer(cx, cy); + } + } + + @Override + public void mouseMoved(MouseEvent e) { + onMove(e); + } + + private static List<ru.olamedia.input.MouseListener> listeners = new ArrayList<ru.olamedia.input.MouseListener>(); + + public static void attach(ru.olamedia.input.MouseListener l) { + listeners.add(l); + } + +} diff --git a/src/ru/olamedia/input/MouseListener.java b/src/ru/olamedia/input/MouseListener.java new file mode 100644 index 0000000..3d93d1d --- /dev/null +++ b/src/ru/olamedia/input/MouseListener.java @@ -0,0 +1,6 @@ +package ru.olamedia.input; + +public interface MouseListener { + public void onMouseMove(float dx, float dy); + public void onMouseClick(); +} diff --git a/src/ru/olamedia/input/package-info.java b/src/ru/olamedia/input/package-info.java new file mode 100644 index 0000000..ad3f418 --- /dev/null +++ b/src/ru/olamedia/input/package-info.java @@ -0,0 +1,8 @@ +/** + * + */ +/** + * @author olamedia + * + */ +package ru.olamedia.input;
\ No newline at end of file |