Search Unity

Feature Request C# Pseudo State API

Discussion in 'UI Toolkit' started by Nexer8, Apr 19, 2021.

  1. Nexer8

    Nexer8

    Joined:
    Dec 10, 2017
    Posts:
    271
    Any updates on when a C# pseudo state API can be expected?
     
    MostHated and noobler like this.
  2. antoine-unity

    antoine-unity

    Unity Technologies

    Joined:
    Sep 10, 2015
    Posts:
    780
    This is not something on our radar as far as I know.

    Can we explain a bit how you'd envision this API to work ? If you had the ability to apply USS strings (as you requested here) would it be needed?
     
  3. Nexer8

    Nexer8

    Joined:
    Dec 10, 2017
    Posts:
    271
    What I had in mind was the ability to set the values of different IStyles associated with the VisualElement. Right now we have style, but we could have hoverStyle, activeStyle etc.

    Alternatively we could provide an optional enum value when setting StyleValues:
    Code (CSharp):
    1. enum PseudoState { Inactive, Active, Hover, Focus, Selected, Enabled, Disabled, Root }
    2. void Example()
    3. {
    4.     VisualElement v = new VisualElement();
    5.  
    6.     v.style.width = new StyleLength(new Length(80, LengthUnit.Pixel), PseudoState.Inactive);
    7.     v.style.width = new StyleLength(new Length(100, LengthUnit.Pixel), PseudoState.Hover);
    8. }
    And is this API needed? Yes. The UI looks very static and unresponsive without pseudostates, and even without transition duration, thaving them would greatly improve the feel. At the moment, only applying the new values when a state changes would be enough.
     
  4. antoine-unity

    antoine-unity

    Unity Technologies

    Joined:
    Sep 10, 2015
    Posts:
    780
    Ok, thanks for the detail.

    Can elaborate on why using USS is not suitable for this (except the fact that USS cannot be constructed dynamically at the moment) ?
     
  5. Nexer8

    Nexer8

    Joined:
    Dec 10, 2017
    Posts:
    271
    Because it is part of a bigger system. The elements are procedurally generated, based on instructions from user defined modules. Currently I can generate entire interfaces with the tool, but it is all static (with the exception of events) and the biggest limitation at the moment is that I can not use pseudo states.
     
  6. MostHated

    MostHated

    Joined:
    Nov 29, 2015
    Posts:
    1,235
    ^ This, but also, the USS pseudo state no longer functions once you make a change using C#, which I do all the time for animations. If I animate the background of an item, once it has completed, the previously set USS :hover state no longer functions. USS is great.. for some things, but if the USS functionality is going to be broken when I try to make a temporary change in C#, then it would make sense to have a similar way to fix it / achieve the same result.

    The quickest example I could come up with that properly demonstrates this would be below. At first, the mouse over is using uss:hover state, then I animate the background color, then the hover no longer works. Being able to define these states in C# and reapply them, or similar, would be ideal vs having to do a whole registration process for mouseover and mouseout for each item.



    Thanks,
    -MH
     
  7. antoine-unity

    antoine-unity

    Unity Technologies

    Joined:
    Sep 10, 2015
    Posts:
    780
    Thanks for reviving the thread, I have logged the feature request.
     
    Thygrrr likes this.
  8. Nexer8

    Nexer8

    Joined:
    Dec 10, 2017
    Posts:
    271
    OK, so not sure about how far in development this feature has come or if it has been started on at all, but just want to give a rough idea of what I had in mind:

    First of all, the VisualElement would need a list of strings called pseudoClasses.
    While some pseudo classes such as hover and focus could be added by internal state changes, it would also be possible for users to add their own. They can also set VisualElement.pickingMode to None and add these pseudo classes by themselves. One example would be to add a hover pseudo class to all children of an element when it is hovered.

    The API would give access to a list of C# styles called pseudoStyles which would be the styles responsible for handling pseudo classes.
    Each of these styles would have a list of strings for their pseudoClasses
    If any of the pseudo classes match with any pseudo class of the element, then apply the style and do necessary transitions.

    To manage priorities among pseudo classes (should A be applied before B) you only need to reorganize the pseudoStyles list.

    These suggestions are based on how I myself implemented it in a tool I am working on.
     
  9. andrew-lukasik

    andrew-lukasik

    Joined:
    Jan 31, 2013
    Posts:
    249
    Excuse my naivety, but can't pseudo states be just plain classes? (style resolution-wise)

    Something like
    :active
    or
    :hover
    . It would make it more transparent, easy to script and probably opens up a door for custom pseudo states.
     
    Nexer8 likes this.
  10. Nexer8

    Nexer8

    Joined:
    Dec 10, 2017
    Posts:
    271
    Actually ended up doing this for my own solution. It has all of the benefits you mentioned, like the ability to create custom pseudo-states. I now use this feature for everything, because of how handy it is.
     
    andrew-lukasik likes this.
  11. MekanYsmos

    MekanYsmos

    Joined:
    Dec 28, 2014
    Posts:
    1
    Could you share this feature? Thanks.