Nifty Slider Controls

I’m trying to implement a slider in my program. I’ve succeeded in getting the slider on the screen and the position .png image is moving correctly when I click left and right. However, I’m trying to get onClick events going, so that I can run certain methods in my program when the left or right slider buttons are clicked.

I’m using nifty-slider.xml and the SliderControl.java class

here is my own .xml implementing the nifty-slider-style.xml and nifty slider.xml control class

                      How do I get the clicks to trigger my own methods?

This has turned into a new question… how the hell do I paste xml code?

@testandrill said: This has turned into a new question.... how the hell do I paste xml code?

The not-so-easiest solution is to replace
[java]
< with & lt ; (no spaces)
[/java]
and
[java]
> with & gt ; (no spaces).
[/java]
However, if you edit your post, you’ll have to redo each. =(

Unfortunately you can’t atm due to some forum problems. Post it to pastebin or similar and link from here.

Thank you

here is my xml file

There are listeners you can add using the nifty event bus, although I’m not sure off hand what events are generated by sliders.

Looks like this:

[java]
@NiftyEventSubscriber(pattern = “.*Slider”) // use can use whatever you want really. It goes with the control’s ID
public void onSliderChangedEvent(final String id, final SliderChangedEvent event) {
if (event.getValue() == 0) {
screen.findNiftyControl(“someChkbox”, CheckBox.class).uncheck();
}
// That’s what I got in the remaining Nifty code I have.
}
[/java]

event.getValue() will return the value of the slider obviously, but except for that you’d need to check SliderChangedEvent.

You can match by pattern to have one listener pick up multiple controls or you can match directly on id.

@zarch said: You can match by pattern to have one listener pick up multiple controls or you can match directly on id.

Exactly. I was using IDs of musicSlider and soundSlider. By using a pattern of “.*Slider” it captured activities by all sliders IDs ending with “Slider”.

@madjack said: Looks like this:

[java]
@NiftyEventSubscriber(pattern = “.*Slider”) // use can use whatever you want really. It goes with the control’s ID
public void onSliderChangedEvent(final String id, final SliderChangedEvent event) {
if (event.getValue() == 0) {
screen.findNiftyControl(“someChkbox”, CheckBox.class).uncheck();
}
// That’s what I got in the remaining Nifty code I have.
}
[/java]

madjack, could I get a little more context behind this code? I’m curious as to where to actually call the “onSliderChangedEvent” method

It is using the nifty event bus feature. If you create methods with certain signatures and attach the right annotations and then register the object for annotations with nifty then nifty automatically calls them.

Read the nifty bible section on the event bus for details and more examples.

@zarch said: It is using the nifty event bus feature. If you create methods with certain signatures and attach the right annotations and then register the object for annotations with nifty then nifty automatically calls them.

Read the nifty bible section on the event bus for details and more examples.

I wanted to answer “magic” but @zarch's answer is also good. :wink:

Got it working, thanks guys. Any other tips on how to manipulate the event.getValue( ) method?

In other words, can I set ranges for events (such as zooming the camera)? instead of setting an event for 1,2,3,4,5,6,…all the way up to whatever the max for nifty sliders is

EDIT: did some tests, figured out the max is 100.

also, is there any way to set the starting location of the slider? Right now on my vertical slider it starts at the top and I want it to start at the bottom

Yeah, there are setting method calls you can make against the slider control. Set the start, min, max, range etc.