ColorrPaletteParts.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using wf;
  5. public class ColorrPaletteParts : MonoBehaviour
  6. {
  7. private void Awake()
  8. {
  9. Transform transform = base.transform;
  10. while (this.ui_camera_ == null && transform != null)
  11. {
  12. UIRoot component = transform.parent.GetComponent<UIRoot>();
  13. if (component != null)
  14. {
  15. this.ui_camera_ = component.GetComponentInChildren<Camera>();
  16. }
  17. transform = transform.parent;
  18. }
  19. Texture2D texture2D = Resources.Load("SceneEdit/ColorPalette/Texture/cm3d2_ColorPalette_PickerGray 2") as Texture2D;
  20. UIEventTrigger uieventTrigger = this.PaletteHitArea.GetComponent<UIEventTrigger>();
  21. if (uieventTrigger == null)
  22. {
  23. uieventTrigger = this.PaletteHitArea.gameObject.AddComponent<UIEventTrigger>();
  24. }
  25. EventDelegate.Add(uieventTrigger.onPress, new EventDelegate.Callback(this.OnMouseDownPalettePanel));
  26. EventDelegate.Add(uieventTrigger.onRelease, new EventDelegate.Callback(this.OnMouseUpPalettePanel));
  27. EventDelegate.Add(this.HueSlider.onChange, new EventDelegate.Callback(this.OnChangeHue));
  28. this.hsv_color_.s = 0f;
  29. this.hsv_color_.v = 1f;
  30. }
  31. public void SetColor(Color color)
  32. {
  33. this.hsv_color_ = new HSVColor(color);
  34. float num = this.hsv_color_.h / 360f;
  35. if (this.HueSlider.value != num)
  36. {
  37. this.HueSlider.value = num;
  38. }
  39. else
  40. {
  41. this.UIUpdate();
  42. }
  43. }
  44. public Color GetColor()
  45. {
  46. return this.hsv_color_.ToColor();
  47. }
  48. public void Update()
  49. {
  50. if (this.is_drag_pic_)
  51. {
  52. Vector3 position = this.ui_camera_.ScreenToWorldPoint(UICamera.lastTouchPosition);
  53. Vector3 v = this.PaletteHitArea.transform.InverseTransformPoint(position);
  54. Vector2 vector = this.ConvertToSelectorPos(v);
  55. Vector3 size = this.PaletteHitArea.size;
  56. this.hsv_color_.s = vector.x / size.x;
  57. this.hsv_color_.v = (size.y - vector.y) / size.y;
  58. this.UIUpdate();
  59. }
  60. }
  61. public void UIUpdate()
  62. {
  63. Vector3 size = this.PaletteHitArea.size;
  64. Vector2 v;
  65. v.x = size.x * this.hsv_color_.s;
  66. v.y = size.y * (1f - this.hsv_color_.v) * -1f;
  67. this.PaletteColorPic.transform.localPosition = v;
  68. float s = this.hsv_color_.s;
  69. float v2 = this.hsv_color_.v;
  70. this.hsv_color_.s = (this.hsv_color_.v = 1f);
  71. this.PaletteColorField.color = this.hsv_color_.ToColor();
  72. this.hsv_color_.s = s;
  73. this.hsv_color_.v = v2;
  74. if (this.onChangeValue != null && 0 < this.onChangeValue.Count)
  75. {
  76. for (int i = 0; i < this.onChangeValue.Count; i++)
  77. {
  78. this.onChangeValue[i](this.hsv_color_.ToColor());
  79. }
  80. }
  81. }
  82. private void OnMouseDownPalettePanel()
  83. {
  84. if (UICamera.currentTouchID == -1)
  85. {
  86. this.is_drag_pic_ = true;
  87. }
  88. }
  89. private void OnMouseUpPalettePanel()
  90. {
  91. this.is_drag_pic_ = false;
  92. }
  93. private void OnChangeHue()
  94. {
  95. this.hsv_color_.h = 360f * this.HueSlider.value;
  96. this.UIUpdate();
  97. }
  98. private Vector2 ConvertToSelectorPos(Vector2 pos)
  99. {
  100. Vector2 localSize = this.PaletteColorField.localSize;
  101. Vector2 localSize2 = this.PaletteColorPic.localSize;
  102. Vector2 zero = Vector2.zero;
  103. zero.x = (localSize.x + localSize2.x) / 2f + pos.x - localSize2.x / 2f;
  104. zero.y = (localSize.y + localSize2.y) / 2f - pos.y - localSize2.y / 2f;
  105. zero.x = Mathf.Clamp(zero.x, 0f, localSize.x);
  106. zero.y = Mathf.Clamp(zero.y, 0f, localSize.y);
  107. return zero;
  108. }
  109. public new bool enabled
  110. {
  111. get
  112. {
  113. return this.PaletteHitArea.enabled;
  114. }
  115. set
  116. {
  117. this.PaletteHitArea.enabled = value;
  118. UIButton[] componentsInChildren = base.GetComponentsInChildren<UIButton>();
  119. foreach (UIButton uibutton in componentsInChildren)
  120. {
  121. uibutton.isEnabled = value;
  122. }
  123. UISlider[] componentsInChildren2 = base.GetComponentsInChildren<UISlider>();
  124. foreach (UISlider uislider in componentsInChildren2)
  125. {
  126. uislider.enabled = value;
  127. }
  128. }
  129. }
  130. public bool visible
  131. {
  132. get
  133. {
  134. return base.gameObject.activeSelf;
  135. }
  136. set
  137. {
  138. base.gameObject.SetActive(value);
  139. }
  140. }
  141. public BoxCollider PaletteHitArea;
  142. public UISprite PaletteColorField;
  143. public UISprite PaletteColorPic;
  144. public UISlider HueSlider;
  145. public List<Action<Color>> onChangeValue = new List<Action<Color>>();
  146. private Camera ui_camera_;
  147. private bool is_drag_pic_;
  148. private HSVColor hsv_color_;
  149. }