Files
HX_MapEditor/Assets/Scripts/UI/UIMonsterItem.cs
tangbin 1cb6dc502f 添加
2025-06-22 15:21:25 +08:00

262 lines
8.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using HxGame.Data;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using static MapManager;
public class UIMonsterItem : MonoBehaviour
{
public delegate void ClickItemEvent(UIMonsterItem item);
public ClickItemEvent OnClick;
//public delegate void CopyItemEvent(MonstersConfig.MonsterConfig item);
public delegate void CopyItemEvent(UIMonsterItem item);
public CopyItemEvent OnCopyItem;
public Text txtID; //序号ID
public Text txtMonsterID; //怪物ID
public Text txtName; //怪物名称
public InputField txtPos; //中心点坐标
public Button btnDel; //删除
public Button btnCopy; //复制
public Button btnHide; //隐藏
public Button btnClickSelf; //点击一条
public List<string> paths;
//public int itemIdx;
public int pathIdx;
public int groupId;
public MonstersConfig.MonsterConfig monster;
// Start is called before the first frame update
void Awake()
{
groupId = -1;
AddInputNameClickEvent(txtPos);
//AddInputNameClickEvent(txtPath, true);
btnDel.onClick.AddListener(RemoveSelf);
//itemIdx = 0;
paths = new List<string>();
btnClickSelf.onClick.AddListener(OnClickSelf);
btnCopy.onClick.AddListener(OnCopySelf);
btnHide.onClick.AddListener(OnHideSelf);
txtPos.onEndEdit.AddListener(OnEndEditPos);
}
private void OnEndEditPos(string arg0)
{
if (!UtilityClass.IsPosValidFormat(arg0))
{
UIWindow.Instance.ShowMessage("请输入正确格式 x,y");
txtPos.text = $"{monster.pos.x},{monster.pos.y}";
return;
}
monster.pos = UtilityClass.GetVector2Pos(arg0);
MapManager.Instance.CreateSpecialPoint(monster.pos.x, monster.pos.y, MapManager.EditCellType.MonsterArea);
}
//public void SetItem(int idx, MonstersConfig.MonsterConfig m)
public void SetItem(MonstersConfig.MonsterConfig m)
{
//itemIdx = idx;
txtID.text = m.itemIdx.ToString();
txtMonsterID.text = m.id.ToString();
txtName.text = m.id.ToString();
txtPos.text = m.pos.ToString();
paths = m.paths;
monster = m;
if (m.groupId > 0)
return;
MapManager.Instance.SetMonsterPoint(m.itemIdx, monster.radius, monster.id, monster.num);
MapManager.Instance.CreateSpecialPoint(monster.pos.x, monster.pos.y, MapManager.EditCellType.MonsterArea);
MapManager.Instance.SetCurMonsterPathIdx(pathIdx);
}
public void RefreshItem()
{
txtMonsterID.text = monster.id.ToString();
txtName.text = monster.id.ToString();
paths = monster.paths;
txtPos.text = monster.pos.ToString();
//刷新场景中的东西
MapManager.Instance.SetMonsterPoint(monster.itemIdx, monster.radius, monster.id, monster.num);
MapManager.Instance.CreateSpecialPoint(monster.pos.x, monster.pos.y, EditCellType.MonsterArea);
}
public void SetGroup(int group)
{
//属于group的隐藏位置、索引,删除,复制
groupId = group;
txtID.gameObject.SetActive(false);
txtPos.gameObject.SetActive(false);
btnCopy.gameObject.SetActive(false);
btnDel.gameObject.SetActive(false);
btnHide.gameObject.SetActive(false);
}
private void AddInputNameClickEvent(InputField input, bool isPath = false) //可以在Awake中调用
{
var eventTrigger = input.gameObject.AddComponent<EventTrigger>();
UnityAction<BaseEventData> selectEvent = null;
if (isPath)
selectEvent = OnPathInputFieldClicked;
else
selectEvent = OnInputFieldClicked;
EventTrigger.Entry onClick = new EventTrigger.Entry()
{
eventID = EventTriggerType.PointerClick
};
onClick.callback.AddListener(selectEvent);
eventTrigger.triggers.Add(onClick);
}
private void OnInputFieldClicked(BaseEventData data)
{
if (string.IsNullOrEmpty(txtMonsterID.text))
return;
if (OnClick == null)
return;
OnClick(this);
UIWindow.Instance.uiMonstersPanel.curActiveInput = data.selectedObject.GetComponent<InputField>();
int radius = Convert.ToInt32(UIWindow.Instance.uiMonstersPanel.txtRadius.text);
int count = Convert.ToInt32(UIWindow.Instance.uiMonstersPanel.txtCount.text);
MapManager.Instance.SetMonsterPoint(monster.itemIdx, radius, Convert.ToInt32(txtMonsterID.text), count);
MapManager.Instance.SetEditCellType(MapManager.EditCellType.MonsterArea);
}
private void OnPathInputFieldClicked(BaseEventData data)
{
UIWindow.Instance.uiMonstersPanel.curActiveInput = data.selectedObject.GetComponent<InputField>();
UIWindow.Instance.uiMonstersPanel.curActiveList = paths;
MapManager.Instance.SetCurMonsterPathIdx(pathIdx);
MapManager.Instance.SetEditCellType(MapManager.EditCellType.MonsterPath);
}
private void RemoveSelf()
{
DestroyImmediate(transform.parent.gameObject);
MapManager.Instance.RemoveMonsterPoint(monster.itemIdx);
}
public bool CheckValid()
{
if (string.IsNullOrEmpty(txtID.text))
{
UIWindow.Instance.ShowMessage("请填写怪物ID");
return false;
}
if (string.IsNullOrEmpty(txtPos.text))
{
UIWindow.Instance.ShowMessage("请填写刷怪坐标");
return false;
}
//if (string.IsNullOrEmpty(txtRadius.text))
//{
// UIWindow.Instance.ShowMessage("请填写刷怪半径");
// return false;
//}
//if (string.IsNullOrEmpty(txtNum.text))
//{
// UIWindow.Instance.ShowMessage("请填写刷怪数量");
// return false;
//}
//if (string.IsNullOrEmpty(txtTime.text))
//{
// UIWindow.Instance.ShowMessage("请填写刷怪时间");
// return false;
//}
//if (dropType.value == (int)CreateMonsterMode.Death)
//{
// if(!APIs.IsUInt(txtTime.text))
// {
// UIWindow.Instance.ShowMessage("刷怪时间请输入正整数");
// return false;
// }
//}
//else if(dropType.value == (int)CreateMonsterMode.FixedTime)
//{
// string[] tmp = txtTime.text.Split(',');
// if (tmp.Length != 2)
// {
// UIWindow.Instance.ShowMessage("刷怪时间用,隔开天数与时间");
// return false;
// }
// if(!APIs.IsUInt(tmp[0]))
// {
// UIWindow.Instance.ShowMessage("刷怪时间-天数请输入正整数");
// return false;
// }
// string[] strTime = tmp[1].Split(':');
// if (strTime.Length != 2)
// {
// UIWindow.Instance.ShowMessage("刷怪时间-时间格式错误");
// return false;
// }
// for (int i = 0; i < strTime.Length; i++)
// {
// if (!APIs.IsUInt(strTime[i]))
// {
// UIWindow.Instance.ShowMessage("刷怪时间-时间格式错误");
// return false;
// }
// }
//}
return true;
}
private void OnClickSelf()
{
if (OnClick == null)
return;
OnClick(this);
}
private void OnCopySelf()
{
if (OnCopyItem == null)
return;
//OnCopyItem(monster);
OnCopyItem(this);
}
private void OnHideSelf()
{
MapManager.Instance.HideMonsterPoint(monster.itemIdx);
btnHide.transform.GetChild(0).GetComponent<Text>().text = "显示";
btnHide.onClick.RemoveAllListeners();
btnHide.onClick.AddListener(OnShowSelf);
}
private void OnShowSelf()
{
MapManager.Instance.ShowMonsterPoint(monster.itemIdx);
btnHide.transform.GetChild(0).GetComponent<Text>().text = "隐藏";
btnHide.onClick.RemoveAllListeners();
btnHide.onClick.AddListener(OnHideSelf);
}
}