WarehouseController.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using Newtonsoft.Json;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using Unity.VisualScripting;
  5. using UnityEditor.UIElements;
  6. using UnityEngine;
  7. using UnityEngine.UIElements;
  8. // 这个controller 是用于控制 Warehouse UI菜单的
  9. public class WarehouseController : MonoBehaviour
  10. {
  11. // Start is called before the first frame update
  12. // 主页面元素
  13. private Button foodButton, toyButton, otherButton, backButton;
  14. private List<ItemInWarehouse> warehouseItems = new();
  15. private VisualElement itemListView;
  16. //弹窗元素
  17. private Button yesButton, noButton;
  18. private Label msgBody;
  19. private VisualElement msgRoot, msgField;
  20. // 选中的产品
  21. private string selectedItemId;
  22. void Start()
  23. {
  24. // 基层元素获取
  25. var root = GetComponent<UIDocument>().rootVisualElement;
  26. var mainManu = root.Q<VisualElement>("mainManu");
  27. itemListView = root.Q<VisualElement>("itemListView");
  28. foodButton = mainManu.Q<Button>("food");
  29. foodButton.clicked += () => TabSwitch(foodButton);
  30. toyButton = mainManu.Q<Button>("toy");
  31. toyButton.clicked += () => TabSwitch(toyButton);
  32. otherButton = mainManu.Q<Button>("other");
  33. otherButton.clicked += () => TabSwitch(otherButton);
  34. backButton = root.Q<Button>("back");
  35. // 弹窗元素获取
  36. msgRoot = root.Q<VisualElement>("msgRoot");
  37. msgField = msgRoot.Q<VisualElement>("msgField");
  38. msgBody = msgField.Q<Label>("msgBody");
  39. yesButton = msgField.Q<Button>("msgYes");
  40. noButton = msgField.Q<Button>("msgNo");
  41. noButton.clicked += MsgNoClick;
  42. //初始化设定
  43. LanguageSetting();
  44. //DataLoading();
  45. GetItemList("food");
  46. foodButton.transform.scale = new Vector3(1.2f, 1.2f);
  47. InstallItems("food");
  48. }
  49. // Update is called once per frame
  50. //void Update()
  51. //{
  52. //}
  53. //菜单语言设定
  54. void LanguageSetting()
  55. {
  56. // 设置 shopping UI界面里面label和按键的语言显示
  57. string textValue = EnviromentSetting.GetValueAtPath(EnviromentSetting.languageData, new string[] { "shoppingUI", "button", "food", EnviromentSetting.languageCode });
  58. foodButton.text = textValue;
  59. textValue = EnviromentSetting.GetValueAtPath(EnviromentSetting.languageData, new string[] { "shoppingUI", "button", "toy", EnviromentSetting.languageCode });
  60. toyButton.text = textValue;
  61. textValue = EnviromentSetting.GetValueAtPath(EnviromentSetting.languageData, new string[] { "shoppingUI", "button", "other", EnviromentSetting.languageCode });
  62. otherButton.text = textValue;
  63. textValue = EnviromentSetting.GetValueAtPath(EnviromentSetting.languageData, new string[] { "shoppingUI", "button", "back", EnviromentSetting.languageCode });
  64. backButton.text = textValue;
  65. }
  66. //获取所有产品列表,切换商品分类之后,需要重新读取和加载
  67. void GetItemList(string type)
  68. {
  69. // 按照类别获取warehouse
  70. warehouseItems.Clear();
  71. // todo 下面这一端要重写。Warehouse获取清单方式和Shopping UI不同
  72. if(UserProperty.itemStocks.TryGetValue(type, out List < ItemStock > items)){
  73. //List<ItemStock> items = UserProperty.itemStocks[type];
  74. // itemDict获取当前分类下所有item
  75. string itemRawData = EnviromentSetting.GetValueAtPath(EnviromentSetting.languageData, new string[] { "shoppingUI", "item", type });
  76. Dictionary<string, object> itemDict = JsonConvert.DeserializeObject<Dictionary<string, object>>(itemRawData);
  77. foreach (ItemStock item in items)
  78. {
  79. var itemDetail = JsonConvert.DeserializeObject<Dictionary<string, object>>(itemDict[item.id].ToString());
  80. string price = itemDetail["price"].ToString();
  81. string picture = itemDetail["picture"].ToString();
  82. var desc = JsonConvert.DeserializeObject<Dictionary<string, object>>(itemDetail["description"].ToString());
  83. string description = desc[EnviromentSetting.languageCode].ToString();
  84. ItemInWarehouse searchedItem = new(item.id, description, price, picture);
  85. warehouseItems.Add(searchedItem);
  86. }
  87. }
  88. }
  89. // 将items装载到菜单中,切换商品分类之后,需要重新读取和加载
  90. void InstallItems(string type)
  91. {
  92. var root = GetComponent<UIDocument>().rootVisualElement;
  93. itemListView.Clear();
  94. VisualTreeAsset itemResource = Resources.Load<VisualTreeAsset>("Shopping/Item");
  95. List<ItemInWarehouse> items = new();
  96. foreach (var item in warehouseItems)
  97. {
  98. var itemFrame = new VisualElement();
  99. itemFrame = itemResource.CloneTree();
  100. var description = itemFrame.Q<Label>("description");
  101. description.text = item.description;
  102. // 这里复用 shopping UI 里面的 item.uxml
  103. // UI 里面命名为price,实际是qty
  104. var price = itemFrame.Q<Label>("price");
  105. price.text = item.qty;
  106. var texture = Resources.Load<Texture2D>(item.picture);
  107. var picture = itemFrame.Q<VisualElement>("picture");
  108. picture.style.backgroundImage = new StyleBackground(texture);
  109. itemFrame.RegisterCallback<ClickEvent>(e => ItemClick(e, item.id, item.description));
  110. itemListView.Add(itemFrame);
  111. }
  112. }
  113. // 点击商品后跳出确认窗口
  114. void ItemClick(ClickEvent clickEvent, string itemId, string description=null)
  115. {
  116. selectedItemId = itemId;
  117. msgBody.text = description;
  118. msgRoot.visible = true;
  119. }
  120. // 数据读取
  121. void DataLoading()
  122. {
  123. }
  124. void TabSwitch(Button btn)
  125. {
  126. itemListView.style.backgroundColor = btn.resolvedStyle.backgroundColor;
  127. foodButton.transform.scale = new Vector3(1f, 1f, 1);
  128. toyButton.transform.scale = new Vector3(1f, 1f, 1);
  129. otherButton.transform.scale = new Vector3(1f, 1f, 1);
  130. btn.transform.scale = new Vector3(1.2f, 1.2f, 1);
  131. itemListView.Clear();
  132. // 这里btn.name必须和json里面商品类别完全匹配
  133. GetItemList(btn.name);
  134. InstallItems(btn.name);
  135. }
  136. // msg no button 点击
  137. void MsgNoClick()
  138. {
  139. msgRoot.visible = false;
  140. }
  141. // msg yes button 点击
  142. // todo 以后在这里添加调出确认页面,确认后将道具使用请求给服务器
  143. void MsgYesClick()
  144. {
  145. Debug.Log("msg yes clicked");
  146. msgRoot.visible = false;
  147. }
  148. }
  149. public class ItemInWarehouse
  150. {
  151. public string id;
  152. public string description;
  153. public string picture;
  154. public string qty;
  155. public ItemInWarehouse(string id, string desc, string qty, string picture)
  156. {
  157. this.id = id;
  158. this.description = desc;
  159. this.qty = qty;
  160. this.picture = picture;
  161. }
  162. }