예제 #1
0
 /// <summary>
 /// 默认生成init和clear方法。
 /// </summary>
 protected virtual void genMembers()
 {
     _initMethod  = genMethod(MemberAttributes.Public | MemberAttributes.Final, typeof(void), getInitMethodName());
     _clearMethod = genMethod(MemberAttributes.Public | MemberAttributes.Final, typeof(void), getClearMethodName());
     if (controllerType == CTRL_TYPE_LIST || controllerType == CTRL_TYPE_BUTTON_LIST)
     {
         //ItemPool类型
         CodeTypeDeclaration itemPoolType = new CodeTypeDeclaration();
         _type.Members.Add(itemPoolType);
         itemPoolType.CustomAttributes.Add(new CodeAttributeDeclaration(typeof(SerializableAttribute).Name));
         itemPoolType.Attributes = MemberAttributes.Public | MemberAttributes.Final;
         itemPoolType.IsClass    = true;
         itemPoolType.Name       = "ItemPool";
         //itemPoolType.BaseTypes.Add(new CodeTypeReference(typeof(ComponentPool<>).Name, new CodeTypeReference(listItemTypeName)));
         //构造器
         CodeConstructor itemPoolTypeConstructor = new CodeConstructor();
         itemPoolType.Members.Add(itemPoolTypeConstructor);
         itemPoolTypeConstructor.Attributes = MemberAttributes.Public | MemberAttributes.Final;
         itemPoolTypeConstructor.Parameters
         .append(typeof(Transform).Name, "root")
         .append(listItemTypeName, "origin");
         itemPoolTypeConstructor.BaseConstructorArgs
         .appendArg("root")
         .appendArg("origin");
         //ItemPool字段
         CodeMemberField itemPool = genField(itemPoolType.Name, FIELD_NAME_ITEM_POOL, false);
         itemPool.CustomAttributes.Add(new CodeAttributeDeclaration(typeof(SerializeField).Name));
         //onItemClick事件
         CodeMemberEvent onItemClick = null;
         if (controllerType == CTRL_TYPE_BUTTON_LIST)
         {
             onItemClick = genEvent(typeof(Action).Name, "onItemClick", Codo.type(listItemTypeName));
         }
         //itemClickCallback方法
         CodeMemberMethod itemClickCallback = null;
         if (controllerType == CTRL_TYPE_BUTTON_LIST)
         {
             itemClickCallback = genMethod(MemberAttributes.Private | MemberAttributes.Final, typeof(void), "itemClickCallback");
             itemClickCallback.Parameters.append(listItemTypeName, "item");
             itemClickCallback.Statements.append(new CodeConditionStatement(
                                                     Codo.op(Codo.This.getEvent(onItemClick.Name), CodeBinaryOperatorType.IdentityInequality, Codo.Null),
                                                     Codo.This.getEvent(onItemClick.Name).invoke(Codo.arg("item")).statement()));
         }
         //onItemCreate方法
         if (controllerType == CTRL_TYPE_BUTTON_LIST)
         {
             genMethod(MemberAttributes.Private | MemberAttributes.Final, typeof(void), "onItemCreate")
             .appendParam(listItemTypeName, "item")
             .appendStatement(Codo.arg("item").getMethod("autoReg").invoke())
             .appendStatement(Codo.arg("item").getEvent("onClick").attach(Codo.This.getMethod(itemClickCallback.Name)));
         }
         else
         {
             genPartialMethod("void", "onItemCreate", Codo.parameter(listItemTypeName, "item"));
         }
         //onItemRemove方法
         if (controllerType == CTRL_TYPE_BUTTON_LIST)
         {
             genMethod(MemberAttributes.Private | MemberAttributes.Final, typeof(void), "onItemRemove")
             .appendParam(listItemTypeName, "item")
             .appendStatement(Codo.arg("item").getMethod("autoUnreg").invoke())
             .appendStatement(Codo.arg("item").getEvent("onClick").remove(Codo.This.getMethod(itemClickCallback.Name)));
         }
         else
         {
             genPartialMethod("void", "onItemRemove", Codo.parameter(listItemTypeName, "item"));
         }
         //initPool方法
         var initPool = genMethod(MemberAttributes.Private | MemberAttributes.Final, typeof(void), METHOD_NAME_LIST_INIT_POOL);
         _initMethod.Statements.Add(Codo.This.getMethod(initPool.Name).invoke().statement());
         const string VAR_NAME_ITEM = "item";
         initPool.Statements.append(Codo.decVar(listItemTypeName, VAR_NAME_ITEM,
                                                string.IsNullOrEmpty(listItemTypeName) ?//有无指定脚本类型?
                                                Codo.This.getField(FIELD_NAME_ORIGIN) as CodeExpression :
                                                Codo.This.getField(FIELD_NAME_ORIGIN).getMethod(NAME_OF_ADDCOMPO, Codo.type(listItemTypeName)).invoke()));
         initPool.Statements.Add(Codo.This.getField(FIELD_NAME_ORIGIN).getProp(NAME_OF_GAMEOBJECT).getMethod(NAME_OF_SET_ACTIVE).invoke(Codo.False));
         initPool.Statements.Add(Codo.assign(Codo.This.getField(itemPool.Name),
                                             Codo.New(itemPoolType.Name, Codo.This.getProp("transform"), Codo.Var(VAR_NAME_ITEM))));
         initPool.Statements.Add(Codo.This.getField(itemPool.Name).getEvent("onCreate").attach(Codo.This.getMethod("onItemCreate")));
         initPool.Statements.Add(Codo.This.getField(itemPool.Name).getEvent("onRemove").attach(Codo.This.getMethod("onItemRemove")));
         //setCount方法
         CodeMemberMethod setCount = genMethod(MemberAttributes.Public | MemberAttributes.Final, typeof(void), "setCount");
         setCount.Parameters.append(typeof(int), "count");
         setCount.Statements.Add(Codo.This.getField(itemPool.Name).getMethod("setCount").invoke(Codo.arg("count")));
         //indexer
         CodeMemberProperty indexer = genIndexer(Codo.type(listItemTypeName));
         indexer.Parameters.append(typeof(int), "index");
         indexer.HasGet = true;
         indexer.GetStatements.Add(Codo.Return(Codo.This.getField(itemPool.Name).index(Codo.arg("index"))));
         //indexOf方法
         CodeMemberMethod indexOf = genMethod(MemberAttributes.Public | MemberAttributes.Final, typeof(int), "indexOf");
         indexOf.Parameters.append(listItemTypeName, "item");
         indexOf.Statements.append(Codo.Return(Codo.This.getField(itemPool.Name).getMethod("indexOf").invoke(Codo.arg("item"))));
     }
 }