-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Deprecate CursorLoader and delete it from Desktop.cfg file #2742
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 8 commits
9dfb1a3
e78b01e
7b0ff9d
dd3335c
ba7e586
85de977
23ab1e7
b9e60e1
ba390ac
40ffe74
ee832ce
db22e58
14c41e7
596ea87
e002d5d
f8688be
0708ab1
be9f260
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,3 @@ | ||
| INCLUDE com/jme3/asset/General.cfg | ||
|
|
||
| # Desktop-specific loaders | ||
| LOADER com.jme3.cursors.plugins.CursorLoader : ani, cur, ico | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,197 @@ | ||
| /* | ||
| * Copyright (c) 2009-2026 jMonkeyEngine | ||
| * All rights reserved. | ||
| * | ||
| * Redistribution and use in source and binary forms, with or without | ||
| * modification, are permitted provided that the following conditions are | ||
| * met: | ||
| * | ||
| * * Redistributions of source code must retain the above copyright | ||
| * notice, this list of conditions and the following disclaimer. | ||
| * | ||
| * * 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. | ||
| * | ||
| * * Neither the name of 'jMonkeyEngine' nor the names of its contributors | ||
| * may be used to endorse or promote products derived from this software | ||
| * without specific prior written permission. | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| * "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 THE COPYRIGHT OWNER 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. | ||
| */ | ||
| package com.jme3.cursors.plugins; | ||
|
|
||
| import java.nio.IntBuffer; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import com.jme3.math.ColorRGBA; | ||
| import com.jme3.texture.Image; | ||
| import com.jme3.texture.Texture2D; | ||
| import com.jme3.texture.image.ImageRaster; | ||
| import com.jme3.util.BufferUtils; | ||
|
|
||
| /** | ||
| * Convert any image like object to a {@link JmeCursor}. | ||
| */ | ||
| public class CursorConverter { | ||
| /** | ||
| * Convert a {@link Texture2D} to a {@link JmeCursor}. The coordinate system used is the same specified | ||
| * in {@link JmeCursor}. The start point is 0, 0 being lower left. | ||
| * | ||
| * @param cursorImage The texture to convert. No modifications will be applyed. | ||
| * | ||
| * @return The {@link JmeCursor} using a deep copy of {@link Texture2D.getImage}. | ||
| */ | ||
| public static JmeCursor fromTexture(Texture2D cursorImage) { | ||
| Image image = cursorImage.getImage().clone(); | ||
|
mondogo24 marked this conversation as resolved.
Outdated
|
||
|
|
||
| if (image == null) { | ||
| throw new NullPointerException("There is not an image set to the Texture2D"); | ||
| } | ||
|
|
||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Solved with commit 40ffe74. Since there are already null checks with Texture2D constructors and Image.clone() with good messages, I thought about deleting the null checks. But I am open to add back the manual null checks if someone disagree.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And also including the java's internal null checks when calling methods of classes. |
||
| int imageHeight = image.getHeight(); | ||
| int imageWidth = image.getWidth(); | ||
|
|
||
| IntBuffer adaptedImageData = getDataAsIntBuffer(image); | ||
|
|
||
| JmeCursor jmeCursor = new JmeCursor(); | ||
| jmeCursor.setWidth(imageWidth); | ||
| jmeCursor.setHeight(imageHeight); | ||
| jmeCursor.setxHotSpot(0); | ||
| jmeCursor.setyHotSpot(imageHeight); | ||
|
mondogo24 marked this conversation as resolved.
Outdated
|
||
| jmeCursor.setNumImages(1); | ||
| jmeCursor.setImagesDelay(null); | ||
| jmeCursor.setImagesData(adaptedImageData); | ||
| return jmeCursor; | ||
| } | ||
|
|
||
| private static IntBuffer getDataAsIntBuffer(Image image) { | ||
| int width = image.getWidth(); | ||
| int height = image.getHeight(); | ||
|
|
||
| ImageRaster raster = ImageRaster.create(image); | ||
|
|
||
| IntBuffer data = BufferUtils.createIntBuffer(width * height); | ||
|
|
||
| //ARGB color system is needed to show cursors correctly. | ||
| for (int y = 0; y < height; y++) { | ||
| for (int x = 0; x < width; x++) { | ||
| ColorRGBA color = raster.getPixel(x, y); | ||
|
|
||
| int a = (int) (color.a * 255) & 0xFF; | ||
| int r = (int) (color.r * 255) & 0xFF; | ||
| int g = (int) (color.g * 255) & 0xFF; | ||
| int b = (int) (color.b * 255) & 0xFF; | ||
|
|
||
| int argb = (a << 24) | (r << 16) | (g << 8) | b; | ||
|
|
||
| data.put(argb); | ||
| } | ||
| } | ||
|
|
||
| data.flip(); | ||
| return data; | ||
| } | ||
|
|
||
| /** | ||
| * Convert a {@link Texture2D} array to a {@link JmeCursor} object that will represent an animated cursor, | ||
| * interpreting each {@link Texture2D} object as a frame of the animated cursor. | ||
| * The coordinate system used for each frame is the same specified in {@link JmeCursor}. The start point | ||
| * is 0, 0 being lower left. | ||
| * | ||
| * @param frameDelay The time delay that will take for a cursor to change from one frame to another. | ||
| * @param cursorFrames The frames that will make up the cursor animation. No modifications will be applyed. | ||
| * | ||
| * @return A {@link JmeCursor} object that contains the data for an animated cursor. | ||
| */ | ||
| public static JmeCursor fromTextureFrames(int frameDelay, Texture2D[] cursorFrames) { | ||
| int[] frameRates = new int[cursorFrames.length]; | ||
| Arrays.fill(frameRates, frameDelay); | ||
| return fromTextureFrames(frameRates, cursorFrames); | ||
| } | ||
|
|
||
| /** | ||
| * Convert a {@link Texture2D} array to a {@link JmeCursor} object that will represent an animated cursor, | ||
| * interpreting each {@link Texture2D} object as a frame of the animated cursor. | ||
| * The coordinate system used for each frame is the same specified in {@link JmeCursor}. The start point | ||
| * is 0, 0 being lower left. | ||
| * | ||
| * @param frameDelays The time delay that will take each frame to change to the next frame. Because of it, | ||
| * it must contains as many delays as frames (lenghts of cursorFrames and frameDelays | ||
| * arrays must be equals). | ||
| * @param cursorFrames The frames that will make up the cursor animation. No modifications will be applyed. | ||
| * | ||
| * @return A {@link JmeCursor} object that contains the data for an animated cursor. | ||
| */ | ||
| public static JmeCursor fromTextureFrames(int[] frameDelays, Texture2D[] cursorFrames) { | ||
| if (frameDelays.length != cursorFrames.length) { | ||
| throw new IllegalArgumentException("The lenghts of cursorFrames and frameDelays arrays must be equals"); | ||
| } | ||
|
|
||
| List<Image> imageFrames = Arrays.stream(cursorFrames) | ||
| .map((frame) -> frame.getImage()) | ||
| .collect(Collectors.toList()); | ||
|
|
||
| boolean missingImage = imageFrames | ||
| .stream() | ||
| .anyMatch((frame) -> frame == null); | ||
|
|
||
| if (missingImage) { | ||
| throw new NullPointerException("Some Texture2D objects does not have a setted imaged"); | ||
| } | ||
|
|
||
| //Avoid working and accidentally modifying original values | ||
| imageFrames = imageFrames | ||
| .stream() | ||
| .map((image) -> image.clone()) | ||
| .collect(Collectors.toList()); | ||
|
|
||
| List<Integer> imageFrameHeights = imageFrames | ||
| .stream() | ||
| .map((image) -> image.getHeight()) | ||
| .distinct() | ||
| .collect(Collectors.toList()); | ||
|
|
||
| List<Integer> imageFrameWidths = imageFrames | ||
| .stream() | ||
| .map((image) -> image.getWidth()) | ||
| .distinct() | ||
| .collect(Collectors.toList()); | ||
|
|
||
| if (imageFrameHeights.size() > 1 || imageFrameWidths.size() > 1) { | ||
| throw new IllegalArgumentException("Some images from the Texture2D objects has different sizes"); | ||
|
mondogo24 marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| int imageHeight = imageFrameHeights.get(0); | ||
| int imageWidth = imageFrameWidths.get(0); | ||
|
|
||
| IntBuffer framesData = BufferUtils.createIntBuffer(imageHeight * imageWidth * cursorFrames.length); | ||
|
|
||
| framesData = imageFrames | ||
| .stream() | ||
| .map((image) -> getDataAsIntBuffer(image)) | ||
| .reduce(framesData, (previous, next) -> previous.put(next)); | ||
|
|
||
| JmeCursor jmeCursor = new JmeCursor(); | ||
| jmeCursor.setWidth(imageWidth); | ||
| jmeCursor.setHeight(imageHeight); | ||
| jmeCursor.setxHotSpot(0); | ||
| jmeCursor.setyHotSpot(imageHeight); | ||
| jmeCursor.setNumImages(cursorFrames.length); | ||
| jmeCursor.setImagesDelay(BufferUtils.createIntBuffer(frameDelays)); | ||
| jmeCursor.setImagesData(framesData); | ||
|
mondogo24 marked this conversation as resolved.
Outdated
|
||
| return jmeCursor; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.