GearLook.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using FairyGUI.Utils;
  4. namespace FairyGUI
  5. {
  6. class GearLookValue
  7. {
  8. public float alpha;
  9. public float rotation;
  10. public bool grayed;
  11. public bool touchable;
  12. public GearLookValue(float alpha, float rotation, bool grayed, bool touchable)
  13. {
  14. this.alpha = alpha;
  15. this.rotation = rotation;
  16. this.grayed = grayed;
  17. this.touchable = touchable;
  18. }
  19. }
  20. /// <summary>
  21. /// Gear is a connection between object and controller.
  22. /// </summary>
  23. public class GearLook : GearBase, ITweenListener
  24. {
  25. Dictionary<string, GearLookValue> _storage;
  26. GearLookValue _default;
  27. public GearLook(GObject owner)
  28. : base(owner)
  29. {
  30. }
  31. protected override void Init()
  32. {
  33. _default = new GearLookValue(_owner.alpha, _owner.rotation, _owner.grayed, _owner.touchable);
  34. _storage = new Dictionary<string, GearLookValue>();
  35. }
  36. override protected void AddStatus(string pageId, ByteBuffer buffer)
  37. {
  38. GearLookValue gv;
  39. if (pageId == null)
  40. gv = _default;
  41. else
  42. {
  43. gv = new GearLookValue(0, 0, false, false);
  44. _storage[pageId] = gv;
  45. }
  46. gv.alpha = buffer.ReadFloat();
  47. gv.rotation = buffer.ReadFloat();
  48. gv.grayed = buffer.ReadBool();
  49. gv.touchable = buffer.ReadBool();
  50. }
  51. override public void Apply()
  52. {
  53. GearLookValue gv;
  54. if (!_storage.TryGetValue(_controller.selectedPageId, out gv))
  55. gv = _default;
  56. if (_tweenConfig != null && _tweenConfig.tween && UIPackage._constructing == 0 && !disableAllTweenEffect)
  57. {
  58. _owner._gearLocked = true;
  59. _owner.grayed = gv.grayed;
  60. _owner.touchable = gv.touchable;
  61. _owner._gearLocked = false;
  62. if (_tweenConfig._tweener != null)
  63. {
  64. if (_tweenConfig._tweener.endValue.x != gv.alpha || _tweenConfig._tweener.endValue.y != gv.rotation)
  65. {
  66. _tweenConfig._tweener.Kill(true);
  67. _tweenConfig._tweener = null;
  68. }
  69. else
  70. return;
  71. }
  72. bool a = gv.alpha != _owner.alpha;
  73. bool b = gv.rotation != _owner.rotation;
  74. if (a || b)
  75. {
  76. if (_owner.CheckGearController(0, _controller))
  77. _tweenConfig._displayLockToken = _owner.AddDisplayLock();
  78. _tweenConfig._tweener = GTween.To(new Vector2(_owner.alpha, _owner.rotation), new Vector2(gv.alpha, gv.rotation), _tweenConfig.duration)
  79. .SetDelay(_tweenConfig.delay)
  80. .SetEase(_tweenConfig.easeType, _tweenConfig.customEase)
  81. .SetUserData((a ? 1 : 0) + (b ? 2 : 0))
  82. .SetTarget(this)
  83. .SetListener(this);
  84. }
  85. }
  86. else
  87. {
  88. _owner._gearLocked = true;
  89. _owner.alpha = gv.alpha;
  90. _owner.rotation = gv.rotation;
  91. _owner.grayed = gv.grayed;
  92. _owner.touchable = gv.touchable;
  93. _owner._gearLocked = false;
  94. }
  95. }
  96. public void OnTweenStart(GTweener tweener)
  97. {
  98. }
  99. public void OnTweenUpdate(GTweener tweener)
  100. {
  101. int flag = (int)tweener.userData;
  102. _owner._gearLocked = true;
  103. if ((flag & 1) != 0)
  104. _owner.alpha = tweener.value.x;
  105. if ((flag & 2) != 0)
  106. {
  107. _owner.rotation = tweener.value.y;
  108. _owner.InvalidateBatchingState();
  109. }
  110. _owner._gearLocked = false;
  111. }
  112. public void OnTweenComplete(GTweener tweener)
  113. {
  114. _tweenConfig._tweener = null;
  115. if (_tweenConfig._displayLockToken != 0)
  116. {
  117. _owner.ReleaseDisplayLock(_tweenConfig._displayLockToken);
  118. _tweenConfig._displayLockToken = 0;
  119. }
  120. _owner.DispatchEvent("onGearStop", this);
  121. }
  122. override public void UpdateState()
  123. {
  124. GearLookValue gv;
  125. if (!_storage.TryGetValue(_controller.selectedPageId, out gv))
  126. _storage[_controller.selectedPageId] = new GearLookValue(_owner.alpha, _owner.rotation, _owner.grayed, _owner.touchable);
  127. else
  128. {
  129. gv.alpha = _owner.alpha;
  130. gv.rotation = _owner.rotation;
  131. gv.grayed = _owner.grayed;
  132. gv.touchable = _owner.touchable;
  133. }
  134. }
  135. }
  136. }