// 一个View变量的绑定代码 public static void WidgetBind(CodeGenerator gen, DataContext rootContext, DataContext widgetContext) { switch (widgetContext.Type) { case WidgetType.ScrollRect: { gen.PrintLine("Framework.Utility.BindCollection<int, ", PresenterTemplate.ClassItemName(widgetContext), ", ", ClassItemName(widgetContext), ">( _Presenter.", PresenterTemplate.Property(widgetContext), ", ", WidgetVariable(widgetContext), " );" ); gen.PrintLine(); } break; case WidgetType.Button: { gen.PrintLine(WidgetVariable(widgetContext), ".onClick.AddListener( _Presenter.", PresenterTemplate.Command(widgetContext), " );"); } break; case WidgetType.InputField: case WidgetType.Text: { if (widgetContext.SyncType == DataSyncType.ViewToPresenter || widgetContext.SyncType == DataSyncType.TwoWay) { gen.PrintLine(WidgetVariable(widgetContext), ".onValueChanged.AddListener( x =>"); gen.PrintLine("{"); gen.In(); gen.PrintLine("_Presenter.", PresenterTemplate.Property(widgetContext), " = x;"); gen.Out(); gen.PrintLine("} );"); // 初始赋值 gen.PrintLine("_Presenter.", PresenterTemplate.Property(widgetContext), " = ", WidgetData(widgetContext), ";"); } if (widgetContext.SyncType == DataSyncType.PresenterToView || widgetContext.SyncType == DataSyncType.TwoWay) { gen.PrintLine("_Presenter.", PresenterTemplate.Event(widgetContext), " += delegate()"); gen.PrintLine("{"); gen.In(); switch (widgetContext.Type) { case WidgetType.Text: case WidgetType.InputField: { gen.PrintLine(WidgetVariable(widgetContext), ".text = _Presenter.", PresenterTemplate.Property(widgetContext), ";"); } break; } gen.Out(); gen.PrintLine("};"); gen.PrintLine("if ( _Presenter.", PresenterTemplate.Property(widgetContext), " != null )"); gen.PrintLine("{"); gen.In(); // 初始赋值 gen.PrintLine(WidgetData(widgetContext), " = ", "_Presenter.", PresenterTemplate.Property(widgetContext), ";"); gen.Out(); gen.PrintLine("}"); } gen.PrintLine(); } break; } }
public override void OnInspectorGUI() { DataContext ctx = target as DataContext; GUILayout.BeginHorizontal(); if (GUILayout.Button("Detect Type")) { ctx.Detect(); } ctx.Type = (WidgetType)EditorGUILayout.EnumPopup(ctx.Type); GUILayout.EndHorizontal(); switch (ctx.Type) { case WidgetType.InputField: case WidgetType.Text: { ctx.SyncType = (DataSyncType)EditorGUILayout.EnumPopup("SyncType", ctx.SyncType); break; } } switch (ctx.Type) { case WidgetType.View: { EditorGUILayout.LabelField("View: " + ViewTemplate.ClassName(ctx)); EditorGUILayout.LabelField("Presenter: " + PresenterTemplate.ClassName(ctx)); break; } case WidgetType.ScrollRect: { EditorGUILayout.LabelField("Instance in View: _" + ctx.Name); EditorGUILayout.LabelField("Property in Presenter: " + ctx.Name); EditorGUILayout.LabelField("Item View: " + ViewTemplate.ClassItemName(ctx)); EditorGUILayout.LabelField("Item Presenter: " + PresenterTemplate.ClassItemName(ctx)); break; } case WidgetType.Button: { EditorGUILayout.LabelField("Command: " + PresenterTemplate.Command(ctx)); break; } case WidgetType.InputField: case WidgetType.Text: { EditorGUILayout.LabelField("Instance in View: _" + ctx.Name); EditorGUILayout.LabelField("Property in Presenter: " + ctx.Name); break; } } if (GUILayout.Button("Add To Child")) { ctx.AddToTopChild(); } if (GUILayout.Button("Remove Child")) { if (EditorUtility.DisplayDialog("移除确认?", "移除所有子节点的DataContext", "是", "否")) { ctx.RemoveAllChild(); } } if (GUILayout.Button("Detect Child")) { ctx.DetectAllChild(); } base.OnInspectorGUI(); }