Exemplo n.º 1
0
        public static GameObject BuildBlockView(Block block)
        {
            GameObject blockPrefab = BlockViewSettings.Get().PrefabRoot;

            if (block.OutputConnection != null)
            {
                blockPrefab = BlockViewSettings.Get().PrefabRootOutput;
            }
            else if (block.PreviousConnection != null && block.NextConnection != null)
            {
                blockPrefab = BlockViewSettings.Get().PrefabRootPrevNext;
            }
            else if (block.PreviousConnection != null)
            {
                blockPrefab = BlockViewSettings.Get().PrefabRootPrev;
            }

            GameObject blockObj = GameObject.Instantiate(blockPrefab);

            blockObj.name = "Block_" + block.Type;
            RectTransform blockTrans = blockObj.GetComponent <RectTransform>();

            UniformRectTransform(blockTrans);

            //blockview script
            BlockView blockView = AddViewComponent <BlockView>(blockObj);

            //block view's background image
            blockView.AddBgImage(blockObj.GetComponent <Image>());

            //block view's childs: connection, lineGroup
            Transform mutatorEntry = null;

            foreach (Transform child in blockTrans)
            {
                string childName = child.name.ToLower();
                if (childName.StartsWith("connection"))
                {
                    //connection node views
                    ConnectionView conView = AddViewComponent <ConnectionView>(child.gameObject);
                    blockView.AddChild(conView, 0);

                    if (childName.EndsWith("output"))
                    {
                        conView.ConnectionType = Define.EConnection.OutputValue;
                    }
                    else if (childName.EndsWith("prev"))
                    {
                        conView.ConnectionType = Define.EConnection.PrevStatement;
                    }
                    else if (childName.EndsWith("next"))
                    {
                        conView.ConnectionType = Define.EConnection.NextStatement;
                    }

                    //connection node view background color
                    Image image = child.GetComponent <Image>();
                    if (image != null)
                    {
                        blockView.AddBgImage(image);
                    }
                }
                else if (childName.Equals("linegroup"))
                {
                    UniformRectTransform(child as RectTransform);
                    //lineGroup view
                    LineGroupView groupView = AddViewComponent <LineGroupView>(child.gameObject);
                    blockView.AddChild(groupView);
                }
                else if (childName.Equals("mutator_entry"))
                {
                    mutatorEntry = child;
                }
                else if (childName.StartsWith("block_count"))
                {
                    blockView.SetCountText(child.GetComponentInChildren <Text>());
                }
            }

            //check if has mutator entry
            if (mutatorEntry == null)
            {
                throw new Exception("There should be a mutator_entry image under block view prefab");
            }
            if (block.Mutator == null || !block.Mutator.NeedEditor)
            {
                GameObject.DestroyImmediate(mutatorEntry.gameObject);
            }
            else
            {
                blockView.GetLineGroup(0).ReservedStartX = ((RectTransform)mutatorEntry).rect.width + BlockViewSettings.Get().ContentSpace.x;
            }

            //block view's input views, including field's views
            BuildInputViews(block, blockView);

            //block view's layout, build from the very first field
            blockView.BuildLayout();

            //default background color
            blockView.ChangeBgColor(Color.blue);

            return(blockObj);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Build Input views, including field views, and calucate block size
        /// including dispose old input views with disposed input models
        /// </summary>
        public static void BuildInputViews(Block block, BlockView blockView)
        {
            bool          inputsInline = block.GetInputsInline();
            LineGroupView groupView    = blockView.GetLineGroup(0);

            //1. check dispose old inputviews
            List <InputView> oldInputViews = blockView.GetInputViews();

            foreach (InputView view in oldInputViews)
            {
                if (!block.InputList.Contains(view.Input))
                {
                    view.UnBindModel();
                    GameObject.DestroyImmediate(view.gameObject);
                }
            }

            //2. build new inputviews
            for (int i = 0; i < block.InputList.Count; i++)
            {
                Input input = block.InputList[i];

                // build new line group view
                bool newLine = i > 0 &&
                               (!inputsInline ||
                                input.Connection != null && input.Connection.Type == Define.EConnection.NextStatement);
                if (newLine)
                {
                    groupView = blockView.GetLineGroup(i);
                    if (groupView == null)
                    {
                        groupView = BuildNewLineGroup(blockView);
                    }
                }

                // build input view
                bool needBuild = true;
                foreach (InputView view in oldInputViews)
                {
                    if (view.Input == input)
                    {
                        needBuild = false;
                        if (view.Parent == null)
                        {
                            //bug fixed: this view may be removed from parent by removing its old previous sibling
                            groupView.AddChild(view);
                        }
                        else if (view.Parent != groupView)
                        {
                            //bug fixed: need to remove from the original groupview, maybe it's different now
                            view.Parent.RemoveChild(view);
                            groupView.AddChild(view);
                        }
                        break;
                    }
                }
                if (needBuild)
                {
                    InputView inputView = BuildInputView(input, groupView, blockView);
                    groupView.AddChild(inputView, newLine ? 0 : i);
                    if (Application.isPlaying)
                    {
                        inputView.BindModel(input);
                    }
                    else
                    {
                        //static build block only needs to bind fields' model for initializing fields' properties
                        for (int j = 0; j < inputView.Childs.Count; j++)
                        {
                            FieldView fieldView = inputView.Childs[j] as FieldView;
                            if (fieldView != null)
                            {
                                fieldView.BindModel(input.FieldRow[j]);
                            }
                        }
                    }
                }
            }

            //3. dispose group view without children
            for (int i = blockView.Childs.Count - 1; i >= 0; i--)
            {
                BaseView view = blockView.Childs[i];
                if (view.Type == ViewType.LineGroup && view.Childs.Count == 0)
                {
                    GameObject.DestroyImmediate(view.gameObject);
                }
            }
        }