ActionBarUi.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using System.IO;
  6. namespace SoftKitty.InventoryEngine
  7. {
  8. public class ActionBarUi : MonoBehaviour
  9. {
  10. [Header("[After click a slot, will this slot be selected?]")]
  11. public bool SelectableSlot = true;
  12. [Header("[Whether display the XP progress bar.]")]
  13. public bool EnableXP = true;
  14. [Header("[Setup the ActionBar Data.]")]
  15. public List<ActionSlotSet> SlotData=new List<ActionSlotSet>();
  16. [Header("[Save path of the ActionBar data.]")]
  17. public string SavePath = "";
  18. [Header("[Current Action Bar index.]")]
  19. public int ActionSet = 0;
  20. [Header("[References]")]
  21. public ActionSlot[] Slots;
  22. public Image LockIcon;
  23. public Text SetNumberText;
  24. public HintText ProgressHint;
  25. public Image ProgressBar;
  26. public Text ProgressText;
  27. #region Variables
  28. private InventoryHolder Holder;
  29. public static ActionBarUi instance;
  30. private bool isLock = false;
  31. private string ProgressBarHintString = "";
  32. private int SelectedSlot = 0;
  33. private int LastSelectedSlot = -1;
  34. private float xp;
  35. private float mxp;
  36. #endregion
  37. #region Internal Methods
  38. void Awake()
  39. {
  40. instance = this;
  41. SetProgressHint(ProgressBarHintString);
  42. ProgressBar.gameObject.SetActive(EnableXP);
  43. ProgressText.gameObject.SetActive(EnableXP);
  44. }
  45. IEnumerator Start()
  46. {
  47. yield return 2;
  48. if (SavePath.Length > 0)//Check if the save path is null;
  49. {
  50. string _dirPath = SavePath.Replace(Path.GetFileName(SavePath), "");
  51. if (!Directory.Exists(_dirPath)) Directory.CreateDirectory(_dirPath);//There might be some sub folder is missing within the save path, create them when needed.
  52. if (File.Exists(SavePath))//Check if the save file exist
  53. {
  54. string _data = File.ReadAllText(SavePath, System.Text.Encoding.UTF8);
  55. Load(_data);
  56. }
  57. }
  58. Initialize(ItemManager.PlayerInventoryHolder);
  59. }
  60. public void OnSlotClick(int _index, int _button)
  61. {
  62. Slots[_index].Use();
  63. if (_button == 0) SelectedSlot = _index;
  64. }
  65. public void Initialize(InventoryHolder _holder)
  66. {
  67. Holder = _holder;
  68. SwitchSet(ActionSet);
  69. }
  70. public void SetChange(int _add)
  71. {
  72. if (_add > 0)
  73. {
  74. if (ActionSet + _add < SlotData.Count)
  75. ActionSet += _add;
  76. else
  77. ActionSet = 0;
  78. SwitchSet(ActionSet);
  79. }
  80. else if (_add < 0)
  81. {
  82. if (ActionSet + _add >= 0)
  83. ActionSet += _add;
  84. else
  85. ActionSet = SlotData.Count - 1;
  86. SwitchSet(ActionSet);
  87. }
  88. }
  89. public void ToggleLock()
  90. {
  91. isLock = !isLock;
  92. LockIcon.color = isLock ? new Color(1F, 0.68F, 0.09F, 1F) : new Color(0.65F, 0.61F, 0.58F, 1F);
  93. for (int i = 0; i < Slots.Length; i++)
  94. {
  95. Slots[i].SetLock(isLock);
  96. }
  97. SoundManager.Play2D("EquipOff");
  98. }
  99. void Update()
  100. {
  101. if (!ItemDragManager.isVisible())
  102. {
  103. if (SelectableSlot && LastSelectedSlot != SelectedSlot)
  104. {
  105. LastSelectedSlot = SelectedSlot;
  106. for (int i = 0; i < Slots.Length; i++)
  107. {
  108. Slots[i].Item.ToggleOutline(i == SelectedSlot);
  109. }
  110. }
  111. }
  112. else
  113. {
  114. LastSelectedSlot = -1;
  115. }
  116. if (EnableXP && ItemManager.PlayerEquipmentHolder != null && Time.frameCount%10==0)
  117. {
  118. if (xp != ItemManager.PlayerEquipmentHolder.GetBaseStatsValue(ItemManager.instance.XpAttributeKey)
  119. || mxp != ItemManager.PlayerEquipmentHolder.GetBaseStatsValue(ItemManager.instance.MaxXpAttributeKey))
  120. {
  121. xp = ItemManager.PlayerEquipmentHolder.GetBaseStatsValue(ItemManager.instance.XpAttributeKey);
  122. mxp = ItemManager.PlayerEquipmentHolder.GetBaseStatsValue(ItemManager.instance.MaxXpAttributeKey);
  123. SetProgress(xp, mxp);
  124. SetProgressHint("Level. "+ Mathf.FloorToInt( ItemManager.PlayerEquipmentHolder.GetBaseStatsValue(ItemManager.instance.LevelAttributeKey)).ToString());
  125. }
  126. }
  127. }
  128. #endregion
  129. public void Load(string _json) // Load data with json string
  130. {
  131. ActionBarSaveRoot _saveRoot = JsonUtility.FromJson<ActionBarSaveRoot>(_json);
  132. SlotData.Clear();
  133. for (int i = 0; i < _saveRoot.sets.Length; i++)
  134. {
  135. ActionSlotSet _newSet = new ActionSlotSet();
  136. _newSet.slots = new List<ActionSlotData>();
  137. for (int u = 0; u < _saveRoot.sets[i].slots.Length; u++)
  138. {
  139. ActionSlotData _newSlot = new ActionSlotData();
  140. _newSlot.key = (KeyCode)_saveRoot.sets[i].slots[u].key;
  141. _newSlot.itemId = _saveRoot.sets[i].slots[u].itemId;
  142. _newSlot.upgradeLevel = _saveRoot.sets[i].slots[u].upgradeLevel;
  143. _newSlot.enchantments = new List<int>();
  144. _newSlot.enchantments.AddRange(_saveRoot.sets[i].slots[u].enchantments);
  145. _newSlot.sockets = new List<int>();
  146. _newSlot.sockets.AddRange(_saveRoot.sets[i].slots[u].sockets);
  147. _newSet.slots.Add(_newSlot);
  148. }
  149. SlotData.Add(_newSet);
  150. }
  151. }
  152. public void Save()//Save data to the SavePath.
  153. {
  154. if (SavePath.Length > 0)
  155. {
  156. ActionBarSaveRoot _saveRoot = new ActionBarSaveRoot();
  157. _saveRoot.sets = new ActionBarSave[SlotData.Count];
  158. for (int i = 0; i < SlotData.Count; i++)
  159. {
  160. _saveRoot.sets[i] = new ActionBarSave();
  161. _saveRoot.sets[i].slots = new ActionBarSlotSave[SlotData[i].slots.Count];
  162. for (int u = 0; u < SlotData[i].slots.Count; u++)
  163. {
  164. _saveRoot.sets[i].slots[u] = new ActionBarSlotSave();
  165. _saveRoot.sets[i].slots[u].key = (int)SlotData[i].slots[u].key;
  166. _saveRoot.sets[i].slots[u].itemId = SlotData[i].slots[u].itemId;
  167. _saveRoot.sets[i].slots[u].upgradeLevel = SlotData[i].slots[u].upgradeLevel;
  168. _saveRoot.sets[i].slots[u].enchantments.Clear();
  169. _saveRoot.sets[i].slots[u].enchantments.AddRange(SlotData[i].slots[u].enchantments);
  170. _saveRoot.sets[i].slots[u].sockets.Clear();
  171. _saveRoot.sets[i].slots[u].sockets.AddRange(SlotData[i].slots[u].sockets);
  172. }
  173. }
  174. string _json = JsonUtility.ToJson(_saveRoot);
  175. File.WriteAllText(SavePath, _json,System.Text.Encoding.UTF8);
  176. }
  177. }
  178. public void SetSelectedSlot(int _index) //Set a slot to be selected
  179. {
  180. SelectedSlot= _index;
  181. }
  182. public int GetSelectedSlot()//Get the slot which set to be selected.
  183. {
  184. return SelectedSlot;
  185. }
  186. public void UseSelectedSlot()//Use the item in the slot which set to be selected.
  187. {
  188. OnSlotClick(SelectedSlot, 0);
  189. }
  190. public void SetProgressHint(string _text)//Set the hint text of the progress bar.
  191. {
  192. ProgressBarHintString = _text;
  193. ProgressHint.HintString = ProgressBarHintString;
  194. }
  195. public void SetProgress(float _value, float _maxium)//Set the progress bar progress value and maxmium value
  196. {
  197. ProgressBar.transform.localScale = new Vector3(Mathf.Clamp01(_value /Mathf.Max(0.0001F,_maxium)),1F,1F);
  198. ProgressText.text = Mathf.FloorToInt(_value).ToString() + " / " + Mathf.FloorToInt(_maxium).ToString();
  199. }
  200. public KeyCode GetAssignedKey(int _index, int _actionBarIndex=-1) //Get the assigned KeyCode by slot index and action bar index.
  201. {
  202. return SlotData[_actionBarIndex == -1 ? ActionSet : _actionBarIndex].slots[_index].key;
  203. }
  204. public void AssignKey(KeyCode _key, int _index, int _actionBarIndex = -1)//Assign key by slot index and action bar index
  205. {
  206. SlotData[_actionBarIndex == -1 ? ActionSet : _actionBarIndex].slots[_index].key= _key;
  207. if(_actionBarIndex == ActionSet || _actionBarIndex == -1) Slots[_index].UpdateKey();
  208. Save();
  209. }
  210. public bool isKeyAssigned(KeyCode _key, int _actionBarIndex = -1)//Check if a key is already assigned within an action bar.
  211. {
  212. for (int i=0;i< SlotData[_actionBarIndex == -1 ? ActionSet : _actionBarIndex].slots.Count;i++) {
  213. if (SlotData[_actionBarIndex == -1 ? ActionSet : _actionBarIndex].slots[i].key == _key) return true;
  214. }
  215. return false;
  216. }
  217. public void SwapKey(int _indexA,int _indexB, int _actionBarIndex = -1)//Swap keys of two slots within an action bar.
  218. {
  219. KeyCode _keyA = GetAssignedKey(_indexA, _actionBarIndex);
  220. KeyCode _keyB = GetAssignedKey(_indexB, _actionBarIndex);
  221. AssignKey(_keyB, _indexA, _actionBarIndex);
  222. AssignKey(_keyA, _indexB, _actionBarIndex);
  223. }
  224. public void SwitchSet(int _actionBarIndex)//Switch the action bar.
  225. {
  226. SoundManager.Play2D("paper");
  227. ActionSet = _actionBarIndex;
  228. SetNumberText.text = (ActionSet+1).ToString();
  229. for (int i = 0; i < Slots.Length; i++)
  230. {
  231. Slots[i].Item.RegisterClickCallback(i, OnSlotClick);
  232. Slots[i].Item.Outline.color = InventorySkin.instance.ItemSelectedColor;
  233. Slots[i].Item.Fav.GetComponent<Image>().color = InventorySkin.instance.FavoriteColor;
  234. if (i < SlotData[ActionSet].slots.Count)
  235. {
  236. Slots[i].gameObject.SetActive(true);
  237. Slots[i].Initialize(i,SlotData[ActionSet].slots[i], Holder);
  238. }
  239. else
  240. {
  241. Slots[i].gameObject.SetActive(false);
  242. }
  243. }
  244. }
  245. }
  246. }