MixedRealityToolkit / MixedRealityToolkit-Unity

This repository holds the third generation of the Mixed Reality Toolkit for Unity. The latest version of the MRTK can be found here.
BSD 3-Clause "New" or "Revised" License
339 stars 86 forks source link

[FEATURE REQUEST] Allow ToggleCollection AllowSwitchOff to work in start mode too #632

Open Maesla opened 5 months ago

Maesla commented 5 months ago

Describe the problem

ToggleCollection has a flag to allow switch off, but it doesn't have a flag to start all the toggle buttons in an off state

Describe the solution you'd like

To be possible to start the toggle group with all the elements off, in the same way it is possible during the execution

Possibly changing this line would be enough

                // Force set initial selection in the toggle collection at start
                //if (CurrentIndex >= 0 && CurrentIndex < Toggles.Count) => old
                 if (CurrentIndex >= 0 && CurrentIndex < Toggles.Count && !startSwitchOff) //=> new
                {
                    SetSelection(CurrentIndex, true);
                    Toggles[CurrentIndex].ForceSetToggled(true);
                }

https://github.com/MixedRealityToolkit/MixedRealityToolkit-Unity/blob/9ae2140007b47fd081d9f5605cc94bbd2131e2c5/org.mixedrealitytoolkit.uxcore/Toggle/ToggleCollection.cs#L141

Describe alternatives you've considered

N/A

Additional context

If we allow to be all the toggles off as a valid state, it should be valid and possible too in the initial state.

PS: ToggleSelectedEvent would be nice to have the information of the previous index and also be raised when all the toggles are off (maybe with a -1 as index?)

BenediktTobias commented 1 month ago

Unity Editor

You can start with all elements of a toggle group deselected by setting CurrentIndex to something like -1 (anything outside the valid range of 0 to (Toggles.Count - 1)).

By doing so the condition in line 141 (specifically CurrentIndex >= 0) is not met and therefore no element will be toggled / selected during Start().

https://github.com/MixedRealityToolkit/MixedRealityToolkit-Unity/blob/b2fba1fbbc180b3118a03b99f7827fbe4d015b0c/org.mixedrealitytoolkit.uxcore/Toggle/ToggleCollection.cs#L140-L145


Special case during runtime

When setting the Toggles list during runtime CurrentIndex is clamped to the valid range of indices and the corresponding toggle is selected. https://github.com/MixedRealityToolkit/MixedRealityToolkit-Unity/blob/b2fba1fbbc180b3118a03b99f7827fbe4d015b0c/org.mixedrealitytoolkit.uxcore/Toggle/ToggleCollection.cs#L46-L47 To achieve a state where all toggles are deselected after setting the list through code one must disable the currently selected button by using e.g. ForceSetToggled(false) or ForceSetToggled(false, false) to suppress the resulting toggle events. Only the initial toggle selection event must be handled or discarded using a condition in the called function.