PointSelectorPopupWindow.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using UnityEditor;
  6. using UnityEngine;
  7. namespace TheraBytes.BetterUi.Editor
  8. {
  9. public class PointSelectorPopupWindow : PopupWindowContent
  10. {
  11. const float BUTTON_SIZE = 50;
  12. public event Action<Vector2> SelctedCallback;
  13. Margin margin = new Margin(10, 5, 10, 5);
  14. Vector2 spacing = new Vector2(5, 5);
  15. public override Vector2 GetWindowSize()
  16. {
  17. float btnSize = 3 * BUTTON_SIZE;
  18. return new Vector2(
  19. margin.Horizontal + 3 * spacing.x + btnSize,
  20. margin.Vertical + 3 * spacing.y + btnSize);
  21. }
  22. public override void OnGUI(Rect rect)
  23. {
  24. float w = BUTTON_SIZE;
  25. float h = BUTTON_SIZE;
  26. Vector2 size = new Vector2(w, h);
  27. for(int x = 0; x < 3; x++)
  28. {
  29. for(int y = 0; y < 3; y++)
  30. {
  31. Vector2 pos = new Vector2(
  32. margin.Left + x * (w + spacing.x),
  33. margin.Top + y * (h + spacing.y));
  34. if(GUI.Button(new Rect(pos, size), GetContent(x, y), GetStyle(x, y)))
  35. {
  36. if(this.SelctedCallback != null)
  37. {
  38. Vector2 point = new Vector2(x / 2f, 1 - (y / 2f));
  39. SelctedCallback(point);
  40. }
  41. }
  42. }
  43. }
  44. GetStyle(1, 1); // reset to centered text
  45. }
  46. GUIContent GetContent(int x, int y)
  47. {
  48. return new GUIContent("♠");
  49. }
  50. GUIStyle GetStyle(int x, int y)
  51. {
  52. var style = GUI.skin.button;
  53. if(y == 0)
  54. {
  55. style.alignment = (x == 0)
  56. ? TextAnchor.UpperLeft
  57. : ((x == 1) ? TextAnchor.UpperCenter : TextAnchor.UpperRight);
  58. }
  59. else if(y == 1)
  60. {
  61. style.alignment = (x == 0)
  62. ? TextAnchor.MiddleLeft
  63. : ((x == 1) ? TextAnchor.MiddleCenter : TextAnchor.MiddleRight);
  64. }
  65. else if(y == 2)
  66. {
  67. style.alignment = (x == 0)
  68. ? TextAnchor.LowerLeft
  69. : ((x == 1) ? TextAnchor.LowerCenter : TextAnchor.LowerRight);
  70. }
  71. return style;
  72. }
  73. }
  74. }