109 lines
3.6 KiB
C#
109 lines
3.6 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using UnityEngine.EventSystems;
|
|
|
|
public class CharacterCardView : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
|
|
{
|
|
public int characterID;
|
|
public string currentBoundaryLevel;
|
|
public GameObject charactCard;
|
|
public Image characterImage;
|
|
public Image levelIcon;
|
|
public Image occupationIcon;
|
|
public bool isSelected=false;
|
|
|
|
// drag
|
|
private GameObject dragGhost;
|
|
private Canvas rootCanvas;
|
|
private CanvasGroup canvasGroup;
|
|
|
|
void Start()
|
|
{
|
|
charactCard = gameObject;
|
|
rootCanvas = GetComponentInParent<Canvas>();
|
|
canvasGroup = GetComponent<CanvasGroup>() ?? gameObject.AddComponent<CanvasGroup>();
|
|
}
|
|
/// <summary>
|
|
/// 未在队中的卡点击后进入被操纵状态。若在队伍中默认进入已经入队状态
|
|
/// </summary>
|
|
public void beSelected()
|
|
{
|
|
if (!isSelected)
|
|
{
|
|
isSelected = true;
|
|
TeamManager.Instance.currentSelectedCharacterCard = new CharacterView(characterID, currentBoundaryLevel);
|
|
characterImage.color = new Color(0.5f, 0.5f, 0.5f,1);
|
|
}
|
|
else beUnSelected();
|
|
}
|
|
|
|
public void beUnSelected()
|
|
{
|
|
//还需要走TeamManager去掉此角色的队列信息
|
|
isSelected = false;
|
|
TeamManager.Instance.removeTeamCharacter(characterID);
|
|
characterImage.color = new Color(1f, 1f, 1f,1);
|
|
}
|
|
|
|
// Drag handlers: create ghost based on characterImage.sprite
|
|
public void OnBeginDrag(PointerEventData eventData)
|
|
{
|
|
Debug.Log($"CharacterCardView OnBeginDrag: {gameObject.name}");
|
|
if (characterImage == null || characterImage.sprite == null) return;
|
|
if (rootCanvas == null) rootCanvas = GetComponentInParent<Canvas>();
|
|
if (rootCanvas == null) return;
|
|
|
|
// prevent the original from blocking raycasts so drop targets can receive events
|
|
if (canvasGroup == null) canvasGroup = GetComponent<CanvasGroup>() ?? gameObject.AddComponent<CanvasGroup>();
|
|
canvasGroup.blocksRaycasts = false;
|
|
|
|
// ensure pointerDrag references this object for drop handlers
|
|
eventData.pointerDrag = gameObject;
|
|
|
|
dragGhost = new GameObject("_RoleDragGhost");
|
|
dragGhost.transform.SetParent(rootCanvas.transform, false);
|
|
var img = dragGhost.AddComponent<Image>();
|
|
img.raycastTarget = false;
|
|
img.sprite = characterImage.sprite;
|
|
img.SetNativeSize();
|
|
var cg = dragGhost.AddComponent<CanvasGroup>();
|
|
cg.blocksRaycasts = false;
|
|
|
|
UpdateGhostPosition(eventData);
|
|
}
|
|
|
|
public void OnDrag(PointerEventData eventData)
|
|
{
|
|
if (dragGhost == null) return;
|
|
UpdateGhostPosition(eventData);
|
|
}
|
|
|
|
public void OnEndDrag(PointerEventData eventData)
|
|
{
|
|
Debug.Log($"CharacterCardView OnEndDrag: {gameObject.name}");
|
|
if (dragGhost != null)
|
|
{
|
|
Destroy(dragGhost);
|
|
dragGhost = null;
|
|
}
|
|
// restore raycast blocking on the original
|
|
if (canvasGroup != null)
|
|
canvasGroup.blocksRaycasts = true;
|
|
}
|
|
|
|
private void UpdateGhostPosition(PointerEventData eventData)
|
|
{
|
|
if (rootCanvas == null) return;
|
|
RectTransformUtility.ScreenPointToLocalPointInRectangle(
|
|
rootCanvas.transform as RectTransform,
|
|
eventData.position,
|
|
eventData.pressEventCamera,
|
|
out Vector2 localPoint);
|
|
if (dragGhost != null)
|
|
dragGhost.transform.localPosition = localPoint;
|
|
}
|
|
}
|