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>()); } } }
/// <summary> /// INPUT_VALUE: mTargetBlockView /// NEXT_STATEMENT: the last block view of the target statement block views chain /// </summary> public BlockView GetChildLastBlockView() { if (ConnectionType == Define.EConnection.InputValue) { return(mTargetBlockView); } BlockView nextView = mTargetBlockView; while (nextView != null) { ConnectionView nextCon = nextView.GetConnectionView(Define.EConnection.NextStatement); if (nextCon == null) { break; } nextView = nextCon.TargetBlockView; } return(nextView); }
/// <summary> /// Get the connection view of connectionType /// output, previous, next connection /// </summary> public ConnectionView GetConnectionView(Define.EConnection connectionType) { int i = 0; while (i < Childs.Count) { ConnectionView view = Childs[i] as ConnectionView; if (view == null) { break; } if (view.ConnectionType == connectionType) { return(view); } i++; } //Debug.LogFormat("<color=red>Can't find the {0} connection view in block view of {1}.</color>", connectionType, BlockType); return(null); }
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(); }
public MemorySafeConnectionObserver(ConnectionView viewRef) { mViewRef = viewRef; }
protected override Vector2 CalculateSize() { switch (m_ConnectionInputViewType) { case ConnectionInputViewType.Value: { //width is not concerned Vector2 size = new Vector2(BlockViewSettings.Get().ValueConnectPointRect.width, 0); if (mTargetBlockView == null) { size.y = BlockViewSettings.Get().ContentHeight; } else { size.y = mTargetBlockView.Height; } return(size); } case ConnectionInputViewType.ValueSlot: { if (mTargetBlockView == null) { return(new Vector2(BlockViewSettings.Get().MinUnitWidth, BlockViewSettings.Get().ContentHeight)); } Vector2 size = new Vector2(BlockViewSettings.Get().ValueConnectPointRect.width + mTargetBlockView.Width, mTargetBlockView.Height); return(size); } case ConnectionInputViewType.Statement: { if (mTargetBlockView == null) { return(new Vector2(70, BlockViewSettings.Get().ContentHeight + BlockViewSettings.Get().ContentMargin.bottom)); } // calculate the height by adding all child statement blocks' height Vector2 size = new Vector2(70, 0); bool addConnectPointSpace = true; BlockView nextView = mTargetBlockView; while (nextView != null) { size.y += nextView.Height; ConnectionView nextCon = nextView.GetConnectionView(Define.EConnection.NextStatement); if (nextCon == null) { addConnectPointSpace = false; break; } nextView = nextCon.TargetBlockView; } if (addConnectPointSpace) { size.y += BlockViewSettings.Get().StatementConnectPointRect.height; } size.y += BlockViewSettings.Get().ContentMargin.bottom; return(size); } } return(Vector2.zero); }
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); }
/// <summary> /// update layout of this view and all views effected /// </summary> public void UpdateLayout(Vector2 startPos) { Vector2 newSize = CalculateSize(); bool changePos = XY != startPos; bool changeSize = Size != newSize; if (changePos) { XY = startPos; } if (changeSize) { Size = newSize; } switch (Type) { case ViewType.Field: case ViewType.Input: case ViewType.ConnectionInput: case ViewType.LineGroup: { if (m_Next == null /*|| (!changePos && !changeSize)*/) { //reach the last child, or no change in current hierarchy, update it's parent view m_Parent.UpdateLayout(m_Parent.SiblingIndex == 0 ? m_Parent.HeaderXY : m_Parent.XY); } else { //update next if (Type != ViewType.LineGroup) { // same line startPos.x += Size.x + BlockViewSettings.Get().ContentSpace.x; } else { // start a new line startPos.y -= Size.y + BlockViewSettings.Get().ContentSpace.y; } BaseView topmostChild = m_Next.GetTopmostChild(); if (topmostChild != m_Next) { //need to update from its topmost child m_Next.XY = startPos; topmostChild.UpdateLayout(topmostChild.HeaderXY); } else { m_Next.UpdateLayout(startPos); } } // update connection location in ConnectionDB if (Type == ViewType.ConnectionInput) { ConnectionView connectionView = (ConnectionView)this; if (connectionView != null) { connectionView.OnXYUpdated(); } } break; } case ViewType.Connection: case ViewType.Block: { //no need to update its m_Next, as it is handled by Unity's Transform autolayout //update its parent directly if (m_Parent != null) { m_Parent.UpdateLayout(m_Parent.SiblingIndex == 0 ? m_Parent.HeaderXY : m_Parent.XY); } // update connection location in ConnectionDB if (Type == ViewType.Block) { BlockView blockView = (BlockView)this; if (blockView != null) { foreach (var view in blockView.Childs) { if (view.Type == ViewType.Connection) { view.OnXYUpdated(); } } } } break; } } }