Lemur slider commands


#1

So I am learning lemur for the first time, and so far am loving it. But I have a question on the Sliders.
Is there a way to add commands to it like there are on buttons? I am in need of a way to listen for the slider value being changed.

Perhaps something like:

slider.addChangeCommands(new Command<Slider>() {
	@Override
	public void execute(Slider source) {
		System.out.println("Slider value changed: " + source.getModel().getValue());
	}
});

Is there currently any way to do this?


#2

not sure this is the intended way but i do it by getting a versionedReference from the RangedValueModel of the slider

VersionedReference<Double> ref = yourSlider.getModel().createReference();

and you probably are doing it in an app state, so you got an update method you can make use of and check weather the value of that reference has changed

//inside your update() method
if (ref.update()) {
   //value has changed
}

EDIT ofc ref would have to be global or accessible in the update method in another way
hope it helps and greetings from the shire,
samwise


#3

That is a way to do it! Too bad there is not a listener of types, but thank you for the suggestion!


#4

Event based systems have their place but The way Paul set it up with versioned references is a lot more efficient and works with the flow of a game loop. I still use events from time to time for other things like network events but in this case Paul’s approach is better.


#5

Yeah, as said: VersionedReferences are the way to go.

Here is an excerpt from: https://github.com/jMonkeyEngine-Contributions/Lemur/wiki/Modules#versioned-objects

Lemur uses a light-weight approach to watching for changes to properties. Rather than adding listeners to the various controls which notify of changes, Lemur objects provide versioned views of values associated with the widget. This allows the user to store a reference to the versioned value and check if the value has changed in the update loop. This check is very quick (a simple == operator on long values). If a value has changed then the update loop can perform the appropriate operations.
…some code examples
The update method checks if the referenced value has changed and updates the local copy if necessary. This provides a very simple method of reacting to changes without the overhead and pitfalls of a publish/subscribe pattern.

And from the javadoc for VersionedReference: http://jmonkeyengine-contributions.github.io/Lemur/javadoc/Lemur/com/simsilica/lemur/core/VersionedReference.html

Tracks an update version of a VersionedObject and can provide basic change tracking for a caller. Calling code can hold a VersionedReference to some value and call update() to update the local version field and detect if the version has changed since the last check.

This is an upside-down way of doing change notification that does not have the event overhead or listener-leak potential of a typical event/listener framework. It is not appropriate for all cases but can be used in cases where values are often changed frequently and/or it’s ok to ignore stacks of events in favor of the latest value. Common applications are things like sliders, document models, etc. for which some view will update itself only when the watched object changes, but otherwise doesn’t care about the specific granularity of events.

All that being said, if you really really really want to do it the harder way for some reason then you can grab the slider buttons and register a command with them just like any other buttons.