Esempio n. 1
0
    public void OpenVariables()
    {
        SetButtonTheme(4);

        variables.SetActive(true);

        foreach (Transform t in storedObjects)
        {
            Destroy(t.gameObject);
        }

        foreach (Transform t in customVariableStorage)
        {
            Destroy(t.gameObject);
        }

        var            y         = 0;
        ICompilerEvent nextEvent = editor.beginEvent;

        while (nextEvent != null)
        {
            if (nextEvent is B_VariableEvent var)
            {
                var block = Instantiate(variableObject, customVariableStorage);
                block.Description = "";
                block.GetComponent <RectTransform>().anchoredPosition += Vector2.up * y;
                y -= 88;

                var b     = (RectTransform)block.transform.Find("Block");
                var field = b.Find("InputField").GetComponent <InputField>();
                field.text = var.variableName.text;
                field.name = var.variableName.text;

                var blockBlock = Instantiate(block.codePrefab, storedObjects);
                var bb         = (RectTransform)blockBlock.Find("Block");
                var inputField = bb.Find("InputField").GetComponent <InputField>();
                inputField.text  = field.name;
                block.codePrefab = blockBlock;

                // Set Size
                var prefWidth = Mathf.Max(inputField.preferredWidth + 28, 60);

                var rt = (RectTransform)inputField.transform;
                rt.sizeDelta = new Vector2(prefWidth, 60);
                bb.sizeDelta = new Vector2(prefWidth + 16, 76);

                rt           = (RectTransform)field.transform;
                rt.sizeDelta = new Vector2(prefWidth, 60);
                b.sizeDelta  = new Vector2(prefWidth + 16, 76);

                field.onEndEdit.AddListener(delegate
                {
                    foreach (var c in GetComponentsInChildren <B_VariableEvent>())
                    {
                        if (c.variableName.text == field.name)
                        {
                            c.variableName.text = field.text;
                        }
                    }

                    foreach (var c in GetComponentsInChildren <B_Variable>())
                    {
                        if (c.variableName.text == field.name)
                        {
                            c.variableName.text = field.text;
                        }
                    }

                    field.name = field.text;
                    editor.ReconnectAll();

                    OpenVariables();
                });
            }
            nextEvent = nextEvent.NextConnection;
        }

        // Scale Scroll View
        EnableCodeHierarchy(variables);
    }
