示例#1
0
        /// <summary>
        /// Get the index's field view, index start from 0
        /// </summary>
        public FieldView GetFieldView(int index)
        {
            int fieldCounter = 0;
            int groupCounter = 0;

            while (groupCounter < Childs.Count)
            {
                LineGroupView groupView = Childs[groupCounter] as LineGroupView;
                groupCounter++;
                if (groupView == null)
                {
                    continue;
                }

                int inputCounter = 0;
                while (inputCounter < groupView.Childs.Count)
                {
                    InputView inputView = groupView.Childs[inputCounter] as InputView;
                    //the last child view of inputView is ConnectionInputView
                    if (fieldCounter + inputView.Childs.Count - 1 > index)
                    {
                        return(inputView.Childs[index - fieldCounter] as FieldView);
                    }

                    fieldCounter += inputView.Childs.Count - 1;
                    inputCounter++;
                }
            }

            //Debug.LogFormat("<color=red>Can't find the {0}th field view in block view of {1}.</color>", index, BlockType);
            return(null);
        }
示例#2
0
        public void UnBindModel()
        {
            //unbind input and connections
            int inputIndex = 0;

            foreach (BaseView view in Childs)
            {
                if (view.Type == ViewType.Connection)
                {
                    ((ConnectionView)view).UnBindModel();
                }
                else if (view.Type == ViewType.LineGroup)
                {
                    LineGroupView groupView = view as LineGroupView;
                    foreach (var inputView in groupView.Childs)
                    {
                        ((InputView)inputView).UnBindModel();
                        inputIndex++;
                    }
                }
            }

            BlocklyUI.WorkspaceView.RemoveBlockView(this);
            mBlock.RemoveObserver(mBlockObserver);
            mBlock = null;
        }
示例#3
0
        protected internal override void OnXYUpdated()
        {
            if (InToolbox)
            {
                return;
            }

            mBlock.XY = XY;
            //update all connection's location
            foreach (var view in Childs)
            {
                if (view.Type == ViewType.Connection)
                {
                    view.OnXYUpdated();
                }
                else if (view.Type == ViewType.LineGroup)
                {
                    LineGroupView groupView = view as LineGroupView;
                    foreach (var inputView in groupView.Childs)
                    {
                        ConnectionInputView conInputView = ((InputView)inputView).GetConnectionView();
                        if (conInputView != null)
                        {
                            conInputView.OnXYUpdated();
                        }
                    }
                }
            }
        }
示例#4
0
        /// <summary>
        /// Dispose the block model and block view
        /// </summary>
        public void Dispose()
        {
            foreach (BaseView view in Childs)
            {
                if (view.Type == ViewType.Connection)
                {
                    if (((ConnectionView)view).TargetBlockView != null)
                    {
                        ((ConnectionView)view).TargetBlockView.Dispose();
                    }
                }
                else if (view.Type == ViewType.LineGroup)
                {
                    LineGroupView groupView = view as LineGroupView;
                    foreach (var inputView in groupView.Childs)
                    {
                        if (((InputView)inputView).HasConnection && ((InputView)inputView).GetConnectionView().TargetBlockView != null)
                        {
                            ((InputView)inputView).GetConnectionView().TargetBlockView.Dispose();
                        }
                    }
                }
            }

            Block model = mBlock;

            UnBindModel();
            GameObject.Destroy(this.gameObject);
            model.Dispose();
        }
示例#5
0
        public static LineGroupView BuildNewLineGroup(BlockView blockView)
        {
            GameObject    groupObj   = new GameObject("LineGroup");
            RectTransform groupTrans = groupObj.AddComponent <RectTransform>();

            groupTrans.SetParent(blockView.transform);
            UniformRectTransform(groupTrans);

            LineGroupView groupView = AddViewComponent <LineGroupView>(groupObj);

            blockView.AddChild(groupView);

            return(groupView);
        }
