Skip to content

Added an optional prefix and suffix for [Dropdown] - #286

Closed
nitz wants to merge 2 commits into
dbrizov:masterfrom
nitz:add-dropdown-prefix-suffix
Closed

nitz wants to merge 2 commits into
dbrizov:masterfrom
nitz:add-dropdown-prefix-suffix

Conversation

@nitz

@nitz nitz commented Nov 19, 2021

Copy link
Copy Markdown

This just optionally modifies the text being drawn in the dropdown, so that the user can easily prefix or suffix options with "decoration".

My use case was to decorate a list of baud rates, but not wanting to have the entire setup with a DropdownList<T>:

image

image

However, the prefix and suffix decorations do still work with the DropdownList<T>:

image

This just optionally modifies the text being drawn in the dropdown, so that the user can easily prefix or suffix options with "decoration".
@nitz
nitz force-pushed the add-dropdown-prefix-suffix branch from af0d396 to f06466d Compare June 14, 2022 15:40
@nitz

nitz commented Jun 14, 2022

Copy link
Copy Markdown
Author

Updated this PR to not conflict with base.

@rhys-vdw

Copy link
Copy Markdown
Contributor

FWIW I feel this is too specific to require its own first-class support. I think it would be more flexible to add a constructor for DropdownList<T> that accepts an IEnumerable<KeyValuePair<string, T>> so it can be used like this:

private readonly int[] _baudRates { 300, 1200, /* ... */ };
private DropdownList<int> BaudRateOptions =>
  new DropDownList<int>(_baudRates.Select(b => new KeyValuePair($"{b} baud", b));

@nitz

nitz commented Jun 15, 2022

Copy link
Copy Markdown
Author

I like that for DropDownList in general — definitely would be an okay way to keep a tighter mapping of typed options to dropdown choice text.

However my approach here was to keep the editor-specific metadata out of the class definition itself. By having it only live in the attribute, it let me keep the class "cleaner" in the sense it keeps the members related to that choice as only needing the serialized field(s) plus the options array.

I realize the distinction there is potentially quite slim; I'm just much more a fan of having as much of my inspector-related data to live in attributes rather than inspector-specific properties that live on the class directly.

@rhys-vdw

rhys-vdw commented Jun 16, 2022 •

Copy link
Copy Markdown
Contributor

Could always do:

#if UNITY_EDITOR
private readonly int[] _baudRates { 300, 1200, /* ... */ };
private DropdownList<int> BaudRateOptions =>
  new DropDownList<int>(_baudRates.Select(b => new KeyValuePair($"{b} baud", b));
[Dropdown(nameof(BaudRateOptions)]
#endif
[SerializeField] int _baudRate = 300;

Or even:

#if UNITY_EDITOR
private DropdownList<int> BaudRateOptions => MyNaUtility.Suffix(new [] {
  300,
  1200,
  // ...
}, " baud");
[Dropdown(nameof(BaudRateOptions)]
#endif
[SerializeField] int _baudRate = 300;

The reason I'm proposing this is because suffix and prefix are just a small subset of value -> label string conversions that a user could conceivably want.

@nitz

nitz commented Jul 19, 2022

Copy link
Copy Markdown
Author

The reason I'm proposing this is because suffix and prefix are just a small subset of value -> label string conversions that a user could conceivably want.

I had to give some real thought to this, because you're completely spot on. I think my affinity for doing it the way I had was due mostly to a desire to keep the user's interface to be mostly similar to a non-modified string conversion. (And to not need #if UNITY_EDITOR sections if at all possible.) Then a more apparent option hit me, despite having been sitting in front of my face the whole time:

Allowing the user to provide an the actual format string lets them provide a mutation that includes any sort of custom formatting, but keeps the attribute usage clean and straightforward. It definitely doesn't provide as much potential flexibility as your KVP solution would, but I think that's a fair trade given the addition of a single, optional attribute parameter.

I pushed a commit that changes the implementation to this.

@nitz nitz changed the title Added an optional prefix and suffix for [Dropdown] Added an optional format string for [Dropdown] Jul 19, 2022
@nitz nitz closed this Jul 19, 2022
@nitz
nitz deleted the add-dropdown-prefix-suffix branch July 19, 2022 13:27
@nitz nitz changed the title Added an optional format string for [Dropdown] Added an optional prefix and suffix for [Dropdown] Jul 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants