暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TilePaletteClipboardElement.cs 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.UIElements;
  4. namespace UnityEditor.Tilemaps
  5. {
  6. /// <summary>
  7. /// A Visual Element which handles and displays a Tile Palette Clipboard.
  8. /// A Tile Palette Clipboard shows the Active Palette for Grid Painting and allows
  9. /// users to use the Active Brush to assign and pick items for painting.
  10. /// </summary>
  11. [UxmlElement]
  12. public partial class TilePaletteClipboardElement : VisualElement
  13. {
  14. /// <summary>
  15. /// Factory for TilePaletteClipboardElement.
  16. /// </summary>
  17. [Obsolete("TilePaletteClipboardElementFactory is deprecated and will be removed. Use UxmlElementAttribute instead.", false)]
  18. public class TilePaletteClipboardElementFactory : UxmlFactory<TilePaletteClipboardElement, TilePaletteClipboardElementUxmlTraits> {}
  19. /// <summary>
  20. /// UxmlTraits for TilePaletteClipboardElement.
  21. /// </summary>
  22. [Obsolete("TilePaletteClipboardElementUxmlTraits is deprecated and will be removed. Use UxmlElementAttribute instead.", false)]
  23. public class TilePaletteClipboardElementUxmlTraits : UxmlTraits {}
  24. private static readonly string ussClassName = "unity-tilepalette-clipboard-element";
  25. private static readonly string k_Name = L10n.Tr("Tile Palette Clipboard Element");
  26. private GridPaintPaletteClipboard m_TilePaletteClipboard;
  27. private EditorWindow m_Window;
  28. /// <summary>
  29. /// Callback when the active Brush does a Pick on the Clipboard.
  30. /// </summary>
  31. public event Action onBrushPicked;
  32. /// <summary>
  33. /// Whether the clipboard is unlocked for editing.
  34. /// </summary>
  35. public bool clipboardUnlocked
  36. {
  37. get => m_TilePaletteClipboard.unlocked;
  38. set => m_TilePaletteClipboard.unlocked = value;
  39. }
  40. /// <summary>
  41. /// The last active grid position on the clipboard.
  42. /// </summary>
  43. public Vector3Int clipboardMouseGridPosition => new Vector3Int(m_TilePaletteClipboard.mouseGridPosition.x, m_TilePaletteClipboard.mouseGridPosition.y, m_TilePaletteClipboard.zPosition);
  44. /// <summary>
  45. /// Callback when the clipboard unlock status has changed
  46. /// </summary>
  47. public event Action<bool> clipboardUnlockedChanged;
  48. internal GridPaintPaletteClipboard clipboardView => m_TilePaletteClipboard;
  49. private TilePaletteClipboardFirstUserElement m_FirstUserElement;
  50. private TilePaletteClipboardErrorElement m_ErrorElement;
  51. private Image m_ClipboardImageElement;
  52. private static readonly PrefColor tilePaletteBackgroundColor = new PrefColor("2D/Tile Palette Background"
  53. , 1.0f / 255.0f // Light
  54. , 35.0f / 255.0f
  55. , 90.0f / 255.0f
  56. , 127.0f / 255.0f
  57. , 1.0f / 255.0f // Dark
  58. , 35.0f / 255.0f
  59. , 90.0f / 255.0f
  60. , 127.0f / 255.0f);
  61. /// <summary>
  62. /// Initializes and returns an instance of TilePaletteClipboardElement.
  63. /// </summary>
  64. public TilePaletteClipboardElement()
  65. {
  66. AddToClassList(ussClassName);
  67. name = k_Name;
  68. TilePaletteOverlayUtility.SetStyleSheet(this);
  69. RegisterCallback<AttachToPanelEvent>(OnAttachedToPanel);
  70. RegisterCallback<DetachFromPanelEvent>(OnDetachFromPanel);
  71. m_ClipboardImageElement = new Image();
  72. m_ClipboardImageElement.style.backgroundColor = tilePaletteBackgroundColor.Color;
  73. m_ClipboardImageElement.focusable = true;
  74. Add(m_ClipboardImageElement);
  75. m_ErrorElement = new TilePaletteClipboardErrorElement();
  76. m_ErrorElement.style.display = DisplayStyle.None;
  77. m_ErrorElement.style.visibility = Visibility.Hidden;
  78. m_ErrorElement.SetEmptyPaletteText();
  79. Add(m_ErrorElement);
  80. m_FirstUserElement = new TilePaletteClipboardFirstUserElement();
  81. m_FirstUserElement.style.display = DisplayStyle.None;
  82. m_FirstUserElement.style.visibility = Visibility.Hidden;
  83. Add(m_FirstUserElement);
  84. ScrollView ms = new ScrollView();
  85. }
  86. private void UnlockChanged(bool unlocked)
  87. {
  88. clipboardUnlockedChanged?.Invoke(unlocked);
  89. }
  90. private void OnAttachedToPanel(AttachToPanelEvent evt)
  91. {
  92. if (EditorApplication.isPlayingOrWillChangePlaymode && !EditorApplication.isPlaying)
  93. {
  94. // Delay AttachToPanel if Editor is entering playmode
  95. EditorApplication.delayCall += AttachToPanel;
  96. }
  97. else
  98. {
  99. AttachToPanel();
  100. }
  101. }
  102. private void AttachToPanel()
  103. {
  104. if (m_TilePaletteClipboard == null)
  105. {
  106. m_TilePaletteClipboard = ScriptableObject.CreateInstance<GridPaintPaletteClipboard>();
  107. m_TilePaletteClipboard.hideFlags = HideFlags.HideAndDontSave;
  108. m_TilePaletteClipboard.unlockedChanged += UnlockChanged;
  109. m_TilePaletteClipboard.unlocked = false;
  110. m_TilePaletteClipboard.attachedVisualElement = this;
  111. var guiRect = new Rect(0, 0, layout.width, layout.height);
  112. m_TilePaletteClipboard.guiRect = guiRect;
  113. CheckPaletteState(m_TilePaletteClipboard.paletteInstance);
  114. }
  115. RegisterCallback<GeometryChangedEvent>(OnGeometryChangedEvent);
  116. RegisterCallback<ValidateCommandEvent>(OnValidateCommandEvent);
  117. RegisterCallback<ExecuteCommandEvent>(OnExecuteCommandEvent);
  118. RegisterCallback<WheelEvent>(OnWheelEvent);
  119. RegisterCallback<PointerDownEvent>(OnPointerDownEvent);
  120. RegisterCallback<PointerMoveEvent>(OnPointerMoveEvent);
  121. RegisterCallback<PointerUpEvent>(OnPointerUpEvent);
  122. RegisterCallback<PointerEnterEvent>(OnPointerEnterEvent);
  123. RegisterCallback<PointerLeaveEvent>(OnPointerLeaveEvent);
  124. m_ClipboardImageElement.RegisterCallback<KeyDownEvent>(OnKeyDownEvent);
  125. m_ClipboardImageElement.RegisterCallback<KeyUpEvent>(OnKeyUpEvent);
  126. RegisterCallback<DragEnterEvent>(OnDragEnterEvent);
  127. RegisterCallback<DragUpdatedEvent>(OnDragUpdatedEvent);
  128. RegisterCallback<DragPerformEvent>(OnDragPerformEvent);
  129. RegisterCallback<DragLeaveEvent>(OnDragLeaveEvent);
  130. RegisterCallback<DragExitedEvent>(OnDragExitedEvent);
  131. generateVisualContent += GenerateVisualContent;
  132. GridPaintingState.beforePaletteChanged += BeforePaletteChanged;
  133. GridPaintingState.paletteChanged += PaletteChanged;
  134. }
  135. private void GenerateVisualContent(MeshGenerationContext obj)
  136. {
  137. if (m_ClipboardImageElement.visible)
  138. {
  139. var texture = m_TilePaletteClipboard.RenderTexture();
  140. EditorApplication.delayCall += () =>
  141. {
  142. m_ClipboardImageElement.image = texture;
  143. };
  144. }
  145. }
  146. private void OnDetachFromPanel(DetachFromPanelEvent evt)
  147. {
  148. UnregisterCallback<GeometryChangedEvent>(OnGeometryChangedEvent);
  149. UnregisterCallback<ValidateCommandEvent>(OnValidateCommandEvent);
  150. UnregisterCallback<ExecuteCommandEvent>(OnExecuteCommandEvent);
  151. UnregisterCallback<WheelEvent>(OnWheelEvent);
  152. UnregisterCallback<PointerDownEvent>(OnPointerDownEvent);
  153. UnregisterCallback<PointerMoveEvent>(OnPointerMoveEvent);
  154. UnregisterCallback<PointerUpEvent>(OnPointerUpEvent);
  155. UnregisterCallback<PointerEnterEvent>(OnPointerEnterEvent);
  156. UnregisterCallback<PointerLeaveEvent>(OnPointerLeaveEvent);
  157. m_ClipboardImageElement.UnregisterCallback<KeyDownEvent>(OnKeyDownEvent);
  158. m_ClipboardImageElement.UnregisterCallback<KeyUpEvent>(OnKeyUpEvent);
  159. UnregisterCallback<DragEnterEvent>(OnDragEnterEvent);
  160. UnregisterCallback<DragUpdatedEvent>(OnDragUpdatedEvent);
  161. UnregisterCallback<DragPerformEvent>(OnDragPerformEvent);
  162. UnregisterCallback<DragExitedEvent>(OnDragExitedEvent);
  163. UnregisterCallback<DragLeaveEvent>(OnDragLeaveEvent);
  164. generateVisualContent -= GenerateVisualContent;
  165. if (m_TilePaletteClipboard != null)
  166. m_TilePaletteClipboard.unlockedChanged -= UnlockChanged;
  167. GridPaintingState.beforePaletteChanged -= BeforePaletteChanged;
  168. GridPaintingState.paletteChanged -= PaletteChanged;
  169. Cleanup();
  170. }
  171. private void OnGeometryChangedEvent(GeometryChangedEvent evt)
  172. {
  173. if (m_TilePaletteClipboard == null)
  174. return;
  175. var guiRect = new Rect(0, 0, layout.width, layout.height);
  176. m_TilePaletteClipboard.guiRect = guiRect;
  177. m_ClipboardImageElement.image = m_TilePaletteClipboard.RenderTexture();
  178. }
  179. private void OnExecuteCommandEvent(ExecuteCommandEvent evt)
  180. {
  181. if (m_TilePaletteClipboard == null)
  182. return;
  183. m_TilePaletteClipboard.HandleExecuteCommandEvent(evt);
  184. }
  185. private void OnValidateCommandEvent(ValidateCommandEvent evt)
  186. {
  187. if (m_TilePaletteClipboard == null)
  188. return;
  189. m_TilePaletteClipboard.HandleValidateCommandEvent(evt);
  190. }
  191. private void OnWheelEvent(WheelEvent evt)
  192. {
  193. if (m_TilePaletteClipboard == null)
  194. return;
  195. m_TilePaletteClipboard.HandleWheelEvent(evt.delta, evt.mousePosition, evt.shiftKey);
  196. evt.StopPropagation();
  197. MarkDirtyRepaint();
  198. }
  199. private void OnPointerDownEvent(PointerDownEvent evt)
  200. {
  201. if (m_TilePaletteClipboard == null)
  202. return;
  203. m_TilePaletteClipboard.HandlePointerDownEvent(evt
  204. , evt.button
  205. , evt.altKey
  206. , evt.ctrlKey
  207. , evt.localPosition);
  208. MarkDirtyRepaint();
  209. }
  210. private void OnPointerMoveEvent(PointerMoveEvent evt)
  211. {
  212. if (m_TilePaletteClipboard == null)
  213. return;
  214. m_TilePaletteClipboard.HandlePointerMoveEvent(evt
  215. , evt.button
  216. , evt.altKey
  217. , evt.localPosition
  218. , evt.deltaPosition);
  219. MarkDirtyRepaint();
  220. }
  221. private void OnPointerUpEvent(PointerUpEvent evt)
  222. {
  223. if (m_TilePaletteClipboard == null)
  224. return;
  225. if (onBrushPicked != null && m_TilePaletteClipboard != null)
  226. m_TilePaletteClipboard.onBrushPicked += onBrushPicked;
  227. m_TilePaletteClipboard.HandlePointerUpEvent(evt);
  228. if (onBrushPicked != null && m_TilePaletteClipboard != null)
  229. m_TilePaletteClipboard.onBrushPicked -= onBrushPicked;
  230. MarkDirtyRepaint();
  231. }
  232. private void OnPointerEnterEvent(PointerEnterEvent evt)
  233. {
  234. if (m_TilePaletteClipboard == null)
  235. return;
  236. m_ClipboardImageElement.Focus();
  237. m_TilePaletteClipboard.HandlePointerEnterEvent(evt);
  238. }
  239. private void OnPointerLeaveEvent(PointerLeaveEvent evt)
  240. {
  241. if (m_TilePaletteClipboard == null)
  242. return;
  243. m_TilePaletteClipboard.HandlePointerLeaveEvent(evt);
  244. }
  245. private void OnKeyDownEvent(KeyDownEvent evt)
  246. {
  247. if (m_TilePaletteClipboard == null)
  248. return;
  249. m_TilePaletteClipboard.HandleKeyDownEvent(evt);
  250. MarkDirtyRepaint();
  251. }
  252. private void OnKeyUpEvent(KeyUpEvent evt)
  253. {
  254. if (m_TilePaletteClipboard == null)
  255. return;
  256. m_TilePaletteClipboard.HandleKeyUpEvent();
  257. MarkDirtyRepaint();
  258. }
  259. private void OnDragEnterEvent(DragEnterEvent evt)
  260. {
  261. if (m_TilePaletteClipboard == null)
  262. return;
  263. m_TilePaletteClipboard.HandleDragEnterEvent(evt);
  264. CheckPaletteState(m_TilePaletteClipboard.paletteInstance);
  265. }
  266. private void OnDragUpdatedEvent(DragUpdatedEvent evt)
  267. {
  268. if (m_TilePaletteClipboard == null)
  269. return;
  270. m_TilePaletteClipboard.HandleDragUpdatedEvent(evt);
  271. MarkDirtyRepaint();
  272. }
  273. private void OnDragPerformEvent(DragPerformEvent evt)
  274. {
  275. if (m_TilePaletteClipboard == null)
  276. return;
  277. m_TilePaletteClipboard.HandleDragPerformEvent(evt);
  278. CheckPaletteState(m_TilePaletteClipboard.paletteInstance);
  279. MarkDirtyRepaint();
  280. }
  281. private void OnDragLeaveEvent(DragLeaveEvent evt)
  282. {
  283. if (m_TilePaletteClipboard == null)
  284. return;
  285. m_TilePaletteClipboard.HandleDragLeaveEvent(evt);
  286. CheckPaletteState(m_TilePaletteClipboard.paletteInstance);
  287. MarkDirtyRepaint();
  288. }
  289. private void OnDragExitedEvent(DragExitedEvent evt)
  290. {
  291. if (m_TilePaletteClipboard == null)
  292. return;
  293. m_TilePaletteClipboard.HandleDragExitedEvent(evt);
  294. MarkDirtyRepaint();
  295. }
  296. /// <summary>
  297. /// Handles cleanup for the Tile Palette Clipboard.
  298. /// </summary>
  299. private void Cleanup()
  300. {
  301. UnityEngine.Object.DestroyImmediate(m_TilePaletteClipboard);
  302. m_TilePaletteClipboard = null;
  303. }
  304. private void BeforePaletteChanged()
  305. {
  306. if (m_TilePaletteClipboard == null)
  307. return;
  308. m_TilePaletteClipboard.OnBeforePaletteSelectionChanged();
  309. }
  310. private void PaletteChanged(GameObject palette)
  311. {
  312. if (m_TilePaletteClipboard == null)
  313. return;
  314. m_TilePaletteClipboard.OnAfterPaletteSelectionChanged();
  315. CheckPaletteState(palette);
  316. }
  317. private void CheckPaletteState(GameObject palette)
  318. {
  319. if (palette == null && GridPaintingState.palettes.Count == 0)
  320. {
  321. m_ClipboardImageElement.style.display = DisplayStyle.None;
  322. m_ClipboardImageElement.style.visibility = Visibility.Hidden;
  323. m_ErrorElement.style.display = DisplayStyle.None;
  324. m_ErrorElement.style.visibility = Visibility.Hidden;
  325. m_ErrorElement.ClearText();
  326. m_FirstUserElement.style.display = DisplayStyle.Flex;
  327. m_FirstUserElement.style.visibility = Visibility.Visible;
  328. }
  329. else if (palette == null && GridPaintingState.palettes.Count > 0)
  330. {
  331. m_ClipboardImageElement.style.display = DisplayStyle.None;
  332. m_ClipboardImageElement.style.visibility = Visibility.Hidden;
  333. m_FirstUserElement.style.display = DisplayStyle.None;
  334. m_FirstUserElement.style.visibility = Visibility.Hidden;
  335. m_ErrorElement.style.display = DisplayStyle.Flex;
  336. m_ErrorElement.style.visibility = Visibility.Visible;
  337. m_ErrorElement.SetInvalidPaletteText();
  338. }
  339. else if (m_TilePaletteClipboard.activeDragAndDrop && m_TilePaletteClipboard.invalidDragAndDrop)
  340. {
  341. m_ClipboardImageElement.style.display = DisplayStyle.None;
  342. m_ClipboardImageElement.style.visibility = Visibility.Hidden;
  343. m_FirstUserElement.style.display = DisplayStyle.None;
  344. m_FirstUserElement.style.visibility = Visibility.Hidden;
  345. m_ErrorElement.style.display = DisplayStyle.Flex;
  346. m_ErrorElement.style.visibility = Visibility.Visible;
  347. m_ErrorElement.SetInvalidDragAndDropText();
  348. }
  349. else if (palette.GetComponent<Grid>() == null)
  350. {
  351. m_ClipboardImageElement.style.display = DisplayStyle.None;
  352. m_ClipboardImageElement.style.visibility = Visibility.Hidden;
  353. m_FirstUserElement.style.display = DisplayStyle.None;
  354. m_FirstUserElement.style.visibility = Visibility.Hidden;
  355. m_ErrorElement.style.display = DisplayStyle.Flex;
  356. m_ErrorElement.style.visibility = Visibility.Visible;
  357. m_ErrorElement.SetInvalidGridText();
  358. }
  359. else if (m_TilePaletteClipboard.showNewEmptyClipboardInfo)
  360. {
  361. m_ClipboardImageElement.style.display = DisplayStyle.None;
  362. m_ClipboardImageElement.style.visibility = Visibility.Hidden;
  363. m_FirstUserElement.style.display = DisplayStyle.None;
  364. m_FirstUserElement.style.visibility = Visibility.Hidden;
  365. m_ErrorElement.style.display = DisplayStyle.Flex;
  366. m_ErrorElement.style.visibility = Visibility.Visible;
  367. m_ErrorElement.SetEmptyPaletteText();
  368. }
  369. else
  370. {
  371. m_ErrorElement.style.display = DisplayStyle.None;
  372. m_ErrorElement.style.visibility = Visibility.Hidden;
  373. m_ErrorElement.ClearText();
  374. m_FirstUserElement.style.display = DisplayStyle.None;
  375. m_FirstUserElement.style.visibility = Visibility.Hidden;
  376. m_ClipboardImageElement.style.display = DisplayStyle.Flex;
  377. m_ClipboardImageElement.style.visibility = Visibility.Visible;
  378. }
  379. if (m_Window != null)
  380. m_Window.Repaint();
  381. }
  382. }
  383. }