Need a plan of action

I have a feature that allows the user to split the screen horizontally and vertically using splitHorizontal() and splitVertical(). What I would like to add is that when a certain button on the menu is pressed:

1- change the mouse cursor to a different shape . Saw several posts on the forum that this is still not resolved (however this is not THAT important)
2- wait for the user to press the left click and then trigger one of these two methods. The reason being is that these methods rely on the mouse cursor position to do the splitting. I used to do that using hotkeys but now that it’s in a menu I need to do that.
3- change mouse cursor back to original shape.

The issue is if I have a code like the one below… how can I hold on till left click is pressed befolre moving to the verticalSplit() method.

[java]
@NiftyEventSubscriber(id=“horizontal”)
public void onHorizontalSplit(final String id, final ButtonClickedEvent event)
{
switchMouseCursor(); // <<<<<< what should this method be in order to wait for a left click before continuing
verticalSplit();
}[/java]

Well you execute it in the frame where the user releases the button? This is basic update loop stuff…

1 Like

Thanks @normen, you’re right, I dk why the idea scared me. I used appStates to do that and was as u said very basic implementation. I basically have the appState listen for a left click before calling these mehtods.

any idea on how to change the mouse cursor though? I spent 2 days looking around on the forums for a hint with no luck… I never did anything like that before.

any idea?

thx

you have to make the cursor invisble, then add your own geometry to the guiNode to represent the cursor. Then use the inputManager to add listeners that will update your custom cursor’s location based on mouse actions.

1 Like
Then use the inputManager to add listeners that will update your custom cursor’s location based on mouse actions.

and that’s where I am stuck…

I usually use : getInputManager().getCursorPosition() however now that the cursor is off how do I get these updates?

cz I tried [java] @Override
public void execute() {

            geom.setLocalTranslation(inputManager().
                    getCursorPosition().x, inputManager().getCursorPosition().y, 0);

        }
    }, MouseInput.AXIS_X);[/java] 

and the object still doesn’t budge >>> setlocaltrans() method isn’t even being called.

I hereby invoke my super-powers as keeper-of-the-javadoc-linke… a power granted to only me and passed down through generations…

http://hub.jmonkeyengine.org/javadoc/com/jme3/input/InputManager.html#addRawInputListener(com.jme3.input.RawInputListener)

haha

thanks @pspeed I didn’t know about the raw input listener, and it works great except that the geomtry that I am draggging (or moving according to the mouse position) cannot keep up with the mouse position. so if you can see the cursor you actually willl see the cursor move all the way up to the right upper corder while the geometry is only slightly moved to that side . Any idea why?

If you wanted to change the cursor, why not just use the JmeCursor class that was added a while back?

(This is MadJack’s class, not mine, just in case you miss it somehow)
[java]
package jme3test.gui;

import com.jme3.app.SimpleApplication;
import com.jme3.cursors.plugins.JmeCursor;
import java.util.ArrayList;

/**

  • This test class demonstrate how to change cursor in jME3.

  • NOTE: This will not work on Android as it does not support cursors.

  • Cursor test

  • @author MadJack
    */
    public class TestCursor extends SimpleApplication {

    private ArrayList<JmeCursor> cursors = new ArrayList<JmeCursor>();
    private long sysTime;
    private int count = 0;

    public static void main(String[] args){
    TestCursor app = new TestCursor();

     app.setShowSettings(false);
     app.start();
    

    }

    @Override
    public void simpleInitApp() {
    flyCam.setEnabled(false);
    // We need the cursor to be visible. If it is not visible the cursor
    // will still be “used” and loaded, you just won’t see it on the screen.
    inputManager.setCursorVisible(true);

     /*
      * To make jME3 use a custom cursor it is as simple as putting the
      * .cur/.ico/.ani file in an asset directory. Here we use
      * "Textures/GUI/Cursors".
      *
      * For the purpose of this demonstration we load 3 different cursors and add them
      * into an array list and switch cursor every 8 seconds.
      *
      * The first ico has been made by Sirea and the set can be found here:
      * http://www.rw-designer.com/icon-set/nyan-cat
      *
      * The second cursor has been made by Virum64 and is Public Domain.
      * http://www.rw-designer.com/cursor-set/memes-faces-v64
      *
      * The animated cursor has been made by Pointer Adic and can be found here:
      * http://www.rw-designer.com/cursor-set/monkey
      */
     cursors.add((JmeCursor) assetManager.loadAsset("Textures/Cursors/meme.cur"));
     cursors.add((JmeCursor) assetManager.loadAsset("Textures/Cursors/nyancat.ico"));
     cursors.add((JmeCursor) assetManager.loadAsset("Textures/Cursors/monkey.ani"));
    
     sysTime = System.currentTimeMillis();
     inputManager.setMouseCursor(cursors.get(count));
    

    }

    @Override
    public void simpleUpdate(float tpf) {
    long currentTime = System.currentTimeMillis();

     if (currentTime - sysTime &gt; 8000) {
         count++;
         if (count &gt;= cursors.size()) {
             count = 0;
         }
         sysTime = currentTime;
         // 8 seconds have passed,
         // tell jME3 to swith to a different cursor.
         inputManager.setMouseCursor(cursors.get(count));
     }
    

    }
    }

