public static void ClassBody(CodeGenerator gen, DataContext rootContext, List <DataContext> widgetContextList) { gen.PrintLine("// Generated by github.com/davyxu/cellorigin"); gen.PrintLine("using UnityEngine;"); gen.PrintLine("using UnityEngine.UI;"); gen.PrintLine(); gen.PrintLine("partial class ", ClassName(rootContext), " : Framework.BaseView"); gen.PrintLine("{"); gen.In(); gen.PrintLine(PresenterTemplate.ClassName(rootContext), " _Presenter;"); gen.PrintLine(); // TODO 变量声明代码 foreach (DataContext widgetContext in widgetContextList) { WidgetDeclare(gen, widgetContext); } gen.PrintLine(); gen.PrintLine("public override void Bind( Framework.BasePresenter presenter )"); gen.PrintLine("{"); gen.In(); gen.PrintLine("_Presenter = presenter as ", PresenterTemplate.ClassName(rootContext), ";"); gen.PrintLine(); gen.PrintLine("var trans = this.transform;"); gen.PrintLine(); // TODO List中的名称重名检查 // 打印控件搜索代码 foreach (DataContext widgetContext in widgetContextList) { FindWidgetlAssignToVar(gen, rootContext, widgetContext); } gen.PrintLine(); // 打印控件绑定代码 foreach (DataContext widgetContext in widgetContextList) { WidgetBind(gen, rootContext, widgetContext); } gen.Out(); gen.PrintLine("}"); // Bind gen.PrintLine(); gen.Out(); gen.PrintLine("}"); // Class }
static void NetworkCallbackBody(CodeGenerator gen, DataContext rootContext, gamedef.CodeGenPeer peer, string msgType) { // Presenter类已经存在函数了, 不再生成 if (IsStringExists(rootContext, NetworkCallback(peer, msgType))) { return; } gen.PrintLine("public void ", NetworkCallback(peer, msgType), "( NetworkPeer peer, ", msgType, " msg )"); gen.PrintLine("{"); gen.In(); gen.PrintLine(); gen.Out(); gen.PrintLine("}"); gen.PrintLine(); }
static void NetworkRegisterBody(CodeGenerator gen, gamedef.CodeGenPeer peer) { gen.PrintLine(NetworkPeerInstance(peer), " = PeerManager.Instance.Get(\"", peer.Name, "\");"); gen.PrintLine(); foreach (string msgType in peer.RecvMessage) { gen.PrintLine(NetworkPeerInstance(peer), ".RegisterMessage<", msgType, ">( obj =>"); gen.PrintLine("{"); gen.In(); gen.PrintLine(NetworkCallback(peer, msgType), "( ", NetworkPeerInstance(peer), ", obj as ", msgType, " );"); gen.Out(); gen.PrintLine("});"); gen.PrintLine(); } }
static void CommandBody(CodeGenerator gen, DataContext rootContext, DataContext ctx) { if (!IsCommand(ctx)) { return; } // Presenter类已经存在函数了, 不再生成 if (IsStringExists(rootContext, Command(ctx))) { return; } gen.PrintLine("public void ", Command(ctx), "()"); gen.PrintLine("{"); gen.In(); gen.PrintLine(); gen.Out(); gen.PrintLine("}"); }
// 一个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; } }
static void PropertyBody(CodeGenerator gen, DataContext rootContext, DataContext ctx) { switch (ctx.Type) { case WidgetType.ScrollRect: { gen.PrintLine("public Framework.ObservableCollection<int, ", ClassItemName(ctx), "> ", Property(ctx), " { get; set; }"); } break; case WidgetType.Text: case WidgetType.InputField: { if (HasPropertyChangedNotify(ctx)) { gen.PrintLine("public Action ", Event(ctx), ";"); } gen.PrintLine("public ", PropertyType(ctx), " ", Property(ctx)); gen.PrintLine("{"); gen.In(); gen.PrintLine("get"); gen.PrintLine("{"); gen.In(); gen.PrintLine("return _Model.", Property(ctx), ";"); gen.Out(); gen.PrintLine("}"); // get gen.PrintLine("set"); gen.PrintLine("{"); gen.In(); gen.PrintLine("_Model.", Property(ctx), " = value;"); gen.PrintLine(); // 有Presenter同步到View时, 调用通知 if (HasPropertyChangedNotify(ctx)) { gen.PrintLine("if ( ", Event(ctx), " != null )"); gen.PrintLine("{"); gen.In(); gen.PrintLine(Event(ctx), "();"); gen.Out(); gen.PrintLine("}"); // set } gen.Out(); gen.PrintLine("}"); // set gen.Out(); gen.PrintLine("}"); // Property } break; default: return; } gen.PrintLine(); }
public static void ClassBody(CodeGenerator gen, DataContext rootContext, List <DataContext> propContextList, gamedef.CodeGenModule module) { gen.PrintLine("// Generated by github.com/davyxu/cellorigin"); gen.PrintLine("using UnityEngine;"); gen.PrintLine("using UnityEngine.UI;"); gen.PrintLine("using System;"); gen.PrintLine(); gen.PrintLine("partial class ", ClassName(rootContext), " : Framework.BasePresenter"); gen.PrintLine("{"); gen.In(); gen.PrintLine(ModelTemplate.ClassName(rootContext), " _Model;"); gen.PrintLine(); // 网络Peer声明 foreach (gamedef.CodeGenPeer peer in module.Peer) { NetworkDeclare(gen, peer); } if (module.ModelGen != gamedef.ModelGenType.MGT_Manual) { // 属性声明 foreach (DataContext propContext in propContextList) { PropertyBody(gen, rootContext, propContext); } } gen.PrintLine("public void Init( )"); gen.PrintLine("{"); gen.In(); ModelInit(gen, rootContext, module); if (module.ModelGen != gamedef.ModelGenType.MGT_Manual) { foreach (DataContext propContext in propContextList) { PropertyInit(gen, rootContext, propContext); } } // 网络获取及消息绑定 foreach (gamedef.CodeGenPeer peer in module.Peer) { NetworkRegisterBody(gen, peer); } gen.Out(); gen.PrintLine("}"); // Bind gen.PrintLine(); foreach (DataContext propContext in propContextList) { CommandBody(gen, rootContext, propContext); } foreach (gamedef.CodeGenPeer peer in module.Peer) { foreach (string msgType in peer.RecvMessage) { NetworkCallbackBody(gen, rootContext, peer, msgType); } } gen.Out(); gen.PrintLine("}"); // Class }