Skip to content
Open
Show file tree
Hide file tree
Changes from all 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 @@ -16,7 +16,6 @@
set opendream_unimplemented = TRUE

proc/GetPixel(x, y, icon_state, dir = 0, frame = 0, moving = -1)
set opendream_unimplemented = TRUE

proc/Height()

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 @@ -176,6 +176,7 @@ public static void SetupNativeProcs(DreamObjectTree objectTree) {
objectTree.SetNativeProc(objectTree.Icon, DreamProcNativeIcon.NativeProc_Blend);
objectTree.SetNativeProc(objectTree.Icon, DreamProcNativeIcon.NativeProc_Scale);
objectTree.SetNativeProc(objectTree.Icon, DreamProcNativeIcon.NativeProc_Turn);
objectTree.SetNativeProc(objectTree.Icon, DreamProcNativeIcon.NativeProc_GetPixel);

objectTree.SetNativeProc(objectTree.Savefile, DreamProcNativeSavefile.NativeProc_ExportText);
objectTree.SetNativeProc(objectTree.Savefile, DreamProcNativeSavefile.NativeProc_Flush);
Expand Down
71 changes: 71 additions & 0 deletions OpenDreamRuntime/Procs/Native/DreamProcNativeIcon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,76 @@ public static DreamValue NativeProc_Turn(NativeProc.Bundle bundle, DreamObject?
public static void _NativeProc_TurnInternal(DreamObjectIcon src, float angle) {
src.Turn(angle);
}

[DreamProc("GetPixel")]
[DreamProcParameter("x", Type = DreamValueTypeFlag.Float)]
[DreamProcParameter("y", Type = DreamValueTypeFlag.Float)]
[DreamProcParameter("icon_state", Type = DreamValueTypeFlag.String)]
[DreamProcParameter("dir", Type = DreamValueTypeFlag.Float, DefaultValue = 0)]
[DreamProcParameter("frame", Type = DreamValueTypeFlag.Float, DefaultValue = 0)]
[DreamProcParameter("moving", Type = DreamValueTypeFlag.Float, DefaultValue = -1)]
public static DreamValue NativeProc_GetPixel(NativeProc.Bundle bundle, DreamObject? src, DreamObject? usr) {
bundle.GetArgument(0, "x").TryGetValueAsInteger(out var xPos);
bundle.GetArgument(1, "y").TryGetValueAsInteger(out var yPos);
bundle.GetArgument(2, "icon_state").TryGetValueAsString(out var iconState);
bundle.GetArgument(3, "dir").TryGetValueAsInteger(out var dir);
bundle.GetArgument(4, "frame").TryGetValueAsInteger(out var frame);
//TODO: Implement moving var

DreamIcon iconObj = ((DreamObjectIcon)src!).Icon;
//TODO BYONDISM: A non-existent icon_state will default to the empty string icon_state,
//or if one doesn't exist it will default to the first icon_state in the DMI.
if(!iconObj.States.TryGetValue(iconState ?? string.Empty, out var state))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using DreamIcon.States is going to get you the textures before any sort of modifications like /icon.Blend() are applied. You need to use the result of iconObj.GenerateDMI() to get the final DMI.

//Bad icon_state returns null
return DreamValue.Null;

//Position values less than 1 are out of bounds. Early escape.
if (xPos < 1 || yPos < 1) return DreamValue.Null;

if (frame < 1) {
frame = 0; //BYONDISM: Frames less than 1 count as 0,
} else if (frame > state.Frames) {
return DreamValue.Null;
} else {
frame -= 1; // Convert from 1-index to 0-index
}

AtomDirection atomDir = dir switch {
0 or 2 => AtomDirection.South,
1 => AtomDirection.North,
4 => AtomDirection.East,
5 => AtomDirection.Northeast,
6 => AtomDirection.Southeast,
8 => AtomDirection.West,
9 => AtomDirection.Northwest,
10 => AtomDirection.Southwest,
_ => AtomDirection.None
};
//BYONDISM: Bad dir values just crash instantly :)
if (atomDir == AtomDirection.None) return DreamValue.Null;
Comment on lines +144 to +156
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can cast the value instead of having a switch

Suggested change
AtomDirection atomDir = dir switch {
0 or 2 => AtomDirection.South,
1 => AtomDirection.North,
4 => AtomDirection.East,
5 => AtomDirection.Northeast,
6 => AtomDirection.Southeast,
8 => AtomDirection.West,
9 => AtomDirection.Northwest,
10 => AtomDirection.Southwest,
_ => AtomDirection.None
};
//BYONDISM: Bad dir values just crash instantly :)
if (atomDir == AtomDirection.None) return DreamValue.Null;
//BYONDISM: Bad dir values just crash instantly :)
if (!Enum.IsDefined(typeof(AtomDirection), dir)) return DreamValue.Null;
var atomDir = (AtomDirection)dir;


if (!state.Directions.TryGetValue(atomDir, out var frameList))
//Empty dir's return null
return DreamValue.Null;

var finalFrame = frameList[frame].Image;
if (finalFrame is null) return DreamValue.Null;

//Out-of-bounds xy values return null.
if (xPos > finalFrame.Width || yPos > finalFrame.Height) return DreamValue.Null;
//SixLabors.Image<Rgba32> is 0-indexed from the top-left, BYOND is 1-indexed from the bottom left.
xPos -= 1;
yPos = finalFrame.Height - yPos;

var pix = finalFrame[xPos, yPos];
//If A is fully transparent return null
//if A is partially transparent return "#RRGGBBAA"
//if A is not transparent return "#RRGGBB"
return pix.A switch {
0 => DreamValue.Null,
255 => new DreamValue($"#{pix.R:x2}{pix.G:x2}{pix.B:x2}"),
_ => new DreamValue($"#{pix.R:x2}{pix.G:x2}{pix.B:x2}{pix.A:x2}")
};
}
}
}
Loading