Decal text input UI

This commit is contained in:
2020-07-19 21:12:48 -07:00
parent 1316dbb553
commit e87fc48edf
14 changed files with 348 additions and 102 deletions

View File

@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using ConformalDecals.Text;
using UniLinq;
using UnityEngine;
using UnityEngine.UI;
namespace ConformalDecals.UI {
public class FontMenuController : MonoBehaviour {
[SerializeField] private GameObject _menuItem;
[SerializeField] private GameObject _menuList;
public DecalFont currentFont;
public delegate void FontUpdateReceiver(DecalFont font);
public FontUpdateReceiver fontUpdateCallback;
public static FontMenuController Create(IEnumerable<DecalFont> fonts, DecalFont currentFont, FontUpdateReceiver fontUpdateCallback) {
var menu = Instantiate(UILoader.FontMenuPrefab, MainCanvasUtil.MainCanvas.transform, true);
menu.AddComponent<DragPanel>();
MenuNavigation.SpawnMenuNavigation(menu, Navigation.Mode.Automatic, true);
var controller = menu.GetComponent<FontMenuController>();
controller.fontUpdateCallback = fontUpdateCallback;
controller.currentFont = currentFont;
controller.Populate(fonts);
return controller;
}
public void OnClose() {
Destroy(gameObject);
}
public void OnFontSelected(DecalFont font) {
currentFont = font ?? throw new ArgumentNullException(nameof(font));
fontUpdateCallback(currentFont);
}
public void Populate(IEnumerable<DecalFont> fonts) {
if (fonts == null) throw new ArgumentNullException(nameof(fonts));
Toggle active = null;
foreach (var font in fonts.OrderBy(x => x.title)) {
Debug.Log(font.title);
var listItem = GameObject.Instantiate(_menuItem, _menuList.transform);
listItem.name = font.title;
listItem.SetActive(true);
var fontItem = listItem.AddComponent<FontMenuItem>();
fontItem.Font = font;
fontItem.fontSelectionCallback = OnFontSelected;
if (font == currentFont) active = fontItem.toggle;
}
if (active != null) active.isOn = true;
}
}
}

View File

@ -0,0 +1,36 @@
using System;
using ConformalDecals.Text;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace ConformalDecals.UI {
public class FontMenuItem : MonoBehaviour {
public DecalFont Font {
get => _font;
set {
_font = value;
_font.SetupSample(_label);
}
}
public delegate void FontSelectionReceiver(DecalFont font);
public FontSelectionReceiver fontSelectionCallback;
public Toggle toggle;
private DecalFont _font;
private TMP_Text _label;
private void Awake() {
_label = gameObject.GetComponentInChildren<TextMeshProUGUI>();
toggle = gameObject.GetComponent<Toggle>();
toggle.isOn = false;
toggle.onValueChanged.AddListener(delegate { OnToggle(toggle); });
}
public void OnToggle(Toggle change) {
if (change.isOn) fontSelectionCallback?.Invoke(_font);
}
}
}

View File

