-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathBrowserViewScript.cs
More file actions
196 lines (164 loc) · 6.57 KB
/
BrowserViewScript.cs
File metadata and controls
196 lines (164 loc) · 6.57 KB
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
42
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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
165
166
167
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
#region Copyright
// Copyright © 2026, TeamDev. All rights reserved.
//
// Redistribution and use in source and/or binary forms, with or without
// modification, must retain the above copyright notice and the following
// disclaimer.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#endregion
using System;
using System.Collections.Concurrent;
using DotNetBrowser.Browser;
using DotNetBrowser.Geometry;
using DotNetBrowser.Ui;
using UnityEngine;
using UnityEngine.EventSystems;
namespace Assets.Scripts
{
/// <summary>
/// Updates texture of geometry primitive. Does forwarding of input to DotNetBrowser and makes focus control.
/// </summary>
public class BrowserViewScript : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler, IPointerUpHandler, IPointerMoveHandler, IPointerClickHandler
{
private Bitmap bitmap;
/// <summary>
/// Browser scene object that contains <see cref="browserScript" /> instance.
/// </summary>
public GameObject BrowserGameObject;
protected BrowserScript browserScript;
private bool isMouseOver;
private KeyboardHelper keyboardHelper;
private RectTransform rectTransform;
private MouseHelper mouseHelper;
private Texture2D texture;
private ConcurrentQueue<(EventHandler, object, EventArgs)> actions = new ConcurrentQueue<(EventHandler, object, EventArgs)>();
/// <summary>
/// Gets an <see cref="IBrowser" /> instance which this view shows.
/// </summary>
public IBrowser Browser => browserScript.Browser;
public bool IsFocused { get; private set; } = true;
public virtual void Focus()
{
Browser.Focus();
IsFocused = true;
}
public virtual void Unfocus()
{
Browser.Unfocus();
IsFocused = false;
}
protected void Dispatch((EventHandler, object, EventArgs) eventHandler) => actions.Enqueue(eventHandler);
protected virtual void Start()
{
browserScript = BrowserGameObject.GetComponent<BrowserScript>();
mouseHelper = new MouseHelper(Browser.Mouse) {ViewSize = Browser.Size};
keyboardHelper = new KeyboardHelper(Browser.Keyboard);
rectTransform = GetComponent<RectTransform>();
}
protected virtual void Update()
{
while(actions.TryDequeue(out (EventHandler, object, EventArgs) action))
{
action.Item1.Invoke(action.Item2, action.Item3);
}
if (isMouseOver)
{
if (Input.GetAxis("Mouse ScrollWheel") != 0f)
{
mouseHelper?.MouseWheel();
}
}
else
{
if (IsFocused && Input.GetMouseButtonDown(0))
{
Unfocus();
}
}
UpdateTexture();
}
protected virtual void SetTexture(Texture texture)
=> gameObject.GetComponent<MeshRenderer>().material.mainTexture = texture;
private void OnGUI()
{
if (IsFocused)
{
keyboardHelper?.HandleKeyboardEvents();
}
}
private void OnMouseDown()
{
mouseHelper?.MouseDown();
Focus();
}
private void OnMouseDrag() => mouseHelper?.MouseDrag();
private void OnMouseEnter() => isMouseOver = true;
private void OnMouseExit() => isMouseOver = false;
private void OnMouseOver() => mouseHelper?.MouseMoved();
private void OnMouseUp() => mouseHelper?.MouseUp();
private void UpdateTexture()
{
if (browserScript?.Bitmap == bitmap)
{
return;
}
bitmap = browserScript.Bitmap;
int newWidth = (int) bitmap.Size.Width;
int newHeight = (int) bitmap.Size.Height;
if (texture == null || texture.width != newWidth || texture.height != newHeight)
{
texture = new Texture2D(newWidth, newHeight, TextureFormat.BGRA32, true);
SetTexture(texture);
mouseHelper.ViewSize = bitmap.Size;
}
texture.SetPixelData((byte[])bitmap.Pixels, 0);
texture.Apply(true);
}
public void OnPointerEnter(PointerEventData eventData) => isMouseOver = true;
public void OnPointerExit(PointerEventData eventData) => isMouseOver = false;
public void OnPointerDown(PointerEventData eventData)
{
SetPoint(eventData);
mouseHelper?.MouseDown();
Focus();
}
public void OnPointerUp(PointerEventData eventData)
{
SetPoint(eventData);
mouseHelper?.MouseUp();
}
public void OnPointerMove(PointerEventData eventData)
{
SetPoint(eventData);
mouseHelper?.MouseMoved();
}
public void OnPointerClick(PointerEventData eventData)
{
}
private void SetPoint(PointerEventData data)
{
if(bitmap == null)
{
return;
}
RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, data.position, null, out Vector2 localClick);
localClick.x = (rectTransform.rect.xMin * -1) - (localClick.x * -1);
localClick.y = (rectTransform.rect.yMin * -1) - (localClick.y * -1);
Vector2 viewportClick = new Vector2(localClick.x / rectTransform.rect.size.x, localClick.y / rectTransform.rect.size.y);
int x = (int)(viewportClick.x * bitmap.Size.Width);
int y = (int)((1.0f - viewportClick.y) * bitmap.Size.Height);
mouseHelper.Point = new Point(x, y);
}
}
}