Main/Assets/Code/Logic/Chat/OneTalkManager.cs
2025-01-25 04:38:09 +08:00

200 lines
7.0 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 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
{
/// <summary>
/// 一句聊天内容包含多个ChatNode这个类就是管理这些ChatNode的
///
/// </summary>
public class OneTalkManager
{
public int CurrentChannel { get; set; }
public GameObject Label { get; set; }
public bool HasSetClickEvent { get; set; }
private Dictionary<int, Dictionary<ChatNode, List<Rect>>> _dataDic = new Dictionary<int, Dictionary<ChatNode, List<Rect>>>();
public OneTalkManager()
{
}
/// <summary>
/// 添加ChatNode对应的Rect范围用于点击UILabel获取到坐标判断是否落在Rect范围
/// </summary>
/// <param name="node"></param>
/// <param name="rect"></param>
public void AddNode(int chatChannel, ChatNode node, Rect rect)
{
CurrentChannel = chatChannel;
int channel = chatChannel;
if (_dataDic.ContainsKey(channel))
{
Dictionary<ChatNode, List<Rect>> nodeToRectDic = _dataDic[channel];
if (!nodeToRectDic.ContainsKey(node))
{
List<Rect> rectList = new List<Rect>();
rectList.Add(rect);
nodeToRectDic.Add(node, rectList);
}
else
{
nodeToRectDic[node].Add(rect);
}
}
else
{
List<Rect> rectList = new List<Rect>();
rectList.Add(rect);
Dictionary<ChatNode, List<Rect>> nodeToRectDic = new Dictionary<ChatNode, List<Rect>>();
nodeToRectDic.Add(node, rectList);
_dataDic.Add(channel, nodeToRectDic);
}
}
//返回当前聊天内容中所有的ChatNode节点
public List<ChatNode> GetChatNodeByChannel(int channel)
{
if (!_dataDic.ContainsKey(channel))
{
return null;
}
Dictionary<ChatNode, List<Rect>> nodeRectDic = _dataDic[channel];
List<ChatNode> nodeList = new List<ChatNode>();
var itor = nodeRectDic.Keys.GetEnumerator();
while (itor.MoveNext())
{
nodeList.Add(itor.Current);
}
return nodeList;
}
/// <summary>
/// 获取带img的节点, 包括表情、vip、语音图片
/// </summary>
/// <param name="channel"></param>
/// <returns></returns>
public List<ChatNode> GetImageChatNode(int channel)
{
List<ChatNode> imgNodes = new List<ChatNode>();
List<ChatNode> 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<ChatNode, List<Rect>> 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<Rect> 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);
}
}
}
}