Skip to content

Commit

Permalink
Flip (those springs) (#36)
Browse files Browse the repository at this point in the history
* Guard against a couple of potential Null Reference Exceptions
* Basic entity (position) flipping when multi-selecting entities
* Tweak Spring rendering, should fix incorrect direction being displayed when flipped in both X and Y planes.
* Add flipping of an individual entity by changing its Attributes during a flipping operation. Only Spring supported so far.
  • Loading branch information
OtherworldBob authored Aug 31, 2018
1 parent d3bda9f commit a095acb
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 5 deletions.
10 changes: 10 additions & 0 deletions ManiacEditor/Editor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -597,11 +597,15 @@ public void GraphicPanel_OnKeyDown(object sender, KeyEventArgs e)
{
if (IsTilesEdit())
flipVerticalToolStripMenuItem_Click(sender, e);
else if (IsEntitiesEdit())
FlipEntities(FlipDirection.Veritcal);
}
else if (e.KeyData == Keys.M)
{
if (IsTilesEdit())
flipHorizontalToolStripMenuItem_Click(sender, e);
else if (IsEntitiesEdit())
FlipEntities(FlipDirection.Horizontal);
}
if (e.Control)
{
Expand All @@ -622,6 +626,12 @@ public void GraphicPanel_OnKeyDown(object sender, KeyEventArgs e)
}
}

private void FlipEntities(FlipDirection direction)
{
entities.Flip(direction);
entitiesToolbar.UpdateCurrentEntityProperites();
}

public void GraphicPanel_OnKeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.ControlKey)
Expand Down
50 changes: 50 additions & 0 deletions ManiacEditor/EditorEntities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using ManiacEditor.Enums;
using RSDKv5;

namespace ManiacEditor
{
Expand All @@ -28,9 +30,16 @@ public EditorEntities(RSDKv5.Scene scene)
{
foreach (var obj in scene.Objects)
entities.AddRange(obj.Entities.Select(x => GenerateEditorEntity(x)));
FindDuplicateIds();
entitiesBySlot = entities.ToDictionary(x => x.Entity.SlotID);
}

private void FindDuplicateIds()
{
var groupedById = entities.GroupBy(e => e.Entity.SlotID)
.Where(g => g.Count()>1);
}

private ushort getFreeSlot(RSDKv5.SceneEntity preferred)
{
if (preferred != null && !entitiesBySlot.ContainsKey(preferred.SlotID)) return preferred.SlotID;
Expand Down Expand Up @@ -274,5 +283,46 @@ private EditorEntity GenerateEditorEntity(RSDKv5.SceneEntity sceneEntity)
return new EditorEntity(sceneEntity);
}

internal void Flip(FlipDirection direction)
{
var positions = selectedEntities.Select(se => se.Entity.Position);
IEnumerable<Position.Value> monoCoordinatePositions;
if (direction == FlipDirection.Horizontal)
{
monoCoordinatePositions = positions.Select(p => p.X);
}
else
{
monoCoordinatePositions = positions.Select(p => p.Y);
}

short min = monoCoordinatePositions.Min(m => m.High);
short max = monoCoordinatePositions.Max(m => m.High);
int diff = max - min;

foreach (var entity in selectedEntities)
{
if (direction == FlipDirection.Horizontal)
{
short xPos = entity.Entity.Position.X.High;
int fromLeft = xPos - min;
int fromRight = max - xPos;

int newX = fromLeft < fromRight ? max - fromLeft : min + fromRight;
entity.Entity.Position.X.High = (short)newX;
}
else
{
short yPos = entity.Entity.Position.Y.High;
int fromBottom = yPos - min;
int fromTop = max - yPos;

int newY = fromBottom < fromTop ? max - fromBottom : min + fromTop;
entity.Entity.Position.Y.High = (short)newY;
}

entity.Flip(direction);
}
}
}
}
24 changes: 21 additions & 3 deletions ManiacEditor/EditorEntity.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using RSDKv5;
using ManiacEditor.Enums;
using RSDKv5;
using SharpDX.Direct3D9;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -658,11 +659,13 @@ public void DrawOthers(DevicePanel d)
var flipFlag = entity.attributesMap["flipFlag"].ValueVar;
bool fliph = false;
bool flipv = false;

// Handle springs being flipped in both planes
// Down
if (flipFlag == 2)
if ((flipFlag & 0x02) == 0x02)
flipv = true;
// Left
if (flipFlag == 1)
if ((flipFlag & 0x01) == 0x01)
fliph = true;

var editorAnim = LoadAnimation2("Springs", d, animID % 6, -1, fliph, flipv, false);
Expand Down Expand Up @@ -1027,6 +1030,21 @@ public void DrawOthers(DevicePanel d)

}

internal void Flip(FlipDirection flipDirection)
{
if (entity.attributesMap.ContainsKey("flipFlag"))
{
if (flipDirection == FlipDirection.Horizontal)
{
entity.attributesMap["flipFlag"].ValueVar ^= 0x01;
}
else
{
entity.attributesMap["flipFlag"].ValueVar ^= 0x02;
}
}
}

public static void ReleaseResources()
{
DataDirectoryList = null;
Expand Down
31 changes: 31 additions & 0 deletions RSDKv5/AttributeValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,5 +195,36 @@ internal void Write(Writer writer)
break;
}
}

public override string ToString()
{
switch (Type)
{
case AttributeTypes.UINT8:
return value_uint8.ToString();
case AttributeTypes.UINT16:
return value_uint16.ToString();
case AttributeTypes.UINT32:
return value_uint32.ToString();
case AttributeTypes.INT8:
return value_int8.ToString();
case AttributeTypes.INT16:
return value_int16.ToString();
case AttributeTypes.INT32:
return value_int32.ToString();
case AttributeTypes.VAR:
return value_var.ToString();
case AttributeTypes.BOOL:
return value_bool.ToString();
case AttributeTypes.STRING:
return value_string.ToString();
case AttributeTypes.POSITION:
return value_position.ToString();
case AttributeTypes.COLOR:
return value_color.ToString();
default:
return "Unhandled Type for ToString()";
}
}
}
}
4 changes: 2 additions & 2 deletions RSDKv5/GIF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void DisposeTextures()
{
if (null == _texturesCache) return;
foreach (Texture texture in _texturesCache.Values)
texture.Dispose();
texture?.Dispose();
_texturesCache.Clear();
}

Expand All @@ -117,7 +117,7 @@ private void ReleaseResources()
_bitmap.Dispose();
DisposeTextures();
foreach (Bitmap b in _bitmapCache.Values)
b.Dispose();
b?.Dispose();
_bitmapCache.Clear();
}

Expand Down
11 changes: 11 additions & 0 deletions RSDKv5/Position.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,16 @@ internal void Write(Writer writer)
writer.Write(Y.Low);
writer.Write(Y.High);
}

public override string ToString()
{
StringBuilder sb = new StringBuilder("X: ");
sb.Append(X.High);
if (0 != X.Low) sb.Append($"[{X.Low}]");
sb.Append(", Y: ");
sb.Append(Y.High);
if (0 != Y.Low) sb.Append($"[{Y.Low}]");
return sb.ToString();
}
}
}

0 comments on commit a095acb

Please sign in to comment.