|
@@ -4,8 +4,16 @@ using System.Collections.Generic;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json;
|
|
using UnityEngine;
|
|
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
+using UnityEngine.UI;
|
|
using UnityEngine.UIElements;
|
|
using UnityEngine.UIElements;
|
|
|
|
|
|
|
|
+
|
|
|
|
+using ZXing;
|
|
|
|
+using ZXing.QrCode;
|
|
|
|
+//using UnityEngine.UI;
|
|
|
|
+using Button = UnityEngine.UIElements.Button;
|
|
|
|
+//using Image = UnityEngine.UIElements.Image;
|
|
|
|
+
|
|
/* 这个controller 是用于控制 Status UI菜单的
|
|
/* 这个controller 是用于控制 Status UI菜单的
|
|
* 包含显示狗的最新状态
|
|
* 包含显示狗的最新状态
|
|
* 添加狗,把狗送给朋友,删除狗
|
|
* 添加狗,把狗送给朋友,删除狗
|
|
@@ -13,23 +21,27 @@ using UnityEngine.UIElements;
|
|
public class StatusController : MonoBehaviour
|
|
public class StatusController : MonoBehaviour
|
|
{
|
|
{
|
|
private Button backButton, cancelButton;
|
|
private Button backButton, cancelButton;
|
|
- private Label nameLabel, genderLabel, breedLabel, hostLabel, hostnameLabel, ageLabel, ageValueLabel,commandLabel, commandValueLabel, statusLabel, statusValueLabel, resetVoice;
|
|
|
|
|
|
+ private Label nameLabel, genderLabel, breedLabel, hostLabel, hostnameLabel, ageLabel, ageValueLabel, commandLabel, commandValueLabel, statusLabel, statusValueLabel, resetVoice;
|
|
private Label leftArrow, rightArrow;
|
|
private Label leftArrow, rightArrow;
|
|
private Label addLabel, transfer, remove, callback;
|
|
private Label addLabel, transfer, remove, callback;
|
|
- private Label transferMsgLabel;
|
|
|
|
- private string transferMsgBasic;
|
|
|
|
- private DateTime transferCountStartTime = new DateTime();
|
|
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ private float transferCountStartTime;
|
|
DogProperty puppy;
|
|
DogProperty puppy;
|
|
- private VisualElement selectElement, statusElement, transferElement;
|
|
|
|
|
|
+ private VisualElement selectElement, statusElement, transferElement, QRcode;
|
|
private StausUIPage currentPage = StausUIPage.Status;
|
|
private StausUIPage currentPage = StausUIPage.Status;
|
|
|
|
+
|
|
|
|
+ // 转移狗使用的变量
|
|
|
|
+ private Label transferMsgLabel;
|
|
|
|
+ private string transferMsgOrigin;
|
|
|
|
+ private string transferCode;
|
|
// Start is called before the first frame update
|
|
// Start is called before the first frame update
|
|
void OnEnable()
|
|
void OnEnable()
|
|
{
|
|
{
|
|
var root = GetComponent<UIDocument>().rootVisualElement;
|
|
var root = GetComponent<UIDocument>().rootVisualElement;
|
|
- statusElement = root.Q("statusElement");
|
|
|
|
- transferElement = root.Q("transferElement");
|
|
|
|
|
|
+ // 状态相关的控件
|
|
|
|
+ statusElement = root.Q<VisualElement>("statusUI");
|
|
backButton = root.Q<Button>("back");
|
|
backButton = root.Q<Button>("back");
|
|
- cancelButton = root.Q<Button>("cancel");
|
|
|
|
nameLabel = root.Q<Label>("name");
|
|
nameLabel = root.Q<Label>("name");
|
|
genderLabel = root.Q<Label>("gender");
|
|
genderLabel = root.Q<Label>("gender");
|
|
breedLabel = root.Q<Label>("breed");
|
|
breedLabel = root.Q<Label>("breed");
|
|
@@ -49,11 +61,16 @@ public class StatusController : MonoBehaviour
|
|
transfer = root.Q<Label>("transfer");
|
|
transfer = root.Q<Label>("transfer");
|
|
remove = root.Q<Label>("remove");
|
|
remove = root.Q<Label>("remove");
|
|
callback = root.Q<Label>("callback");
|
|
callback = root.Q<Label>("callback");
|
|
|
|
+
|
|
|
|
+ // 转移狗的相关控件
|
|
transferMsgLabel = root.Q<Label>("transferMsg");
|
|
transferMsgLabel = root.Q<Label>("transferMsg");
|
|
|
|
+ QRcode = root.Q<VisualElement>("QRcode");
|
|
|
|
+ cancelButton = root.Q<Button>("cancel");
|
|
|
|
+ transferElement = root.Q<VisualElement>("transferElement");
|
|
|
|
|
|
// 绑定事件
|
|
// 绑定事件
|
|
backButton.clicked += BackBtnClick;
|
|
backButton.clicked += BackBtnClick;
|
|
- cancelButton.clicked += CancelBtnClick;
|
|
|
|
|
|
+ cancelButton.clicked += CancelTransferBtnClick;
|
|
leftArrow.RegisterCallback<ClickEvent>(e => LeftArrowClicked(e));
|
|
leftArrow.RegisterCallback<ClickEvent>(e => LeftArrowClicked(e));
|
|
rightArrow.RegisterCallback<ClickEvent>(e => RightArrowClicked(e));
|
|
rightArrow.RegisterCallback<ClickEvent>(e => RightArrowClicked(e));
|
|
addLabel.RegisterCallback<ClickEvent>(e => AddClick(e));
|
|
addLabel.RegisterCallback<ClickEvent>(e => AddClick(e));
|
|
@@ -109,14 +126,24 @@ public class StatusController : MonoBehaviour
|
|
// StatusSummary();
|
|
// StatusSummary();
|
|
if (currentPage == StausUIPage.Transfer)
|
|
if (currentPage == StausUIPage.Transfer)
|
|
{
|
|
{
|
|
- TimeSpan ts = DateTime.Now - transferCountStartTime;
|
|
|
|
- if (ts.TotalSeconds > 120)
|
|
|
|
|
|
+ float ts = Time.time - transferCountStartTime;
|
|
|
|
+ if (ts > 120f)
|
|
{
|
|
{
|
|
// todo 超过120秒,关闭二维码
|
|
// todo 超过120秒,关闭二维码
|
|
transferElement.style.display = DisplayStyle.None;
|
|
transferElement.style.display = DisplayStyle.None;
|
|
statusElement.style.display = DisplayStyle.Flex;
|
|
statusElement.style.display = DisplayStyle.Flex;
|
|
currentPage = StausUIPage.Status;
|
|
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)
|
|
if (currentPage == StausUIPage.Status)
|
|
@@ -163,7 +190,7 @@ public class StatusController : MonoBehaviour
|
|
hostnameLabel.text = UserProperty.name;
|
|
hostnameLabel.text = UserProperty.name;
|
|
|
|
|
|
// 计算狗生日和现在时间差
|
|
// 计算狗生日和现在时间差
|
|
- TimeSpan ts = System.DateTime.Now - puppy.brithday;
|
|
|
|
|
|
+ TimeSpan ts = System.DateTime.Now - puppy.birthday;
|
|
ageValueLabel.text = ts.Days.ToString();
|
|
ageValueLabel.text = ts.Days.ToString();
|
|
|
|
|
|
}
|
|
}
|
|
@@ -185,7 +212,7 @@ public class StatusController : MonoBehaviour
|
|
textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "label", "resetVoice", EnviromentSetting.languageCode });
|
|
textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "label", "resetVoice", EnviromentSetting.languageCode });
|
|
resetVoice.text = textValue;
|
|
resetVoice.text = textValue;
|
|
textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "message", "scan_QRcode", EnviromentSetting.languageCode });
|
|
textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "message", "scan_QRcode", EnviromentSetting.languageCode });
|
|
- transferMsgBasic = textValue;
|
|
|
|
|
|
+ transferMsgOrigin = textValue;
|
|
}
|
|
}
|
|
|
|
|
|
void CommandSummary()
|
|
void CommandSummary()
|
|
@@ -396,10 +423,13 @@ public class StatusController : MonoBehaviour
|
|
{
|
|
{
|
|
// todo 重置狗的声音提交成功
|
|
// todo 重置狗的声音提交成功
|
|
var data = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
|
|
var data = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
|
|
- if (data != null && data["status"].ToString() == "success"){
|
|
|
|
|
|
+ if (data != null && data["status"].ToString() == "success")
|
|
|
|
+ {
|
|
// 跳转login场景,重新登录
|
|
// 跳转login场景,重新登录
|
|
MaskTransitions.TransitionManager.Instance.LoadLevel("Login");
|
|
MaskTransitions.TransitionManager.Instance.LoadLevel("Login");
|
|
- }else{
|
|
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
Debug.Log(data["message"]);
|
|
Debug.Log(data["message"]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -416,8 +446,9 @@ public class StatusController : MonoBehaviour
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- void CancelBtnClick()
|
|
|
|
|
|
+ void CancelTransferBtnClick()
|
|
{
|
|
{
|
|
|
|
+ CancelTransferDogRequest();
|
|
statusElement.style.display = DisplayStyle.Flex;
|
|
statusElement.style.display = DisplayStyle.Flex;
|
|
transferElement.style.display = DisplayStyle.None;
|
|
transferElement.style.display = DisplayStyle.None;
|
|
currentPage = StausUIPage.Status;
|
|
currentPage = StausUIPage.Status;
|
|
@@ -484,7 +515,7 @@ public class StatusController : MonoBehaviour
|
|
|
|
|
|
void TransferDogRequest()
|
|
void TransferDogRequest()
|
|
{
|
|
{
|
|
- string url = "/api/transfer_dog/receive/";
|
|
|
|
|
|
+ string url = "/api/transfer_dog/send/";
|
|
Dictionary<string, string> formData = new();
|
|
Dictionary<string, string> formData = new();
|
|
WWWForm form = new();
|
|
WWWForm form = new();
|
|
form.AddField("user_id", UserProperty.userId);
|
|
form.AddField("user_id", UserProperty.userId);
|
|
@@ -494,10 +525,70 @@ public class StatusController : MonoBehaviour
|
|
|
|
|
|
void TransferDogCallback(string json)
|
|
void TransferDogCallback(string json)
|
|
{
|
|
{
|
|
- // TODO 生成二维码,和120秒倒计时
|
|
|
|
- transferCountStartTime = DateTime.Now;
|
|
|
|
- currentPage = StausUIPage.Transfer;
|
|
|
|
|
|
+ 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;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|