少年修仙传客户端代码仓库
client_Hale
2019-04-11 9f89e3be35da42eb9ccb44e6589d62f320aa444c
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
//--------------------------------------------------------
//    [Author]:           第二世界
//    [  Date ]:           Tuesday, October 31, 2017
//--------------------------------------------------------
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
 
namespace Snxxz.UI
{
 
    public class FunctionButtonGroup : MonoBehaviour
    {
        public int unLockedCount {
            get {
                var count = 0;
                foreach (var button in toggleButtons.Values)
                {
                    if (button.state != TitleBtnState.Locked)
                    {
                        count++;
                    }
                }
 
                return count;
            }
        }
 
        int currentOrder = 0;
        List<int> orders = new List<int>();
        Dictionary<int, FunctionButton> toggleButtons = new Dictionary<int, FunctionButton>();
 
        public void Register(FunctionButton _toggleButton)
        {
            toggleButtons[_toggleButton.order] = _toggleButton;
            if (!orders.Contains(_toggleButton.order))
            {
                orders.Add(_toggleButton.order);
                orders.Sort(OrderCompare);
            }
        }
 
        public void UnRegister(FunctionButton _toggleButton)
        {
            if (toggleButtons.ContainsKey(_toggleButton.order))
            {
                toggleButtons.Remove(_toggleButton.order);
            }
 
            if (orders.Contains(_toggleButton.order))
            {
                orders.Remove(_toggleButton.order);
                orders.Sort(OrderCompare);
            }
 
        }
 
        public void NotifyToggleOn(FunctionButton _toggleButton)
        {
            if (_toggleButton.state == TitleBtnState.Click)
            {
                currentOrder = _toggleButton.order;
                for (int i = 0; i < orders.Count; i++)
                {
                    var toggleButton = toggleButtons[orders[i]];
                    if (toggleButton != _toggleButton && toggleButton.state != TitleBtnState.Locked)
                    {
                        toggleButton.state = TitleBtnState.Normal;
                    }
                }
            }
        }
 
        public void TriggerByOrder(int _order)
        {
            for (int i = 0; i < orders.Count; i++)
            {
                var order = orders[i];
                if (order == _order)
                {
                    toggleButtons[order].Invoke(true);
                    break;
                }
            }
        }
 
        public void TriggerLast()
        {
            var index = orders.IndexOf(currentOrder);
            if (index < 0)
            {
                return;
            }
 
            var loopTimes = 0;
            while (loopTimes < 2)
            {
                index--;
                if (index < 0)
                {
                    index = orders.Count - 1;
                    loopTimes++;
                }
 
                var next = orders[index];
                if (toggleButtons[next].state != TitleBtnState.Locked)
                {
                    toggleButtons[next].Invoke(false);
                    break;
                }
            }
 
        }
 
        public void TriggerNext()
        {
            var index = orders.IndexOf(currentOrder);
            if (index < 0)
            {
                return;
            }
 
            var loopTimes = 0;
            while (loopTimes < 2)
            {
                index++;
                if (index > orders.Count - 1)
                {
                    index = 0;
                    loopTimes++;
                }
 
                var next = orders[index];
                if (toggleButtons[next].state != TitleBtnState.Locked)
                {
                    toggleButtons[next].Invoke(false);
                    break;
                }
            }
        }
 
        public bool IsFirst()
        {
            return orders.Count > 0 && currentOrder == orders[0];
        }
 
        public bool IsLast()
        {
            return orders.Count > 0 && currentOrder == orders[orders.Count - 1];
        }
 
        private int OrderCompare(int a, int b)
        {
            return a < b ? -1 : 1;
        }
    }
 
}