示例#1
0
        void Parse()
        {
            btnodes.Clear();
            Clear();

            BTLTokenizer.Token[] tokens = null;

            try
            {
                tokens = BTLTokenizer.Tokenize(content);
            }
            catch (PandaScriptException e)
            {
                Debug.LogException(e);
            }

            GUINode node = null;

            if (tokens != null)
            {
                foreach (var t in tokens)
                {
                    if (t.content == null)
                    {
                        continue;
                    }

                    string trimmed = t.content.Trim();
                    if (trimmed == "")
                    {
                        continue;
                    }
                    if (trimmed.StartsWith("//"))
                    {
                        node       = new GUINode(GUINode.NodeType.Comment);
                        node.label = "//";
                        string comment = trimmed.Substring(2, trimmed.Length - 2);
                        node.Parameters_Add(new GUINodeParameter(typeof(string), comment));
                        Nodes_Add(node);
                    }
                    else
                    if (IsLabel(t))
                    {
                        node = new GUINode(trimmed);
                        Nodes_Add(node);
                        node.line = this;
                    }
                    else
                    if (t.valueType != BTLTokenizer.TokenValueType.None)
                    {
                        if (node != null)
                        {
                            node.Parameters_Add(new GUINodeParameter(t));
                        }
                    }
                }
            }
        }
示例#2
0
        public void Nodes_Remove(GUINode node)
        {
            if (_nodeList.Contains(node))
            {
                _nodeList.Remove(node);

                if (_nodeList.Count == 0)
                {
                    Clear();
                }

                _nodes = null;
            }
        }
        public GUINode Duplicate()
        {
            GUINode copy = new GUINode();

            copy.label    = this.label;
            copy.nodeType = this.nodeType;

            foreach (var p in this.parameters)
            {
                var pcopy = p.Duplicate();
                copy.Parameters_Add(pcopy);
            }

            return(copy);
        }
示例#4
0
        void Clear()
        {
            var nodes = this.nodes;

            foreach (var n in nodes)
            {
                Nodes_Remove(n);
            }
            _nodeList.Clear();
            _nodes = null;

            var node = new GUINode(" ", GUINode.NodeType.EmptyLine);

            Nodes_Add(node);
        }
