Skip to content

Commit

Permalink
Allow users to save non-square tiles
Browse files Browse the repository at this point in the history
  • Loading branch information
0xC0000054 committed Jan 24, 2021
1 parent ae27eb7 commit cd7ebc6
Showing 1 changed file with 28 additions and 9 deletions.
37 changes: 28 additions & 9 deletions TileImageFileType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,41 @@ protected override bool IsReflexive(PropertyBasedSaveConfigToken token)

private enum PropertyNames
{
TileSize
TileHeight,
TileWidth,
SquareTiles
}

public override PropertyCollection OnCreateSavePropertyCollection()
{
const int DefaultTileSize = 256;
const int MinTileSize = 8;
const int MaxTileSize = 2048;

List<Property> props = new List<Property> {
new Int32Property(PropertyNames.TileSize, 256, 8, 2048)
new Int32Property(PropertyNames.TileHeight, DefaultTileSize, MinTileSize, MaxTileSize),
new Int32Property(PropertyNames.TileWidth, DefaultTileSize, MinTileSize, MaxTileSize),
new BooleanProperty(PropertyNames.SquareTiles, true)
};

List<PropertyCollectionRule> rules = new List<PropertyCollectionRule> {
new LinkValuesBasedOnBooleanRule<int, Int32Property>(
new object[] { PropertyNames.TileHeight, PropertyNames.TileWidth },
PropertyNames.SquareTiles,
false)
};

return new PropertyCollection(props);
return new PropertyCollection(props, rules);
}

public override ControlInfo OnCreateSaveConfigUI(PropertyCollection props)
{
ControlInfo info = CreateDefaultSaveConfigUI(props);

info.SetPropertyControlValue(PropertyNames.TileSize, ControlInfoPropertyNames.DisplayName, "Tile size");
info.SetPropertyControlValue(PropertyNames.TileHeight, ControlInfoPropertyNames.DisplayName, "Tile height");
info.SetPropertyControlValue(PropertyNames.TileWidth, ControlInfoPropertyNames.DisplayName, "Tile width");
info.SetPropertyControlValue(PropertyNames.SquareTiles, ControlInfoPropertyNames.Description, "Square tiles");
info.SetPropertyControlValue(PropertyNames.SquareTiles, ControlInfoPropertyNames.DisplayName, string.Empty);

return info;
}
Expand Down Expand Up @@ -237,20 +255,21 @@ protected override void OnSaveT(Document input, Stream output, PropertyBasedSave
int width = input.Width;
int height = input.Height;

int tileSize = token.GetProperty<Int32Property>(PropertyNames.TileSize).Value;

using (RenderArgs args = new RenderArgs(scratchSurface))
{
input.Render(args, true);
}

int tileHeight = token.GetProperty<Int32Property>(PropertyNames.TileHeight).Value;
int tileWidth = token.GetProperty<Int32Property>(PropertyNames.TileWidth).Value;

List<Rectangle> rects = new List<Rectangle>();

for (int y = 0; y < height; y += tileSize)
for (int y = 0; y < height; y += tileHeight)
{
for (int x = 0; x < width; x += tileSize)
for (int x = 0; x < width; x += tileWidth)
{
Rectangle bounds = new Rectangle(x, y, Math.Min(tileSize, width - x), Math.Min(tileSize, height - y));
Rectangle bounds = new Rectangle(x, y, Math.Min(tileWidth, width - x), Math.Min(tileHeight, height - y));
rects.Add(bounds);
}
}
Expand Down

0 comments on commit cd7ebc6

Please sign in to comment.