Exemplo n.º 1
0
    string[] GetAvailablePorts(BaseDevice bd)
    {
        List <string> result = new List <string>();

        foreach (string port in bd._ports)
        {
            string deviceName = bd.GetFullName();
            string devicePort = deviceName + ":" + port;
            bool   connection = MessageHandler.PortConnectionLookup.ContainsKey(devicePort);

            if (!MessageHandler.PortConnectionLookup.ContainsKey(bd.GetFullName() + ":" + port))
            {
                result.Add(port);
            }
        }
        return(result.ToArray());
    }
Exemplo n.º 2
0
    private void OnGUI()
    {
        serializedObject = new SerializedObject(this);
        int normalTextSize = GUI.skin.label.fontSize;

        titleBar = "Port Connections Editor";


        editorScrollPosition = EditorGUILayout.BeginScrollView(editorScrollPosition);
        EditorGUILayout.BeginVertical();
        GUILayout.Space(10);
        EditorGUILayout.BeginHorizontal();
        GUI.skin.label.fontSize = 20;
        GUILayout.FlexibleSpace();
        GUILayout.Label(titleBar);
        GUILayout.FlexibleSpace();
        GUI.skin.label.fontSize = normalTextSize;
        EditorGUILayout.EndHorizontal();
        GUILayout.Space(10);

        EditorGUILayout.BeginHorizontal();
        serializedProperty = serializedObject.FindProperty("fromDevice");
        EditorGUI.BeginChangeCheck();
        EditorGUILayout.PropertyField(serializedProperty, GUILayout.MaxWidth(500), GUILayout.MinWidth(250));
        if (EditorGUI.EndChangeCheck())
        {
            try
            {
                fromDevice = serializedProperty.objectReferenceValue as BaseDevice;
                fromPort   = GetAvailablePorts(fromDevice);
            }
            catch
            {
                fromDevice = null;
                fromPort   = new string[] { };
            }
            fromPortSelected = -1;
        }
        fromPortSelected = EditorGUILayout.Popup(fromPortSelected, fromPort);
        EditorGUILayout.EndHorizontal();


        EditorGUILayout.BeginHorizontal();
        serializedProperty = serializedObject.FindProperty("toDevice");
        EditorGUI.BeginChangeCheck();
        EditorGUILayout.PropertyField(serializedProperty, GUILayout.MaxWidth(500), GUILayout.MinWidth(250));
        if (EditorGUI.EndChangeCheck())
        {
            try
            {
                toDevice = serializedProperty.objectReferenceValue as BaseDevice;
                toPort   = GetAvailablePorts(toDevice);// fromDevice.ports;
            }
            catch
            {
                toDevice = null;
                toPort   = new string[] { };
            }
            toPortSelected = -1;
        }
        toPortSelected = EditorGUILayout.Popup(toPortSelected, toPort);
        EditorGUILayout.EndHorizontal();
        EditorGUILayout.BeginHorizontal();
        if (GUILayout.Button("Add Connection"))
        {
            if (fromDevice != null && toDevice != null && fromPortSelected >= 0 && toPortSelected >= 0)
            {
                //new PortConnection(fromDevice, fromPort[fromPortSelected], toDevice, toPort[toPortSelected]);
                try
                {
                    MessageHandler.PortConnectionLookup.Add(fromDevice.GetFullName() + ":" + fromPort[fromPortSelected], toDevice.GetFullName() + ":" + toPort[toPortSelected]);
                    MessageHandler.PortConnectionLookup.Add(toDevice.GetFullName() + ":" + toPort[toPortSelected], fromDevice.GetFullName() + ":" + fromPort[fromPortSelected]);
                }
                catch
                {
                    //entry already exists
                    Debug.LogError("Connection already exists: " + fromDevice.GetFullName() + ":" + fromPort[fromPortSelected] + "<>" + toDevice.GetFullName() + ":" + toPort[toPortSelected]);
                }

                ResetPortConnection();
            }
            else
            {
                EditorUtility.DisplayDialog(
                    "Add Connection Error",
                    "Error: Unable to Add Connection. Make sure that both the from device/port " +
                    "and to device/port item values have been selected before attempting to add the connection.",
                    "Ok"
                    );
            }
        }
        if (GUILayout.Button("Save Port Connections"))
        {
            SavePortConnections();
        }
        if (GUILayout.Button("Load Port Connections"))
        {
            LoadPortConnections();
        }

        EditorGUILayout.EndHorizontal();
        GUILayout.Space(50);
        EditorGUILayout.HelpBox("PortConnection JSON Path: StreamingAssets/PortConnections/connections.portconnections", MessageType.Info);
        GUILayout.Space(15);

        if (MessageHandler.PortConnectionLookup.Count > 0)
        {
            EditorGUILayout.BeginHorizontal();
            serializedProperty = serializedObject.FindProperty("filterDevice");
            EditorGUI.BeginChangeCheck();
            EditorGUILayout.PropertyField(serializedProperty, GUILayout.MaxWidth(500), GUILayout.MinWidth(250));
            if (EditorGUI.EndChangeCheck())
            {
                try
                {
                    filterDevice = serializedProperty.objectReferenceValue as BaseDevice;
                    filterPort   = GetAllPorts(filterDevice);
                }
                catch
                {
                    filterDevice = null;
                    filterPort   = new string[] { };
                }
                filterPortSelected = 0;
            }
            filterPortSelected = EditorGUILayout.Popup(filterPortSelected, filterPort);
            EditorGUILayout.EndHorizontal();
        }
        GUILayout.Space(15);

        DrawDictionary();
        GUILayout.Space(10);

        /*
         * narrativeScrollPosition = EditorGUILayout.BeginScrollView(narrativeScrollPosition);
         * serializedObject.ApplyModifiedProperties();
         * EditorGUILayout.EndScrollView();
         */
        EditorGUILayout.EndVertical();
        EditorGUILayout.EndScrollView();
    }