From 0511d26cd4d6cb97ae997b989c483d7c2fb14b5f Mon Sep 17 00:00:00 2001 From: Alexis DRAI Date: Sun, 26 Feb 2023 19:34:27 +0100 Subject: [PATCH] :bug: Prevent crash and bug * when dropping from same inv slot as where started dragging * when dropping an item in inv, then deleting, then dropping another in same slot --- blazor_lab/Components/InventoryItem.razor.cs | 56 ++++++++++---------- 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/blazor_lab/Components/InventoryItem.razor.cs b/blazor_lab/Components/InventoryItem.razor.cs index afa1f2b..2dda9a6 100644 --- a/blazor_lab/Components/InventoryItem.razor.cs +++ b/blazor_lab/Components/InventoryItem.razor.cs @@ -26,16 +26,13 @@ namespace blazor_lab.Components [CascadingParameter] public Inventory? InventoryParent { get; set; } - public InventoryModel? InventoryModel { get; set; } = new InventoryModel(); + public InventoryModel InventoryModel { get; set; } = new InventoryModel(); public InventoryItem() { if (IsInInventory) { - InventoryModel.ImageBase64 = null; - InventoryModel.ItemName = ""; - InventoryModel.NumberItem = 0; - InventoryModel.Position = Position; + InitInventoryModel(); } } @@ -53,10 +50,9 @@ namespace blazor_lab.Components return; } - if (IsInInventory) + if (IsInInventory && InventoryParent!.CurrentDragItem is not null) { - InventoryModel ??= new(); - if (InventoryModel.ItemName.IsNullOrEmpty()) // new inventoryModel + if (InventoryModel!.ItemName.IsNullOrEmpty()) // new inventoryModel { InventoryModel.ImageBase64 = InventoryParent!.CurrentDragItem!.ImageBase64; InventoryModel.ItemName = InventoryParent!.CurrentDragItem!.ItemName; @@ -65,32 +61,30 @@ namespace blazor_lab.Components InventoryParent.InventoryContent.Insert(Position, InventoryModel); InventoryParent.Actions.Add(new InventoryAction { - Action = "Drop on inventory -- successful", + Action = "Drop on inventory -- new -- successful", InventoryModel = InventoryModel, Position = Position }); } - else + else if (InventoryModel.ItemName == InventoryParent!.CurrentDragItem!.ItemName) // adding to an existing stack { - if (InventoryModel.ItemName == InventoryParent!.CurrentDragItem!.ItemName) // adding to an existing stack - { - InventoryModel.NumberItem += 1; - } + InventoryModel.NumberItem += 1; InventoryParent.Actions.Add(new InventoryAction { - Action = "Drop on inventory -- successful", + Action = "Drop on inventory -- add -- successful", InventoryModel = InventoryModel, Position = Position }); } - - InventoryParent.Actions.Add(new InventoryAction + else { - Action = "Drop on inventory -- unsuccessful", - InventoryModel = null, - Position = Position - }); - + InventoryParent.Actions.Add(new InventoryAction + { + Action = "Drop on inventory -- unsuccessful", + InventoryModel = null, + Position = Position + }); + } InventoryParent!.CurrentDragItem = null; } } @@ -113,18 +107,26 @@ namespace blazor_lab.Components Position = ListParent.Parent.CurrentDragItem.Position }); } - else if (IsInInventory && InventoryParent!.CurrentDragItem is not null && InventoryModel is not null) - // delete item stack if it is dragged from inventory + else if (IsInInventory) + // delete item stack if it is dragged from inventory { - InventoryParent.Actions.Add(new InventoryAction + InventoryParent!.Actions.Add(new InventoryAction { Action = "Drag from inventory (deleting)", InventoryModel = InventoryParent.CurrentDragItem, - Position = InventoryParent.CurrentDragItem.Position + Position = Position }); InventoryParent.CurrentDragItem = null; - InventoryModel = null; + InitInventoryModel(); } } + + private void InitInventoryModel() + { + InventoryModel.ImageBase64 = null; + InventoryModel.ItemName = ""; + InventoryModel.NumberItem = 0; + InventoryModel.Position = Position; + } } }