KSP-Conformal-Decals/Source/ConformalDecals/UI/TextEntryController.cs

148 lines
4.9 KiB
C#
Raw Normal View History

2020-07-21 07:07:18 +00:00
using System;
2020-07-16 01:12:50 +00:00
using ConformalDecals.Text;
using TMPro;
2020-07-13 05:38:37 +00:00
using UnityEngine;
2020-07-21 07:07:18 +00:00
using UnityEngine.Events;
2020-07-16 01:12:50 +00:00
using UnityEngine.UI;
2020-07-13 05:38:37 +00:00
namespace ConformalDecals.UI {
public class TextEntryController : MonoBehaviour {
2020-07-21 07:07:18 +00:00
[Serializable]
public class TextUpdateEvent : UnityEvent<DecalText> { }
2020-07-20 04:12:48 +00:00
2020-07-23 05:37:16 +00:00
[SerializeField] public TextUpdateEvent onTextUpdate = new TextUpdateEvent();
2020-07-20 04:12:48 +00:00
2020-07-16 01:12:50 +00:00
[SerializeField] private Selectable _textBox;
2020-07-20 04:12:48 +00:00
[SerializeField] private Button _fontButton;
2020-07-16 01:12:50 +00:00
[SerializeField] private Slider _outlineWidthSlider;
[SerializeField] private Toggle _boldButton;
[SerializeField] private Toggle _italicButton;
[SerializeField] private Toggle _underlineButton;
[SerializeField] private Toggle _smallCapsButton;
[SerializeField] private Toggle _verticalButton;
2020-07-21 07:52:23 +00:00
private DecalText _decalText;
private FontMenuController _fontMenu;
2020-07-16 01:12:50 +00:00
2020-07-21 07:07:18 +00:00
public static TextEntryController Create(DecalText text, UnityAction<DecalText> textUpdateCallback) {
2020-07-20 04:12:48 +00:00
var window = Instantiate(UILoader.TextEntryPrefab, MainCanvasUtil.MainCanvas.transform, true);
window.AddComponent<DragPanel>();
MenuNavigation.SpawnMenuNavigation(window, Navigation.Mode.Automatic, true);
2020-07-16 01:12:50 +00:00
2020-07-20 04:12:48 +00:00
var controller = window.GetComponent<TextEntryController>();
2020-07-21 07:07:18 +00:00
controller._decalText = text;
2020-07-23 05:37:16 +00:00
controller.onTextUpdate.AddListener(textUpdateCallback);
2020-07-16 01:12:50 +00:00
2020-07-20 04:12:48 +00:00
return controller;
}
2020-07-16 01:12:50 +00:00
private void Start() {
2020-07-21 07:07:18 +00:00
((TMP_InputField) _textBox).text = _decalText.text;
2020-07-21 07:52:23 +00:00
_decalText.font.SetupSample(_fontButton.GetComponentInChildren<TextMeshProUGUI>());
2020-07-20 04:12:48 +00:00
2020-07-21 07:07:18 +00:00
_outlineWidthSlider.value = _decalText.outlineWidth;
_boldButton.isOn = (_decalText.style & FontStyles.Bold) != 0;
_italicButton.isOn = (_decalText.style & FontStyles.Italic) != 0;
_underlineButton.isOn = (_decalText.style & FontStyles.Underline) != 0;
_smallCapsButton.isOn = (_decalText.style & FontStyles.SmallCaps) != 0;
_verticalButton.isOn = _decalText.vertical;
2020-07-16 01:12:50 +00:00
}
2020-07-20 04:12:48 +00:00
public void OnClose() {
if (_fontMenu != null) _fontMenu.OnClose();
2020-07-16 01:12:50 +00:00
Destroy(gameObject);
}
2020-07-20 04:12:48 +00:00
public void OnAnyUpdate() {
2020-07-23 05:37:16 +00:00
onTextUpdate.Invoke(_decalText);
2020-07-16 01:12:50 +00:00
}
2020-07-20 04:12:48 +00:00
public void OnTextUpdate(string newText) {
2020-07-21 07:07:18 +00:00
this._decalText.text = newText;
2020-07-20 04:12:48 +00:00
OnAnyUpdate();
2020-07-16 01:12:50 +00:00
}
2020-07-20 04:12:48 +00:00
public void OnFontMenu() {
2020-07-21 07:07:18 +00:00
if (_fontMenu == null) _fontMenu = FontMenuController.Create(DecalConfig.Fonts, _decalText.font, OnFontUpdate);
2020-07-20 04:12:48 +00:00
}
public void OnFontUpdate(DecalFont font) {
2020-07-21 07:07:18 +00:00
_decalText.font = font;
2020-07-20 04:12:48 +00:00
font.SetupSample(_fontButton.GetComponentInChildren<TextMeshProUGUI>());
var textBox = ((TMP_InputField) _textBox);
2020-07-21 07:07:18 +00:00
textBox.textComponent.fontStyle = _decalText.style | _decalText.font.fontStyle;
textBox.fontAsset = _decalText.font.fontAsset;
2020-07-16 01:12:50 +00:00
2020-07-20 04:12:48 +00:00
OnAnyUpdate();
2020-07-16 01:12:50 +00:00
}
2020-07-20 04:12:48 +00:00
public void OnColorMenu() { }
2020-07-16 01:12:50 +00:00
2020-07-20 04:12:48 +00:00
public void OnColorUpdate(Color color) {
2020-07-21 07:07:18 +00:00
_decalText.color = color;
2020-07-20 04:12:48 +00:00
OnAnyUpdate();
}
2020-07-16 01:12:50 +00:00
2020-07-20 04:12:48 +00:00
public void OnOutlineColorMenu() { }
2020-07-16 01:12:50 +00:00
2020-07-20 04:12:48 +00:00
public void OnOutlineColorUpdate(Color color) {
2020-07-21 07:07:18 +00:00
_decalText.outlineColor = color;
2020-07-20 04:12:48 +00:00
OnAnyUpdate();
}
public void OnOutlineUpdate(float value) {
2020-07-21 07:07:18 +00:00
_decalText.outlineWidth = value;
2020-07-20 04:12:48 +00:00
OnAnyUpdate();
2020-07-16 01:12:50 +00:00
}
public void OnBoldUpdate(bool state) {
2020-07-21 07:07:18 +00:00
if (state) _decalText.style |= FontStyles.Bold;
else _decalText.style &= ~FontStyles.Bold;
2020-07-16 01:12:50 +00:00
2020-07-21 07:07:18 +00:00
((TMP_InputField) _textBox).textComponent.fontStyle = _decalText.style | _decalText.font.fontStyle;
2020-07-20 04:12:48 +00:00
OnAnyUpdate();
2020-07-16 01:12:50 +00:00
}
public void OnItalicUpdate(bool state) {
2020-07-21 07:07:18 +00:00
if (state) _decalText.style |= FontStyles.Italic;
else _decalText.style &= ~FontStyles.Italic;
2020-07-20 04:12:48 +00:00
2020-07-21 07:07:18 +00:00
((TMP_InputField) _textBox).textComponent.fontStyle = _decalText.style | _decalText.font.fontStyle;
2020-07-16 01:12:50 +00:00
2020-07-20 04:12:48 +00:00
OnAnyUpdate();
2020-07-16 01:12:50 +00:00
}
public void OnUnderlineUpdate(bool state) {
2020-07-21 07:07:18 +00:00
if (state) _decalText.style |= FontStyles.Underline;
else _decalText.style &= ~FontStyles.Underline;
2020-07-16 01:12:50 +00:00
2020-07-21 07:07:18 +00:00
((TMP_InputField) _textBox).textComponent.fontStyle = _decalText.style | _decalText.font.fontStyle;
2020-07-20 04:12:48 +00:00
OnAnyUpdate();
2020-07-16 01:12:50 +00:00
}
public void OnSmallCapsUpdate(bool state) {
2020-07-21 07:07:18 +00:00
if (state) _decalText.style |= FontStyles.SmallCaps;
else _decalText.style &= ~FontStyles.SmallCaps;
2020-07-20 04:12:48 +00:00
2020-07-21 07:07:18 +00:00
((TMP_InputField) _textBox).textComponent.fontStyle = _decalText.style | _decalText.font.fontStyle;
2020-07-16 01:12:50 +00:00
2020-07-20 04:12:48 +00:00
OnAnyUpdate();
2020-07-16 01:12:50 +00:00
}
public void OnVerticalUpdate(bool state) {
2020-07-21 07:07:18 +00:00
_decalText.vertical = state;
2020-07-20 04:12:48 +00:00
OnAnyUpdate();
2020-07-13 05:38:37 +00:00
}
}
}