Improve color API and add material API

This commit is contained in:
Norbi Peti 2021-04-16 01:40:30 +02:00
parent 124ef410c7
commit 9a4ff858f3
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
6 changed files with 62 additions and 47 deletions

View file

@ -40,18 +40,18 @@ namespace GamecraftModdingAPI
/// </summary> /// </summary>
/// <param name="block">The block's type</param> /// <param name="block">The block's type</param>
/// <param name="color">The block's color</param> /// <param name="color">The block's color</param>
/// <param name="darkness">The block color's darkness (0-9) - 0 is default color</param> /// <param name="material">The block's material</param>
/// <param name="position">The block's position - default block size is 0.2</param> /// <param name="position">The block's position - default block size is 0.2</param>
/// <param name="rotation">The block's rotation in degrees</param> /// <param name="rotation">The block's rotation in degrees</param>
/// <param name="uscale">The block's uniform scale - default scale is 1 (with 0.2 width)</param> /// <param name="uscale">The block's uniform scale - default scale is 1 (with 0.2 width)</param>
/// <param name="scale">The block's non-uniform scale - 0 means <paramref name="uscale"/> is used</param> /// <param name="scale">The block's non-uniform scale - 0 means <paramref name="uscale"/> is used</param>
/// <param name="player">The player who placed the block</param> /// <param name="player">The player who placed the block</param>
/// <returns>The placed block or null if failed</returns> /// <returns>The placed block or null if failed</returns>
public static Block PlaceNew(BlockIDs block, float3 position, public static Block PlaceNew(BlockIDs block, float3 position, float3 rotation = default,
float3 rotation = default, BlockColors color = BlockColors.Default, byte darkness = 0, BlockColors color = BlockColors.Default, BlockMaterial material = BlockMaterial.Default,
int uscale = 1, float3 scale = default, Player player = null) int uscale = 1, float3 scale = default, Player player = null)
{ {
return PlaceNew<Block>(block, position, rotation, color, darkness, uscale, scale, player); return PlaceNew<Block>(block, position, rotation, color, material, uscale, scale, player);
} }
/// <summary> /// <summary>
@ -61,7 +61,7 @@ namespace GamecraftModdingAPI
/// </summary> /// </summary>
/// <param name="block">The block's type</param> /// <param name="block">The block's type</param>
/// <param name="color">The block's color</param> /// <param name="color">The block's color</param>
/// <param name="darkness">The block color's darkness (0-9) - 0 is default color</param> /// <param name="material">The block's materialr</param>
/// <param name="position">The block's position - default block size is 0.2</param> /// <param name="position">The block's position - default block size is 0.2</param>
/// <param name="rotation">The block's rotation in degrees</param> /// <param name="rotation">The block's rotation in degrees</param>
/// <param name="uscale">The block's uniform scale - default scale is 1 (with 0.2 width)</param> /// <param name="uscale">The block's uniform scale - default scale is 1 (with 0.2 width)</param>
@ -69,12 +69,12 @@ namespace GamecraftModdingAPI
/// <param name="player">The player who placed the block</param> /// <param name="player">The player who placed the block</param>
/// <returns>The placed block or null if failed</returns> /// <returns>The placed block or null if failed</returns>
public static T PlaceNew<T>(BlockIDs block, float3 position, public static T PlaceNew<T>(BlockIDs block, float3 position,
float3 rotation = default, BlockColors color = BlockColors.Default, byte darkness = 0, float3 rotation = default, BlockColor? color = null, BlockMaterial material = BlockMaterial.Default,
int uscale = 1, float3 scale = default, Player player = null) where T : Block int uscale = 1, float3 scale = default, Player player = null) where T : Block
{ {
if (PlacementEngine.IsInGame && GameState.IsBuildMode()) if (PlacementEngine.IsInGame && GameState.IsBuildMode())
{ {
var egid = PlacementEngine.PlaceBlock(block, color, darkness, var egid = PlacementEngine.PlaceBlock(block, color ?? BlockColors.Default, material,
position, uscale, scale, player, rotation, out var initializer); position, uscale, scale, player, rotation, out var initializer);
var bl = New<T>(egid.entityID, egid.groupID); var bl = New<T>(egid.entityID, egid.groupID);
bl.InitData.Group = BlockEngine.InitGroup(initializer); bl.InitData.Group = BlockEngine.InitGroup(initializer);
@ -319,9 +319,7 @@ namespace GamecraftModdingAPI
{ {
BlockEngine.SetBlockInfo(this, (ref ColourParameterEntityStruct color, BlockColor val) => BlockEngine.SetBlockInfo(this, (ref ColourParameterEntityStruct color, BlockColor val) =>
{ {
color.indexInPalette = (byte) (val.Color + val.Darkness * 10); color.indexInPalette = val.Index;
//color.overridePaletteColour = false;
//color.needsUpdate = true;
color.hasNetworkChange = true; color.hasNetworkChange = true;
color.paletteColour = BlockEngine.ConvertBlockColor(color.indexInPalette); color.paletteColour = BlockEngine.ConvertBlockColor(color.indexInPalette);
}, value); }, value);
@ -339,13 +337,18 @@ namespace GamecraftModdingAPI
BlockEngine.SetBlockInfo(this, (ref ColourParameterEntityStruct color, float4 val) => BlockEngine.SetBlockInfo(this, (ref ColourParameterEntityStruct color, float4 val) =>
{ {
color.paletteColour = val; color.paletteColour = val;
//color.overridePaletteColour = true;
//color.needsUpdate = true;
color.hasNetworkChange = true; color.hasNetworkChange = true;
}, value); }, value);
} }
} }
public BlockMaterial Material
{
get => BlockEngine.GetBlockInfo(this, (CubeMaterialStruct cmst) => (BlockMaterial) cmst.materialId);
set => BlockEngine.SetBlockInfo(this,
(ref CubeMaterialStruct cmst, BlockMaterial val) => cmst.materialId = (byte) val, value);
}
/// <summary> /// <summary>
/// The text displayed on the block if applicable, or null. /// The text displayed on the block if applicable, or null.
/// Setting it is temporary to the session, it won't be saved. /// Setting it is temporary to the session, it won't be saved.
@ -433,7 +436,7 @@ namespace GamecraftModdingAPI
/// <returns></returns> /// <returns></returns>
public T Copy<T>() where T : Block public T Copy<T>() where T : Block
{ {
var block = PlaceNew<T>(Type, Position, Rotation, Color.Color, Color.Darkness, UniformScale, Scale); var block = PlaceNew<T>(Type, Position, Rotation, Color, Material, UniformScale, Scale);
block.copiedFrom = Id; block.copiedFrom = Id;
return block; return block;
} }

View file

@ -5,35 +5,35 @@ namespace GamecraftModdingAPI.Blocks
{ {
public struct BlockColor public struct BlockColor
{ {
public BlockColors Color; public BlockColors Color => Index == byte.MaxValue
public byte Darkness; ? BlockColors.Default
: (BlockColors) (Index % 10);
public byte Index => Color == BlockColors.Default public byte Darkness => (byte) (Index == byte.MaxValue
? byte.MaxValue ? 0
: (byte) (Darkness * 10 + Color); : Index / 10);
public byte Index { get; }
public BlockColor(byte index) public BlockColor(byte index)
{ {
if (index == byte.MaxValue) if (index > 99 && index != byte.MaxValue)
{
Color = BlockColors.Default;
Darkness = 0;
}
else
{
if (index > 99)
throw new ArgumentOutOfRangeException(nameof(index), "Invalid color index. Must be 0-90 or 255."); throw new ArgumentOutOfRangeException(nameof(index), "Invalid color index. Must be 0-90 or 255.");
Color = (BlockColors) (index % 10); Index = index;
Darkness = (byte) (index / 10);
}
} }
public BlockColor(BlockColors color, byte darkness) public BlockColor(BlockColors color, byte darkness = 0)
{ {
if (darkness > 9) if (darkness > 9)
throw new ArgumentOutOfRangeException(nameof(darkness), "Darkness must be 0-9 where 0 is default."); throw new ArgumentOutOfRangeException(nameof(darkness), "Darkness must be 0-9 where 0 is default.");
Color = color; if (color > BlockColors.Red) //Last valid color
Darkness = darkness; throw new ArgumentOutOfRangeException(nameof(color), "Invalid color!");
Index = (byte) (darkness * 10 + (byte) color);
}
public static implicit operator BlockColor(BlockColors color)
{
return new BlockColor(color);
} }
public float4 RGBA => Block.BlockEngine.ConvertBlockColor(Index); public float4 RGBA => Block.BlockEngine.ConvertBlockColor(Index);
@ -47,7 +47,7 @@ namespace GamecraftModdingAPI.Blocks
/// <summary> /// <summary>
/// Preset block colours /// Preset block colours
/// </summary> /// </summary>
public enum BlockColors public enum BlockColors : byte
{ {
Default = byte.MaxValue, Default = byte.MaxValue,
White = 0, White = 0,

View file

@ -0,0 +1,12 @@
namespace GamecraftModdingAPI.Blocks
{
public enum BlockMaterial : byte
{
Default = byte.MaxValue,
SteelBodywork = 0,
RigidSteel,
CarbonFiber,
Plastic,
Wood
}
}

View file

@ -40,17 +40,17 @@ namespace GamecraftModdingAPI.Blocks
public EntitiesDB entitiesDB { get; set; } public EntitiesDB entitiesDB { get; set; }
private static BlockEntityFactory _blockEntityFactory; //Injected from PlaceSingleBlockEngine private static BlockEntityFactory _blockEntityFactory; //Injected from PlaceSingleBlockEngine
public EGID PlaceBlock(BlockIDs block, BlockColors color, byte darkness, float3 position, int uscale, public EGID PlaceBlock(BlockIDs block, BlockColor color, BlockMaterial materialId, float3 position, int uscale,
float3 scale, Player player, float3 rotation, out EntityInitializer initializer) float3 scale, Player player, float3 rotation, out EntityInitializer initializer)
{ //It appears that only the non-uniform scale has any visible effect, but if that's not given here it will be set to the uniform one { //It appears that only the non-uniform scale has any visible effect, but if that's not given here it will be set to the uniform one
if (darkness > 9) if (color.Darkness > 9)
throw new Exception("That is too dark. Make sure to use 0-9 as darkness. (0 is default.)"); throw new Exception("That is too dark. Make sure to use 0-9 as darkness. (0 is default.)");
initializer = BuildBlock((ushort) block, (byte) (color + darkness * 10), position, uscale, scale, rotation, initializer = BuildBlock((ushort) block, color.Index, (byte) materialId, position, uscale, scale, rotation,
(player ?? new Player(PlayerType.Local)).Id); (player ?? new Player(PlayerType.Local)).Id);
return initializer.EGID; return initializer.EGID;
} }
private EntityInitializer BuildBlock(ushort block, byte color, float3 position, int uscale, float3 scale, float3 rot, uint playerId) private EntityInitializer BuildBlock(ushort block, byte color, byte materialId, float3 position, int uscale, float3 scale, float3 rot, uint playerId)
{ {
if (_blockEntityFactory == null) if (_blockEntityFactory == null)
throw new BlockException("The factory is null."); throw new BlockException("The factory is null.");
@ -81,10 +81,10 @@ namespace GamecraftModdingAPI.Blocks
indexInPalette = color, indexInPalette = color,
hasNetworkChange = true hasNetworkChange = true
}); });
if (materialId != byte.MaxValue)
structInitializer.Init(new CubeMaterialStruct structInitializer.Init(new CubeMaterialStruct
{ {
materialId = 0, //TODO materialId = materialId
cosmeticallyPaintedOnly = true //TODO
}); });
uint prefabId = PrefabsID.GetOrCreatePrefabID(block, 0, 0, false); //TODO uint prefabId = PrefabsID.GetOrCreatePrefabID(block, 0, 0, false); //TODO
structInitializer.Init(new GFXPrefabEntityStructGPUI(prefabId)); structInitializer.Init(new GFXPrefabEntityStructGPUI(prefabId));

View file

@ -224,8 +224,8 @@ namespace GamecraftModdingAPI.Tests
float.Parse(s[1]), float.Parse(s[2]), float.Parse(s[3])); float.Parse(s[1]), float.Parse(s[2]), float.Parse(s[3]));
return; return;
} }
new Player(PlayerType.Local).GetBlockLookedAt().Color =
new BlockColor { Color = color }; new Player(PlayerType.Local).GetBlockLookedAt().Color = color;
Logging.CommandLog("Colored block to " + color); Logging.CommandLog("Colored block to " + color);
}).Build(); }).Build();

View file

@ -31,8 +31,8 @@ namespace GamecraftModdingAPI.Utility
public void SetInfo(string id, Func<string> contentGetter) => _extraInfo[id] = contentGetter; public void SetInfo(string id, Func<string> contentGetter) => _extraInfo[id] = contentGetter;
public bool RemoveInfo(string id) => _extraInfo.Remove(id); public bool RemoveInfo(string id) => _extraInfo.Remove(id);
public string Name { get; } = "GamecraftModdingAPIDebugInterfaceGameEngine"; public string Name => "GamecraftModdingAPIDebugInterfaceGameEngine";
public bool isRemovable { get; } = true; public bool isRemovable => true;
[HarmonyPatch] [HarmonyPatch]
private class Patch private class Patch