|
|
|
|
|
|
using UnityEngine; |
|
|
|
|
|
|
|
namespace Unity.UIWidgets.editor2 { |
|
|
|
public class UIWidgetsPanelWrapper { |
|
|
|
|
|
|
|
#region Platform: Windows Specific Functionalities
|
|
|
|
|
|
|
|
#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
|
|
|
|
public partial class UIWidgetsPanelWrapper { |
|
|
|
|
|
|
|
|
|
|
|
void _createRenderTexture(int width, int height, float devicePixelRatio) { |
|
|
|
D.assert(_renderTexture == null); |
|
|
|
|
|
|
|
|
|
|
void _disableUIWidgetsPanel() { |
|
|
|
_renderTexture = null; |
|
|
|
} |
|
|
|
|
|
|
|
public static UIWidgetsPanelWrapper current { |
|
|
|
get { return Window.instance._panel; } |
|
|
|
} |
|
|
|
public IUIWidgetsWindow window => _host; |
|
|
|
[DllImport(NativeBindings.dllName)] |
|
|
|
static extern void UIWidgetsPanel_onEnable(IntPtr ptr, |
|
|
|
IntPtr nativeTexturePtr, int width, int height, float dpi, string streamingAssetsPath, |
|
|
|
string font_settings); |
|
|
|
public Isolate isolate { get; private set; } |
|
|
|
[DllImport(NativeBindings.dllName)] |
|
|
|
static extern void UIWidgetsPanel_onRenderTexture( |
|
|
|
IntPtr ptr, IntPtr nativeTexturePtr, int width, int height, float dpi); |
|
|
|
#endif
|
|
|
|
} |
|
|
|
IntPtr _ptr; |
|
|
|
GCHandle _handle; |
|
|
|
#endregion
|
|
|
|
int _width; |
|
|
|
int _height; |
|
|
|
float _devicePixelRatio; |
|
|
|
#region Platform: MacOs Specific Functionalities
|
|
|
|
public RenderTexture renderTexture => _renderTexture; |
|
|
|
|
|
|
|
public float devicePixelRatio => _devicePixelRatio; |
|
|
|
#if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
|
|
|
|
IUIWidgetsWindow _host; |
|
|
|
public partial class UIWidgetsPanelWrapper { |
|
|
|
Texture _renderTexture; |
|
|
|
public void Initiate(IUIWidgetsWindow host, int width, int height, float dpr, Dictionary<string, object> settings) { |
|
|
|
D.assert(_renderTexture == null); |
|
|
|
_recreateRenderTexture(width, height, dpr); |
|
|
|
void _createRenderTexture(int width, int height, float devicePixelRatio) { |
|
|
|
D.assert(_renderTexture == null); |
|
|
|
_handle = GCHandle.Alloc(this); |
|
|
|
_ptr = UIWidgetsPanel_constructor((IntPtr) _handle, UIWidgetsPanel_entrypoint); |
|
|
|
_width = width; |
|
|
|
_height = height; |
|
|
|
_devicePixelRatio = devicePixelRatio; |
|
|
|
} |
|
|
|
_host = host; |
|
|
|
void _destroyRenderTexture() { |
|
|
|
D.assert(_renderTexture != null); |
|
|
|
var releaseOK = UIWidgetsPanel_releaseNativeTexture(_ptr); |
|
|
|
D.assert(releaseOK); |
|
|
|
_enableUIWidgetsPanel(JSONMessageCodec.instance.toJson(settings)); |
|
|
|
|
|
|
|
Input_OnEnable(); |
|
|
|
NativeConsole.OnEnable(); |
|
|
|
} |
|
|
|
|
|
|
|
public void _entryPoint() { |
|
|
|
try { |
|
|
|
isolate = Isolate.current; |
|
|
|
Window.instance._panel = this; |
|
|
|
_renderTexture = null; |
|
|
|
} |
|
|
|
_host.mainEntry(); |
|
|
|
} |
|
|
|
catch (Exception ex) { |
|
|
|
Debug.LogException(new Exception("exception in main", ex)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public void OnRectTransformDimensionsChange(int width, int height, float dpr) { |
|
|
|
if (_ptr != IntPtr.Zero && _renderTexture) { |
|
|
|
if (_recreateRenderTexture(width, height, dpr)) { |
|
|
|
_resizeUIWidgetsPanel(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public void Destroy() { |
|
|
|
Input_OnDisable(); |
|
|
|
void _enableUIWidgetsPanel(string font_settings) { |
|
|
|
D.assert(_renderTexture == null); |
|
|
|
IntPtr native_tex_ptr = UIWidgetsPanel_onEnable(_ptr, _width, _height, _devicePixelRatio, |
|
|
|
Application.streamingAssetsPath, font_settings); |
|
|
|
D.assert(native_tex_ptr != IntPtr.Zero); |
|
|
|
UIWidgetsPanel_onDisable(_ptr); |
|
|
|
UIWidgetsPanel_dispose(_ptr); |
|
|
|
_ptr = IntPtr.Zero; |
|
|
|
_renderTexture = |
|
|
|
Texture2D.CreateExternalTexture(_width, _height, TextureFormat.BGRA32, false, true, native_tex_ptr); |
|
|
|
} |
|
|
|
_handle.Free(); |
|
|
|
_handle = default; |
|
|
|
void _disableUIWidgetsPanel() { |
|
|
|
_renderTexture = null; |
|
|
|
} |
|
|
|
_disableUIWidgetsPanel(); |
|
|
|
void _resizeUIWidgetsPanel() { |
|
|
|
D.assert(_renderTexture == null); |
|
|
|
D.assert(!isolate.isValid); |
|
|
|
} |
|
|
|
|
|
|
|
bool _recreateRenderTexture(int width, int height, float devicePixelRatio) { |
|
|
|
if (_renderTexture != null && _width == width && _height == height && |
|
|
|
_devicePixelRatio == devicePixelRatio) { |
|
|
|
return false; |
|
|
|
} |
|
|
|
IntPtr native_tex_ptr = UIWidgetsPanel_onRenderTexture(_ptr, _width, _height, _devicePixelRatio); |
|
|
|
D.assert(native_tex_ptr != IntPtr.Zero); |
|
|
|
|
|
|
|
_renderTexture = |
|
|
|
Texture2D.CreateExternalTexture(_width, _height, TextureFormat.BGRA32, false, true, native_tex_ptr); |
|
|
|
} |
|
|
|
|
|
|
|
[DllImport(NativeBindings.dllName)] |
|
|
|
static extern IntPtr UIWidgetsPanel_onEnable(IntPtr ptr, |
|
|
|
int width, int height, float dpi, string streamingAssetsPath, string font_settings); |
|
|
|
|
|
|
|
[DllImport(NativeBindings.dllName)] |
|
|
|
static extern bool UIWidgetsPanel_releaseNativeTexture(IntPtr ptr); |
|
|
|
|
|
|
|
[DllImport(NativeBindings.dllName)] |
|
|
|
static extern IntPtr UIWidgetsPanel_onRenderTexture( |
|
|
|
IntPtr ptr, int width, int height, float dpi); |
|
|
|
} |
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Window Common Properties and Functions
|
|
|
|
|
|
|
|
public partial class UIWidgetsPanelWrapper { |
|
|
|
public static UIWidgetsPanelWrapper current { |
|
|
|
get { return Window.instance._panel; } |
|
|
|
} |
|
|
|
|
|
|
|
IntPtr _ptr; |
|
|
|
GCHandle _handle; |
|
|
|
|
|
|
|
int _width; |
|
|
|
int _height; |
|
|
|
float _devicePixelRatio; |
|
|
|
|
|
|
|
IUIWidgetsWindow _host; |
|
|
|
|
|
|
|
public IUIWidgetsWindow window { |
|
|
|
get { return _host; } |
|
|
|
} |
|
|
|
|
|
|
|
public Isolate isolate { get; private set; } |
|
|
|
|
|
|
|
public RenderTexture renderTexture { |
|
|
|
get { return _renderTexture; } |
|
|
|
} |
|
|
|
|
|
|
|
public float devicePixelRatio { |
|
|
|
get { return _devicePixelRatio; } |
|
|
|
} |
|
|
|
if (_renderTexture) { |
|
|
|
_destroyRenderTexture(); |
|
|
|
} |
|
|
|
public void Initiate(IUIWidgetsWindow host, int width, int height, float dpr, |
|
|
|
Dictionary<string, object> settings) { |
|
|
|
D.assert(_renderTexture == null); |
|
|
|
_recreateRenderTexture(width, height, dpr); |
|
|
|
|
|
|
|
_handle = GCHandle.Alloc(this); |
|
|
|
_ptr = UIWidgetsPanel_constructor((IntPtr) _handle, UIWidgetsPanel_entrypoint); |
|
|
|
_host = host; |
|
|
|
|
|
|
|
_enableUIWidgetsPanel(JSONMessageCodec.instance.toJson(settings)); |
|
|
|
NativeConsole.OnEnable(); |
|
|
|
} |
|
|
|
|
|
|
|
public void _entryPoint() { |
|
|
|
try { |
|
|
|
isolate = Isolate.current; |
|
|
|
Window.instance._panel = this; |
|
|
|
_createRenderTexture(width, height, devicePixelRatio); |
|
|
|
return true; |
|
|
|
_host.mainEntry(); |
|
|
|
|
|
|
|
public int registerTexture(Texture texture) { |
|
|
|
return UIWidgetsPanel_registerTexture(_ptr, texture.GetNativeTexturePtr()); |
|
|
|
catch (Exception ex) { |
|
|
|
Debug.LogException(new Exception("exception in main", ex)); |
|
|
|
} |
|
|
|
public void unregisterTexture(int textureId) { |
|
|
|
UIWidgetsPanel_unregisterTexture(_ptr, textureId); |
|
|
|
public void OnWindowChanged(int width, int height, float dpr) { |
|
|
|
if (_ptr != IntPtr.Zero && _renderTexture) { |
|
|
|
if (_recreateRenderTexture(width, height, dpr)) { |
|
|
|
_resizeUIWidgetsPanel(); |
|
|
|
} |
|
|
|
} |
|
|
|
public void markNewFrameAvailable(int textureId) { |
|
|
|
UIWidgetsPanel_markNewFrameAvailable(_ptr, textureId); |
|
|
|
public void Destroy() { |
|
|
|
UIWidgetsPanel_onDisable(_ptr); |
|
|
|
UIWidgetsPanel_dispose(_ptr); |
|
|
|
_ptr = IntPtr.Zero; |
|
|
|
|
|
|
|
_handle.Free(); |
|
|
|
_handle = default; |
|
|
|
|
|
|
|
_disableUIWidgetsPanel(); |
|
|
|
D.assert(!isolate.isValid); |
|
|
|
} |
|
|
|
|
|
|
|
bool _recreateRenderTexture(int width, int height, float devicePixelRatio) { |
|
|
|
if (_renderTexture != null && _width == width && _height == height && |
|
|
|
_devicePixelRatio == devicePixelRatio) { |
|
|
|
return false; |
|
|
|
|
|
|
|
void Input_OnEnable() { |
|
|
|
|
|
|
|
if (_renderTexture) { |
|
|
|
_destroyRenderTexture(); |
|
|
|
void Input_OnDisable() { |
|
|
|
} |
|
|
|
|
|
|
|
public void OnMouseMove(Vector2? pos) { |
|
|
|
if (pos == null) { |
|
|
|
return; |
|
|
|
} |
|
|
|
UIWidgetsPanel_onMouseMove(_ptr, pos.Value.x, pos.Value.y); |
|
|
|
} |
|
|
|
_createRenderTexture(width, height, devicePixelRatio); |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
public int registerTexture(Texture texture) { |
|
|
|
return UIWidgetsPanel_registerTexture(_ptr, texture.GetNativeTexturePtr()); |
|
|
|
} |
|
|
|
|
|
|
|
public void unregisterTexture(int textureId) { |
|
|
|
UIWidgetsPanel_unregisterTexture(_ptr, textureId); |
|
|
|
} |
|
|
|
|
|
|
|
public void markNewFrameAvailable(int textureId) { |
|
|
|
UIWidgetsPanel_markNewFrameAvailable(_ptr, textureId); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
delegate void UIWidgetsPanel_EntrypointCallback(IntPtr handle); |
|
|
|
|
|
|
|
[MonoPInvokeCallback(typeof(UIWidgetsPanel_EntrypointCallback))] |
|
|
|
static void UIWidgetsPanel_entrypoint(IntPtr handle) { |
|
|
|
GCHandle gcHandle = (GCHandle) handle; |
|
|
|
UIWidgetsPanelWrapper panel = (UIWidgetsPanelWrapper) gcHandle.Target; |
|
|
|
panel._entryPoint(); |
|
|
|
} |
|
|
|
|
|
|
|
[DllImport(NativeBindings.dllName)] |
|
|
|
static extern IntPtr UIWidgetsPanel_constructor(IntPtr handle, |
|
|
|
UIWidgetsPanel_EntrypointCallback entrypointCallback); |
|
|
|
|
|
|
|
[DllImport(NativeBindings.dllName)] |
|
|
|
static extern void UIWidgetsPanel_dispose(IntPtr ptr); |
|
|
|
|
|
|
|
[DllImport(NativeBindings.dllName)] |
|
|
|
static extern void UIWidgetsPanel_onDisable(IntPtr ptr); |
|
|
|
|
|
|
|
[DllImport(NativeBindings.dllName)] |
|
|
|
static extern int UIWidgetsPanel_registerTexture(IntPtr ptr, IntPtr nativeTexturePtr); |
|
|
|
public void OnPointerDown(Vector2? pos, int pointerId) { |
|
|
|
if (pos == null) { |
|
|
|
return; |
|
|
|
} |
|
|
|
[DllImport(NativeBindings.dllName)] |
|
|
|
static extern void UIWidgetsPanel_unregisterTexture(IntPtr ptr, int textureId); |
|
|
|
|
|
|
|
[DllImport(NativeBindings.dllName)] |
|
|
|
static extern void UIWidgetsPanel_markNewFrameAvailable(IntPtr ptr, int textureId); |
|
|
|
} |
|
|
|
|
|
|
|
#endregion
|
|
|
|
// mouse event
|
|
|
|
if (pointerId < 0) { |
|
|
|
UIWidgetsPanel_onMouseDown(_ptr, pos.Value.x, pos.Value.y, pointerId); |
|
|
|
} |
|
|
|
} |
|
|
|
public void OnPointerUp(Vector2? pos, int pointerId) { |
|
|
|
if (pos == null) { |
|
|
|
return; |
|
|
|
} |
|
|
|
#region Input Events Handles
|
|
|
|
// mouse event
|
|
|
|
if (pointerId < 0) { |
|
|
|
UIWidgetsPanel_onMouseUp(_ptr, pos.Value.x, pos.Value.y, pointerId); |
|
|
|
} |
|
|
|
public partial class UIWidgetsPanelWrapper { |
|
|
|
public void OnMouseMove(Vector2? pos) { |
|
|
|
if (pos == null) { |
|
|
|
return; |
|
|
|
public void OnDrag(Vector2? pos, int pointerId) { |
|
|
|
if (pos == null) { |
|
|
|
return; |
|
|
|
} |
|
|
|
UIWidgetsPanel_onMouseMove(_ptr, pos.Value.x, pos.Value.y); |
|
|
|
} |
|
|
|
// mouse event
|
|
|
|
if (pointerId < 0) { |
|
|
|
UIWidgetsPanel_onMouseMove(_ptr, pos.Value.x, pos.Value.y); |
|
|
|
} |
|
|
|
public void OnPointerDown(Vector2? pos, int pointerId) { |
|
|
|
if (pos == null) { |
|
|
|
return; |
|
|
|
public void OnPointerLeave() { |
|
|
|
UIWidgetsPanel_onMouseLeave(_ptr); |
|
|
|
// mouse event
|
|
|
|
if (pointerId < 0) { |
|
|
|
UIWidgetsPanel_onMouseDown(_ptr, pos.Value.x, pos.Value.y, pointerId); |
|
|
|
} |
|
|
|
public void OnKeyDown(Event e) { |
|
|
|
UIWidgetsPanel_onKey(_ptr, e.keyCode, e.type == EventType.KeyDown); |
|
|
|
if (e.character != 0 || e.keyCode == KeyCode.Backspace) { |
|
|
|
PointerEventConverter.KeyEvent.Enqueue(new Event(e)); |
|
|
|
// TODO: add on char
|
|
|
|
// UIWidgetsPanel_onChar(_ptr, e.character);
|
|
|
|
} |
|
|
|
public void OnPointerUp(Vector2? pos, int pointerId) { |
|
|
|
if (pos == null) { |
|
|
|
return; |
|
|
|
|
|
|
|
|
|
|
|
delegate void UIWidgetsPanel_EntrypointCallback(IntPtr handle); |
|
|
|
[MonoPInvokeCallback(typeof(UIWidgetsPanel_EntrypointCallback))] |
|
|
|
static void UIWidgetsPanel_entrypoint(IntPtr handle) { |
|
|
|
GCHandle gcHandle = (GCHandle) handle; |
|
|
|
UIWidgetsPanelWrapper panel = (UIWidgetsPanelWrapper) gcHandle.Target; |
|
|
|
panel._entryPoint(); |
|
|
|
// mouse event
|
|
|
|
if (pointerId < 0) { |
|
|
|
UIWidgetsPanel_onMouseUp(_ptr, pos.Value.x, pos.Value.y, pointerId); |
|
|
|
} |
|
|
|
[DllImport(NativeBindings.dllName)] |
|
|
|
static extern IntPtr UIWidgetsPanel_constructor(IntPtr handle, |
|
|
|
UIWidgetsPanel_EntrypointCallback entrypointCallback); |
|
|
|
public void OnDrag(Vector2? pos, int pointerId) { |
|
|
|
if (pos == null) { |
|
|
|
return; |
|
|
|
} |
|
|
|
[DllImport(NativeBindings.dllName)] |
|
|
|
static extern void UIWidgetsPanel_dispose(IntPtr ptr); |
|
|
|
// mouse event
|
|
|
|
if (pointerId < 0) { |
|
|
|
UIWidgetsPanel_onMouseMove(_ptr, pos.Value.x, pos.Value.y); |
|
|
|
} |
|
|
|
} |
|
|
|
[DllImport(NativeBindings.dllName)] |
|
|
|
static extern void UIWidgetsPanel_onDisable(IntPtr ptr); |
|
|
|
public void OnPointerLeave() { |
|
|
|
UIWidgetsPanel_onMouseLeave(_ptr); |
|
|
|
} |
|
|
|
[DllImport(NativeBindings.dllName)] |
|
|
|
static extern int UIWidgetsPanel_registerTexture(IntPtr ptr, IntPtr nativeTexturePtr); |
|
|
|
public void OnKeyDown(Event e) { |
|
|
|
UIWidgetsPanel_onKey(_ptr, e.keyCode, e.type == EventType.KeyDown); |
|
|
|
if (e.character != 0 || e.keyCode == KeyCode.Backspace) { |
|
|
|
PointerEventConverter.KeyEvent.Enqueue(new Event(e)); |
|
|
|
// TODO: add on char
|
|
|
|
// UIWidgetsPanel_onChar(_ptr, e.character);
|
|
|
|
} |
|
|
|
} |
|
|
|
[DllImport(NativeBindings.dllName)] |
|
|
|
static extern void UIWidgetsPanel_unregisterTexture(IntPtr ptr, int textureId); |
|
|
|
[DllImport(NativeBindings.dllName)] |
|
|
|
static extern void UIWidgetsPanel_onChar(IntPtr ptr, char c); |
|
|
|
[DllImport(NativeBindings.dllName)] |
|
|
|
static extern void UIWidgetsPanel_markNewFrameAvailable(IntPtr ptr, int textureId); |
|
|
|
|
|
|
|
|
|
|
|
[DllImport(NativeBindings.dllName)] |
|
|
|
static extern void UIWidgetsPanel_onEnable(IntPtr ptr, |
|
|
|
IntPtr nativeTexturePtr, int width, int height, float dpi, string streamingAssetsPath, |
|
|
|
string font_settings); |
|
|
|
[DllImport(NativeBindings.dllName)] |
|
|
|
static extern void UIWidgetsPanel_onKey(IntPtr ptr, KeyCode keyCode, bool isKeyDown); |
|
|
|
[DllImport(NativeBindings.dllName)] |
|
|
|
static extern void UIWidgetsPanel_onRenderTexture( |
|
|
|
IntPtr ptr, IntPtr nativeTexturePtr, int width, int height, float dpi); |
|
|
|
|
|
|
|
[DllImport(NativeBindings.dllName)] |
|
|
|
static extern void UIWidgetsPanel_onChar(IntPtr ptr, char c); |
|
|
|
[DllImport(NativeBindings.dllName)] |
|
|
|
static extern void UIWidgetsPanel_onMouseDown(IntPtr ptr, float x, float y, int button); |
|
|
|
[DllImport(NativeBindings.dllName)] |
|
|
|
static extern void UIWidgetsPanel_onKey(IntPtr ptr, KeyCode keyCode, bool isKeyDown); |
|
|
|
[DllImport(NativeBindings.dllName)] |
|
|
|
static extern void UIWidgetsPanel_onMouseUp(IntPtr ptr, float x, float y, int button); |
|
|
|
[DllImport(NativeBindings.dllName)] |
|
|
|
static extern void UIWidgetsPanel_onMouseDown(IntPtr ptr, float x, float y, int button); |
|
|
|
[DllImport(NativeBindings.dllName)] |
|
|
|
static extern void UIWidgetsPanel_onMouseMove(IntPtr ptr, float x, float y); |
|
|
|
[DllImport(NativeBindings.dllName)] |
|
|
|
static extern void UIWidgetsPanel_onMouseUp(IntPtr ptr, float x, float y, int button); |
|
|
|
[DllImport(NativeBindings.dllName)] |
|
|
|
static extern void UIWidgetsPanel_onMouseLeave(IntPtr ptr); |
|
|
|
} |
|
|
|
[DllImport(NativeBindings.dllName)] |
|
|
|
static extern void UIWidgetsPanel_onMouseMove(IntPtr ptr, float x, float y); |
|
|
|
#endregion
|
|
|
|
[DllImport(NativeBindings.dllName)] |
|
|
|
static extern void UIWidgetsPanel_onMouseLeave(IntPtr ptr); |
|
|
|
} |
|
|
|
|
|
|
|
} |