using Thousandto.Code.Center; using Thousandto.Code.Logic; using Thousandto.Core.Base; using System; using System.Collections.Generic; using System.Text; using UnityEngine; namespace Thousandto.Code.Logic { /// /// 一句聊天内容包含多个ChatNode,这个类就是管理这些ChatNode的, /// /// public class OneTalkManager { public int CurrentChannel { get; set; } public GameObject Label { get; set; } public bool HasSetClickEvent { get; set; } private Dictionary>> _dataDic = new Dictionary>>(); public OneTalkManager() { } /// /// 添加ChatNode对应的Rect范围,用于点击UILabel获取到坐标,判断是否落在Rect范围 /// /// /// public void AddNode(int chatChannel, ChatNode node, Rect rect) { CurrentChannel = chatChannel; int channel = chatChannel; if (_dataDic.ContainsKey(channel)) { Dictionary> nodeToRectDic = _dataDic[channel]; if (!nodeToRectDic.ContainsKey(node)) { List rectList = new List(); rectList.Add(rect); nodeToRectDic.Add(node, rectList); } else { nodeToRectDic[node].Add(rect); } } else { List rectList = new List(); rectList.Add(rect); Dictionary> nodeToRectDic = new Dictionary>(); nodeToRectDic.Add(node, rectList); _dataDic.Add(channel, nodeToRectDic); } } //返回当前聊天内容中所有的ChatNode节点 public List GetChatNodeByChannel(int channel) { if (!_dataDic.ContainsKey(channel)) { return null; } Dictionary> nodeRectDic = _dataDic[channel]; List nodeList = new List(); var itor = nodeRectDic.Keys.GetEnumerator(); while (itor.MoveNext()) { nodeList.Add(itor.Current); } return nodeList; } /// /// 获取带img的节点, 包括表情、vip、语音图片 /// /// /// public List GetImageChatNode(int channel) { List imgNodes = new List(); List nodes = GetChatNodeByChannel(channel); for (int i = 0; nodes != null && i < nodes.Count; ++i) { if (nodes[i].Type == (int)TNodeType.Image || nodes[i].Type == (int)TNodeType.BigImage || nodes[i].Type == (int)TNodeType.ImageEx || nodes[i].Type == (int)TNodeType.Vip || nodes[i].Type == (int)TNodeType.Audio) { imgNodes.Add(nodes[i]); } } return imgNodes; } public void OnClick(GameObject go) { //20180327-yuqiang-屏蔽掉打开不能发言的频道 var keyItor = _dataDic[CurrentChannel].Keys.GetEnumerator(); keyItor.MoveNext(); if (keyItor.Current.ChannelID != (int)ChatChanelType.JOINTEAM && keyItor.Current.ChannelID != (int)ChatChanelType.Experience) { GameCenter.ChatSystem.Open(keyItor.Current.ChannelID); } else GameCenter.ChatSystem.Open(); return; { //获取屏幕坐标 Vector3 mScreenPos = Input.mousePosition; //定义射线 Ray mRay = GameCenter.ChatSystem.UICamera.ScreenPointToRay(mScreenPos); RaycastHit[] hits = Physics.RaycastAll(mRay); for (int i = 0; hits != null && i < hits.Length; ++i) { if (hits[i].collider.gameObject == Label) { Vector3 tPos = hits[i].point; Vector3 lPos = go.transform.InverseTransformPoint(tPos); if (!_dataDic.ContainsKey(CurrentChannel)) { return; } Dictionary> nodeToRectDir = _dataDic[CurrentChannel]; var itor = nodeToRectDir.GetEnumerator(); float x = 0; float y = 0; float width = 0; float height = 0; int line = 0; while (itor.MoveNext()) { ChatNode node = itor.Current.Key; //表情跳过 if (node.Type == (int)TNodeType.Image || node.Type == (int)TNodeType.BigImage || node.Type == (int)TNodeType.ImageEx || node.Type == (int)TNodeType.Vip) { continue; } List rectList = itor.Current.Value; //换行了 if (line != node.Line) { x = 0; line = node.Line; } y = -(node.Height * (node.Line)); width = node.Width; height = node.Height; for (int m = 0; m < rectList.Count; ++m) { if (x <= lPos.x && y >= lPos.y && x + width >= lPos.x && y - height <= lPos.y) { if (node.GO == null) { node.TriggerClick(go); } else node.TriggerClick(node.GO); return; } } x += node.Width; } } } } } public void Destory() { if (Label != null) { GameObject.Destroy(Label); } } } }