Skip to content

Commit

Permalink
Added VStancerNode for VStancerPreset
Browse files Browse the repository at this point in the history
  • Loading branch information
carmineos committed Feb 23, 2020
1 parent 53b6a94 commit 52f4f2f
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 101 deletions.
32 changes: 14 additions & 18 deletions VStancer.Client/VStancerEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,22 +152,22 @@ private void OnMenuPresetValueChanged(string id, string newValue)
if (id == FrontRotationID)
{
currentPreset.SetRotationFront(value);
defaultValue = currentPreset.DefaultRotationY[0];
defaultValue = currentPreset.DefaultNodes[0].RotationY;
}
else if (id == RearRotationID)
{
currentPreset.SetRotationRear(value);
defaultValue = currentPreset.DefaultRotationY[currentPreset.FrontWheelsCount];
defaultValue = currentPreset.DefaultNodes[currentPreset.FrontWheelsCount].RotationY;
}
else if (id == FrontOffsetID)
{
currentPreset.SetOffsetFront(-value);
defaultValue = currentPreset.DefaultOffsetX[0];
defaultValue = currentPreset.DefaultNodes[0].PositionX;
}
else if (id == RearOffsetID)
{
currentPreset.SetOffsetRear(-value);
defaultValue = currentPreset.DefaultOffsetX[currentPreset.FrontWheelsCount];
defaultValue = currentPreset.DefaultNodes[currentPreset.FrontWheelsCount].PositionX;
}

// Force one single refresh to update rendering at correct position after reset
Expand Down Expand Up @@ -591,21 +591,17 @@ private void UpdateFloatDecorator(int vehicle, string name, float currentValue,
/// <param name="preset">The preset for this vehicle</param>
private void UpdateVehicleDecorators(int vehicle, VStancerPreset preset)
{
float[] DefaultOffsetX = preset.DefaultOffsetX;
float[] DefaultRotationY = preset.DefaultRotationY;
float[] OffsetX = preset.OffsetX;
float[] RotationY = preset.RotationY;
int frontCount = preset.FrontWheelsCount;

UpdateFloatDecorator(vehicle, DefaultFrontOffsetID, DefaultOffsetX[0], OffsetX[0]);
UpdateFloatDecorator(vehicle, DefaultFrontRotationID, DefaultRotationY[0], RotationY[0]);
UpdateFloatDecorator(vehicle, DefaultRearOffsetID, DefaultOffsetX[frontCount], OffsetX[frontCount]);
UpdateFloatDecorator(vehicle, DefaultRearRotationID, DefaultRotationY[frontCount], RotationY[frontCount]);
UpdateFloatDecorator(vehicle, DefaultFrontOffsetID, preset.DefaultNodes[0].PositionX, preset.Nodes[0].PositionX);
UpdateFloatDecorator(vehicle, DefaultFrontRotationID, preset.DefaultNodes[0].RotationY, preset.Nodes[0].RotationY);
UpdateFloatDecorator(vehicle, DefaultRearOffsetID, preset.DefaultNodes[frontCount].PositionX, preset.Nodes[frontCount].PositionX);
UpdateFloatDecorator(vehicle, DefaultRearRotationID, preset.DefaultNodes[frontCount].RotationY, preset.Nodes[frontCount].RotationY);

UpdateFloatDecorator(vehicle, FrontOffsetID, OffsetX[0], DefaultOffsetX[0]);
UpdateFloatDecorator(vehicle, FrontRotationID, RotationY[0], DefaultRotationY[0]);
UpdateFloatDecorator(vehicle, RearOffsetID, OffsetX[frontCount], DefaultOffsetX[frontCount]);
UpdateFloatDecorator(vehicle, RearRotationID, RotationY[frontCount], DefaultRotationY[frontCount]);
UpdateFloatDecorator(vehicle, FrontOffsetID, preset.Nodes[0].PositionX, preset.DefaultNodes[0].PositionX);
UpdateFloatDecorator(vehicle, FrontRotationID, preset.Nodes[0].RotationY, preset.DefaultNodes[0].RotationY);
UpdateFloatDecorator(vehicle, RearOffsetID, preset.Nodes[frontCount].PositionX, preset.DefaultNodes[frontCount].PositionX);
UpdateFloatDecorator(vehicle, RearRotationID, preset.Nodes[frontCount].RotationY, preset.DefaultNodes[frontCount].RotationY);
}

/// <summary>
Expand Down Expand Up @@ -646,8 +642,8 @@ private void RefreshVehicleUsingPreset(int vehicle, VStancerPreset preset)
int wheelsCount = preset.WheelsCount;
for (int index = 0; index < wheelsCount; index++)
{
SetVehicleWheelXOffset(vehicle, index, preset.OffsetX[index]);
SetVehicleWheelYRotation(vehicle, index, preset.RotationY[index]);
SetVehicleWheelXOffset(vehicle, index, preset.Nodes[index].PositionX);
SetVehicleWheelYRotation(vehicle, index, preset.Nodes[index].RotationY);
}
}

