Assets/Scripts/Utility/UI.cs (view raw)
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.IO;
5using System.Runtime.Serialization.Formatters.Binary;
6using UnityEngine;
7using UnityEngine.UI;
8
9public static class UI
10{
11 private static GameMaster gm = GameMaster.Instance;
12 private static BattleUIManager battle = null;
13 private static Coroutine text_coroutine;
14
15
16 //hh, HH: 24-hour, 12-hour; MMMM, MM: March, 03
17 public static string SaveDateFormat = "dd/MM/yyyy HH:mm";
18 public static float TextSpeed = PlayerPrefs.GetFloat("TextSpeed", 0.015f);
19 public static float BattleSpeed = PlayerPrefs.GetFloat("BattleSpeed", 1f);
20 public static bool displaying_text = false;
21
22 public static void setSLButtonText(Button btn, int slot)
23 {
24 int seconds;
25 DateTime dt = GetSlotDateTime(slot, out seconds);
26 if(dt == default)
27 {
28 btn.enabled = false;
29 btn.GetComponentInChildren<Text>().text = "Empty";
30 } else
31 {
32 btn.GetComponentInChildren<Text>().text = dt.ToString(SaveDateFormat) + "\nPlay time " + Mathf.Floor(seconds / 60).ToString("00") + ":" + seconds.ToString("00");
33 btn.enabled = true;
34 }
35
36 }
37
38 public static DateTime GetSlotDateTime(int slot, out int seconds)
39 {
40 if (File.Exists(Application.persistentDataPath + "/game" + slot + ".save"))
41 {
42 BinaryFormatter bf = new BinaryFormatter();
43 FileStream file = File.Open(Application.persistentDataPath + "/game" + slot + ".save", FileMode.Open);
44 Save save = (Save)bf.Deserialize(file);
45 file.Close();
46 seconds = Mathf.RoundToInt(save.timerfloat % 60);
47 return save.timestamp;
48 }
49 else
50 {
51 seconds = 0;
52 return default;
53 }
54 }
55 public static bool ShowDamage(Fighter target, int danno, bool didCrit)
56 {
57 if (!battle)
58 {
59 battle = UnityEngine.Object.FindObjectOfType<BattleUIManager>();
60 }
61
62 if (battle)
63 {
64 battle.ShowDamage(target, danno, didCrit);
65 return true;
66 }
67 else
68 {
69 return false;
70 }
71 }
72
73 #region Slow Text
74 public static void changeText(Text textComp, string content) //change any text slowly.
75 {
76 if(Time.timeScale == 0f)
77 {
78 textComp.text = content;
79 } else
80 {
81 if (displaying_text)
82 {
83 stopText();
84 }
85 textComp.text = "";
86 text_coroutine = gm.StartCoroutine(TypeText(textComp, content, null, null, TextSpeed));
87 }
88 }
89
90 public static void changeDialogue(Text textComp, string content, AudioSource source, AudioClip clip) //change text with sound
91 {
92 if (Time.timeScale == 0f)
93 {
94 textComp.text = content;
95 }
96 else
97 {
98 if (displaying_text)
99 {
100 stopText();
101
102 }
103 textComp.text = "";
104 text_coroutine = gm.StartCoroutine(TypeText(textComp, content, source, clip, TextSpeed));
105 }
106 }
107 public static void stopText()
108 {
109 displaying_text = false;
110 gm.StopCoroutine(text_coroutine);
111 }
112
113
114 public static IEnumerator TypeText(Text textComp, string message, AudioSource source, AudioClip clip, float seconds)
115 {
116 if(message == null)
117 {
118 yield break;
119 }
120 displaying_text = true;
121 int i = 0;
122 foreach (char letter in message.ToCharArray())
123 {
124 i++;
125 textComp.text += letter;
126 if (clip && i % 2 == 0)
127 {
128 source.PlayOneShot(clip);
129 }
130
131 yield return null;
132 yield return new WaitForSeconds(seconds);
133 }
134 displaying_text = false;
135 }
136 #endregion
137
138 public static void updateItems(UIManager ui, Inventory inv, Button[] item_buttons)
139 {
140 int i = 0;
141 foreach (ItemInfo it in inv.getContents())
142 {
143 if (i == item_buttons.Length)
144 {
145 break;
146 }
147
148 //show item_buttons
149 if (it.item.consumable)
150 {
151 item_buttons[i].GetComponent<ActionButton>().setDescription(it.GetDescription());
152 item_buttons[i].onClick.AddListener(() => ui.showTargets(it));
153 item_buttons[i].gameObject.SetActive(true);
154 item_buttons[i].GetComponentInChildren<Text>().text = it.ToString();
155 i++;
156 }
157 }
158 for (; i < item_buttons.Length; i++) //disable remaining item_buttons
159 {
160 item_buttons[i].gameObject.SetActive(false);
161 }
162 }
163
164 public static void updateMeters(Stats s, Transform t)
165 {
166 t.GetComponent<Text>().text = s.Name + ":";
167
168 Transform tmphp = t.GetChild(0), tmpmp = null;
169
170 tmphp.GetComponent<Text>().text = (int)s.HP + "/" + (int)s.MaxHP + " HP";
171 tmphp.GetComponentInChildren<Slider>().value = (float)(s.HP / s.MaxHP);
172 if (s.MaxMP > 0)
173 {
174 tmpmp = t.GetChild(1);
175 tmpmp.GetComponent<Text>().text = (int)s.MP + "/" + (int)s.MaxMP + " MP";
176 tmpmp.GetComponentInChildren<Slider>().value = (float)(s.MP / s.MaxMP);
177 }
178 }
179}