示例#6
0
        protected override Vector2 CalculateSize()
        {
            bool alignRight = false;

            //accumulate all child lineGroups' size
            Vector2 size = Vector2.zero;

            for (int i = 0; i < Childs.Count; i++)
            {
                LineGroupView groupView = Childs[i] as LineGroupView;
                if (groupView != null)
                {
                    size.x  = Mathf.Max(size.x, groupView.Size.x);
                    size.y += groupView.Size.y;
                    if (i < Childs.Count - 1)
                    {
                        size.y += BlockViewSettings.Get().ContentSpace.y;
                    }

                    if (((InputView)groupView.LastChild).AlignRight)
                    {
                        alignRight = true;
                    }
                }
            }

            //collect all child lineGroups' vertices for custom drawing
            List <Vector4> dimensions = new List <Vector4>();

            for (int i = 0; i < Childs.Count; i++)
            {
                LineGroupView groupView = Childs[i] as LineGroupView;
                if (groupView != null)
                {
                    if (alignRight)
                    {
                        groupView.UpdateAlignRight(size.x);
                    }

                    //linegroup's anchor and pivot both are top-left
                    Vector2 drawSize = groupView.GetDrawSize();
                    dimensions.Add(new Vector4(groupView.XY.x, groupView.XY.y - drawSize.y, groupView.XY.x + drawSize.x, groupView.XY.y));
                }
            }

            //update image mesh
            ((CustomMeshImage)m_BgImages[0]).SetDrawDimensions(dimensions.ToArray());
            return(size);
        }
示例#7
0
        public void BindModel(Block block)
        {
            if (mBlock == block)
            {
                return;
            }
            if (mBlock != null)
            {
                UnBindModel();
            }

            mBlock = block;
            BlocklyUI.WorkspaceView.AddBlockView(this);

            mBlockObserver = new MemorySafeBlockObserver(this);
            mBlock.AddObserver(mBlockObserver);

            //bind input and connections
            int inputIndex = 0;

            foreach (BaseView view in Childs)
            {
                if (view.Type == ViewType.Connection)
                {
                    ConnectionView conView = view as ConnectionView;
                    conView.BindModel(mBlock.GetFirstClassConnection(conView.ConnectionType));
                }
                else if (view.Type == ViewType.LineGroup)
                {
                    LineGroupView groupView = view as LineGroupView;
                    foreach (var inputView in groupView.Childs)
                    {
                        ((InputView)inputView).BindModel(mBlock.InputList[inputIndex]);
                        inputIndex++;
                    }
                }
            }

            RegisterUIEvents();

            foreach (Transform child in transform)
            {
                if (child.name.ToLower().StartsWith("block_count"))
                {
                    SetCountText(child.GetComponentInChildren <Text>());
                }
            }
        }
示例#8
0
        public List <InputView> GetInputViews()
        {
            List <InputView> inputViews = new List <InputView>();
            int i = 0;

            while (i < Childs.Count)
            {
                LineGroupView view = Childs[i] as LineGroupView;
                if (view != null && view.HasChild())
                {
                    inputViews.AddRange(view.Childs.Select(v => v as InputView));
                }
                i++;
            }
            return(inputViews);
        }
示例#9
0
        /// <summary>
        /// Get the index's lineGroup child view, index start from 0
        /// </summary>
        public LineGroupView GetLineGroup(int index)
        {
            int i = 0;

            while (i < Childs.Count)
            {
                LineGroupView view = Childs[i] as LineGroupView;
                if (view != null)
                {
                    if (i + index < Childs.Count)
                    {
                        return(Childs[i + index] as LineGroupView);
                    }
                    break;
                }
                i++;
            }
            //Debug.LogFormat("<color=red>Can't find the {0}th lineGroup in block view of {1}.</color>", index, BlockType);
            return(null);
        }
示例#10
0
        /// <summary>
        /// Dispose the block model and block view
        /// </summary>
        public void Dispose()
        {
            foreach (BaseView view in Childs)
            {
                if (view.Type == ViewType.Connection)
                {
                    if (((ConnectionView)view).TargetBlockView != null)
                    {
                        ((ConnectionView)view).TargetBlockView.Dispose();
                    }
                }
                else if (view.Type == ViewType.LineGroup)
                {
                    LineGroupView groupView = view as LineGroupView;
                    foreach (var inputView in groupView.Childs)
                    {
                        if (((InputView)inputView).HasConnection && ((InputView)inputView).GetConnectionView().TargetBlockView != null)
                        {
                            ((InputView)inputView).GetConnectionView().TargetBlockView.Dispose();
                        }
                    }
                }
            }

            //Update available blocks
            if (Block.blocksAvailable.ContainsKey(BlockType))
            {
                Block.blocksAvailable[BlockType]++;
            }
            else
            {
                Block.blocksAvailable[BlockType] = 1;
            }
            UpdateCount();

            Block model = mBlock;

            UnBindModel();
            GameObject.Destroy(this.gameObject, 0.1f);
            model.Dispose();
        }
