MenuBase.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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.Linq;
  24. using UnityEngine;
  25. internal abstract class MenuBase : ConsoleBase
  26. {
  27. private static ShareDialogMode shareDialogMode;
  28. protected abstract void GetGui();
  29. protected virtual bool ShowDialogModeSelector()
  30. {
  31. return false;
  32. }
  33. protected virtual bool ShowBackButton()
  34. {
  35. return true;
  36. }
  37. protected void HandleResult(IResult result)
  38. {
  39. if (result == null)
  40. {
  41. this.LastResponse = "Null Response\n";
  42. LogView.AddLog(this.LastResponse);
  43. return;
  44. }
  45. this.LastResponseTexture = null;
  46. // Some platforms return the empty string instead of null.
  47. if (!string.IsNullOrEmpty(result.Error))
  48. {
  49. this.Status = "Error - Check log for details";
  50. this.LastResponse = "Error Response:\n" + result.Error;
  51. }
  52. else if (result.Cancelled)
  53. {
  54. this.Status = "Cancelled - Check log for details";
  55. this.LastResponse = "Cancelled Response:\n" + result.RawResult;
  56. }
  57. else if (!string.IsNullOrEmpty(result.RawResult))
  58. {
  59. this.Status = "Success - Check log for details";
  60. this.LastResponse = "Success Response:\n" + result.RawResult;
  61. }
  62. else
  63. {
  64. this.LastResponse = "Empty Response\n";
  65. }
  66. LogView.AddLog(result.ToString());
  67. }
  68. protected void OnGUI()
  69. {
  70. if (this.IsHorizontalLayout())
  71. {
  72. GUILayout.BeginHorizontal();
  73. GUILayout.BeginVertical();
  74. }
  75. GUILayout.Label(this.GetType().Name, this.LabelStyle);
  76. this.AddStatus();
  77. #if UNITY_IOS || UNITY_ANDROID || UNITY_WP8
  78. if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
  79. {
  80. Vector2 scrollPosition = this.ScrollPosition;
  81. scrollPosition.y += Input.GetTouch(0).deltaPosition.y;
  82. this.ScrollPosition = scrollPosition;
  83. }
  84. #endif
  85. this.ScrollPosition = GUILayout.BeginScrollView(
  86. this.ScrollPosition,
  87. GUILayout.MinWidth(ConsoleBase.MainWindowFullWidth));
  88. GUILayout.BeginHorizontal();
  89. if (this.ShowBackButton())
  90. {
  91. this.AddBackButton();
  92. }
  93. this.AddLogButton();
  94. if (this.ShowBackButton())
  95. {
  96. // Fix GUILayout margin issues
  97. GUILayout.Label(GUIContent.none, GUILayout.MinWidth(ConsoleBase.MarginFix));
  98. }
  99. GUILayout.EndHorizontal();
  100. if (this.ShowDialogModeSelector())
  101. {
  102. this.AddDialogModeButtons();
  103. }
  104. GUILayout.BeginVertical();
  105. // Add the ui from decendants
  106. this.GetGui();
  107. GUILayout.Space(10);
  108. GUILayout.EndVertical();
  109. GUILayout.EndScrollView();
  110. }
  111. private void AddStatus()
  112. {
  113. GUILayout.Space(5);
  114. GUILayout.Box("Status: " + this.Status, this.TextStyle, GUILayout.MinWidth(ConsoleBase.MainWindowWidth));
  115. }
  116. private void AddBackButton()
  117. {
  118. GUI.enabled = ConsoleBase.MenuStack.Any();
  119. if (this.Button("Back"))
  120. {
  121. this.GoBack();
  122. }
  123. GUI.enabled = true;
  124. }
  125. private void AddLogButton()
  126. {
  127. if (this.Button("Log"))
  128. {
  129. this.SwitchMenu(typeof(LogView));
  130. }
  131. }
  132. private void AddDialogModeButtons()
  133. {
  134. GUILayout.BeginHorizontal();
  135. foreach (var value in Enum.GetValues(typeof(ShareDialogMode)))
  136. {
  137. this.AddDialogModeButton((ShareDialogMode)value);
  138. }
  139. GUILayout.EndHorizontal();
  140. }
  141. private void AddDialogModeButton(ShareDialogMode mode)
  142. {
  143. bool enabled = GUI.enabled;
  144. GUI.enabled = enabled && (mode != shareDialogMode);
  145. if (this.Button(mode.ToString()))
  146. {
  147. shareDialogMode = mode;
  148. FB.Mobile.ShareDialogMode = mode;
  149. }
  150. GUI.enabled = enabled;
  151. }
  152. }
  153. }