Esempio n. 2
0
        void LoadFunctions(SubstringOptimizedString subcode, ICompilerEvent currentEvent, SelectableObject obj, ICompilerCode currentCode = null, int codeId = 0)
        {
            var           functionScope      = subcode.IndexOf(CodeValueBegin);
            ICompilerCode lastFunctionObject = null;

            while (functionScope != -1)
            {
                var valueScopeAmount = 1;
                var valueEndIndex    = -1;

                for (int i = functionScope + 1; i < subcode.Length; i++)
                {
                    if (subcode[i] == CodeValueBegin)
                    {
                        valueScopeAmount++;
                    }
                    else if (subcode[i] == CodeValueEnd)
                    {
                        valueScopeAmount--;

                        if (valueScopeAmount == 0)
                        {
                            if (subcode.Length > i + 1 && subcode[i + 1] == CodeValueBegin)
                            {
                                // There is still another ()
                                // Eg: (Val1)(Val2)
                            }
                            else                     // if it is the end
                            {
                                valueEndIndex = i;
                                break;
                            }
                        }
                    }
                }

                // Check if code contains equal amount of {}
                if (valueEndIndex == -1)
                {
                    Debug.LogWarning($"{currentEvent as MonoBehaviour} code was corrupted.");
                    return;
                }

                // Create Function
                var functionName = subcode.Substring(0, functionScope).ToString();
                //print(functionName);

                // Broadcast function is only (currently) function with no values, but InputField
                var    functionValueSplit = functionName.IndexOf(FunctionSplit);
                string functionValue      = "";
                if (functionValueSplit != -1)
                {
                    functionValue = functionName.Substring(functionValueSplit + 1);
                    functionName  = functionName.Substring(0, functionValueSplit);
                }

#if UNITY_EDITOR
                if (!CodeIdPrefabs.ContainsKey(functionName))
                {
                    Debug.LogError($"Doesn't Contain {functionName}");
                }
#endif

                var functionObject  = Instantiate(CodeIdPrefabs[functionName], obj.Script.transform);
                var currentFunction = functionObject.GetComponent <ICompilerCode>();

                if (functionValueSplit != -1)
                {
                    if (currentFunction is B_Broadcast bb)
                    {
                        bb.broadcastName.text = functionValue;
                    }
                    else if (currentFunction is B_Comment cc)
                    {
                        cc.inputField.text = functionValue;
                    }
                }

                // Event Offset
                var offset = functionObject.gameObject.AddComponent <BlockOffset>();
                currentFunction.Offset = offset;

                if (lastFunctionObject != null)
                {
                    lastFunctionObject.CodeConnections[0] = currentFunction;
                    offset.Connection = (lastFunctionObject as MonoBehaviour).transform.Find("CodeConnection");
                }
                else
                {
                    if (currentEvent != null)
                    {
                        currentEvent.CodeConnections[0] = currentFunction;
                        offset.Connection = (currentEvent as MonoBehaviour).transform.Find("Scope")
                                            .Find("CodeConnection");
                    }
                    else
                    {
                        currentCode.CodeConnections[codeId] = currentFunction;
                        if (codeId == 1)
                        {
                            offset.Connection = (currentCode as MonoBehaviour).transform.Find("Scope")
                                                .Find("CodeConnection");
                        }
                        else
                        {
                            offset.Connection = (currentCode as MonoBehaviour).transform.Find($"Scope{codeId}")
                                                .Find("CodeConnection");
                        }
                    }
                }

                offset.Connect();

                // Todo: Implement () value creation (LoadValues)
                var valueString = subcode.Substring(functionScope /*+ 1*/, valueEndIndex - functionScope + 1 /*- 1*/);
                if (valueString.Length > 0)
                {
                    LoadValueScopes(valueString, currentFunction);
                }

                // Todo: Implement {}{} function searches (if, if else) (recursion)
                // Function Scopes
                var innerScope = LoadFunctionScopes(subcode, currentFunction, obj, valueEndIndex);
                // Connect innerScope.Variable1? to mainfunction

                // Next Loop:
                if (innerScope != -1)
                {
                    subcode = subcode.Substring(innerScope + 2);
                }
                else
                {
                    subcode = subcode.Substring(valueEndIndex + 1);
                }

                // Next Loop:
                functionScope = subcode.IndexOf(CodeValueBegin);

                lastFunctionObject = currentFunction;
            }
        }
