123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- using Newtonsoft.Json;
- using System.Collections;
- using System.Collections.Generic;
- using Unity.VisualScripting;
- using UnityEngine;
- using UnityEngine.UIElements;
- /* 这个controller 是用于控制 Warehouse UI菜单的
- */
- public class WarehouseController : MonoBehaviour
- {
- // Start is called before the first frame update
- // 主页面元素
- private Button foodButton, toyButton, otherButton, backButton;
- private List<ItemInWarehouse> warehouseItems = new();
- private VisualElement itemListView;
- //弹窗元素
- private Button yesButton, noButton;
- private Label msgBody;
- private TextField qtyField;
- private VisualElement msgRoot, msgField;
- // 选中的产品
- private string selectedItemId;
- private string currentTab;
- void OnEnable()
- {
- // 基层元素获取
- var root = GetComponent<UIDocument>().rootVisualElement;
- var mainManu = root.Q<VisualElement>("mainManu");
- itemListView = root.Q<VisualElement>("itemListView");
- foodButton = mainManu.Q<Button>("food");
- foodButton.clicked += () => TabSwitch(foodButton);
- toyButton = mainManu.Q<Button>("toy");
- toyButton.clicked += () => TabSwitch(toyButton);
- otherButton = mainManu.Q<Button>("other");
- otherButton.clicked += () => TabSwitch(otherButton);
- backButton = root.Q<Button>("back");
- // 绑定事件
- backButton.clicked += BackBtnClick;
- // 弹窗元素获取
- msgRoot = root.Q<VisualElement>("msgRoot");
- msgField = msgRoot.Q<VisualElement>("msgField");
- msgBody = msgField.Q<Label>("msgBody");
- yesButton = msgField.Q<Button>("msgYes");
- yesButton.clicked += MsgYesClick;
- noButton = msgField.Q<Button>("msgNo");
- noButton.clicked += MsgNoClick;
- qtyField = msgField.Q<TextField>("qtyField");
- //初始化设定
- LanguageSetting();
- foodButton.transform.scale = new Vector3(1.2f, 1.2f);
- InstallItems("food");
- qtyField.visible = false;
- }
- private void OnDisable()
- {
- warehouseItems.Clear(); // 因为启动时候调用OnEable数据会被重复加载
- }
- // Update is called once per frame
- //void Update()
- //{
- //}
- //菜单语言设定
- void LanguageSetting()
- {
- // 设置 shopping UI界面里面label和按键的语言显示
- string textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "shoppingUI", "button", "food", EnviromentSetting.languageCode });
- foodButton.text = textValue;
- textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "shoppingUI", "button", "toy", EnviromentSetting.languageCode });
- toyButton.text = textValue;
- textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "shoppingUI", "button", "other", EnviromentSetting.languageCode });
- otherButton.text = textValue;
- textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "shoppingUI", "button", "back", EnviromentSetting.languageCode });
- backButton.text = textValue;
- textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "shoppingUI", "button", "yes", EnviromentSetting.languageCode });
- yesButton.text = textValue;
- textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "shoppingUI", "button", "no", EnviromentSetting.languageCode });
- noButton.text = textValue;
- }
- // 将items装载到菜单中,切换商品分类之后,需要重新读取和加载
- void InstallItems(string type)
- {
- currentTab = type;
- var root = GetComponent<UIDocument>().rootVisualElement;
- itemListView.Clear();
- VisualTreeAsset itemResource = Resources.Load<VisualTreeAsset>("Shopping/Item");
- Dictionary<string, int> items = new();
- if (type == "food")
- {
- items = UserProperty.food;
- }
- else if (type == "toy")
- {
- items = UserProperty.toy;
- }
- else if (type == "other")
- {
- items = UserProperty.other;
- }
- foreach (var item in items)
- {
- if (item.Value > 0)
- {
- var itemFrame = new VisualElement();
- itemFrame = itemResource.CloneTree();
- // 从language.json读取item的文字描述
- string desc = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "item", type, item.Key, "description", EnviromentSetting.languageCode });
- var description = itemFrame.Q<Label>("description");
- description.text = desc;
- // 这里复用 shopping UI 里面的 item.uxml
- // UI 里面命名为price,实际是qty
- var price = itemFrame.Q<Label>("price");
- price.text = item.Value.ToString();
- // 读取图片
- string picturePath = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "item", type, item.Key, "picture" });
- var texture = Resources.Load<Texture2D>(picturePath);
- var picture = itemFrame.Q<VisualElement>("picture");
- picture.style.backgroundImage = new StyleBackground(texture);
- itemFrame.RegisterCallback<ClickEvent>(e => ItemClick(e, item.Key, desc));
- itemListView.Add(itemFrame);
- }
- }
- }
- // 点击商品后跳出确认窗口
- void ItemClick(ClickEvent clickEvent, string itemId, string description = null)
- {
- selectedItemId = itemId;
- string targetType = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "item", currentTab, itemId, "target" });
- targetType = currentTab + "_" + targetType;
- string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "warehouseUI", "message", targetType, EnviromentSetting.languageCode });
- if (msg.Contains("<<dog_name>>"))
- {
- msg = msg.Replace("<<dog_name>>", UserProperty.dogs[GameData.focusDog].dog_name);
- }
- if (msg.Contains("<<item_name>>"))
- {
- msg = msg.Replace("<<item_name>>", description);
- }
- msgBody.text = msg;
- msgRoot.visible = true;
- }
- void BackBtnClick()
- {
- HomeSoundEffectController.Instance.PlaySoundEffect(2);
- var uiPlaceholder = GameObject.Find("UI Placeholder");
- if (uiPlaceholder != null)
- {
- var warehouseUI = uiPlaceholder.transform.Find("Warehouse").gameObject;
- var vamUI = uiPlaceholder.transform.Find("VoiceAndMenu").gameObject;
- warehouseUI.SetActive(false);
- vamUI.SetActive(true);
- }
- }
- void TabSwitch(Button btn)
- {
- itemListView.style.backgroundColor = btn.resolvedStyle.backgroundColor;
- foodButton.transform.scale = new Vector3(1f, 1f, 1);
- toyButton.transform.scale = new Vector3(1f, 1f, 1);
- otherButton.transform.scale = new Vector3(1f, 1f, 1);
- btn.transform.scale = new Vector3(1.2f, 1.2f, 1);
- itemListView.Clear();
- HomeSoundEffectController.Instance.PlaySoundEffect(0);
- // 这里btn.name必须和json里面商品类别完全匹配
- //GetItemList(btn.name);
- InstallItems(btn.name);
- }
- // msg no button 点击
- void MsgNoClick()
- {
- msgRoot.visible = false;
- }
- void MsgYesClick()
- {
- //Debug.Log("msg yes clicked");
- msgRoot.visible = false;
- //ItemUseController itemUseController = new ItemUseController();
- ItemUseController.ItemUsed(selectedItemId);
- HomeSoundEffectController.Instance.PlaySoundEffect(1);
- // 关闭菜单
- var uiPlaceholder = GameObject.Find("UI Placeholder");
- if (uiPlaceholder != null)
- {
- var warehouseUI = uiPlaceholder.transform.Find("Warehouse").gameObject;
- var vamUI = uiPlaceholder.transform.Find("VoiceAndMenu").gameObject;
- warehouseUI.SetActive(false);
- vamUI.SetActive(true);
- }
- }
- }
- public class ItemInWarehouse
- {
- public string id;
- public string description;
- public string picture;
- public string qty;
- public ItemInWarehouse(string id, string desc, string qty, string picture)
- {
- this.id = id;
- this.description = desc;
- this.qty = qty;
- this.picture = picture;
- }
- }
|