1

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.

Zeebats
  • 480
  • 7
  • 22
  • Could you explain why you need this event? Reacting on the end of a sliding operation is not that easy. Mainly because there are more then one way to perform that operation. You can drag the handle, click on the track, use the keyboard. How is "stop sliding" defined when you do anything but dragging the handle? – Nitram Jan 12 '16 at 10:58
  • The reason i need this is becuase i have to send the value of the slider over a socket to a controller. If i just use the onSliderChange event, the value would be send multiple times, so i thougd it would be better that when the user stops sliding, the value is send once. – Zeebats Jan 12 '16 at 12:41
  • Wouldn't it be safer to build in a fixed timeout? If the value was changed and does not change anymore for `x` milliseconds the updated value is send? This would cover changes by mouse clicks/presses on the track and keyboard changes as well. – Nitram Jan 12 '16 at 15:12

1 Answers1

0

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.

1000ml
  • 864
  • 6
  • 14