Skip to content

Commit

Permalink
Merge pull request #872 from OpenKH/origin/layout-editor
Browse files Browse the repository at this point in the history
Add a way to add Sprites in the Layout Editor [SEQUENCE]
  • Loading branch information
Vladabdf authored Sep 11, 2023
2 parents 1d28f99 + 9a1fd15 commit 6c0a73f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
20 changes: 7 additions & 13 deletions OpenKh.Kh2/Sequence.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using OpenKh.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -48,6 +49,8 @@ private class Header
[Data] public Section SpriteGroupDesc { get; set; }
[Data] public Section AnimationDesc { get; set; }
[Data] public Section AnimationGroupDesc { get; set; }

[Data] public int UVEnableFlag { get; set; }
}

public class RawSprite
Expand Down Expand Up @@ -165,6 +168,7 @@ public class AnimationGroup
}

public int Unknown04 { get; set; }
public bool UVEnableFlag { get; set; }
public List<Sprite> Sprites { get; set; }
public List<List<SpritePart>> SpriteGroups { get; set; }
public List<AnimationGroup> AnimationGroups { get; set; }
Expand All @@ -186,6 +190,7 @@ private Sequence(Stream inputStream)
throw new InvalidDataException("Invalid header");

Unknown04 = header.Unknown04;
UVEnableFlag = Convert.ToBoolean(header.UVEnableFlag);
Sprites = stream.ReadList<RawSprite>(header.SpriteDesc.Offset, header.SpriteDesc.Count)
.Select(x => new Sprite
{
Expand Down Expand Up @@ -235,6 +240,7 @@ public void Write(Stream stream)
SpriteGroupDesc = new Section() { Count = SpriteGroups.Count },
AnimationDesc = new Section() { Count = AnimationGroups.Sum(x => x.Animations.Count) },
AnimationGroupDesc = new Section() { Count = AnimationGroups.Count },
UVEnableFlag = Convert.ToInt32(UVEnableFlag)
};

var index = 0;
Expand All @@ -243,19 +249,7 @@ public void Write(Stream stream)
stream.Position = basePosition + MinimumLength;
header.SpriteDesc.Offset = (int)(stream.Position - basePosition);

header.SpritePartDesc.Offset = stream.WriteList(Sprites.Select(x => new RawSprite
{
U0 = x.Left,
V0 = x.Top,
U1 = x.Right,
V1 = x.Bottom,
UScroll = x.UTranslation,
VScroll = x.VTranslation,
ColorLeft = x.ColorLeft,
ColorTop = x.ColorTop,
ColorRight = x.ColorRight,
ColorBottom = x.ColorBottom,
})) + header.SpriteDesc.Offset;
header.SpritePartDesc.Offset = stream.WriteList(Sprites.Select(x => new RawSprite { U0 = x.Left, V0 = x.Top, U1 = x.Right, V1 = x.Bottom, UScroll = x.UTranslation, VScroll = x.VTranslation, ColorLeft = x.ColorLeft, ColorTop = x.ColorTop, ColorRight = x.ColorRight, ColorBottom = x.ColorBottom })) + header.SpriteDesc.Offset;
header.SpriteGroupDesc.Offset = stream.WriteList(SpriteGroups.SelectMany(x => x)) + header.SpritePartDesc.Offset;

index = 0;
Expand Down
5 changes: 5 additions & 0 deletions OpenKh.Tools.LayoutEditor/AppSequenceEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ public void Menu()
{
ForMenuItem("Sprite groups...", () => _isSpriteGroupEditDialogOpen = true);
ForMenuItem("Sprites...", () => _isSpriteEditDialogOpen = true);
ForMenuCheck("Enable UV Animations", () => _sequence.UVEnableFlag, x => _sequence.UVEnableFlag = x);
});
}

Expand All @@ -149,6 +150,10 @@ public bool Run()
ImGui.EndPopup();
}

_sequence.Sprites = _sprites
.Select(x => x.Sprite)
.ToList();

const float SpriteListWidthMul = 1f;
const float SpriteListWidthMax = 192f;
const float RightWidthMul = 1.5f;
Expand Down
6 changes: 6 additions & 0 deletions OpenKh.Tools.LayoutEditor/Dialogs/SpriteEditDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ public void Run()
DrawCropAtlasTexture();
}

if (ImGui.Button("+"))
_spriteModels.Add(new SpriteModel(new Kh2.Sequence.Sprite(), _spriteDrawing, _atlasTexture, _textureBinder, _settings));
ImGui.SameLine();
if (ImGui.Button("-"))
_spriteModels.RemoveAt(_selectedSpriteModel);

ForChild("AtlasTexture", _atlasTexture.Width, _atlasTexture.Height, false,
() =>
{
Expand Down
2 changes: 1 addition & 1 deletion OpenKh.Tools.LayoutEditor/Dialogs/SpriteGroupEditDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void Run()
var previewWidth = Math.Min(windowSize / TotalWidthMul * PreviewWidthMul, PreviewWidthMax);
var editorWidth = Math.Min(windowSize / TotalWidthMul * EditorWidthMul, EditorWidthMax);

ForChild(nameof(SpriteGroupPreview), previewWidth, 512, true, SpriteGroupPreview);
ForChild(nameof(SpriteGroupPreview), previewWidth + 64, 512, true, SpriteGroupPreview);
ImGui.SameLine();
ForChild(nameof(SptiteGroupEditor), editorWidth, 512, false, SptiteGroupEditor);

Expand Down

0 comments on commit 6c0a73f

Please sign in to comment.