[/java]

@homsi said: haha

thanks @pspeed I didn’t know about the raw input listener, and it works great except that the geomtry that I am draggging (or moving according to the mouse position) cannot keep up with the mouse position. so if you can see the cursor you actually willl see the cursor move all the way up to the right upper corder while the geometry is only slightly moved to that side . Any idea why?

Must be how you are converting mouse coordinates to scene coordinates or something. (shrug)

Note that I think when the cursor is invisible then the cursor gets routinely snapped back to center, I think.

I like @navid113 's solution, except that it worked on the latest nightly but not on my stable version (and I am intending to stay with the stable version)

I tried looking into the source code to avoid doing an update but ran into a wall:

[java]
private void changeMouseCursorHorizontal() throws LWJGLException {

    // load custom cursor
    JmeCursor jmeCursor = (JmeCursor) app.getAssetManager()
            .loadAsset("/Cursors/horizontalLine.cur");
    
    // set width
    jmeCursor.setWidth(app.getSettings().getWidth());
   
    
    // convert jmecursor to Cursor
    Cursor newCursor;
    try {
        newCursor = new Cursor(
                jmeCursor.getWidth(),
                jmeCursor.getHeight(),
                jmeCursor.getXHotSpot(),
                jmeCursor.getYHotSpot(),
                jmeCursor.getNumImages(),
                jmeCursor.getImagesData(),
                jmeCursor.getImagesDelay());

        // Update the mouse native cursor
        Mouse.setNativeCursor(newCursor);

    } catch (LWJGLException e) {

        System.err.println("Problem changing the default mouse cursor");
        e.printStackTrace();
    }
    
    
  
}[/java] 

where the map extensionToLoaderMap in ImplHAndler.class doesn’t recognize the type .cur

Are there any workarounds towards adding it (and that goes beyond my knowledge on how to do that) or am I forced to update?

@pspeed , you’re right >>>>Note that I think when the cursor is invisible then the cursor gets routinely snapped back to center, I think.

so our previous appraoch wont work

I tried the following code from @madjack:

[java]
rivate void setMouseCursor() {

Texture tex = assetManager.loadTexture(“Textures/GUI/Cursors/derp.png”);

Image img = tex.getImage();

ByteBuffer data = img.getData(0);
data.rewind();
IntBuffer image = BufferUtils.createIntBuffer(img.getHeight() * img.getWidth());
for (int y = 0; y < img.getHeight(); y++){
for (int x = 0; x < img.getWidth(); x++){
int rgba = data.getInt();
image.put(rgba);
}
}
image.rewind();

Cursor cur = null;
try {
cur = new Cursor(img.getWidth(), img.getHeight(), 1, img.getHeight() – 1, 1, image, null);
} catch (LWJGLException ex) {
Logger.getLogger(Disenthral.class.getName()).log(Level.SEVERE, null, ex);
}

inputManager.setMouseCursor(cur);
image.clear();
}[/java]

and the issue is that every .png that I create (of a single rectangle with one color) tends to show distorted

Hey.

Sorry for not answering before, I was on vacations.

Is this still relevant?

There are ways to make cursors from png already, I’m not sure if the above is a rewrite of what I wrote or a simple copy/paste hoping it would work in your program, but my question is, why not use one of the factory method to create a cursor based on an image?

I haven’t touched that class in a very, very long time and I barely remember anything in there, but it is working here. If it were broken I would know since I use it in Disenthral. Arguably it’s not as good/perfect as it could be, but distortion shouldn’t happen.

Anyway, more information would be appreciated if the ‘issue’ is still there.