public class TradeGoodsItem : MonoBehaviour
{
CommonDelegate.VoidDelegate _CallBack = null;
int _GoodsNum = 0;
ItemDataStruct _Goods = null;
PaoshangData _PaoshangData = null;
void Awake()
{
_UISlider = ObjectCommon.GetChildComponent<UISlider>(gameObject, "Slider");</span>
if (_UISlider != null)
{
_UISlider.onDragProcess = OnDragFinished;
}
UIButton button = ObjectCommon.GetChildComponent<UIButton>(gameObject, "sub_btn");
UITools.AddOnclick(button, OnSubClick);
button = ObjectCommon.GetChildComponent<UIButton>(gameObject, "add_btn");
UITools.AddOnclick(button, OnAddClick);
}
public void SetCallBack(CommonDelegate.VoidDelegate func)
{
_CallBack = func;
}
public void Init(ItemDataStruct goods)
{
_Goods = goods;
if (_Goods == null)
{
UITools.setActive(gameObject, false);
return;
}
PaoshangData data = ShopDataProvider.GetInstance().GetPaoshangDataByID(goods._Id);
if (data != null)
{
_PaoshangData = data;
List<string> arg = ConfigDataTool.SplitStringDouhao(data.price);
if (arg != null && arg.Count >= 2)
{
UITools.SetLabText(_Jiage,string.Format("价格:{0}",arg[1]));
}
UITools.SetLabText(_FuZhong,string.Format("负重:{0}",data.weight));
}
else
{
UITools.ClearLabel(_Jiage);
UITools.ClearLabel(_FuZhong);
}
if (_UISlider != null)
{
_UISlider.value = 0;
}
_GoodsNum = 0;
ItemIconInfo info = ItemWrap.GetItemIconInfo(_Goods._Id);
UITools.SetLabText(_Name, info._Name);
UITools.SetLabText(_Num, string.Format("{0}/{1}", _GoodsNum, _Goods._Num));
NGUIAtlasManager.GetInstance().Attach(_Icon, info._Icon);
NGUIAtlasManager.GetInstance().Attach(_IconFrame, info._IconFrame);
}
/// <summary>
/// 是否能拖拽
/// </summary>
private bool isCanDrag()
{
if (_PaoshangData == null)
return false;
int max = ; // 该物品总共有多少个
int cur = ; // 当前选中了多少个
float canDragCount = (float)(max - cur)/(float)_PaoshangData.weight; // 特殊条件 总共可以选多少个 可加可不加
int canCount =(int)canDragCount; // 可拖拽几个
if (canCount <= 0)
return false;
return true;
}
private void OnDragFinished()
{
bool iscanDraging = isCanDrag();
float yuanlai = (float)_GoodsNum/(float)_Goods._Num;
float dangqian = _UISlider.value;
bool isQian = false;
if (dangqian < yuanlai)
isQian = true;
if (iscanDraging == false&& isQian == false) // 特殊条件 可加可不加
{
_UISlider.value = (float)_GoodsNum/(float)_Goods._Num;
UITemplate.ShowInfoTips(StringManager.GetInstance().GetValue("txt_shangpinfuzhongnocanrdrag"));
return;
}
_GoodsNum = (int)(_UISlider.value * _Goods._Num);
UpdateNumber();
}
private void OnSubClick() // 左btn --
{
if (_GoodsNum - 1 >= 0)
{
_GoodsNum--;
_UISlider.value = (float)_GoodsNum / (float)_Goods._Num;
UpdateNumber();
}
}
private void OnAddClick()// 右btn ++
{
bool iscanDraging = isCanDrag();
if (iscanDraging == false )
{
_UISlider.value = (float)_GoodsNum/(float)_Goods._Num;
UITemplate.ShowInfoTips(StringManager.GetInstance().GetValue("txt_shangpinfuzhongnocanrdrag"));
return;
}
if (_GoodsNum + 1 <= _Goods._Num)
{
_GoodsNum++;
_UISlider.value = (float)_GoodsNum / (float)_Goods._Num;
UpdateNumber();
}
}
public void SetValue(int num) // slider 拖拽 value在变
{
_GoodsNum = num;// 选择了这么多个
int havaCount = _Goods._Num;
float value = (float)_GoodsNum/(float)havaCount;
_UISlider.value = value;
UpdateNumber();
}
private void UpdateNumber() 更新选中的数量
{
if (_CallBack != null)
_CallBack();
UITools.SetLabText(_Num, string.Format("{0}/{1}", _GoodsNum, _Goods._Num));
}
public int GetGoodsNum()
{
return _GoodsNum;
}
public int GetGoodsId()
{
if (_Goods == null)
{
return 0;
}
return _Goods._Id;
}
public void OnSelectAll() // 一键全选
{
if (_Goods == null)
{
return;
}
_GoodsNum = _Goods._Num;
_UISlider.value = (float)_GoodsNum / (float)_Goods._Num;
UpdateNumber();
}
}
slider 控件 结合 左右btn
最新推荐文章于 2022-03-21 15:03:46 发布