@ -1,4 +1,3 @@
using System;
using ConformalDecals.Text;
using TMPro;
using UnityEngine;
@ -6,12 +5,15 @@ using UnityEngine.UI;
namespace ConformalDecals.UI {
public class TextEntryController : MonoBehaviour {
private FormattedText _text;
public delegate void TextUpdateReceiver(DecalText text);
public TextUpdateReceiver textUpdateCallback;
public DecalText decalText;
private FontMenuController _fontMenu;
[SerializeField] private Selectable _textBox;
[SerializeField] private Toggle _fontColorButton;
[SerializeField] private Toggle _fontButton;
[SerializeField] private Toggle _outlineColorButton;
[SerializeField] private Button _fontButton;
[SerializeField] private Slider _outlineWidthSlider;
[SerializeField] private Toggle _boldButton;
@ -20,89 +22,123 @@ namespace ConformalDecals.UI {
[SerializeField] private Toggle _smallCapsButton;
[SerializeField] private Toggle _verticalButton;
public delegate void TextUpdateReceiver(FormattedText text);
public static TextEntryController Create(DecalText text, TextUpdateReceiver textUpdateCallback) {
var window = Instantiate(UILoader.TextEntryPrefab, MainCanvasUtil.MainCanvas.transform, true);
window.AddComponent<DragPanel>();
MenuNavigation.SpawnMenuNavigation(window, Navigation.Mode.Automatic, true);
public delegate void TextCancelReceiver();
var controller = window.GetComponent<TextEntryController>();
controller.decalText = text;
controller.textUpdateCallback = textUpdateCallback;
text.font.SetupSample(controller._fontButton.GetComponentInChildren<TextMeshProUGUI>());
public TextUpdateReceiver textUpdateCallback;
public TextCancelReceiver textCancelCallback;
private void Start() {
(_textBox as TMP_InputField).text = _text.text;
_boldButton.isOn = (_text.style | FontStyles.Bold) != 0;
_italicButton.isOn = (_text.style | FontStyles.Italic) != 0;
_underlineButton.isOn = (_text.style | FontStyles.Underline) != 0;
_smallCapsButton.isOn = (_text.style | FontStyles.SmallCaps) != 0;
_verticalButton.isOn = _text.vertical;
return controller;
}
public void Close() {
private void Start() {
((TMP_InputField) _textBox).text = decalText.text;
_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;
}
public void OnClose() {
if (_fontMenu != null) _fontMenu.OnClose();
Destroy(gameObject);
}
public void OnCancel() {
textCancelCallback();
Close();
public void OnAnyUpdate() {
textUpdateCallback(decalText);
}
public void OnApply() {
textUpdateCallback(_text);
Close();
public void OnTextUpdate(string newText) {
this.decalText.text = newText;
OnAnyUpdate();
}
public void OnTextUpdate(string text) {
_text.text = text;
textUpdateCallback(_text);
public void OnFontMenu() {
if (_fontMenu == null) _fontMenu = FontMenuController.Create(DecalConfig.Fonts, decalText.font, OnFontUpdate);
}
public void OnFontMenu(bool state) { }
public void OnColorMenu(bool state) { }
public void OnFontUpdate(DecalFont font) {
decalText.font = font;
font.SetupSample(_fontButton.GetComponentInChildren<TextMeshProUGUI>());
public void OnOutlineColorMenu(bool state) { }
var textBox = ((TMP_InputField) _textBox);
textBox.textComponent.fontStyle = decalText.style | decalText.font.fontStyle;
textBox.fontAsset = decalText.font.fontAsset;
OnAnyUpdate();
}
public void OnColorMenu() { }
public void OnColorUpdate(Color color) {
decalText.color = color;
OnAnyUpdate();
}
public void OnOutlineColorMenu() { }
public void OnOutlineColorUpdate(Color color) {
decalText.outlineColor = color;
OnAnyUpdate();
}
public void OnOutlineUpdate(float value) {
_text.outlineWidth = value;
textUpdateCallback(_text);
decalText.outlineWidth = value;
OnAnyUpdate();
}
public void OnBoldUpdate(bool state) {
if (state) _text.style |= FontStyles.Bold;
else _text.style &= ~FontStyles.Bold;
if (state) decalText.style |= FontStyles.Bold;
else decalText.style &= ~FontStyles.Bold;
textUpdateCallback(_text);
((TMP_InputField) _textBox).textComponent.fontStyle = decalText.style | decalText.font.fontStyle;
OnAnyUpdate();
}
public void OnItalicUpdate(bool state) {
if (state) _text.style |= FontStyles.Italic;
else _text.style &= ~FontStyles.Italic;
if (state) decalText.style |= FontStyles.Italic;
else decalText.style &= ~FontStyles.Italic;
textUpdateCallback(_text);
((TMP_InputField) _textBox).textComponent.fontStyle = decalText.style | decalText.font.fontStyle;
OnAnyUpdate();
}
public void OnUnderlineUpdate(bool state) {
if (state) _text.style |= FontStyles.Underline;
else _text.style &= ~FontStyles.Underline;
if (state) decalText.style |= FontStyles.Underline;
else decalText.style &= ~FontStyles.Underline;
textUpdateCallback(_text);
((TMP_InputField) _textBox).textComponent.fontStyle = decalText.style | decalText.font.fontStyle;
OnAnyUpdate();
}
public void OnSmallCapsUpdate(bool state) {
if (state) _text.style |= FontStyles.SmallCaps;
else _text.style &= ~FontStyles.SmallCaps;
if (state) decalText.style |= FontStyles.SmallCaps;
else decalText.style &= ~FontStyles.SmallCaps;
textUpdateCallback(_text);
((TMP_InputField) _textBox).textComponent.fontStyle = decalText.style | decalText.font.fontStyle;
OnAnyUpdate();
}
public void OnVerticalUpdate(bool state) {
_text.vertical = state;
textUpdateCallback(_text);
decalText.vertical = state;
OnAnyUpdate();
}
}
}

View File

@ -8,30 +8,22 @@ namespace ConformalDecals.UI {
public class UILoader : MonoBehaviour {
private static readonly string Path = KSPUtil.ApplicationRootPath + "GameData/ConformalDecals/Resources/";
public static GameObject textEntryPrefab;
private static GameObject _textEntryPrefab;
private static GameObject _fontMenuPrefab;
public static GameObject FontMenuPrefab => _fontMenuPrefab;
public static GameObject TextEntryPrefab => _textEntryPrefab;
private void Awake() {
var prefabs = AssetBundle.LoadFromFile(Path + "ui.conformaldecals");
textEntryPrefab = prefabs.LoadAsset("TextEntryPanel") as GameObject;
_textEntryPrefab = prefabs.LoadAsset("TextEntryPanel") as GameObject;
_fontMenuPrefab = prefabs.LoadAsset("FontMenuPanel") as GameObject;
ProcessWindow(textEntryPrefab);
Debug.Log("[ConformalDecals] UI prefabs loaded and modified");
Debug.Log($"[ConformalDecals] {MainCanvasUtil.MainCanvas.renderMode}");
Debug.Log($"[ConformalDecals] {MainCanvasUtil.MainCanvas.sortingOrder}");
Debug.Log($"[ConformalDecals] {MainCanvasUtil.MainCanvas.sortingLayerID}");
Debug.Log($"[ConformalDecals] {MainCanvasUtil.MainCanvas.sortingLayerName}");
foreach (var layer in SortingLayer.layers) {
Debug.Log(layer.name);
Debug.Log(layer.id);
Debug.Log(layer.value);
}
var window = Instantiate(UILoader.textEntryPrefab, MainCanvasUtil.MainCanvas.transform, true);
ProcessWindow(_textEntryPrefab);
ProcessWindow(_fontMenuPrefab);
}
private static void ProcessWindow(GameObject window) {
var skin = UISkinManager.defaultSkin;
var font = UISkinManager.TMPFont;