-
Notifications
You must be signed in to change notification settings - Fork 666
Expand file tree
/
Copy pathGLoader.cs
More file actions
759 lines (676 loc) · 21 KB
/
GLoader.cs
File metadata and controls
759 lines (676 loc) · 21 KB
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
using System;
using UnityEngine;
using FairyGUI.Utils;
namespace FairyGUI
{
/// <summary>
/// GLoader class
/// </summary>
public class GLoader : GObject, IAnimationGear, IColorGear
{
/// <summary>
/// Display an error sign if the loader fails to load the content.
/// UIConfig.loaderErrorSign muse be set.
/// </summary>
public bool showErrorSign;
string _url;
AlignType _align;
VertAlignType _verticalAlign;
bool _autoSize;
FillType _fill;
bool _shrinkOnly;
bool _useResize;
bool _updatingLayout;
PackageItem _contentItem;
Action<NTexture> _reloadDelegate;
MovieClip _content;
GObject _errorSign;
GComponent _content2;
#if FAIRYGUI_PUERTS
public Action __loadExternal;
public Action<NTexture> __freeExternal;
public Action<string> __setUrl;
#endif
public GLoader()
{
_url = string.Empty;
_align = AlignType.Left;
_verticalAlign = VertAlignType.Top;
showErrorSign = true;
_reloadDelegate = OnExternalReload;
}
override protected void CreateDisplayObject()
{
displayObject = new Container("GLoader");
displayObject.gOwner = this;
_content = new MovieClip();
((Container)displayObject).AddChild(_content);
((Container)displayObject).opaque = true;
}
override public void Dispose()
{
if (_disposed) return;
if (_content.texture != null)
{
if (_contentItem == null)
{
_content.texture.onSizeChanged -= _reloadDelegate;
try
{
FreeExternal(_content.texture);
}
catch (Exception err)
{
Debug.LogWarning(err);
}
}
}
if (_errorSign != null)
_errorSign.Dispose();
if (_content2 != null)
_content2.Dispose();
_content.Dispose();
base.Dispose();
}
/// <summary>
///
/// </summary>
public string url
{
get { return _url; }
set
{
if (_url == value)
return;
ClearContent();
_url = value;
#if FAIRYGUI_PUERTS
if (__setUrl != null)
{
__setUrl(_url);
}
#endif
LoadContent();
UpdateGear(7);
}
}
override public string icon
{
get { return _url; }
set { this.url = value; }
}
/// <summary>
///
/// </summary>
public AlignType align
{
get { return _align; }
set
{
if (_align != value)
{
_align = value;
UpdateLayout();
}
}
}
/// <summary>
///
/// </summary>
public VertAlignType verticalAlign
{
get { return _verticalAlign; }
set
{
if (_verticalAlign != value)
{
_verticalAlign = value;
UpdateLayout();
}
}
}
/// <summary>
///
/// </summary>
public FillType fill
{
get { return _fill; }
set
{
if (_fill != value)
{
_fill = value;
UpdateLayout();
}
}
}
/// <summary>
///
/// </summary>
public bool useResize
{
get { return _useResize; }
set
{
if (_useResize != value)
{
_useResize = value;
UpdateLayout();
}
}
}
/// <summary>
///
/// </summary>
public bool shrinkOnly
{
get { return _shrinkOnly; }
set
{
if (_shrinkOnly != value)
{
_shrinkOnly = value;
UpdateLayout();
}
}
}
/// <summary>
///
/// </summary>
public bool autoSize
{
get { return _autoSize; }
set
{
if (_autoSize != value)
{
_autoSize = value;
UpdateLayout();
}
}
}
/// <summary>
///
/// </summary>
public bool playing
{
get { return _content.playing; }
set
{
_content.playing = value;
UpdateGear(5);
}
}
/// <summary>
///
/// </summary>
public int frame
{
get { return _content.frame; }
set
{
_content.frame = value;
UpdateGear(5);
}
}
/// <summary>
///
/// </summary>
public float timeScale
{
get { return _content.timeScale; }
set { _content.timeScale = value; }
}
/// <summary>
///
/// </summary>
public bool ignoreEngineTimeScale
{
get { return _content.ignoreEngineTimeScale; }
set { _content.ignoreEngineTimeScale = value; }
}
/// <summary>
///
/// </summary>
/// <param name="time"></param>
public void Advance(float time)
{
_content.Advance(time);
}
/// <summary>
///
/// </summary>
public Material material
{
get { return _content.material; }
set { _content.material = value; }
}
/// <summary>
///
/// </summary>
public string shader
{
get { return _content.shader; }
set { _content.shader = value; }
}
/// <summary>
///
/// </summary>
public Color color
{
get { return _content.color; }
set
{
if (_content.color != value)
{
_content.color = value;
UpdateGear(4);
}
}
}
/// <summary>
///
/// </summary>
public FillMethod fillMethod
{
get { return _content.fillMethod; }
set { _content.fillMethod = value; }
}
/// <summary>
///
/// </summary>
public int fillOrigin
{
get { return _content.fillOrigin; }
set { _content.fillOrigin = value; }
}
/// <summary>
///
/// </summary>
public bool fillClockwise
{
get { return _content.fillClockwise; }
set { _content.fillClockwise = value; }
}
/// <summary>
///
/// </summary>
public float fillAmount
{
get { return _content.fillAmount; }
set { _content.fillAmount = value; }
}
/// <summary>
///
/// </summary>
public Image image
{
get { return _content; }
}
/// <summary>
///
/// </summary>
public MovieClip movieClip
{
get { return _content; }
}
/// <summary>
///
/// </summary>
public GComponent component
{
get { return _content2; }
}
/// <summary>
///
/// </summary>
public NTexture texture
{
get
{
return _content.texture;
}
set
{
this.url = null;
_content.texture = value;
if (value != null)
{
sourceWidth = value.width;
sourceHeight = value.height;
}
else
{
sourceWidth = sourceHeight = 0;
}
UpdateLayout();
}
}
override public IFilter filter
{
get { return _content.filter; }
set { _content.filter = value; }
}
override public BlendMode blendMode
{
get { return _content.blendMode; }
set { _content.blendMode = value; }
}
/// <summary>
///
/// </summary>
protected void LoadContent()
{
ClearContent();
if (string.IsNullOrEmpty(_url))
return;
if (_url.StartsWith(UIPackage.URL_PREFIX))
LoadFromPackage(_url);
else
LoadExternal();
}
public void SetComponentContent(GComponent obj)
{
if (obj == _content2) return;
ClearContent();
_content2 = obj;
if (_content2 != null)
{
((Container)displayObject).AddChild(obj.displayObject);
sourceWidth = (int)_content2.width;
sourceHeight = (int)_content2.height;
UpdateLayout();
}
}
protected void LoadFromPackage(string itemURL)
{
_contentItem = UIPackage.GetItemByURL(itemURL);
if (_contentItem != null)
{
_contentItem = _contentItem.getBranch();
sourceWidth = _contentItem.width;
sourceHeight = _contentItem.height;
_contentItem = _contentItem.getHighResolution();
_contentItem.Load();
if (_contentItem.type == PackageItemType.Image)
{
_content.texture = _contentItem.texture;
_content.textureScale = new Vector2(_contentItem.width / (float)sourceWidth, _contentItem.height / (float)sourceHeight);
_content.scale9Grid = _contentItem.scale9Grid;
_content.scaleByTile = _contentItem.scaleByTile;
_content.tileGridIndice = _contentItem.tileGridIndice;
UpdateLayout();
}
else if (_contentItem.type == PackageItemType.MovieClip)
{
_content.interval = _contentItem.interval;
_content.swing = _contentItem.swing;
_content.repeatDelay = _contentItem.repeatDelay;
_content.frames = _contentItem.frames;
UpdateLayout();
}
else if (_contentItem.type == PackageItemType.Component)
{
GObject obj = UIPackage.CreateObjectFromURL(itemURL);
if (obj == null)
SetErrorState();
else if (!(obj is GComponent))
{
obj.Dispose();
SetErrorState();
}
else
{
_content2 = (GComponent)obj;
((Container)displayObject).AddChild(_content2.displayObject);
UpdateLayout();
}
}
else
{
if (_autoSize)
this.SetSize(_contentItem.width, _contentItem.height);
SetErrorState();
Debug.LogWarning("Unsupported type of GLoader: " + _contentItem.type);
}
}
else
SetErrorState();
}
virtual protected void LoadExternal()
{
#if FAIRYGUI_PUERTS
if (__loadExternal != null)
{
__loadExternal();
return;
}
#endif
Texture2D tex = (Texture2D)Resources.Load(_url, typeof(Texture2D));
if (tex != null)
onExternalLoadSuccess(new NTexture(tex));
else
onExternalLoadFailed();
}
virtual protected void FreeExternal(NTexture texture)
{
#if FAIRYGUI_PUERTS
if (__freeExternal != null)
{
__freeExternal(texture);
return;
}
#endif
}
public void onExternalLoadSuccess(NTexture texture)
{
_content.texture = texture;
sourceWidth = texture.width;
sourceHeight = texture.height;
_content.scale9Grid = null;
_content.scaleByTile = false;
texture.onSizeChanged += _reloadDelegate;
UpdateLayout();
}
public void onExternalLoadFailed()
{
SetErrorState();
}
void OnExternalReload(NTexture texture)
{
sourceWidth = texture.width;
sourceHeight = texture.height;
UpdateLayout();
}
private void SetErrorState()
{
if (!showErrorSign || !Application.isPlaying)
return;
if (_errorSign == null)
{
if (UIConfig.loaderErrorSign != null)
_errorSign = UIPackage.CreateObjectFromURL(UIConfig.loaderErrorSign);
else
return;
}
if (_errorSign != null)
{
_errorSign.SetSize(this.width, this.height);
((Container)displayObject).AddChild(_errorSign.displayObject);
}
}
protected void ClearErrorState()
{
if (_errorSign != null && _errorSign.displayObject.parent != null)
((Container)displayObject).RemoveChild(_errorSign.displayObject);
}
protected void UpdateLayout()
{
if (_content2 == null && _content.texture == null && _content.frames == null)
{
if (_autoSize)
{
_updatingLayout = true;
SetSize(50, 30);
_updatingLayout = false;
}
return;
}
float contentWidth = sourceWidth;
float contentHeight = sourceHeight;
if (_autoSize)
{
_updatingLayout = true;
if (contentWidth == 0)
contentWidth = 50;
if (contentHeight == 0)
contentHeight = 30;
SetSize(contentWidth, contentHeight);
_updatingLayout = false;
if (_width == contentWidth && _height == contentHeight)
{
if (_content2 != null)
{
_content2.SetXY(0, 0);
_content2.SetScale(1, 1);
if (_useResize)
_content2.SetSize(contentWidth, contentHeight, true);
}
else
{
_content.SetXY(0, 0);
_content.SetSize(contentWidth, contentHeight);
}
InvalidateBatchingState();
return;
}
//如果不相等,可能是由于大小限制造成的,要后续处理
}
float sx = 1, sy = 1;
if (_fill != FillType.None)
{
sx = this.width / sourceWidth;
sy = this.height / sourceHeight;
if (sx != 1 || sy != 1)
{
if (_fill == FillType.ScaleMatchHeight)
sx = sy;
else if (_fill == FillType.ScaleMatchWidth)
sy = sx;
else if (_fill == FillType.Scale)
{
if (sx > sy)
sx = sy;
else
sy = sx;
}
else if (_fill == FillType.ScaleNoBorder)
{
if (sx > sy)
sy = sx;
else
sx = sy;
}
if (_shrinkOnly)
{
if (sx > 1)
sx = 1;
if (sy > 1)
sy = 1;
}
contentWidth = sourceWidth * sx;
contentHeight = sourceHeight * sy;
}
}
if (_content2 != null)
{
if (_useResize)
{
sx = sy = 1;
contentWidth = width;
contentHeight = height;
_content2.SetXY(0, 0);
_content2.SetScale(sx, sy);
_content2.SetSize(contentWidth, contentHeight, true);
}
_content2.SetScale(sx, sy);
}
else
_content.size = new Vector2(contentWidth, contentHeight);
float nx;
float ny;
if (_align == AlignType.Center)
nx = (this.width - contentWidth) / 2;
else if (_align == AlignType.Right)
nx = this.width - contentWidth;
else
nx = 0;
if (_verticalAlign == VertAlignType.Middle)
ny = (this.height - contentHeight) / 2;
else if (_verticalAlign == VertAlignType.Bottom)
ny = this.height - contentHeight;
else
ny = 0;
if (_content2 != null)
_content2.SetXY(nx, ny);
else
_content.SetXY(nx, ny);
InvalidateBatchingState();
}
private void ClearContent()
{
ClearErrorState();
if (_content.texture != null)
{
if (_contentItem == null)
{
_content.texture.onSizeChanged -= _reloadDelegate;
FreeExternal(_content.texture);
}
_content.texture = null;
}
_content.frames = null;
if (_content2 != null)
{
_content2.Dispose();
_content2 = null;
}
_contentItem = null;
}
override protected void HandleSizeChanged()
{
base.HandleSizeChanged();
if (!_updatingLayout)
UpdateLayout();
}
override public void Setup_BeforeAdd(ByteBuffer buffer, int beginPos)
{
base.Setup_BeforeAdd(buffer, beginPos);
buffer.Seek(beginPos, 5);
_url = buffer.ReadS();
_align = (AlignType)buffer.ReadByte();
_verticalAlign = (VertAlignType)buffer.ReadByte();
_fill = (FillType)buffer.ReadByte();
_shrinkOnly = buffer.ReadBool();
_autoSize = buffer.ReadBool();
showErrorSign = buffer.ReadBool();
_content.playing = buffer.ReadBool();
_content.frame = buffer.ReadInt();
if (buffer.ReadBool())
_content.color = buffer.ReadColor();
_content.fillMethod = (FillMethod)buffer.ReadByte();
if (_content.fillMethod != FillMethod.None)
{
_content.fillOrigin = buffer.ReadByte();
_content.fillClockwise = buffer.ReadBool();
_content.fillAmount = buffer.ReadFloat();
}
if (buffer.version >= 7)
_useResize = buffer.ReadBool();
else
_useResize = _fill == FillType.ScaleFree;
if (!string.IsNullOrEmpty(_url))
LoadContent();
}
}
}