Add checkbox to toggle auto-extension behavior, clamp map bounds

This commit is contained in:
Kurt 2020-05-02 14:41:32 -07:00
parent 1c8a56746a
commit ef065f889b
3 changed files with 62 additions and 29 deletions

View File

@ -124,16 +124,7 @@ public int RemoveAll(int xmin, int ymin, int width, int height, Func<FieldItem,
public void DeleteExtensionTiles(FieldItem tile, in int x, in int y)
{
var type = ItemInfo.GetItemSize(tile);
var w = type.GetWidth();
var h = type.GetHeight();
if ((tile.Rotation & 1) == 1)
{
var tmp = w;
w = h;
h = tmp;
}
GetTileWidthHeight(tile, x, y, out var w, out var h);
for (int ix = 0; ix < w; ix++)
{
@ -149,16 +140,7 @@ public void DeleteExtensionTiles(FieldItem tile, in int x, in int y)
public void SetExtensionTiles(FieldItem tile, in int x, in int y)
{
var type = ItemInfo.GetItemSize(tile);
var w = type.GetWidth();
var h = type.GetHeight();
if ((tile.Rotation & 1) == 1)
{
var tmp = w;
w = h;
h = tmp;
}
GetTileWidthHeight(tile, x, y, out var w, out var h);
for (byte ix = 0; ix < w; ix++)
{
@ -172,16 +154,42 @@ public void SetExtensionTiles(FieldItem tile, in int x, in int y)
}
}
private void GetTileWidthHeight(FieldItem tile, int x, int y, out int w, out int h)
{
var type = ItemInfo.GetItemSize(tile);
w = type.GetWidth();
h = type.GetHeight();
// Rotation
if ((tile.Rotation & 1) == 1)
{
var tmp = w;
w = h;
h = tmp;
}
// Clamp to grid bounds
if (x + w - 1 >= MapWidth)
w = MapWidth - x;
if (y + h - 1 >= MapHeight)
h = MapHeight - y;
}
/// <summary>
/// Checks if writing the <see cref="tile"/> at the specified <see cref="x"/> and <see cref="y"/> coordinates will overlap with any existing tiles.
/// </summary>
/// <returns>True if any tile will be overwritten, false if nothing is there.</returns>
public bool IsOccupied(FieldItem tile, in int x, in int y)
public FieldItemPermission IsOccupied(FieldItem tile, in int x, in int y)
{
var type = ItemInfo.GetItemSize(tile);
var w = type.GetWidth();
var h = type.GetHeight();
if (x + w - 1 >= MapWidth)
return FieldItemPermission.OutOfBounds;
if (y + h - 1 >= MapHeight)
return FieldItemPermission.OutOfBounds;
if ((tile.Rotation & 1) == 1)
{
var tmp = w;
@ -195,11 +203,18 @@ public bool IsOccupied(FieldItem tile, in int x, in int y)
{
var t = GetTile(x + ix, y + iy);
if (!t.IsNone)
return true;
return FieldItemPermission.Collision;
}
}
return false;
return FieldItemPermission.NoCollision;
}
}
public enum FieldItemPermission
{
NoCollision,
Collision,
OutOfBounds,
}
}

View File

@ -67,6 +67,7 @@ private void InitializeComponent()
this.B_RemovePlacedItems = new System.Windows.Forms.Button();
this.TR_Transparency = new System.Windows.Forms.TrackBar();
this.CHK_NoOverwrite = new System.Windows.Forms.CheckBox();
this.CHK_AutoExtension = new System.Windows.Forms.CheckBox();
this.CM_Click.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.PB_Map)).BeginInit();
this.CM_Picture.SuspendLayout();
@ -446,11 +447,24 @@ private void InitializeComponent()
this.CHK_NoOverwrite.Text = "Prevent Writing Occupied Tiles";
this.CHK_NoOverwrite.UseVisualStyleBackColor = true;
//
// CHK_AutoExtension
//
this.CHK_AutoExtension.AutoSize = true;
this.CHK_AutoExtension.Checked = true;
this.CHK_AutoExtension.CheckState = System.Windows.Forms.CheckState.Checked;
this.CHK_AutoExtension.Location = new System.Drawing.Point(532, 398);
this.CHK_AutoExtension.Name = "CHK_AutoExtension";
this.CHK_AutoExtension.Size = new System.Drawing.Size(197, 17);
this.CHK_AutoExtension.TabIndex = 38;
this.CHK_AutoExtension.Text = "Automatically Set/Delete Extensions";
this.CHK_AutoExtension.UseVisualStyleBackColor = true;
//
// FieldItemEditor
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(981, 537);
this.Controls.Add(this.CHK_AutoExtension);
this.Controls.Add(this.CHK_NoOverwrite);
this.Controls.Add(this.TR_Transparency);
this.Controls.Add(this.GB_Remove);
@ -530,5 +544,6 @@ private void InitializeComponent()
private System.Windows.Forms.Button B_RemovePlacedItems;
private System.Windows.Forms.TrackBar TR_Transparency;
private System.Windows.Forms.CheckBox CHK_NoOverwrite;
private System.Windows.Forms.CheckBox CHK_AutoExtension;
}
}

View File

@ -157,18 +157,21 @@ private void ViewTile(FieldItem tile)
private void SetTile(FieldItem tile, int x, int y)
{
var pgt = (FieldItem)PG_Tile.SelectedObject;
if (CHK_NoOverwrite.Checked && Layer.IsOccupied(pgt, x, y))
var permission = Layer.IsOccupied(pgt, x, y);
switch (permission)
{
System.Media.SystemSounds.Asterisk.Play();
return;
case FieldItemPermission.OutOfBounds:
case FieldItemPermission.Collision when CHK_NoOverwrite.Checked:
System.Media.SystemSounds.Asterisk.Play();
return;
}
// Clean up original placed data
if (tile.IsRoot)
if (tile.IsRoot && CHK_AutoExtension.Checked)
Layer.DeleteExtensionTiles(tile, x, y);
// Set new placed data
if (pgt.IsRoot)
if (pgt.IsRoot && CHK_AutoExtension.Checked)
Layer.SetExtensionTiles(pgt, x, y);
tile.CopyFrom(pgt);
@ -178,7 +181,7 @@ private void SetTile(FieldItem tile, int x, int y)
private void DeleteTile(FieldItem tile, int x, int y)
{
if (tile.IsRoot)
if (tile.IsRoot && CHK_AutoExtension.Checked)
Layer.DeleteExtensionTiles(tile, x, y);
tile.Delete();