FM/Assets/Scripts/FUJIFILM/UI/Item/ShopCartItem.cs

129 lines
3.9 KiB
C#
Raw Normal View History

2025-08-20 11:14:21 +08:00
using System;
2025-08-27 01:05:19 +08:00
using HK.FUJIFILM;
2025-08-20 11:14:21 +08:00
using Runtime;
using UnityEngine.UI;
using TMPro;
using UnityEngine;
using ZXL.Helper;
namespace HK
{
public class ShopCartItem : UIItemBase
{
[SerializeField] private Button btnDelete;
[SerializeField] private Button btnEditor;
[SerializeField] private RawImage rawPic;
[SerializeField] private TMP_Text txtProductName;
[SerializeField] private Button btnAdd;
[SerializeField] private TMP_Text txtCount;
[SerializeField] private Button btnRemove;
2025-08-22 00:33:25 +08:00
[SerializeField] private TMP_Text txtMonetary;
2025-08-20 11:14:21 +08:00
[SerializeField] private TMP_Text txtPrice;
ShoppingCartData shoppingCartData;
public Action OnUpdateCallback;
2025-08-27 01:05:19 +08:00
public Action OnDeleteCallback;
2025-08-20 11:14:21 +08:00
public override void OnInit()
{
base.OnInit();
#region AutoGen_Init
btnDelete = GetValue<Button>("btnDelete");
btnEditor = GetValue<Button>("btnEditor");
rawPic = GetValue<RawImage>("rawPic");
txtProductName = GetValue<TMP_Text>("txtProductName");
btnAdd = GetValue<Button>("btnAdd");
txtCount = GetValue<TMP_Text>("txtCount");
btnRemove = GetValue<Button>("btnRemove");
2025-08-22 00:33:25 +08:00
txtMonetary = GetValue<TMP_Text>("txtMonetary");
2025-08-20 11:14:21 +08:00
txtPrice = GetValue<TMP_Text>("txtPrice");
btnDelete.onClick.AddListener(OnClickbtnDelete);
btnEditor.onClick.AddListener(OnClickbtnEditor);
btnAdd.onClick.AddListener(OnClickbtnAdd);
btnRemove.onClick.AddListener(OnClickbtnReduce);
#endregion
}
#region AutoGen_Method
private void OnClickbtnEditor()
{
}
private void OnClickbtnDelete()
{
ShoppingCartManager.Instance.RemoveFromCart(shoppingCartData);
2025-08-27 01:05:19 +08:00
OnDeleteCallback?.Invoke();
2025-08-20 11:14:21 +08:00
GameObject.Destroy(gameObject);
}
private void OnClickbtnReduce()
{
2025-08-22 00:33:25 +08:00
if (shoppingCartData.count <= 1)
2025-08-20 11:14:21 +08:00
{
return;
}
2025-08-22 00:33:25 +08:00
2025-08-20 11:14:21 +08:00
shoppingCartData.count--;
ShoppingCartManager.Instance.RefreshCart();
txtCount.text = shoppingCartData.count.ToString();
OnUpdateCallback?.Invoke();
}
private void OnClickbtnAdd()
{
if (shoppingCartData.count >= 10)
{
return;
}
2025-08-22 00:33:25 +08:00
2025-08-20 11:14:21 +08:00
shoppingCartData.count++;
ShoppingCartManager.Instance.RefreshCart();
txtCount.text = shoppingCartData.count.ToString();
OnUpdateCallback?.Invoke();
}
#endregion
public void SetData(ShoppingCartData data)
{
shoppingCartData = data;
2025-08-27 01:05:19 +08:00
var texture2D = TextureHelper.ConvertByteArrayToTexture2D(data.previewImage);
rawPic.texture = texture2D;
// rawPic.rectTransform.sizeDelta = new Vector2(texture2D.width, texture2D.height);
RawImageScaler.SetTextureWithAspectRatio(rawPic, texture2D);
2025-08-20 11:14:21 +08:00
txtCount.text = data.count.ToString();
2025-08-22 00:33:25 +08:00
txtMonetary.text = ShoppingCartManager.Instance.ShoppingCart.monetary;
2025-08-20 11:14:21 +08:00
txtPrice.text = data.bookAmount.ToString("F");
txtProductName.text = data.bookName;
}
public override void OnDispose()
{
base.OnDispose();
#region AutoGen_Dispose
btnDelete.onClick.RemoveListener(OnClickbtnDelete);
btnEditor.onClick.RemoveListener(OnClickbtnEditor);
btnAdd.onClick.RemoveListener(OnClickbtnAdd);
btnRemove.onClick.RemoveListener(OnClickbtnReduce);
btnDelete = null;
btnEditor = null;
rawPic = null;
txtProductName = null;
btnAdd = null;
txtCount = null;
btnRemove = null;
2025-08-22 00:33:25 +08:00
txtMonetary = null;
2025-08-20 11:14:21 +08:00
txtPrice = null;
#endregion
}
}
}