Newer
Older
Bjoern Esswein
committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using bessw.Unity.WebView.ChromeDevTools;
using bessw.Unity.WebView.ChromeDevTools.Protocol.Input;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace bessw.Unity.WebView
{
[RequireComponent(typeof(RawImage))]
public class WebViewComponent : MonoBehaviour, IPointerDownHandler, IPointerMoveHandler, IPointerUpHandler, IPointerEnterHandler, IDropHandler, IPointerExitHandler
{
#region json serializer
/// <summary>
/// Json serializer settings for the user space objects (e.g. transfering objects that have been droped on the BrowserView)
/// Users are allowed to change serializer settings to their liking.
/// </summary>
public static JsonSerializerSettings serializerSettings = new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
Converters = new List<JsonConverter>()
{
new ColorConverter(),
new Vector2Converter(),
new Vector3Converter(),
new Vector4Converter()
}
};
/// <summary>
/// JsonSerializer for the user space objects (e.g. transfering objects that have been droped on the BrowserView)
/// Users are allowed to change serializer settings to their liking.
/// </summary>
public static JsonSerializer serializer = JsonSerializer.Create(serializerSettings);
#endregion json serializer
private Browser browser;
Bjoern Esswein
committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
private RawImage rawImage;
private RectTransform rectTransform;
public bool headlessBrowser = true;
//TODO: handle changed targetUrl
public string targetUrl = "https://google.de";
// Start is called before the first frame update
private void Start()
{
Debug.LogWarning("start Webview");
rawImage = this.gameObject.GetComponent<RawImage>();
rectTransform = this.gameObject.GetComponent<RectTransform>();
}
private void OnEnable()
{
Debug.LogWarning("enable Webview");
Browser.headless = headlessBrowser;
browser = Browser.getInstance();
//StartCoroutine(GetOpenTabs());
var c = StartCoroutine(browser.OpenNewTab(targetUrl, (BrowserTab bt) =>
{
tab = bt;
StartCoroutine(tab.Update());
//StartCoroutine(createScreenshots());
StartCoroutine(tab.StartStream(900, 560, (frame) =>
{
Debug.LogWarning("stream");
rawImage.texture = frame;
rawImage.SetNativeSize();
}));
}));
}
public IEnumerator createScreenshots ()
{
yield return tab.CreateScreenshot(900, 560, (screenshot) =>
{
rawImage.texture = screenshot;
StartCoroutine(createScreenshots());
});
}
private IEnumerator getDropzoneState ()
{
Debug.LogWarning($"dropzone pre");
DomNodeWrapper doc = null;
yield return DomNodeWrapper.getDocument(tab, (document) => {
Debug.LogWarning($"dropzone 1: '{document}'");
doc = document;
StartCoroutine(document.querySelectorAll("[dropzone='copy']", (dropzones) => {
foreach (var dropzone in dropzones)
{
Debug.LogWarning($"dropzone 2: Node is Null?: '{dropzone.Node == null}'");
StartCoroutine(dropzone.getAttributes((attributes) =>
{
Debug.LogWarning($"dropzone 3 getAttributes: '{string.Join(", ", attributes.Values)}'");
}));
}
}));
});
Debug.LogWarning($"dropzone post: '{doc}'");
// alternative way to get the dropzone state
}
Bjoern Esswein
committed
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Update is called once per frame
private void Update()
{
}
#region pointer event handlers
public void OnPointerDown(PointerEventData eventData)
{
Vector2Int pos = toBrowserCoordinates(eventData.position);
tab.OnPointerDown(pos, eventData);
}
public void OnPointerMove(PointerEventData eventData)
{
// TODO: transform eventData delta vector to browser coordinates
Vector2Int pos = toBrowserCoordinates(eventData.position);
tab.OnPointerMove(pos, eventData);
// On drag over
if (eventData.dragging)
{
Debug.LogWarning($"OnDragOver: {eventData.position}");
createDragEvent(DragEventType.dragOver, eventData);
}
}
public void OnPointerUp(PointerEventData eventData)
{
Vector2Int pos = toBrowserCoordinates(eventData.position);
tab.OnPointerUp(pos, eventData);
}
#endregion pointer event handlers
#region drag event handlers
public void OnPointerEnter(PointerEventData eventData)
{
if (eventData.dragging)
{
Debug.LogWarning($"OnDragEnter: {eventData.position}");
createDragEvent(DragEventType.dragEnter, eventData);
}
}
public void OnDrop(PointerEventData eventData)
{
Debug.LogWarning($"OnDrop: {eventData.position}");
createDragEvent(DragEventType.drop, eventData);
// TODO: remove debug code
StartCoroutine(getDropzoneState());
Bjoern Esswein
committed
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
}
public void OnPointerExit(PointerEventData eventData)
{
if (eventData.dragging)
{
Debug.LogWarning($"OnDragLeave: {eventData.position}");
//createDragEvent(DragEventType.dragCancel, eventData);
tab.CancelDragging();
// TODO: drag cancel seems to be ignored by the browser
}
}
private void createDragEvent(DragEventType dragEventType, PointerEventData eventData)
{
if (eventData.pointerDrag.TryGetComponent(out BrowserDropable dropable))
{
var position = toBrowserCoordinates(eventData.position);
var dragEvent = new dispatchDragEvent
{
type = dragEventType,
data = new DragData
{
items = new DragDataItem[]
{
new DragDataItem
{
mimeType = "application/json",
data = JsonConvert.SerializeObject(dropable, serializerSettings)
}
},
dragOperationsMask = DragOperationsMask.Copy
},
x = position.x,
y = position.y,
};
Debug.LogWarning($"DragEvent: {dragEvent.type}, {eventData.position}, '{dragEvent.data.items[0].data}'");
// send the DragEvent as drag event to the browser
tab.OnDragNDrop(dragEvent);
}
}
#endregion drag event handlers
private Vector2Int toBrowserCoordinates(Vector2 eventPos)
{
Vector2 localPoint;
RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, eventPos, null, out localPoint);
// invert y because the browser has y=0 on the top
Vector2 invertedLocalPos = new Vector2(localPoint.x, rectTransform.rect.size.y - localPoint.y);
Vector2 browserCoorinate = tab.size / rectTransform.rect.size * invertedLocalPos;
Debug.Log($"eventPos: {eventPos}, invertedLocalPos: {invertedLocalPos}, browserCoordinate: {browserCoorinate}");
return new Vector2Int((int) browserCoorinate.x, (int) browserCoorinate.y);
}
private void OnDisable()
{
// TODO: do we want to close the browser when not in use?
// close browser when recompiling
tab.Close();
browser.Close();
}
private void OnDestroy()
{
tab.Close();
browser.Close();
}
/**
* Close all browser windows.
*/
private void OnApplicationQuit()
{
tab.Close();
browser.Close();
}
}
public interface BrowserDropable { }
}