少年修仙传客户端代码仓库
client_Wu Xijin
2019-06-13 033958214c0b16d7e7b93cc821b018c295251867
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
//--------------------------------------------------------
//    [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);
        }
    }
 
}