ppy / osu-framework

A game framework written with osu! in mind.
MIT License
1.67k stars 419 forks source link

Add custom mouse step and range properties to SliderBar #6376

Open OliBomby opened 2 months ago

OliBomby commented 2 months ago

With this you can implement a slider bar with a user-defined range and precision without putting any constraints on the backing bindable.

It improves the API because you don't have to use the previous API of setting the SliderBar.Current to a BindableNumber with user-defined precision and range. The previous API is bad because the range and precision are hidden because SliderBar.Current is not of type BindableNumber.

In UI I thought it would be be nice to have a slider bar with text input field with higher precision, so you can quickly change a integer value with the sliderbar but use the text field if you need more precision. Example video below using a Slider from WPF. In osu! framework this behaviour is not possible because the text input and slider bar have to share a Bindable and that bindable determines the precision of the slider bar and text input. This PR fixes this by adding a custom mouse precision to the slider bar.

https://github.com/user-attachments/assets/1aa5c8d8-7ae8-414e-83d4-359e7947af5b

The workaround of using two bindables with bi-directional value change callbacks does not work because the slider bar will display the wrong value in the tooltip. https://github.com/ppy/osu/pull/29927#pullrequestreview-2324310898

I also added custom range so you can do stuff like https://github.com/ppy/osu/pull/30482#issuecomment-2453929647 or https://github.com/ppy/osu/pull/29971

The clamping behaviour when the current value is outside the custom range and using keyboard inputs, as outlined in TestCustomRange, is consistent with how it behaves in WPF.

bdach commented 2 months ago

In osu! framework this behaviour is not possible because the text input and slider bar have to share a Bindable and that bindable determines the precision of the slider bar and text input

It's not immediately clear to me why this is an issue exactly? You steer mouse step by changing precision of the bindable. If you don't want the coupling, you can have a loose-coupled setup wherein the slider bindable is not bound to the other bindable but you set up bidirectional or one-directional value change callbacks to propagate the value.

I'm personally going to require a more convincing rationale to try this because the OP is not doing it for me. In particular, consider this: what happens if you set a Step that the underlying bindable cannot support? Something like 0.05 step and 0.1 precision. I imagine things will begin to break down at that point.

OliBomby commented 2 months ago

If you don't want the coupling, you can have a loose-coupled setup wherein the slider bindable is not bound to the other bindable but you set up bidirectional or one-directional value change callbacks to propagate the value.

That setup is actually not as bad as I initially thought. After implementing it the callback loop you create is not hard to fix.