@@ -18,18 +18,16 @@ public class UICircularFlodScrollView : UICircularScrollView
18
18
/// </summary>
19
19
private struct FlodBtnInfo
20
20
{
21
- public int index ;
22
21
public GameObject obj ;
23
22
public bool isFlod ;
24
23
public CellInfo [ ] cellInfos ;
25
24
public float cellsSize ; //子元素横向或纵尺寸
26
- public int cellCount ;
27
25
}
28
26
private FlodBtnInfo [ ] _flodBtnInfos ;
29
27
30
28
private UnityAction < GameObject , int > OnFlodBtnShow ;
31
29
private UnityAction < GameObject , int , int > OnFlodCellShow ;
32
- private UnityAction < int , bool , GameObject > OnFlodBtnClick ;
30
+ private UnityAction < GameObject , int , bool > OnFlodBtnClick ;
33
31
34
32
public void Init ( UnityAction < GameObject , int > onFlodBtnShow , UnityAction < GameObject , int , int > onFlodCellShow )
35
33
{
@@ -54,49 +52,43 @@ public void Init(UnityAction<GameObject, int> onFlodBtnShow, UnityAction<GameObj
54
52
public void ShowList ( params int [ ] num )
55
53
{
56
54
RecycleItems ( ) ;
57
- int curCellCount = 0 ; //当前元素数量
58
- int cellCount = 0 ; //元素总数量
59
- int flodBtnCount = num . Length ; //折叠按钮总数量
55
+ int flodBtnCount = num . Length ;
60
56
_flodBtnInfos = new FlodBtnInfo [ flodBtnCount ] ;
61
- for ( int k = 0 ; k < flodBtnCount ; k ++ )
57
+ for ( int i = 0 ; i < flodBtnCount ; i ++ )
62
58
{
63
59
//====生成折叠按钮
64
60
GameObject flodObj = ObjectPool . Instance . GetObject ( FLODBTNPOOL , _content ) ;
65
61
flodObj . SetActive ( true ) ;
66
- flodObj . name = k . ToString ( ) ;
67
- int count = num [ k ] ;
68
- cellCount += count ;
62
+ flodObj . name = i . ToString ( ) ;
63
+ int count = num [ i ] ;
69
64
//存储按钮数据
70
65
FlodBtnInfo flodBtnInfo = new FlodBtnInfo
71
66
{
72
- index = k ,
73
67
obj = flodObj ,
74
- cellCount = count ,
75
68
cellInfos = new CellInfo [ count ] ,
76
69
isFlod = _defaultFlod ,
77
70
cellsSize = _dir == UIDir . Vertical ? ( _cellHeight + _spacing ) * Mathf . CeilToInt ( ( float ) count / _crNum ) : ( _cellWight + _spacing ) * Mathf . CeilToInt ( ( float ) count / _crNum )
78
71
} ;
79
- _flodBtnInfos [ k ] = flodBtnInfo ;
80
- Button btn = flodObj . GetComponent < Button > ( ) ;
81
- btn . onClick . AddListener ( ( ) => OnFlodClick ( flodBtnInfo ) ) ;
82
- SetFlodBtnPos ( flodBtnInfo , curCellCount ) ;
72
+ _flodBtnInfos [ i ] = flodBtnInfo ;
73
+ int flodIndex = i ;
74
+ flodObj . GetComponent < Button > ( ) . onClick . AddListener ( ( ) => OnFlodClick ( flodBtnInfo , flodIndex ) ) ;
75
+ SetFlodBtnPos ( i ) ;
83
76
//=====生成cell
84
- for ( int i = 0 ; i < count ; i ++ )
77
+ for ( int k = 0 ; k < count ; k ++ )
85
78
{
86
79
if ( flodBtnInfo . isFlod ) break ;
87
80
CellInfo cellInfo = new CellInfo ( ) ;
88
- SetCellPos ( ref cellInfo , k , i , curCellCount ) ;
81
+ SetCellPos ( ref cellInfo , i , k ) ;
89
82
if ( SetCellState ( ref cellInfo ) )
90
83
{
91
- cellInfo . obj . name = k + "-" + i ;
92
- OnFlodCellShow ( cellInfo . obj , i , k ) ; //cell显示回调
84
+ cellInfo . obj . name = i + "-" + k ;
85
+ OnFlodCellShow ( cellInfo . obj , k , i ) ; //cell显示回调
93
86
}
94
- flodBtnInfo . cellInfos [ i ] = cellInfo ;
87
+ flodBtnInfo . cellInfos [ k ] = cellInfo ;
95
88
}
96
- curCellCount += flodBtnInfo . isFlod ? 0 : count ;
97
- OnFlodBtnShow ( flodObj , k ) ; //flodBtn显示回调
89
+ OnFlodBtnShow ( flodObj , i ) ; //flodBtn显示回调
98
90
}
99
- SetContentSize ( flodBtnCount , cellCount ) ;
91
+ SetContentSize ( flodBtnCount ) ;
100
92
}
101
93
102
94
protected override void ScrollRectListener ( Vector2 value )
@@ -106,7 +98,7 @@ protected override void ScrollRectListener(Vector2 value)
106
98
{
107
99
FlodBtnInfo flodBtnInfo = _flodBtnInfos [ i ] ;
108
100
if ( flodBtnInfo . isFlod ) continue ;
109
- for ( int k = 0 ; k < flodBtnInfo . cellCount ; k ++ )
101
+ for ( int k = 0 ; k < flodBtnInfo . cellInfos . Length ; k ++ )
110
102
{
111
103
CellInfo cellInfo = flodBtnInfo . cellInfos [ k ] ;
112
104
if ( SetCellState ( ref cellInfo ) )
@@ -119,6 +111,23 @@ protected override void ScrollRectListener(Vector2 value)
119
111
}
120
112
}
121
113
114
+ protected override void SetContentSize ( int flodBtnCount )
115
+ {
116
+ int lastIndex = flodBtnCount - 1 ;
117
+ if ( _dir == UIDir . Vertical )
118
+ {
119
+ float sizeY = - _flodBtnInfos [ lastIndex ] . obj . transform . localPosition . y + _flodBtnHeight + _spacing ;
120
+ sizeY += _defaultFlod ? 0 : _flodBtnInfos [ lastIndex ] . cellsSize ;
121
+ _content . sizeDelta = new Vector2 ( _content . sizeDelta . x , sizeY ) ;
122
+ }
123
+ else
124
+ {
125
+ float sizeX = _flodBtnInfos [ lastIndex ] . obj . transform . localPosition . x + _flodBtnWidth + _spacing ;
126
+ sizeX += _defaultFlod ? 0 : _flodBtnInfos [ lastIndex ] . cellsSize ;
127
+ _content . sizeDelta = new Vector2 ( sizeX , _content . sizeDelta . y ) ;
128
+ }
129
+ }
130
+
122
131
private void RecycleItems ( )
123
132
{
124
133
if ( ! _isInited ) return ;
@@ -140,62 +149,47 @@ private void RecycleItems()
140
149
}
141
150
}
142
151
143
- private void SetFlodBtnPos ( FlodBtnInfo flod , int curCellCount )
152
+ private void SetFlodBtnPos ( int index )
144
153
{
145
154
if ( _dir == UIDir . Vertical )
146
155
{
147
- float pos = _spacing ;
148
- if ( flod . index > 0 )
156
+ float posY = - _spacing ;
157
+ if ( index > 0 )
149
158
{
150
- pos + = _cellHeight ;
151
- pos += Mathf . Abs ( _flodBtnInfos [ flod . index - 1 ] . obj . transform . localPosition . y ) ;
152
- pos + = _flodBtnInfos [ flod . index - 1 ] . isFlod ? 0 : _flodBtnInfos [ flod . index - 1 ] . cellsSize ;
159
+ posY - = _cellHeight ;
160
+ posY += _flodBtnInfos [ index - 1 ] . obj . transform . localPosition . y ;
161
+ posY - = _flodBtnInfos [ index - 1 ] . isFlod ? 0 : _flodBtnInfos [ index - 1 ] . cellsSize ;
153
162
}
154
- flod . obj . transform . localPosition = new Vector2 ( _flodBtnX , - pos ) ;
163
+ _flodBtnInfos [ index ] . obj . transform . localPosition = new Vector2 ( _flodBtnX , posY ) ;
155
164
}
156
165
else
157
166
{
158
- float pos = _flodBtnWidth * flod . index + _spacing * ( flod . index + 1 ) ;
159
- pos += flod . index > 0 ? ( _cellWight + _spacing ) * Mathf . CeilToInt ( ( float ) curCellCount / _crNum ) : 0 ;
160
- flod . obj . transform . localPosition = new Vector2 ( pos , _flodBtnY ) ;
167
+ float posX = _spacing ;
168
+ if ( index > 0 )
169
+ {
170
+ posX += _cellWight ;
171
+ posX += _flodBtnInfos [ index - 1 ] . obj . transform . localPosition . x ;
172
+ posX += _flodBtnInfos [ index - 1 ] . isFlod ? 0 : _flodBtnInfos [ index - 1 ] . cellsSize ;
173
+ }
174
+ _flodBtnInfos [ index ] . obj . transform . localPosition = new Vector2 ( posX , _flodBtnY ) ;
161
175
}
162
176
}
163
177
164
- private void SetCellPos ( ref CellInfo cellInfo , int flodIndex , int cellIndex , int curCellCount )
178
+ private void SetCellPos ( ref CellInfo cellInfo , int flodIndex , int cellIndex )
165
179
{
166
180
if ( _dir == UIDir . Vertical )
167
181
{
168
- float posY = ( _cellHeight + _spacing ) * ( Mathf . CeilToInt ( cellIndex / _crNum ) + 1 ) + Mathf . Abs ( _flodBtnInfos [ flodIndex ] . obj . transform . localPosition . y ) ;
169
- //posY += (_flodBtnHeight + _spacing) * (flodIndex + 1);
170
- //posY += (_cellHeight + _spacing) * Mathf.CeilToInt((float)curCellCount / _crNum);
171
- //posY += Mathf.Abs(_flodBtnInfos[flodIndex].obj.transform.localPosition.y) + _cellHeight;
172
182
float posX = ( _cellWight + _spacing ) * ( cellIndex % _crNum ) ;
173
- cellInfo . pos = new Vector2 ( posX , - posY ) ;
174
- }
175
- else
176
- {
177
- float pos = _cellWight * Mathf . CeilToInt ( cellIndex / _crNum ) + _spacing * ( Mathf . CeilToInt ( cellIndex / _crNum ) + 1 ) ;
178
- pos += ( _flodBtnWidth + _spacing ) * ( flodIndex + 1 ) ;
179
- pos += ( _cellHeight + _spacing ) * Mathf . CeilToInt ( ( float ) curCellCount / _crNum ) ;
180
- float posY = _cellHeight * ( cellIndex % _crNum ) + _spacing * ( cellIndex % _crNum ) ;
181
- cellInfo . pos = new Vector2 ( pos , - posY ) ;
182
- }
183
- }
184
-
185
- private void SetContentSize ( int flodBtnCount , int cellCount )
186
- {
187
- if ( _dir == UIDir . Vertical )
188
- {
189
- float contentSize = Mathf . Abs ( _flodBtnInfos [ flodBtnCount - 1 ] . obj . transform . localPosition . y ) + _flodBtnHeight + _spacing ;
190
- // float contentSize = (_spacing + _flodBtnHeight) * flodBtnCount;
191
- contentSize += _defaultFlod ? 0 : _flodBtnInfos [ flodBtnCount - 1 ] . cellsSize ;
192
- _content . sizeDelta = new Vector2 ( _content . sizeDelta . x , contentSize ) ;
183
+ float posY = - ( _cellHeight + _spacing ) * ( Mathf . CeilToInt ( cellIndex / _crNum ) + 1 ) ;
184
+ posY += _flodBtnInfos [ flodIndex ] . obj . transform . localPosition . y ;
185
+ cellInfo . pos = new Vector2 ( posX , posY ) ;
193
186
}
194
187
else
195
188
{
196
- float contentSize = _defaultFlod ? 0 : ( _spacing + _cellWight ) * Mathf . CeilToInt ( ( float ) cellCount / _crNum ) ;
197
- contentSize += ( _spacing + _flodBtnWidth ) * flodBtnCount ;
198
- _content . sizeDelta = new Vector2 ( contentSize , _content . sizeDelta . y ) ;
189
+ float posX = ( _cellWight + _spacing ) * ( Mathf . CeilToInt ( cellIndex / _crNum ) + 1 ) ;
190
+ posX += _flodBtnInfos [ flodIndex ] . obj . transform . localPosition . x ;
191
+ float posY = - ( _cellHeight + _spacing ) * ( cellIndex % _crNum ) ;
192
+ cellInfo . pos = new Vector2 ( posX , posY ) ;
199
193
}
200
194
}
201
195
@@ -214,12 +208,12 @@ private void SetContentSize(FlodBtnInfo flod)
214
208
}
215
209
}
216
210
217
- private void OnFlodClick ( FlodBtnInfo flod )
211
+ private void OnFlodClick ( FlodBtnInfo flod , int index )
218
212
{
219
- OnFlodClick ( flod . index ) ;
213
+ OnFlodClick ( index ) ;
220
214
if ( OnFlodBtnClick != null )
221
215
{
222
- OnFlodBtnClick ( flod . index , flod . isFlod , flod . obj ) ;
216
+ OnFlodBtnClick ( flod . obj , index , flod . isFlod ) ;
223
217
}
224
218
}
225
219
@@ -228,15 +222,14 @@ private void OnFlodClick(int index)
228
222
_flodBtnInfos [ index ] . isFlod = ! _flodBtnInfos [ index ] . isFlod ;
229
223
//重置Content大小
230
224
SetContentSize ( _flodBtnInfos [ index ] ) ;
231
- int curCellCount = 0 ;
232
225
//下面的折叠按钮,重新计算坐标显示处理
233
226
for ( int i = 0 ; i < _flodBtnInfos . Length ; i ++ )
234
227
{
235
228
FlodBtnInfo flodBtnInfo = _flodBtnInfos [ i ] ;
236
- int count = flodBtnInfo . cellCount ;
229
+ int count = flodBtnInfo . cellInfos . Length ;
237
230
if ( i >= index )
238
231
{
239
- SetFlodBtnPos ( _flodBtnInfos [ i ] , curCellCount ) ;
232
+ SetFlodBtnPos ( i ) ;
240
233
for ( int k = 0 ; k < count ; k ++ )
241
234
{
242
235
if ( flodBtnInfo . isFlod )
@@ -250,7 +243,7 @@ private void OnFlodClick(int index)
250
243
else
251
244
{
252
245
CellInfo cellInfo = flodBtnInfo . cellInfos [ k ] ;
253
- SetCellPos ( ref cellInfo , i , k , curCellCount ) ;
246
+ SetCellPos ( ref cellInfo , i , k ) ;
254
247
if ( SetCellState ( ref cellInfo ) )
255
248
{
256
249
OnFlodCellShow ( cellInfo . obj , i , k ) ; //cell显示回调
@@ -260,10 +253,6 @@ private void OnFlodClick(int index)
260
253
}
261
254
}
262
255
}
263
- if ( ! _flodBtnInfos [ i ] . isFlod )
264
- {
265
- curCellCount += count ;
266
- }
267
256
}
268
257
}
269
258
}
0 commit comments