org.lwjgl.input.Cursor Java Examples

The following examples show how to use org.lwjgl.input.Cursor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Utils.java    From Hyperium with GNU Lesser General Public License v3.0 7 votes vote down vote up
public void setCursor(ResourceLocation cursor) {
    try {
        BufferedImage image = ImageIO.read(Minecraft.getMinecraft().getResourceManager().getResource(cursor).getInputStream());
        int w = image.getWidth();
        int h = image.getHeight();
        int[] pixels = new int[(w * h)];
        image.getRGB(0, 0, w, h, pixels, 0, w);
        ByteBuffer buffer = BufferUtils.createByteBuffer(w * h * 4);

        for (int y = 0; y < h; y++) {
            for (int x = 0; x < w; x++) {
                int pixel = pixels[(h - 1 - y) * w + x];
                buffer.put((byte) (pixel & 0xFF));
                buffer.put((byte) (pixel >> 8 & 0xFF));
                buffer.put((byte) (pixel >> 16 & 0xFF));
                buffer.put((byte) (pixel >> 24 & 0xFF));
            }
        }

        buffer.flip();
        Mouse.setNativeCursor(new Cursor(w, h, 0, h - 1, 1, buffer.asIntBuffer(), null));
    } catch (Exception ignored) {
    }
}
 
Example #2
Source File: CursorLoader.java    From slick2d-maven with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Get a cursor based on a set of image data
 * 
 * @param imageData The data from which the cursor can read it's contents
 * @param x The x-coordinate of the cursor hotspot (left -> right)
 * @param y The y-coordinate of the cursor hotspot (bottom -> top)
 * @return The create cursor
 * @throws IOException Indicates a failure to load the image
 * @throws LWJGLException Indicates a failure to create the hardware cursor
 */
public Cursor getCursor(ImageData imageData,int x,int y) throws IOException, LWJGLException {
	ByteBuffer buf = imageData.getImageBufferData();
	for (int i=0;i<buf.limit();i+=4) {
		byte red = buf.get(i);
		byte green = buf.get(i+1);
		byte blue = buf.get(i+2);
		byte alpha = buf.get(i+3);
		
		buf.put(i+2, red);
		buf.put(i+1, green);
		buf.put(i, blue);
		buf.put(i+3, alpha);
	}
	
	try {
		int yspot = imageData.getHeight() - y - 1;
		if (yspot < 0) {
			yspot = 0;
		}
		return new Cursor(imageData.getTexWidth(), imageData.getTexHeight(), x, yspot, 1, buf.asIntBuffer(), null);
	} catch (Throwable e) {
		Log.info("Chances are you cursor is too small for this platform");
		throw new LWJGLException(e);
	}
}
 
Example #3
Source File: CursorLoader.java    From slick2d-maven with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Get a cursor based on a set of image data
 * 
 * @param buf The image data (stored in RGBA) to load the cursor from
 * @param x The x-coordinate of the cursor hotspot (left -> right)
 * @param y The y-coordinate of the cursor hotspot (bottom -> top)
 * @param width The width of the image data provided
 * @param height The height of the image data provided
 * @return The create cursor
 * @throws IOException Indicates a failure to load the image
 * @throws LWJGLException Indicates a failure to create the hardware cursor
 */
public Cursor getCursor(ByteBuffer buf,int x,int y,int width,int height) throws IOException, LWJGLException {
	for (int i=0;i<buf.limit();i+=4) {
		byte red = buf.get(i);
		byte green = buf.get(i+1);
		byte blue = buf.get(i+2);
		byte alpha = buf.get(i+3);
		
		buf.put(i+2, red);
		buf.put(i+1, green);
		buf.put(i, blue);
		buf.put(i+3, alpha);
	}
	
	try {
		int yspot = height - y - 1;
		if (yspot < 0) {
			yspot = 0;
		}
		return new Cursor(width,height, x, yspot, 1, buf.asIntBuffer(), null);
	} catch (Throwable e) {
		Log.info("Chances are you cursor is too small for this platform");
		throw new LWJGLException(e);
	}
}
 
Example #4
Source File: AppGameContainer.java    From slick2d-maven with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * @see org.newdawn.slick.GameContainer#setMouseCursor(org.newdawn.slick.Image, int, int)
 */
