Beispiel #1
0
        /// <summary>
        /// Saves the height map data at the specified position and area.
        /// </summary>
        /// <returns>Undo buffer information.</returns>
        /// <param name="terrain">Terrain to get undo information from.</param>
        /// <param name="xs">X position of top-left corner.</param>
        /// <param name="ys">Y position of top-left corner.</param>
        /// <param name="dimension">Dimension of undo area.</param>
        public Documents.SceneDocument.TerrainDeformUndoInfo?GetHeightMapUndo(QuadTerrainComponent terrain, int xs, int ys, int dimension)
        {
            // Create undo array
            float[] undo = new float[dimension * dimension];

            // Get data
            float[] data = terrain.GetHeight();

            // Capture all valid height pixels
            for (int y = ys; y < ys + dimension; y++)
            {
                // Cannot go out of bounds
                if (y < 0 || y >= mapDimension)
                {
                    continue;
                }

                for (int x = xs; x < xs + dimension; x++)
                {
                    // Cannot go out of bounds
                    if (x < 0 || x >= mapDimension)
                    {
                        continue;
                    }

                    // Get height pixel
                    float value = data[y * mapDimension + x];

                    // Save height pixel
                    int xd = (x - xs);
                    int yd = (y - ys);
                    if (xd < 0 || xd >= dimension)
                    {
                        continue;
                    }
                    if (yd < 0 || yd >= dimension)
                    {
                        continue;
                    }
                    undo[yd * dimension + xd] = value;
                }
            }

            // Create undo information
            Documents.SceneDocument.TerrainDeformUndoInfo undoInfo = new Documents.SceneDocument.TerrainDeformUndoInfo
            {
                TerrainEditor    = this,
                TerrainComponent = terrain,
                HeightMap        = heightMapData,
                UndoBuffer       = undo,
                Position         = new Microsoft.Xna.Framework.Point(xs, ys),
                Dimension        = dimension,
            };

            return(undoInfo);
        }
Beispiel #2
0
        /// <summary>
        /// Restores undo information to the height map.
        /// </summary>
        /// <param name="undoInfo">Undo information.</param>
        public void RestoreHeightMapUndo(Documents.SceneDocument.TerrainDeformUndoInfo undoInfo)
        {
            // Get area of undo operation
            int xs        = undoInfo.Position.X;
            int ys        = undoInfo.Position.Y;
            int dimension = undoInfo.Dimension;

            // Set all valid height pixels
            QuadTerrainComponent terrain = undoInfo.TerrainComponent;

            float[] data = terrain.GetHeight();
            for (int y = ys; y < ys + dimension; y++)
            {
                // Cannot go out of bounds
                if (y < 0 || y >= mapDimension)
                {
                    continue;
                }

                for (int x = xs; x < xs + dimension; x++)
                {
                    // Cannot go out of bounds
                    if (x < 0 || x >= mapDimension)
                    {
                        continue;
                    }

                    // Get height pixel
                    int xd = (x - xs);
                    int yd = (y - ys);
                    if (xd < 0 || xd >= dimension)
                    {
                        continue;
                    }
                    if (yd < 0 || yd >= dimension)
                    {
                        continue;
                    }
                    float value = undoInfo.UndoBuffer[yd * dimension + xd];

                    // Set height pixel
                    data[y * terrain.MapDimension + x] = value;
                }
            }

            // Set height back in editor if this is active terrain
            if (terrain == this.terrain)
            {
                heightMapData = data;
                UpdatePreview();
            }

            // Set data back on terrain
            terrain.SetHeight(data);
        }