using System; using System.Collections; using System.Collections.Generic; using UnityEngine; namespace Snxxz.UI { public class ChatBubbleModel : Model, IBeforePlayerDataInitialize, IPlayerLoginOk { public Dictionary chatBubbles = new Dictionary(); public event Action chatBubbleStateRefresh; VipModel vipModel { get { return ModelCenter.Instance.GetModel(); } } bool serverInited = false; public override void Init() { ParseConfig(); PlayerDatas.Instance.playerDataRefreshEvent += PlayerDataRefreshInfoEvent; } public override void UnInit() { PlayerDatas.Instance.playerDataRefreshEvent -= PlayerDataRefreshInfoEvent; } public void OnBeforePlayerDataInitialize() { bubbleState = 0; serverInited = false; } public void OnPlayerLoginOk() { serverInited = true; } private void PlayerDataRefreshInfoEvent(PlayerDataType refreshType) { if (refreshType == PlayerDataType.ExAttr10 && serverInited) { SysNotifyMgr.Instance.ShowTip("ChangeBubbleSuccess"); } } void ParseConfig() { var configs = ChatBubbleBoxConfig.GetValues(); for (int i = 0; i < configs.Count; i++) { var config = configs[i]; if (chatBubbles.ContainsKey(config.ID)) { continue; } var bubble = new ChatBubble(); bubble.id = config.ID; bubble.leftPadding = new RectOffset() { left = config.leftOffset.Length > 0 ? config.leftOffset[0] : 0, right = config.leftOffset.Length > 1 ? config.leftOffset[1] : 0, top = config.leftOffset.Length > 2 ? config.leftOffset[2] : 0, bottom = config.leftOffset.Length > 3 ? config.leftOffset[3] : 0, }; bubble.rifhtPadding = new RectOffset() { left = config.rightOffset.Length > 0 ? config.rightOffset[0] : 0, right = config.rightOffset.Length > 1 ? config.rightOffset[1] : 0, top = config.rightOffset.Length > 2 ? config.rightOffset[2] : 0, bottom = config.rightOffset.Length > 3 ? config.rightOffset[3] : 0, }; bubble.color = new Color32() { r = (byte)(config.color.Length > 0 ? config.color[0] : 0), g = (byte)(config.color.Length > 1 ? config.color[1] : 0), b = (byte)(config.color.Length > 2 ? config.color[2] : 0), a = (byte)(config.color.Length > 3 ? config.color[3] : 255), }; chatBubbles.Add(config.ID, bubble); } } public bool TryGetBubble(int id, out ChatBubble bubble) { return chatBubbles.TryGetValue(id, out bubble); } public bool IsBubbleGot(int id) { ChatBubble bubble; if (TryGetBubble(id, out bubble)) { var config = ChatBubbleBoxConfig.Get(id); bool got = false; try { got = MathUtility.GetBitValue(bubbleState, (ushort)id); } catch (ArgumentOutOfRangeException) { DebugEx.LogError("目前不支持id超过31的聊天框:" + id); } if (!got && config.NeedLV != 0) { return PlayerDatas.Instance.baseData.LV >= config.NeedLV; } return got; } return false; } #region 服务端数据 public uint bubbleState { get; private set; } public void UpdateBubbleState(HA717_tagMCChatBubbleBoxState package) { List list = null; if (serverInited) { list = new List(); foreach (var id in chatBubbles.Keys) { if (!IsBubbleGot(id)) { list.Add(id); } } } bubbleState = package.BoxState; if (serverInited) { if (list != null) { for (int i = 0; i < list.Count; i++) { if (IsBubbleGot(list[i])) { SendUseBubble(list[i]); break; } } } } if (chatBubbleStateRefresh != null) { chatBubbleStateRefresh(); } } public void SendUseBubble(int id) { if (!IsBubbleGot(id)) { return; } if (id == PlayerDatas.Instance.baseData.bubbleId) { return; } CA230_tagCMSetChatBubbleBox pak = new CA230_tagCMSetChatBubbleBox(); pak.BubbleBoxType = (byte)id; GameNetSystem.Instance.SendInfo(pak); } #endregion public struct ChatBubble { public int id; public RectOffset leftPadding; public RectOffset rifhtPadding; public Color32 color; public string GetBubbleIcon(bool left, ref bool isFlip) { var config = ChatBubbleBoxConfig.Get(id); isFlip = false; if (left) { if (string.IsNullOrEmpty(config.leftBubbleIcon)) { isFlip = true; return config.rightBubbleIcon; } return config.leftBubbleIcon; } else { if (string.IsNullOrEmpty(config.rightBubbleIcon)) { isFlip = true; return config.leftBubbleIcon; } return config.rightBubbleIcon; } } } } }