public void setMouseCursor(Image image, int hotSpotX, int hotSpotY) throws SlickException {
	try {
		Image temp = new Image(get2Fold(image.getWidth()), get2Fold(image.getHeight()));
		Graphics g = temp.getGraphics();
		
		ByteBuffer buffer = BufferUtils.createByteBuffer(temp.getWidth() * temp.getHeight() * 4);
		g.drawImage(image.getFlippedCopy(false, true), 0, 0);
		g.flush();
		g.getArea(0,0,temp.getWidth(),temp.getHeight(),buffer);
		
		Cursor cursor = CursorLoader.get().getCursor(buffer, hotSpotX, hotSpotY,temp.getWidth(),temp.getHeight());
		Mouse.setNativeCursor(cursor);
	} catch (Throwable e) {
		Log.error("Failed to load and apply cursor.", e);
		throw new SlickException("Failed to set mouse cursor", e);
	}
}
 
Example #5
Source File: LwjglMouseInput.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void initialize() {
    if (!context.isRenderable())
        return;

    try {
        Mouse.create();
        logger.fine("Mouse created.");
        supportHardwareCursor = (Cursor.getCapabilities() & Cursor.CURSOR_ONE_BIT_TRANSPARENCY) != 0;

        // Recall state that was set before initialization
        Mouse.setGrabbed(!cursorVisible);
    } catch (LWJGLException ex) {
        logger.log(Level.SEVERE, "Error while creating mouse", ex);
    }

    if (listener != null) {
        sendFirstMouseEvent();
    }
}
 
Example #6
Source File: AppletGameContainer.java    From slick2d-maven with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
  * {@inheritDoc}
  */
 public void setMouseCursor(Image image, int hotSpotX, int hotSpotY) throws SlickException {
     try {
        Image temp = new Image(get2Fold(image.getWidth()), get2Fold(image.getHeight()));
        Graphics g = temp.getGraphics();
        
        ByteBuffer buffer = BufferUtils.createByteBuffer(temp.getWidth() * temp.getHeight() * 4);
        g.drawImage(image.getFlippedCopy(false, true), 0, 0);
        g.flush();
        g.getArea(0,0,temp.getWidth(),temp.getHeight(),buffer);
        
        Cursor cursor = CursorLoader.get().getCursor(buffer, hotSpotX, hotSpotY,temp.getWidth(),temp.getHeight());
        Mouse.setNativeCursor(cursor);
     } catch (Throwable e) {
        Log.error("Failed to load and apply cursor.", e);
throw new SlickException("Failed to set mouse cursor", e);
     }
  }
 
Example #7
Source File: SeventhGame.java    From seventh with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void create() {        
    ClientMain.logSystemSpecs(console);
    ClientMain.logVideoSpecs(console);
    
    Art.load();
    Sounds.init(config);
            

    this.uiManager = new UserInterfaceManager();
    seventh.client.gfx.Cursor cursor = this.uiManager.getCursor();
    float sensitivity = config.getMouseSensitivity();
    if(sensitivity > 0) {
        cursor.setMouseSensitivity(sensitivity);
    }
    
    Gdx.input.setInputProcessor(this.inputs);
    
    initControllers();        
    videoReload();        
    
    this.menuScreen = new MenuScreen(this);
    goToMenuScreen();
    
    launchStartupScript(this.startupConfig);
}
 
Example #8
Source File: AppletGameContainer.java    From slick2d-maven with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
* @see org.newdawn.slick.GameContainer#setMouseCursor(java.lang.String,
*      int, int)
*/
   public void setMouseCursor(String ref, int hotSpotX, int hotSpotY) throws SlickException {
      try {
         Cursor cursor = CursorLoader.get().getCursor(ref, hotSpotX, hotSpotY);
         Mouse.setNativeCursor(cursor);
      } catch (Throwable e) {
         Log.error("Failed to load and apply cursor.", e);
throw new SlickException("Failed to set mouse cursor", e);
      }
   }
 
Example #9
Source File: LwjglMouseInput.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void initialize() {
    if (!context.isRenderable())
        return;

    try {
        Mouse.create();
        logger.info("Mouse created.");
        supportHardwareCursor = (Cursor.getCapabilities() & Cursor.CURSOR_ONE_BIT_TRANSPARENCY) != 0;
        
        // Recall state that was set before initialization
        Mouse.setGrabbed(!cursorVisible);
    } catch (LWJGLException ex) {
        logger.log(Level.SEVERE, "Error while creating mouse", ex);
    }
}
 
Example #10
Source File: CursorLoader.java    From slick2d-maven with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Get a cursor based on a image reference on the classpath
 * 
 * @param ref The reference to the image to be loaded
 * @param x The x-coordinate of the cursor hotspot (left -> right)
 * @param y The y-coordinate of the cursor hotspot (bottom -> top)
 * @return The create cursor
 * @throws IOException Indicates a failure to load the image
 * @throws LWJGLException Indicates a failure to create the hardware cursor
 */
