Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements icon.Insert() #714

Merged
merged 9 commits into from
Oct 30, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion DMCompiler/DMStandard/Types/Icon.dm
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
return icon_states(src, mode)

proc/Insert(new_icon, icon_state, dir, frame, moving, delay)
set opendream_unimplemented = TRUE

proc/MapColors(...)
set opendream_unimplemented = TRUE
Expand Down
1 change: 1 addition & 0 deletions OpenDreamRuntime/Procs/Native/DreamProcNative.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ public static void SetupNativeProcs(DreamObjectTree objectTree) {
DreamObjectDefinition icon = objectTree.GetObjectDefinition(DreamPath.Icon);
icon.SetNativeProc(DreamProcNativeIcon.NativeProc_Width);
icon.SetNativeProc(DreamProcNativeIcon.NativeProc_Height);
icon.SetNativeProc(DreamProcNativeIcon.NativeProc_Insert);

//DreamObjectDefinition savefile = objectTree.GetObjectDefinitionFromPath(DreamPath.Savefile);
//savefile.SetNativeProc(DreamProcNativeSavefile.NativeProc_Flush);
Expand Down
36 changes: 36 additions & 0 deletions OpenDreamRuntime/Procs/Native/DreamProcNativeIcon.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using OpenDreamRuntime.Objects;
using OpenDreamRuntime.Objects.MetaObjects;
using OpenDreamShared.Dream;

namespace OpenDreamRuntime.Procs.Native {
static class DreamProcNativeIcon {
Expand All @@ -17,6 +18,41 @@ public static DreamValue NativeProc_Height(DreamObject instance, DreamObject usr
return new DreamValue(dreamIconObject.Description.Height);
}

[DreamProc("Insert")]
[DreamProcParameter("new_icon", Type = DreamValue.DreamValueType.DreamObject)]
[DreamProcParameter("icon_state", Type = DreamValue.DreamValueType.String)]
[DreamProcParameter("dir", Type = DreamValue.DreamValueType.Float)]
[DreamProcParameter("frame", Type = DreamValue.DreamValueType.Float)]
[DreamProcParameter("moving", Type = DreamValue.DreamValueType.Float)]
[DreamProcParameter("delay", Type = DreamValue.DreamValueType.Float)]
public static DreamValue NativeProc_Insert(DreamObject instance, DreamObject usr, DreamProcArguments arguments) {
//TODO Figure out what happens when you pass the wrong types as args

arguments.GetArgument(0, "new_icon").TryGetValueAsDreamObject(out var new_icon);
arguments.GetArgument(1, "icon_state").TryGetValueAsString(out var icon_state);
AtomDirection? dir = null;
if (arguments.GetArgument(2, "dir").TryGetValueAsInteger(out var dirNum))
{
dir = (AtomDirection)dirNum;
}
int? frame = null;
if (arguments.GetArgument(3, "frame").TryGetValueAsInteger(out var frameNum))
{
frame = frameNum;
}
bool moving = !(arguments.GetArgument(4, "moving").TryGetValueAsInteger(out var movingNum) && movingNum == 0);
int? delay = null;
if (arguments.GetArgument(5, "delay").TryGetValueAsInteger(out var delayNum))
{
delay = delayNum;
}

DreamMetaObjectIcon.DreamIconObject instanceIconObject = DreamMetaObjectIcon.ObjectToDreamIcon[instance];
DreamMetaObjectIcon.DreamIconObject newIconObject = DreamMetaObjectIcon.ObjectToDreamIcon[new_icon];
instanceIconObject.Description.InsertIcon(newIconObject.Description, icon_state, dir, frame, delay);

instanceIconObject.Moving = moving ? DreamMetaObjectIcon.DreamIconMovingMode.Movement : DreamMetaObjectIcon.DreamIconMovingMode.NonMovement;
return new DreamValue(instanceIconObject);
}
}
}
75 changes: 75 additions & 0 deletions OpenDreamShared/Resources/DMIParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Text;
using OpenDreamShared.Dream;
using System.Globalization;
using JetBrains.Annotations;

namespace OpenDreamShared.Resources {
public static class DMIParser {
Expand Down Expand Up @@ -44,6 +45,80 @@ public static ParsedDMIDescription CreateEmpty(int width, int height) {
};
}

public void InsertIcon(ParsedDMIDescription icon, string icon_state, AtomDirection? dir, int? frame, float? delay)
{
List<ParsedDMIState> states;
if (icon_state is null)
{
states = icon.States.Values.ToList();
}
else
{
states = new List<ParsedDMIState>(1) { icon.States[icon_state] };
}

foreach (var state in states)
{
if (dir is not null)
{
var goodDir = state.Directions[dir.Value];
state.Directions.Clear();
state.Directions = new Dictionary<AtomDirection, ParsedDMIFrame[]>(1) { { dir.Value, goodDir } };
}

if (frame is not null)
{
// TODO Ref says it must start at 1, need to check behavior for when it's less. Manually validate it for now.
var goodFrame = Math.Max(frame.Value, 1);
foreach (var (direction, frames) in state.Directions)
{
state.Directions[direction] = new ParsedDMIFrame[1] { frames[goodFrame] };
if (delay is not null)
{
if (delay > 0)
{
state.Rewind = false;
}
else if(delay < 0)
{
state.Rewind = true;
}
else
{
if (States.Count == 0)
{
// TODO Delay should be 1 tick. Is this a tick?
frames[goodFrame].Delay = 1f;
}
else
{
// Delay of the nearest frame. Hopefully. Christ
var stateDirs = States.Last().Value.Directions;
frames[goodFrame].Delay = dir is null
? stateDirs.First().Value.Last().Delay
: stateDirs[dir.Value].Last().Delay;
}
}
}
}
}
}

// All of that above was just to adjust the inserted icon to match the args. Now we can actually insert it.

foreach (var state in states)
{
if (States.ContainsKey(state.Name))
{
States[state.Name] = state;
}
else
{
States.Add(state.Name, state);
}
}
}

public bool HasState(string stateName = null) {
return States.ContainsKey(stateName ?? "");
}
Expand Down