Skip to content

Commit e5b5cc3

Browse files
author
邹杭特
committed
修正类父级解析失败的问题,增加const类型关键字
1 parent b08db2c commit e5b5cc3

36 files changed

+327
-225
lines changed

Assets/BridgeUI/Core/Behaiver/Panels/PanelBase.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ namespace BridgeUI
2828
[BridgeUI.Attributes.PanelParent]
2929
public class PanelBase : PanelCore, IBindingContext
3030
{
31-
private Binding.PropertyBinder _binder;
31+
protected Binding.PropertyBinder _binder;
3232
public virtual Binding.PropertyBinder Binder
3333
{
3434
get
@@ -49,7 +49,7 @@ protected IViewModel defultViewModel
4949
{
5050
if (_defultViewModel == null)
5151
{
52-
_defultViewModel = ScriptableObject.CreateInstance<ViewModel>();
52+
_defultViewModel = ScriptableObject.CreateInstance<ViewModelObject>();
5353
}
5454
return _defultViewModel;
5555
}
@@ -72,7 +72,7 @@ public IViewModel ViewModel
7272
[ContextMenu("加载ViewModel")]
7373
private void LoadDefultViewModel()
7474
{
75-
_viewModel = ScriptableObject.CreateInstance<ViewModel>();
75+
_viewModel = ScriptableObject.CreateInstance<ViewModelObject>();
7676
}
7777
[ContextMenu("清除ViewModel")]
7878
private void ClearDefultViewModel()
@@ -117,9 +117,9 @@ protected override void HandleData(object data)
117117
{
118118
base.HandleData(data);
119119

120-
if (data is Binding.ViewModel)
120+
if (data is IViewModel)
121121
{
122-
ViewModel = data as Binding.ViewModel;
122+
ViewModel = data as IViewModel;
123123
}
124124
else if (data is IDictionary)
125125
{

Assets/BridgeUI/Core/Binding/Editor/BindingWindow.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ private void SwitchComponent(MonoBehaviour v)
112112
panelDrawer = UnityEditor.Editor.CreateEditor(panelCompnent);
113113
}
114114
InitPanelBase(v);
115-
GenCodeUtil.AnalysisComponent(panelCompnent, components);
115+
GenCodeUtil.AnalysisComponent(panelCompnent, components,rule);
116116
}
117117

118118
private void InitPanelBase(MonoBehaviour v)
@@ -255,7 +255,7 @@ private void DrawPreComponents()
255255
else
256256
{
257257
//从旧的脚本解析出
258-
GenCodeUtil.AnalysisComponent(component, components);
258+
GenCodeUtil.AnalysisComponent(component, components,rule);
259259
}
260260
});
261261

