/// <summary> /// update all child inputviews to align right /// </summary> /// <param name="width"></param> public void UpdateAlignRight(float width) { if (Mathf.Approximately(this.Width, width)) { return; } this.Width = width; ConnectionInputView conView = ((InputView)LastChild).GetConnectionView(); if (conView != null && conView.ConnectionInputViewType == ConnectionInputViewType.Statement) { conView.Width = width - (LastChild.XY.x + conView.XY.x); } else { float startX = this.XY.x + width; for (int i = Childs.Count - 1; i >= 0; i--) { InputView inputView = Childs[i] as InputView; if (i < Childs.Count - 1) { startX -= BlockViewSettings.Get().ContentSpace.x; } startX -= inputView.Width; inputView.XY = new Vector2(startX, inputView.XY.y); } } }
/// <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); }
/// <summary> /// Get the connection view of input /// </summary> public ConnectionView GetInputConnectionView(int inputIndex) { InputView inputView = GetInputView(inputIndex); if (inputView != null) { return(inputView.GetConnectionView()); } return(null); }
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); }
/// <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); } } }