示例#5
0
        public void Nodes_Add(GUINode node)
        {
            if (isEmpty)
            {
                foreach (var n in _nodeList)
                {
                    n.line = null;
                }

                _nodeList.Clear();
            }

            _nodeList.Add(node);
            node.line = this;
            _nodes    = null;
        }
        void OnGUI_OneBlock()
        {
            GUINode node  = this;
            var     style = new GUIStyle(node.isSelected ? BTLSyntaxHighlight.style_selected : BTLSyntaxHighlight.style_running);
            //GUIStyle style = new GUIStyle();

            bool displayInsert = false;

            if (!node.isSelected && (GUIBTScript.mode == GUIBTScript.Mode.MouseDrag || GUIBTScript.mode == GUIBTScript.Mode.InsertingNewNode))
            {
                switch (node.mouseHoverPosition)
                {
                case MouseHoverPosition.Above: style = BTLSyntaxHighlight.style_insert_above; break;

                case MouseHoverPosition.Below: style = BTLSyntaxHighlight.style_insert_below; break;

                case MouseHoverPosition.Center: style = BTLSyntaxHighlight.style_insert_in; break;
                }
                displayInsert = true;
            }

            var sbLabel = new System.Text.StringBuilder();

            if (displayInsert && mouseHoverPosition == MouseHoverPosition.Left)
            {
                sbLabel.Append("|");
                //GUILayout.Label(BTLSyntaxHighlight.texture_insert_LeftRigth, GUILayout.ExpandWidth(false));
            }

            sbLabel.Append(node.label);


            bool withParenthesis = parameters.Count > 0 && nodeType != NodeType.Comment;

            if (withParenthesis)
            {
                sbLabel.Append("(");
            }

            for (int i = 0; i < node.parameters.Count; i++)
            {
                var p = node.parameters[i];
                if (nodeType == NodeType.Comment && p.value.Trim() == "")
                {
                    sbLabel.Append("...");
                }
                else
                {
                    sbLabel.Append(p.value);

                    if (withParenthesis && i + 1 < node.parameters.Count)
                    {
                        sbLabel.Append(", ");
                    }
                }
            }

            if (withParenthesis)
            {
                sbLabel.Append(")");
            }


            if (displayInsert && mouseHoverPosition == MouseHoverPosition.Right)
            {
                sbLabel.Append("|");
                //GUILayout.Label(BTLSyntaxHighlight.texture_insert_LeftRigth, GUILayout.ExpandWidth(false));
            }

            GUI.SetNextControlName(node.controlName);
            if (node.nodeType == NodeType.EmptyLine)
            {
                GUILayout.Label(node.label, style, GUILayout.ExpandWidth(true));
            }
            else
            {
                GUILayout.Label(sbLabel.ToString(), style);
            }


            // Selection and drag start
            if (isMouseOver && Event.current.isMouse && Event.current.type == EventType.MouseDown)
            {
                if (GUIBTScript.isCTRLPressed)
                {
                    if (this.isSelected)
                    {
                        GUIBTScript.SelectionRemove(node);
                    }
                    else
                    {
                        GUIBTScript.SelectionAdd(node);
                    }
                }
                else
                {
                    if (!isSelected)
                    {
                        GUIBTScript.SelectionClear();
                    }
                    GUIBTScript.SelectionAdd(node);
                }

                GUI.FocusControl(node.controlName);
            }

            if (isMouseOver && Event.current.isMouse && Event.current.type == EventType.MouseUp && !GUIBTScript.isCTRLPressed && !GUIBTScript.isControlLocked)
            {
                GUIBTScript.SelectionClear();
                GUIBTScript.SelectionAdd(node);
                GUI.FocusControl(node.controlName);
            }
        }
        void OnGUI_ColoredNode()
        {
            GUINode node = this;

            GUI.SetNextControlName(node.controlName);

            GUIStyle style = null;

            switch (nodeType)
            {
            case NodeType.Comment: style = BTLSyntaxHighlight.style_comment; break;

            case NodeType.Structural: style = BTLSyntaxHighlight.style_keyword; break;

            case NodeType.Task: style = label == "tree" ? BTLSyntaxHighlight.style_keyword : BTLSyntaxHighlight.style_task; break;

            case NodeType.EmptyLine: style = BTLSyntaxHighlight.style_task; break;
            }


            if (node.nodeType == NodeType.EmptyLine)
            {
                GUILayout.Label(node.label, style, GUILayout.ExpandWidth(true));
            }
            else
            {
                GUILayout.Label(node.label, style);
            }

            if (isRectDirty && Event.current.type == EventType.Repaint)
            {
                rect = GUILayoutUtility.GetLastRect();
                var pRects = new List <Rect>();

                float width           = rect.width;
                bool  withParenthesis = parameters.Count > 0 && nodeType != NodeType.Comment;
                if (withParenthesis)
                {
                    GUILayout.Label("(", BTLSyntaxHighlight.style_label);
                    width += GUILayoutUtility.GetLastRect().width;
                }

                for (int i = 0; i < node.parameters.Count; i++)
                {
                    var p = node.parameters[i];
                    GUINode._currentParameterIndex = i;

                    p.OnGUI(nodeType == NodeType.Comment);
                    p.rect = GUILayoutUtility.GetLastRect();
                    pRects.Add(p.rect);
                    width += p.rect.width;

                    if (withParenthesis && i + 1 < node.parameters.Count)
                    {
                        GUILayout.Label(", ", BTLSyntaxHighlight.style_label);
                        width += GUILayoutUtility.GetLastRect().width;
                    }
                }

                if (withParenthesis)
                {
                    GUILayout.Label(")", BTLSyntaxHighlight.style_label);
                    width += GUILayoutUtility.GetLastRect().width;
                }

                rect.width     = width;
                parameterRects = pRects.ToArray();
                isRectDirty    = false;
            }
            else
            {
                bool withParenthesis = parameters.Count > 0 && nodeType != NodeType.Comment;

                if (withParenthesis)
                {
                    GUILayout.Label("(", BTLSyntaxHighlight.style_label);
                }

                for (int i = 0; i < node.parameters.Count; i++)
                {
                    var p = node.parameters[i];
                    GUINode._currentParameterIndex = i;
                    p.OnGUI(nodeType == NodeType.Comment);
                    if (withParenthesis && i + 1 < node.parameters.Count)
                    {
                        GUILayout.Label(", ", BTLSyntaxHighlight.style_label);
                    }
                }

                if (withParenthesis)
                {
                    GUILayout.Label(")", BTLSyntaxHighlight.style_label);
                }
            }
        }
        public void OnGUI()
        {
            var node = this;

            GUINode._current = this;

            // Recompute rects when edit state has changed.
            if (GUIBTScript.isEventSafe)
            {
                if (isEdited == false && _isEditedPrev)
                {
                    SetDirty();
                }

                if (isEdited && _isEditedPrev == false)
                {
                    GUIBTScript.SelectionClear();
                }

                _isEditedPrev = isEdited;
            }

            if (node.nodeType == NodeType.TaskList)
            {
                OnGUI_TaskListSelector();
            }
            else
            {
                if ((node.isSelected || node == GUIBTScript.cursorNode) && !isRectDirty && !isEdited)
                {
                    OnGUI_OneBlock();
                }
                else
                {
                    OnGUI_ColoredNode();
                }
            }

            // Edit parameter when left mouse click.
            bool isClicked = Event.current.isMouse && Event.current.type == EventType.MouseDown && Event.current.button == 0;
            var  mpos      = Event.current.mousePosition;

            if (!isRectDirty && !GUIBTScript.isCTRLPressed && isClicked && GUIBTScript.mode == GUIBTScript.Mode.Normal)
            {
                bool isParameterClicked = false;
                for (int i = 0; i < parameters.Count; i++)
                {
                    var r            = parameterRects[i];
                    var isWithinRect = r.Contains(mpos);
                    parameters[i].isEdited = isWithinRect;

                    if (isWithinRect)
                    {
                        isParameterClicked = true;
                    }
                }

                if (this.rect.Contains(mpos) && !isParameterClicked)
                {
                    bool isDoubleClicked = Time.realtimeSinceStartup - lastClickTime < 0.4f;
                    lastClickTime = Time.realtimeSinceStartup;
                    if (isDoubleClicked && this.nodeType == NodeType.Task && this.label.ToLower() != "tree")
                    {
                        GUIBTScript.OpenScript(this);
                    }
                }
            }

            // On press Enter or Esc stop editing the parameter.
            if (Event.current.isKey && (Event.current.keyCode == KeyCode.Return || Event.current.keyCode == KeyCode.KeypadEnter || Event.current.keyCode == KeyCode.Escape))
            {
                foreach (var p in parameters)
                {
                    if (p.isEdited)
                    {
                        p.isEdited = false;
                    }
                }
            }

            // Locate mouse position in rect.
            if (Event.current.type == EventType.Repaint)
            {
                if (rect.Contains(mpos))
                {
                    isMouseOver = true;

                    if (GUIBTScript.cursorNode != node)
                    {
                        GUIBTScript.cursorNode = node;
                        //GUIBTScript.current.redraw = true;
                    }

                    float f  = 0.3f;
                    var   w  = rect.width;
                    var   wb = w * f;
                    var   h  = rect.height;
                    var   hb = h * f;

                    if (mpos.y < rect.y + hb)
                    {
                        mouseHoverPosition = MouseHoverPosition.Above;
                    }
                    else
                    if (mpos.y > rect.y + h - hb && node.line.children.Count == 0)
                    {
                        mouseHoverPosition = MouseHoverPosition.Below;
                    }
                    else
                    if (mpos.x < rect.x + wb)
                    {
                        mouseHoverPosition = MouseHoverPosition.Left;
                    }
                    else
                    if (mpos.x > rect.x + w - wb)
                    {
                        mouseHoverPosition = MouseHoverPosition.Right;
                    }
                    else
                    if (nodeType == NodeType.Structural && node.line.nodes[0] == node)
                    {
                        mouseHoverPosition = MouseHoverPosition.Center;
                    }
                }
                else
                {
                    isMouseOver = false;
                }
            }
            else if (node != GUIBTScript.cursorNode)
            {
                mouseHoverPosition = MouseHoverPosition.None;
            }
        }
示例#9
0
 public bool Nodes_Contains(GUINode node)
 {
     return(_nodeList.Contains(node));
 }
示例#10
0
 public void Nodes_Insert(int index, GUINode node)
 {
     _nodeList.Insert(index, node);
     _nodes = null;
 }