Skip to content

Commit

Permalink
Added input system.
Browse files Browse the repository at this point in the history
  • Loading branch information
JunaMeinhold committed May 4, 2024
1 parent 5656a8e commit 7c19451
Show file tree
Hide file tree
Showing 62 changed files with 2,430 additions and 1,241 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
using System.Numerics;

/// <summary>
/// Represents a layer mask used for terrain rendering.
/// Represents a layer blend mask used for terrain rendering.
/// </summary>
public class LayerMask
public class LayerBlendMask
{
private uint width;
private uint height;
Expand All @@ -19,37 +19,39 @@ public class LayerMask

#nullable disable

private LayerMask()
private LayerBlendMask()
{
}

#nullable restore

/// <summary>
/// Initializes a new instance of the <see cref="LayerMask"/> class with the specified width, height, and data.
/// Initializes a new instance of the <see cref="LayerBlendMask"/> class with the specified width, height, and data.
/// </summary>
/// <param name="width">The width of the layer mask.</param>
/// <param name="height">The height of the layer mask.</param>
/// <param name="data">The data of the layer mask.</param>
public LayerMask(uint width, uint height, ulong[] data)
public LayerBlendMask(uint width, uint height, ulong[] data)
{
this.width = width;
this.height = height;
this.data = data;
}

/// <summary>
/// Initializes a new instance of the <see cref="LayerMask"/> class with the specified width and height.
/// Initializes a new instance of the <see cref="LayerBlendMask"/> class with the specified width and height.
/// </summary>
/// <param name="width">The width of the layer mask.</param>
/// <param name="height">The height of the layer mask.</param>
public LayerMask(uint width = 1024, uint height = 1024)
public LayerBlendMask(uint width = 1024, uint height = 1024)
{
this.width = width;
this.height = height;
data = new ulong[width * height];
}

public ulong[] Data => data;

/// <summary>
/// Gets or sets the layer mask data at the specified index.
/// </summary>
Expand Down Expand Up @@ -131,16 +133,16 @@ public uint GetIndexFor(uint x, uint y)
}

/// <summary>
/// Reads a <see cref="LayerMask"/> from a stream.
/// Reads a <see cref="LayerBlendMask"/> from a stream.
/// </summary>
/// <param name="src">The source stream.</param>
/// <param name="endianness">The endianness used for reading.</param>
/// <param name="compression"></param>
/// <param name="mode"></param>
/// <returns>The read height map.</returns>
public static LayerMask ReadFrom(Stream src, Endianness endianness, Compression compression, TerrainLoadMode mode)
public static LayerBlendMask ReadFrom(Stream src, Endianness endianness, Compression compression, TerrainLoadMode mode)
{
LayerMask layerMask = new();
LayerBlendMask layerMask = new();
layerMask.Read(src, endianness, compression, mode);
return layerMask;
}
Expand Down Expand Up @@ -176,6 +178,7 @@ public void ReadMaskData(Stream stream)
return;
}

stream.Position = streamPosition;
var decompressor = stream.CreateDecompressionStream(compression, out var isCompressed);

data = new ulong[width * height];
Expand Down
53 changes: 53 additions & 0 deletions HexaEngine.Core/IO/Binary/Terrains/TerrainCellData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
using HexaEngine.Core.IO;
using HexaEngine.Core.IO.Binary.Meshes;
using HexaEngine.Mathematics;
using K4os.Compression.LZ4;
using System.IO;
using System.Numerics;

/// <summary>
/// Represents a terrain mesh generated from a height map.
Expand Down Expand Up @@ -293,5 +295,56 @@ public void AverageEdgeLevel(int level, Edge xPos, TerrainCellData other)
var lodSelf = lods[level];
lodSelf.AverageEdge(xPos, lodOther);
}

