SetNameOrDeleteOptimizedScreen.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 SetNameOrDeleteOptimizedScreen : PopupWindowContent
  10. {
  11. const float MARGIN = 10;
  12. bool deleteConfirm = false;
  13. bool renameMode;
  14. ScreenTypeConditions condition;
  15. string cachedName;
  16. public event Action CloseCallback;
  17. public SetNameOrDeleteOptimizedScreen(ScreenTypeConditions condition, Action closeCallback)
  18. {
  19. this.renameMode = condition != null;
  20. this.condition = condition;
  21. this.cachedName = (renameMode) ? condition.Name : "";
  22. this.CloseCallback = closeCallback;
  23. }
  24. public override Vector2 GetWindowSize()
  25. {
  26. return new Vector2(400, (renameMode) ? 300 : 175);
  27. }
  28. public override void OnGUI(Rect rect)
  29. {
  30. Rect inner = new Rect(rect.x + MARGIN, rect.y + MARGIN, rect.width - 2 * MARGIN, rect.height - 2 * MARGIN);
  31. float y = inner.y;
  32. float h = 20;
  33. EditorGUI.LabelField(new Rect(inner.x, y, inner.width - 15, h),
  34. (renameMode) ? "Rename or Delete" : "Create", EditorStyles.boldLabel);
  35. if(GUI.Button(new Rect(inner.x + inner.width - h, y, h, h), "X"))
  36. {
  37. if (CloseCallback != null)
  38. CloseCallback();
  39. }
  40. y += h;
  41. if (renameMode)
  42. {
  43. h = 100;
  44. EditorGUI.LabelField(new Rect(inner.x, y, inner.width, h),
  45. @"Attention!
  46. Settings for certain screen conditions are mapped by name.
  47. If you change the name, your already configured UI Elements
  48. may not look as expected.
  49. You should only rename if there are no UI Elements connected
  50. to this configuration yet or if you are willing
  51. to adjust it everywhere.");
  52. y += h;
  53. h = 16;
  54. cachedName = EditorGUI.TextField(new Rect(inner.x, y, inner.width, h), "New Name", cachedName);
  55. y += h + 10;
  56. h = 20;
  57. if (CheckNameValidity())
  58. {
  59. if (GUI.Button(new Rect(inner.x, y, inner.width, h), "Rename"))
  60. {
  61. condition.Name = cachedName;
  62. if (CloseCallback != null)
  63. CloseCallback();
  64. }
  65. }
  66. else
  67. {
  68. EditorGUI.LabelField(new Rect(inner.x, y, inner.width, h), "Name is not valid", EditorStyles.helpBox);
  69. }
  70. y += h + 20;
  71. if (deleteConfirm)
  72. {
  73. h = 40;
  74. EditorGUI.LabelField(new Rect(inner.x, y, inner.width, h),
  75. string.Format(@"Are you sure you want to delete the screen condition
  76. '{0}'?.", condition.Name));
  77. y += h;
  78. h = 20;
  79. if (GUI.Button(new Rect(inner.x, y, 0.5f * inner.width - 4, h), "Yes"))
  80. {
  81. ResolutionMonitor.Instance.OptimizedScreens.Remove(condition);
  82. if (CloseCallback != null)
  83. CloseCallback();
  84. }
  85. if (GUI.Button(new Rect(inner.x + 0.5f * inner.width + 4, y, 0.5f * inner.width - 4, h), "No"))
  86. {
  87. deleteConfirm = false;
  88. }
  89. }
  90. else
  91. {
  92. h = 60;
  93. EditorGUI.LabelField(new Rect(inner.x, y, inner.width, h),
  94. @"You May also delete this screen condition
  95. but the mapping to its name will stay connected
  96. to the controls which references it.");
  97. y += h;
  98. h = 20;
  99. if (GUI.Button(new Rect(inner.x, y, inner.width, h), string.Format("Delete '{0}'", condition.Name)))
  100. {
  101. deleteConfirm = true;
  102. }
  103. }
  104. }
  105. else // CREATE MODE
  106. {
  107. h = 80;
  108. EditorGUI.LabelField(new Rect(inner.x, y, inner.width, h),
  109. @"Give your new Condition a short and clear name.
  110. Choose the name wisely. It identifies your condition.
  111. When it is in use in the project you should not change it anymore.");
  112. y += h;
  113. h = 16;
  114. cachedName = EditorGUI.TextField(new Rect(inner.x, y, inner.width, h), "Name", cachedName);
  115. y += h + 10;
  116. h = 20;
  117. if (CheckNameValidity())
  118. {
  119. if (GUI.Button(new Rect(inner.x, y, inner.width, h), "Create"))
  120. {
  121. condition = new ScreenTypeConditions(cachedName);
  122. ResolutionMonitor.Instance.OptimizedScreens.Add(condition);
  123. if (CloseCallback != null)
  124. CloseCallback();
  125. }
  126. }
  127. else
  128. {
  129. EditorGUI.LabelField(new Rect(inner.x, y, inner.width, h), "Name is not valid", EditorStyles.helpBox);
  130. }
  131. }
  132. }
  133. bool CheckNameValidity()
  134. {
  135. return !(string.IsNullOrEmpty(cachedName))
  136. && (ResolutionMonitor.Instance.OptimizedScreens.FirstOrDefault(o => o != condition && o.Name == cachedName) == null);
  137. }
  138. }
  139. }