Expand Down
8 changes: 4 additions & 4 deletions VStancer.Client/VStancerMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,10 @@ private void UpdateEditorMenu()
if (!CurrentPresetIsValid)
return;

AddDynamicFloatList(editorMenu, "Front Track Width", -currentPreset.DefaultOffsetX[0], -currentPreset.OffsetX[0], frontMaxOffset, FrontOffsetID);
AddDynamicFloatList(editorMenu, "Rear Track Width", -currentPreset.DefaultOffsetX[currentPreset.FrontWheelsCount], -currentPreset.OffsetX[currentPreset.FrontWheelsCount], rearMaxOffset, RearOffsetID);
AddDynamicFloatList(editorMenu, "Front Camber", currentPreset.DefaultRotationY[0], currentPreset.RotationY[0], frontMaxCamber, FrontRotationID);
AddDynamicFloatList(editorMenu, "Rear Camber", currentPreset.DefaultRotationY[currentPreset.FrontWheelsCount], currentPreset.RotationY[currentPreset.FrontWheelsCount], rearMaxCamber, RearRotationID);
AddDynamicFloatList(editorMenu, "Front Track Width", -currentPreset.DefaultNodes[0].PositionX, -currentPreset.Nodes[0].PositionX, frontMaxOffset, FrontOffsetID);
AddDynamicFloatList(editorMenu, "Rear Track Width", -currentPreset.DefaultNodes[currentPreset.FrontWheelsCount].PositionX, -currentPreset.Nodes[currentPreset.FrontWheelsCount].PositionX, rearMaxOffset, RearOffsetID);
AddDynamicFloatList(editorMenu, "Front Camber", currentPreset.DefaultNodes[0].RotationY, currentPreset.Nodes[0].RotationY, frontMaxCamber, FrontRotationID);
AddDynamicFloatList(editorMenu, "Rear Camber", currentPreset.DefaultNodes[currentPreset.FrontWheelsCount].RotationY, currentPreset.Nodes[currentPreset.FrontWheelsCount].RotationY, rearMaxCamber, RearRotationID);
editorMenu.AddMenuItem(new MenuItem("Reset", "Restores the default values") { ItemData = ResetID });
}

Expand Down
134 changes: 55 additions & 79 deletions VStancer.Client/VStancerPreset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,32 @@ public class VStancerPreset : IEquatable<VStancerPreset>
public int WheelsCount { get; private set; }
public int FrontWheelsCount { get; private set; }

public float[] DefaultRotationY { get; private set; }
public float[] DefaultOffsetX { get; private set; }
public float[] RotationY { get; set; }
public float[] OffsetX { get; set; }

public VStancerNode[] Nodes { get; set; }
public VStancerNode[] DefaultNodes { get; private set; }

public void SetOffsetFront(float value)
{
for (int index = 0; index < FrontWheelsCount; index++)
OffsetX[index] = (index % 2 == 0) ? value : -value;
Nodes[index].PositionX = (index % 2 == 0) ? value : -value;
}

public void SetOffsetRear(float value)
{
for (int index = FrontWheelsCount; index < WheelsCount; index++)
OffsetX[index] = (index % 2 == 0) ? value : -value;
Nodes[index].PositionX = (index % 2 == 0) ? value : -value;
}

