Esempio n. 1
0
    public override void OnInspectorGUI()
    {
        DrawDefaultInspector();

        EditorGUILayout.BeginVertical();
        EditorGUILayout.BeginHorizontal();
        currentId = EditorGUILayout.IntField("ID", currentId);
        if (GUILayout.Button("Next Free ID"))
        {
            var t = target as TrafSystem;
            currentId = t.entries.Max(e => e.identifier) + 1;
        }
        currentSubId = EditorGUILayout.IntField("SubID", currentSubId);
        EditorGUILayout.EndHorizontal();

        switch (currentState)
        {
        case TrafEditState.IDLE:
            if (GUILayout.Button("DELETE - CAREFUL!"))
            {
                var t = target as TrafSystem;
                t.entries.RemoveAll(e => e.identifier == currentId);
            }

            if (GUILayout.Button("new/edit"))
            {
                var t = target as TrafSystem;
                if (t.entries.Any(entry => entry.identifier == currentId && entry.subIdentifier == currentSubId))
                {
                    var road = t.entries.Find(entry => entry.identifier == currentId && entry.subIdentifier == currentSubId);
                    currentRoad = road.road;
                    t.entries.Remove(road);
                    currentState = TrafEditState.EDIT;
                }
                else
                {
                    currentRoad = ScriptableObject.CreateInstance(typeof(TrafRoad)) as TrafRoad;
                    InitRoad(currentRoad);
                    currentState = TrafEditState.EDIT;
                }
            }

            if (GUILayout.Button("new multi one way"))
            {
                var t = target as TrafSystem;
                if (t.entries.Any(entry => entry.identifier == currentId))
                {
                    Debug.Log("TrafSystem: A road with that ID already exists. Multi editing not supported");
                }
                else
                {
                    currentRoad = ScriptableObject.CreateInstance(typeof(TrafRoad)) as TrafRoad;
                    InitRoad(currentRoad);
                    currentState = TrafEditState.EDIT_MULTI;
                }
            }

            if (GUILayout.Button("new multi two way"))
            {
                var t = target as TrafSystem;
                if (t.entries.Any(entry => entry.identifier == currentId))
                {
                    Debug.Log("TrafSystem: A road with that ID already exists. Multi editing not supported");
                }
                else
                {
                    currentRoad = ScriptableObject.CreateInstance(typeof(TrafRoad)) as TrafRoad;
                    InitRoad(currentRoad);
                    currentState = TrafEditState.EDIT_MULTI_TWOWAY;
                }
            }

            if (GUILayout.Button("Visualize Entries and Intersections"))
            {
                currentState = TrafEditState.DISPLAY_ENTRIES_INTERSECTIONS;
                Debug.Log("Total entry count " + (target as TrafSystem).entries.Count);
                Debug.Log("Total intersection count " + (target as TrafSystem).intersections.Count);
                SceneView.RepaintAll();
            }

            if (GUILayout.Button("Visualize Entries"))
            {
                currentState = TrafEditState.DISPLAY_ENTRIES;
                Debug.Log("Total entry count " + (target as TrafSystem).entries.Count);
                SceneView.RepaintAll();
            }

            if (GUILayout.Button("Visualize Intersections"))
            {
                currentState = TrafEditState.DISPLAY_INTERSECTIONS;
                Debug.Log("Total intersection count " + (target as TrafSystem).intersections.Count);
                SceneView.RepaintAll();
            }

            if (GUILayout.Button("Register all intersections"))
            {
                var t = target as TrafSystem;
                t.intersections.Clear();
                TrafIntersectionEditor.GoAll(t);
                EditorUtility.SetDirty(target);
            }

            if (GUILayout.Button("Deregister all intersections"))
            {
                var t = target as TrafSystem;
                t.intersections.Clear();
                EditorUtility.SetDirty(target);
            }

            if (GUILayout.Button("Populate all owning entries"))
            {
                var t = target as TrafSystem;
                foreach (var i in t.intersections)
                {
                    if (i.intersection != null)
                    {
                        i.intersection.owningEntry = i;
                    }
                }
                EditorUtility.SetDirty(target);
            }

            if (GUILayout.Button("Generate RoadGraph"))
            {
                GenerateRoadGraph();
                EditorUtility.SetDirty(target);
            }

            if (GUILayout.Button("Clear RoadGraph"))
            {
                var t = target as TrafSystem;
                t.roadGraph = new TrafRoadGraph();
                EditorUtility.SetDirty(target);
            }

            if (GUILayout.Button("Remove Markers"))
            {
                var t = target as TrafSystem;
                foreach (var g in t.GetComponentsInChildren <Renderer>())
                {
                    g.enabled = false;
                }
            }

            if (GUILayout.Button("TEMP"))
            {
                var t = target as TrafSystem;
                foreach (var e in t.entries)
                {
                    if (e.road != null)
                    {
                        e.waypoints = new List <Vector3>();
                        foreach (var v in e.road.waypoints)
                        {
                            e.waypoints.Add(v.position);
                        }
                    }
                }
            }

            if (GUILayout.Button("Localize Waypoint World Transformations"))
            {
                var t          = target as TrafSystem;
                var allEntries = new List <TrafEntry>();
                allEntries.AddRange(t.entries);
                allEntries.AddRange(t.intersections);
                foreach (var e in allEntries)
                {
                    for (int i = 0; i < e.waypoints.Count; i++)
                    {
                        e.waypoints[i] = t.transform.localToWorldMatrix.MultiplyPoint(e.waypoints[i]);
                    }
                }
                t.transform.position   = Vector3.zero;
                t.transform.rotation   = Quaternion.identity;
                t.transform.localScale = Vector3.one;
            }

            if (GUILayout.Button("EXPORT DATA"))
            {
                var    t             = target as TrafSystem;
                var    intersections = t.intersections;
                string output        = "";
                for (int i = 0; i < intersections.Count; i++)
                {
                    var    inter = intersections[i];
                    string index = i.ToString();
                    string name  = inter.light == null ? "" : inter.light.gameObject.name;
                    string x     = inter.light == null ? "" : inter.light.transform.position.x.ToString();
                    string y     = inter.light == null ? "" : inter.light.transform.position.y.ToString();
                    string z     = inter.light == null ? "" : inter.light.transform.position.z.ToString();
                    output += $"{index},{name},{x},{y},{z}\n";
                }
                using (StreamWriter sw = File.CreateText("dataTransfer.rl"))
                {
                    sw.Write(output);
                }
            }

            if (GUILayout.Button("READ DATA"))
            {
                var t             = target as TrafSystem;
                var intersections = t.intersections;
                var lines         = File.ReadAllLines("dataTransfer.rl");
                for (int i = 0; i < lines.Length; i++)
                {
                    string   line       = lines[i];
                    string[] items      = line.Split(',');
                    string   searchName = items[1];
                    if (searchName == "")
                    {
                        continue;
                    }
                    Vector3 pos        = new Vector3(float.Parse(items[2]), float.Parse(items[3]), float.Parse(items[4]));
                    var     containers = GameObject.FindObjectsOfType <TrafficLightContainer>();
                    foreach (var c in containers)
                    {
                        if (c.gameObject.name != searchName)
                        {
                            continue;
                        }
                        float dist = (c.transform.position - pos).magnitude;
                        if (dist < 0.01f)
                        {
                            Debug.Log("assign");
                            intersections[i].light = c;
                            break;
                        }
                    }
                }
            }

            if (GUILayout.Button("Debug Number"))
            {
                var t             = target as TrafSystem;
                var intersections = t.intersections;
                int num           = 0;
                foreach (var inter in intersections)
                {
                    if (inter.light != null)
                    {
                        num++;
                    }
                }
                Debug.Log(num);
            }

            EditorGUILayout.Space();

            if (GUILayout.Button("Generate Lanes Texture"))
            {
                GenerateInfractionsTexture();
            }

            if (GUILayout.Button("Generate Highway Lanes Texture"))
            {
                GenerateInfractionsTextureHighway();
            }

            if (GUILayout.Button("Apply stoplight infraction triggers"))
            {
                ApplyStoplightInfractionTriggers();
            }

            EditorGUILayout.Space();
            EditorGUILayout.LabelField("Specific Entry:");

            specificId    = EditorGUILayout.IntField("ID", specificId);
            specificSubId = EditorGUILayout.IntField("SubID", specificSubId);
            specificIndex = EditorGUILayout.IntField("Index", specificIndex);

            if (GUILayout.Button("Visualize single entry/intersection"))
            {
                currentState = TrafEditState.DISPLAY_SINGLE;
                SceneView.RepaintAll();
            }

            if (GUILayout.Button("Snap scene camera to specific entry point(\"-1\" is end point)"))
            {
                SnapSceneCameraToEntry();
            }

            EditorGUILayout.Space();

            if (GUILayout.Button("Debug road graph"))
            {
                DebugRoadGraph();
            }

            if (GUILayout.Button("Auto fix road graph"))
            {
                AutoFixRoadGraph();
            }

            EditorGUILayout.Space();

            EditorGUILayout.BeginHorizontal();
            fromId    = EditorGUILayout.IntField("From ID", fromId);
            fromSubId = EditorGUILayout.IntField("From sub ID", fromSubId);
            EditorGUILayout.EndHorizontal();

            if (GUILayout.Button("Manual check road graph edge"))
            {
                ManualCheckRoadGraph(fromId, fromSubId);
            }

            EditorGUILayout.BeginHorizontal();
            toId    = EditorGUILayout.IntField("To ID", toId);
            toSubId = EditorGUILayout.IntField("To sub ID", toSubId);
            EditorGUILayout.EndHorizontal();


            if (GUILayout.Button("Manual connect road graph edge"))
            {
                ManualConnectRoadGraph(fromId, fromSubId, toId, toSubId);
            }

            if (GUILayout.Button("Manual remove road graph edge"))
            {
                ManualRemoveRoadGraph(fromId, fromSubId, toId, toSubId);
            }

            break;

        case TrafEditState.EDIT:
            if (GUILayout.Button("save"))
            {
                var t = target as TrafSystem;
                currentState = TrafEditState.IDLE;
                t.entries.Add(new TrafEntry()
                {
                    road = currentRoad, identifier = currentId, subIdentifier = currentSubId
                });
            }
            break;

        case TrafEditState.EDIT_MULTI:
            laneWidth = EditorGUILayout.Slider("lane width", laneWidth, 0f, 50f);

            if (GUILayout.Button("save"))
            {
                var t = target as TrafSystem;
                currentState = TrafEditState.IDLE;
                t.entries.Add(new TrafEntry()
                {
                    road = InitRoadMulti(currentRoad, laneWidth * -1.5f, false), identifier = currentId, subIdentifier = 0
                });
                t.entries.Add(new TrafEntry()
                {
                    road = InitRoadMulti(currentRoad, laneWidth * -.5f, false), identifier = currentId, subIdentifier = 1
                });
                t.entries.Add(new TrafEntry()
                {
                    road = InitRoadMulti(currentRoad, laneWidth * .5f, false), identifier = currentId, subIdentifier = 2
                });
                t.entries.Add(new TrafEntry()
                {
                    road = InitRoadMulti(currentRoad, laneWidth * 1.5f, false), identifier = currentId, subIdentifier = 3
                });
            }

            if (GUILayout.Button("cancel"))
            {
                currentState = TrafEditState.IDLE;
            }
            break;

        case TrafEditState.EDIT_MULTI_TWOWAY:
            laneWidth = EditorGUILayout.Slider("lane width", laneWidth, 0f, 50f);

            if (GUILayout.Button("save"))
            {
                var t = target as TrafSystem;
                currentState = TrafEditState.IDLE;
                t.entries.Add(new TrafEntry()
                {
                    road = InitRoadMulti(currentRoad, laneWidth * -1.5f, true), identifier = currentId, subIdentifier = 0
                });
                t.entries.Add(new TrafEntry()
                {
                    road = InitRoadMulti(currentRoad, laneWidth * -.5f, true), identifier = currentId, subIdentifier = 1
                });
                t.entries.Add(new TrafEntry()
                {
                    road = InitRoadMulti(currentRoad, laneWidth * .5f, false), identifier = currentId, subIdentifier = 2
                });
                t.entries.Add(new TrafEntry()
                {
                    road = InitRoadMulti(currentRoad, laneWidth * 1.5f, false), identifier = currentId, subIdentifier = 3
                });
            }
            break;

        case TrafEditState.EDIT_MULTI_RA:
            laneWidth = EditorGUILayout.Slider("lane width", laneWidth, 0f, 50f);
            if (GUILayout.Button("save"))
            {
                var t = target as TrafSystem;
                currentState = TrafEditState.IDLE;
                t.entries.Add(new TrafEntry()
                {
                    identifier = currentId, subIdentifier = 0, waypoints = currentRoadMulti[0]
                });
                t.entries.Add(new TrafEntry()
                {
                    identifier = currentId, subIdentifier = 1, waypoints = currentRoadMulti[1]
                });
                t.entries.Add(new TrafEntry()
                {
                    identifier = currentId, subIdentifier = 2, waypoints = currentRoadMulti[2]
                });
                t.entries.Add(new TrafEntry()
                {
                    identifier = currentId, subIdentifier = 3, waypoints = currentRoadMulti[3]
                });
            }
            break;

        case TrafEditState.DISPLAY_ENTRIES:
            if (GUILayout.Button("Back"))
            {
                currentState = TrafEditState.IDLE;
                SceneView.RepaintAll();
            }
            showIds = GUILayout.Toggle(showIds, "Show IDs?");
            break;

        case TrafEditState.DISPLAY_INTERSECTIONS:
            if (GUILayout.Button("Back"))
            {
                currentState = TrafEditState.IDLE;
                SceneView.RepaintAll();
            }
            showIds = GUILayout.Toggle(showIds, "Show IDs?");
            break;

        case TrafEditState.DISPLAY_ENTRIES_INTERSECTIONS:
            if (GUILayout.Button("Back"))
            {
                currentState = TrafEditState.IDLE;
                SceneView.RepaintAll();
            }
            showIds = GUILayout.Toggle(showIds, "Show IDs?");
            break;

        case TrafEditState.DISPLAY_SINGLE:
            if (GUILayout.Button("Back"))
            {
                currentState = TrafEditState.IDLE;
                SceneView.RepaintAll();
            }
            showIds = GUILayout.Toggle(showIds, "Show IDs?");
            break;
        }

        EditorGUILayout.EndVertical();
    }
    public override void OnInspectorGUI()
    {
        //DrawDefaultInspector();
        EditorGUILayout.BeginVertical();
        EditorGUILayout.BeginHorizontal();
        currentId = EditorGUILayout.IntField("ID", currentId);
        if (GUILayout.Button("Next Free ID"))
        {
            var t = target as TrafSystem;
            currentId = t.entries.Max(e => e.identifier) + 1;
        }
        currentSubId = EditorGUILayout.IntField("SubID", currentSubId);
        EditorGUILayout.EndHorizontal();

        switch (currentState)
        {
        case TrafEditState.IDLE:
            if (GUILayout.Button("DELETE - CAREFUL!"))
            {
                var t = target as TrafSystem;
                t.entries.RemoveAll(e => e.identifier == currentId);
            }

            if (GUILayout.Button("new/edit"))
            {
                var t = target as TrafSystem;
                if (t.entries.Any(entry => entry.identifier == currentId && entry.subIdentifier == currentSubId))
                {
                    var road = t.entries.Find(entry => entry.identifier == currentId && entry.subIdentifier == currentSubId);
                    currentRoad = road.road;
                    t.entries.Remove(road);
                    currentState = TrafEditState.EDIT;
                }
                else
                {
                    currentRoad = ScriptableObject.CreateInstance(typeof(TrafRoad)) as TrafRoad;
                    InitRoad(currentRoad);
                    currentState = TrafEditState.EDIT;
                }
            }

            if (GUILayout.Button("new multi one way"))
            {
                var t = target as TrafSystem;
                if (t.entries.Any(entry => entry.identifier == currentId))
                {
                    Debug.Log("TrafSystem: A road with that ID already exists. Multi editing not supported");
                }
                else
                {
                    currentRoad = ScriptableObject.CreateInstance(typeof(TrafRoad)) as TrafRoad;
                    InitRoad(currentRoad);
                    currentState = TrafEditState.EDIT_MULTI;
                }
            }

            if (GUILayout.Button("new multi two way"))
            {
                var t = target as TrafSystem;
                if (t.entries.Any(entry => entry.identifier == currentId))
                {
                    Debug.Log("TrafSystem: A road with that ID already exists. Multi editing not supported");
                }
                else
                {
                    currentRoad = ScriptableObject.CreateInstance(typeof(TrafRoad)) as TrafRoad;
                    InitRoad(currentRoad);
                    currentState = TrafEditState.EDIT_MULTI_TWOWAY;
                }
            }

            if (GUILayout.Button("Visualize"))
            {
                currentState = TrafEditState.DISPLAY;
                SceneView.RepaintAll();
            }

            if (GUILayout.Button("Register all intersections"))
            {
                var t = target as TrafSystem;
                t.intersections.Clear();
                TrafIntersectionEditor.GoAll(t);
                EditorUtility.SetDirty(target);
            }

            if (GUILayout.Button("Deregister all intersections"))
            {
                var t = target as TrafSystem;
                t.intersections.Clear();
                EditorUtility.SetDirty(target);
            }

            if (GUILayout.Button("Generate RoadGraph"))
            {
                GenerateRoadGraph();
                EditorUtility.SetDirty(target);
            }

            if (GUILayout.Button("Clear RoadGraph"))
            {
                var t = target as TrafSystem;
                t.roadGraph = new TrafRoadGraph();
                EditorUtility.SetDirty(target);
            }
            if (GUILayout.Button("Remove Markers"))
            {
                var t = target as TrafSystem;
                foreach (var g in t.GetComponentsInChildren <Renderer>())
                {
                    g.enabled = false;
                }
            }

            if (GUILayout.Button("TEMP"))
            {
                var t = target as TrafSystem;
                foreach (var e in t.entries)
                {
                    if (e.road != null)
                    {
                        e.waypoints = new List <Vector3>();
                        foreach (var v in e.road.waypoints)
                        {
                            e.waypoints.Add(v.position);
                        }
                    }
                }
            }

            EditorGUILayout.Space();

            if (GUILayout.Button("Generate Lanes Texture"))
            {
                GenerateInfractionsTexture();
            }
            if (GUILayout.Button("Generate Highway Lanes Texture"))
            {
                GenerateInfractionsTextureHighway();
            }

            if (GUILayout.Button("Apply stoplight infraction triggers"))
            {
                ApplyStoplightInfractionTriggers();
            }

            break;

        case TrafEditState.EDIT:
            if (GUILayout.Button("save"))
            {
                var t = target as TrafSystem;
                currentState = TrafEditState.IDLE;
                t.entries.Add(new TrafEntry()
                {
                    road = currentRoad, identifier = currentId, subIdentifier = currentSubId
                });
            }
            break;

        case TrafEditState.EDIT_MULTI:
            laneWidth = EditorGUILayout.Slider("lane width", laneWidth, 0f, 50f);

            if (GUILayout.Button("save"))
            {
                var t = target as TrafSystem;
                currentState = TrafEditState.IDLE;
                t.entries.Add(new TrafEntry()
                {
                    road = InitRoadMulti(currentRoad, laneWidth * -1.5f, false), identifier = currentId, subIdentifier = 0
                });
                t.entries.Add(new TrafEntry()
                {
                    road = InitRoadMulti(currentRoad, laneWidth * -.5f, false), identifier = currentId, subIdentifier = 1
                });
                t.entries.Add(new TrafEntry()
                {
                    road = InitRoadMulti(currentRoad, laneWidth * .5f, false), identifier = currentId, subIdentifier = 2
                });
                t.entries.Add(new TrafEntry()
                {
                    road = InitRoadMulti(currentRoad, laneWidth * 1.5f, false), identifier = currentId, subIdentifier = 3
                });
            }
            if (GUILayout.Button("cancel"))
            {
                currentState = TrafEditState.IDLE;
            }
            break;

        case TrafEditState.EDIT_MULTI_TWOWAY:
            laneWidth = EditorGUILayout.Slider("lane width", laneWidth, 0f, 50f);

            if (GUILayout.Button("save"))
            {
                var t = target as TrafSystem;
                currentState = TrafEditState.IDLE;
                t.entries.Add(new TrafEntry()
                {
                    road = InitRoadMulti(currentRoad, laneWidth * -1.5f, true), identifier = currentId, subIdentifier = 0
                });
                t.entries.Add(new TrafEntry()
                {
                    road = InitRoadMulti(currentRoad, laneWidth * -.5f, true), identifier = currentId, subIdentifier = 1
                });
                t.entries.Add(new TrafEntry()
                {
                    road = InitRoadMulti(currentRoad, laneWidth * .5f, false), identifier = currentId, subIdentifier = 2
                });
                t.entries.Add(new TrafEntry()
                {
                    road = InitRoadMulti(currentRoad, laneWidth * 1.5f, false), identifier = currentId, subIdentifier = 3
                });
            }
            break;

        case TrafEditState.EDIT_MULTI_RA:
            laneWidth = EditorGUILayout.Slider("lane width", laneWidth, 0f, 50f);
            if (GUILayout.Button("save"))
            {
                var t = target as TrafSystem;
                currentState = TrafEditState.IDLE;
                t.entries.Add(new TrafEntry()
                {
                    identifier = currentId, subIdentifier = 0, waypoints = currentRoadMulti[0]
                });
                t.entries.Add(new TrafEntry()
                {
                    identifier = currentId, subIdentifier = 1, waypoints = currentRoadMulti[1]
                });
                t.entries.Add(new TrafEntry()
                {
                    identifier = currentId, subIdentifier = 2, waypoints = currentRoadMulti[2]
                });
                t.entries.Add(new TrafEntry()
                {
                    identifier = currentId, subIdentifier = 3, waypoints = currentRoadMulti[3]
                });
            }
            break;

        case TrafEditState.DISPLAY:



            if (GUILayout.Button("Back"))
            {
                currentState = TrafEditState.IDLE;
                SceneView.RepaintAll();
            }

            showIds = GUILayout.Toggle(showIds, "Show IDs?");

            break;
        }


        EditorGUILayout.EndVertical();
    }