少年修仙传客户端代码仓库
lcy
2024-12-16 a39c35fc6449430cd02bccb681c4a0a880e46cd9
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
//--------------------------------------------------------
//    [Author]:           第二世界
//    [  Date ]:           Thursday, April 18, 2019
//--------------------------------------------------------
 
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 
namespace vnxbqy.UI
{
 
    public class HolidayLoginWin : Window
    {
        Image DayImg;
        ScrollerController m_Controller;    // 登录天
        List<ItemCell> items = new List<ItemCell>();
        Button GetGift;
        Image GotYet;
        Text ShowDayInfo; //可领天数说明
 
        List<int> displayItems = new List<int>();
        HolidayLoginModel model { get { return ModelCenter.Instance.GetModel<HolidayLoginModel>(); } }
 
 
        #region Built-in
        protected override void BindController()
        {
            DayImg = GetWidgt<Image>("DayWord");
            m_Controller = GetWidgt<ScrollerController>("ScrollerControl");
            items.Clear();
 
            for (int i = 0; i < 6; i++)
            {
                var itemCell = GetWidgt<ItemCell>("ItemCell" + i);
                items.Add(itemCell);
            }
 
            GetGift = GetWidgt<Button>("ReceiveAwardBtn");
            GotYet = GetWidgt<Image>("AlreadyReceivedImage");
            ShowDayInfo = GetWidgt<Text>("TimeRemaining");
 
        }
 
        protected override void AddListeners()
        {
            
        }
 
        protected override void OnPreOpen()
        {
            m_Controller.OnRefreshCell += OnRefreshCell;
            OperationTimeHepler.Instance.operationTimeUpdateEvent += OperationTimeUpdateEvent;
            model.UpdateHolidayLogin += UpdateHolidayLogin;
            model.selectIndex = GetDefaultSelect();
            DisplayScroll();
            DisplayDayAward();
        }
 
 
        protected override void OnAfterOpen()
        {
        }
 
        protected override void OnPreClose()
        {
            m_Controller.OnRefreshCell -= OnRefreshCell;
            OperationTimeHepler.Instance.operationTimeUpdateEvent -= OperationTimeUpdateEvent;
            model.UpdateHolidayLogin -= UpdateHolidayLogin;
        }
 
        protected override void OnAfterClose()
        {
        }
        #endregion
 
 
        void DisplayScroll()
        {
            m_Controller.Refresh();
            OperationHolidayLogin holiday;
            OperationTimeHepler.Instance.TryGetOperation(Operation.HolidayLogin, out holiday);
 
            for (int i = 0; i < holiday.loginAwards.Count; i++)
            {
                m_Controller.AddCell(ScrollerDataType.Header, i);
            }
            m_Controller.Restart();
            m_Controller.m_Scorller.RefreshActiveCellViews();
            m_Controller.JumpIndex(model.selectIndex);
        }
 
        private void OnRefreshCell(ScrollerDataType type, CellView cell)
        {
            if (type != ScrollerDataType.Header) return;
 
            var _cell = cell as HolidayLoginDayCell;
            
            _cell.Display(_cell.index);
            // 注意 AddListener RefreshActiveCellViews 在 Display之前会卡死
            _cell.funcBtn.AddListener(() =>
            {
                model.selectIndex = cell.index;
                DisplayDayAward();
                m_Controller.m_Scorller.RefreshActiveCellViews();
            });
 
        }
 
        void UpdateHolidayLogin()
        {
            DisplayDayAward();
        }
 
        private void OperationTimeUpdateEvent(Operation type)
        {
            if (type == Operation.HolidayLogin)
            {
                DisplayScroll();
                DisplayDayAward();
            }
        }
 
        void DisplayDayAward()
        {
            OperationHolidayLogin holiday;
            OperationTimeHepler.Instance.TryGetOperation(Operation.HolidayLogin, out holiday);
            DayImg.SetSprite("cnum" + holiday.loginAwards[model.selectIndex].DayNum);
            DayImg.SetNativeSize();
 
            var awards = holiday.loginAwards[model.selectIndex].AwardItemList;
            for (int i = 0; i < items.Count; i++)
            {
                if (i < awards.Length)
                {
                    var award = awards[i];
                    items[i].SetActive(true);
                    var itemData = new ItemCellModel((int)award.ItemID, false, award.ItemCount);
                    items[i].Init(itemData);
                    items[i].button.SetListener(()=> {
                        ItemTipUtility.Show((int)award.ItemID);
                    });
                }
                else
                {
                    items[i].SetActive(false);
                }
            }
            var state = model.GetLoginAwardState(holiday.loginAwards[model.selectIndex].DayNum);
            GetGift.SetActive(state == 1);
            GetGift.SetListener(()=> {
                model.SendGetAward(holiday.loginAwards[model.selectIndex].DayNum);
            });
            GotYet.SetActive(state == 2);
            ShowDayInfo.SetActive(state == 0);
            if (state == 0)
            {
                int curDayIndex = GetTodayIndex(); 
                if (curDayIndex + 1 == model.selectIndex)
                    ShowDayInfo.text = Language.Get("Tomorrow_Draw");
                else
                    ShowDayInfo.text = Language.Get("Rest_Time", model.selectIndex - curDayIndex);
            }
        }
 
        //优先选择可领取的,然后是已领取的下一天
        int GetDefaultSelect()
        {
            OperationHolidayLogin holiday;
            OperationTimeHepler.Instance.TryGetOperation(Operation.HolidayLogin, out holiday);
 
            int getYet = -1;
            for (int i = 0; i < holiday.loginAwards.Count; i++)
            {
                var state = model.GetLoginAwardState(i+1);
                if (state == 1)
                    return i;
                if (state == 2)
                    getYet = i;
            }
            return Math.Min(getYet + 1, holiday.loginAwards.Count - 1);
            
        }
 
        //获取当前登录天
        int GetTodayIndex()
        {
            OperationHolidayLogin holiday;
            OperationTimeHepler.Instance.TryGetOperation(Operation.HolidayLogin, out holiday);
 
            int index = 0;
            for (int i = 0; i < holiday.loginAwards.Count; i++)
            {
                var state = model.GetLoginAwardState(i + 1);
                if (state == 1 || state == 2)
                    index = i;
            }
            return index;
        }
    }
}