public void SetRotationFront(float value)
{
for (int index = 0; index < FrontWheelsCount; index++)
RotationY[index] = (index % 2 == 0) ? value : -value;
Nodes[index].RotationY = (index % 2 == 0) ? value : -value;
}

public void SetRotationRear(float value)
{
for (int index = FrontWheelsCount; index < WheelsCount; index++)
RotationY[index] = (index % 2 == 0) ? value : -value;
Nodes[index].RotationY = (index % 2 == 0) ? value : -value;
}

public bool IsEdited
Expand All @@ -45,89 +44,56 @@ public bool IsEdited
{
for (int index = 0; index < WheelsCount; index++)
{
if (!MathUtil.WithinEpsilon(DefaultOffsetX[index], OffsetX[index], Epsilon) ||
!MathUtil.WithinEpsilon(DefaultRotationY[index], RotationY[index], Epsilon))
if (!MathUtil.WithinEpsilon(DefaultNodes[index].PositionX, Nodes[index].PositionX, Epsilon) ||
!MathUtil.WithinEpsilon(DefaultNodes[index].RotationY, Nodes[index].RotationY, Epsilon))
return true;
}
return false;
}
}

public VStancerPreset()
{
WheelsCount = 4;
FrontWheelsCount = 2;

DefaultRotationY = new float[] { 0, 0, 0, 0 };
DefaultOffsetX = new float[] { 0, 0, 0, 0 };
RotationY = new float[] { 0, 0, 0, 0 };
OffsetX = new float[] { 0, 0, 0, 0 };
}

public VStancerPreset(int count, float[] defRot, float[] defOff)
{
WheelsCount = count;
FrontWheelsCount = CalculateFrontWheelsCount(WheelsCount);

DefaultRotationY = new float[WheelsCount];
DefaultOffsetX = new float[WheelsCount];
RotationY = new float[WheelsCount];
OffsetX = new float[WheelsCount];

for (int index = 0; index < WheelsCount; index++)
{
DefaultRotationY[index] = defRot[index];
DefaultOffsetX[index] = defOff[index];

RotationY[index] = DefaultRotationY[index];
OffsetX[index] = DefaultOffsetX[index];
}
}

public VStancerPreset(int count, float frontOffset, float frontRotation, float rearOffset, float rearRotation, float defaultFrontOffset, float defaultFrontRotation, float defaultRearOffset, float defaultRearRotation)
{
WheelsCount = count;

DefaultRotationY = new float[WheelsCount];
DefaultOffsetX = new float[WheelsCount];
RotationY = new float[WheelsCount];
OffsetX = new float[WheelsCount];
DefaultNodes = new VStancerNode[WheelsCount];
Nodes = new VStancerNode[WheelsCount];

FrontWheelsCount = CalculateFrontWheelsCount(WheelsCount);

for (int index = 0; index < FrontWheelsCount; index++)
{
if (index % 2 == 0)
{
DefaultRotationY[index] = defaultFrontRotation;
DefaultOffsetX[index] = defaultFrontOffset;
RotationY[index] = frontRotation;
OffsetX[index] = frontOffset;
DefaultNodes[index].RotationY = defaultFrontRotation;
DefaultNodes[index].PositionX = defaultFrontOffset;
Nodes[index].RotationY = frontRotation;
Nodes[index].PositionX = frontOffset;
}
else
{
DefaultRotationY[index] = -defaultFrontRotation;
DefaultOffsetX[index] = -defaultFrontOffset;
RotationY[index] = -frontRotation;
OffsetX[index] = -frontOffset;
DefaultNodes[index].RotationY = -defaultFrontRotation;
DefaultNodes[index].PositionX = -defaultFrontOffset;
Nodes[index].RotationY = -frontRotation;
Nodes[index].PositionX = - frontOffset;
}
}

for (int index = FrontWheelsCount; index < WheelsCount; index++)
{
if (index % 2 == 0)
{
DefaultRotationY[index] = defaultRearRotation;
DefaultOffsetX[index] = defaultRearOffset;
RotationY[index] = rearRotation;
OffsetX[index] = rearOffset;
DefaultNodes[index].RotationY = defaultRearRotation;
DefaultNodes[index].PositionX = defaultRearOffset;
Nodes[index].RotationY = rearRotation;
Nodes[index].PositionX = rearOffset;
}
else
{
DefaultRotationY[index] = -defaultRearRotation;
DefaultOffsetX[index] = -defaultRearOffset;
RotationY[index] = -rearRotation;
OffsetX[index] = -rearOffset;
DefaultNodes[index].RotationY = -defaultRearRotation;
DefaultNodes[index].PositionX = -defaultRearOffset;
Nodes[index].RotationY = -rearRotation;
Nodes[index].PositionX = -rearOffset;
}
}
}
Expand All @@ -136,8 +102,7 @@ public void Reset()
{
for (int index = 0; index < WheelsCount; index++)
{
RotationY[index] = DefaultRotationY[index];
OffsetX[index] = DefaultOffsetX[index];
Nodes[index] = DefaultNodes[index];
}
}

