public void RefreshBlocks(BlockModel.BlockStatus _status)
 {
     CustomElementModel.CustomElementStatus statusElement = _status.Access(CustomElementModel.CustomElementStatus.NAME) as CustomElementModel.CustomElementStatus;
     foreach (BlockModel.Block block in _status.blocks)
     {
         addBlock(block, statusElement);
     }
 }
 public void RefreshAddUnit(PageModel.PageStatus _status, PageModel.Unit _unit)
 {
     BlockModel.BlockStatus statusBlock = _status.Access(BlockModel.BlockStatus.NAME) as BlockModel.BlockStatus;
     CustomElementModel.CustomElementStatus statusElement = statusBlock.Access(CustomElementModel.CustomElementStatus.NAME) as CustomElementModel.CustomElementStatus;
     BlockModel.Block block = statusBlock.blocks.Find((_item) => {
         return(_item.method.Equals(_unit.block));
     });
     addBlock(_unit.uuid, block, _unit.variants, statusElement);
 }
 public void RefreshAddUnits(PageModel.PageStatus _status, List <PageModel.Unit> _units)
 {
     destroyActiveChildren(uiWorkbench.templateExpression.parent.gameObject);
     BlockModel.BlockStatus statusBlock = _status.Access(BlockModel.BlockStatus.NAME) as BlockModel.BlockStatus;
     CustomElementModel.CustomElementStatus statusElement = statusBlock.Access(CustomElementModel.CustomElementStatus.NAME) as CustomElementModel.CustomElementStatus;
     foreach (PageModel.Unit unit in _units)
     {
         BlockModel.Block block = statusBlock.blocks.Find((_item) => {
             return(_item.method.Equals(unit.block));
         });
         addBlock(unit.uuid, block, unit.variants, statusElement);
     }
 }
        private void addBlock(BlockModel.Block _block, CustomElementModel.CustomElementStatus _elementStatus)
        {
            GameObject clone = GameObject.Instantiate(uiBlockly.tsTempalteBlock.gameObject);

            clone.transform.SetParent(uiBlockly.tsTempalteBlock.parent);
            clone.transform.localScale = Vector3.one;
            clone.SetActive(true);
            clone.name = _block.method;

            BlockBuilder.BuildBlock(_block, clone, _elementStatus);

            Transform tsSection = uiBlockly.tsTempalteBlock.parent.Find(_block.ns);

            if (null != tsSection)
            {
                clone.transform.SetSiblingIndex(tsSection.GetSiblingIndex());
            }
        }
        public static void BuildBlock(BlockModel.Block _block, GameObject _obj, CustomElementModel.CustomElementStatus _elementStatus)
        {
            Transform  tsExpression    = _obj.transform.Find("expression");
            Color      color           = FacadeUtility.HexToColor(_block.color);
            GameObject expressionClone = null;

            int line = _block.elements.Count;

            expressionClone = cloneExpression(tsExpression.gameObject, color, line > 1 ? imgBlockHead : imgBlockSingle, line == 1 ? Symbol.Single : Symbol.Left);
            decorateElements(expressionClone, _block.elements[0], new Dictionary <string, string>(), _elementStatus);

            for (int i = 1; i < _block.elements.Count; ++i)
            {
                Symbol symbol = Symbol.Blank;
                Sprite sprite = imgBlockBlank;
                // blank
                cloneExpression(tsExpression.gameObject, color, sprite, symbol);

                if (i == line - 1)
                {
                    // last one
                    symbol = Symbol.Right;
                    sprite = imgBlockFoot;
                }
                else
                {
                    symbol = Symbol.Middle;
                    sprite = imgBlockMiddle;
                }

                expressionClone = cloneExpression(tsExpression.gameObject, color, sprite, symbol);
                decorateElements(expressionClone, _block.elements[i], new Dictionary <string, string>(), _elementStatus);
            }

            Transform tsDrag = _obj.transform.Find("#drag");

            tsDrag.SetAsLastSibling();

            DragBlock drag = tsDrag.GetComponent <DragBlock>();

            drag.method = _block.method;
        }
        private void addBlock(string _uuid, BlockModel.Block _block, Dictionary <string, string> _variants, CustomElementModel.CustomElementStatus _elementStatus)
        {
            if (null == _block)
            {
                return;
            }

            BlockBuilder.Symbol symbol = BlockBuilder.LineToSymbol(_block, 0);
            addExpression(_uuid, _block, _variants, _elementStatus, _block.elements[0], symbol);
            for (int line = 1; line < _block.elements.Count; ++line)
            {
                addExpression(_uuid, _block, _variants, _elementStatus, new List <BlockModel.Element>(0), BlockBuilder.Symbol.Blank);
                symbol = BlockBuilder.LineToSymbol(_block, line);
                addExpression(_uuid, _block, _variants, _elementStatus, _block.elements[line], symbol);
            }
        }
        private void addExpression(string _uuid, BlockModel.Block _block, Dictionary <string, string> _variants, CustomElementModel.CustomElementStatus _elementStatus,
                                   List <BlockModel.Element> _elements, BlockBuilder.Symbol _symbol)
        {
            GameObject clone = GameObject.Instantiate(uiWorkbench.templateExpression.gameObject);

            clone.transform.SetParent(uiWorkbench.templateExpression.parent);
            clone.transform.localScale = Vector3.one;
            clone.SetActive(true);
            clone.name = _uuid;
            BlockBuilder.BuildExpression(_elements, _variants, clone, _elementStatus, _symbol, _block.color);
        }
        private static GameObject buildCustomElement(string _type, string _value, Dictionary <string, string> _variants, CustomElementModel.CustomElementStatus _elementStatus)
        {
            CustomElementModel.CustomElement element = _elementStatus.elements.Find((_item) =>
            {
                return(_item.name.Equals(_type));
            });

            if (null == element)
            {
                return(null);
            }

            GameObject clone = null;

            if (element.type.Equals("dropdown"))
            {
                clone = GameObject.Instantiate(templateElementDropdown.gameObject);
                Dropdown dd = clone.GetComponent <Dropdown>();
                dd.options.Clear();
                foreach (string value in element.values)
                {
                    dd.options.Add(new Dropdown.OptionData(value));
                }

                if (element.values.Count > 0)
                {
                    dd.captionText.text = element.values[0];
                }
                FacadeUtility.StretchDropdown(dd);
                dd.onValueChanged.AddListener((_ddValue) =>
                {
                    if (null != OnDropdownUpdated)
                    {
                        OnDropdownUpdated(clone.transform.parent.name, _value, dd.value);
                    }
                });
            }
            return(clone);
        }
        private static void decorateElements(GameObject _expression, List <BlockModel.Element> _elements, Dictionary <string, string> _variants, CustomElementModel.CustomElementStatus _elementStatus)
        {
            RectTransform space = _expression.transform.Find("__space__").GetComponent <RectTransform>();

            space.gameObject.SetActive(_elements.Count == 0);

            foreach (BlockModel.Element element in _elements)
            {
                GameObject clone = null;
                if (element.type.Equals("text"))
                {
                    clone = GameObject.Instantiate(templateElementText.gameObject);
                    UnityEngine.UI.Text text = clone.GetComponent <UnityEngine.UI.Text>();
                    text.text = element.value;
                    FacadeUtility.StretchText(text);
                }
                else if (element.type.Equals("input"))
                {
                    clone = GameObject.Instantiate(templateElementInput.gameObject);
                    InputField input = clone.GetComponent <InputField>();
                    input.text = element.value;
                    if (_variants.ContainsKey(element.value))
                    {
                        input.text = _variants[element.value];
                    }
                    FacadeUtility.StretchInput(input);
                    input.onEndEdit.AddListener((_text) =>
                    {
                        if (null != OnInputUpdated)
                        {
                            OnInputUpdated(clone.transform.parent.name, element.value, _text);
                        }
                    });
                }
                else if (element.type.Equals("dropdown"))
                {
                    clone = GameObject.Instantiate(templateElementDropdown.gameObject);
                    Dropdown dropdown = clone.GetComponent <Dropdown>();
                    FacadeUtility.StretchDropdown(dropdown);
                    dropdown.onValueChanged.AddListener((_value) =>
                    {
                        if (null != OnDropdownUpdated)
                        {
                            OnDropdownUpdated(clone.transform.parent.name, element.value, dropdown.value);
                        }
                    });
                }
                else if (element.type.Equals("object"))
                {
                    clone = GameObject.Instantiate(templateElementObject.gameObject);
                    DropObject drop = clone.AddComponent <DropObject>();
                    FacadeUtility.StretchDropObject(drop);
                    if (_variants.ContainsKey(element.value))
                    {
                        string uuid = _variants[element.value];
                        if (!string.IsNullOrEmpty(uuid))
                        {
                            if (objectElements.ContainsKey(uuid))
                            {
                                ObjectElement oe = objectElements[uuid];
                                drop.captionText.text = oe.alias;
                                drop.imgIcon.gameObject.SetActive(true);
                                drop.imgIcon.sprite = oe.icon;
                            }
                        }
                    }
                    drop.onObjectDrop = (_dragObject) =>
                    {
                        if (null != OnDropObjectUpdated)
                        {
                            OnDropObjectUpdated(clone.transform.parent.name, element.value, _dragObject.name);
                        }
                    };
                }
                else
                {
                    clone = buildCustomElement(element.type, element.value, _variants, _elementStatus);
                }

                if (null == clone)
                {
                    continue;
                }

                clone.name = element.value;
                clone.transform.SetParent(_expression.transform);
                clone.transform.localScale = Vector3.one;
            }
        }
        public static void BuildExpression(List <BlockModel.Element> _elements, Dictionary <string, string> _variants, GameObject _obj, CustomElementModel.CustomElementStatus _elementStatus, Symbol _symbol, string _color)
        {
            Color  color  = FacadeUtility.HexToColor(_color);
            Sprite sprite = imgBlockBlank;

            if (Symbol.Single == _symbol)
            {
                sprite = imgBlockSingle;
            }
            else if (Symbol.Left == _symbol)
            {
                sprite = imgBlockHead;
            }
            else if (Symbol.Right == _symbol)
            {
                sprite = imgBlockFoot;
            }
            else if (Symbol.Middle == _symbol)
            {
                sprite = imgBlockMiddle;
            }

            Image img = _obj.transform.Find("__img__").GetComponent <Image>();

            img.color  = color;
            img.sprite = sprite;
            decorateElements(_obj, _elements, _variants, _elementStatus);
        }