| | 1 | | using System; |
| | 2 | | using System.Runtime.InteropServices; |
| | 3 | | using AOT; |
| | 4 | | using DCL; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | internal class ClipboardWebGL : Singleton<ClipboardWebGL>, IClipboardHandler, IDisposable |
| | 8 | | { |
| | 9 | | private Action<string, bool> OnRead; |
| | 10 | | private bool copyInput = false; |
| | 11 | |
|
| | 12 | | private delegate void ReadTextCallback(IntPtr ptrText, int intError); |
| | 13 | |
|
| | 14 | | private delegate void OnPasteInputCallback(IntPtr ptrText); |
| | 15 | |
|
| | 16 | | private delegate void OnCopyInputCallback(); |
| | 17 | |
|
| | 18 | | [DllImport("__Internal")] |
| | 19 | | private static extern void initialize(Action<IntPtr, int> readTextCallback, Action<IntPtr> pasteCallback, |
| | 20 | | Action copyCallback); |
| | 21 | |
|
| | 22 | | /// <summary> |
| | 23 | | /// External call to write text in the browser's clipboard |
| | 24 | | /// </summary> |
| | 25 | | /// <param name="text">string to push to the clipboard</param> |
| | 26 | | [DllImport("__Internal")] |
| | 27 | | private static extern void writeText(string text); |
| | 28 | |
|
| | 29 | | /// <summary> |
| | 30 | | /// External call to request the string value stored at browser's clipboard |
| | 31 | | /// </summary> |
| | 32 | | [DllImport("__Internal")] |
| | 33 | | private static extern void readText(); |
| | 34 | |
|
| | 35 | | /// <summary> |
| | 36 | | /// This static function is called from the browser. It will receive a pointer to the string value |
| | 37 | | /// stored at browser's clipboard or and error if it couldn't get the value |
| | 38 | | /// </summary> |
| | 39 | | /// <param name="ptrText">pointer to the clipboard's string value</param> |
| | 40 | | /// <param name="intError">0 if error, other if OK</param> |
| | 41 | | [MonoPInvokeCallback(typeof(ReadTextCallback))] |
| | 42 | | private static void OnReceiveReadText(IntPtr ptrText, int intError) |
| | 43 | | { |
| 0 | 44 | | string value = Marshal.PtrToStringAuto(ptrText); |
| 0 | 45 | | bool error = intError == 0; |
| 0 | 46 | | i?.OnRead?.Invoke(value, error); |
| 0 | 47 | | } |
| | 48 | |
|
| | 49 | | /// <summary> |
| | 50 | | /// This static function is called from the browser. It will be called when a PASTE input is performed (CTRL+V) |
| | 51 | | /// and it will receive a pointer to the string value stored at browser's clipboard |
| | 52 | | /// </summary> |
| | 53 | | /// <param name="ptrText">pointer to the clipboard's string value</param> |
| | 54 | | [MonoPInvokeCallback(typeof(OnPasteInputCallback))] |
| | 55 | | private static void OnReceivePasteInput(IntPtr ptrText) |
| | 56 | | { |
| 0 | 57 | | string value = Marshal.PtrToStringAuto(ptrText); |
| | 58 | | // NOTE: after marshalling we overwrite unity's clipboard buffer with the value coming from the browser |
| 0 | 59 | | GUIUtility.systemCopyBuffer = value; |
| 0 | 60 | | } |
| | 61 | |
|
| | 62 | | /// <summary> |
| | 63 | | /// This static function is called from the browser. It will be called when a COPY input is performed (CTRL+C) |
| | 64 | | /// </summary> |
| | 65 | | [MonoPInvokeCallback(typeof(OnCopyInputCallback))] |
| | 66 | | private static void OnReceiveCopyInput() |
| | 67 | | { |
| | 68 | | // NOTE: here we set the flag that a copy input was performed to be used in OnBeforeRender function |
| 0 | 69 | | if (i != null) |
| 0 | 70 | | i.copyInput = true; |
| 0 | 71 | | } |
| | 72 | |
|
| 0 | 73 | | public ClipboardWebGL() { Application.onBeforeRender += OnBeforeRender; } |
| | 74 | |
|
| 0 | 75 | | public void Dispose() { Application.onBeforeRender -= OnBeforeRender; } |
| | 76 | |
|
| | 77 | | void IClipboardHandler.Initialize(Action<string, bool> onRead) |
| | 78 | | { |
| 0 | 79 | | this.OnRead = onRead; |
| 0 | 80 | | initialize(OnReceiveReadText, OnReceivePasteInput, OnReceiveCopyInput); |
| 0 | 81 | | } |
| | 82 | |
|
| 0 | 83 | | void IClipboardHandler.RequestWriteText(string text) { writeText(text); } |
| | 84 | |
|
| 0 | 85 | | void IClipboardHandler.RequestGetText() { readText(); } |
| | 86 | |
|
| | 87 | | void OnBeforeRender() |
| | 88 | | { |
| | 89 | | // NOTE: before rendering (just after Unity's input is processed) we check if there was a COPY input (CTRL+C) tr |
| | 90 | | // the browser. If there was a COPY input we push the text copied and stored inside Unity's clipboard into the b |
| | 91 | | // It is done this way cause we don't have a callback for Unity's copy input and because we want to store the va |
| | 92 | | // browser's clipboard so we are able to paste it outside Unity's "sandboxing" |
| 0 | 93 | | if (copyInput) |
| | 94 | | { |
| 0 | 95 | | copyInput = false; |
| 0 | 96 | | writeText(GUIUtility.systemCopyBuffer); |
| | 97 | | } |
| 0 | 98 | | } |
| | 99 | | } |