mirror of
https://github.com/DerTyp7/defrain-shooter-unity.git
synced 2025-10-29 12:52:07 +01:00
finished inputfield prefab
This commit is contained in:
1016
Assets/Prefabs/UI/InputField.prefab
Normal file
1016
Assets/Prefabs/UI/InputField.prefab
Normal file
File diff suppressed because it is too large
Load Diff
7
Assets/Prefabs/UI/InputField.prefab.meta
Normal file
7
Assets/Prefabs/UI/InputField.prefab.meta
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 2bcd316cc5d96504197c7bd6f749e4d9
|
||||||
|
PrefabImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -7,54 +7,167 @@ using TMPro;
|
|||||||
|
|
||||||
public class HomeSceneInputField : MonoBehaviour, IPointerClickHandler
|
public class HomeSceneInputField : MonoBehaviour, IPointerClickHandler
|
||||||
{
|
{
|
||||||
GameObject marker;
|
[SerializeField] float markerHeight = 20.0f;
|
||||||
GameObject focusText;
|
[SerializeField] float transitionStartTime = 0.2f;
|
||||||
GameObject helpText;
|
[SerializeField] float transitionEndTime = 0.2f;
|
||||||
GameObject underline;
|
|
||||||
|
[SerializeField] Color helpTextOriginColor;
|
||||||
|
[SerializeField] Color helpTextColor;
|
||||||
|
[SerializeField] Color focusTextOriginColor;
|
||||||
|
[SerializeField] Color focusTextColor;
|
||||||
|
|
||||||
|
[SerializeField] float helpTextEndPosition = -25f;
|
||||||
|
[SerializeField] float focusTextEndPosition = 20f;
|
||||||
|
|
||||||
|
[Header("GameObjects")]
|
||||||
|
[SerializeField] GameObject marker;
|
||||||
|
[SerializeField] GameObject focusText;
|
||||||
|
[SerializeField] GameObject helpText;
|
||||||
|
[SerializeField] GameObject underline;
|
||||||
|
[SerializeField] GameObject placeholder;
|
||||||
|
|
||||||
|
TMP_InputField inputField;
|
||||||
|
|
||||||
bool isFocused = false;
|
bool isFocused = false;
|
||||||
bool transitionStartDone = false;
|
bool isActive = false;
|
||||||
bool transitionEndDone = false;
|
bool transitionCycleStarted = false;
|
||||||
|
|
||||||
|
float transitionStartTimePassed = 0;
|
||||||
|
float transitionEndTimePassed = 0;
|
||||||
|
|
||||||
|
|
||||||
void Start()
|
void Start()
|
||||||
{
|
{
|
||||||
marker = gameObject.transform.Find("Marker").gameObject;
|
inputField = gameObject.GetComponent<TMP_InputField>();
|
||||||
focusText = gameObject.transform.Find("FocusText").gameObject;
|
InputValidator validator = gameObject.GetComponent<InputValidator>();
|
||||||
helpText = gameObject.transform.Find("HelpText").gameObject;
|
|
||||||
underline = gameObject.transform.Find("Underline").gameObject;
|
if (gameObject.GetComponent<InputValidator>() != null)
|
||||||
|
{
|
||||||
|
helpText.GetComponent<TMP_Text>().text = gameObject.GetComponent<InputValidator>().GetHelpText();
|
||||||
|
focusText.GetComponent<TMP_Text>().text = "Enter " + validator.GetValidatorTypeName();
|
||||||
|
placeholder.GetComponent<TMP_Text>().text = "Enter " + validator.GetValidatorTypeName() + "...";
|
||||||
|
|
||||||
|
if(validator.InputType == InputValidator.TypeOfInput.Port) // Because PORT has a default value and so there is text in the inputfield by start
|
||||||
|
{
|
||||||
|
isActive = true;
|
||||||
|
focusText.GetComponent<TMP_Text>().color = focusTextColor;
|
||||||
|
|
||||||
|
RectTransform focusTextRectTranform = focusText.GetComponent<RectTransform>();
|
||||||
|
focusTextRectTranform.anchoredPosition = new Vector2(focusTextRectTranform.anchoredPosition.x, focusTextEndPosition);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Update()
|
void Update()
|
||||||
{
|
{
|
||||||
if (!gameObject.GetComponent<TMP_InputField>().isFocused)
|
//UnFocus && isActive
|
||||||
|
if (!inputField.isFocused)
|
||||||
{
|
{
|
||||||
isFocused = false;
|
isFocused = false;
|
||||||
|
|
||||||
if (transitionEndDone)
|
if (inputField.text != "")
|
||||||
{
|
{
|
||||||
transitionEndDone = false;
|
isActive = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
isActive = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#region transition
|
||||||
|
if (!transitionCycleStarted && isFocused) // Start Transition
|
||||||
|
{
|
||||||
|
StartTransition();
|
||||||
|
}
|
||||||
|
else if(transitionCycleStarted && !isFocused) // End Transition
|
||||||
|
{
|
||||||
|
EndTransition();
|
||||||
|
}
|
||||||
|
else if(!transitionCycleStarted && !isFocused) // Start cancelled by user
|
||||||
|
{
|
||||||
|
|
||||||
|
transitionStartTimePassed = 0;
|
||||||
|
transitionEndTimePassed = 0;
|
||||||
|
|
||||||
|
RectTransform markerRectTranform = marker.GetComponent<RectTransform>();
|
||||||
|
markerRectTranform.sizeDelta = new Vector2(markerRectTranform.rect.width, 0);
|
||||||
|
|
||||||
|
helpText.GetComponent<TMP_Text>().color = helpTextOriginColor;
|
||||||
|
|
||||||
|
if (!isActive)
|
||||||
|
focusText.GetComponent<TMP_Text>().color = focusTextOriginColor;
|
||||||
|
|
||||||
if (!transitionStartDone)
|
|
||||||
{
|
|
||||||
marker.SetActive(true);
|
|
||||||
focusText.SetActive(true);
|
|
||||||
helpText.SetActive(true);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
marker.SetActive(false);
|
|
||||||
focusText.SetActive(false);
|
|
||||||
helpText.SetActive(false);
|
|
||||||
}
|
}
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnPointerClick(PointerEventData eventData)
|
public void OnPointerClick(PointerEventData eventData)
|
||||||
{
|
{
|
||||||
isFocused = true;
|
if (!isFocused)
|
||||||
transitionStartDone = false;
|
{
|
||||||
|
isFocused = true;
|
||||||
|
transitionCycleStarted = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#region transitions
|
||||||
|
void StartTransition()
|
||||||
|
{
|
||||||
|
transitionStartTimePassed += Time.deltaTime;
|
||||||
|
RectTransform markerRectTranform = marker.GetComponent<RectTransform>();
|
||||||
|
markerRectTranform.sizeDelta = new Vector2(markerRectTranform.rect.width, Mathf.Lerp(0, markerHeight, transitionStartTimePassed / transitionStartTime));
|
||||||
|
|
||||||
|
//Help Text / Focus text
|
||||||
|
helpText.GetComponent<TMP_Text>().color = Color.Lerp(helpTextOriginColor, helpTextColor, transitionStartTimePassed / transitionStartTime);
|
||||||
|
if (!isActive)
|
||||||
|
focusText.GetComponent<TMP_Text>().color = Color.Lerp(focusTextOriginColor, focusTextColor, transitionStartTimePassed / transitionStartTime);
|
||||||
|
|
||||||
|
RectTransform helpTextRectTranform = helpText.GetComponent<RectTransform>();
|
||||||
|
helpTextRectTranform.anchoredPosition = new Vector2(helpTextRectTranform.anchoredPosition.x, Mathf.Lerp(helpTextEndPosition / 2, helpTextEndPosition, transitionStartTimePassed / transitionStartTime));
|
||||||
|
|
||||||
|
RectTransform focusTextRectTranform = focusText.GetComponent<RectTransform>();
|
||||||
|
|
||||||
|
if (!isActive)
|
||||||
|
focusTextRectTranform.anchoredPosition = new Vector2(focusTextRectTranform.anchoredPosition.x, Mathf.Lerp(focusTextEndPosition / 2, focusTextEndPosition, transitionStartTimePassed / transitionStartTime));
|
||||||
|
|
||||||
|
|
||||||
|
if (transitionStartTimePassed >= transitionStartTime)
|
||||||
|
{
|
||||||
|
transitionCycleStarted = true;
|
||||||
|
transitionStartTimePassed = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void EndTransition()
|
||||||
|
{
|
||||||
|
transitionEndTimePassed += Time.deltaTime;
|
||||||
|
RectTransform rectTranform = marker.GetComponent<RectTransform>();
|
||||||
|
rectTranform.sizeDelta = new Vector2(rectTranform.rect.width, Mathf.Lerp(markerHeight, 0, transitionEndTimePassed / transitionEndTime));
|
||||||
|
|
||||||
|
|
||||||
|
//Help Text / Focus text
|
||||||
|
helpText.GetComponent<TMP_Text>().color = Color.Lerp(focusTextColor, helpTextOriginColor, transitionEndTimePassed / transitionEndTime);
|
||||||
|
|
||||||
|
if(!isActive)
|
||||||
|
focusText.GetComponent<TMP_Text>().color = Color.Lerp(focusTextColor, focusTextOriginColor, transitionEndTimePassed / transitionEndTime);
|
||||||
|
|
||||||
|
|
||||||
|
RectTransform helpTextRectTranform = helpText.GetComponent<RectTransform>();
|
||||||
|
helpTextRectTranform.anchoredPosition = new Vector2(helpTextRectTranform.anchoredPosition.x, Mathf.Lerp(helpTextEndPosition, helpTextEndPosition / 2, transitionEndTimePassed / transitionEndTime));
|
||||||
|
|
||||||
|
if (!isActive)
|
||||||
|
{
|
||||||
|
RectTransform focusTextRectTranform = focusText.GetComponent<RectTransform>();
|
||||||
|
focusTextRectTranform.anchoredPosition = new Vector2(focusTextRectTranform.anchoredPosition.x, Mathf.Lerp(focusTextEndPosition, focusTextEndPosition / 2, transitionEndTimePassed / transitionEndTime));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (transitionEndTimePassed >= transitionEndTime)
|
||||||
|
{
|
||||||
|
transitionCycleStarted = false;
|
||||||
|
transitionEndTimePassed = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,7 +26,16 @@ public class InputValidator : MonoBehaviour
|
|||||||
|
|
||||||
if(InputType == TypeOfInput.Port)
|
if(InputType == TypeOfInput.Port)
|
||||||
{
|
{
|
||||||
|
inputField.contentType = TMP_InputField.ContentType.IntegerNumber;
|
||||||
inputField.text = "7777";
|
inputField.text = "7777";
|
||||||
|
}else if(InputType == TypeOfInput.Username)
|
||||||
|
{
|
||||||
|
inputField.contentType = TMP_InputField.ContentType.Alphanumeric;
|
||||||
|
inputField.characterLimit = 25;
|
||||||
|
}
|
||||||
|
else if(InputType== TypeOfInput.IP)
|
||||||
|
{
|
||||||
|
inputField.contentType = TMP_InputField.ContentType.Alphanumeric;
|
||||||
}
|
}
|
||||||
|
|
||||||
inputField.onValueChanged.AddListener(delegate { ValueChangeCheck(); });
|
inputField.onValueChanged.AddListener(delegate { ValueChangeCheck(); });
|
||||||
@@ -84,54 +93,54 @@ public class InputValidator : MonoBehaviour
|
|||||||
|
|
||||||
if(InputType == TypeOfInput.Port)
|
if(InputType == TypeOfInput.Port)
|
||||||
{//0 - 65535
|
{//0 - 65535
|
||||||
foreach (char c in inputField.text)
|
|
||||||
|
if (inputField.text[0] == '0')
|
||||||
{
|
{
|
||||||
if (!Char.IsDigit(c))
|
inputField.text = "";
|
||||||
{
|
|
||||||
inputField.text = inputField.text.Remove(inputField.text.LastIndexOf(c));
|
|
||||||
}
|
|
||||||
else if (int.Parse(inputField.text) > 65535)
|
|
||||||
{
|
|
||||||
inputField.text = "65535";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (int.Parse(inputField.text) > 65535)
|
||||||
}
|
|
||||||
|
|
||||||
// USERNAME
|
|
||||||
if (InputType == TypeOfInput.Username)
|
|
||||||
{
|
|
||||||
|
|
||||||
foreach (char c in inputField.text)
|
|
||||||
{
|
{
|
||||||
if (!Char.IsLetter(c))
|
inputField.text = "65535";
|
||||||
{
|
|
||||||
if (!Char.IsDigit(c))
|
|
||||||
{
|
|
||||||
inputField.text = inputField.text.Remove(inputField.text.LastIndexOf(c));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if(inputField.text.Length > 25)
|
|
||||||
{
|
|
||||||
string tempText = "";
|
|
||||||
|
|
||||||
|
|
||||||
for(int c = 0; c < inputField.text.Length; c++)
|
|
||||||
{
|
|
||||||
if(c < 25)
|
|
||||||
{
|
|
||||||
tempText += inputField.text[c];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
inputField.text = tempText;
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public string GetHelpText()
|
||||||
|
{
|
||||||
|
string helpText = "no help text defined";
|
||||||
|
|
||||||
|
if(InputType == TypeOfInput.IP)
|
||||||
|
{
|
||||||
|
helpText = "Only numeric IP-Addresses";
|
||||||
|
}else if(InputType == TypeOfInput.Username)
|
||||||
|
{
|
||||||
|
helpText = "Only Numbers and Letters";
|
||||||
|
}else if(InputType == TypeOfInput.Port)
|
||||||
|
{
|
||||||
|
helpText = "Only Numbers (max: 65535)";
|
||||||
|
}
|
||||||
|
|
||||||
|
return helpText;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string GetValidatorTypeName()
|
||||||
|
{
|
||||||
|
string name = "text";
|
||||||
|
|
||||||
|
if (InputType == TypeOfInput.IP)
|
||||||
|
{
|
||||||
|
name = "IP-Address";
|
||||||
|
}
|
||||||
|
else if (InputType == TypeOfInput.Username)
|
||||||
|
{
|
||||||
|
name = "Username";
|
||||||
|
}
|
||||||
|
else if (InputType == TypeOfInput.Port)
|
||||||
|
{
|
||||||
|
name = "Port";
|
||||||
|
}
|
||||||
|
|
||||||
|
return name;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user