1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-26 06:28:35 +01:00

added from instruction to DataTextureAtlas

This commit is contained in:
Ell 2022-09-14 11:59:28 +02:00
parent ff92a00e1a
commit 7d8b14ee8d
4 changed files with 27 additions and 6 deletions

View file

@ -46,7 +46,7 @@ Fixes
### MLEM.Data ### MLEM.Data
Additions Additions
- Added data and copy instructions to DataTextureAtlas - Added data, from, and copy instructions to DataTextureAtlas
Improvements Improvements
- Allow data texture atlas pivots and offsets to be negative - Allow data texture atlas pivots and offsets to be negative

View file

@ -27,6 +27,7 @@ namespace MLEM.Data {
/// <item><description>The (optional) <c>off</c> (of <c>offset</c>) instruction defines an offset that is added onto the location and pivot of this texture region. This is useful when duplicating a previously defined texture region to create a second region that has a constant offset. It requires two arguments: The x and y offset.</description></item> /// <item><description>The (optional) <c>off</c> (of <c>offset</c>) instruction defines an offset that is added onto the location and pivot of this texture region. This is useful when duplicating a previously defined texture region to create a second region that has a constant offset. It requires two arguments: The x and y offset.</description></item>
/// <item><description>The (optional and repeatable) <c>cpy</c> (or <c>copy</c>) instruction defines an additional texture region that should also be generated from the same data, but with a given offset that will be applied to the location and pivot. It requires three arguments: the copy region's name and the x and y offsets.</description></item> /// <item><description>The (optional and repeatable) <c>cpy</c> (or <c>copy</c>) instruction defines an additional texture region that should also be generated from the same data, but with a given offset that will be applied to the location and pivot. It requires three arguments: the copy region's name and the x and y offsets.</description></item>
/// <item><description>The (optional and repeatable) <c>dat</c> (or <c>data</c>) instruction defines a custom data point that can be added to the resulting <see cref="TextureRegion"/>'s <see cref="GenericDataHolder"/> data. It requires two arguments: the data point's name and the data point's value, the latter of which is also stored as a string value.</description></item> /// <item><description>The (optional and repeatable) <c>dat</c> (or <c>data</c>) instruction defines a custom data point that can be added to the resulting <see cref="TextureRegion"/>'s <see cref="GenericDataHolder"/> data. It requires two arguments: the data point's name and the data point's value, the latter of which is also stored as a string value.</description></item>
/// <item><description>The (optional) <c>frm</c> (or <c>from</c>) instruction defines a texture region (defined before the current region) whose data should be copied. All data from the region will be copied, but adding additional instructions afterwards modifies the data. It requires one argument: the name of the region whose data to copy. If this instruction is used, the <c>loc</c> instruction is not required.</description></item>
/// </list> /// </list>
/// </para> /// </para>
/// <example> /// <example>
@ -141,6 +142,19 @@ namespace MLEM.Data {
customData.Add(words[i + 1], words[i + 2]); customData.Add(words[i + 1], words[i + 2]);
i += 2; i += 2;
break; break;
case "frm":
case "from":
var fromRegion = atlas[words[i + 1]];
customData.Clear();
foreach (var key in fromRegion.GetDataKeys())
customData.Add(key, fromRegion.GetData<string>(key));
location = fromRegion.Area;
pivot = fromRegion.PivotPixels;
if (pivot != Vector2.Zero && !pivotRelative)
pivot += location.Location.ToVector2();
offset = Vector2.Zero;
i += 1;
break;
default: default:
// if we have data for the previous regions, they're valid so we add them // if we have data for the previous regions, they're valid so we add them
AddCurrentRegions(); AddCurrentRegions();
@ -163,7 +177,7 @@ namespace MLEM.Data {
return atlas; return atlas;
void AddCurrentRegions() { void AddCurrentRegions() {
// the location is the only mandatory instruction, which is why we check it here // the location is the only mandatory information, which is why we check it here
if (location == Rectangle.Empty || namesOffsets.Count <= 0) if (location == Rectangle.Empty || namesOffsets.Count <= 0)
return; return;
foreach (var (name, addedOff) in namesOffsets) { foreach (var (name, addedOff) in namesOffsets) {

View file

@ -20,11 +20,13 @@ dat DataPoint2 3.5
dat DataPoint3 --- dat DataPoint3 ---
LongTableUp LongTableUp
piv 16 48 piv 16 48 loc 0 32 64 48
loc 0 32 64 48
copy Copy1 16 0 copy Copy1 16 0
cpy Copy2 32 4 cpy Copy2 32 4
Copy3 from
LongTableUp off 2 4
LongTableRight LongTableDown LongTableLeft LongTableRight LongTableDown LongTableLeft
location 32 30 64 48 location 32 30 64 48
piv 80 46 piv 80 46

View file

@ -13,7 +13,7 @@ namespace Tests {
using var game = TestGame.Create(); using var game = TestGame.Create();
using var texture = new Texture2D(game.GraphicsDevice, 1, 1); using var texture = new Texture2D(game.GraphicsDevice, 1, 1);
var atlas = DataTextureAtlas.LoadAtlasData(new TextureRegion(texture), game.RawContent, "Texture.atlas"); var atlas = DataTextureAtlas.LoadAtlasData(new TextureRegion(texture), game.RawContent, "Texture.atlas");
Assert.AreEqual(11, atlas.Regions.Count()); Assert.AreEqual(12, atlas.Regions.Count());
// no pivot // no pivot
var plant = atlas["Plant"]; var plant = atlas["Plant"];
@ -35,7 +35,7 @@ namespace Tests {
Assert.AreEqual(negativePivot.Area, new Rectangle(0, 32, 16, 16)); Assert.AreEqual(negativePivot.Area, new Rectangle(0, 32, 16, 16));
Assert.AreEqual(negativePivot.PivotPixels, new Vector2(-32, 46 - 32)); Assert.AreEqual(negativePivot.PivotPixels, new Vector2(-32, 46 - 32));
// copies (pivot pixels should be identical to LongTableUp because they're region-internal) // cpy (pivot pixels should be identical to LongTableUp because they're region-internal)
var copy1 = atlas["Copy1"]; var copy1 = atlas["Copy1"];
Assert.AreEqual(copy1.Area, new Rectangle(0 + 16, 32, 64, 48)); Assert.AreEqual(copy1.Area, new Rectangle(0 + 16, 32, 64, 48));
Assert.AreEqual(copy1.PivotPixels, new Vector2(16, 48 - 32)); Assert.AreEqual(copy1.PivotPixels, new Vector2(16, 48 - 32));
@ -43,6 +43,11 @@ namespace Tests {
Assert.AreEqual(copy2.Area, new Rectangle(0 + 32, 32 + 4, 64, 48)); Assert.AreEqual(copy2.Area, new Rectangle(0 + 32, 32 + 4, 64, 48));
Assert.AreEqual(copy2.PivotPixels, new Vector2(16, 48 - 32)); Assert.AreEqual(copy2.PivotPixels, new Vector2(16, 48 - 32));
// frm
var copy3 = atlas["Copy3"];
Assert.AreEqual(copy3.Area, new Rectangle(0 + 2, 32 + 4, 64, 48));
Assert.AreEqual(copy3.PivotPixels, new Vector2(16, 48 - 32));
// data // data
var data = atlas["DataTest"]; var data = atlas["DataTest"];
Assert.AreEqual("ThisIsSomeData", data.GetData<string>("DataPoint1")); Assert.AreEqual("ThisIsSomeData", data.GetData<string>("DataPoint1"));