@@ -304,7 +304,7 @@ private void DrawPreComponents()
304304
if (item is GameObject)
305305
{
306306
var obj = item as GameObject;
307-
var parent = PrefabUtility.GetPrefabParent(obj);
307+
var parent = PrefabUtility.GetPrefabObject(obj);
308308
if (parent){
309309
obj = parent as GameObject;
310310
}

Assets/BridgeUI/Core/Binding/Editor/ViewModelContainerDrawer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ private ScriptableObject GetAssetByType(Type type)
8282

8383
private void InitTypeOptions()
8484
{
85-
viewModelTypes = BridgeUI.Drawer.MvvmUtil.GetSubInstenceTypes(typeof(ViewModel)).ToArray();
85+
viewModelTypes = BridgeUI.Drawer.MvvmUtil.GetSubInstenceTypes(typeof(ViewModelObject)).ToArray();
8686
options = viewModelTypes.Select(x => new GUIContent(x.FullName)).ToArray();
8787
}
8888

Assets/BridgeUI/Core/Binding/ViewModel.cs

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
using UnityEngine;
22
using System.Collections.Generic;
33
using UnityEngine.Events;
4+
using System;
45

56
namespace BridgeUI.Binding
67
{
7-
public class ViewModel : ScriptableObject, IViewModel
8+
public class ViewModel : IViewModel
89
{
910
#if BridgeUI_Log
1011
public static bool log { get; set; }
1112
#endif
1213
private List<IBindingContext> _contexts = new List<IBindingContext>();
13-
protected List<IBindingContext> Contexts { get { return _contexts; } }
14+
public List<IBindingContext> Contexts { get { return _contexts; } }
1415
private readonly Dictionary<string, IBindableProperty> innerDic = new Dictionary<string, IBindableProperty>();
1516
protected IBindableProperty this[string key]
1617
{
@@ -73,14 +74,63 @@ protected virtual void SetValue<T>(string keyward, T value)
7374
}
7475
public virtual void OnBinding(IBindingContext context) { this._contexts.Add(context); }
7576
public virtual void OnUnBinding(IBindingContext context) { this._contexts.Remove(context); }
76-
protected virtual void Monitor<T>(string sourceName, UnityAction<T> onValueChanged)
77+
public virtual void Monitor<T>(string sourceName, UnityAction<T> onValueChanged)
7778
{
7879
if (!string.IsNullOrEmpty(sourceName) && onValueChanged != null)
7980
{
8081
GetBindableProperty<T>(sourceName).RegistValueChanged(onValueChanged);
8182
}
8283
}
84+
}
85+
86+
public class ViewModelObject : ScriptableObject, IViewModel
87+
{
88+
protected ViewModel viewModel = new ViewModel();
89+
protected List<IBindingContext> Contexts { get { return viewModel.Contexts; } }
90+
91+
public virtual bool ContainsKey(string key)
92+
{
93+
return viewModel.ContainsKey(key);
94+
}
95+
96+
public virtual IBindableProperty GetBindableProperty(string keyward, Type type)
97+
{
98+
return viewModel.GetBindableProperty(keyward,type);
99+
}
100+
101+
public virtual BindableProperty<T> GetBindableProperty<T>(string keyward)
102+
{
103+
return viewModel.GetBindableProperty<T>(keyward);
104+
}
105+
106+
public virtual void OnBinding(IBindingContext context)
107+
{
108+
viewModel.OnBinding(context);
109+
}
110+
111+
public virtual void OnUnBinding(IBindingContext context)
112+
{
113+
viewModel.OnUnBinding(context);
114+
}
83115

116+
public virtual void SetBindableProperty(string keyward, IBindableProperty value)
117+
{
118+
viewModel.SetBindableProperty(keyward,value);
119+
}
120+
#region
121+
protected virtual T GetValue<T>(string keyward)
122+
{
123+
return viewModel.GetBindableProperty<T>(keyward).Value;
124+
}
125+
protected virtual void SetValue<T>(string keyward, T value)
126+
{
127+
viewModel.GetBindableProperty<T>(keyward).Value = value;
128+
}
129+
protected virtual void Monitor<T>(string sourceName, UnityAction<T> onValueChanged)
130+
{
131+
viewModel.Monitor(sourceName, onValueChanged);
132+
}
133+
#endregion
84134
}
85135

86136
}

Assets/BridgeUI/Core/Binding/ViewModelContainer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace BridgeUI.Binding
77
public class ViewModelContainer : ScriptableObject, IViewModel
88
{
99
[SerializeField]
10-
public ViewModel instence;
10+
public ViewModelObject instence;
1111

1212
public bool ContainsKey(string key)
1313
{

Assets/BridgeUI/Core/Editor/PanelGroupDrawer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ private void TrySaveAllPrefabs(UIInfoBase[] proprety)
281281
var item = proprety[i];
282282
var obj = EditorUtility.InstanceIDToObject(item.instanceID);
283283
if (obj == null) continue;
284-
var prefab = PrefabUtility.GetPrefabParent(obj);
284+
var prefab = PrefabUtility.GetPrefabObject(obj);
285285
if (prefab != null)
286286
{
287287
var root = PrefabUtility.FindPrefabRoot((GameObject)prefab);

Assets/BridgeUI/Core/GenCode/Editor/ComponentItemDrawer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ private void DrawScriptTarget(Rect targetRect, ComponentItem item)
150150
var newTarget = EditorGUI.ObjectField(targetRect, item.scriptTarget, item.componentType, true);
151151
if (newTarget != item.scriptTarget)
152152
{
153-
var prefabTarget = PrefabUtility.GetPrefabParent(newTarget);
153+
var prefabTarget = PrefabUtility.GetPrefabObject(newTarget);
154154
Debug.Log(prefabTarget);
155155
if (prefabTarget != null)
156156
{
@@ -175,7 +175,7 @@ private void DrawTarget(Rect targetRect, ComponentItem item)
175175
var newTarget = EditorGUI.ObjectField(targetRect, item.target, item.componentType, true);
176176
if (newTarget != item.target)
177177
{
178-
var prefabTarget = PrefabUtility.GetPrefabParent(newTarget);
178+
var prefabTarget = PrefabUtility.GetPrefabObject(newTarget);
179179
if (prefabTarget != null)
180180
{
181181
newTarget = prefabTarget;
@@ -185,7 +185,7 @@ private void DrawTarget(Rect targetRect, ComponentItem item)
185185

186186
if (EditorGUI.EndChangeCheck() && item.target)
187187
{
188-
var parent = PrefabUtility.GetPrefabParent(item.target);
188+
var parent = PrefabUtility.GetPrefabObject(item.target);
189189
if (parent)
190190
{
191191
item.target = parent as GameObject;

Assets/BridgeUI/Core/GenCode/Editor/ComponetCode/ComponentCode.cs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,14 @@ public virtual void CompleteCode(ComponentItem component, bool bindingAble)
6565
{
6666
foreach (var item in component.viewItems)
6767
{
68+
GenCodeUtil.CompleteKeyField(item.bindingSource, classNode);
6869
BindingMemberInvocations(component.name, item);
6970
}
7071

7172
foreach (var item in component.eventItems)
7273
{
74+
GenCodeUtil.CompleteKeyField(item.bindingSource, classNode);
75+
7376
switch (item.type)
7477
{
7578
case BindingType.NoBinding:
@@ -104,7 +107,7 @@ protected virtual void BindingMemberInvocations(string name, BindingShow binding
104107
//UnityEngine.Debug.Log(bindingInfo.bindingTarget);
105108
//UnityEngine.Debug.Log(bindingInfo.bindingTargetType.type);
106109

107-
var arg1 = string.Format("\"{0}\"", bindingInfo.bindingSource);
110+
var arg1 = GenCodeUtil.GetSourceKeyWord(bindingInfo.bindingSource);
108111
var invocation = invocations.Where(
109112
x => x.Target.ToString().Contains("Binder") &&
110113
x.Arguments.Count > 0 &&
@@ -136,7 +139,7 @@ protected virtual void BindingMemberInvocations(string name, BindingShow binding
136139
invocation = new InvocationExpression();
137140
invocation.Target = new MemberReferenceExpression(new IdentifierExpression("Binder"), methodName, new AstType[0]);
138141
invocation.Arguments.Add(new IdentifierExpression(arg0));
139-
invocation.Arguments.Add(new PrimitiveExpression(bindingInfo.bindingSource));
142+
invocation.Arguments.Add(new IdentifierExpression(GenCodeUtil.GetSourceKeyWord(bindingInfo.bindingSource)));
140143
PropBindingsNode.Body.Add(invocation);
141144
}
142145

@@ -153,7 +156,7 @@ protected virtual void BindingEventInvocations(string name, Type componentType,
153156
{
154157
var invocations = PropBindingsNode.Body.Descendants.OfType<InvocationExpression>();
155158
var arg0_name = "m_" + name + "." + bindingInfo.bindingTarget;//绑定目标
156-
var arg1_name = string.Format("\"{0}\"", bindingInfo.bindingSource);//绑定源
159+
var arg1_name = GenCodeUtil.GetSourceKeyWord(bindingInfo.bindingSource);//绑定源
157160

158161
var may_invocations = invocations.Where(
159162
x => x.Target.ToString().Contains("Binder") &&
@@ -195,7 +198,7 @@ protected virtual void BindingEventInvocations(string name, Type componentType,
195198
invocation = new InvocationExpression();
196199
invocation.Target = new MemberReferenceExpression(new IdentifierExpression("Binder"), methodName, new AstType[0]);
197200
invocation.Arguments.Add(new IdentifierExpression(arg0_name));
198-
invocation.Arguments.Add(new PrimitiveExpression(bindingInfo.bindingSource));
201+
invocation.Arguments.Add(new IdentifierExpression(GenCodeUtil.GetSourceKeyWord(bindingInfo.bindingSource)));
199202
if (bindingInfo.type == BindingType.WithTarget)
200203
{
201204
invocation.Arguments.Add(new IdentifierExpression("m_" + name));
@@ -217,7 +220,7 @@ protected virtual void LocalEventInvocations(string name, BindingEvent bindingIn
217220
var targetName = "m_" + name;
218221
var invocation = invocations.Where(x =>
219222
x.Target.ToString().Contains(targetName) &&
220-
x.Arguments.FirstOrDefault().ToString() == bindingInfo.bindingSource).FirstOrDefault();
223+
x.Arguments.FirstOrDefault().ToString() == GenCodeUtil.GetSourceKeyWord(bindingInfo.bindingSource)).FirstOrDefault();
221224

222225
var eventName = bindingInfo.bindingTarget;//如onClick
223226
if (invocation == null && !string.IsNullOrEmpty(eventName) && !string.IsNullOrEmpty(bindingInfo.bindingSource))
@@ -345,7 +348,7 @@ protected virtual void AnalysisLoaclInvocation(InvocationExpression invocation,
345348
var component = components.Find(x => invocation.Target.ToString().Contains("m_" + x.name));
346349
if (component != null)
347350
{
348-
string bindingSource = invocation.Arguments.First().ToString();
351+
string bindingSource = GenCodeUtil.FromSourceKey(invocation.Arguments.First().ToString());
349352
var info = component.eventItems.Find(x => invocation.Target.ToString().Contains("m_" + component.name + "." + x.bindingTarget) && x.type == BindingType.NoBinding && x.bindingSource == bindingSource);
350353

351354
if (info == null)
@@ -372,23 +375,23 @@ protected virtual void AnalysisBindingMembers(InvocationExpression invocation, L
372375
var component = components.Find(x => invocation.Arguments.Count > 1 && invocation.Arguments.First().ToString().Contains(string.Format("m_{0}.", x.name)));
373376
if (component != null)
374377
{
375-
var source = invocation.Arguments.ToArray()[1].ToString().Replace("\"", "");
376-
var info = component.viewItems.Find(x => x.bindingSource == source);
378+
var sourceName = GenCodeUtil.FromSourceKey(invocation.Arguments.ToArray()[1].ToString());
379+
var info = component.viewItems.Find(x => x.bindingSource == sourceName);
377380
var isMethod = false;
378381
var targetName = AnalysisTargetFromLamdaArgument(invocation.Arguments.First().ToString(), out isMethod);
379-
UnityEngine.Debug.Log("解析出字段:" + targetName);
382+
//UnityEngine.Debug.Log("解析出字段:" + targetName);
380383

381384
if (string.IsNullOrEmpty(targetName))
382385
{
383386
UnityEngine.Debug.Assert(!string.IsNullOrEmpty(targetName), "annalysis err:" + invocation.Arguments.First().ToString());
384387
return;
385388
}
386389
var type = GetTypeClamp(component.componentType, targetName);
387-
UnityEngine.Debug.Log("解析出类型:" + type);
390+
//UnityEngine.Debug.Log("解析出类型:" + type);
388391
if (info == null)
389392
{
390393
info = new BindingShow();
391-
info.bindingSource = source;
394+
info.bindingSource = sourceName;
392395
info.bindingTarget = targetName;
393396
info.isMethod = isMethod;
394397
component.viewItems.Add(info);
@@ -420,7 +423,7 @@ private string AnalysisTargetFromLamdaArgument(string arg, out bool isMethod)
420423
else
421424
{
422425
var value = arg;
423-
if(arg.Contains("\""))
426+
if (arg.Contains("\""))
424427
{
425428
value = arg.Replace("\"", "");
426429
isMethod = false;
@@ -429,9 +432,10 @@ private string AnalysisTargetFromLamdaArgument(string arg, out bool isMethod)
429432
{
430433
isMethod = true;
431434
}
432-
UnityEngine.Debug.Log(isMethod);
435+
//UnityEngine.Debug.Log(isMethod);
433436

434-
if (value.Contains(".")){
437+
if (value.Contains("."))
438+
{
435439
value = value.Substring(value.IndexOf('.') + 1);
436440
}
437441
return value;
@@ -450,16 +454,16 @@ protected virtual void AnalysisBindingEvents(InvocationExpression invocation, Li
450454

451455
if (component != null)
452456
{
453-
var source = invocation.Arguments.ToArray()[1].ToString().Replace("\"", "");
454-
var info = component.eventItems.Find(x => x.bindingSource == source);
457+
var sourceName = GenCodeUtil.FromSourceKey(invocation.Arguments.ToArray()[1].ToString());
458+
var info = component.eventItems.Find(x => x.bindingSource == sourceName);
455459
var arg0 = invocation.Arguments.First().ToString();
456460
var targetName = arg0.Substring(arg0.IndexOf(".") + 1);
457461
Type infoType = GetTypeClamp(component.componentType, targetName);
458462

459463
if (info == null)
460464
{
461465
info = new BindingEvent();
462-
info.bindingSource = source;
466+
info.bindingSource = sourceName;
463467
info.bindingTarget = targetName;
464468
info.bindingTargetType.Update(infoType);
465469
component.eventItems.Add(info);

0 commit comments

Comments
 (0)