Skip to content

Commit 9fc30f7

Browse files
committed
add editortester and some editor components
1 parent c92cfd3 commit 9fc30f7

28 files changed

+336
-84
lines changed

Editor/Developer/TypescriptModelsGenerator.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@ public static void GenerateUnity()
2525
typeof(UnityEngine.Input).Assembly,
2626
typeof(UnityEngine.Animator).Assembly,
2727
typeof(UnityEngine.Event).Assembly,
28-
//#if REACT_INPUT_SYSTEM
29-
// typeof(UnityEngine.InputSystem.InputSystem).Assembly,
30-
// typeof(UnityEngine.InputSystem.UI.ExtendedPointerEventData).Assembly,
31-
//#endif
32-
//#if REACT_VECTOR_GRAPHICS
33-
// typeof(Unity.VectorGraphics.VectorUtils).Assembly,
34-
//#endif
28+
typeof(UnityEngine.UIElements.VisualElement).Assembly,
29+
//#if REACT_INPUT_SYSTEM
30+
// typeof(UnityEngine.InputSystem.InputSystem).Assembly,
31+
// typeof(UnityEngine.InputSystem.UI.ExtendedPointerEventData).Assembly,
32+
//#endif
33+
//#if REACT_VECTOR_GRAPHICS
34+
// typeof(Unity.VectorGraphics.VectorUtils).Assembly,
35+
//#endif
3536
},
3637
new List<string> { "Unity", "UnityEngine" },
3738
new List<string> { },
@@ -43,7 +44,7 @@ public static void GenerateUnity()
4344
public static void GenerateReactUnity()
4445
{
4546
Generate(
46-
new List<Assembly> { typeof(ReactUnity).Assembly },
47+
new List<Assembly> { typeof(ReactUnity).Assembly, typeof(TypescriptModelsGenerator).Assembly },
4748
new List<string> { "ReactUnity" },
4849
new List<string> { "UnityEngine.InputSystem" },
4950
new Dictionary<string, string> { { "UnityEngine", "unity" }, { "Unity", "unity" } }
@@ -297,6 +298,8 @@ static string getTypesScriptType(Type type, bool withNs)
297298
default:
298299
break;
299300
}
301+
302+
if (typeof(Attribute).IsAssignableFrom(type)) return "any";
300303
if (!type.IsEnum && propertyType.Contains("`")) return "any";
301304
if (type.DeclaringType != null)
302305
{

Editor/Renderer/Components/EditorReactComponent.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class EditorReactComponent : IReactComponent, IHostComponent, IContainerC
2020

2121
public static readonly YogaNode DefaultLayout = new YogaNode();
2222

23+
ReactContext IHostComponent.Context => Context;
2324
public EditorContext Context { get; }
2425
public IContainerComponent Parent { get; private set; }
2526
public VisualElement Element { get; protected set; }
@@ -44,6 +45,7 @@ public class EditorReactComponent : IReactComponent, IHostComponent, IContainerC
4445
public List<RuleTreeNode<StyleData>> BeforeRules { get; protected set; }
4546
public List<RuleTreeNode<StyleData>> AfterRules { get; protected set; }
4647

48+
4749
public EditorReactComponent(VisualElement element, EditorContext context, string tag)
4850
{
4951
Tag = tag;
@@ -59,6 +61,7 @@ public EditorReactComponent(EditorContext context, string tag)
5961
{
6062
Tag = tag;
6163
Context = context;
64+
Element = new Box();
6265

6366
StateStyles = new StateStyles(this);
6467
Style = new NodeStyle(StateStyles);
@@ -77,6 +80,10 @@ public void ApplyLayoutStyles()
7780

7881
public virtual void ApplyStyles()
7982
{
83+
Element.style.backgroundColor = Style.backgroundColor;
84+
Element.style.color = Style.color;
85+
Element.style.width = Layout.LayoutWidth;
86+
Element.style.height = Layout.LayoutHeight;
8087
}
8188

8289
public void Destroy()
@@ -110,7 +117,6 @@ public void ResolveStyle(bool recursive = false)
110117
if (Style.CssLayouts != null)
111118
foreach (var item in Style.CssLayouts) item.SetDefault(Layout, DefaultLayout);
112119
Style.CssLayouts = matchingRules.Where(x => x.Data?.Layouts != null).SelectMany(x => x.Data?.Layouts).Concat(inlineLayouts).ToList();
113-
//foreach (var item in Style.CssLayouts) item.Set(Layout, DefaultLayout);
114120

115121
for (int i = matchingRules.Count - 1; i >= importantIndex; i--) matchingRules[i].Data?.Layouts?.ForEach(x => x.Set(Layout, DefaultLayout));
116122
inlineLayouts.ForEach(x => x.Set(Layout, DefaultLayout));
@@ -149,12 +155,12 @@ public void ScheduleLayout(Action callback = null)
149155
public void SetParent(IContainerComponent parent, IReactComponent relativeTo = null, bool insertAfter = false)
150156
{
151157
Parent = parent;
152-
parent.RegisterChild(this);
153158

154159
relativeTo = relativeTo ?? (insertAfter ? null : parent.AfterPseudo);
155160

156161
if (relativeTo == null)
157162
{
163+
parent.RegisterChild(this);
158164
parent.Children.Add(this);
159165
parent.Layout.AddChild(Layout);
160166
}
@@ -163,9 +169,9 @@ public void SetParent(IContainerComponent parent, IReactComponent relativeTo = n
163169
var ind = parent.Children.IndexOf(relativeTo);
164170
if (insertAfter) ind++;
165171

172+
parent.RegisterChild(this, ind);
166173
parent.Children.Insert(ind, this);
167174
parent.Layout.Insert(ind, Layout);
168-
//RectTransform.SetSiblingIndex(ind);
169175
}
170176

171177
Style.Parent = parent.Style;
@@ -214,10 +220,13 @@ public object AddComponent(Type type)
214220
throw new NotImplementedException();
215221
}
216222

217-
public void RegisterChild(IReactComponent child)
223+
public void RegisterChild(IReactComponent child, int index = -1)
218224
{
219225
if (child is EditorReactComponent u)
220-
Element.Add(u.Element);
226+
{
227+
if(index >= 0) Element.Insert(index, u.Element);
228+
else Element.Add(u.Element);
229+
}
221230
}
222231
}
223232
}

Editor/Renderer/Components/EditorTextComponent.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ namespace ReactUnity.Editor.Renderer.Components
44
{
55
public class EditorTextComponent : EditorReactComponent, ITextComponent
66
{
7-
public EditorTextComponent(EditorContext context) : base(context, "text")
7+
public EditorTextComponent(string text, EditorContext context, string tag) : base(context, tag)
88
{
9-
Element = new Label();
9+
var lb = new Label();
10+
Element = lb;
11+
lb.text = text;
1012
}
1113

1214
public void SetText(string text)

Editor/Renderer/EditorContext.cs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
11
using ReactUnity.Editor.Renderer.Components;
2+
using ReactUnity.Interop;
23
using ReactUnity.Schedulers;
34
using ReactUnity.Types;
5+
using System;
6+
using System.Collections.Generic;
47
using UnityEngine;
58
using UnityEngine.UIElements;
69

710
namespace ReactUnity.Editor.Renderer
811
{
912
public class EditorContext : ReactContext
1013
{
11-
public EditorContext(VisualElement hostElement, StringObjectDictionary globals, ReactScript script, IUnityScheduler scheduler, bool isDevServer)
12-
: base(globals, script, scheduler, isDevServer)
14+
public static Func<string, string, EditorContext, EditorReactComponent> defaultCreator =
15+
(tag, text, context) => new EditorReactComponent(context, tag);
16+
17+
public static Func<string, EditorContext, ITextComponent> textCreator =
18+
(text, context) => new EditorTextComponent(text, context, "_text");
19+
20+
public static Dictionary<string, Func<string, string, EditorContext, EditorReactComponent>> ComponentCreators
21+
= new Dictionary<string, Func<string, string, EditorContext, EditorReactComponent>>()
22+
{
23+
{ "text", (tag, text, context) => new EditorTextComponent(text, context, tag) },
24+
{ "view", (tag, text, context) => new EditorReactComponent(context, "view") },
25+
};
26+
27+
public EditorContext(VisualElement hostElement, StringObjectDictionary globals, ReactScript script, IUnityScheduler scheduler, bool isDevServer, Action onRestart = null)
28+
: base(globals, script, scheduler, isDevServer, onRestart)
1329
{
1430
Host = new EditorReactComponent(hostElement, this, "_root");
1531
Host.ResolveStyle(true);
@@ -25,5 +41,20 @@ public EditorContext(VisualElement hostElement, StringObjectDictionary globals,
2541
}
2642
});
2743
}
44+
45+
public override ITextComponent CreateText(string text)
46+
{
47+
return textCreator(text, this);
48+
}
49+
50+
public override IReactComponent CreateComponent(string tag, string text)
51+
{
52+
EditorReactComponent res = null;
53+
if (ComponentCreators.TryGetValue(tag, out var creator))
54+
res = creator(tag, text, this);
55+
else res = defaultCreator(tag, text, this);
56+
if (res.Element != null) res.Element.name = $"<{tag}>";
57+
return res;
58+
}
2859
}
2960
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
using ReactUnity.Interop;
2+
using ReactUnity.Types;
3+
using System;
4+
using UnityEditor;
5+
using UnityEditor.UIElements;
6+
using UnityEngine;
7+
using UnityEngine.UIElements;
8+
9+
namespace ReactUnity.Editor.Renderer
10+
{
11+
public class ReactEditorTester : EditorWindow
12+
{
13+
[MenuItem("React/Editor Tester")]
14+
public static void ShowDefaultWindow()
15+
{
16+
var wnd = GetWindow<ReactEditorTester>();
17+
wnd.titleContent = new GUIContent("React Editor Tester");
18+
}
19+
20+
IDisposable ScriptWatchDisposable;
21+
ReactUnityRunner runner;
22+
EditorContext context;
23+
24+
public void OnEnable()
25+
{
26+
var uiAsset = Resources.Load<VisualTreeAsset>("ReactUnity/EditorTester");
27+
var ui = uiAsset.CloneTree();
28+
29+
var stylesheet = Resources.Load<StyleSheet>("ReactUnity/EditorTesterStyles");
30+
31+
ui.style.height = Length.Percent(100);
32+
rootVisualElement.Add(ui);
33+
rootVisualElement.styleSheets.Add(stylesheet);
34+
35+
rootVisualElement.Q<Button>("run").clicked += Run;
36+
}
37+
38+
ReactScript GetScript()
39+
{
40+
var source = rootVisualElement.Q<TextField>("source");
41+
var useDevServer = rootVisualElement.Q<Toggle>("useDevServer");
42+
var devServer = rootVisualElement.Q<TextField>("devServer");
43+
44+
useDevServer.RegisterValueChangedCallback(x =>
45+
{
46+
devServer.SetEnabled(x.newValue);
47+
});
48+
49+
return new ReactScript()
50+
{
51+
ScriptSource = ScriptSource.Resource,
52+
SourcePath = source.text,
53+
UseDevServer = useDevServer.value,
54+
DevServer = devServer.text,
55+
};
56+
}
57+
58+
void Run()
59+
{
60+
var host = rootVisualElement.Q("root");
61+
if (host == null) return;
62+
63+
host.Clear();
64+
var src = GetScript();
65+
66+
runner = new ReactUnityRunner();
67+
68+
ScriptWatchDisposable = src.GetScript((sc, isDevServer) =>
69+
{
70+
context = new EditorContext(host, new StringObjectDictionary(), src, new EditorScheduler(), isDevServer, Restart);
71+
runner.RunScript(sc, context);
72+
}, true, true);
73+
}
74+
75+
private void OnDestroy()
76+
{
77+
if (ScriptWatchDisposable != null) ScriptWatchDisposable.Dispose();
78+
if (context != null) context.Scheduler.clearAllTimeouts();
79+
EditorDispatcher.StopAll();
80+
81+
runner = null;
82+
context = null;
83+
ScriptWatchDisposable = null;
84+
}
85+
86+
public void Restart()
87+
{
88+
OnDestroy();
89+
Run();
90+
}
91+
}
92+
}

Editor/Renderer/ReactEditorTester.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/Resources.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/Resources/ReactUnity.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<UXML xmlns:ui="UnityEngine.Experimental.UIElements">
2+
<ui:TextField label="Source File" name="source" text="react-editor/index" />
3+
4+
<ui:VisualElement style="flex-shrink:0; flex-direction:row;">
5+
<ui:Toggle name="useDevServer" text="Use Dev Server" />
6+
<ui:TextField name="devServer" text="http://localhost:3000" style="flex-grow:1;" />
7+
</ui:VisualElement>
8+
9+
<ui:VisualElement style="flex-shrink:0;">
10+
<ui:Button name="run" text="Run" />
11+
</ui:VisualElement>
12+
13+
<ui:VisualElement name="root">
14+
</ui:VisualElement>
15+
</UXML>

Editor/Resources/ReactUnity/EditorTester.uxml.meta

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)