public Cursor getCursor(String ref,int x,int y) throws IOException, LWJGLException {
	LoadableImageData imageData = null;
	
	imageData = ImageDataFactory.getImageDataFor(ref);
	imageData.configureEdging(false);
	
	ByteBuffer buf = imageData.loadImage(ResourceLoader.getResourceAsStream(ref), true, true, null);
	for (int i=0;i<buf.limit();i+=4) {
		byte red = buf.get(i);
		byte green = buf.get(i+1);
		byte blue = buf.get(i+2);
		byte alpha = buf.get(i+3);
		
		buf.put(i+2, red);
		buf.put(i+1, green);
		buf.put(i, blue);
		buf.put(i+3, alpha);
	}
	
	try {
		int yspot = imageData.getHeight() - y - 1;
		if (yspot < 0) {
			yspot = 0;
		}
		
		return new Cursor(imageData.getTexWidth(), imageData.getTexHeight(), x, yspot, 1, buf.asIntBuffer(), null);
	} catch (Throwable e) {
		Log.info("Chances are you cursor is too small for this platform");
		throw new LWJGLException(e);
	}
}
 
Example #11
Source File: AppGameContainer.java    From slick2d-maven with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * @see org.newdawn.slick.GameContainer#setMouseCursor(org.lwjgl.input.Cursor, int, int)
 */
public void setMouseCursor(Cursor cursor, int hotSpotX, int hotSpotY) throws SlickException {
	try {
		Mouse.setNativeCursor(cursor);
	} catch (Throwable e) {
		Log.error("Failed to load and apply cursor.", e);
		throw new SlickException("Failed to set mouse cursor", e);
	}
}
 
Example #12
Source File: AppGameContainer.java    From slick2d-maven with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * @see org.newdawn.slick.GameContainer#setMouseCursor(org.newdawn.slick.opengl.ImageData, int, int)
 */
public void setMouseCursor(ImageData data, int hotSpotX, int hotSpotY) throws SlickException {
	try {
		Cursor cursor = CursorLoader.get().getCursor(data, hotSpotX, hotSpotY);
		Mouse.setNativeCursor(cursor);
	} catch (Throwable e) {
		Log.error("Failed to load and apply cursor.", e);
		throw new SlickException("Failed to set mouse cursor", e);
	}
}
 
Example #13
Source File: AppGameContainer.java    From slick2d-maven with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * @see org.newdawn.slick.GameContainer#setMouseCursor(java.lang.String, int, int)
 */
public void setMouseCursor(String ref, int hotSpotX, int hotSpotY) throws SlickException {
	try {
		Cursor cursor = CursorLoader.get().getCursor(ref, hotSpotX, hotSpotY);
		Mouse.setNativeCursor(cursor);
	} catch (Throwable e) {
		Log.error("Failed to load and apply cursor.", e);
		throw new SlickException("Failed to set mouse cursor", e);
	}
}
 
Example #14
Source File: AppletGameContainer.java    From slick2d-maven with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
    * @see org.newdawn.slick.GameContainer#setMouseCursor(org.lwjgl.input.Cursor, int, int)
    */
   public void setMouseCursor(Cursor cursor, int hotSpotX, int hotSpotY) throws SlickException {
      try {
         Mouse.setNativeCursor(cursor);
      } catch (Throwable e) {
         Log.error("Failed to load and apply cursor.", e);
throw new SlickException("Failed to set mouse cursor", e);
      }
   }
 
Example #15
Source File: AppletGameContainer.java    From slick2d-maven with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
    * @see org.newdawn.slick.GameContainer#setMouseCursor(org.newdawn.slick.opengl.ImageData, int, int)
    */
   public void setMouseCursor(ImageData data, int hotSpotX, int hotSpotY) throws SlickException {
      try {
         Cursor cursor = CursorLoader.get().getCursor(data, hotSpotX, hotSpotY);
         Mouse.setNativeCursor(cursor);
      } catch (Throwable e) {
         Log.error("Failed to load and apply cursor.", e);
throw new SlickException("Failed to set mouse cursor", e);
      }
   }
 
Example #16
Source File: SeventhGame.java    From seventh with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Hide the Mouse cursor
 * 
 * @param visible
 */
