Exemplo n.º 1
0
        public static void CreateUserScript(ViewNode node, bool replaceIfExists)
        {
            var rn = Environment.NewLine;

            LoadTemplates();

            //get list of UI controls (not views and not Buttons)
            var selectables = node.ViewInfo.Members.Values.Where(m => m.Component is Selectable sel && !(sel is Button));

            //get template
            var res = GetTemplate("ViewClassUser");

            if (selectables.Any())
            {
                res.Add("#METHODS#", GetTemplate("OnChanged").ToString());
            }

            //prepare Set methods in OnBuild
            //get list of UI controls (not views and not Buttons)
            var controls = node.ViewInfo.Members.Values.Where(m => m.Component != null && !(m.Component is Button));

            foreach (var sel in controls)
            {
                res.Add("#OnBuildBody#", $"            //Set({sel.Name}, default);");
            }

            var view = node.rt.GetComponent <BaseView>();

            if (view != null && !string.IsNullOrWhiteSpace(view.SignatureOfBuildMethod))
            {
                res.Add("#ALLOWEDDATA#", $"//Data: {view.SignatureOfBuildMethod}");
            }

            //prepare Subscribe methods in Start
            //get list of buttons w/o bindings
            var buttons = node.ViewInfo.Members.Values.Where(m => (m.Component is Button) && !node.Outputs.Any(p => p.ValueType == typeof(ActionOutputPort) && p.fieldName == m.Name));

            foreach (var bt in buttons)
            {
                res.Add("#StartBody#", $"            Subscribe({bt.Name}, () => {{;}});");
            }

            //replace class name
            res.Add("#CLASSNAME#", node.name);
            res.Add("#SCENENAME#", GetSceneName(node.rt.gameObject.scene));

            //save
            CreateNeededFolders(node.rt.gameObject.scene);

            var filePath = Path.Combine(ScriptsFolder, node.name + ".cs");

            if (!File.Exists(filePath) || replaceIfExists)
            {
                var scriptText = CSharpPreparer.Prepare(res.ToString());
                File.WriteAllText(filePath, scriptText);
                AssetDatabase.Refresh();
            }
        }
Exemplo n.º 2
0
        public static void CreateScript(ViewNode node, bool loadTemplates = true)
        {
            if (node.rt == null)
            {
                return;
            }

            if (loadTemplates)
            {
                LoadTemplates();
            }

            var rn             = Environment.NewLine;
            var res            = GetTemplate("ViewClass");
            var fieldsTemplate = GetTemplate("Fields");
            var sceneName      = GetSceneName(node.rt.gameObject.scene);

            node.GrabInfoAboutView(node.rt);

            if (node.ViewInfo == null)
            {
                return;
            }

            var  view      = node.rt.GetComponent <BaseView>();
            bool wasFields = false;

            //fields
            foreach (var item in node.ViewInfo.Members.Values)
            {
                if (item.Type == node.name)
                {
                    continue;//myself
                }
                if ((item.FieldInfo != null && item.IsAutogenerated) ||
                    (item.Binded != null && item.IsAutogenerated) ||
                    (item.Component != null))
                {
                    wasFields = true;
                    fieldsTemplate.Add("#FIELDS#", $"[AutoGenerated, SerializeField, HideInInspector] {item.Type} {item.Name} = default;");
                    res.Add("#INIT#", $"SubscribeOnChanged({item.Name});");
                }
            }

            //links
            foreach (var port in node.Outputs.Where(p => p.ValueType == typeof(ActionOutputPort) || p.ValueType == typeof(GesturePort) || p.ValueType == typeof(EventPort)))
            {
                foreach (var linked in port.GetConnections())
                {
                    //arg
                    var arg = "";
                    if (linked.fieldName == nameof(BaseView.Show) ||
                        linked.fieldName == nameof(BaseView.ShowOrClose) ||
                        linked.fieldName == nameof(BaseView.Reopen) ||
                        linked.fieldName == nameof(BaseView.ReopenAnimated) ||
                        linked.fieldName == nameof(BaseView.BuildAndShow))
                    {
                        arg = nameof(BaseView.OwnerForChild);
                    }

                    //obj name
                    var objName = linked.node.name;

                    if (linked.node.name == node.name)
                    {
                        objName = "this";
                    }

                    if (linked.node is GlobalNode)
                    {
                        objName = nameof(UIManager);
                    }

                    //my name
                    var myName = port.fieldName;
                    if (port.ValueType == typeof(GesturePort))
                    {
                        myName = nameof(Gesture) + "." + myName;
                    }

                    //is event?
                    if (port.ValueType == typeof(EventPort))
                    {
                        //subscribe event
                        res.Add("#INIT#", $"{myName} += {objName}.{linked.fieldName};");
                    }
                    else
                    {
                        //subscribe
                        res.Add("#INIT#", $"Subscribe({myName}, () => {objName}.{linked.fieldName}({arg}));");
                    }
                }
            }

            //method Build
            var signature = view == null ? "" : view.SignatureOfBuildMethod;

            CreateBuildMethod(node.name, signature, res);

            //prepare
            if (wasFields)
            {
                res.Add("#FIELDS#", fieldsTemplate.ToString());
            }

            //replace class name
            res.Add("#CLASSNAME#", node.name);
            res.Add("#SCENENAME#", sceneName);

            //save
            CreateNeededFolders(node.rt.gameObject.scene);

            var filePath   = Path.Combine(AutoScriptsFolder, node.name + ".cs");
            var scriptText = CSharpPreparer.Prepare(res.ToString());

            File.WriteAllText(filePath, scriptText);

            if (loadTemplates)
            {
                AssetDatabase.Refresh();
            }
        }