123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using Newtonsoft.Json;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- using UnityEngine.UI;
- using UnityEngine.UIElements;
- using ZXing;
- using ZXing.QrCode;
- //using UnityEngine.UI;
- using Button = UnityEngine.UIElements.Button;
- //using Image = UnityEngine.UIElements.Image;
- /* 这个controller 是用于控制 Status UI菜单的
- * 包含显示狗的最新状态
- * 添加狗,把狗送给朋友,删除狗
- */
- public class StatusController : MonoBehaviour
- {
- private Button backButton, cancelButton;
- private Label nameLabel, genderLabel, breedLabel, hostLabel, hostnameLabel, ageLabel, ageValueLabel, commandLabel, commandValueLabel, statusLabel, statusValueLabel, resetVoice;
- private Label leftArrow, rightArrow;
- private Label addLabel, transfer, remove, callback;
-
-
- private float transferCountStartTime;
- DogProperty puppy;
- private VisualElement selectElement, statusElement, transferElement, QRcode;
- private StausUIPage currentPage = StausUIPage.Status;
- // 转移狗使用的变量
- private Label transferMsgLabel;
- private string transferMsgOrigin;
- private string transferCode;
- // Start is called before the first frame update
- void OnEnable()
- {
- var root = GetComponent<UIDocument>().rootVisualElement;
- // 状态相关的控件
- statusElement = root.Q<VisualElement>("statusUI");
- backButton = root.Q<Button>("back");
- nameLabel = root.Q<Label>("name");
- genderLabel = root.Q<Label>("gender");
- breedLabel = root.Q<Label>("breed");
- hostLabel = root.Q<Label>("host");
- hostnameLabel = root.Q<Label>("hostname");
- ageLabel = root.Q<Label>("age");
- ageValueLabel = root.Q<Label>("ageValue");
- commandLabel = root.Q<Label>("command");
- commandValueLabel = root.Q<Label>("commandValue");
- resetVoice = root.Q<Label>("resetVoice");
- statusLabel = root.Q<Label>("status");
- statusValueLabel = root.Q<Label>("statusValue");
- selectElement = root.Q("selectElement");
- leftArrow = root.Q<Label>("leftArrow");
- rightArrow = root.Q<Label>("rightArrow");
- addLabel = root.Q<Label>("add");
- transfer = root.Q<Label>("transfer");
- remove = root.Q<Label>("remove");
- callback = root.Q<Label>("callback");
- // 转移狗的相关控件
- transferMsgLabel = root.Q<Label>("transferMsg");
- QRcode = root.Q<VisualElement>("QRcode");
- cancelButton = root.Q<Button>("cancel");
- transferElement = root.Q<VisualElement>("transferElement");
- // 绑定事件
- backButton.clicked += BackBtnClick;
- cancelButton.clicked += CancelTransferBtnClick;
- leftArrow.RegisterCallback<ClickEvent>(e => LeftArrowClicked(e));
- rightArrow.RegisterCallback<ClickEvent>(e => RightArrowClicked(e));
- addLabel.RegisterCallback<ClickEvent>(e => AddClick(e));
- remove.RegisterCallback<ClickEvent>(e => RemoveClick(e));
- transfer.RegisterCallback<ClickEvent>(e => TransferClick(e));
- resetVoice.RegisterCallback<ClickEvent>(e => ResetVoiceClick(e));
- // 箭头是否显示
- if (UserProperty.dogs.Count > 1)
- {
- leftArrow.visible = true;
- rightArrow.visible = true;
- }
- else
- {
- leftArrow.visible = false;
- rightArrow.visible = false;
- }
- // 根据用户名下狗的数量判断是否显示transfer按键
- if (UserProperty.dogs.Count > 1)
- {
- transfer.style.display = DisplayStyle.Flex;
- }
- else
- {
- transfer.style.display = DisplayStyle.None;
- }
- // 如果用户狗的数量达到系统规定上限,则隐藏add按键
- if (UserProperty.dogs.Count >= EnviromentSetting.maxDogQty)
- {
- addLabel.style.display = DisplayStyle.None;
- }
- else
- {
- addLabel.style.display = DisplayStyle.Flex;
- }
- }
- //private void Start()
- //{
- //}
- // Update is called once per frame
- void Update()
- {
- // 刷新狗的数据
- // puppy = UserProperty.dogs[GameData.focusDog];
- // StatusPageUpdate();
- // LabelLanguageSetting();
- // StatusSummary();
- if (currentPage == StausUIPage.Transfer)
- {
- float ts = Time.time - transferCountStartTime;
- if (ts > 120f)
- {
- // todo 超过120秒,关闭二维码
- transferElement.style.display = DisplayStyle.None;
- statusElement.style.display = DisplayStyle.Flex;
- currentPage = StausUIPage.Status;
- }
- else
- {
- // todo 未超过120秒,保持二维码显示
- transferElement.style.display = DisplayStyle.Flex;
- statusElement.style.display = DisplayStyle.None;
- currentPage = StausUIPage.Transfer;
- string remainingTime = Mathf.RoundToInt(120 - ts).ToString();
- string transferMsg = transferMsgOrigin.Replace("<<second>>", remainingTime);
- transferMsgLabel.text = transferMsg;
- }
- }
- if (currentPage == StausUIPage.Status)
- {
- // 刷新狗的数据
- puppy = UserProperty.dogs[GameData.focusDog];
- StatusPageUpdate();
- LabelLanguageSetting();
- StatusSummary();
- CommandSummary();
- }
- }
- //void BackPressed()
- //{
- // if (backButton != null)
- // {
- // var root = GetComponent<UIDocument>();
- // Destroy(GameObject.Find(root.GetComponentInParent<Canvas>().name));
- // }
- //}
- void StatusPageUpdate()
- {
- //backButton.clicked += BackPressed;
- nameLabel.text = puppy.dog_name;
- if (puppy.sex == 1)
- {
- genderLabel.text = "♂";
- }
- else
- {
- genderLabel.text = "♀";
- }
- breedLabel.text = puppy.breed;
- foreach (var breed in EnviromentSetting.dogBreeds)
- {
- if (breed.breed == puppy.breed)
- {
- breedLabel.text = breed.name[EnviromentSetting.languageCode];
- break;
- }
- }
- hostnameLabel.text = UserProperty.name;
- // 计算狗生日和现在时间差
- TimeSpan ts = System.DateTime.Now - puppy.birthday;
- ageValueLabel.text = ts.Days.ToString();
- }
- void LabelLanguageSetting()
- {
- // 设置status UI界面里面label和按键的语言显示
- string textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "button", "back", EnviromentSetting.languageCode });
- backButton.text = textValue;
- textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "button", "cancel", EnviromentSetting.languageCode });
- cancelButton.text = textValue;
- textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "label", "host", EnviromentSetting.languageCode });
- hostLabel.text = textValue;
- textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "label", "days", EnviromentSetting.languageCode });
- ageLabel.text = textValue;
- textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "label", "status", EnviromentSetting.languageCode });
- statusLabel.text = textValue;
- textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "label", "command", EnviromentSetting.languageCode });
- commandLabel.text = textValue;
- textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "label", "resetVoice", EnviromentSetting.languageCode });
- resetVoice.text = textValue;
- textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "message", "scan_QRcode", EnviromentSetting.languageCode });
- transferMsgOrigin = textValue;
- }
- void CommandSummary()
- {
- // 先清空状态文字,应对切换多只狗
- commandValueLabel.text = null;
- // 汇总宠物状态
- string summary = String.Empty;
- if (puppy.commandSit)
- {
- summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "command", "Sit", EnviromentSetting.languageCode });
- summary += ", ";
- }
- if (puppy.commandStand)
- {
- summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "command", "Stand", EnviromentSetting.languageCode });
- summary += ", ";
- }
- if (puppy.commandBark)
- {
- summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "command", "Bark", EnviromentSetting.languageCode });
- summary += ", ";
- }
- if (puppy.commandShake)
- {
- summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "command", "Shake", EnviromentSetting.languageCode });
- summary += ", ";
- }
- if (puppy.commandLieDown)
- {
- summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "command", "LieDown", EnviromentSetting.languageCode });
- summary += ", ";
- }
- if (puppy.commandTouch)
- {
- summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "command", "Touch", EnviromentSetting.languageCode });
- summary += ", ";
- }
- if (puppy.commandDeath)
- {
- summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "command", "Death", EnviromentSetting.languageCode });
- summary += ", ";
- }
- if (puppy.commandTurnL)
- {
- summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "command", "TurnL", EnviromentSetting.languageCode });
- summary += ", ";
- }
- if (puppy.commandTurnR)
- {
- summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "command", "TurnR", EnviromentSetting.languageCode });
- summary += ", ";
- }
- if (summary == String.Empty)
- {
- summary = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "command", "none", EnviromentSetting.languageCode });
- }
- else
- {
- // 去掉最后一个逗号
- summary = summary.Substring(0, summary.Length - 2);
- commandValueLabel.text = summary;
- }
- }
- void StatusSummary()
- {
- // 先清空状态文字,应对切换多只狗
- statusValueLabel.text = null;
- // 汇总宠物状态
- string summary = "";
- if (puppy.satiety < 30)
- {
- if (puppy.satiety < 10)
- {
- // 小于10,达到L2警告
- summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "satiety_2", EnviromentSetting.languageCode });
- }
- else
- {
- // 不小于10,触发L1警告
- summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "satiety_1", EnviromentSetting.languageCode });
- }
- summary += "<br>";
- }
- if (puppy.stamina < 30)
- {
- if (puppy.stamina < 10)
- {
- // 小于10,达到L2警告
- summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "stamina_2", EnviromentSetting.languageCode });
- }
- else
- {
- // 不小于10,触发L1警告
- summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "stamina_1", EnviromentSetting.languageCode });
- }
- summary += "<br>";
- }
- if (puppy.thirsty < 30)
- {
- if (puppy.thirsty < 10)
- {
- // 小于10,达到L2警告
- summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "thirsty_2", EnviromentSetting.languageCode });
- }
- else
- {
- // 不小于10,触发L1警告
- summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "thirsty_1", EnviromentSetting.languageCode });
- }
- summary += "<br>";
- }
- if (puppy.healthy < 30)
- {
- if (puppy.healthy < 10)
- {
- // 小于10,达到L2警告
- summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "healthy_2", EnviromentSetting.languageCode });
- }
- else
- {
- // 不小于10,触发L1警告
- summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "healthy_1", EnviromentSetting.languageCode });
- }
- summary += "<br>";
- }
- if (puppy.clean < 30)
- {
- if (puppy.clean < 10)
- {
- // 小于10,达到L2警告
- summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "clean_2", EnviromentSetting.languageCode });
- }
- else
- {
- // 不小于10,触发L1警告
- summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "clean_1", EnviromentSetting.languageCode });
- }
- summary += "<br>";
- }
- if (puppy.obesity > 70)
- {
- if (puppy.obesity > 90)
- {
- // 小于10,达到L2警告
- summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "obesity_2", EnviromentSetting.languageCode });
- }
- else
- {
- // 不小于10,触发L1警告
- summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "obesity_1", EnviromentSetting.languageCode });
- }
- summary += "<br>";
- }
- // 如果没有任何异常,返回正常的状态
- if (summary.Length == 0)
- {
- summary = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "normal", EnviromentSetting.languageCode });
- }
- statusValueLabel.text = summary;
- }
- void RightArrowClicked(ClickEvent e)
- {
- GameData.focusDog++;
- if (GameData.focusDog == UserProperty.dogs.Count)
- {
- GameData.focusDog = 0;
- }
- }
- void LeftArrowClicked(ClickEvent e)
- {
- GameData.focusDog--;
- if (GameData.focusDog == -1)
- {
- GameData.focusDog = UserProperty.dogs.Count - 1;
- }
- }
- void ResetVoiceClick(ClickEvent e)
- {
- // todo 重置狗的语音训练数据提示
- string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", "reset_voice_reminder", EnviromentSetting.languageCode });
- msg = msg.Replace("<<dog_name>>", UserProperty.dogs[GameData.focusDog].dog_name);
- MessageBoxController.YorN_Message(msg, ResetVoiceRequest);
- }
- void ResetVoiceRequest()
- {
- // todo 重置狗的声音提交
- string url = "/api/voice/reset/";
- Dictionary<string, string> formData = new();
- WWWForm form = new();
- form.AddField("user_id", UserProperty.userId);
- form.AddField("dog_id", UserProperty.GetDogIdByIndex(GameData.focusDog));
- form.AddField("type", "all");
- StartCoroutine(WebController.PostRequest(url, form, callback: ResetVoiceCallback));
- }
- void ResetVoiceCallback(string json)
- {
- // todo 重置狗的声音提交成功
- var data = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
- if (data != null && data["status"].ToString() == "success")
- {
- // 跳转login场景,重新登录
- MaskTransitions.TransitionManager.Instance.LoadLevel("Login");
- }
- else
- {
- Debug.Log(data["message"]);
- }
- }
- // 点击返回按钮
- void BackBtnClick()
- {
- var uiPlaceholder = GameObject.Find("UI Placeholder");
- if (uiPlaceholder != null)
- {
- var shoppingUI = uiPlaceholder.transform.Find("Status").gameObject;
- var vamUI = uiPlaceholder.transform.Find("VoiceAndMenu").gameObject;
- shoppingUI.SetActive(false);
- vamUI.SetActive(true);
- }
- }
- void CancelTransferBtnClick()
- {
- CancelTransferDogRequest();
- statusElement.style.display = DisplayStyle.Flex;
- transferElement.style.display = DisplayStyle.None;
- currentPage = StausUIPage.Status;
- }
- void AddClick(ClickEvent e)
- {
- // if (UserProperty.level == "basic")
- // {
- // string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", "add_dog_fail_account_level", EnviromentSetting.languageCode });
- // MessageBoxController.ShowMessage(msg);
- // }
- if (UserProperty.dogs.Count >= EnviromentSetting.maxDogQty)
- {
- string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", "add_dog_fail_no_more", EnviromentSetting.languageCode });
- MessageBoxController.ShowMessage(msg);
- }
- else
- {
- string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", "add_dog_prompt", EnviromentSetting.languageCode });
- MessageBoxController.YorN_Message(msg, SwitchAddNewDog);
- }
- }
- // 跳转login 场景添加新的狗
- void SwitchAddNewDog()
- {
- GameData.subScene = "Login_InitDog";
- MaskTransitions.TransitionManager.Instance.LoadLevel("Login");
- }
- void RemoveClick(ClickEvent e)
- {
- string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", "foster_dog_prompt", EnviromentSetting.languageCode });
- msg = msg.Replace("<<dog name>>", UserProperty.dogs[GameData.focusDog].dog_name);
- MessageBoxController.YorN_Message(msg, FosterDog);
- }
- // 点击确认remove后调用寄养的程序
- void FosterDog()
- {
- // 检测用户是否有200金币
- if (UserProperty.coin >= 200)
- {
- // todo 寄养某一只狗,并扣除金币
- }
- }
- // 找回寄养的狗
- void CallbackDogClick(ClickEvent e)
- {
- }
- void TransferClick(ClickEvent e)
- {
- // TODO 添加代码增加面对面赠送狗的功能
- string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", "transfer_dog_prompt", EnviromentSetting.languageCode });
- msg = msg.Replace("<<dog name>>", UserProperty.dogs[GameData.focusDog].dog_name);
- MessageBoxController.YorN_Message(msg, TransferDogRequest);
- }
- void TransferDogRequest()
- {
- string url = "/api/transfer_dog/send/";
- Dictionary<string, string> formData = new();
- WWWForm form = new();
- form.AddField("user_id", UserProperty.userId);
- form.AddField("dog_id", UserProperty.GetDogIdByIndex(GameData.focusDog));
- StartCoroutine(WebController.PostRequest(url, form, callback: TransferDogCallback));
- }
- void TransferDogCallback(string json)
- {
- var data = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
- if (data != null && data["status"].ToString() == "success")
- {
- transferCountStartTime = Time.time;
- currentPage = StausUIPage.Transfer;
- transferCode = data["transfer_code"].ToString();
- transferCode = "ARdog://transfer/" + transferCode;
- Texture2D qrCodeTexture = GenerateQRCode(transferCode);
- QRcode.style.backgroundImage = new StyleBackground(qrCodeTexture);
- }
- else
- {
- Debug.Log(data["message"]);
- }
- }
- void CancelTransferDogRequest()
- {
- string url = "/api/transfer_dog/cancel/";
- Dictionary<string, string> formData = new();
- WWWForm form = new();
- form.AddField("user_id", UserProperty.userId);
- form.AddField("transfer_code", transferCode);
- StartCoroutine(WebController.PostRequest(url, form, callback: CancelTransferDogCallback));
- }
- void CancelTransferDogCallback(string json)
- {
- var data = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
- if (data != null && data["status"].ToString() == "success")
- {
- Debug.Log("取消转移成功");
- }
- else
- {
- Debug.Log(data["message"]);
- }
- }
- Texture2D GenerateQRCode(string text)
- {
- // 创建二维码编码器
- BarcodeWriter barcodeWriter = new BarcodeWriter
- {
- Format = BarcodeFormat.QR_CODE,
- Options = new QrCodeEncodingOptions
- {
- Width = 256,
- Height = 256
- }
- };
- // 生成二维码
- Color32[] color32 = barcodeWriter.Write(text);
- // 创建纹理并设置像素
- Texture2D qrCodeTexture = new Texture2D(256, 256);
- qrCodeTexture.SetPixels32(color32);
- qrCodeTexture.Apply();
- return qrCodeTexture;
- // 将纹理转换为Sprite
- // Sprite qrCodeSprite = Sprite.Create(qrCodeTexture, new Rect(0, 0, qrCodeTexture.width, qrCodeTexture.height), new Vector2(0.5f, 0.5f));
- // return qrCodeSprite;
- }
- }
- enum StausUIPage
- {
- Status,
- Transfer,
- }
|