Search in sources :

Example 1 with JmeCursor

use of com.jme3.cursors.plugins.JmeCursor in project jmonkeyengine by jMonkeyEngine.

the class CursorLoader method load.

/**
     * Loads and return a cursor file of one of the following format: .ani, .cur and .ico.
     * @param info The {@link AssetInfo} describing the cursor file.
     * @return A JmeCursor representation of the LWJGL's Cursor.
     * @throws IOException if the file is not found.
     */
public JmeCursor load(AssetInfo info) throws IOException {
    isIco = false;
    isAni = false;
    isCur = false;
    isIco = ((AssetKey) info.getKey()).getExtension().equals("ico");
    if (!isIco) {
        isCur = ((AssetKey) info.getKey()).getExtension().equals("cur");
        if (!isCur) {
            isAni = ((AssetKey) info.getKey()).getExtension().equals("ani");
        }
    }
    if (!isAni && !isIco && !isCur) {
        throw new IllegalArgumentException("Cursors supported are .ico, .cur or .ani");
    }
    InputStream in = null;
    try {
        in = info.openStream();
        return loadCursor(in);
    } finally {
        if (in != null) {
            in.close();
        }
    }
}
Also used : AssetKey(com.jme3.asset.AssetKey) DataInputStream(java.io.DataInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream)

Example 2 with JmeCursor

use of com.jme3.cursors.plugins.JmeCursor in project jmonkeyengine by jMonkeyEngine.

the class CursorLoader method loadCursor.

private JmeCursor loadCursor(InputStream inStream) throws IOException {
    // new byte [0] facilitates read()
    byte[] icoimages = new byte[0];
    if (isAni) {
        CursorLoader.CursorImageData ciDat = new CursorLoader.CursorImageData();
        int numIcons = 0;
        int jiffy = 0;
        // not using those but keeping references for now.
        int steps = 0;
        int width = 0;
        int height = 0;
        // we don't use that.
        int flag = 0;
        int[] rate = null;
        int[] animSeq = null;
        ArrayList<byte[]> icons;
        DataInput leIn = new LittleEndien(inStream);
        int riff = leIn.readInt();
        if (riff == 0x46464952) {
            // RIFF
            // read next int (file length), discarding it, we don't need that.
            leIn.readInt();
            int nextInt = 0;
            nextInt = getNext(leIn);
            if (nextInt == 0x4e4f4341) {
                // We have ACON, we do nothing
                //                    System.out.println("We have ACON. Next!");
                nextInt = getNext(leIn);
                while (nextInt >= 0) {
                    if (nextInt == 0x68696e61) {
                        //                            System.out.println("we have 'anih' header");
                        // internal struct length (always 36)
                        leIn.skipBytes(8);
                        numIcons = leIn.readInt();
                        // number of blits for ani cycles
                        steps = leIn.readInt();
                        width = leIn.readInt();
                        height = leIn.readInt();
                        leIn.skipBytes(8);
                        jiffy = leIn.readInt();
                        flag = leIn.readInt();
                        nextInt = leIn.readInt();
                    } else if (nextInt == 0x65746172) {
                        // found a 'rate' of animation
                        //                            System.out.println("we have 'rate'.");
                        // Fill rate here.
                        // Rate is synchronous with frames.
                        int length = leIn.readInt();
                        rate = new int[length / 4];
                        for (int i = 0; i < length / 4; i++) {
                            rate[i] = leIn.readInt();
                        }
                        nextInt = leIn.readInt();
                    } else if (nextInt == 0x20716573) {
                        // found a 'seq ' of animation
                        //                            System.out.println("we have 'seq '.");
                        // Fill animation sequence here
                        int length = leIn.readInt();
                        animSeq = new int[length / 4];
                        for (int i = 0; i < length / 4; i++) {
                            animSeq[i] = leIn.readInt();
                        }
                        nextInt = leIn.readInt();
                    } else if (nextInt == 0x5453494c) {
                        // Found a LIST
                        //                            System.out.println("we have 'LIST'.");
                        int length = leIn.readInt();
                        nextInt = leIn.readInt();
                        if (nextInt == 0x4f464e49) {
                            // Got an INFO, skip its length
                            // this part consist  of Author, title, etc
                            leIn.skipBytes(length - 4);
                            //                                System.out.println(" Discarding INFO (skipped = " + skipped + ")");
                            nextInt = leIn.readInt();
                        } else if (nextInt == 0x6d617266) {
                            //                                System.out.println("we have 'fram'.");
                            if (leIn.readInt() == 0x6e6f6369) {
                                // we have 'icon'
                                // We have an icon and from this point on
                                // the rest is only icons.
                                int icoLength = leIn.readInt();
                                ciDat.numImages = numIcons;
                                icons = new ArrayList<byte[]>(numIcons);
                                for (int i = 0; i < numIcons; i++) {
                                    if (i > 0) {
                                        // skip 'icon' header and length as they are
                                        // known already and won't change.
                                        leIn.skipBytes(8);
                                    }
                                    byte[] data = new byte[icoLength];
                                    ((InputStream) leIn).read(data, 0, icoLength);
                                    // get it from first image.
                                    if (width == 0 || height == 0 && i == 1) {
                                        width = data[6];
                                        height = data[7];
                                    }
                                    icons.add(data);
                                }
                                // at this point we have the icons, rates (either
                                // through jiffy or rate array, the sequence (if
                                // applicable) and the ani header info.
                                // Put things together.
                                ciDat.assembleCursor(icons, rate, animSeq, jiffy, steps, width, height);
                                ciDat.completeCursor();
                                nextInt = leIn.readInt();
                                // if for some reason there's JUNK (nextInt > -1)
                                // bail out.
                                nextInt = nextInt > -1 ? -1 : nextInt;
                            }
                        }
                    }
                }
            }
            return setJmeCursor(ciDat);
        } else if (riff == 0x58464952) {
            throw new IllegalArgumentException("Big-Endian RIFX is not supported. Sorry.");
        } else {
            throw new IllegalArgumentException("Unknown format.");
        }
    } else if (isCur || isIco) {
        DataInputStream in = new DataInputStream(inStream);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buffer = new byte[16384];
        int bytesRead;
        while ((bytesRead = in.read(buffer)) >= 0) {
            out.write(buffer, 0, bytesRead);
        }
        icoimages = out.toByteArray();
    }
    BufferedImage[] bi = parseICOImage(icoimages);
    int hotSpotX = 0;
    int hotSpotY = 0;
    CursorLoader.CursorImageData cid = new CursorLoader.CursorImageData(bi, 0, hotSpotX, hotSpotY, 0);
    if (isCur) {
        /*
             * Per http://msdn.microsoft.com/en-us/library/ms997538.aspx
             * every .cur file should provide hotspot coordinates.
             */
        hotSpotX = icoimages[FDE_OFFSET + 4] + icoimages[FDE_OFFSET + 5] * 255;
        hotSpotY = icoimages[FDE_OFFSET + 6] + icoimages[FDE_OFFSET + 7] * 255;
        cid.xHotSpot = hotSpotX;
        /*
             * Flip the Y-coordinate.
             */
        cid.yHotSpot = cid.height - 1 - hotSpotY;
    }
    cid.completeCursor();
    return setJmeCursor(cid);
}
Also used : DataInputStream(java.io.DataInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream) LittleEndien(com.jme3.util.LittleEndien) BufferedImage(java.awt.image.BufferedImage) DataInput(java.io.DataInput)

