diff --git a/NHSE.Core/Structures/Acres/Terrain/FieldItemLayer.cs b/NHSE.Core/Structures/Acres/Terrain/FieldItemLayer.cs index 6e074f3..f963bcc 100644 --- a/NHSE.Core/Structures/Acres/Terrain/FieldItemLayer.cs +++ b/NHSE.Core/Structures/Acres/Terrain/FieldItemLayer.cs @@ -62,41 +62,64 @@ public void ImportAcre(int acre, byte[] data) } } - public int ClearFieldPlanted(Func criteria) + public int ClearFieldPlanted(Func criteria) => ClearFieldPlanted(0, 0, MapWidth, MapHeight, criteria); + public int RemoveAll(Func criteria) => RemoveAll(0, 0, MapWidth, MapHeight, criteria); + + public int ClearFieldPlanted(int xmin, int ymin, int width, int height, Func criteria) { int count = 0; var fi = FieldItemList.Items; - foreach (var t in Tiles) + + for (int x = xmin; x < xmin + width; x++) { - var disp = t.DisplayItemId; - if (!fi.TryGetValue(disp, out var val)) - continue; + for (int y = ymin; y < ymin + height; y++) + { + var t = GetTile(x, y); + var disp = t.DisplayItemId; + if (!fi.TryGetValue(disp, out var val)) + continue; - if (!criteria(val.Kind)) - continue; - t.Delete(); - count++; + if (!criteria(val.Kind)) + continue; + t.Delete(); + count++; + } } - return count; } - public int RemoveAll(Func criteria) + public int RemoveAll(int xmin, int ymin, int width, int height, Func criteria) { int count = 0; - foreach (var t in Tiles) + for (int x = xmin; x < xmin + width; x++) { - if (!criteria(t)) - continue; - t.Delete(); - count++; + for (int y = ymin; y < ymin + height; y++) + { + var t = GetTile(x, y); + if (!criteria(t)) + continue; + t.Delete(); + count++; + } } - return count; } public int RemoveAllHoles() => ClearFieldPlanted(z => z == FieldItemKind.UnitIconHole); public int RemoveAllWeeds() => ClearFieldPlanted(z => z.IsWeed()); - public int RemoveAllPlants() => ClearFieldPlanted(_ => true); + public int RemoveAllPlants() => ClearFieldPlanted(z => z.IsPlant()); + public int RemoveAllFences() => ClearFieldPlanted(z => z.IsFence()); + public int RemoveAllObjects() => ClearFieldPlanted(_ => true); + public int RemoveAll() => RemoveAll(_ => true); + + public int RemoveAllHoles(int xmin, int ymin, int width, int height) => ClearFieldPlanted(xmin, ymin, width, height, z => z == FieldItemKind.UnitIconHole); + public int RemoveAllWeeds(int xmin, int ymin, int width, int height) => ClearFieldPlanted(xmin, ymin, width, height, z => z.IsWeed()); + public int RemoveAllPlants(int xmin, int ymin, int width, int height) => ClearFieldPlanted(xmin, ymin, width, height, z => z.IsPlant()); + public int RemoveAllFences(int xmin, int ymin, int width, int height) => ClearFieldPlanted(xmin, ymin, width, height, z => z.IsFence()); + public int RemoveAllObjects(int xmin, int ymin, int width, int height) => ClearFieldPlanted(xmin, ymin, width, height, _ => true); + + public int RemoveAll(int xmin, int ymin, int width, int height) => RemoveAll(xmin, ymin, width, height, _ => true); + public int RemoveAllPlacedItems(int xmin, int ymin, int width, int height) => RemoveAll(xmin, ymin, width, + height, z => z.DisplayItemId != FieldItem.NONE && !FieldItemList.Items.ContainsKey(z.DisplayItemId)); } } diff --git a/NHSE.Core/Structures/Item/FieldItem/FieldItemKind.cs b/NHSE.Core/Structures/Item/FieldItem/FieldItemKind.cs index 9eed2c8..abcd2da 100644 --- a/NHSE.Core/Structures/Item/FieldItem/FieldItemKind.cs +++ b/NHSE.Core/Structures/Item/FieldItem/FieldItemKind.cs @@ -53,5 +53,7 @@ public enum FieldItemKind : byte public static class FieldItemKindExtensions { public static bool IsWeed(this FieldItemKind type) => FieldItemKind.PltWeedAut0 <= type && type <= FieldItemKind.PltWeedWin1; + public static bool IsPlant(this FieldItemKind type) => FieldItemKind.PltFlwAnemone <= type && type <= FieldItemKind.PltWeedWin1; + public static bool IsFence(this FieldItemKind type) => FieldItemKind.FenceBamboo <= type && type <= FieldItemKind.FenceWoodWhite; } } diff --git a/NHSE.WinForms/Subforms/FieldItemEditor.Designer.cs b/NHSE.WinForms/Subforms/FieldItemEditor.Designer.cs index 6329234..a975f4d 100644 --- a/NHSE.WinForms/Subforms/FieldItemEditor.Designer.cs +++ b/NHSE.WinForms/Subforms/FieldItemEditor.Designer.cs @@ -61,11 +61,16 @@ private void InitializeComponent() this.B_FillHoles = new System.Windows.Forms.Button(); this.B_RemovePlants = new System.Windows.Forms.Button(); this.B_RemoveAll = new System.Windows.Forms.Button(); + this.B_RemoveFences = new System.Windows.Forms.Button(); + this.B_RemoveObjects = new System.Windows.Forms.Button(); + this.GB_Remove = new System.Windows.Forms.GroupBox(); + this.B_RemovePlacedItems = new System.Windows.Forms.Button(); this.CM_Click.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.PB_Map)).BeginInit(); this.CM_Picture.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.NUD_Layer)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.PB_Acre)).BeginInit(); + this.GB_Remove.SuspendLayout(); this.SuspendLayout(); // // B_Cancel @@ -331,53 +336,96 @@ private void InitializeComponent() // // B_RemoveAllWeeds // - this.B_RemoveAllWeeds.Location = new System.Drawing.Point(767, 393); + this.B_RemoveAllWeeds.Location = new System.Drawing.Point(6, 17); this.B_RemoveAllWeeds.Name = "B_RemoveAllWeeds"; - this.B_RemoveAllWeeds.Size = new System.Drawing.Size(98, 40); + this.B_RemoveAllWeeds.Size = new System.Drawing.Size(92, 23); this.B_RemoveAllWeeds.TabIndex = 29; - this.B_RemoveAllWeeds.Text = "Remove Weeds"; + this.B_RemoveAllWeeds.Text = "Weeds"; this.B_RemoveAllWeeds.UseVisualStyleBackColor = true; this.B_RemoveAllWeeds.Click += new System.EventHandler(this.B_RemoveAllWeeds_Click); // // B_FillHoles // - this.B_FillHoles.Location = new System.Drawing.Point(871, 393); + this.B_FillHoles.Location = new System.Drawing.Point(105, 39); this.B_FillHoles.Name = "B_FillHoles"; - this.B_FillHoles.Size = new System.Drawing.Size(98, 40); + this.B_FillHoles.Size = new System.Drawing.Size(91, 23); this.B_FillHoles.TabIndex = 30; - this.B_FillHoles.Text = "Fill Holes"; + this.B_FillHoles.Text = "Holes"; this.B_FillHoles.UseVisualStyleBackColor = true; this.B_FillHoles.Click += new System.EventHandler(this.B_FillHoles_Click); // // B_RemovePlants // - this.B_RemovePlants.Location = new System.Drawing.Point(767, 439); + this.B_RemovePlants.Location = new System.Drawing.Point(6, 39); this.B_RemovePlants.Name = "B_RemovePlants"; - this.B_RemovePlants.Size = new System.Drawing.Size(98, 40); + this.B_RemovePlants.Size = new System.Drawing.Size(92, 23); this.B_RemovePlants.TabIndex = 31; - this.B_RemovePlants.Text = "Remove Plants"; + this.B_RemovePlants.Text = "Plants"; this.B_RemovePlants.UseVisualStyleBackColor = true; this.B_RemovePlants.Click += new System.EventHandler(this.B_RemovePlants_Click); // // B_RemoveAll // - this.B_RemoveAll.Location = new System.Drawing.Point(871, 439); + this.B_RemoveAll.Location = new System.Drawing.Point(104, 83); this.B_RemoveAll.Name = "B_RemoveAll"; - this.B_RemoveAll.Size = new System.Drawing.Size(98, 40); + this.B_RemoveAll.Size = new System.Drawing.Size(91, 23); this.B_RemoveAll.TabIndex = 32; - this.B_RemoveAll.Text = "Remove All"; + this.B_RemoveAll.Text = "All"; this.B_RemoveAll.UseVisualStyleBackColor = true; this.B_RemoveAll.Click += new System.EventHandler(this.B_RemoveAll_Click); // + // B_RemoveFences + // + this.B_RemoveFences.Location = new System.Drawing.Point(105, 17); + this.B_RemoveFences.Name = "B_RemoveFences"; + this.B_RemoveFences.Size = new System.Drawing.Size(91, 23); + this.B_RemoveFences.TabIndex = 33; + this.B_RemoveFences.Text = "Fences"; + this.B_RemoveFences.UseVisualStyleBackColor = true; + this.B_RemoveFences.Click += new System.EventHandler(this.B_RemoveFences_Click); + // + // B_RemoveObjects + // + this.B_RemoveObjects.Location = new System.Drawing.Point(6, 61); + this.B_RemoveObjects.Name = "B_RemoveObjects"; + this.B_RemoveObjects.Size = new System.Drawing.Size(92, 23); + this.B_RemoveObjects.TabIndex = 34; + this.B_RemoveObjects.Text = "Non-Items"; + this.B_RemoveObjects.UseVisualStyleBackColor = true; + this.B_RemoveObjects.Click += new System.EventHandler(this.B_RemoveObjects_Click); + // + // GB_Remove + // + this.GB_Remove.Controls.Add(this.B_RemovePlacedItems); + this.GB_Remove.Controls.Add(this.B_RemoveFences); + this.GB_Remove.Controls.Add(this.B_RemoveObjects); + this.GB_Remove.Controls.Add(this.B_RemoveAllWeeds); + this.GB_Remove.Controls.Add(this.B_FillHoles); + this.GB_Remove.Controls.Add(this.B_RemovePlants); + this.GB_Remove.Controls.Add(this.B_RemoveAll); + this.GB_Remove.Location = new System.Drawing.Point(767, 385); + this.GB_Remove.Name = "GB_Remove"; + this.GB_Remove.Size = new System.Drawing.Size(202, 113); + this.GB_Remove.TabIndex = 35; + this.GB_Remove.TabStop = false; + this.GB_Remove.Text = "Remove from View (Hold Shift=Map)"; + // + // B_RemovePlacedItems + // + this.B_RemovePlacedItems.Location = new System.Drawing.Point(6, 83); + this.B_RemovePlacedItems.Name = "B_RemovePlacedItems"; + this.B_RemovePlacedItems.Size = new System.Drawing.Size(92, 23); + this.B_RemovePlacedItems.TabIndex = 36; + this.B_RemovePlacedItems.Text = "Placed Items"; + this.B_RemovePlacedItems.UseVisualStyleBackColor = true; + this.B_RemovePlacedItems.Click += new System.EventHandler(this.B_RemovePlacedItems_Click); + // // 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.B_RemoveAll); - this.Controls.Add(this.B_RemovePlants); - this.Controls.Add(this.B_FillHoles); - this.Controls.Add(this.B_RemoveAllWeeds); + this.Controls.Add(this.GB_Remove); this.Controls.Add(this.PB_Acre); this.Controls.Add(this.label1); this.Controls.Add(this.NUD_Layer); @@ -409,6 +457,7 @@ private void InitializeComponent() this.CM_Picture.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.NUD_Layer)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.PB_Acre)).EndInit(); + this.GB_Remove.ResumeLayout(false); this.ResumeLayout(false); this.PerformLayout(); @@ -446,5 +495,9 @@ private void InitializeComponent() private System.Windows.Forms.Button B_FillHoles; private System.Windows.Forms.Button B_RemovePlants; private System.Windows.Forms.Button B_RemoveAll; + private System.Windows.Forms.Button B_RemoveFences; + private System.Windows.Forms.Button B_RemoveObjects; + private System.Windows.Forms.GroupBox GB_Remove; + private System.Windows.Forms.Button B_RemovePlacedItems; } } \ No newline at end of file diff --git a/NHSE.WinForms/Subforms/FieldItemEditor.cs b/NHSE.WinForms/Subforms/FieldItemEditor.cs index af67859..a52d333 100644 --- a/NHSE.WinForms/Subforms/FieldItemEditor.cs +++ b/NHSE.WinForms/Subforms/FieldItemEditor.cs @@ -399,64 +399,37 @@ private void PB_Map_MouseMove(object sender, MouseEventArgs e) private void NUD_Layer_ValueChanged(object sender, EventArgs e) => LoadGrid(X, Y); - private void B_RemoveAllWeeds_Click(object sender, EventArgs e) + private void Remove(Control sender, Func removal) { - int count = Layer.RemoveAllWeeds(); - if (count == 0) - { - WinFormsUtil.Alert("Nothing removed from the map (none found)."); - return; - } - LoadGrid(X, Y); - System.Media.SystemSounds.Asterisk.Play(); - } + bool wholeMap = ModifierKeys == Keys.Shift; - private void B_FillHoles_Click(object sender, EventArgs e) - { - int count = Layer.RemoveAllHoles(); - if (count == 0) - { - WinFormsUtil.Alert("Nothing removed from the map (none found)."); - return; - } - LoadGrid(X, Y); - System.Media.SystemSounds.Asterisk.Play(); - } - - private void B_RemovePlants_Click(object sender, EventArgs e) - { - const string q = "Are you sure you want to remove all plants?"; + string q = $"Are you sure you want to remove {sender.Text}?"; var question = WinFormsUtil.Prompt(MessageBoxButtons.YesNo, q); if (question != DialogResult.Yes) return; - int count = Layer.RemoveAllPlants(); + var count = wholeMap + ? removal(0, 0, Layer.MapWidth, Layer.MapHeight) + : removal(X, Y, Layer.GridWidth, Layer.GridHeight); + if (count == 0) { - WinFormsUtil.Alert("Nothing removed from the map (none found)."); + WinFormsUtil.Alert("Nothing removed (none found)."); return; } LoadGrid(X, Y); - System.Media.SystemSounds.Asterisk.Play(); + WinFormsUtil.Alert($"Removed {count} from the map."); } - private void B_RemoveAll_Click(object sender, EventArgs e) - { - const string q = "Are you sure you want to remove everything?"; - var question = WinFormsUtil.Prompt(MessageBoxButtons.YesNo, q); - if (question != DialogResult.Yes) - return; - - int count = Layer.RemoveAll(_ => true); - if (count == 0) - { - WinFormsUtil.Alert("Nothing removed from the map (none found)."); - return; - } - LoadGrid(X, Y); - System.Media.SystemSounds.Asterisk.Play(); - } + private void B_RemoveAllWeeds_Click(object sender, EventArgs e) => Remove(B_RemoveAllWeeds, Layer.RemoveAllWeeds); + private void B_FillHoles_Click(object sender, EventArgs e) => Remove(B_FillHoles, Layer.RemoveAllHoles); + private void B_RemovePlants_Click(object sender, EventArgs e) => Remove(B_RemovePlants, Layer.RemoveAllPlants); + private void B_RemoveFences_Click(object sender, EventArgs e) => Remove(B_RemoveFences, Layer.RemoveAllFences); + private void B_RemoveObjects_Click(object sender, EventArgs e) => Remove(B_RemoveObjects, Layer.RemoveAllObjects); + private void B_RemoveAll_Click(object sender, EventArgs e) => Remove(B_RemoveAll, Layer.RemoveAll); + private void B_RemovePlacedItems_Click(object sender, EventArgs e) => Remove(B_RemovePlacedItems, Layer.RemoveAllPlacedItems); private void PG_Tile_PropertyValueChanged(object s, PropertyValueChangedEventArgs e) => PG_Tile.SelectedObject = PG_Tile.SelectedObject; + } }