ResolutionMonitorPage.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 ResolutionMonitorPage : WizardPage
  10. {
  11. const int SMALL_SCREEN = 1 << 0;
  12. const int BIG_SCREEN = 1 << 1;
  13. ValueWizardPageElement<Vector2> optimizedResolutionElement;
  14. ValueWizardPageElement<bool> isFallbackLandscapeElement;
  15. ValueWizardPageElement<bool> supportBothOrientationsElement;
  16. ValueWizardPageElement<int> responsiveDesignElement;
  17. public override string NameId { get { return "ResolutionMonitorPage"; } }
  18. public ResolutionMonitorPage(IWizard wizard)
  19. : base(wizard)
  20. {
  21. }
  22. protected override void OnInitialize()
  23. {
  24. Add(new InfoWizardPageElement("Resolution Monitor Setup", InfoType.Header));
  25. Add(new InfoWizardPageElement(
  26. "The Resolution Monitor is responsible for applying the correct sizes to your Better UI elements as well as detecting screen configurations.\n" +
  27. "This wizard page will help you setting it up correctly",
  28. InfoType.InfoBox));
  29. // optimized resolution
  30. Add(new SeparatorWizardPageElement());
  31. Add(new InfoWizardPageElement("♠ Which is the resolution your game is optimized for?"));
  32. Add(new InfoWizardPageElement("The 'Optimized Resolution' is used as reference for resizing UI variables (like font size).\n" +
  33. "If the artists also create their UI graphics for that resolution it will be easier to make the UI looking right.",
  34. InfoType.InfoBox));
  35. optimizedResolutionElement = new ValueWizardPageElement<Vector2>("optimizedResolution",
  36. (o, v) =>
  37. {
  38. if (v == Vector2.zero)
  39. {
  40. v = new Vector2(1920, 1080);
  41. }
  42. EditorGUILayout.BeginHorizontal();
  43. EditorGUILayout.PrefixLabel("Optimized Resolution");
  44. v = EditorGUILayout.Vector2Field("", v);
  45. if (GUILayout.Button("Full HD")) v = new Vector2(1920, 1080);
  46. if (GUILayout.Button("UHD-4k")) v = new Vector2(3840, 2160);
  47. EditorGUILayout.EndHorizontal();
  48. DrawContinueButton(o);
  49. return v;
  50. });
  51. Add(optimizedResolutionElement);
  52. // main orientation
  53. Add(new SeparatorWizardPageElement());
  54. Add(new InfoWizardPageElement("♠ Which is the orientation your game is played (or played mainly)?"));
  55. isFallbackLandscapeElement = new ValueWizardPageElement<bool>("isFallbackLandscape",
  56. (o, v) =>
  57. {
  58. EditorGUILayout.BeginHorizontal();
  59. v = GUILayout.Toggle(v, "Landscape", EditorStyles.miniButtonLeft);
  60. v = !GUILayout.Toggle(!v, "Portrait", EditorStyles.miniButtonRight);
  61. EditorGUILayout.EndHorizontal();
  62. DrawContinueButton(o);
  63. return v;
  64. });
  65. Add(isFallbackLandscapeElement);
  66. // both orientations
  67. Add(new SeparatorWizardPageElement());
  68. Add(new InfoWizardPageElement("♠ Do you support both, Landscape and Portrait orientation?"));
  69. supportBothOrientationsElement = new ValueWizardPageElement<bool>("supportBothOrientations",
  70. (o, v) =>
  71. {
  72. EditorGUILayout.BeginHorizontal();
  73. v = GUILayout.Toggle(v, "Yes", EditorStyles.miniButtonLeft);
  74. v = !GUILayout.Toggle(!v, "No", EditorStyles.miniButtonRight);
  75. EditorGUILayout.EndHorizontal();
  76. DrawContinueButton(o);
  77. return v;
  78. });
  79. Add(supportBothOrientationsElement);
  80. // Responsive Design
  81. Add(new SeparatorWizardPageElement());
  82. Add(new InfoWizardPageElement("♠ Do you support Responsive Design for different screen sizes?"));
  83. Add(new InfoWizardPageElement("With responsive design you can place certain elements at different locations or hide them " +
  84. "depending on the screen size / orientation (or other parameters).\n" +
  85. "You can also configure other variables differently for different screens (e.g. controlling the sizes of layout parameters differently)",
  86. InfoType.InfoBox));
  87. responsiveDesignElement = new ValueWizardPageElement<int>("responsiveDesign",
  88. (o, v) =>
  89. {
  90. bool containsSmall = (v & SMALL_SCREEN) == SMALL_SCREEN;
  91. bool containsBig = (v & BIG_SCREEN) == BIG_SCREEN;
  92. EditorGUILayout.LabelField("In addition to the default screen size, support differnt layouts for ... (multi-select allowed)");
  93. EditorGUILayout.BeginHorizontal();
  94. containsSmall = GUILayout.Toggle(containsSmall, "Smaller Screens", EditorStyles.miniButtonLeft);
  95. containsBig = GUILayout.Toggle(containsBig, "Larger Screens", EditorStyles.miniButtonRight);
  96. EditorGUILayout.EndHorizontal();
  97. v = (containsSmall ? SMALL_SCREEN : 0) | (containsBig ? BIG_SCREEN : 0);
  98. DrawContinueButton(o);
  99. return v;
  100. });
  101. Add(responsiveDesignElement);
  102. // Generate!
  103. Add(new SeparatorWizardPageElement());
  104. Add(new InfoWizardPageElement("Generate Resolution Monitor", InfoType.Header));
  105. Add(new InfoWizardPageElement("When you feel comfortable with the settings you made, go on and click the button below."));
  106. var generateElement = new CustomWizardPageElement((o) =>
  107. {
  108. string suffix = (ResolutionMonitor.ScriptableObjectFileExists) ? " (overwrite existing)" : "";
  109. if (GUILayout.Button("Generate Resolution Monitor!" + suffix, GUILayout.Height(50)))
  110. {
  111. if (!ResolutionMonitor.ScriptableObjectFileExists
  112. || EditorUtility.DisplayDialog("Are you sure?",
  113. "Overwriting the current Resolution Monitor settings could break your UI! (some screen configuration references may get lost)",
  114. "Overwrite!", "Cancel"))
  115. {
  116. GenerateResolutionMonitor();
  117. o.MarkComplete();
  118. }
  119. }
  120. });
  121. Add(generateElement);
  122. if(ResolutionMonitor.ScriptableObjectFileExists)
  123. {
  124. generateElement.MarkComplete();
  125. }
  126. else
  127. {
  128. // display only if resolution monitor was not present before.
  129. Add(new InfoWizardPageElement("Resolution Monitor has been generated."));
  130. }
  131. // Extra info
  132. Add(new SeparatorWizardPageElement());
  133. Add(new CustomWizardPageElement((o) =>
  134. {
  135. EditorGUILayout.HelpBox("You have configured your Resolution Monitor. " +
  136. "You can even add more configuratons and also trigger your own layouts via code. " +
  137. "This would require manual configuration of the Resolution Monitor and some deeper knowledge of the system.", MessageType.Info);
  138. EditorGUILayout.BeginHorizontal();
  139. if(GUILayout.Button("Open Documentation"))
  140. {
  141. Application.OpenURL("https://documentation.therabytes.de/better-ui/ScreenConfigurations.html");
  142. }
  143. if (GUILayout.Button("Edit Resolution Monitor"))
  144. {
  145. Selection.activeObject = ResolutionMonitor.Instance;
  146. }
  147. EditorGUILayout.EndHorizontal();
  148. }).MarkComplete());
  149. }
  150. private void GenerateResolutionMonitor()
  151. {
  152. ResolutionMonitor.EnsureInstance();
  153. var rm = ResolutionMonitor.Instance;
  154. Vector2 lRes = optimizedResolutionElement.Value;
  155. if(lRes.x < lRes.y)
  156. {
  157. lRes = new Vector2(lRes.y, lRes.x);
  158. }
  159. Vector2 pRes = new Vector2(lRes.y, lRes.x);
  160. // fallback
  161. bool isLandscape = isFallbackLandscapeElement.Value;
  162. if (isLandscape)
  163. {
  164. rm.FallbackName = "Landscape";
  165. rm.SetOptimizedResolutionFallback(lRes);
  166. }
  167. else
  168. {
  169. rm.FallbackName = "Portrait";
  170. rm.SetOptimizedResolutionFallback(pRes);
  171. }
  172. rm.OptimizedScreens.Clear();
  173. // both orientations
  174. ScreenTypeConditions alt = null;
  175. bool supportBoth = supportBothOrientationsElement.Value;
  176. if (supportBoth)
  177. {
  178. alt = CreateScreenTypeCondition(!isLandscape, "", lRes, pRes);
  179. // added later
  180. }
  181. // Responsive Design
  182. bool containsSmall = (responsiveDesignElement.Value & SMALL_SCREEN) == SMALL_SCREEN;
  183. bool containsBig = (responsiveDesignElement.Value & BIG_SCREEN) == BIG_SCREEN;
  184. var smallChecker = new IsScreenOfCertainSize(0, IsScreenOfCertainSize.DEFAULT_SMALL_THRESHOLD);
  185. var bigChecker = new IsScreenOfCertainSize(IsScreenOfCertainSize.DEFAULT_LARGE_THRESHOLD, 9999999);
  186. ScreenTypeConditions smallMain = null;
  187. ScreenTypeConditions smallAlt = null;
  188. ScreenTypeConditions bigMain = null;
  189. ScreenTypeConditions bigAlt = null;
  190. if (supportBoth)
  191. {
  192. if (containsSmall)
  193. {
  194. smallAlt = CreateScreenTypeCondition(!isLandscape, " Small", lRes, pRes, smallChecker);
  195. smallAlt.Fallbacks.Add(alt.Name);
  196. alt.Fallbacks.Add(smallAlt.Name);
  197. rm.OptimizedScreens.Add(smallAlt);
  198. }
  199. if(containsBig)
  200. {
  201. bigAlt = CreateScreenTypeCondition(!isLandscape, " Large", lRes, pRes, bigChecker);
  202. bigAlt.Fallbacks.Add(alt.Name);
  203. alt.Fallbacks.Add(bigAlt.Name);
  204. if (containsSmall)
  205. {
  206. bigAlt.Fallbacks.Add(smallAlt.Name);
  207. smallAlt.Fallbacks.Add(bigAlt.Name);
  208. }
  209. rm.OptimizedScreens.Add(bigAlt);
  210. }
  211. rm.OptimizedScreens.Add(alt);
  212. }
  213. if (containsSmall)
  214. {
  215. smallMain = CreateScreenTypeCondition(isLandscape, " Small", lRes, pRes, smallChecker);
  216. rm.OptimizedScreens.Add(smallMain);
  217. if(smallAlt != null)
  218. {
  219. smallAlt.Fallbacks.Add(smallMain.Name);
  220. }
  221. }
  222. if (containsBig)
  223. {
  224. bigMain = CreateScreenTypeCondition(isLandscape, " Large", lRes, pRes, bigChecker);
  225. rm.OptimizedScreens.Add(bigMain);
  226. if(bigAlt != null)
  227. {
  228. bigAlt.Fallbacks.Add(bigMain.Name);
  229. }
  230. }
  231. Debug.Log("Resolution Monitor has been generated successfully.");
  232. }
  233. private ScreenTypeConditions CreateScreenTypeCondition(bool isLandscape, string nameSuffix, Vector2 lRes, Vector2 pRes,
  234. IsScreenOfCertainSize sizeInfo = null)
  235. {
  236. string name = (isLandscape)
  237. ? "Landscape" : "Portrait";
  238. name += nameSuffix;
  239. var sct = new ScreenTypeConditions(name, typeof(IsCertainScreenOrientation));
  240. sct.OptimizedScreenInfo.Resolution = (isLandscape)
  241. ? lRes : pRes;
  242. sct.CheckOrientation.ExpectedOrientation = (isLandscape)
  243. ? IsCertainScreenOrientation.Orientation.Landscape
  244. : IsCertainScreenOrientation.Orientation.Portrait;
  245. sct.CheckScreenSize.IsActive = sizeInfo != null;
  246. if(sizeInfo != null)
  247. {
  248. sct.CheckScreenSize.Units = sizeInfo.Units;
  249. sct.CheckScreenSize.MeasureType = sizeInfo.MeasureType;
  250. sct.CheckScreenSize.MinSize = sizeInfo.MinSize;
  251. sct.CheckScreenSize.MaxSize = sizeInfo.MaxSize;
  252. }
  253. return sct;
  254. }
  255. private static void DrawContinueButton(WizardPageElementBase o)
  256. {
  257. if (o.State == WizardElementState.WaitForInput && GUILayout.Button("Continue..."))
  258. {
  259. o.MarkComplete();
  260. }
  261. }
  262. }
  263. }