Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Context
The current UI system automatically generates IDs for each element to handle focus/other stateful aspects. For many elements, the IDs are generated based on the address of the input variable passed in (for instance the text box's string, the checkbox state, slider value, etc). This doesn't work once the SplashKit translation layers are involved, leading to state being incorrectly shared between different UI elements.
This PR attempts to improve this by keeping track of each element's index within its context (panel/popup/inset/treenode). Rather than using the input variable's address for the ID, it just uses an incrementing number that is contained to that context.
How it works
This interacts with MicroUI's own context stack to work properly. MicroUI already contains a context stack, which contains an ID for each panel/popup/inset/treenode in the current stack, generated from their label (which is required).
When we generate IDs for the UI elements via
mu_get_id, it uses the ID for the current context as a base.So all we need to do is keep track of an integer incrementing from 0 within the context, and provide that to
mu_get_idrather than the input value's address - MicroUI will handle making this integer unique in each context.Known Issues
This works well for most types of interfaces - the only edge cases I can think of are when the interface changes during an interaction. For instance when a text box is focused (let's say its ID is
2), if a different text box appears above it within the same context while the user is typing, focus will switch to that text box (since that text box now has ID2, while the one the user was typing in has become3). This can be seen in theID Test 2section of the UI test.Future work can involve anchoring the IDs when labels are known (since text inputs, checkboxes, etc, may have a label).
Type of change
How Has This Been Tested?
The existing UI test has been checked to ensure things still work. Checking that the state of unrelated elements don't affect each other (unless in the case above) has been tested. The fail case above has also been added as a test to demonstrate it.
This hasn't been tested when ran through translation layers however - I can't foresee any issues (since it isn't using the variable addresses anymore), but that doesn't mean there aren't any 😅
Testing Checklist
Checklist