// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved.
#pragma once
#include "Engine/Scripting/ScriptingType.h"
class Terrain;
class Texture;
///
/// Terrain* tools for editor. Allows to create and modify terrain.
///
API_CLASS(Static, Namespace="FlaxEditor") class TerrainTools
{
DECLARE_SCRIPTING_TYPE_NO_SPAWN(TerrainTools);
///
/// Checks if a given ray hits any of the terrain patches sides to add a new patch there.
///
/// The terrain.
/// The ray to use for tracing (eg. mouse ray in world space).
/// The result patch coordinates (x and z). Valid only when method returns true.
/// True if result is valid, otherwise nothing to add there.
API_FUNCTION() static bool TryGetPatchCoordToAdd(Terrain* terrain, const Ray& ray, API_PARAM(Out) Int2& result);
///
/// Generates the terrain from the input heightmap and splat maps.
///
/// The terrain.
/// The number of patches (X and Z axis).
/// The heightmap texture.
/// The heightmap scale. Applied to adjust the normalized heightmap values into the world units.
/// The custom terrain splat map used as a source of the terrain layers weights. Each channel from RGBA is used as an independent layer weight for terrain layers composting. It's optional.
/// The custom terrain splat map used as a source of the terrain layers weights. Each channel from RGBA is used as an independent layer weight for terrain layers composting. It's optional.
/// True if failed, otherwise false.
API_FUNCTION() static bool GenerateTerrain(Terrain* terrain, API_PARAM(Ref) const Int2& numberOfPatches, Texture* heightmap, float heightmapScale, Texture* splatmap1, Texture* splatmap2);
///
/// Serializes the terrain chunk data to JSON string.
///
/// The terrain.
/// The patch coordinates (x and z).
/// The serialized chunk data.
API_FUNCTION() static StringAnsi SerializePatch(Terrain* terrain, API_PARAM(Ref) const Int2& patchCoord);
///
/// Deserializes the terrain chunk data from the JSON string.
///
/// The terrain.
/// The patch coordinates (x and z).
/// The JSON string with serialized patch data.
API_FUNCTION() static void DeserializePatch(Terrain* terrain, API_PARAM(Ref) const Int2& patchCoord, const StringAnsiView& value);
///
/// Initializes the patch heightmap and collision to the default flat level.
///
/// The terrain.
/// The patch coordinates (x and z) to initialize it.
/// True if failed, otherwise false.
API_FUNCTION() static bool InitializePatch(Terrain* terrain, API_PARAM(Ref) const Int2& patchCoord);
///
/// Modifies the terrain patch heightmap with the given samples.
///
/// The terrain.
/// The patch coordinates (x and z) to modify it.
/// The samples. The array length is size.X*size.Y. It has to be type of float.
/// The offset from the first row and column of the heightmap data (offset destination x and z start position).
/// The size of the heightmap to modify (x and z). Amount of samples in each direction.
/// True if failed, otherwise false.
API_FUNCTION() static bool ModifyHeightMap(Terrain* terrain, API_PARAM(Ref) const Int2& patchCoord, float* samples, API_PARAM(Ref) const Int2& offset, API_PARAM(Ref) const Int2& size);
///
/// Modifies the terrain patch holes mask with the given samples.
///
/// The terrain.
/// The patch coordinates (x and z) to modify it.
/// The samples. The array length is size.X*size.Y. It has to be type of byte.
/// The offset from the first row and column of the mask data (offset destination x and z start position).
/// The size of the mask to modify (x and z). Amount of samples in each direction.
/// True if failed, otherwise false.
API_FUNCTION() static bool ModifyHolesMask(Terrain* terrain, API_PARAM(Ref) const Int2& patchCoord, byte* samples, API_PARAM(Ref) const Int2& offset, API_PARAM(Ref) const Int2& size);
///
/// Modifies the terrain patch splat map (layers mask) with the given samples.
///
/// The terrain.
/// The patch coordinates (x and z) to modify it.
/// The zero-based splatmap texture index.
/// The samples. The array length is size.X*size.Y. It has to be type of .
/// The offset from the first row and column of the splatmap data (offset destination x and z start position).
/// The size of the splatmap to modify (x and z). Amount of samples in each direction.
/// True if failed, otherwise false.
API_FUNCTION() static bool ModifySplatMap(Terrain* terrain, API_PARAM(Ref) const Int2& patchCoord, int32 index, Color32* samples, API_PARAM(Ref) const Int2& offset, API_PARAM(Ref) const Int2& size);
///
/// Gets the raw pointer to the heightmap data (cached internally by the c++ core in editor).
///
/// The terrain.
/// The patch coordinates (x and z) to gather it.
/// The pointer to the array of floats with terrain patch heights data. Null if failed to gather the data.
API_FUNCTION() static float* GetHeightmapData(Terrain* terrain, API_PARAM(Ref) const Int2& patchCoord);
///
/// Gets the raw pointer to the holes mask data (cached internally by the c++ core in editor).
///
/// The terrain.
/// The patch coordinates (x and z) to gather it.
/// The pointer to the array of bytes with terrain patch holes mask data. Null if failed to gather the data.
API_FUNCTION() static byte* GetHolesMaskData(Terrain* terrain, API_PARAM(Ref) const Int2& patchCoord);
///
/// Gets the raw pointer to the splatmap data (cached internally by the c++ core in editor).
///
/// The terrain.
/// The patch coordinates (x and z) to gather it.
/// The zero-based splatmap texture index.
/// The pointer to the array of Color32 with terrain patch packed splatmap data. Null if failed to gather the data.
API_FUNCTION() static Color32* GetSplatMapData(Terrain* terrain, API_PARAM(Ref) const Int2& patchCoord, int32 index);
///
/// Export terrain's heightmap as a texture.
///
/// The terrain.
/// The output folder path
/// True if failed, otherwise false.
API_FUNCTION() static bool ExportTerrain(Terrain* terrain, String outputFolder);
};