Elin Decompiled Documentation EA 23.102 Nightly
Loading...
Searching...
No Matches
HexInput.cs
Go to the documentation of this file.
1using System;
2using System.Text.RegularExpressions;
5using UnityEngine;
6using UnityEngine.UI;
7
8public class HexInput : MonoBehaviour
9{
10 [SerializeField]
12
13 [SerializeField]
15
16 private string hexBackup;
17
18 private string currentHex;
19
20 public event Action<Color> ColorSelected = delegate
21 {
22 };
23
24 private void Awake()
25 {
26 input.InputSelected += BackupColor;
27 input.onEndEdit.AddListener(OnInputEndEdit);
28 input.onValueChanged.AddListener(OnInputValueChanged);
29 UiInputField uiInputField = input;
30 uiInputField.onValidateInput = (InputField.OnValidateInput)Delegate.Combine(uiInputField.onValidateInput, new InputField.OnValidateInput(OnValidateInput));
31 input.characterLimit = (colorPicker.IsAlphaBeingUsed() ? 8 : 6);
32 }
33
34 private char OnValidateInput(string text, int charIndex, char addedChar)
35 {
36 if (!Regex.IsMatch(addedChar.ToString(), "[A-Fa-f0-9]"))
37 {
38 return '\0';
39 }
40 return addedChar;
41 }
42
43 private void BackupColor()
44 {
45 hexBackup = input.text;
46 }
47
48 private void OnInputValueChanged(string value)
49 {
50 if (input.isFocused)
51 {
52 switch (value.Length)
53 {
54 default:
55 return;
56 case 0:
57 case 1:
58 case 2:
59 case 5:
60 case 7:
62 return;
63 case 3:
64 currentHex = string.Concat(value[0], value[0], value[1], value[1], value[2], value[2], GetAlpha("FF"));
65 break;
66 case 4:
67 currentHex = string.Concat(value[0], value[0], value[1], value[1], value[2], value[2], GetAlpha(string.Concat(value[3], value[3])));
68 break;
69 case 6:
70 currentHex = value + GetAlpha("FF");
71 break;
72 case 8:
73 currentHex = value;
74 break;
75 }
76 this.ColorSelected(GetColorByHex(currentHex));
77 }
78 }
79
80 private string GetAlpha(string alpha)
81 {
83 {
84 return "";
85 }
86 return alpha;
87 }
88
89 private void OnInputEndEdit(string value)
90 {
91 if (Input.GetKeyDown(KeyCode.Escape))
92 {
94 }
95 input.text = currentHex;
96 this.ColorSelected(GetColorByHex(currentHex));
97 }
98
99 private Color GetColorByHex(string hex)
100 {
101 return Colorist.HexToColor(hex);
102 }
103
104 public void SelectColor(Color color)
105 {
106 if (!input.isFocused)
107 {
108 string text = (currentHex = Colorist.ColorToHex(color));
109 input.text = text;
110 }
111 }
112}
static Color HexToColor(string hex)
Definition: Colorist.cs:237
static string ColorToHex(Color32 color)
Definition: Colorist.cs:232
ColorPicker colorPicker
Definition: HexInput.cs:11
void SelectColor(Color color)
Definition: HexInput.cs:104
string currentHex
Definition: HexInput.cs:18
Action< Color > ColorSelected
Definition: HexInput.cs:20
void BackupColor()
Definition: HexInput.cs:43
Color GetColorByHex(string hex)
Definition: HexInput.cs:99
string GetAlpha(string alpha)
Definition: HexInput.cs:80
UiInputField input
Definition: HexInput.cs:14
void OnInputEndEdit(string value)
Definition: HexInput.cs:89
string hexBackup
Definition: HexInput.cs:16
void OnInputValueChanged(string value)
Definition: HexInput.cs:48
void Awake()
Definition: HexInput.cs:24
char OnValidateInput(string text, int charIndex, char addedChar)
Definition: HexInput.cs:34