Skip to content

Commit

Permalink
Added automatic translokator grids render update
Browse files Browse the repository at this point in the history
The grids now update accordingly when editing their properties, they also update when enabling a grid.
  • Loading branch information
Kamzik123 committed Dec 3, 2024
1 parent 8d4e455 commit 688066d
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 29 deletions.
46 changes: 43 additions & 3 deletions Mafia2Libs/Forms/MapEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,17 @@ private void UpdateAssetVisualisation(TreeNode node, TreeNode parent)
}
else if (node.Tag is Grid trGrid)
{
int trGridIndex = Array.IndexOf(SceneData.Translokator.Grids, trGrid);
Graphics.SetTranslokatorGridEnabled(trGridIndex, node.Checked && node.CheckIfParentsAreValid());
bool enabled = node.Checked && node.CheckIfParentsAreValid();

if (enabled)
{
RebuildTranslokatorGrids();
}
else
{
int trGridIndex = Array.IndexOf(SceneData.Translokator.Grids, trGrid);
Graphics.SetTranslokatorGridEnabled(trGridIndex, enabled);
}
}
}
else
Expand Down Expand Up @@ -1148,7 +1157,7 @@ private void BuildRenderObjects()
translokatorRoot.Nodes.Add(gridNode);

dSceneTree.AddToTree(translokatorRoot);
Graphics.SetTranslokatorGrid(SceneData.Translokator);
Graphics.BuildTranslokatorGrid(SceneData.Translokator);
}
}

Expand Down Expand Up @@ -1861,6 +1870,10 @@ private void OnPropertyValueChanged(object s, PropertyValueChangedEventArgs e)
Graphics.InstanceGizmo.UpdateInstanceBuffer(instance, Graphics.GetId3D11Device());
}
}
if (pGrid.SelectedObject is Grid trGrid)
{
RebuildTranslokatorGrids();
}

pGrid.Refresh();
}
Expand Down Expand Up @@ -2810,6 +2823,33 @@ private void DeleteTRObject(TreeNode objectNode)
}
dSceneTree.RemoveNode(objectNode);
}

private void RebuildTranslokatorGrids()
{
SceneData.Translokator.RebuildGridData();
Graphics.BuildTranslokatorGrid(SceneData.Translokator);

TreeNode gridsNode = null;

foreach (TreeNode node in translokatorRoot.Nodes)
{
if (node.Text.Equals("Grids", StringComparison.InvariantCultureIgnoreCase))
{
gridsNode = node;
break;
}
}

for (int i = 0; i < gridsNode.Nodes.Count; i++)
{
TreeNode child = gridsNode.Nodes[i];

if (child.Tag is Grid)
{
Graphics.SetTranslokatorGridEnabled(i, child.Checked && child.CheckIfParentsAreValid());
}
}
}
}
}

5 changes: 2 additions & 3 deletions Mafia2Libs/Rendering/Graphics/GraphicsClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,10 @@ public bool InitScene(int width, int height)
return true;
}

public void SetTranslokatorGrid(TranslokatorLoader translokator)
public void BuildTranslokatorGrid(TranslokatorLoader translokator)
{
translokatorGrid = new TranslokatorSpatialGrid(translokator);
translokatorGrid.Build(translokator);
translokatorGrid.Initialise(D3D.Device, D3D.DeviceContext);
//return translokatorGrid.GetTreeNodes();
}

public TreeNode SetNavigationGrid(ResourceTypes.Navigation.OBJData[] data)
Expand Down
26 changes: 5 additions & 21 deletions Mafia2Libs/Rendering/Spatial/TranslokatorSpatialGrid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public TranslokatorSpatialGrid()
}

public TranslokatorSpatialGrid(TranslokatorLoader translokator)
{
Build(translokator);
}

public void Build(TranslokatorLoader translokator)
{
bIsReady = true;

Expand Down Expand Up @@ -59,27 +64,6 @@ public TranslokatorSpatialGrid(TranslokatorLoader translokator)

cellBoundingBox[k].SetInstanceTransforms(CellTransforms);
}


/*for (int i = 0; i != translokator.ObjectGroups.Length; i++)
{
ObjectGroup objectGroup = translokator.ObjectGroups[i];
for (int x = 0; x != objectGroup.NumObjects; x++)
{
ResourceTypes.Translokator.Object obj = objectGroup.Objects[x];
for (int y = 0; y != obj.NumInstances; y++)
{
Instance instance = obj.Instances[y];
var cell = GetCell(instance.Position);
RenderBoundingBox box = new RenderBoundingBox();
box.SetTransform(Matrix4x4.CreateTranslation(instance.Position));
box.Init(new BoundingBox(-Vector3.One, Vector3.One));
cells[cell].AddAsset(box, RefManager.GetNewRefID());
}
}
}*/
}

public void Initialise(ID3D11Device device, ID3D11DeviceContext deviceContext)
Expand Down
47 changes: 45 additions & 2 deletions Mafia2Libs/ResourceTypes/FileTypes/Translokator/Translokator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ public short Key {
get { return key; }
set { key = value; }
}
[TypeConverter(typeof(Vector3Converter))]
[TypeConverter(typeof(Vector3Converter)), ReadOnly(true)]
public Vector3 Origin {
get { return origin; }
set { origin = value; }
}
[TypeConverter(typeof(Vector2Converter))]
[TypeConverter(typeof(Vector2Converter)), ReadOnly(true)]
public Vector2 CellSize {
get { return cellSize; }
set { cellSize = value; }
Expand Down Expand Up @@ -573,6 +573,49 @@ private void CompileData()
#endregion encode instance data / build grid
}

public void RebuildGridData()
{
for (int i = 0; i < Grids.Length; i++)
{
var grid = Grids[i];
grid.CellSize = new Vector2(bounds.GetWidth() / grid.Width, bounds.GetHeight() / grid.Height);
grid.Data = new ushort[grid.Width * grid.Height];
grid.Origin = bounds.Min;
}

for (int i = 0; i < ObjectGroups.Length; i++)
{
ObjectGroup objectGroup = ObjectGroups[i];

for (int x = 0; x < objectGroup.Objects.Length; x++)
{
Object obj = objectGroup.Objects[x];

for (int y = 0; y < obj.Instances.Length; y++)
{
Instance instance = obj.Instances[y];

for (int a = 0; a < Grids.Length; a++)
{
var grid = Grids[a];

if (obj.GridMax == grid.Key)
{
var offsetX = instance.Position.X - grid.Origin.X;
var offsetY = instance.Position.Y - grid.Origin.Y;

var gridX = (ushort)Math.Abs(Math.Floor(offsetX / grid.CellSize.X));
var gridY = (ushort)Math.Abs(Math.Floor(offsetY / grid.CellSize.Y));
gridX = (ushort)Math.Min(gridX, grid.Width - 1);
gridY = (ushort)Math.Min(gridY, grid.Height - 1);
grid.Data[gridX + (gridY * grid.Width)]++;
}
}
}
}
}
}


public void ReadFromFile(BinaryReader reader)
{
Expand Down

0 comments on commit 688066d

Please sign in to comment.