//--------------------------------------------------------
|
// [Author]: 第二世界
|
// [ Date ]: Monday, April 09, 2018
|
//--------------------------------------------------------
|
|
using System;
|
using System.Collections;
|
using System.Collections.Generic;
|
using System.Linq;
|
|
using UnityEngine;
|
using UnityEngine.UI;
|
|
namespace Snxxz.UI
|
{
|
|
public class ChatExtentWin : Window
|
{
|
[SerializeField] RectTransform m_ContainerQuickChat;
|
[SerializeField] List<Button> m_QuickChatBtns;
|
[SerializeField] List<Text> m_QuickChats;
|
|
[SerializeField] RectTransform m_ContainerBag;
|
[SerializeField] ScrollerController m_BagControl;
|
[SerializeField] RectTransform m_ContainerFace;
|
[SerializeField] ScrollerController m_FaceControl;
|
[SerializeField] RectTransform m_ContainerChatBubble;
|
[SerializeField] ScrollerController m_ChatBubbleController;
|
|
[SerializeField] Button m_Face;
|
[SerializeField] Button m_QuickChat;
|
[SerializeField] Button m_Bag;
|
[SerializeField] Button m_ChatBubble;
|
[SerializeField] Text m_FaceBtnTxt;
|
[SerializeField] Text m_QuickChatBtnTxt;
|
[SerializeField] Text m_BagBtnTxt;
|
[SerializeField] Text m_ChatBubbleBtnTxt;
|
|
[SerializeField] ChatBubbleCell m_ChatBubbleCell;
|
|
ChatCenter m_ChatCenter;
|
ChatCenter chatCenter
|
{
|
get
|
{
|
return m_ChatCenter ?? (m_ChatCenter = ModelCenter.Instance.GetModel<ChatCenter>());
|
}
|
}
|
|
PackModel _playerPack;
|
PackModel playerPack
|
{
|
get { return _playerPack ?? (_playerPack = ModelCenter.Instance.GetModel<PackModel>()); }
|
}
|
|
List<string> m_Faces = null;
|
List<ItemModel> m_DisplayItems = new List<ItemModel>();
|
|
private int presentSelect = 0;
|
#region Built-in
|
protected override void BindController()
|
{
|
}
|
|
protected override void AddListeners()
|
{
|
for (int i = 0; i < m_QuickChatBtns.Count; i++)
|
{
|
int _index = i;
|
m_QuickChatBtns[i].onClick.AddListener(() =>
|
{
|
OnQuickChat(_index);
|
});
|
}
|
|
m_Face.onClick.AddListener(OnFaceBtn);
|
m_Bag.onClick.AddListener(OnBagBtn);
|
m_QuickChat.onClick.AddListener(OnQuickChat);
|
m_ChatBubble.onClick.AddListener(OnChatBubble);
|
|
m_FaceControl.OnRefreshCell += OnRefreshFaceCell;
|
m_BagControl.OnRefreshCell += OnRefreshBagCell;
|
m_ChatBubbleController.OnRefreshCell += OnRefreshBubbleCell;
|
}
|
|
private void OnRefreshBagCell(ScrollerDataType type, CellView cell)
|
{
|
ChatItemCell _itemCell = cell as ChatItemCell;
|
int _line = cell.index;
|
for (int i = 0; i < 3; i++)
|
{
|
int index = _line * 3 + i;
|
if (index < m_DisplayItems.Count)
|
{
|
_itemCell.items[i].gameObject.SetActive(true);
|
ItemModel _item = m_DisplayItems[index];
|
ItemConfig itemCfg = ItemConfig.Get((int)_item.itemId);
|
_itemCell.items[i].Init(_item);
|
_itemCell.itemEquips[i].gameObject.SetActive(_item.packType == PackType.Equip);
|
_itemCell.items[i].button.onClick.RemoveAllListeners();
|
_itemCell.items[i].button.onClick.AddListener(() =>
|
{
|
OnItemClick(_item);
|
});
|
}
|
else
|
{
|
_itemCell.items[i].gameObject.SetActive(false);
|
}
|
}
|
}
|
|
private void OnItemClick(ItemModel _item)
|
{
|
if (chatCenter.recentlyChat != null)
|
{
|
chatCenter.recentlyChat = null;
|
chatCenter.ChangeChatValue(string.Empty, false, false);
|
}
|
ItemConfig _cfg = ItemConfig.Get((int)_item.itemId);
|
string _showtext = StringUtility.Contact("[", _cfg.ItemName, "]");
|
ChatCtrl.Inst.itemPlaceList.Add(_item);
|
chatCenter.ChangeChatValue(_showtext, true, true);
|
}
|
|
private void OnRefreshFaceCell(ScrollerDataType type, CellView cell)
|
{
|
ChatFaceCell _chatFaceCell = cell as ChatFaceCell;
|
int _line = cell.index;
|
for (int i = 0; i < 3; i++)
|
{
|
int index = _line * 3 + i;
|
if (index < m_Faces.Count)
|
{
|
_chatFaceCell.faceBtns[i].gameObject.SetActive(true);
|
_chatFaceCell.faceFrames[i].ResetFrame(m_Faces[index]);
|
_chatFaceCell.faceBtns[i].RemoveAllListeners();
|
_chatFaceCell.faceBtns[i].onClick.AddListener(() =>
|
{
|
OnFaceClick(index);
|
});
|
_chatFaceCell.faceFrames[i].enabled = true;
|
}
|
else
|
{
|
_chatFaceCell.faceBtns[i].gameObject.SetActive(false);
|
}
|
}
|
}
|
|
private void OnFaceClick(int index)
|
{
|
if (chatCenter.chatInputLength + 5 > chatCenter.chatCharacterLimit)
|
{
|
return;
|
}
|
if (index < m_Faces.Count)
|
{
|
string facename = m_Faces[index];
|
if (facename.Length == 1)
|
{
|
facename = "00" + facename;
|
}
|
else if (facename.Length == 2)
|
{
|
facename = "0" + facename;
|
}
|
chatCenter.ChangeChatValue(string.Format("#~{0}", facename), true, true);
|
}
|
}
|
|
private void OnFaceBtn()
|
{
|
presentSelect = 0;
|
UpdateButtonState();
|
|
if (m_Faces == null)
|
{
|
m_Faces = UIFrameMgr.Inst.GetAllFace().Keys.ToList();
|
int _line = Mathf.CeilToInt((float)m_Faces.Count / 3);
|
m_FaceControl.Refresh();
|
for (int i = 0; i < _line; i++)
|
{
|
m_FaceControl.AddCell(ScrollerDataType.Normal, i);
|
}
|
m_FaceControl.Restart();
|
}
|
if (m_FaceControl.GetNumberOfCells(m_FaceControl.m_Scorller) > 0)
|
{
|
m_FaceControl.JumpIndex(0);
|
}
|
}
|
|
private void OnBagBtn()
|
{
|
presentSelect = 2;
|
UpdateButtonState();
|
|
if (!m_ContainerBag.gameObject.activeInHierarchy)
|
{
|
return;
|
}
|
OnRefreshBagItem();
|
}
|
|
private void OnQuickChat()
|
{
|
presentSelect = 1;
|
chatCenter.recentlyChat = null;
|
UpdateButtonState();
|
RecentlyChatChangeEvent();
|
}
|
|
private void OnChatBubble()
|
{
|
presentSelect = 3;
|
UpdateButtonState();
|
|
RefreshChatBubble();
|
}
|
|
private void OnQuickChat(int _index)
|
{
|
if (_index < chatCenter.recentlyChats.Count)
|
{
|
chatCenter.recentlyChat = chatCenter.recentlyChats[_index];
|
chatCenter.recentlyChat.Reset();
|
chatCenter.ChangeChatValue(chatCenter.recentlyChats[_index].display, false, true);
|
}
|
}
|
|
protected override void OnPreOpen()
|
{
|
presentSelect = 0;
|
playerPack.refreshItemCountEvent += RefreshItemCnt;
|
chatCenter.RecentlyChatChangeEvent += RecentlyChatChangeEvent;
|
OnFaceBtn();
|
}
|
|
protected override void OnAfterOpen()
|
{
|
}
|
|
protected override void OnPreClose()
|
{
|
playerPack.refreshItemCountEvent -= RefreshItemCnt;
|
chatCenter.RecentlyChatChangeEvent -= RecentlyChatChangeEvent;
|
}
|
|
protected override void OnAfterClose()
|
{
|
}
|
#endregion
|
private void RecentlyChatChangeEvent()
|
{
|
for (int i = 0; i < m_QuickChatBtns.Count; i++)
|
{
|
m_QuickChatBtns[i].gameObject.SetActive(i < chatCenter.recentlyChats.Count);
|
if (i < chatCenter.recentlyChats.Count)
|
{
|
var _value = chatCenter.recentlyChats[i].display;
|
m_QuickChats[i].text = _value;
|
if (GetCutOffValue(m_QuickChats[i], _value, ImgAnalysis.FaceRegex.IsMatch(_value) ? 2 : 3, out _value))
|
{
|
m_QuickChats[i].text = StringUtility.Contact(_value, "...");
|
}
|
}
|
}
|
}
|
|
private bool GetCutOffValue(Text _text, string _value, int _line, out string _display)
|
{
|
_display = string.Empty;
|
if (_text.preferredWidth >= _text.rectTransform.rect.width * _line)
|
{
|
CharacterInfo _info;
|
float _length = 0;
|
var _index = 0;
|
var _font = _text.font;
|
_font.RequestCharactersInTexture(_value, _text.fontSize, _text.fontStyle);
|
for (int i = 0; i < _value.Length; i++)
|
{
|
_font.GetCharacterInfo(_value[i], out _info, _text.fontSize, _text.fontStyle);
|
_length += _info.advance;
|
if (_length >= _text.rectTransform.rect.width * _line)
|
{
|
_index = i;
|
break;
|
}
|
}
|
_display = _value.Substring(0, Mathf.Max(0, _index - 5));
|
return true;
|
}
|
return false;
|
}
|
|
private void RefreshItemCnt(PackType type, int index, int id)
|
{
|
OnRefreshBagItem();
|
}
|
|
private void OnRefreshBagItem()
|
{
|
m_BagControl.Refresh();
|
m_DisplayItems.Clear();
|
var _packModel = ModelCenter.Instance.GetModel<PackModel>();
|
SinglePack packTypeModel = _packModel.GetSinglePack(PackType.Equip);
|
if (packTypeModel != null)
|
{
|
Dictionary<int, ItemModel> equipBodyDict = packTypeModel.GetAllItems();
|
if (equipBodyDict != null && equipBodyDict.Count > 0)
|
{
|
foreach (var _equip in equipBodyDict.Values)
|
{
|
if (_equip.config.EquipPlace <= (int)RoleEquipType.Guard)
|
{
|
m_DisplayItems.Add(_equip);
|
}
|
}
|
}
|
}
|
packTypeModel = _packModel.GetSinglePack(PackType.Item);
|
if (packTypeModel != null)
|
{
|
Dictionary<int, ItemModel> dic = packTypeModel.GetAllItems();
|
if (dic != null && dic.Count > 0)
|
{
|
m_DisplayItems.AddRange(dic.Values.ToList());
|
}
|
}
|
if (m_DisplayItems.Count > 0)
|
{
|
int _line = Mathf.CeilToInt((float)m_DisplayItems.Count / 3);
|
for (int i = 0; i < _line; i++)
|
{
|
m_BagControl.AddCell(ScrollerDataType.Normal, i);
|
}
|
}
|
m_BagControl.Restart();
|
}
|
|
private void RefreshChatBubble()
|
{
|
if (m_ChatBubbleController.GetNumberOfCells(m_ChatBubbleController.m_Scorller) == 0)
|
{
|
m_ChatBubbleController.Refresh();
|
var configs = ChatBubbleBoxConfig.GetValues();
|
var lineCount = m_ChatBubbleCell.lineCount == 0 ? 7 : m_ChatBubbleCell.lineCount;
|
var line = Mathf.CeilToInt((float)configs.Count / lineCount);
|
for (int i = 0; i < line; i++)
|
{
|
m_ChatBubbleController.AddCell(ScrollerDataType.Header, i);
|
}
|
m_ChatBubbleController.Restart();
|
}
|
else
|
{
|
m_ChatBubbleController.JumpIndex(0);
|
m_ChatBubbleController.m_Scorller.RefreshActiveCellViews();
|
}
|
}
|
|
private void OnRefreshBubbleCell(ScrollerDataType type, CellView cell)
|
{
|
var chatBubbleCell = cell as ChatBubbleCell;
|
chatBubbleCell.Display(cell.index);
|
}
|
|
void UpdateButtonState()
|
{
|
m_ContainerFace.gameObject.SetActive(presentSelect == 0);
|
m_ContainerBag.gameObject.SetActive(presentSelect == 2);
|
m_ContainerQuickChat.gameObject.SetActive(presentSelect == 1);
|
m_ContainerChatBubble.gameObject.SetActive(presentSelect == 3);
|
|
m_Bag.image.SetSprite(presentSelect == 2 ? "Chat_SelectBtn" : "Chat_UnSelectBtn");
|
m_Face.image.SetSprite(presentSelect == 0 ? "Chat_SelectBtn" : "Chat_UnSelectBtn");
|
m_QuickChat.image.SetSprite(presentSelect == 1 ? "Chat_SelectBtn" : "Chat_UnSelectBtn");
|
m_ChatBubble.image.SetSprite(presentSelect == 3 ? "Chat_SelectBtn" : "Chat_UnSelectBtn");
|
|
m_QuickChatBtnTxt.color = presentSelect == 1 ? UIHelper.s_LightYellow : UIHelper.GetUIColor(TextColType.NavyBrown);
|
m_BagBtnTxt.color = presentSelect == 2 ? UIHelper.s_LightYellow : UIHelper.GetUIColor(TextColType.NavyBrown);
|
m_FaceBtnTxt.color = presentSelect == 0 ? UIHelper.s_LightYellow : UIHelper.GetUIColor(TextColType.NavyBrown);
|
m_ChatBubbleBtnTxt.color = presentSelect == 3 ? UIHelper.s_LightYellow : UIHelper.GetUIColor(TextColType.NavyBrown);
|
}
|
}
|
|
}
|
|
|
|
|