Esempio n. 3
0
        void MoveCode(Transform hit, bool left = true)
        {
            var canDropCode = previousConnection == null ||
                              (left ? ((byte)Settings.CodeDropType & 0b01) == 1 : ((byte)Settings.CodeDropType & 0b10) == 1);

            if (CanDelete())
            {
                switch (clonedObjectType)
                {
                case CodeType.Event:
                    CodeDelete(currentClonedObject.GetComponent <ICompilerEvent>());
                    Destroy(currentClonedObject.gameObject);
                    break;

                case CodeType.Function:
                    CodeDelete(currentClonedObject.GetComponent <ICompilerCode>());
                    break;

                case CodeType.Value:
                    ValueDelete(currentClonedObject.GetComponent <ICompilerValues>());
                    break;
                }

                currentClonedObject = null;

                //clickAudioSource.Play();
            }
            else
            {
                switch (clonedObjectType)
                {
                case CodeType.Event:
                    var curEvent = currentClonedObject.GetComponent <ICompilerEvent>();
                    if (hit && !hit.IsChildOf(otherCodeTransform))
                    {
                        var oldEvent = hit.GetComponentInParent <ICompilerEvent>();

                        ICompilerEvent nextCon = oldEvent.NextConnection;
                        oldEvent.NextConnection = curEvent;
                        curEvent.NextConnection = nextCon;

                        curEvent.Offset.Connection = oldEvent.NextConnectionObject;
                        if (curEvent.NextConnection != null)
                        {
                            nextCon.Offset.Connection = curEvent.NextConnectionObject;
                        }

                        currentClonedObject.SetParent(currentScriptObject, false);
                        currentClonedObject = null;
                        ReconnectAll();
                    }
                    else
                    {
                        if (!canDropCode)
                        {
                            var            prevConEvent = (ICompilerEvent)previousConnection;
                            ICompilerEvent nextCon      = prevConEvent.NextConnection;
                            prevConEvent.NextConnection = curEvent;
                            curEvent.NextConnection     = nextCon;

                            curEvent.Offset.Connection = prevConEvent.NextConnectionObject;
                            if (curEvent.NextConnection != null)
                            {
                                nextCon.Offset.Connection = curEvent.NextConnectionObject;
                            }

                            currentClonedObject.SetParent(currentScriptObject, false);
                            currentClonedObject = null;
                            ReconnectAll();
                        }
                        else
                        {
                            currentClonedObject.SetParent(otherCodeTransform);
                            currentClonedObject = null;
                            ReconnectOtherCode();
                        }
                    }

                    break;



                case CodeType.Function:
                    var func = currentClonedObject.GetComponent <ICompilerCode>();

                    var lastCodeInMove = func;
                    var lastFunc       = func.CodeConnections[0];

                    while (lastFunc != null)
                    {
                        lastCodeInMove = lastFunc;
                        lastFunc       = lastFunc.CodeConnections[0];
                    }

                    if (hit)
                    {
                        var connection = hit.GetComponentInParent <ICompilerCodeConnection>();

                        if (connection != null && func != null)
                        {
                            var con = connection.GetCodeConnection(hit).Value;

                            ICompilerCode afterCode = connection.CodeConnections[con.codeIndex];
                            if (afterCode != null)
                            {
                                lastCodeInMove.CodeConnections[0] = afterCode;
                                afterCode.Offset.Connection       = lastCodeInMove.CodeConnectionObjects[0];
                            }
                            connection.CodeConnections[con.codeIndex] = func;
                            func.Offset.Connection = connection.CodeConnectionObjects[con.codeIndex];

                            currentClonedObject.SetParent(currentScriptObject, false);
                            currentClonedObject = null;
                            ReconnectAll();
                        }
                    }
                    else
                    {
                        if (!canDropCode)
                        {
                            var           prevConCode = (CodeCon)previousConnection;
                            ICompilerCode afterCode   = prevConCode.con.CodeConnections[prevConCode.index];
                            if (afterCode != null)
                            {
                                lastCodeInMove.CodeConnections[0] = afterCode;
                                afterCode.Offset.Connection       = lastCodeInMove.CodeConnectionObjects[0];
                            }

                            prevConCode.con.CodeConnections[prevConCode.index] = func;
                            func.Offset.Connection = prevConCode.con.CodeConnectionObjects[prevConCode.index];

                            currentClonedObject.SetParent(currentScriptObject, false);
                            currentClonedObject = null;
                            ReconnectAll();
                        }
                        else
                        {
                            currentClonedObject.SetParent(otherCodeTransform);
                            currentClonedObject = null;
                            ReconnectOtherCode();
                        }
                    }

                    break;



                case CodeType.Value:
                    var val         = currentClonedObject.GetComponent <ICompilerValue>();
                    var connectPrev = true;

                    if (hit)
                    {
                        var connection = hit.GetComponentInParent <ICompilerValues>();

                        if (connection != null && val != null)
                        {
                            var con = connection.GetValue(hit).Value;

                            ICompilerValue oldValue = connection.Values[con.valueIndex];
                            if (oldValue != null)
                            {
                                connectPrev = true;
                            }
                            else
                            {
                                connection.Values[con.valueIndex] = val;
                                val.Offset.Connection             = con.connectObject;

                                currentClonedObject.SetParent(hit, false);
                                currentClonedObject = null;
                                ReconnectAll();

                                connectPrev = false;
                            }
                        }
                    }

                    if (connectPrev)
                    {
                        if (!canDropCode)
                        {
                            var prevCodeCon = (ValueCon)previousConnection;
                            prevCodeCon.con.Values[prevCodeCon.index] = val;
                            val.Offset.Connection = prevCodeCon.lastCon;

                            currentClonedObject.SetParent(prevCodeCon.lastCon, false);
                            currentClonedObject = null;
                            ReconnectAll();
                        }
                        else
                        {
                            currentClonedObject.SetParent(otherCodeTransform);
                            currentClonedObject = null;
                        }
                    }

                    ReconnectOtherCode();

                    break;
                }

                //clickAudioSource.Play();
            }

            //if (hit != null && hit.IsChildOf(otherCodeTransform))
            //ReconnectOtherCode();
        }