public void FuseHeightMap(Edge edge, TerrainCellData other)
{
uint rows = heightMap.Width;
uint columns = heightMap.Height;
if (heightMap.Width != other.heightMap.Width || heightMap.Height != other.heightMap.Height)
{
throw new InvalidOperationException($"Terrain cell height map dimensions must be the same. (Width: {rows}, Height: {columns}), but other was (Width: {other.heightMap.Width}, Height: {other.heightMap.Height})");
}

if (edge == Edge.ZPos)
{
for (uint i = 0; i < rows; i++)
{
var indexA = heightMap.GetIndexFor(i, columns - 1);
var indexB = heightMap.GetIndexFor(i, 0);

heightMap[indexA] = other.heightMap[indexB] = (heightMap[indexA] + other.heightMap[indexB]) / 2;
}
}
if (edge == Edge.ZNeg)
{
for (uint i = 0; i < rows; i++)
{
var indexA = heightMap.GetIndexFor(i, 0);
var indexB = heightMap.GetIndexFor(i, columns - 1);

heightMap[indexA] = other.heightMap[indexB] = (heightMap[indexA] + other.heightMap[indexB]) / 2;
}
}
if (edge == Edge.XPos)
{
for (uint i = 0; i < columns; i++)
{
var indexA = heightMap.GetIndexFor(rows - 1, i);
var indexB = heightMap.GetIndexFor(0, i);

heightMap[indexA] = other.heightMap[indexB] = (heightMap[indexA] + other.heightMap[indexB]) / 2;
}
}
if (edge == Edge.XNeg)
{
for (uint i = 0; i < columns; i++)
{
var indexA = heightMap.GetIndexFor(0, i);
var indexB = heightMap.GetIndexFor(rows - 1, i);

heightMap[indexA] = other.heightMap[indexB] = (heightMap[indexA] + other.heightMap[indexB]) / 2;
}
}
}
}
}
6 changes: 3 additions & 3 deletions HexaEngine.Core/IO/Binary/Terrains/TerrainLayerGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class TerrainLayerGroup
/// <summary>
/// The mask associated with the layer group.
/// </summary>
public LayerMask Mask = new();
public LayerBlendMask Mask = new();

/// <summary>
/// Initializes a new instance of the <see cref="TerrainLayerGroup"/> class.
Expand All @@ -38,7 +38,7 @@ public TerrainLayerGroup()
/// <param name="layers">The array of terrain layers in the group.</param>
/// <param name="layerCount">The number of layers in the group.</param>
/// <param name="mask">The mask associated with the layer group.</param>
public TerrainLayerGroup(TerrainLayer[] layers, int layerCount, LayerMask mask)
public TerrainLayerGroup(TerrainLayer[] layers, int layerCount, LayerBlendMask mask)
{
this.layers = layers;
count = layerCount;
Expand Down Expand Up @@ -170,7 +170,7 @@ public static TerrainLayerGroup Read(Stream stream, Endianness endianness, Compr
}
layersInGroup[i] = layers[idx];
}
LayerMask layerMask = LayerMask.ReadFrom(stream, endianness, compression, mode);
LayerBlendMask layerMask = LayerBlendMask.ReadFrom(stream, endianness, compression, mode);
return new TerrainLayerGroup(layersInGroup, layerCount, layerMask);
}
}
Expand Down
4 changes: 2 additions & 2 deletions HexaEngine.Core/IO/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ public static Stream CreateCompressionStream(this Stream stream, Compression com
if (compression == Compression.Deflate)
{
isCompressed = true;
return new DeflateStream(stream, CompressionLevel.SmallestSize, true);
return new DeflateStream(stream, CompressionLevel.Optimal, true);
}

if (compression == Compression.LZ4)
{
isCompressed = true;
return LZ4Stream.Encode(stream, LZ4Level.L10_OPT, 0, true);
return LZ4Stream.Encode(stream, LZ4Level.L00_FAST, 0, true);
}

isCompressed = false;
Expand Down
4 changes: 2 additions & 2 deletions HexaEngine.Core/Input/Mouse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ internal static void OnMotion(MouseMotionEvent mouseMotionEvent)
motionEventArgs.Timestamp = mouseMotionEvent.Timestamp;
motionEventArgs.Handled = false;
motionEventArgs.MouseId = mouseMotionEvent.Which;
motionEventArgs.RelX = delta.X;
motionEventArgs.RelY = delta.Y;
motionEventArgs.RelX = mouseMotionEvent.Xrel;
motionEventArgs.RelY = mouseMotionEvent.Yrel;
motionEventArgs.X = pos.X;
motionEventArgs.Y = pos.Y;
Moved?.Invoke(null, motionEventArgs);
Expand Down
48 changes: 46 additions & 2 deletions HexaEngine.Editor/Projects/HexaProjectWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,59 @@ public static HexaProject CreateEmpty()
return new();
}

public static HexaProject CreateNew()
public static HexaProject CreateNew(string folder)
{
HexaProject project = new();
PropertyGroup propertyGroup = [new("TargetVersion", "HexaEngine1.0")];
PropertyGroup propertyGroup = [new("TargetVersion", "HexaEngine1.0"), new("AppConfig", Path.Combine(folder, "app.config"))];
project.Items.Add(propertyGroup);
return project;
}

public List<HexaProjectItem> Items { get; } = [];

public PropertyGroupItem? GetPropertyGroupItem(string name)
{
for (int i = 0; i < Items.Count; i++)
{
var item = Items[i];
if (item is PropertyGroup group)
{
if (group.TryGetPropertyItem(name, out var groupItem))
{
return groupItem;
}
}
}

return null;
}

