summaryrefslogtreecommitdiff
path: root/Valheim_v202102/Valheim/assembly_valheim/TextViewer.cs
blob: 9e41172c4b76931e91a113071fbd0554f0d72409 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using UnityEngine;
using UnityEngine.UI;

public class TextViewer : MonoBehaviour
{
	public enum Style
	{
		Rune,
		Intro,
		Raven
	}

	private static TextViewer m_instance;

	private Animator m_animator;

	private Animator m_animatorIntro;

	private Animator m_animatorRaven;

	[Header("Rune")]
	public GameObject m_root;

	public Text m_topic;

	public Text m_text;

	public Text m_runeText;

	public GameObject m_closeText;

	[Header("Intro")]
	public GameObject m_introRoot;

	public Text m_introTopic;

	public Text m_introText;

	[Header("Raven")]
	public GameObject m_ravenRoot;

	public Text m_ravenTopic;

	public Text m_ravenText;

	private static int m_visibleID = Animator.StringToHash("visible");

	private static int m_animatorTagVisible = Animator.StringToHash("visible");

	private float m_showTime;

	private bool m_autoHide;

	private Vector3 m_openPlayerPos = Vector3.zero;

	public static TextViewer instance => m_instance;

	private void Awake()
	{
		m_instance = this;
		m_root.SetActive(value: true);
		m_introRoot.SetActive(value: true);
		m_ravenRoot.SetActive(value: true);
		m_animator = m_root.GetComponent<Animator>();
		m_animatorIntro = m_introRoot.GetComponent<Animator>();
		m_animatorRaven = m_ravenRoot.GetComponent<Animator>();
	}

	private void OnDestroy()
	{
		m_instance = null;
	}

	private void LateUpdate()
	{
		if (!IsVisible())
		{
			return;
		}
		m_showTime += Time.deltaTime;
		if (m_showTime > 0.2f)
		{
			if (m_autoHide && (bool)Player.m_localPlayer && Vector3.Distance(Player.m_localPlayer.transform.position, m_openPlayerPos) > 3f)
			{
				Hide();
			}
			if (ZInput.GetButtonDown("Use") || ZInput.GetButtonDown("JoyUse") || Input.GetKeyDown(KeyCode.Escape))
			{
				Hide();
			}
		}
	}

	public void ShowText(Style style, string topic, string text, bool autoHide)
	{
		if (!(Player.m_localPlayer == null))
		{
			topic = Localization.instance.Localize(topic);
			text = Localization.instance.Localize(text);
			switch (style)
			{
			case Style.Rune:
				m_topic.text = topic;
				m_text.text = text;
				m_runeText.text = text;
				m_animator.SetBool(m_visibleID, value: true);
				break;
			case Style.Intro:
				m_introTopic.text = topic;
				m_introText.text = text;
				m_animatorIntro.SetTrigger("play");
				ZLog.Log("Show intro " + Time.frameCount);
				break;
			case Style.Raven:
				m_ravenTopic.text = topic;
				m_ravenText.text = text;
				m_animatorRaven.SetBool(m_visibleID, value: true);
				break;
			}
			m_autoHide = autoHide;
			m_openPlayerPos = Player.m_localPlayer.transform.position;
			m_showTime = 0f;
			ZLog.Log("Show text " + topic + ":" + text);
		}
	}

	public void Hide()
	{
		m_autoHide = false;
		m_animator.SetBool(m_visibleID, value: false);
		m_animatorRaven.SetBool(m_visibleID, value: false);
	}

	public bool IsVisible()
	{
		if (m_instance.m_animatorIntro.GetCurrentAnimatorStateInfo(0).tagHash == m_animatorTagVisible)
		{
			return true;
		}
		if (!m_animator.GetBool(m_visibleID) && !m_animatorIntro.GetBool(m_visibleID))
		{
			return m_animatorRaven.GetBool(m_visibleID);
		}
		return true;
	}

	public static bool IsShowingIntro()
	{
		if (m_instance != null)
		{
			return m_instance.m_animatorIntro.GetCurrentAnimatorStateInfo(0).tagHash == m_animatorTagVisible;
		}
		return false;
	}
}