guanghuispark
4 年前
当前提交
755bb996
共有 60 个文件被更改,包括 1871 次插入 和 551 次删除
-
2Samples/UIWidgetsSamples_2019_4/Assets/UIWidgetsGallery/demo/cupertino/cupertino_switch_demo.cs
-
4Samples/UIWidgetsSamples_2019_4/Assets/UIWidgetsGallery/demo/material/backdrop_demo.cs
-
3Samples/UIWidgetsSamples_2019_4/Assets/UIWidgetsSample.asmdef
-
2com.unity.uiwidgets/Editor/UIWidgetsPanelEditor.cs
-
12com.unity.uiwidgets/Runtime/cupertino/date_picker.cs
-
384com.unity.uiwidgets/Runtime/engine2/UIWidgetsPanel.cs
-
5com.unity.uiwidgets/Runtime/gestures/binding.cs
-
8com.unity.uiwidgets/Runtime/material/app.cs
-
5com.unity.uiwidgets/Runtime/material/pickers/date_picker_head.cs
-
7com.unity.uiwidgets/Runtime/painting/image_provider.cs
-
6com.unity.uiwidgets/Runtime/rendering/list_wheel_viewport.cs
-
7com.unity.uiwidgets/Runtime/services/asset_bundle.cs
-
27com.unity.uiwidgets/Runtime/ui2/window.cs
-
138com.unity.uiwidgets/Runtime/widgets/app.cs
-
6com.unity.uiwidgets/Runtime/widgets/focus_manager.cs
-
8com.unity.uiwidgets/Runtime/widgets/image.cs
-
14com.unity.uiwidgets/Runtime/widgets/navigator.cs
-
19com.unity.uiwidgets/Runtime/widgets/nested_scroll_view.cs
-
15com.unity.uiwidgets/Runtime/widgets/routes.cs
-
19com.unity.uiwidgets/Runtime/widgets/scrollable.cs
-
19com.unity.uiwidgets/Runtime/widgets/text.cs
-
2engine/src/lib/ui/window/window.cc
-
21engine/src/shell/platform/unity/darwin/macos/uiwidgets_panel.h
-
64engine/src/shell/platform/unity/darwin/macos/uiwidgets_panel.mm
-
3engine/src/shell/platform/unity/darwin/macos/uiwidgets_system.h
-
20engine/src/shell/platform/unity/darwin/macos/uiwidgets_system.mm
-
33engine/src/shell/platform/unity/darwin/macos/unity_surface_manager.mm
-
81engine/src/shell/platform/unity/windows/uiwidgets_panel.cc
-
21engine/src/shell/platform/unity/windows/uiwidgets_panel.h
-
19engine/src/shell/platform/unity/windows/uiwidgets_system.cc
-
3engine/src/shell/platform/unity/windows/uiwidgets_system.h
-
27engine/src/shell/platform/unity/windows/unity_surface_manager.cc
-
3Samples/UIWidgetsSamples_2019_4/Assets/Editor.meta
-
14Samples/UIWidgetsSamples_2019_4/Assets/UIWidgetsGallery/UIWidgetsGallery.asmdef
-
135com.unity.uiwidgets/Editor/UIWidgetsEditorPanel.cs
-
8com.unity.uiwidgets/Editor/third_parties.meta
-
377com.unity.uiwidgets/Runtime/engine2/UIWidgetsPanelWrapper.cs
-
8com.unity.uiwidgets/Runtime/redux.meta
-
3Samples/UIWidgetsSamples_2019_4/Assets/Editor/EditorWindowSample.meta
-
7Samples/UIWidgetsSamples_2019_4/Assets/Editor/EditorWindowSample/UIWidgetsEditorWindowSample.asmdef.meta
-
21Samples/UIWidgetsSamples_2019_4/Assets/Editor/EditorWindowSample/GalleryMain.cs
-
16Samples/UIWidgetsSamples_2019_4/Assets/Editor/EditorWindowSample/UIWidgetsEditorWindowSample.asmdef
-
3Samples/UIWidgetsSamples_2019_4/Assets/Editor/EditorWindowSample/GalleryMain.cs.meta
-
39com.unity.uiwidgets/Editor/third_parties/EditorCoroutines.cs
-
8com.unity.uiwidgets/Editor/third_parties/editor_coroutines.meta
-
407com.unity.uiwidgets/Editor/third_parties/editor_coroutines/EditorCoroutines.cs
-
21com.unity.uiwidgets/Editor/third_parties/editor_coroutines/LICENSE
-
19com.unity.uiwidgets/Runtime/redux/redux_logging.cs
-
11com.unity.uiwidgets/Runtime/redux/redux_logging.cs.meta
-
32com.unity.uiwidgets/Runtime/redux/redux_thunk.cs
-
11com.unity.uiwidgets/Runtime/redux/redux_thunk.cs.meta
-
81com.unity.uiwidgets/Runtime/redux/store.cs
-
11com.unity.uiwidgets/Runtime/redux/store.cs.meta
-
172com.unity.uiwidgets/Runtime/redux/widget_redux.cs
-
11com.unity.uiwidgets/Runtime/redux/widget_redux.cs.meta
|
|||
fileFormatVersion: 2 |
|||
guid: fbf4348f25a14fb7a48fd287e0d01b88 |
|||
timeCreated: 1614220178 |
|
|||
{ |
|||
"name": "UIWidgetsGallery", |
|||
"references": [ |
|||
"Unity.UIWidgets" |
|||
], |
|||
"optionalUnityReferences": [], |
|||
"includePlatforms": [], |
|||
"excludePlatforms": [], |
|||
"allowUnsafeCode": false, |
|||
"overrideReferences": false, |
|||
"precompiledReferences": [], |
|||
"autoReferenced": true, |
|||
"defineConstraints": [] |
|||
} |
|
|||
using System.Collections; |
|||
using System.Collections.Generic; |
|||
using Unity.UIWidgets.editor2; |
|||
using Unity.UIWidgets.engine2; |
|||
using Unity.UIWidgets.foundation; |
|||
using Unity.UIWidgets.ui; |
|||
using UnityEditor; |
|||
using UnityEngine; |
|||
using Rect = UnityEngine.Rect; |
|||
|
|||
namespace Unity.UIWidgets.Editor { |
|||
public class UIWidgetsEditorPanel : EditorWindow, IUIWidgetsWindow { |
|||
UIWidgetsPanelWrapper _wrapper; |
|||
|
|||
public UIWidgetsWindowType getWindowType() { |
|||
return UIWidgetsWindowType.EditorWindowPanel; |
|||
} |
|||
|
|||
public bool isActive() { |
|||
return true; |
|||
} |
|||
|
|||
public void startCoroutine(IEnumerator routing) { |
|||
this.StartCoroutine(routing); |
|||
} |
|||
|
|||
public void onNewFrameScheduled() { |
|||
Repaint(); |
|||
} |
|||
|
|||
public Offset windowPosToScreenPos(Offset offset) { |
|||
return offset; |
|||
} |
|||
|
|||
int _currentWidth { |
|||
get { return Mathf.RoundToInt(position.size.x); } |
|||
} |
|||
|
|||
int _currentHeight { |
|||
get { return Mathf.RoundToInt(position.size.y); } |
|||
} |
|||
|
|||
float _currentDevicePixelRatio { |
|||
get { return EditorGUIUtility.pixelsPerPoint; } |
|||
} |
|||
|
|||
void OnDestroy() { |
|||
D.assert(_wrapper != null); |
|||
_wrapper?.Destroy(); |
|||
_wrapper = null; |
|||
|
|||
Input_OnDisable(); |
|||
} |
|||
|
|||
void OnEnable() { |
|||
D.assert(_wrapper == null); |
|||
_wrapper = new UIWidgetsPanelWrapper(); |
|||
_wrapper.Initiate(this, _currentWidth, _currentHeight, _currentDevicePixelRatio, new Dictionary<string, object>()); |
|||
|
|||
Input_OnEnable(); |
|||
} |
|||
|
|||
void Update() { |
|||
_wrapper.onEditorUpdate(); |
|||
} |
|||
|
|||
void OnGUI() |
|||
{ |
|||
if (_wrapper != null) { |
|||
if (_wrapper.didDisplayMetricsChanged(_currentWidth, _currentHeight, _currentDevicePixelRatio)) { |
|||
_wrapper.OnDisplayMetricsChanged(_currentWidth, _currentHeight, _currentDevicePixelRatio); |
|||
} |
|||
|
|||
GUI.DrawTexture(new Rect(0.0f, 0.0f, position.width, position.height), _wrapper.renderTexture); |
|||
Input_OnGUIEvent(Event.current); |
|||
} |
|||
} |
|||
|
|||
Vector2? _getPointerPosition(Vector2 position) { |
|||
return new Vector2(position.x, position.y); |
|||
} |
|||
|
|||
int _buttonToPointerId(int buttonId) { |
|||
if (buttonId == 0) { |
|||
return -1; |
|||
} |
|||
else if (buttonId == 1) { |
|||
return -2; |
|||
} |
|||
|
|||
return 0; |
|||
} |
|||
|
|||
void Input_OnGUIEvent(Event evt) { |
|||
if (evt.type == EventType.MouseDown) { |
|||
var pos = _getPointerPosition(evt.mousePosition); |
|||
_wrapper.OnPointerDown(pos, _buttonToPointerId(evt.button)); |
|||
} |
|||
else if (evt.type == EventType.MouseUp || evt.rawType == EventType.MouseUp) { |
|||
var pos = _getPointerPosition(evt.mousePosition); |
|||
_wrapper.OnPointerUp(pos, _buttonToPointerId(evt.button)); |
|||
} |
|||
else if (evt.type == EventType.MouseDrag) { |
|||
var pos = _getPointerPosition(evt.mousePosition); |
|||
_wrapper.OnMouseMove(pos); |
|||
} |
|||
else if (evt.type == EventType.MouseMove) { |
|||
var pos = _getPointerPosition(evt.mousePosition); |
|||
_wrapper.OnMouseMove(pos); |
|||
} |
|||
else if (evt.type == EventType.ScrollWheel && evt.delta.magnitude != 0) { |
|||
var delta = evt.delta; |
|||
delta.y /= 3f; |
|||
var pos = _getPointerPosition(evt.mousePosition); |
|||
_wrapper.OnMouseScroll(delta, pos); |
|||
} |
|||
|
|||
} |
|||
|
|||
public void mainEntry() { |
|||
main(); |
|||
} |
|||
|
|||
protected virtual void main() { |
|||
} |
|||
|
|||
void Input_OnDisable() { |
|||
|
|||
} |
|||
|
|||
void Input_OnEnable() { |
|||
|
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 41a89d8cd28257149b9e6d7116b7d598 |
|||
folderAsset: yes |
|||
DefaultImporter: |
|||
externalObjects: {} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Runtime.InteropServices; |
|||
using AOT; |
|||
using Unity.UIWidgets.engine2; |
|||
using Unity.UIWidgets.foundation; |
|||
using Unity.UIWidgets.gestures; |
|||
using Unity.UIWidgets.services; |
|||
using Unity.UIWidgets.ui; |
|||
using UnityEngine; |
|||
|
|||
namespace Unity.UIWidgets.editor2 { |
|||
|
|||
#region Platform: Windows Specific Functionalities
|
|||
|
|||
#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
|
|||
public partial class UIWidgetsPanelWrapper { |
|||
RenderTexture _renderTexture; |
|||
|
|||
|
|||
public RenderTexture renderTexture { |
|||
get { return _renderTexture; } |
|||
} |
|||
|
|||
void _createRenderTexture(int width, int height, float devicePixelRatio) { |
|||
D.assert(_renderTexture == null); |
|||
|
|||
var desc = new RenderTextureDescriptor( |
|||
width, height, RenderTextureFormat.ARGB32, 0) { |
|||
useMipMap = false, |
|||
autoGenerateMips = false, |
|||
}; |
|||
|
|||
_renderTexture = new RenderTexture(desc) {hideFlags = HideFlags.HideAndDontSave}; |
|||
_renderTexture.Create(); |
|||
|
|||
_width = width; |
|||
_height = height; |
|||
_devicePixelRatio = devicePixelRatio; |
|||
} |
|||
|
|||
void _destroyRenderTexture() { |
|||
D.assert(_renderTexture != null); |
|||
ObjectUtils.SafeDestroy(_renderTexture); |
|||
_renderTexture = null; |
|||
} |
|||
|
|||
void _enableUIWidgetsPanel(string font_settings) { |
|||
UIWidgetsPanel_onEnable(_ptr, _renderTexture.GetNativeTexturePtr(), |
|||
_width, _height, _devicePixelRatio, Application.streamingAssetsPath, font_settings); |
|||
} |
|||
|
|||
void _resizeUIWidgetsPanel() { |
|||
UIWidgetsPanel_onRenderTexture(_ptr, |
|||
_renderTexture.GetNativeTexturePtr(), |
|||
_width, _height, _devicePixelRatio); |
|||
} |
|||
|
|||
void _disableUIWidgetsPanel() { |
|||
_renderTexture = null; |
|||
} |
|||
|
|||
[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_onRenderTexture( |
|||
IntPtr ptr, IntPtr nativeTexturePtr, int width, int height, float dpi); |
|||
} |
|||
#endif
|
|||
|
|||
#endregion
|
|||
|
|||
|
|||
#region Platform: MacOs Specific Functionalities
|
|||
|
|||
#if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
|
|||
|
|||
public partial class UIWidgetsPanelWrapper { |
|||
Texture _renderTexture; |
|||
|
|||
public Texture renderTexture { |
|||
get { return _renderTexture; } |
|||
} |
|||
|
|||
void _createRenderTexture(int width, int height, float devicePixelRatio) { |
|||
D.assert(_renderTexture == null); |
|||
|
|||
_width = width; |
|||
_height = height; |
|||
_devicePixelRatio = devicePixelRatio; |
|||
} |
|||
|
|||
void _destroyRenderTexture() { |
|||
D.assert(_renderTexture != null); |
|||
var releaseOK = UIWidgetsPanel_releaseNativeTexture(_ptr); |
|||
D.assert(releaseOK); |
|||
|
|||
_renderTexture = null; |
|||
} |
|||
|
|||
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); |
|||
|
|||
_renderTexture = |
|||
Texture2D.CreateExternalTexture(_width, _height, TextureFormat.BGRA32, false, true, native_tex_ptr); |
|||
} |
|||
|
|||
void _disableUIWidgetsPanel() { |
|||
_renderTexture = null; |
|||
} |
|||
|
|||
void _resizeUIWidgetsPanel() { |
|||
D.assert(_renderTexture == null); |
|||
|
|||
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 float devicePixelRatio { |
|||
get { return _devicePixelRatio; } |
|||
} |
|||
|
|||
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, (int) host.getWindowType(), UIWidgetsPanel_entrypoint); |
|||
_host = host; |
|||
|
|||
_enableUIWidgetsPanel(JSONMessageCodec.instance.toJson(settings)); |
|||
NativeConsole.OnEnable(); |
|||
} |
|||
|
|||
public void _entryPoint() { |
|||
try { |
|||
isolate = Isolate.current; |
|||
Window.instance._panel = this; |
|||
|
|||
_host.mainEntry(); |
|||
} |
|||
catch (Exception ex) { |
|||
Debug.LogException(new Exception("exception in main", ex)); |
|||
} |
|||
} |
|||
|
|||
public bool didDisplayMetricsChanged(int width, int height, float dpr) { |
|||
return width != _width || height != _height || dpr != _devicePixelRatio; |
|||
} |
|||
|
|||
public void OnDisplayMetricsChanged(int width, int height, float dpr) { |
|||
if (_ptr != IntPtr.Zero && _renderTexture) { |
|||
if (_recreateRenderTexture(width, height, dpr)) { |
|||
_resizeUIWidgetsPanel(); |
|||
} |
|||
} |
|||
} |
|||
|
|||
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; |
|||
} |
|||
|
|||
if (_renderTexture) { |
|||
_destroyRenderTexture(); |
|||
} |
|||
|
|||
_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); |
|||
} |
|||
|
|||
public void onEditorUpdate() { |
|||
UIWidgetsPanel_onEditorUpdate(_ptr); |
|||
} |
|||
|
|||
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, int windowType, |
|||
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); |
|||
|
|||
[DllImport(NativeBindings.dllName)] |
|||
static extern void UIWidgetsPanel_unregisterTexture(IntPtr ptr, int textureId); |
|||
|
|||
[DllImport(NativeBindings.dllName)] |
|||
static extern void UIWidgetsPanel_markNewFrameAvailable(IntPtr ptr, int textureId); |
|||
|
|||
#if UNITY_EDITOR
|
|||
[DllImport(NativeBindings.dllName)] |
|||
static extern void UIWidgetsPanel_onEditorUpdate(IntPtr ptr); |
|||
#else
|
|||
static void UIWidgetsPanel_onEditorUpdate(IntPtr ptr) { throw new NotImplementedException(); } |
|||
#endif
|
|||
} |
|||
|
|||
#endregion
|
|||
|
|||
|
|||
#region Input Events Handles
|
|||
|
|||
public partial class UIWidgetsPanelWrapper { |
|||
public void OnMouseMove(Vector2? pos) { |
|||
if (pos == null) { |
|||
return; |
|||
} |
|||
|
|||
UIWidgetsPanel_onMouseMove(_ptr, pos.Value.x, pos.Value.y); |
|||
} |
|||
|
|||
public void OnMouseScroll(Vector2 delta, Vector2? pos) { |
|||
if (pos == null) { |
|||
return; |
|||
} |
|||
UIWidgetsPanel_onScroll(_ptr, delta.x, delta.y, pos.Value.x, pos.Value.y); |
|||
} |
|||
|
|||
public void OnPointerDown(Vector2? pos, int pointerId) { |
|||
if (pos == null) { |
|||
return; |
|||
} |
|||
|
|||
// 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; |
|||
} |
|||
|
|||
// mouse event
|
|||
if (pointerId < 0) { |
|||
UIWidgetsPanel_onMouseUp(_ptr, pos.Value.x, pos.Value.y, pointerId); |
|||
} |
|||
} |
|||
|
|||
public void OnDrag(Vector2? pos, int pointerId) { |
|||
if (pos == null) { |
|||
return; |
|||
} |
|||
|
|||
// mouse event
|
|||
if (pointerId < 0) { |
|||
UIWidgetsPanel_onMouseMove(_ptr, pos.Value.x, pos.Value.y); |
|||
} |
|||
} |
|||
|
|||
public void OnPointerLeave() { |
|||
UIWidgetsPanel_onMouseLeave(_ptr); |
|||
} |
|||
|
|||
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_onChar(IntPtr ptr, char c); |
|||
|
|||
[DllImport(NativeBindings.dllName)] |
|||
static extern void UIWidgetsPanel_onKey(IntPtr ptr, KeyCode keyCode, bool isKeyDown); |
|||
|
|||
[DllImport(NativeBindings.dllName)] |
|||
static extern void UIWidgetsPanel_onMouseDown(IntPtr ptr, float x, float y, int button); |
|||
|
|||
[DllImport(NativeBindings.dllName)] |
|||
static extern void UIWidgetsPanel_onMouseUp(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_onMouseLeave(IntPtr ptr); |
|||
|
|||
[DllImport(NativeBindings.dllName)] |
|||
static extern void UIWidgetsPanel_onScroll(IntPtr ptr, float x, float y, float px, float py); |
|||
} |
|||
|
|||
#endregion
|
|||
|
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: a074d5db1a47743f19fc0f9ac709e173 |
|||
folderAsset: yes |
|||
DefaultImporter: |
|||
externalObjects: {} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
fileFormatVersion: 2 |
|||
guid: 67e95d5f76f549d4ad7bfdeda34a9e75 |
|||
timeCreated: 1614132309 |
|
|||
fileFormatVersion: 2 |
|||
guid: 0ee4c4c70c305834389c168df0125409 |
|||
AssemblyDefinitionImporter: |
|||
externalObjects: {} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using UIWidgetsGallery.gallery; |
|||
using Unity.UIWidgets.Editor; |
|||
using UnityEditor; |
|||
using ui_ = Unity.UIWidgets.widgets.ui_; |
|||
|
|||
namespace UIWidgetsEditorWindowSample |
|||
{ |
|||
public class EditorWindowGallery : UIWidgetsEditorPanel |
|||
{ |
|||
[MenuItem("UIWidgets/EditorSample")] |
|||
public static void CountDemo() |
|||
{ |
|||
CreateWindow<EditorWindowGallery>(); |
|||
} |
|||
|
|||
protected override void main() |
|||
{ |
|||
ui_.runApp(new GalleryApp()); |
|||
} |
|||
} |
|||
} |
|
|||
{ |
|||
"name": "UIWidgetsEditorWindowSample", |
|||
"references": [ |
|||
"Unity.UIWidgets", |
|||
"Unity.UIWidgets.Editor", |
|||
"UIWidgetsGallery" |
|||
], |
|||
"optionalUnityReferences": [], |
|||
"includePlatforms": [], |
|||
"excludePlatforms": [], |
|||
"allowUnsafeCode": false, |
|||
"overrideReferences": false, |
|||
"precompiledReferences": [], |
|||
"autoReferenced": true, |
|||
"defineConstraints": [] |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 5e098e276aca4edf89cd58abb8af51b0 |
|||
timeCreated: 1614132321 |
|
|||
using System.Collections; |
|||
using UnityEditor; |
|||
using marijnz.EditorCoroutines; |
|||
|
|||
namespace Unity.UIWidgets.Editor { |
|||
|
|||
public static class UIWidgetsEditorCoroutineExtensions |
|||
{ |
|||
public static EditorCoroutines.EditorCoroutine StartCoroutine(this EditorWindow thisRef, IEnumerator coroutine) |
|||
{ |
|||
return EditorCoroutines.StartCoroutine(coroutine, thisRef); |
|||
} |
|||
|
|||
public static EditorCoroutines.EditorCoroutine StartCoroutine(this EditorWindow thisRef, string methodName) |
|||
{ |
|||
return EditorCoroutines.StartCoroutine(methodName, thisRef); |
|||
} |
|||
|
|||
public static EditorCoroutines.EditorCoroutine StartCoroutine(this EditorWindow thisRef, string methodName, object value) |
|||
{ |
|||
return EditorCoroutines.StartCoroutine(methodName, value, thisRef); |
|||
} |
|||
|
|||
public static void StopCoroutine(this EditorWindow thisRef, IEnumerator coroutine) |
|||
{ |
|||
EditorCoroutines.StopCoroutine(coroutine, thisRef); |
|||
} |
|||
|
|||
public static void StopCoroutine(this EditorWindow thisRef, string methodName) |
|||
{ |
|||
EditorCoroutines.StopCoroutine(methodName, thisRef); |
|||
} |
|||
|
|||
public static void StopAllCoroutines(this EditorWindow thisRef) |
|||
{ |
|||
EditorCoroutines.StopAllCoroutines(thisRef); |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: d2360e9d4e895a442a14215d1d4953f7 |
|||
folderAsset: yes |
|||
DefaultImporter: |
|||
externalObjects: {} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using UnityEngine; |
|||
using System.Collections; |
|||
using UnityEditor; |
|||
using System.Collections.Generic; |
|||
using System; |
|||
using System.Reflection; |
|||
|
|||
namespace marijnz.EditorCoroutines |
|||
{ |
|||
public class EditorCoroutines |
|||
{ |
|||
public class EditorCoroutine |
|||
{ |
|||
public ICoroutineYield currentYield = new YieldDefault(); |
|||
public IEnumerator routine; |
|||
public string routineUniqueHash; |
|||
public string ownerUniqueHash; |
|||
public string MethodName = ""; |
|||
|
|||
public int ownerHash; |
|||
public string ownerType; |
|||
|
|||
public bool finished = false; |
|||
|
|||
public EditorCoroutine(IEnumerator routine, int ownerHash, string ownerType) |
|||
{ |
|||
this.routine = routine; |
|||
this.ownerHash = ownerHash; |
|||
this.ownerType = ownerType; |
|||
ownerUniqueHash = ownerHash + "_" + ownerType; |
|||
|
|||
if (routine != null) |
|||
{ |
|||
string[] split = routine.ToString().Split('<', '>'); |
|||
if (split.Length == 3) |
|||
{ |
|||
MethodName = split[1]; |
|||
} |
|||
} |
|||
|
|||
routineUniqueHash = ownerHash + "_" + ownerType + "_" + MethodName; |
|||
} |
|||
|
|||
public EditorCoroutine(string methodName, int ownerHash, string ownerType) |
|||
{ |
|||
MethodName = methodName; |
|||
this.ownerHash = ownerHash; |
|||
this.ownerType = ownerType; |
|||
ownerUniqueHash = ownerHash + "_" + ownerType; |
|||
routineUniqueHash = ownerHash + "_" + ownerType + "_" + MethodName; |
|||
} |
|||
} |
|||
|
|||
public interface ICoroutineYield |
|||
{ |
|||
bool IsDone(float deltaTime); |
|||
} |
|||
|
|||
struct YieldDefault : ICoroutineYield |
|||
{ |
|||
public bool IsDone(float deltaTime) |
|||
{ |
|||
return true; |
|||
} |
|||
} |
|||
|
|||
struct YieldWaitForSeconds : ICoroutineYield |
|||
{ |
|||
public float timeLeft; |
|||
|
|||
public bool IsDone(float deltaTime) |
|||
{ |
|||
timeLeft -= deltaTime; |
|||
return timeLeft < 0; |
|||
} |
|||
} |
|||
|
|||
struct YieldCustomYieldInstruction : ICoroutineYield |
|||
{ |
|||
public CustomYieldInstruction customYield; |
|||
|
|||
public bool IsDone(float deltaTime) |
|||
{ |
|||
return !customYield.keepWaiting; |
|||
} |
|||
} |
|||
|
|||
struct YieldWWW : ICoroutineYield |
|||
{ |
|||
public WWW Www; |
|||
|
|||
public bool IsDone(float deltaTime) |
|||
{ |
|||
return Www.isDone; |
|||
} |
|||
} |
|||
|
|||
struct YieldAsync : ICoroutineYield |
|||
{ |
|||
public AsyncOperation asyncOperation; |
|||
|
|||
public bool IsDone(float deltaTime) |
|||
{ |
|||
return asyncOperation.isDone; |
|||
} |
|||
} |
|||
|
|||
struct YieldNestedCoroutine : ICoroutineYield |
|||
{ |
|||
public EditorCoroutine coroutine; |
|||
|
|||
public bool IsDone(float deltaTime) |
|||
{ |
|||
return coroutine.finished; |
|||
} |
|||
} |
|||
|
|||
static EditorCoroutines instance = null; |
|||
|
|||
Dictionary<string, List<EditorCoroutine>> coroutineDict = new Dictionary<string, List<EditorCoroutine>>(); |
|||
List<List<EditorCoroutine>> tempCoroutineList = new List<List<EditorCoroutine>>(); |
|||
|
|||
Dictionary<string, Dictionary<string, EditorCoroutine>> coroutineOwnerDict = |
|||
new Dictionary<string, Dictionary<string, EditorCoroutine>>(); |
|||
|
|||
DateTime previousTimeSinceStartup; |
|||
|
|||
/// <summary>Starts a coroutine.</summary>
|
|||
/// <param name="routine">The coroutine to start.</param>
|
|||
/// <param name="thisReference">Reference to the instance of the class containing the method.</param>
|
|||
public static EditorCoroutine StartCoroutine(IEnumerator routine, object thisReference) |
|||
{ |
|||
CreateInstanceIfNeeded(); |
|||
return instance.GoStartCoroutine(routine, thisReference); |
|||
} |
|||
|
|||
/// <summary>Starts a coroutine.</summary>
|
|||
/// <param name="methodName">The name of the coroutine method to start.</param>
|
|||
/// <param name="thisReference">Reference to the instance of the class containing the method.</param>
|
|||
public static EditorCoroutine StartCoroutine(string methodName, object thisReference) |
|||
{ |
|||
return StartCoroutine(methodName, null, thisReference); |
|||
} |
|||
|
|||
/// <summary>Starts a coroutine.</summary>
|
|||
/// <param name="methodName">The name of the coroutine method to start.</param>
|
|||
/// <param name="value">The parameter to pass to the coroutine.</param>
|
|||
/// <param name="thisReference">Reference to the instance of the class containing the method.</param>
|
|||
public static EditorCoroutine StartCoroutine(string methodName, object value, object thisReference) |
|||
{ |
|||
MethodInfo methodInfo = thisReference.GetType() |
|||
.GetMethod(methodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); |
|||
if (methodInfo == null) |
|||
{ |
|||
Debug.LogError("Coroutine '" + methodName + "' couldn't be started, the method doesn't exist!"); |
|||
} |
|||
object returnValue; |
|||
|
|||
if (value == null) |
|||
{ |
|||
returnValue = methodInfo.Invoke(thisReference, null); |
|||
} |
|||
else |
|||
{ |
|||
returnValue = methodInfo.Invoke(thisReference, new object[] {value}); |
|||
} |
|||
|
|||
if (returnValue is IEnumerator) |
|||
{ |
|||
CreateInstanceIfNeeded(); |
|||
return instance.GoStartCoroutine((IEnumerator) returnValue, thisReference); |
|||
} |
|||
else |
|||
{ |
|||
Debug.LogError("Coroutine '" + methodName + "' couldn't be started, the method doesn't return an IEnumerator!"); |
|||
} |
|||
|
|||
return null; |
|||
} |
|||
|
|||
/// <summary>Stops all coroutines being the routine running on the passed instance.</summary>
|
|||
/// <param name="routine"> The coroutine to stop.</param>
|
|||
/// <param name="thisReference">Reference to the instance of the class containing the method.</param>
|
|||
public static void StopCoroutine(IEnumerator routine, object thisReference) |
|||
{ |
|||
CreateInstanceIfNeeded(); |
|||
instance.GoStopCoroutine(routine, thisReference); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Stops all coroutines named methodName running on the passed instance.</summary>
|
|||
/// <param name="methodName"> The name of the coroutine method to stop.</param>
|
|||
/// <param name="thisReference">Reference to the instance of the class containing the method.</param>
|
|||
public static void StopCoroutine(string methodName, object thisReference) |
|||
{ |
|||
CreateInstanceIfNeeded(); |
|||
instance.GoStopCoroutine(methodName, thisReference); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Stops all coroutines running on the passed instance.</summary>
|
|||
/// <param name="thisReference">Reference to the instance of the class containing the method.</param>
|
|||
public static void StopAllCoroutines(object thisReference) |
|||
{ |
|||
CreateInstanceIfNeeded(); |
|||
instance.GoStopAllCoroutines(thisReference); |
|||
} |
|||
|
|||
static void CreateInstanceIfNeeded() |
|||
{ |
|||
if (instance == null) |
|||
{ |
|||
instance = new EditorCoroutines(); |
|||
instance.Initialize(); |
|||
} |
|||
} |
|||
|
|||
void Initialize() |
|||
{ |
|||
previousTimeSinceStartup = DateTime.Now; |
|||
EditorApplication.update += OnUpdate; |
|||
} |
|||
|
|||
void GoStopCoroutine(IEnumerator routine, object thisReference) |
|||
{ |
|||
GoStopActualRoutine(CreateCoroutine(routine, thisReference)); |
|||
} |
|||
|
|||
void GoStopCoroutine(string methodName, object thisReference) |
|||
{ |
|||
GoStopActualRoutine(CreateCoroutineFromString(methodName, thisReference)); |
|||
} |
|||
|
|||
void GoStopActualRoutine(EditorCoroutine routine) |
|||
{ |
|||
if (coroutineDict.ContainsKey(routine.routineUniqueHash)) |
|||
{ |
|||
coroutineOwnerDict[routine.ownerUniqueHash].Remove(routine.routineUniqueHash); |
|||
coroutineDict.Remove(routine.routineUniqueHash); |
|||
} |
|||
} |
|||
|
|||
void GoStopAllCoroutines(object thisReference) |
|||
{ |
|||
EditorCoroutine coroutine = CreateCoroutine(null, thisReference); |
|||
if (coroutineOwnerDict.ContainsKey(coroutine.ownerUniqueHash)) |
|||
{ |
|||
foreach (var couple in coroutineOwnerDict[coroutine.ownerUniqueHash]) |
|||
{ |
|||
coroutineDict.Remove(couple.Value.routineUniqueHash); |
|||
} |
|||
coroutineOwnerDict.Remove(coroutine.ownerUniqueHash); |
|||
} |
|||
} |
|||
|
|||
EditorCoroutine GoStartCoroutine(IEnumerator routine, object thisReference) |
|||
{ |
|||
if (routine == null) |
|||
{ |
|||
Debug.LogException(new Exception("IEnumerator is null!"), null); |
|||
} |
|||
EditorCoroutine coroutine = CreateCoroutine(routine, thisReference); |
|||
GoStartCoroutine(coroutine); |
|||
return coroutine; |
|||
} |
|||
|
|||
void GoStartCoroutine(EditorCoroutine coroutine) |
|||
{ |
|||
if (!coroutineDict.ContainsKey(coroutine.routineUniqueHash)) |
|||
{ |
|||
List<EditorCoroutine> newCoroutineList = new List<EditorCoroutine>(); |
|||
coroutineDict.Add(coroutine.routineUniqueHash, newCoroutineList); |
|||
} |
|||
coroutineDict[coroutine.routineUniqueHash].Add(coroutine); |
|||
|
|||
if (!coroutineOwnerDict.ContainsKey(coroutine.ownerUniqueHash)) |
|||
{ |
|||
Dictionary<string, EditorCoroutine> newCoroutineDict = new Dictionary<string, EditorCoroutine>(); |
|||
coroutineOwnerDict.Add(coroutine.ownerUniqueHash, newCoroutineDict); |
|||
} |
|||
|
|||
// If the method from the same owner has been stored before, it doesn't have to be stored anymore,
|
|||
// One reference is enough in order for "StopAllCoroutines" to work
|
|||
if (!coroutineOwnerDict[coroutine.ownerUniqueHash].ContainsKey(coroutine.routineUniqueHash)) |
|||
{ |
|||
coroutineOwnerDict[coroutine.ownerUniqueHash].Add(coroutine.routineUniqueHash, coroutine); |
|||
} |
|||
|
|||
MoveNext(coroutine); |
|||
} |
|||
|
|||
EditorCoroutine CreateCoroutine(IEnumerator routine, object thisReference) |
|||
{ |
|||
return new EditorCoroutine(routine, thisReference.GetHashCode(), thisReference.GetType().ToString()); |
|||
} |
|||
|
|||
EditorCoroutine CreateCoroutineFromString(string methodName, object thisReference) |
|||
{ |
|||
return new EditorCoroutine(methodName, thisReference.GetHashCode(), thisReference.GetType().ToString()); |
|||
} |
|||
|
|||
void OnUpdate() |
|||
{ |
|||
float deltaTime = (float) (DateTime.Now.Subtract(previousTimeSinceStartup).TotalMilliseconds / 1000.0f); |
|||
|
|||
previousTimeSinceStartup = DateTime.Now; |
|||
if (coroutineDict.Count == 0) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
tempCoroutineList.Clear(); |
|||
foreach (var pair in coroutineDict) |
|||
tempCoroutineList.Add(pair.Value); |
|||
|
|||
for (var i = tempCoroutineList.Count - 1; i >= 0; i--) |
|||
{ |
|||
List<EditorCoroutine> coroutines = tempCoroutineList[i]; |
|||
|
|||
for (int j = coroutines.Count - 1; j >= 0; j--) |
|||
{ |
|||
EditorCoroutine coroutine = coroutines[j]; |
|||
|
|||
if (!coroutine.currentYield.IsDone(deltaTime)) |
|||
{ |
|||
continue; |
|||
} |
|||
|
|||
if (!MoveNext(coroutine)) |
|||
{ |
|||
coroutines.RemoveAt(j); |
|||
coroutine.currentYield = null; |
|||
coroutine.finished = true; |
|||
} |
|||
|
|||
if (coroutines.Count == 0) |
|||
{ |
|||
coroutineDict.Remove(coroutine.routineUniqueHash); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
static bool MoveNext(EditorCoroutine coroutine) |
|||
{ |
|||
if (coroutine.routine.MoveNext()) |
|||
{ |
|||
return Process(coroutine); |
|||
} |
|||
|
|||
return false; |
|||
} |
|||
|
|||
// returns false if no next, returns true if OK
|
|||
static bool Process(EditorCoroutine coroutine) |
|||
{ |
|||
object current = coroutine.routine.Current; |
|||
if (current == null) |
|||
{ |
|||
coroutine.currentYield = new YieldDefault(); |
|||
} |
|||
else if (current is WaitForSeconds) |
|||
{ |
|||
float seconds = float.Parse(GetInstanceField(typeof(WaitForSeconds), current, "m_Seconds").ToString()); |
|||
coroutine.currentYield = new YieldWaitForSeconds() {timeLeft = seconds}; |
|||
} |
|||
else if (current is CustomYieldInstruction) |
|||
{ |
|||
coroutine.currentYield = new YieldCustomYieldInstruction() |
|||
{ |
|||
customYield = current as CustomYieldInstruction |
|||
}; |
|||
} |
|||
else if (current is WWW) |
|||
{ |
|||
coroutine.currentYield = new YieldWWW {Www = (WWW) current}; |
|||
} |
|||
else if (current is WaitForFixedUpdate || current is WaitForEndOfFrame) |
|||
{ |
|||
coroutine.currentYield = new YieldDefault(); |
|||
} |
|||
else if (current is AsyncOperation) |
|||
{ |
|||
coroutine.currentYield = new YieldAsync {asyncOperation = (AsyncOperation) current}; |
|||
} |
|||
else if (current is EditorCoroutine) |
|||
{ |
|||
coroutine.currentYield = new YieldNestedCoroutine { coroutine= (EditorCoroutine) current}; |
|||
} |
|||
else |
|||
{ |
|||
Debug.LogException( |
|||
new Exception("<" + coroutine.MethodName + "> yielded an unknown or unsupported type! (" + current.GetType() + ")"), |
|||
null); |
|||
coroutine.currentYield = new YieldDefault(); |
|||
} |
|||
return true; |
|||
} |
|||
|
|||
static object GetInstanceField(Type type, object instance, string fieldName) |
|||
{ |
|||
BindingFlags bindFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static; |
|||
FieldInfo field = type.GetField(fieldName, bindFlags); |
|||
return field.GetValue(instance); |
|||
} |
|||
} |
|||
} |
|
|||
MIT License |
|||
|
|||
Copyright (c) 2017 Marijn Zwemmer |
|||
|
|||
Permission is hereby granted, free of charge, to any person obtaining a copy |
|||
of this software and associated documentation files (the "Software"), to deal |
|||
in the Software without restriction, including without limitation the rights |
|||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|||
copies of the Software, and to permit persons to whom the Software is |
|||
furnished to do so, subject to the following conditions: |
|||
|
|||
The above copyright notice and this permission notice shall be included in all |
|||
copies or substantial portions of the Software. |
|||
|
|||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|||
SOFTWARE. |
|
|||
using UnityEngine; |
|||
|
|||
namespace Unity.UIWidgets.Redux { |
|||
public static class ReduxLogging { |
|||
public static Middleware<State> create<State>() { |
|||
return (store) => (next) => new DispatcherImpl((action) => { |
|||
var previousState = store.getState(); |
|||
var previousStateDump = JsonUtility.ToJson(previousState); |
|||
var result = next.dispatch(action); |
|||
var afterState = store.getState(); |
|||
var afterStateDump = JsonUtility.ToJson(afterState); |
|||
Debug.LogFormat("Action name={0} data={1}", action.ToString(), JsonUtility.ToJson(action)); |
|||
Debug.LogFormat("previousState=\n{0}", previousStateDump); |
|||
Debug.LogFormat("afterState=\n{0}", afterStateDump); |
|||
return result; |
|||
}); |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 66d5d5237e5f84f7f870b6e8d2b87815 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System; |
|||
|
|||
namespace Unity.UIWidgets.Redux { |
|||
public static class ReduxThunk { |
|||
public static Middleware<State> create<State>() { |
|||
return store => next => new DispatcherImpl(action => { |
|||
var thunkAction = action as ThunkAction<State>; |
|||
if (thunkAction != null && thunkAction.action != null) { |
|||
return thunkAction.action(store.dispatcher, store.getState); |
|||
} |
|||
|
|||
return next.dispatch(action); |
|||
}); |
|||
} |
|||
} |
|||
|
|||
public sealed class ThunkAction<State> { |
|||
public readonly Func<Dispatcher, Func<State>, object> action; |
|||
public readonly string displayName; |
|||
|
|||
public ThunkAction( |
|||
Func<Dispatcher, Func<State>, object> action = null, |
|||
string displayName = null) { |
|||
this.action = action; |
|||
this.displayName = displayName ?? ""; |
|||
} |
|||
|
|||
public override string ToString() { |
|||
return "ThunkAction(" + displayName + ")"; |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: f837de040a3fa4a3fb3e6c344153e1eb |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System; |
|||
using System.Linq; |
|||
|
|||
namespace Unity.UIWidgets { |
|||
public interface Dispatcher { |
|||
T dispatch<T>(object action); |
|||
|
|||
object dispatch(object action); |
|||
} |
|||
|
|||
public class DispatcherImpl : Dispatcher { |
|||
readonly Func<object, object> _impl; |
|||
|
|||
public DispatcherImpl(Func<object, object> impl) { |
|||
_impl = impl; |
|||
} |
|||
|
|||
public T dispatch<T>(object action) { |
|||
if (_impl == null) { |
|||
return default; |
|||
} |
|||
|
|||
return (T) _impl(action); |
|||
} |
|||
|
|||
public object dispatch(object action) { |
|||
if (_impl == null) { |
|||
return default; |
|||
} |
|||
|
|||
return _impl(action); |
|||
} |
|||
} |
|||
|
|||
public delegate State Reducer<State>(State previousState, object action); |
|||
|
|||
public delegate Func<Dispatcher, Dispatcher> Middleware<State>(Store<State> store); |
|||
|
|||
public delegate void StateChangedHandler<State>(State action); |
|||
|
|||
public class Store<State> { |
|||
public StateChangedHandler<State> stateChanged; |
|||
|
|||
readonly Dispatcher _dispatcher; |
|||
readonly Reducer<State> _reducer; |
|||
State _state; |
|||
|
|||
public Store( |
|||
Reducer<State> reducer, |
|||
State initialState = default, |
|||
params Middleware<State>[] middleware) { |
|||
_reducer = reducer; |
|||
_dispatcher = _applyMiddleware(middleware); |
|||
_state = initialState; |
|||
} |
|||
|
|||
public Dispatcher dispatcher { |
|||
get { return _dispatcher; } |
|||
} |
|||
|
|||
public State getState() { |
|||
return _state; |
|||
} |
|||
|
|||
Dispatcher _applyMiddleware(params Middleware<State>[] middleware) { |
|||
return middleware.Reverse().Aggregate<Middleware<State>, Dispatcher>( |
|||
new DispatcherImpl(_innerDispatch), |
|||
(current, middlewareItem) => middlewareItem(this)(current)); |
|||
} |
|||
|
|||
object _innerDispatch(object action) { |
|||
_state = _reducer(_state, action); |
|||
|
|||
if (stateChanged != null) { |
|||
stateChanged(_state); |
|||
} |
|||
|
|||
return action; |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 14bfb41ffcfd144f1b32a04eff5f93d0 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System; |
|||
using Unity.UIWidgets.foundation; |
|||
using Unity.UIWidgets.ui; |
|||
using Unity.UIWidgets.widgets; |
|||
|
|||
namespace Unity.UIWidgets.Redux { |
|||
public class StoreProvider<State> : InheritedWidget { |
|||
readonly Store<State> _store; |
|||
|
|||
public StoreProvider( |
|||
Store<State> store = null, |
|||
Widget child = null, |
|||
Key key = null) : base(key: key, child: child) { |
|||
D.assert(store != null); |
|||
D.assert(child != null); |
|||
_store = store; |
|||
} |
|||
|
|||
public static Store<State> of(BuildContext context) { |
|||
var type = _typeOf<StoreProvider<State>>(); |
|||
StoreProvider<State> provider = context.inheritFromWidgetOfExactType(type) as StoreProvider<State>; |
|||
if (provider == null) { |
|||
throw new UIWidgetsError("StoreProvider is missing"); |
|||
} |
|||
|
|||
return provider._store; |
|||
} |
|||
|
|||
static Type _typeOf<T>() { |
|||
return typeof(T); |
|||
} |
|||
|
|||
public override bool updateShouldNotify(InheritedWidget old) { |
|||
return !Equals(objA: _store, objB: ((StoreProvider<State>) old)._store); |
|||
} |
|||
} |
|||
|
|||
public delegate Widget ViewModelBuilder<in ViewModel>(BuildContext context, ViewModel viewModel, Dispatcher dispatcher); |
|||
|
|||
public delegate ViewModel StoreConverter<in State, out ViewModel>(State state); |
|||
|
|||
public delegate bool ShouldRebuildCallback<in ViewModel>(ViewModel previous, ViewModel current); |
|||
|
|||
public class StoreConnector<State, ViewModel> : StatelessWidget { |
|||
public readonly ViewModelBuilder<ViewModel> builder; |
|||
|
|||
public readonly StoreConverter<State, ViewModel> converter; |
|||
|
|||
public readonly ShouldRebuildCallback<ViewModel> shouldRebuild; |
|||
|
|||
public readonly bool pure; |
|||
|
|||
public StoreConnector( |
|||
ViewModelBuilder<ViewModel> builder = null, |
|||
StoreConverter<State, ViewModel> converter = null, |
|||
bool pure = false, |
|||
ShouldRebuildCallback<ViewModel> shouldRebuild = null, |
|||
Key key = null) : base(key) { |
|||
D.assert(builder != null); |
|||
D.assert(converter != null); |
|||
this.pure = pure; |
|||
this.builder = builder; |
|||
this.converter = converter; |
|||
this.shouldRebuild = shouldRebuild; |
|||
} |
|||
|
|||
public override Widget build(BuildContext context) { |
|||
return new _StoreListener<State, ViewModel>( |
|||
store: StoreProvider<State>.of(context), |
|||
builder: builder, |
|||
converter: converter, |
|||
pure: pure, |
|||
shouldRebuild: shouldRebuild |
|||
); |
|||
} |
|||
} |
|||
|
|||
public class _StoreListener<State, ViewModel> : StatefulWidget { |
|||
public readonly ViewModelBuilder<ViewModel> builder; |
|||
|
|||
public readonly StoreConverter<State, ViewModel> converter; |
|||
|
|||
public readonly Store<State> store; |
|||
|
|||
public readonly ShouldRebuildCallback<ViewModel> shouldRebuild; |
|||
|
|||
public readonly bool pure; |
|||
|
|||
public _StoreListener( |
|||
ViewModelBuilder<ViewModel> builder = null, |
|||
StoreConverter<State, ViewModel> converter = null, |
|||
Store<State> store = null, |
|||
bool pure = false, |
|||
ShouldRebuildCallback<ViewModel> shouldRebuild = null, |
|||
Key key = null) : base(key) { |
|||
D.assert(builder != null); |
|||
D.assert(converter != null); |
|||
D.assert(store != null); |
|||
this.store = store; |
|||
this.builder = builder; |
|||
this.converter = converter; |
|||
this.pure = pure; |
|||
this.shouldRebuild = shouldRebuild; |
|||
} |
|||
|
|||
public override widgets.State createState() { |
|||
return new _StoreListenerState<State, ViewModel>(); |
|||
} |
|||
} |
|||
|
|||
class _StoreListenerState<State, ViewModel> : State<_StoreListener<State, ViewModel>> { |
|||
ViewModel latestValue; |
|||
|
|||
public override void initState() { |
|||
base.initState(); |
|||
_init(); |
|||
} |
|||
|
|||
public override void dispose() { |
|||
widget.store.stateChanged -= _handleStateChanged; |
|||
base.dispose(); |
|||
} |
|||
|
|||
public override void didUpdateWidget(StatefulWidget oldWidget) { |
|||
var oldStore = ((_StoreListener<State, ViewModel>) oldWidget).store; |
|||
if (widget.store != oldStore) { |
|||
oldStore.stateChanged -= _handleStateChanged; |
|||
_init(); |
|||
} |
|||
|
|||
base.didUpdateWidget(oldWidget); |
|||
} |
|||
|
|||
void _init() { |
|||
widget.store.stateChanged += _handleStateChanged; |
|||
latestValue = widget.converter(widget.store.getState()); |
|||
} |
|||
|
|||
void _handleStateChanged(State state) { |
|||
if (Window.instance._panel != null) { |
|||
_innerStateChanged(state: state); |
|||
} |
|||
else { |
|||
var isolate = Isolate.current; |
|||
using (Isolate.getScope(isolate: isolate)) { |
|||
_innerStateChanged(state: state); |
|||
} |
|||
} |
|||
} |
|||
|
|||
void _innerStateChanged(State state) { |
|||
var preValue = latestValue; |
|||
latestValue = widget.converter(widget.store.getState()); |
|||
if (widget.shouldRebuild != null) { |
|||
if (!widget.shouldRebuild(preValue, latestValue)) { |
|||
return; |
|||
} |
|||
} |
|||
else if (widget.pure) { |
|||
if (Equals(preValue, latestValue)) { |
|||
return; |
|||
} |
|||
} |
|||
|
|||
setState(); |
|||
} |
|||
|
|||
public override Widget build(BuildContext context) { |
|||
return widget.builder(context, latestValue, widget.store.dispatcher); |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: a1765e003a4744e39b381a0bd927735a |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
撰写
预览
正在加载...
取消
保存
Reference in new issue