uGUIDraggingObject.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Events;
  4. using UnityEngine.EventSystems;
  5. using UnityEngine.UI;
  6. [RequireComponent(typeof(CanvasGroup))]
  7. public class uGUIDraggingObject : UIBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler, IPointerDownHandler, IPointerUpHandler, IPointerClickHandler, IEventSystemHandler
  8. {
  9. private RectTransform rectTrans
  10. {
  11. get
  12. {
  13. return this.m_RectTrans;
  14. }
  15. }
  16. private CanvasGroup canvasGroup
  17. {
  18. get
  19. {
  20. return this.m_CanvasGroup;
  21. }
  22. }
  23. public UnityEvent onSelectEvent
  24. {
  25. get
  26. {
  27. return this.m_OnSelectEvent;
  28. }
  29. set
  30. {
  31. this.m_OnSelectEvent = value;
  32. }
  33. }
  34. public UnityEvent onDeselectEvent
  35. {
  36. get
  37. {
  38. return this.m_OnDeselectEvent;
  39. }
  40. set
  41. {
  42. this.m_OnDeselectEvent = value;
  43. }
  44. }
  45. public UnityEvent onClickEvent
  46. {
  47. get
  48. {
  49. return this.m_OnClickEvent;
  50. }
  51. set
  52. {
  53. this.m_OnClickEvent = value;
  54. }
  55. }
  56. public bool isScaling
  57. {
  58. get
  59. {
  60. return this.m_IsScaling;
  61. }
  62. set
  63. {
  64. this.m_IsScaling = value;
  65. }
  66. }
  67. public float minSizeX
  68. {
  69. get
  70. {
  71. return this.m_MinSizeX;
  72. }
  73. set
  74. {
  75. this.m_MinSizeX = Mathf.Clamp(value, 20f, float.PositiveInfinity);
  76. }
  77. }
  78. public float minSizeY
  79. {
  80. get
  81. {
  82. return this.m_MinSizeY;
  83. }
  84. set
  85. {
  86. this.m_MinSizeY = Mathf.Clamp(value, 20f, float.PositiveInfinity);
  87. }
  88. }
  89. protected override void Start()
  90. {
  91. base.Start();
  92. this.m_StartPosition = this.rectTrans.anchoredPosition;
  93. this.m_StartSizeDelta = this.rectTrans.sizeDelta;
  94. if (this.rectTrans.parent)
  95. {
  96. int siblingIndex = base.transform.GetSiblingIndex();
  97. uGUIDraggingObject[] componentsInChildren = this.rectTrans.parent.GetComponentsInChildren<uGUIDraggingObject>();
  98. int num = componentsInChildren.Length - 1;
  99. if (componentsInChildren[num] == this)
  100. {
  101. this.canvasGroup.alpha = 1f;
  102. this.canvasGroup.interactable = true;
  103. }
  104. else
  105. {
  106. this.canvasGroup.alpha = 0.5f;
  107. this.canvasGroup.interactable = false;
  108. this.CreateBlocker();
  109. }
  110. }
  111. this.m_IsStarted = true;
  112. }
  113. public void ResetTransform()
  114. {
  115. if (!this.m_IsStarted)
  116. {
  117. return;
  118. }
  119. this.rectTrans.anchoredPosition = this.m_StartPosition;
  120. this.rectTrans.sizeDelta = this.m_StartSizeDelta;
  121. }
  122. public void OnPointerDown(PointerEventData eventData)
  123. {
  124. if (eventData.button != PointerEventData.InputButton.Left)
  125. {
  126. return;
  127. }
  128. this.ChangeWindowColor();
  129. EventSystem.current.SetSelectedGameObject(base.gameObject);
  130. this.OnBeginDrag(eventData);
  131. }
  132. public void OnDrag(PointerEventData eventData)
  133. {
  134. if (!this.m_IsMove)
  135. {
  136. return;
  137. }
  138. if (eventData.button != PointerEventData.InputButton.Left)
  139. {
  140. return;
  141. }
  142. Vector3 b = eventData.position - eventData.pressPosition;
  143. b.Scale(this.m_ReciprocalCanvasScale);
  144. Rect rect = this.rectTrans.rect;
  145. Vector3 vector = this.m_ClickPosition;
  146. if (this.m_IsDragRight)
  147. {
  148. rect.width = this.m_ClickRect.width + b.x;
  149. vector -= new Vector3(b.x * 0.5f, b.y, 0f);
  150. }
  151. if (this.m_IsDragLeft)
  152. {
  153. rect.width = this.m_ClickRect.width - b.x;
  154. vector -= new Vector3(b.x * 0.5f, b.y, 0f);
  155. }
  156. if (this.m_IsDragBottom)
  157. {
  158. rect.height = this.m_ClickRect.height + b.y;
  159. vector -= new Vector3(b.x, b.y * 0.5f, 0f);
  160. }
  161. if (this.m_IsDragTop)
  162. {
  163. rect.height = this.m_ClickRect.height - b.y;
  164. vector -= new Vector3(b.x, b.y * 0.5f, 0f);
  165. }
  166. if (rect.size.x < this.minSizeX)
  167. {
  168. rect.width = this.minSizeX;
  169. float num = Mathf.Sign(b.x);
  170. vector.x -= (b.x - num * (this.m_ClickRect.width - this.minSizeX)) * 0.5f;
  171. }
  172. if (rect.size.y < this.minSizeY)
  173. {
  174. rect.height = this.minSizeY;
  175. float num2 = Mathf.Sign(b.y);
  176. vector.y -= (b.y - num2 * (this.m_ClickRect.height - this.minSizeY)) * 0.5f;
  177. }
  178. if ((this.m_IsDragRight || this.m_IsDragLeft) && (this.m_IsDragTop || this.m_IsDragBottom))
  179. {
  180. vector += b;
  181. }
  182. vector += b;
  183. this.rectTrans.localPosition = vector;
  184. this.rectTrans.sizeDelta = rect.size;
  185. }
  186. public void OnPointerUp(PointerEventData eventData)
  187. {
  188. if (eventData.button != PointerEventData.InputButton.Left)
  189. {
  190. return;
  191. }
  192. this.m_IsDragRight = false;
  193. this.m_IsDragLeft = false;
  194. this.m_IsDragTop = false;
  195. this.m_IsDragBottom = false;
  196. this.m_IsMove = false;
  197. }
  198. public void OnPointerClick(PointerEventData eventData)
  199. {
  200. if (this.onClickEvent != null)
  201. {
  202. this.onClickEvent.Invoke();
  203. }
  204. }
  205. public void OnBeginDrag(PointerEventData eventData)
  206. {
  207. if (this.m_IsMove)
  208. {
  209. return;
  210. }
  211. Canvas rootCanvas = base.GetComponentInParent<Canvas>().rootCanvas;
  212. RectTransform component = rootCanvas.GetComponent<RectTransform>();
  213. this.UpdateReciprocalCanvasScale();
  214. Vector2 position = eventData.position;
  215. Rect rect = this.rectTrans.rect;
  216. Vector2 vector = position - new Vector2(this.rectTrans.localPosition.x, this.rectTrans.localPosition.y);
  217. vector = position - RectTransformUtility.WorldToScreenPoint(rootCanvas.worldCamera, this.rectTrans.position);
  218. vector.Scale(this.m_ReciprocalCanvasScale);
  219. this.m_ClickPosition = this.rectTrans.localPosition;
  220. this.m_ClickRect = rect;
  221. if (this.isScaling)
  222. {
  223. float dragScalingRange = this.m_DragScalingRange;
  224. this.m_IsDragRight = (Mathf.Abs(vector.x - rect.xMax) < dragScalingRange);
  225. this.m_IsDragLeft = (Mathf.Abs(vector.x - rect.xMin) < dragScalingRange);
  226. this.m_IsDragBottom = (Mathf.Abs(vector.y - rect.yMax) < dragScalingRange);
  227. this.m_IsDragTop = (Mathf.Abs(vector.y - rect.yMin) < dragScalingRange);
  228. }
  229. this.m_IsMove = true;
  230. }
  231. public void OnEndDrag(PointerEventData eventData)
  232. {
  233. this.OnPointerUp(eventData);
  234. }
  235. private void UpdateReciprocalCanvasScale()
  236. {
  237. Canvas rootCanvas = base.GetComponentInParent<Canvas>().rootCanvas;
  238. UIRoot componentInParent = base.GetComponentInParent<UIRoot>();
  239. Vector2 v = new Vector2((float)componentInParent.manualWidth, (float)componentInParent.manualHeight);
  240. Vector2 vector = new Vector2((float)rootCanvas.worldCamera.pixelWidth, (float)rootCanvas.worldCamera.pixelHeight);
  241. float num = v.y / v.x;
  242. float num2 = vector.y / vector.x;
  243. float num3 = num2 / num;
  244. this.m_ReciprocalCanvasScale = v;
  245. this.m_ReciprocalCanvasScale.x = this.m_ReciprocalCanvasScale.x / vector.x;
  246. this.m_ReciprocalCanvasScale.y = this.m_ReciprocalCanvasScale.y / vector.y;
  247. if (num3 > 1f)
  248. {
  249. this.m_ReciprocalCanvasScale.y = this.m_ReciprocalCanvasScale.y * num3;
  250. }
  251. else
  252. {
  253. this.m_ReciprocalCanvasScale.x = this.m_ReciprocalCanvasScale.x / num3;
  254. }
  255. }
  256. private void ChangeWindowColor()
  257. {
  258. this.rectTrans.SetAsLastSibling();
  259. if (this.canvasGroup)
  260. {
  261. this.canvasGroup.alpha = 1f;
  262. this.canvasGroup.interactable = true;
  263. }
  264. if (this.onSelectEvent != null)
  265. {
  266. this.onSelectEvent.Invoke();
  267. }
  268. if (this.rectTrans.parent)
  269. {
  270. uGUIDraggingObject[] componentsInChildren = this.rectTrans.parent.GetComponentsInChildren<uGUIDraggingObject>();
  271. for (int i = 0; i < componentsInChildren.Length; i++)
  272. {
  273. if (!(componentsInChildren[i] == this))
  274. {
  275. CanvasGroup canvasGroup = componentsInChildren[i].canvasGroup;
  276. if (canvasGroup)
  277. {
  278. canvasGroup.alpha = 0.5f;
  279. canvasGroup.interactable = false;
  280. componentsInChildren[i].CreateBlocker();
  281. }
  282. if (componentsInChildren[i].m_OnDeselectEvent != null)
  283. {
  284. componentsInChildren[i].m_OnDeselectEvent.Invoke();
  285. }
  286. }
  287. }
  288. }
  289. }
  290. private void CreateBlocker()
  291. {
  292. if (this.m_Blocker)
  293. {
  294. return;
  295. }
  296. GameObject blocker = new GameObject("[uGUIDraggingObject] Raycast Blocker");
  297. this.m_Blocker = blocker;
  298. RectTransform rectTransform = blocker.AddComponent<RectTransform>();
  299. rectTransform.SetParent(this.rectTrans, false);
  300. rectTransform.anchorMin = Vector3.zero;
  301. rectTransform.anchorMax = Vector3.one;
  302. rectTransform.sizeDelta = Vector2.zero;
  303. Canvas canvas = blocker.AddComponent<Canvas>();
  304. canvas.overrideSorting = true;
  305. canvas.sortingOrder = base.GetComponentInParent<Canvas>().sortingOrder + 10;
  306. CanvasGroup canvasGroup = blocker.AddComponent<CanvasGroup>();
  307. canvasGroup.ignoreParentGroups = true;
  308. canvasGroup.interactable = true;
  309. canvasGroup.blocksRaycasts = true;
  310. GraphicRaycaster graphicRaycaster = blocker.AddComponent<GraphicRaycaster>();
  311. graphicRaycaster.blockingObjects = GraphicRaycaster.BlockingObjects.All;
  312. Image image = blocker.AddComponent<Image>();
  313. image.color = Color.clear;
  314. uGUIDraggingObject.RaycastBlocker raycastBlocker = blocker.AddComponent<uGUIDraggingObject.RaycastBlocker>();
  315. raycastBlocker.onPointerDown = delegate(PointerEventData data)
  316. {
  317. UnityEngine.Object.Destroy(blocker);
  318. this.m_Blocker = null;
  319. this.OnPointerDown(data);
  320. };
  321. }
  322. private void OnReset()
  323. {
  324. Canvas canvas = base.GetComponent<Canvas>();
  325. if (!canvas)
  326. {
  327. canvas = base.gameObject.AddComponent<Canvas>();
  328. canvas.overrideSorting = false;
  329. uGUIUtility.SetLayer(base.gameObject, LayerMask.NameToLayer("NGUI"), true);
  330. }
  331. GraphicRaycaster graphicRaycaster = base.GetComponent<GraphicRaycaster>();
  332. if (!graphicRaycaster)
  333. {
  334. graphicRaycaster = base.gameObject.AddComponent<GraphicRaycaster>();
  335. graphicRaycaster.blockingObjects = GraphicRaycaster.BlockingObjects.All;
  336. }
  337. CanvasGroup exists = base.GetComponent<CanvasGroup>();
  338. if (!exists)
  339. {
  340. exists = base.gameObject.AddComponent<CanvasGroup>();
  341. }
  342. if (!this.m_RectTrans)
  343. {
  344. this.m_RectTrans = base.GetComponent<RectTransform>();
  345. }
  346. if (!this.m_CanvasGroup)
  347. {
  348. this.m_CanvasGroup = base.GetComponent<CanvasGroup>();
  349. }
  350. }
  351. [SerializeField]
  352. [HideInInspector]
  353. private RectTransform m_RectTrans;
  354. [SerializeField]
  355. [HideInInspector]
  356. private CanvasGroup m_CanvasGroup;
  357. [SerializeField]
  358. [Tooltip("選択されたときのイベント")]
  359. private UnityEvent m_OnSelectEvent;
  360. [SerializeField]
  361. [Tooltip("別のウィンドウが選択されたときのイベント")]
  362. private UnityEvent m_OnDeselectEvent;
  363. [SerializeField]
  364. [Tooltip("クリックされるたびに呼ばれるイベント")]
  365. private UnityEvent m_OnClickEvent;
  366. [SerializeField]
  367. [Tooltip("端のドラッグによるスケーリングを行うかどうか")]
  368. private bool m_IsScaling;
  369. [SerializeField]
  370. private float m_MinSizeX = 100f;
  371. [SerializeField]
  372. private float m_MinSizeY = 100f;
  373. private bool m_IsDragRight;
  374. private bool m_IsDragLeft;
  375. private bool m_IsDragTop;
  376. private bool m_IsDragBottom;
  377. private float m_DragScalingRange = 15f;
  378. private bool m_IsMove;
  379. private Vector3 m_ClickPosition = Vector3.zero;
  380. private Rect m_ClickRect;
  381. private Vector3 m_StartPosition;
  382. private Vector2 m_StartSizeDelta;
  383. private Vector3 m_ReciprocalCanvasScale;
  384. private GameObject m_Blocker;
  385. private bool m_IsStarted;
  386. private class RaycastBlocker : MonoBehaviour, IPointerDownHandler, IEventSystemHandler
  387. {
  388. public void OnPointerDown(PointerEventData eventData)
  389. {
  390. if (this.onPointerDown != null)
  391. {
  392. this.onPointerDown(eventData);
  393. }
  394. }
  395. public Action<PointerEventData> onPointerDown;
  396. }
  397. }