DialogHandler.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace ZenFulcrum.EmbeddedBrowser {
  5. /**
  6. * Helper for browser dialog boxes, like alert(). You don't need to use this directly, it will
  7. * automatically be added where it's needed.
  8. */
  9. [RequireComponent(typeof(Browser))]
  10. public class DialogHandler : MonoBehaviour {
  11. protected static string dialogPage;
  12. public delegate void DialogCallback(bool affirm, string text1, string text2);
  13. public delegate void MenuCallback(int commandId);
  14. public static DialogHandler Create(Browser parent, DialogCallback dialogCallback, MenuCallback contextCallback) {
  15. if (dialogPage == null) {
  16. dialogPage = Resources.Load<TextAsset>("Browser/Dialogs").text;
  17. }
  18. var go = new GameObject("Browser Dialog for " + parent.name);
  19. var handler = go.AddComponent<DialogHandler>();
  20. handler.parentBrowser = parent;
  21. handler.dialogCallback = dialogCallback;
  22. var db = handler.dialogBrowser = handler.GetComponent<Browser>();
  23. db.UIHandler = parent.UIHandler;
  24. db.EnableRendering = false;
  25. db.EnableInput = false;
  26. db.allowContextMenuOn = BrowserNative.ContextMenuOrigin.Editable;
  27. //Use the parent texture. Except, we don't actually use it. So
  28. //mostly we just mimic the size and don't consume more texture memory.
  29. db.Resize(parent.Texture);
  30. db.LoadHTML(dialogPage, "zfb://dialog");
  31. db.UIHandler = parent.UIHandler;
  32. db.RegisterFunction("reportDialogResult", args => {
  33. dialogCallback(args[0], args[1], args[2]);
  34. handler.Hide();
  35. });
  36. db.RegisterFunction("reportContextMenuResult", args => {
  37. contextCallback(args[0]);
  38. handler.Hide();
  39. });
  40. return handler;
  41. }
  42. protected Browser parentBrowser;
  43. protected Browser dialogBrowser;
  44. protected DialogCallback dialogCallback;
  45. protected MenuCallback contextCallback;
  46. public void HandleDialog(BrowserNative.DialogType type, string text, string promptDefault = null) {
  47. if (type == BrowserNative.DialogType.DLT_HIDE) {
  48. Hide();
  49. return;
  50. }
  51. Show();
  52. //Debug.Log("HandleDialog " + type + " text " + text + " prompt " + promptDefault);
  53. switch (type) {
  54. case BrowserNative.DialogType.DLT_ALERT:
  55. dialogBrowser.CallFunction("showAlert", text);
  56. break;
  57. case BrowserNative.DialogType.DLT_CONFIRM:
  58. dialogBrowser.CallFunction("showConfirm", text);
  59. break;
  60. case BrowserNative.DialogType.DLT_PROMPT:
  61. dialogBrowser.CallFunction("showPrompt", text, promptDefault);
  62. break;
  63. case BrowserNative.DialogType.DLT_PAGE_UNLOAD:
  64. dialogBrowser.CallFunction("showConfirmNav", text);
  65. break;
  66. case BrowserNative.DialogType.DLT_PAGE_RELOAD:
  67. dialogBrowser.CallFunction("showConfirmReload", text);
  68. break;
  69. case BrowserNative.DialogType.DLT_GET_AUTH:
  70. dialogBrowser.CallFunction("showAuthPrompt", text);
  71. break;
  72. default:
  73. throw new ArgumentOutOfRangeException("type", type, null);
  74. }
  75. }
  76. public void Show() {
  77. parentBrowser.SetOverlay(dialogBrowser);
  78. parentBrowser.EnableInput = false;
  79. dialogBrowser.EnableInput = true;
  80. dialogBrowser.UpdateCursor();
  81. }
  82. public void Hide() {
  83. parentBrowser.SetOverlay(null);
  84. parentBrowser.EnableInput = true;
  85. dialogBrowser.EnableInput = false;
  86. parentBrowser.UpdateCursor();
  87. if (dialogBrowser.IsLoaded) dialogBrowser.CallFunction("reset");
  88. }
  89. public void HandleContextMenu(string menuJSON, int x, int y) {
  90. if (menuJSON == null) {
  91. Hide();
  92. return;
  93. }
  94. Show();
  95. dialogBrowser.CallFunction("showContextMenu", menuJSON, x, y);
  96. }
  97. }
  98. }