ConsoleBase.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /**
  2. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  3. *
  4. * You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
  5. * copy, modify, and distribute this software in source code or binary form for use
  6. * in connection with the web services and APIs provided by Facebook.
  7. *
  8. * As with any software that integrates with the Facebook platform, your use of
  9. * this software is subject to the Facebook Developer Principles and Policies
  10. * [http://developers.facebook.com/policy/]. This copyright notice shall be
  11. * included in all copies or substantial portions of the software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  15. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  16. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  17. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  18. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. */
  20. namespace Facebook.Unity.Example
  21. {
  22. using System;
  23. using System.Collections.Generic;
  24. using System.Linq;
  25. using UnityEngine;
  26. using UnityEngine.SceneManagement;
  27. internal class ConsoleBase : MonoBehaviour
  28. {
  29. private const int DpiScalingFactor = 160;
  30. private static Stack<string> menuStack = new Stack<string>();
  31. private string status = "Ready";
  32. private string lastResponse = string.Empty;
  33. private Vector2 scrollPosition = Vector2.zero;
  34. // DPI scaling
  35. private float? scaleFactor;
  36. private GUIStyle textStyle;
  37. private GUIStyle buttonStyle;
  38. private GUIStyle textInputStyle;
  39. private GUIStyle labelStyle;
  40. protected static int ButtonHeight
  41. {
  42. get
  43. {
  44. return Constants.IsMobile ? 60 : 24;
  45. }
  46. }
  47. protected static int MainWindowWidth
  48. {
  49. get
  50. {
  51. return Constants.IsMobile ? Screen.width - 30 : 700;
  52. }
  53. }
  54. protected static int MainWindowFullWidth
  55. {
  56. get
  57. {
  58. return Constants.IsMobile ? Screen.width : 760;
  59. }
  60. }
  61. protected static int MarginFix
  62. {
  63. get
  64. {
  65. return Constants.IsMobile ? 0 : 48;
  66. }
  67. }
  68. protected static Stack<string> MenuStack
  69. {
  70. get
  71. {
  72. return ConsoleBase.menuStack;
  73. }
  74. set
  75. {
  76. ConsoleBase.menuStack = value;
  77. }
  78. }
  79. protected string Status
  80. {
  81. get
  82. {
  83. return this.status;
  84. }
  85. set
  86. {
  87. this.status = value;
  88. }
  89. }
  90. protected Texture2D LastResponseTexture { get; set; }
  91. protected string LastResponse
  92. {
  93. get
  94. {
  95. return this.lastResponse;
  96. }
  97. set
  98. {
  99. this.lastResponse = value;
  100. }
  101. }
  102. protected Vector2 ScrollPosition
  103. {
  104. get
  105. {
  106. return this.scrollPosition;
  107. }
  108. set
  109. {
  110. this.scrollPosition = value;
  111. }
  112. }
  113. // Note we assume that these styles will be accessed from OnGUI otherwise the
  114. // unity APIs will fail.
  115. protected float ScaleFactor
  116. {
  117. get
  118. {
  119. if (!this.scaleFactor.HasValue)
  120. {
  121. this.scaleFactor = Screen.dpi / ConsoleBase.DpiScalingFactor;
  122. }
  123. return this.scaleFactor.Value;
  124. }
  125. }
  126. protected int FontSize
  127. {
  128. get
  129. {
  130. return (int)Math.Round(this.ScaleFactor * 16);
  131. }
  132. }
  133. protected GUIStyle TextStyle
  134. {
  135. get
  136. {
  137. if (this.textStyle == null)
  138. {
  139. this.textStyle = new GUIStyle(GUI.skin.textArea);
  140. this.textStyle.alignment = TextAnchor.UpperLeft;
  141. this.textStyle.wordWrap = true;
  142. this.textStyle.padding = new RectOffset(10, 10, 10, 10);
  143. this.textStyle.stretchHeight = true;
  144. this.textStyle.stretchWidth = false;
  145. this.textStyle.fontSize = this.FontSize;
  146. }
  147. return this.textStyle;
  148. }
  149. }
  150. protected GUIStyle ButtonStyle
  151. {
  152. get
  153. {
  154. if (this.buttonStyle == null)
  155. {
  156. this.buttonStyle = new GUIStyle(GUI.skin.button);
  157. this.buttonStyle.fontSize = this.FontSize;
  158. }
  159. return this.buttonStyle;
  160. }
  161. }
  162. protected GUIStyle TextInputStyle
  163. {
  164. get
  165. {
  166. if (this.textInputStyle == null)
  167. {
  168. this.textInputStyle = new GUIStyle(GUI.skin.textField);
  169. this.textInputStyle.fontSize = this.FontSize;
  170. }
  171. return this.textInputStyle;
  172. }
  173. }
  174. protected GUIStyle LabelStyle
  175. {
  176. get
  177. {
  178. if (this.labelStyle == null)
  179. {
  180. this.labelStyle = new GUIStyle(GUI.skin.label);
  181. this.labelStyle.fontSize = this.FontSize;
  182. }
  183. return this.labelStyle;
  184. }
  185. }
  186. protected virtual void Awake()
  187. {
  188. // Limit the framerate to 60 to keep device from burning through cpu
  189. Application.targetFrameRate = 60;
  190. }
  191. protected bool Button(string label)
  192. {
  193. return GUILayout.Button(
  194. label,
  195. this.ButtonStyle,
  196. GUILayout.MinHeight(ConsoleBase.ButtonHeight * this.ScaleFactor),
  197. GUILayout.MaxWidth(ConsoleBase.MainWindowWidth));
  198. }
  199. protected void LabelAndTextField(string label, ref string text)
  200. {
  201. GUILayout.BeginHorizontal();
  202. GUILayout.Label(label, this.LabelStyle, GUILayout.MaxWidth(200 * this.ScaleFactor));
  203. text = GUILayout.TextField(
  204. text,
  205. this.TextInputStyle,
  206. GUILayout.MaxWidth(ConsoleBase.MainWindowWidth - 150));
  207. GUILayout.EndHorizontal();
  208. }
  209. protected bool IsHorizontalLayout()
  210. {
  211. #if UNITY_IOS || UNITY_ANDROID
  212. return Screen.orientation == ScreenOrientation.Landscape;
  213. #else
  214. return true;
  215. #endif
  216. }
  217. protected void SwitchMenu(Type menuClass)
  218. {
  219. ConsoleBase.menuStack.Push(this.GetType().Name);
  220. SceneManager.LoadScene(menuClass.Name);
  221. }
  222. protected void GoBack()
  223. {
  224. if (ConsoleBase.menuStack.Any())
  225. {
  226. SceneManager.LoadScene(ConsoleBase.menuStack.Pop());
  227. }
  228. }
  229. }
  230. }