131 lines
4.5 KiB
C#
131 lines
4.5 KiB
C#
using System;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace TH1_UI.View.Outside
|
|
{
|
|
public class UIOutsideQuestionnaireOptionMono : MonoBehaviour
|
|
{
|
|
public Toggle Toggle;
|
|
public TextMeshProUGUI LabelText;
|
|
|
|
public QuestionnaireOptionInfo OptionInfo { get; private set; }
|
|
private Action<UIOutsideQuestionnaireOptionMono, bool> _onValueChanged;
|
|
|
|
public bool IsOn => Toggle != null && Toggle.isOn;
|
|
|
|
public void SetContent(QuestionnaireOptionInfo info, bool selected, Action<UIOutsideQuestionnaireOptionMono, bool> onValueChanged)
|
|
{
|
|
EnsureLayout();
|
|
OptionInfo = info;
|
|
_onValueChanged = onValueChanged;
|
|
|
|
if (LabelText != null)
|
|
{
|
|
LabelText.text = UIOutsideQuestionnaireView.ResolveText(info?.Text);
|
|
}
|
|
|
|
if (Toggle != null)
|
|
{
|
|
Toggle.onValueChanged.RemoveAllListeners();
|
|
Toggle.SetIsOnWithoutNotify(selected);
|
|
Toggle.onValueChanged.AddListener(OnToggleChanged);
|
|
}
|
|
}
|
|
|
|
public void SetSelectedWithoutNotify(bool value)
|
|
{
|
|
Toggle?.SetIsOnWithoutNotify(value);
|
|
}
|
|
|
|
public void SetInteractable(bool value)
|
|
{
|
|
if (Toggle != null)
|
|
{
|
|
Toggle.interactable = value;
|
|
}
|
|
}
|
|
|
|
private void OnToggleChanged(bool value)
|
|
{
|
|
_onValueChanged?.Invoke(this, value);
|
|
}
|
|
|
|
private void EnsureLayout()
|
|
{
|
|
if (Toggle == null)
|
|
{
|
|
Toggle = GetComponent<Toggle>();
|
|
if (Toggle == null)
|
|
{
|
|
Toggle = gameObject.AddComponent<Toggle>();
|
|
}
|
|
}
|
|
|
|
var rowImage = GetComponent<Image>();
|
|
if (rowImage == null)
|
|
{
|
|
rowImage = gameObject.AddComponent<Image>();
|
|
}
|
|
|
|
rowImage.color = new Color(1f, 1f, 1f, 0.04f);
|
|
rowImage.raycastTarget = true;
|
|
Toggle.targetGraphic = rowImage;
|
|
|
|
var layout = GetComponent<HorizontalLayoutGroup>();
|
|
if (layout == null)
|
|
{
|
|
layout = gameObject.AddComponent<HorizontalLayoutGroup>();
|
|
}
|
|
|
|
layout.padding = new RectOffset(10, 10, 6, 6);
|
|
layout.spacing = 10f;
|
|
layout.childControlWidth = false;
|
|
layout.childControlHeight = true;
|
|
layout.childForceExpandWidth = false;
|
|
layout.childForceExpandHeight = false;
|
|
layout.childAlignment = TextAnchor.MiddleLeft;
|
|
|
|
var layoutElement = GetComponent<LayoutElement>();
|
|
if (layoutElement == null)
|
|
{
|
|
layoutElement = gameObject.AddComponent<LayoutElement>();
|
|
}
|
|
|
|
layoutElement.minHeight = 38f;
|
|
layoutElement.preferredHeight = 38f;
|
|
|
|
if (Toggle.graphic == null)
|
|
{
|
|
var markRoot = new GameObject("Mark", typeof(RectTransform));
|
|
markRoot.transform.SetParent(transform, false);
|
|
var markRect = markRoot.GetComponent<RectTransform>();
|
|
markRect.sizeDelta = new Vector2(22f, 22f);
|
|
markRoot.AddComponent<LayoutElement>().preferredWidth = 22f;
|
|
|
|
var markBg = markRoot.AddComponent<Image>();
|
|
markBg.color = new Color(1f, 1f, 1f, 0.16f);
|
|
|
|
var check = new GameObject("Check", typeof(RectTransform), typeof(Image));
|
|
check.transform.SetParent(markRoot.transform, false);
|
|
var checkRect = check.GetComponent<RectTransform>();
|
|
checkRect.anchorMin = new Vector2(0.22f, 0.22f);
|
|
checkRect.anchorMax = new Vector2(0.78f, 0.78f);
|
|
checkRect.offsetMin = Vector2.zero;
|
|
checkRect.offsetMax = Vector2.zero;
|
|
var checkImage = check.GetComponent<Image>();
|
|
checkImage.color = new Color(0.2f, 0.85f, 0.72f, 1f);
|
|
Toggle.graphic = checkImage;
|
|
}
|
|
|
|
if (LabelText == null)
|
|
{
|
|
LabelText = UIOutsideQuestionnaireView.CreateText(transform, "Label", "", 20f, new Color(0.94f, 0.94f, 0.9f, 1f), TextAlignmentOptions.Left);
|
|
LabelText.enableWordWrapping = true;
|
|
LabelText.gameObject.AddComponent<LayoutElement>().flexibleWidth = 1f;
|
|
}
|
|
}
|
|
}
|
|
}
|