230 lines
7.6 KiB
C#
230 lines
7.6 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using QFramework;
|
|
using System.Collections.Generic;
|
|
using UnityEngine.EventSystems;
|
|
using System;
|
|
using TMPro;
|
|
using System.Xml;
|
|
|
|
namespace QFramework.Example
|
|
{
|
|
public class UIImageSelectMapData : UIPanelData
|
|
{
|
|
public class OptionItem
|
|
{
|
|
public string pic;
|
|
public string name;
|
|
}
|
|
public class Row
|
|
{
|
|
public string name;
|
|
public List<OptionItem> items = new List<OptionItem>();
|
|
}
|
|
public string scoreName;
|
|
public float totalScore;
|
|
public float rightScore;
|
|
public float wrongScore;
|
|
public string rightLabel;
|
|
public string wrongLabel;
|
|
public string finishedEvent;
|
|
public bool random = false;
|
|
public List<Row> rows = new List<Row>();
|
|
|
|
}
|
|
public partial class UIImageSelectMap : UIPanel
|
|
{
|
|
/// <summary>
|
|
/// ÓÒ²àÊý¾Ý½á¹¹
|
|
/// </summary>
|
|
class RightDataItem
|
|
{
|
|
public GameObject rightObj;
|
|
public bool isRight = false;
|
|
}
|
|
ResLoader loader;
|
|
GameObject moveObj;
|
|
GameObject enterRight;
|
|
GameObject leftDragObj;
|
|
Dictionary<GameObject, UIImageSelectMapData.OptionItem> itemDatas = new Dictionary<GameObject, UIImageSelectMapData.OptionItem>();
|
|
Dictionary<GameObject, RightDataItem> leftAndRightMap = new Dictionary<GameObject, RightDataItem>();
|
|
protected override void OnInit(IUIData uiData = null)
|
|
{
|
|
mData = uiData as UIImageSelectMapData ?? new UIImageSelectMapData();
|
|
TypeEventSystem.Global.Register<OnModuleQuit>(OnModuleQuitHandler);
|
|
// please add init code here
|
|
loader = ResLoader.Allocate();
|
|
}
|
|
|
|
protected override void OnOpen(IUIData uiData = null)
|
|
{
|
|
mData = uiData as UIImageSelectMapData ?? new UIImageSelectMapData();
|
|
LeftContent.RemoveAllChildren();
|
|
RightContent.RemoveAllChildren();
|
|
itemDatas.Clear();
|
|
leftAndRightMap.Clear();
|
|
|
|
foreach (var row in mData.rows)
|
|
{
|
|
GameObject rowObj = GameObject.Instantiate(RightRowItem.gameObject, RightContent);
|
|
rowObj.transform.Find("Type").GetComponent<TextMeshProUGUI>().text = row.name.Replace("\\n", "\n");
|
|
Transform itemContent = rowObj.transform.Find("Items");
|
|
foreach (var item in row.items)
|
|
{
|
|
var rightObj = GameObject.Instantiate(RightItem, itemContent);
|
|
var nameText = rightObj.transform.Find("Name").GetComponent<TextMeshProUGUI>();
|
|
nameText.text = item.name;
|
|
nameText.color = Color.white;
|
|
rightObj.name = item.pic;
|
|
rightObj.transform.Find("PicBg/Pic").GetComponent<Image>().color = new Color(1, 1, 1, 0);
|
|
rightObj.OnPointerEnterEvent(RightOnEnter);
|
|
rightObj.OnPointerExitEvent(RightOnExit);
|
|
rightObj.OnPointerClickEvent(RightOnClick);
|
|
|
|
var leftObj = GameObject.Instantiate(LeftItem, LeftContent);
|
|
leftObj.name = item.pic;
|
|
var path = Global.deviceIconsPath + item.pic;
|
|
loader.Add2Load(path.ToNetImageResName(), (success, res) =>
|
|
{
|
|
if (success)
|
|
{
|
|
leftObj.GetComponent<Image>().sprite = Utility.GetSprite(res.Asset as Texture2D);
|
|
}
|
|
});
|
|
|
|
leftObj.OnBeginDragEvent(LeftOnBeginDrag);
|
|
leftObj.OnDragEvent(LeftOnDrag);
|
|
leftObj.OnEndDragEvent(LeftOnEndDrag);
|
|
itemDatas.Add(leftObj.gameObject, item);
|
|
}
|
|
}
|
|
|
|
if (mData.random)
|
|
{
|
|
Utility.ShuffleChildObjects(LeftContent);
|
|
}
|
|
|
|
loader.LoadAsync();
|
|
|
|
|
|
|
|
}
|
|
|
|
private void OnModuleQuitHandler(OnModuleQuit quit)
|
|
{
|
|
Hide();
|
|
}
|
|
|
|
private void RightOnClick(PointerEventData data)
|
|
{
|
|
Transform trans = data.pointerClick.transform;
|
|
if (leftAndRightMap.ContainsKey(trans.gameObject))
|
|
{
|
|
leftAndRightMap[trans.gameObject].rightObj.SetActive(true);
|
|
trans.GetComponent<Image>().color = Color.white;
|
|
trans.Find("PicBg/Pic").GetComponent<Image>().color = new Color(1, 1, 1, 0);
|
|
|
|
enterRight.transform.Find("Name").GetComponent<TextMeshProUGUI>().color = Color.white;
|
|
leftAndRightMap.Remove(trans.gameObject);
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private void RightOnExit(PointerEventData data)
|
|
{
|
|
enterRight = null;
|
|
}
|
|
|
|
private void RightOnEnter(PointerEventData data)
|
|
{
|
|
enterRight = data.pointerEnter.gameObject;
|
|
}
|
|
|
|
private void LeftOnBeginDrag(PointerEventData data)
|
|
{
|
|
leftDragObj = data.pointerDrag;
|
|
moveObj = GameObject.Instantiate(leftDragObj, gameObject.transform);
|
|
moveObj.name = moveObj.name.Replace("(Clone)", "");
|
|
moveObj.GetComponent<RectTransform>().sizeDelta = LeftContent.GetComponent<GridLayoutGroup>().cellSize;
|
|
moveObj.GetComponent<Image>().raycastTarget = false;
|
|
|
|
}
|
|
|
|
private void LeftOnEndDrag(PointerEventData data)
|
|
{
|
|
if (enterRight && leftAndRightMap.ContainsKey(enterRight) == false)
|
|
{
|
|
TextMeshProUGUI nameText = enterRight.transform.Find("Name").GetComponent<TextMeshProUGUI>();
|
|
bool isRight = false;
|
|
if (enterRight.name == itemDatas[leftDragObj].pic)
|
|
{
|
|
nameText.color = Color.green;
|
|
isRight = true;
|
|
|
|
}
|
|
else
|
|
{
|
|
nameText.color = Color.red;
|
|
|
|
}
|
|
Image img = enterRight.transform.Find("PicBg/Pic").GetComponent<Image>();
|
|
img.sprite = moveObj.GetComponent<Image>().sprite;
|
|
img.color = Color.white;
|
|
//nameText.text = itemDatas[leftDragObj].name;
|
|
leftAndRightMap.Add(enterRight, new RightDataItem() { rightObj = leftDragObj, isRight = isRight });
|
|
leftDragObj.SetActive(false);
|
|
|
|
if (isRight && Check())
|
|
{
|
|
if (string.IsNullOrEmpty(mData.finishedEvent) == false)
|
|
{
|
|
StringEventSystem.Global.Send(mData.finishedEvent);
|
|
}
|
|
}
|
|
|
|
}
|
|
GameObject.Destroy(moveObj);
|
|
moveObj = null;
|
|
leftDragObj = null;
|
|
}
|
|
|
|
public bool Check()
|
|
{
|
|
foreach (var item in leftAndRightMap)
|
|
{
|
|
if (item.Value.isRight == false)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
return leftAndRightMap.Count == itemDatas.Count;
|
|
}
|
|
|
|
private void LeftOnDrag(PointerEventData data)
|
|
{
|
|
//data.pointerDrag.transform.position = Input.mousePosition;
|
|
if (moveObj)
|
|
{
|
|
moveObj.transform.position = Input.mousePosition;
|
|
}
|
|
}
|
|
|
|
|
|
protected override void OnShow()
|
|
{
|
|
}
|
|
|
|
protected override void OnHide()
|
|
{
|
|
}
|
|
|
|
protected override void OnClose()
|
|
{
|
|
TypeEventSystem.Global.UnRegister<OnModuleQuit>(OnModuleQuitHandler);
|
|
}
|
|
}
|
|
}
|