RobustToolbox icon indicating copy to clipboard operation
RobustToolbox copied to clipboard

VVPropEditorEnum blows up on flags enums.

Open PJB3005 opened this issue 4 years ago • 3 comments
trafficstars

PJB3005 avatar Jan 23 '21 17:01 PJB3005

got an example of a flag enum?

PaulRitter avatar Jan 23 '21 22:01 PaulRitter

SolutionContainerCaps

PJB3005 avatar Jan 24 '21 00:01 PJB3005

Encountered this again with CollisionGroup enum.

This is caused because the function described as: f(enum_label) = enum_value is not an injective function. We can have several labels map to the same value in an enum. Scenario:

public enum TestEnum
{
   label0 = 0,
   label1 = 5,
   label2 = label1 // alternatively label2 = 5
}

In this case VVPropEditorEnum blows up as it attempts to map label2 to value 5, and it already has label1 mapped to value 5:

OptionsButton.cs
if (_idMap.ContainsKey(id.Value))
{
    throw new ArgumentException("An item with the same ID already exists.");
}

This can be solved by mapping the enum domain to UI elements, rather than its co-domain. (One enum value can have multiple enum labels; one enum label cannot have multiple enum values)

EDIT: A simpler solution would filter out enum labels with duplicate values - though that certainly makes this system less robust (some enum labels will be missing from UI - this may be confusing depending where this function will be used).


An interesting observation: I am not encountering any problems with this when rendering VV UI for a Content.Server component that has non-injective enums. Surprisingly, this only blows up for me when a Content.Shared component contains the same non-injective enum.

~~Not sure why, requires further investigation.~~

The above is not related to the problem. It seems that for VV UI purposes Content.Server enums are simply always considered ReadOnly, even with ViewVariables(VVAccess.ReadWrite). Since they are read only - all enum labels / values aren't being enumerated, so this issue doesn't occur.

The original problem remains as described.

LordCarve avatar Dec 31 '23 16:12 LordCarve