private void setHWCursorVisible(boolean visible) {
    if (Gdx.app.getType() != ApplicationType.Desktop && Gdx.app instanceof LwjglApplication) {
        return;
    }

    try {
        /* make sure the mouse doesn't move off the screen */
        seventh.client.gfx.Cursor cursor = this.uiManager.getCursor();
        cursor.setClampEnabled(config.getVideo().isFullscreen());
        Gdx.input.setCursorCatched(config.getVideo().isFullscreen());
        
        //Gdx.input.setCursorCatched(true);
        //Gdx.input.setCursorPosition(getScreenWidth()/2, getScreenHeight()/2);
        
        Cursor emptyCursor = null;
        if (Mouse.isCreated()) {
            int min = org.lwjgl.input.Cursor.getMinCursorSize();
            IntBuffer tmp = BufferUtils.createIntBuffer(min * min);
            emptyCursor = new org.lwjgl.input.Cursor(min, min, min / 2, min / 2, 1, tmp, null);
        } else {
            Cons.println("Could not create empty cursor before Mouse object is created");
        }
    
        if (/*Mouse.isInsideWindow() &&*/ emptyCursor != null) {
            Mouse.setNativeCursor(visible ? null : emptyCursor);
        }
    }
    catch(LWJGLException e) {
        Cons.println("*** Unable to hide cursor: " + e);
    }
}
 
Example #17
Source File: DesktopLauncher.java    From bladecoder-adventure-engine with Apache License 2.0 5 votes vote down vote up
private void hideCursor() {
	Cursor emptyCursor;

	int min = org.lwjgl.input.Cursor.getMinCursorSize();
	IntBuffer tmp = BufferUtils.createIntBuffer(min * min);
	try {
		emptyCursor = new org.lwjgl.input.Cursor(min, min, min / 2,
				min / 2, 1, tmp, null);

		Mouse.setNativeCursor(emptyCursor);
	} catch (LWJGLException e) {
		EditorLogger.printStackTrace(e);
	}

}
 
Example #18
Source File: LwjglMouseInput.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void destroy() {
    if (!context.isRenderable())
        return;

    Mouse.destroy();

    // Destroy the cursor cache
    for (Cursor cursor : cursorMap.values()) {
        cursor.destroy();
    }
    cursorMap.clear();

    logger.fine("Mouse destroyed.");
}
 
Example #19
Source File: PointerInput.java    From tribaltrouble with GNU General Public License v2.0 5 votes vote down vote up
private final static void doSetActiveCursor(Cursor cursor) {
	active_cursor = cursor;
	try {
		Mouse.setNativeCursor(LocalEventQueue.getQueue().getDeterministic().isPlayback() ? debug_cursor.getCursor() : cursor);
	} catch (LWJGLException e) {
		throw new RuntimeException(e);
	}
}
 
Example #20
Source File: PointerInput.java    From tribaltrouble with GNU General Public License v2.0 5 votes vote down vote up
public final static void setActiveCursor(Cursor cursor) {
	if (cursor != null && Mouse.isGrabbed()) {
		Mouse.setGrabbed(false);
		resetCursorPos();
	} else if (cursor == null && !Mouse.isGrabbed()) {
		Mouse.setGrabbed(true);
		resetCursorPos();
	}
	if (active_cursor != cursor) {
		doSetActiveCursor(cursor);
	}
}
 
Example #21
Source File: NativeCursor.java    From tribaltrouble with GNU General Public License v2.0 5 votes vote down vote up
public NativeCursor(GLIntImage image_16_1, int offset_x_16_1, int offset_y_16_1,
					GLIntImage image_32_1, int offset_x_32_1, int offset_y_32_1,
					GLIntImage image_32_8, int offset_x_32_8, int offset_y_32_8) {
	org.lwjgl.input.Cursor native_cursor = null;
	int caps = Cursor.getCapabilities();
	
	int alpha_bits = 0;
	if ((caps & Cursor.CURSOR_8_BIT_ALPHA) != 0)
		alpha_bits = 8;
	else if ((caps & Cursor.CURSOR_ONE_BIT_TRANSPARENCY) != 0)
		alpha_bits = 1;

	int max_size = Cursor.getMaxCursorSize();
		
	try {
		if (max_size < 32 && max_size >= 16 && alpha_bits >= 1)
			native_cursor = new org.lwjgl.input.Cursor(image_16_1.getWidth(), image_16_1.getHeight(), offset_x_16_1, offset_y_16_1, 1, image_16_1.createCursorPixels(), null);
		else if (max_size >= 32) {
			if (alpha_bits == 8)
				native_cursor = new org.lwjgl.input.Cursor(image_32_8.getWidth(), image_32_8.getHeight(), offset_x_32_8, offset_y_32_8, 1, image_32_8.createCursorPixels(), null);
			else if (alpha_bits == 1)
				native_cursor = new org.lwjgl.input.Cursor(image_32_1.getWidth(), image_32_1.getHeight(), offset_x_32_1, offset_y_32_1, 1, image_32_1.createCursorPixels(), null);
		}
	} catch (LWJGLException e) {
		e.printStackTrace();
	}
	cursor = native_cursor;
}
 
