Jmonkey Nifty. detect if user stops sliding

105 Views Asked by At

Is there a way to detect if a user stopped slyding? Maybe bij mouse input check or something. I tried mouse isButton0Release and hasFocus in the onSliderChange event to check if the user stopped sliding, but this doesn't work.

What i got so far

@NiftyEventSubscriber(id = "speedSlider")
public void onSliderChange(String id, SliderChangedEvent event) 
{   boolean test2 = event.getSlider().hasFocus();
    boolean test = nifty.getMouseInputEventQueue().getLastMouseDownEvent().isButton0Release();
    System.out.println("before " + test2);
    int speed = (int)event.getValue();
    speedTextField.getRenderer(TextRenderer.class).setText(speed + "");
    if(test2){
        System.out.println("after " + test2);
        main.setSimSpeed(speed);
    }
}

And the slider in xml

                <panel id="speed_up_down_panel" height="30px" width="180px" childLayout="center">         
                <control id="speedSlider" name="horizontalSlider" width="150px" min="1" initial="1" buttonStepSize="1">
                    <image id="#position" filename="Interface/sliderbutton.png" visibleToMouse="true" width="40px" height="40px"></image>
                </control>
            </panel> 

The code is a bit messy now becuase i testing.

1

There are 1 best solutions below

0
1000ml On

The easiest way I can think of is to define your own control that adds an onRelease event handler:

<?xml version="1.0" encoding="UTF-8"?>
<nifty-controls xmlns="http://nifty-gui.lessvoid.com/nifty-gui">
<controlDefinition name="horizontalSlider" style="nifty-horizontal-slider"
    controller="de.lessvoid.nifty.controls.slider.SliderControl"
    inputMapping="de.lessvoid.nifty.controls.scrollbar.ScrollbarInputMapping">

    <panel style="#panel">
        <interact onMouseWheel="mouseWheel()"/>
        <image style="#left">
            <interact onClickRepeat="upClick()"/>
        </image>
        <image id="#background" style="#background">
            <interact onClick="mouseClick()" onClickMouseMove="mouseClick()"/>
            <image id="#position" style="#position">
                <interact onClick="mouseClick()" onClickMouseMove="mouseClick()"
                    onRelease="onRelease()"/>
                    <!-- ^ ^ ^ ADD THIS ^ ^ ^ -->
            </image>
        </image>
        <image style="#right">
            <interact onClickRepeat="downClick()"/>
        </image>
    </panel>
</controlDefinition>

Note: I just copied the original definition of the SliderControl and added onRelease to the <interact> tag. See: https://github.com/nifty-gui/nifty-gui/blob/1.4/nifty-controls/src/main/resources/nifty-controls/nifty-slider.xml

Now load this new control using this line in your XML:
(insert it after the line which loads nifty-default-controls.xml)

<useControls filename="Interface/NewSlider.xml" />

And then add a method public void onRelease() to your Controller.