MenuUtilities.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.UIElements;
  4. namespace Unity.Cloud.Collaborate.Utilities
  5. {
  6. static class MenuUtilities
  7. {
  8. /// <summary>
  9. /// Corner of the anchor element the dialogue should anchor to.
  10. /// </summary>
  11. public enum AnchorPoint
  12. {
  13. TopLeft,
  14. TopRight,
  15. BottomLeft,
  16. BottomRight
  17. }
  18. /// <summary>
  19. /// Direction the dialogue should open from its anchor.
  20. /// </summary>
  21. public enum OpenDirection
  22. {
  23. UpLeft,
  24. UpRight,
  25. DownLeft,
  26. DownRight
  27. }
  28. /// <summary>
  29. /// Given an element and an anchor point, calculate the world coords to draw a menu at.
  30. /// </summary>
  31. /// <param name="e">Element to start at.</param>
  32. /// <param name="anchorPoint">Corner of the element to calculate.</param>
  33. /// <returns>World coordinates from the given values.</returns>
  34. public static (float X, float Y) GetMenuPosition(VisualElement e, AnchorPoint anchorPoint)
  35. {
  36. // Calculate position of the start corner.
  37. (float x, float y) anchorCoords;
  38. switch (anchorPoint)
  39. {
  40. case AnchorPoint.TopLeft:
  41. anchorCoords = (e.worldBound.xMin, e.worldBound.yMin);
  42. break;
  43. case AnchorPoint.TopRight:
  44. anchorCoords = (e.worldBound.xMax, e.worldBound.yMin);
  45. break;
  46. case AnchorPoint.BottomLeft:
  47. anchorCoords = (e.worldBound.xMin, e.worldBound.yMax);
  48. break;
  49. case AnchorPoint.BottomRight:
  50. anchorCoords = (e.worldBound.xMax, e.worldBound.yMax);
  51. break;
  52. default:
  53. throw new ArgumentOutOfRangeException(nameof(anchorPoint), anchorPoint, null);
  54. }
  55. return anchorCoords;
  56. }
  57. }
  58. }