Esempio n. 1
0
        public static void DrawSceneGUI(MapMagicObject mapMagic, SelectionMode selectionMode)
        {
            Dictionary <Coord, TerrainTile> tilesLut = new Dictionary <Coord, TerrainTile>(mapMagic.tiles.grid);

            if (selectionMode != SelectionMode.none && !Event.current.alt)
            {
                //returning if no scene veiw (right after script compile)
                SceneView sceneview = UnityEditor.SceneView.lastActiveSceneView;
                if (sceneview == null || sceneview.camera == null)
                {
                    return;
                }

                //disabling selection
                HandleUtility.AddDefaultControl(GUIUtility.GetControlID(FocusType.Passive));

                //preparing the sets of both custom and tile terrains
                HashSet <Terrain> pinnedCustomTerrains = new HashSet <Terrain>();               //custom terrains that were already pinned before
                foreach (TerrainTile tile in mapMagic.tiles.customTiles)
                {
                    if (tile.main != null)
                    {
                        pinnedCustomTerrains.Add(tile.main.terrain);
                    }
                    if (tile.draft != null)
                    {
                        pinnedCustomTerrains.Add(tile.draft.terrain);
                    }
                }

                HashSet <Terrain> pinnedTileTerrains = new HashSet <Terrain>();
                foreach (var kvp in tilesLut)
                {
                    TerrainTile tile = kvp.Value;
                    if (tile.main != null)
                    {
                        pinnedTileTerrains.Add(tile.main.terrain);
                    }
                    if (tile.draft != null)
                    {
                        pinnedTileTerrains.Add(tile.draft.terrain);
                    }
                }

                if (selectionMode == SelectionMode.pin)
                {
                    Handles.color = FrameDraw.pinColor;

                    List <Coord> selectedCoords = SelectTiles((Vector3)mapMagic.tileSize, false, tilesLut, mapMagic.transform);
                    if (selectedCoords != null)
                    {
                        UnityEditor.Undo.RegisterFullObjectHierarchyUndo(mapMagic.gameObject, "MapMagic Pin Terrains");

                        foreach (Coord coord in selectedCoords)
                        {
                            mapMagic.tiles.Pin(coord, false, mapMagic);
                        }

                        foreach (Coord coord in selectedCoords)
                        {
                            UnityEditor.Undo.RegisterCreatedObjectUndo(mapMagic.tiles[coord].gameObject, "MapMagic Pin Terrains");
                        }
                    }
                }

                if (selectionMode == SelectionMode.pinLowres)
                {
                    Handles.color = FrameDraw.pinColor;

                    List <Coord> selectedCoords = SelectTiles((Vector3)mapMagic.tileSize, true, tilesLut, mapMagic.transform);
                    if (selectedCoords != null)
                    {
                        UnityEditor.Undo.RegisterFullObjectHierarchyUndo(mapMagic.gameObject, "MapMagic Pin Draft Terrains");

                        foreach (Coord coord in selectedCoords)
                        {
                            mapMagic.tiles.Pin(coord, true, mapMagic);
                        }

                        foreach (Coord coord in selectedCoords)
                        {
                            UnityEditor.Undo.RegisterCreatedObjectUndo(mapMagic.tiles[coord].gameObject, "MapMagic Pin Terrains");
                        }
                    }
                }

                if (selectionMode == SelectionMode.pinExisting)
                {
                    //excluding tiles
                    HashSet <Terrain> possibleTerrains = new HashSet <Terrain>();
                    Terrain[]         allTerrains      = GameObject.FindObjectsOfType <Terrain>();
                    foreach (Terrain terrain in allTerrains)
                    {
                        if (!pinnedTileTerrains.Contains(terrain))
                        {
                            possibleTerrains.Add(terrain);
                        }
                    }

                    HashSet <Terrain> selectedTerrains = SelectTerrains(possibleTerrains, FrameDraw.pinColor, false);
                    if (selectedTerrains != null)
                    {
                        UnityEditor.Undo.RegisterFullObjectHierarchyUndo(mapMagic.gameObject, "MapMagic Pin Terrains");

                        foreach (Terrain terrain in selectedTerrains)
                        {
                            if (pinnedCustomTerrains.Contains(terrain))
                            {
                                continue;
                            }
                            terrain.transform.parent = mapMagic.transform;

                            TerrainTile tile = terrain.gameObject.GetComponent <TerrainTile>();
                            if (tile == null)
                            {
                                tile = terrain.gameObject.AddComponent <TerrainTile>();
                            }
                            tile.main.terrain = terrain;
                            //tile.main.area = new Terrains.Area(terrain);
                            //tile.main.use = true;
                            //tile.lodData = null;

                            mapMagic.tiles.PinCustom(tile);
                            mapMagic.StartGenerate(tile);
                        }
                    }
                }


                if (selectionMode == SelectionMode.selectPreview)
                {
                    //hash set of all pinned terrains contains main data
                    HashSet <Terrain> pinnedMainTerrains = new HashSet <Terrain>();
                    foreach (var kvp in tilesLut)
                    {
                        TerrainTile tile = kvp.Value;
                        if (tile.main != null)
                        {
                            pinnedMainTerrains.Add(tile.main.terrain);
                        }
                    }
                    pinnedMainTerrains.UnionWith(pinnedCustomTerrains);

                    HashSet <Terrain> selectedTerrains = SelectTerrains(pinnedMainTerrains, FrameDraw.selectPreviewColor, false);
                    if (selectedTerrains != null && selectedTerrains.Count == 1)
                    {
                        UnityEditor.Undo.RegisterCompleteObjectUndo(mapMagic, "MapMagic Select Preview");

                        Terrain selectedTerrain = selectedTerrains.Any();

                        //clearing preview
                        if (selectedTerrain == mapMagic.AssignedPreviewTerrain)
                        {
                            mapMagic.ClearPreviewTile();
                            TerrainTile.OnPreviewAssigned(mapMagic.PreviewData);
                        }

                        //assigning new
                        else
                        {
                            foreach (var kvp in tilesLut)
                            {
                                TerrainTile tile = kvp.Value;
                                if (tile.main?.terrain == selectedTerrain && mapMagic.AssignedPreviewTerrain != selectedTerrain)
                                {
                                    mapMagic.AssignPreviewTile(tile);
                                    mapMagic.AssignedPreviewData.isPreview = true;
                                    TerrainTile.OnPreviewAssigned(mapMagic.AssignedPreviewData);

                                    UI.RepaintAllWindows();
                                }
                            }
                        }
                    }
                }


                if (selectionMode == SelectionMode.unpin)
                {
                    HashSet <Terrain> possibleTerrains = new HashSet <Terrain>(pinnedTileTerrains);
                    possibleTerrains.UnionWith(pinnedCustomTerrains);

                    HashSet <Terrain> selectedTerrains = SelectTerrains(possibleTerrains, FrameDraw.unpinColor, false);
                    if (selectedTerrains != null)
                    {
                        UnityEditor.Undo.RegisterFullObjectHierarchyUndo(mapMagic.gameObject, "MapMagic Unpin Terrains");

                        foreach (Terrain terrain in selectedTerrains)
                        {
                            //terrain-to-coord lut (to pick tile terrain coord faster)
                            Dictionary <Terrain, Coord> terrainToCoordLut = new Dictionary <Terrain, Coord>();
                            foreach (var kvp in tilesLut)
                            {
                                Coord       coord = kvp.Key;
                                TerrainTile tile  = kvp.Value;
                                if (tile.main != null)
                                {
                                    terrainToCoordLut.Add(tile.main.terrain, coord);
                                }
                                if (tile.draft != null)
                                {
                                    terrainToCoordLut.Add(tile.draft.terrain, coord);
                                }
                            }

                            //if it's tile
                            if (terrainToCoordLut.ContainsKey(terrain))
                            {
                                Coord coord = terrainToCoordLut[terrain];
                                mapMagic.tiles.Unpin(coord);
                            }

                            //if it's custom
                            if (pinnedCustomTerrains.Contains(terrain))
                            {
                                TerrainTile tileComponent = terrain.gameObject.GetComponent <TerrainTile>();
                                mapMagic.tiles.UnpinCustom(tileComponent);
                                GameObject.DestroyImmediate(tileComponent);
                            }
                        }
                    }
                }

                //redrawing scene
                SceneView.lastActiveSceneView.Repaint();
            }
        }