示例#11
0
        public void BindModel(Block block)
        {
            if (mBlock == block)
            {
                return;
            }
            if (mBlock != null)
            {
                UnBindModel();
            }

            mBlock = block;
            BlocklyUI.WorkspaceView.AddBlockView(this);

            mBlockObserver = new MemorySafeBlockObserver(this);
            mBlock.AddObserver(mBlockObserver);

            //bind input and connections
            int inputIndex = 0;

            foreach (BaseView view in Childs)
            {
                if (view.Type == ViewType.Connection)
                {
                    ConnectionView conView = view as ConnectionView;
                    conView.BindModel(mBlock.GetFirstClassConnection(conView.ConnectionType));
                }
                else if (view.Type == ViewType.LineGroup)
                {
                    LineGroupView groupView = view as LineGroupView;
                    foreach (var inputView in groupView.Childs)
                    {
                        ((InputView)inputView).BindModel(mBlock.InputList[inputIndex]);
                        inputIndex++;
                    }
                }
            }

            RegisterUIEvents();
        }
示例#12
0
        /// <summary>
        /// Get the index's input view, index start from 0
        /// </summary>
        public InputView GetInputView(int index)
        {
            int inputCounter = 0;
            int groupCounter = 0;

            while (groupCounter < Childs.Count)
            {
                LineGroupView view = Childs[groupCounter] as LineGroupView;
                groupCounter++;
                if (view == null)
                {
                    continue;
                }

                if (inputCounter + view.Childs.Count > index)
                {
                    return(view.Childs[index - inputCounter] as InputView);
                }

                inputCounter += view.Childs.Count;
            }
            //Debug.LogFormat("<color=red>Can't find the {0}th input view in block view of {1}.</color>", index, BlockType);
            return(null);
        }
示例#13
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);
        }
示例#14
0
        public static InputView BuildInputView(Input input, LineGroupView groupView, BlockView blockView)
        {
            GameObject inputPrefab;
            ConnectionInputViewType viewType;

            if (input.Type == Define.EConnection.NextStatement)
            {
                inputPrefab = BlockViewSettings.Get().PrefabInputStatement;
                viewType    = ConnectionInputViewType.Statement;
            }
            else if (input.SourceBlock.InputList.Count > 1 && input.SourceBlock.GetInputsInline())
            {
                inputPrefab = BlockViewSettings.Get().PrefabInputValueSlot;
                viewType    = ConnectionInputViewType.ValueSlot;
            }
            else
            {
                inputPrefab = BlockViewSettings.Get().PrefabInputValue;
                viewType    = ConnectionInputViewType.Value;
            }

            GameObject inputObj = GameObject.Instantiate(inputPrefab);

            inputObj.name = "Input_" + (!string.IsNullOrEmpty(input.Name) ? input.Name : "");
            RectTransform inputTrans = inputObj.GetComponent <RectTransform>();

            inputTrans.SetParent(groupView.transform, false);
            UniformRectTransform(inputTrans);

            Transform conInputTrans = inputTrans.GetChild(0);

            InputView inputView = AddViewComponent <InputView>(inputObj);

            inputView.AlignRight = input.Align == Define.EAlign.Right;

            // build child field views of this input view
            List <Field> fields = input.FieldRow;

            foreach (Field field in fields)
            {
                FieldView fieldView = BuildFieldView(field);
                inputView.AddChild(fieldView);
                RectTransform fieldTrans = fieldView.GetComponent <RectTransform>();
                UniformRectTransform(fieldTrans);
            }

            if (input.Type == Define.EConnection.DummyInput)
            {
                //dummy input doesn't need to have a connection point
                GameObject.DestroyImmediate(conInputTrans.gameObject);
            }
            else
            {
                ConnectionInputView conInputView = AddViewComponent <ConnectionInputView>(conInputTrans.gameObject);
                conInputView.ConnectionType          = input.Type;
                conInputView.ConnectionInputViewType = viewType;
                inputView.AddChild(conInputView);

                conInputView.BgImage.raycastTarget = false;
                if (viewType != ConnectionInputViewType.ValueSlot)
                {
                    blockView.AddBgImage(conInputView.BgImage);
                }
            }

            return(inputView);
        }
示例#15
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);
                }
            }
        }