Expand All @@ -148,10 +113,10 @@ public bool Equals(VStancerPreset other)

for (int index = 0; index < WheelsCount; index++)
{
if (!MathUtil.WithinEpsilon(DefaultOffsetX[index], other.DefaultOffsetX[index], Epsilon) ||
!MathUtil.WithinEpsilon(DefaultRotationY[index], other.DefaultRotationY[index], Epsilon) ||
!MathUtil.WithinEpsilon(OffsetX[index], other.OffsetX[index], Epsilon) ||
!MathUtil.WithinEpsilon(RotationY[index], other.RotationY[index], Epsilon))
if (!MathUtil.WithinEpsilon(DefaultNodes[index].PositionX, other.DefaultNodes[index].PositionX, Epsilon) ||
!MathUtil.WithinEpsilon(DefaultNodes[index].RotationY, other.DefaultNodes[index].RotationY, Epsilon) ||
!MathUtil.WithinEpsilon(Nodes[index].PositionX, other.Nodes[index].PositionX, Epsilon) ||
!MathUtil.WithinEpsilon(Nodes[index].RotationY, other.Nodes[index].RotationY, Epsilon))
return false;
}
return true;
Expand All @@ -169,10 +134,10 @@ public override string ToString()

for (int i = 0; i < WheelsCount; i++)
{
defOff.Append(string.Format("{0,15}", DefaultOffsetX[i]));
defRot.Append(string.Format("{0,15}", DefaultRotationY[i]));
curOff.Append(string.Format("{0,15}", OffsetX[i]));
curRot.Append(string.Format("{0,15}", RotationY[i]));
defOff.Append(string.Format("{0,15}", DefaultNodes[i].PositionX));
defRot.Append(string.Format("{0,15}", DefaultNodes[i].RotationY));
curOff.Append(string.Format("{0,15}", Nodes[i].PositionX));
curRot.Append(string.Format("{0,15}", Nodes[i].RotationY));
}

s.AppendLine(curOff.ToString());
Expand Down Expand Up @@ -207,15 +172,26 @@ public static int CalculateFrontWheelsCount(int wheelsCount)
public float[] ToArray()
{
return new float[] {
OffsetX[0],
RotationY[0],
OffsetX[FrontWheelsCount],
RotationY[FrontWheelsCount],
DefaultOffsetX[0],
DefaultRotationY[0],
DefaultOffsetX[FrontWheelsCount],
DefaultRotationY[FrontWheelsCount],
Nodes[0].PositionX,
Nodes[0].RotationY,
Nodes[FrontWheelsCount].PositionX,
Nodes[FrontWheelsCount].RotationY,
DefaultNodes[0].PositionX,
DefaultNodes[0].RotationY,
DefaultNodes[FrontWheelsCount].PositionX,
DefaultNodes[FrontWheelsCount].RotationY,
};
}
}


// TODO: Edit Preset to use nodes structs
public struct VStancerNode
{
//public Vector3 Position { get; set; }
//public Vector3 Rotation { get; set; }
public float PositionX { get; set; }
public float RotationY { get; set; }
//public Vector3 Scale { get; set; }
}
}

0 comments on commit 52f4f2f

Please sign in to comment.