public PropertyGroupItem GetOrCreatePropertyGroupItem(string name, string defaultValue)
{
PropertyGroup? firstGroup = null;
for (int i = 0; i < Items.Count; i++)
{
var item = Items[i];
if (item is PropertyGroup group)
{
firstGroup ??= group;
if (group.TryGetPropertyItem(name, out var groupItem))
{
return groupItem;
}
}
}

if (firstGroup == null)
{
Items.Add(firstGroup = []);
}

PropertyGroupItem newGroupItem = new(name, defaultValue);
firstGroup.Add(newGroupItem);

return newGroupItem;
}
}

public class HexaProjectReader
Expand Down
25 changes: 16 additions & 9 deletions HexaEngine.Editor/Projects/ProjectManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ public static Task Load(string path)
CurrentProject = reader.Read() ?? throw new FileNotFoundException($"Couldn't find project file '{path}'");
var item = CurrentProject.GetOrCreatePropertyGroupItem("AppConfig", Path.Combine(CurrentProjectFolder, "app.config"));
Platform.AppConfig = AppConfig.Load(item.Value);
ProjectVersionControl.TryInit();
CurrentProjectAssetsFolder = Path.Combine(CurrentProjectFolder, "assets");
Expand All @@ -118,8 +122,10 @@ public static Task Load(string path)
SourceAssetsDatabase.Init(CurrentProjectFolder, popup);
watcher = new(Path.Combine(CurrentProjectFolder, solutionName));
watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.Size | NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.Attributes | NotifyFilters.CreationTime | NotifyFilters.Security;
watcher = new(Path.Combine(CurrentProjectFolder, solutionName))
{
NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.Size | NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.Attributes | NotifyFilters.CreationTime | NotifyFilters.Security
};
watcher.Changed += WatcherChanged;
watcher.Created += WatcherChanged;
watcher.Deleted += WatcherChanged;
Expand Down Expand Up @@ -239,7 +245,7 @@ private static string GenerateProject(string path)

string projectName = Path.GetFileName(path);
string projectFilePath = Path.Combine(path, $"{projectName}.hexproj");
HexaProject project = HexaProject.CreateNew();
HexaProject project = HexaProject.CreateNew(path);

HexaProjectWriter writer = new(projectFilePath);
writer.Write(project);
Expand Down Expand Up @@ -569,12 +575,13 @@ public static Task Publish(PublishSettings settings)

// app config
string configBuildPath = Path.Combine(buildPath, "app.config");
AppConfig config = new()
{
StartupScene = Path.Combine("assets", settings.StartupScene),
ScriptAssembly = assemblyPathRelative,
};
config.Save(configBuildPath);

AppConfig config = Platform.AppConfig?.Clone(configBuildPath) ?? new(configBuildPath);

config.StartupScene = Path.Combine("assets", settings.StartupScene);
config.ScriptAssembly = assemblyPathRelative;

config.SaveTo(configBuildPath);

// build app
string appTempPath = Path.Combine(CurrentProjectFolder, ".build");
Expand Down
4 changes: 2 additions & 2 deletions HexaEngine.Editor/Projects/PropertyGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public PropertyGroupItem GetPropertyItem(string name)
return nameToItem[name];
}

public bool TryGetPropertyItem(string name, [MaybeNullWhen(false)] out PropertyGroupItem? value)
public bool TryGetPropertyItem(string name, [NotNullWhen(true)] out PropertyGroupItem? value)
{
return nameToItem.TryGetValue(name, out value);
}
Expand All @@ -82,7 +82,7 @@ public string GetProperty(string name)
return nameToItem[name].Value;
}

public bool TryGetProperty(string name, [MaybeNullWhen(false)] out string? value)
public bool TryGetProperty(string name, [NotNullWhen(true)] out string? value)
{
if (nameToItem.TryGetValue(name, out var item))
{
Expand Down
4 changes: 4 additions & 0 deletions HexaEngine.Editor/SceneWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@ public static unsafe void Stop(Scene scene)
{
return;
}
ImGuiManager.DisableNav(false);
scene.IsSimulating = false;
SceneManager.BeginReload();
scene.RestoreState();
Expand All @@ -474,6 +475,7 @@ public static unsafe void Pause(Scene scene)
{
return;
}
ImGuiManager.DisableNav(false);
scene.IsSimulating = false;
Application.EditorPlayState = EditorPlayState.Pause;
}
Expand All @@ -497,6 +499,7 @@ public static unsafe void Play(Scene scene)
MessageBox.Show("Script Project Build Failed", "The script project build failed. Please fix any compilation errors before playing the scene.");
return;
}
ImGuiManager.DisableNav(true);
SceneManager.Save();
scene.IsSimulating = false;
SceneManager.BeginReload();
Expand All @@ -512,6 +515,7 @@ public static unsafe void Play(Scene scene)
{
return;
}
ImGuiManager.DisableNav(true);
scene.IsSimulating = true;
Application.EditorPlayState = EditorPlayState.Play;
}
Expand Down
Loading

0 comments on commit 7c19451

Please sign in to comment.