Esempio n. 4
0
        // Load Events
        public void LoadCode(SelectableObject obj, SubstringOptimizedString code)
        {
            var            eventScope      = code.IndexOf(CodeScopeBegin);
            ICompilerEvent lastEventObject = null;

            // CREATE CODE IF THERE IS CODE
            if (eventScope != -1)
            {
                //print(code);

                Editor.OpenScript(obj);
                Editor.CloseEditor();

                lastEventObject = obj.Script.beginEvent;
            }

            while (eventScope != -1)
            {
                // Find where event {} ends
                var scopeAmount   = 1;
                var eventScopeEnd = -1;
                for (int i = eventScope + 1; i < code.Length; i++)
                {
                    if (code[i] == CodeScopeBegin)
                    {
                        scopeAmount++;
                    }
                    else if (code[i] == CodeScopeEnd)
                    {
                        scopeAmount--;

                        if (scopeAmount == 0)
                        {
                            eventScopeEnd = i;
                            break;
                        }
                    }
                }

                // Check if code contains equal amount of {}
                if (eventScopeEnd == -1)
                {
                    Debug.LogWarning($"{obj} code was corrupted.");
                    return;
                }

                // Create Event
                var eventName = code.Substring(0, eventScope);

                // Is it a Variable Event
                var varSplit = eventName.IndexOf(VariableSplit);
                if (varSplit != -1)
                {
                    eventName = eventName.Substring(0, varSplit);
                }

                var eventObject  = Instantiate(CodeIdPrefabs[eventName.ToString()], obj.Script.transform);
                var currentEvent = eventObject.GetComponent <ICompilerEvent>();

                // Event has custom saving
                if (varSplit != -1)
                {
                    var str = eventName.ToString();
                    if (str == "VarEvent")
                    {
                        B_VariableEvent variable = currentEvent as B_VariableEvent;
                        variable.variableName.text = code.Substring(varSplit + 1, eventScope - varSplit - 1).ToString();
                    }
                    else if (str == "Receive")
                    {
                        B_Received variable = currentEvent as B_Received;
                        variable.broadcastName.text = code.Substring(varSplit + 1, eventScope - varSplit - 1).ToString();
                    }
                }

                // Event Offset
                var offset = eventObject.gameObject.AddComponent <BlockOffset>();
                currentEvent.Offset = offset;
                offset.Connection   = (lastEventObject as MonoBehaviour).transform.Find("EventConnection");
                offset.Connect();
                //offset.Connect();

                // Connect event to previous event
                lastEventObject.NextConnection = currentEvent;



                // FUNCTIONS INSIDE OF CODE
                var subcode = code.Substring(eventScope + 1, eventScopeEnd - eventScope - 1);
                LoadFunctions(subcode, currentEvent, obj);



                // Update Size
                //currentEvent.UpdateSize(0);

                // Next Loop:
                code       = code.Substring(eventScopeEnd + 1);
                eventScope = code.IndexOf(CodeScopeBegin);

                lastEventObject = currentEvent;
            }
        }
 public void NewEvent(ICompilerEvent log)
 {
     console.WriteLine(log.GetMessage());
 }