PointerUIGUI.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. using UnityEngine.UI;
  6. namespace ZenFulcrum.EmbeddedBrowser {
  7. /** Attach this script to a GUI Image to use a browser on it. */
  8. [RequireComponent(typeof(RawImage))]
  9. public class PointerUIGUI :
  10. PointerUIBase,
  11. IBrowserUI,
  12. ISelectHandler, IDeselectHandler,
  13. IPointerEnterHandler, IPointerExitHandler,
  14. IPointerDownHandler
  15. {
  16. protected RawImage myImage;
  17. public bool enableInput = true;
  18. public bool automaticResize = true;
  19. public override void Awake() {
  20. base.Awake();
  21. myImage = GetComponent<RawImage>();
  22. browser.afterResize += UpdateTexture;
  23. // BrowserCursor.cursorChange += () => {
  24. // SetCursor(BrowserCursor);
  25. // };
  26. rTransform = GetComponent<RectTransform>();
  27. }
  28. protected void OnEnable() {
  29. if(automaticResize) StartCoroutine(WatchResize());
  30. }
  31. /** Automatically resizes the browser to match the size of this object. */
  32. private IEnumerator WatchResize() {
  33. Rect currentSize = new Rect();
  34. while (enabled) {
  35. var rect = rTransform.rect;
  36. if (rect.size.x <= 0 || rect.size.y <= 0) rect.size = new Vector2(512, 512);
  37. if (rect.size != currentSize.size) {
  38. browser.Resize((int)rect.size.x, (int)rect.size.y);
  39. currentSize = rect;
  40. }
  41. yield return null;
  42. }
  43. }
  44. protected void UpdateTexture(Texture2D texture) {
  45. myImage.texture = texture;
  46. myImage.uvRect = new Rect(0, 0, 1, 1);
  47. }
  48. protected BaseRaycaster raycaster;
  49. protected RectTransform rTransform;
  50. // protected List<RaycastResult> raycastResults = new List<RaycastResult>();
  51. protected override Vector2 MapPointerToBrowser(Vector2 screenPosition, int pointerId) {
  52. if (!raycaster) raycaster = GetComponentInParent<BaseRaycaster>();
  53. Vector2 pos;
  54. RectTransformUtility.ScreenPointToLocalPointInRectangle(
  55. (RectTransform)transform, screenPosition, raycaster.eventCamera, out pos
  56. );
  57. pos.x = pos.x / rTransform.rect.width + rTransform.pivot.x;
  58. pos.y = pos.y / rTransform.rect.height + rTransform.pivot.y;
  59. return pos;
  60. }
  61. protected override Vector2 MapRayToBrowser(Ray worldRay, int pointerId) {
  62. // world-space GUI
  63. return new Vector2(float.NaN, float.NaN);
  64. }
  65. public override void GetCurrentHitLocation(out Vector3 pos, out Quaternion rot) {
  66. //world space GUI
  67. pos = new Vector3(float.NaN, float.NaN, float.NaN);
  68. rot = Quaternion.identity;
  69. }
  70. protected bool _mouseHasFocus;
  71. public override bool MouseHasFocus {
  72. get { return _mouseHasFocus && enableInput; }
  73. protected set { _mouseHasFocus = value; }
  74. }
  75. protected bool _keyboardHasFocus;
  76. public override bool KeyboardHasFocus { get { return _keyboardHasFocus && enableInput; } }
  77. public void OnSelect(BaseEventData eventData) {
  78. _keyboardHasFocus = true;
  79. Input.imeCompositionMode = IMECompositionMode.Off;//CEF will handle the IME
  80. }
  81. public void OnDeselect(BaseEventData eventData) {
  82. _keyboardHasFocus = false;
  83. Input.imeCompositionMode = IMECompositionMode.Auto;
  84. }
  85. public void OnPointerEnter(PointerEventData eventData) {
  86. _mouseHasFocus = true;
  87. // SetCursor(BrowserCursor);
  88. }
  89. public void OnPointerExit(PointerEventData eventData) {
  90. _mouseHasFocus = false;
  91. // SetCursor(null);
  92. }
  93. public void OnPointerDown(PointerEventData eventData) {
  94. EventSystem.current.SetSelectedGameObject(gameObject);
  95. }
  96. }
  97. }