-
Notifications
You must be signed in to change notification settings - Fork 5
Add a Color Input #74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
flibber-hk
merged 12 commits into
silksong-modding:main
from
kaycodes13:feature/colour-input
Apr 29, 2026
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d4da4ce
add TextModels.ForHexColors() and ColorInput element
kaycodes13 325b532
add generator for ColorInput & test for automatic menu creation
kaycodes13 d8f746b
Merge branch 'silksong-modding:main' into feature/colour-input
kaycodes13 b681937
bump version to v0.6.1
kaycodes13 27a3415
fix mixed spacing in auto menu test
kaycodes13 9849f86
ColorInput now gives access to swatch's RectTransform
kaycodes13 8368530
Plugin proxy test
flibber-hk f5e9e90
Clean up merge
flibber-hk 9a216c4
Un-bump version
flibber-hk 473f5d0
ColorInput swatch now resizes when font size is set
kaycodes13 cd3c9dc
add RGBColorValues config constraint, which auto-ColorInputs accomodate
kaycodes13 b2bfce0
Spaces
flibber-hk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| using Silksong.ModMenu.Internal; | ||
| using Silksong.ModMenu.Models; | ||
| using UnityEngine; | ||
| using UnityEngine.UI; | ||
|
|
||
| namespace Silksong.ModMenu.Elements; | ||
|
|
||
| /// <summary> | ||
| /// Selectable element that accepts <see cref="Color"/> input in 3, 6, or 8 character hex strings. | ||
| /// Includes a preview swatch beside the hex code. | ||
| /// </summary> | ||
| public class ColorInput : TextInput<Color> | ||
| { | ||
| /// <summary> | ||
| /// The relative size of the Swatch to the choice text when the input was first created. | ||
| /// Used to automatically resize the Swatch when <see cref="SetFontSizes"/> is called. | ||
| /// </summary> | ||
| private readonly float swatchSizeMultiplier; | ||
|
|
||
| /// <summary> | ||
| /// Construct a color input with no description. | ||
| /// </summary> | ||
| public ColorInput(LocalizedText label) | ||
| : this(label, "") { } | ||
|
|
||
| /// <summary> | ||
| /// Construct a color input with a description. | ||
| /// </summary> | ||
| public ColorInput(LocalizedText label, LocalizedText description) | ||
| : base(label, TextModels.ForHexColors(), description) | ||
| { | ||
| Container.name = $"{label.Canonical} Color Input"; | ||
| InputField.contentType = InputField.ContentType.Custom; | ||
| InputField.onValidateInput = HexValidation; | ||
| ApplyDefaultColors = true; | ||
| Format = InputFormat.RGBA; | ||
|
|
||
| Swatch = MenuPrefabs.Get().NewColorSwatch().RectTransform; | ||
| Swatch.SetParent(InputField.textComponent.transform, false); | ||
| Swatch.gameObject.SetActive(true); | ||
| SwatchFill = Swatch.Find("Fill").GetComponent<Image>(); | ||
| SwatchOutline = Swatch.Find("Outline").GetComponent<Image>(); | ||
| InvalidValueIndicator = Swatch.Find("Invalid Indicator").GetComponent<Text>(); | ||
|
|
||
| swatchSizeMultiplier = Swatch.rect.height / InputField.textComponent.preferredHeight; | ||
|
|
||
| OnTextValueChanged += _ => | ||
| { | ||
| SwatchFill.color = Value; | ||
| State = TextModel.IsTextValid ? ElementState.DEFAULT : ElementState.INVALID; | ||
| InvalidValueIndicator.enabled = !TextModel.IsTextValid; | ||
| }; | ||
|
|
||
| Value = Color.clear; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Whether or not this input accepts 8-character RGBA strings. | ||
| /// </summary> | ||
| public InputFormat Format | ||
| { | ||
| get => field; | ||
| set | ||
| { | ||
| field = value; | ||
| InputField.characterLimit = (int)field; | ||
|
|
||
| // force the field to re-clamp the length of the current text value | ||
| string val = InputField.text; | ||
| InputField.SetTextWithoutNotify(""); | ||
| InputField.text = val; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Semantic input formats for <see cref="ColorInput"/>s. | ||
| /// </summary> | ||
| public enum InputFormat | ||
| { | ||
| /// <summary> | ||
| /// The input accepts at most 6-character RGB hex codes. | ||
| /// </summary> | ||
| RGB = 6, | ||
|
|
||
| /// <summary> | ||
| /// The input accepts at most 8-character RGBA hex codes. | ||
| /// </summary> | ||
| RGBA = 8, | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// The unity component that controls the size and position of the preview swatch. | ||
| /// </summary> | ||
| public readonly RectTransform Swatch; | ||
|
|
||
| /// <summary> | ||
| /// The unity component for the filled area of the preview swatch. | ||
| /// </summary> | ||
| public readonly Image SwatchFill; | ||
|
|
||
| /// <summary> | ||
| /// The unity component for the outline around the preview swatch. | ||
| /// </summary> | ||
| public readonly Image SwatchOutline; | ||
|
|
||
| /// <summary> | ||
| /// The unity component for the symbol that appears in the preview swatch | ||
| /// to visually indicate an invalid value. | ||
| /// </summary> | ||
| public readonly Text InvalidValueIndicator; | ||
|
|
||
| /// <inheritdoc/> | ||
| /// <remarks> | ||
| /// This will also adjust the size and position of the <see cref="Swatch"/>. | ||
| /// </remarks> | ||
| public override void SetFontSizes(FontSizes fontSizes) | ||
| { | ||
| base.SetFontSizes(fontSizes); | ||
| float size = InputField.textComponent.preferredHeight * swatchSizeMultiplier; | ||
| Swatch.sizeDelta = Vector2.one * size; | ||
| Swatch.anchoredPosition = Swatch.anchoredPosition with { x = -0.5f * size }; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// <see cref="InputField"/> validation for hex codes; only accepts characters a-fA-F0-7. | ||
| /// </summary> | ||
| static char HexValidation(string input, int index, char addedChar) => | ||
| $"{addedChar}".TryParseHex(out _) ? char.ToUpper(addedChar) : '0'; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.