1 /* 2 * Copyright (c) 2009-2010 jMonkeyEngine 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: 8 * 9 * * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * * Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 package com.jme3.input.lwjgl; 34 35 import com.jme3.input.MouseInput; 36 import com.jme3.input.RawInputListener; 37 import com.jme3.input.event.MouseButtonEvent; 38 import com.jme3.input.event.MouseMotionEvent; 39 import com.jme3.system.lwjgl.LwjglAbstractDisplay; 40 import com.jme3.system.lwjgl.LwjglTimer; 41 import java.util.logging.Level; 42 import java.util.logging.Logger; 43 import org.lwjgl.LWJGLException; 44 import org.lwjgl.Sys; 45 import org.lwjgl.input.Cursor; 46 import org.lwjgl.input.Mouse; 47 48 public class LwjglMouseInput implements MouseInput { 49 50 private static final Logger logger = Logger.getLogger(LwjglMouseInput.class.getName()); 51 52 private LwjglAbstractDisplay context; 53 54 private RawInputListener listener; 55 56 private boolean supportHardwareCursor = false; 57 private boolean cursorVisible = true; 58 59 private int curX, curY, curWheel; 60 LwjglMouseInput(LwjglAbstractDisplay context)61 public LwjglMouseInput(LwjglAbstractDisplay context){ 62 this.context = context; 63 } 64 initialize()65 public void initialize() { 66 if (!context.isRenderable()) 67 return; 68 69 try { 70 Mouse.create(); 71 logger.info("Mouse created."); 72 supportHardwareCursor = (Cursor.getCapabilities() & Cursor.CURSOR_ONE_BIT_TRANSPARENCY) != 0; 73 74 // Recall state that was set before initialization 75 Mouse.setGrabbed(!cursorVisible); 76 } catch (LWJGLException ex) { 77 logger.log(Level.SEVERE, "Error while creating mouse", ex); 78 } 79 } 80 isInitialized()81 public boolean isInitialized(){ 82 return Mouse.isCreated(); 83 } 84 getButtonCount()85 public int getButtonCount(){ 86 return Mouse.getButtonCount(); 87 } 88 update()89 public void update() { 90 if (!context.isRenderable()) 91 return; 92 93 while (Mouse.next()){ 94 int btn = Mouse.getEventButton(); 95 96 int wheelDelta = Mouse.getEventDWheel(); 97 int xDelta = Mouse.getEventDX(); 98 int yDelta = Mouse.getEventDY(); 99 int x = Mouse.getX(); 100 int y = Mouse.getY(); 101 102 curWheel += wheelDelta; 103 if (cursorVisible){ 104 xDelta = x - curX; 105 yDelta = y - curY; 106 curX = x; 107 curY = y; 108 }else{ 109 x = curX + xDelta; 110 y = curY + yDelta; 111 curX = x; 112 curY = y; 113 } 114 115 if (xDelta != 0 || yDelta != 0 || wheelDelta != 0){ 116 MouseMotionEvent evt = new MouseMotionEvent(x, y, xDelta, yDelta, curWheel, wheelDelta); 117 evt.setTime(Mouse.getEventNanoseconds()); 118 listener.onMouseMotionEvent(evt); 119 } 120 if (btn != -1){ 121 MouseButtonEvent evt = new MouseButtonEvent(btn, 122 Mouse.getEventButtonState(), x, y); 123 evt.setTime(Mouse.getEventNanoseconds()); 124 listener.onMouseButtonEvent(evt); 125 } 126 } 127 } 128 destroy()129 public void destroy() { 130 if (!context.isRenderable()) 131 return; 132 133 Mouse.destroy(); 134 logger.info("Mouse destroyed."); 135 } 136 setCursorVisible(boolean visible)137 public void setCursorVisible(boolean visible){ 138 cursorVisible = visible; 139 if (!context.isRenderable()) 140 return; 141 142 Mouse.setGrabbed(!visible); 143 } 144 setInputListener(RawInputListener listener)145 public void setInputListener(RawInputListener listener) { 146 this.listener = listener; 147 } 148 getInputTimeNanos()149 public long getInputTimeNanos() { 150 return Sys.getTime() * LwjglTimer.LWJGL_TIME_TO_NANOS; 151 } 152 153 } 154