Example #22
Source File: GLHelper.java    From opsu-dance with GNU General Public License v3.0 5 votes vote down vote up
public static void hideNativeCursor()
{
	try {
		int min = Cursor.getMinCursorSize();
		IntBuffer tmp = BufferUtils.createIntBuffer(min * min);
		Mouse.setNativeCursor(new Cursor(min, min, min / 2, min / 2, 1, tmp, null));
	} catch (LWJGLException e) {
		softErr(e, "Could not hide native cursor");
	}
}
 
Example #23
Source File: LocalInput.java    From tribaltrouble with GNU General Public License v2.0 4 votes vote down vote up
public final static int getNativeCursorCaps() {
	return LocalEventQueue.getQueue().getDeterministic().log(Cursor.getCapabilities());
}
 
Example #24
Source File: PointerInput.java    From tribaltrouble with GNU General Public License v2.0 4 votes vote down vote up
public final static void deletingCursor(Cursor cursor) {
	if (active_cursor == cursor)
		doSetActiveCursor(null);
}
 
Example #25
Source File: NativeCursor.java    From tribaltrouble with GNU General Public License v2.0 4 votes vote down vote up
public final org.lwjgl.input.Cursor getCursor() {
	return cursor;
}
 
Example #26
Source File: CursorLoader.java    From slick2d-maven with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Get a cursor based on a image reference on the classpath. The image 
 * is assumed to be a set/strip of cursor animation frames running from top to 
 * bottom.
 * 
 * @param ref The reference to the image to be loaded
 * @param x The x-coordinate of the cursor hotspot (left -> right)
 * @param y The y-coordinate of the cursor hotspot (bottom -> top)
 * @param width The x width of the cursor
 * @param height The y height of the cursor
 * @param cursorDelays image delays between changing frames in animation
 * 					
 * @return The created cursor
 * @throws IOException Indicates a failure to load the image
 * @throws LWJGLException Indicates a failure to create the hardware cursor
 */
public Cursor getAnimatedCursor(String ref,int x,int y, int width, int height, int[] cursorDelays) throws IOException, LWJGLException {
	IntBuffer cursorDelaysBuffer = ByteBuffer.allocateDirect(cursorDelays.length*4).order(ByteOrder.nativeOrder()).asIntBuffer();
	for (int i=0;i<cursorDelays.length;i++) {
		cursorDelaysBuffer.put(cursorDelays[i]);
	}
	cursorDelaysBuffer.flip();

	LoadableImageData imageData = new TGAImageData();
	ByteBuffer buf = imageData.loadImage(ResourceLoader.getResourceAsStream(ref), false, null);
				
	return new Cursor(width, height, x, y, cursorDelays.length, buf.asIntBuffer(), cursorDelaysBuffer);
}
 
Example #27
Source File: GameContainer.java    From opsu with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Set the mouse cursor to be displayed - this is a hardware cursor and hence
 * shouldn't have any impact on FPS.
 * 
 * @param cursor The cursor to use
 * @param hotSpotX The x coordinate of the hotspot within the cursor image
 * @param hotSpotY The y coordinate of the hotspot within the cursor image
 * @throws SlickException Indicates a failure to load the cursor image or create the hardware cursor
 */
@Override
public abstract void setMouseCursor(Cursor cursor, int hotSpotX, int hotSpotY) throws SlickException;
 
Example #28
Source File: GameContainer.java    From slick2d-maven with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Set the mouse cursor to be displayed - this is a hardware cursor and hence
 * shouldn't have any impact on FPS.
 * 
 * @param cursor The cursor to use
 * @param hotSpotX The x coordinate of the hotspot within the cursor image
 * @param hotSpotY The y coordinate of the hotspot within the cursor image
 * @throws SlickException Indicates a failure to load the cursor image or create the hardware cursor
 */
public abstract void setMouseCursor(Cursor cursor, int hotSpotX, int hotSpotY) throws SlickException;
 
Example #29
Source File: GUIContext.java    From slick2d-maven with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Set the mouse cursor to be displayed - this is a hardware cursor and hence
 * shouldn't have any impact on FPS.
 * 
 * @param cursor The cursor to use
 * @param hotSpotX The x coordinate of the hotspot within the cursor image
 * @param hotSpotY The y coordinate of the hotspot within the cursor image
 * @throws SlickException Indicates a failure to load the cursor image or create the hardware cursor
 */
public abstract void setMouseCursor(Cursor cursor, int hotSpotX, int hotSpotY) throws SlickException;