Example 3 with JmeCursor

use of com.jme3.cursors.plugins.JmeCursor in project jmonkeyengine by jMonkeyEngine.

the class LwjglMouseInput method setNativeCursor.

public void setNativeCursor(JmeCursor jmeCursor) {
    try {
        Cursor newCursor = null;
        if (jmeCursor != null) {
            newCursor = cursorMap.get(jmeCursor);
            if (newCursor == null) {
                newCursor = new Cursor(jmeCursor.getWidth(), jmeCursor.getHeight(), jmeCursor.getXHotSpot(), jmeCursor.getYHotSpot(), jmeCursor.getNumImages(), jmeCursor.getImagesData(), jmeCursor.getImagesDelay());
                // Add to cache
                cursorMap.put(jmeCursor, newCursor);
            }
        }
        Mouse.setNativeCursor(newCursor);
    } catch (LWJGLException ex) {
        Logger.getLogger(LwjglMouseInput.class.getName()).log(Level.SEVERE, null, ex);
    }
}
Also used : JmeCursor(com.jme3.cursors.plugins.JmeCursor) Cursor(org.lwjgl.input.Cursor) LWJGLException(org.lwjgl.LWJGLException)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)2 DataInputStream (java.io.DataInputStream)2 InputStream (java.io.InputStream)2 AssetKey (com.jme3.asset.AssetKey)1 JmeCursor (com.jme3.cursors.plugins.JmeCursor)1 LittleEndien (com.jme3.util.LittleEndien)1 BufferedImage (java.awt.image.BufferedImage)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataInput (java.io.DataInput)1 LWJGLException (org.lwjgl.LWJGLException)1 Cursor (org.lwjgl.input.Cursor)1