diff --git a/blazor_lab/Components/Inventory.razor b/blazor_lab/Components/Inventory.razor index 5f0d3f3..07d395a 100644 --- a/blazor_lab/Components/Inventory.razor +++ b/blazor_lab/Components/Inventory.razor @@ -26,4 +26,6 @@ +
+ \ No newline at end of file diff --git a/blazor_lab/Components/Inventory.razor.cs b/blazor_lab/Components/Inventory.razor.cs index 3f25207..8b3177b 100644 --- a/blazor_lab/Components/Inventory.razor.cs +++ b/blazor_lab/Components/Inventory.razor.cs @@ -1,6 +1,9 @@ using Microsoft.AspNetCore.Components; using Microsoft.Extensions.Localization; +using Microsoft.JSInterop; using Minecraft.Crafting.Api.Models; +using System.Collections.ObjectModel; +using System.Collections.Specialized; using Item = blazor_lab.Models.Item; namespace blazor_lab.Components @@ -9,9 +12,24 @@ namespace blazor_lab.Components { [Inject] public IStringLocalizer Localizer { get; set; } + [Inject] + internal IJSRuntime JavaScriptRuntime { get; set; } [Parameter] public List Items { get; set; } public InventoryModel? CurrentDragItem { get; set; } = new(); public List InventoryContent { get; set; } = Enumerable.Range(1, 18).Select(_ => new InventoryModel()).ToList(); + + public Inventory() + { + Actions = new ObservableCollection(); + Actions.CollectionChanged += OnActionsCollectionChanged; + } + + public ObservableCollection Actions { get; set; } + + private void OnActionsCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) + { + JavaScriptRuntime.InvokeVoidAsync("Inventory.AddActions", e.NewItems); + } } } diff --git a/blazor_lab/Components/Inventory.razor.css b/blazor_lab/Components/Inventory.razor.css index dd61555..2957546 100644 --- a/blazor_lab/Components/Inventory.razor.css +++ b/blazor_lab/Components/Inventory.razor.css @@ -23,3 +23,9 @@ align-items: center; overflow: hidden; } + +.actions { + border: 1px solid black; + height: 250px; + overflow: scroll; +} \ No newline at end of file diff --git a/blazor_lab/Components/Inventory.razor.js b/blazor_lab/Components/Inventory.razor.js new file mode 100644 index 0000000..078bedc --- /dev/null +++ b/blazor_lab/Components/Inventory.razor.js @@ -0,0 +1,16 @@ +window.Inventory = +{ + AddActions: function (data) { + + data.forEach(element => { + const div = document.createElement('div'); + div.innerHTML = 'Action: ' + element.action + ' - Position: ' + element.position; + + if (element.inventoryModel) { + div.innerHTML += ' - Item Name: ' + element.inventoryModel.itemName; + } + + document.getElementById('actions').appendChild(div); + }); + } +} \ No newline at end of file diff --git a/blazor_lab/Components/InventoryAction.cs b/blazor_lab/Components/InventoryAction.cs new file mode 100644 index 0000000..37054e9 --- /dev/null +++ b/blazor_lab/Components/InventoryAction.cs @@ -0,0 +1,11 @@ +using Minecraft.Crafting.Api.Models; + +namespace blazor_lab.Components +{ + public class InventoryAction + { + public string? Action { get; set; } + public int Position { get; set; } + public InventoryModel? InventoryModel { get; set; } + } +} diff --git a/blazor_lab/Components/InventoryItem.razor.cs b/blazor_lab/Components/InventoryItem.razor.cs index c40dd1d..afa1f2b 100644 --- a/blazor_lab/Components/InventoryItem.razor.cs +++ b/blazor_lab/Components/InventoryItem.razor.cs @@ -1,4 +1,5 @@ -using Blazorise.Extensions; +using Blazorise; +using Blazorise.Extensions; using Microsoft.AspNetCore.Components; using Minecraft.Crafting.Api.Models; using Item = blazor_lab.Models.Item; @@ -43,6 +44,12 @@ namespace blazor_lab.Components if (IsInList) { ListParent!.Parent.CurrentDragItem = null; + ListParent.Parent.Actions.Add(new InventoryAction + { + Action = "Tried to drop on list", + InventoryModel = null, + Position = -1 + }); return; } @@ -56,6 +63,12 @@ namespace blazor_lab.Components InventoryModel.Position = Position; InventoryModel.NumberItem = 1; InventoryParent.InventoryContent.Insert(Position, InventoryModel); + InventoryParent.Actions.Add(new InventoryAction + { + Action = "Drop on inventory -- successful", + InventoryModel = InventoryModel, + Position = Position + }); } else { @@ -63,7 +76,21 @@ namespace blazor_lab.Components { InventoryModel.NumberItem += 1; } + InventoryParent.Actions.Add(new InventoryAction + { + Action = "Drop on inventory -- successful", + InventoryModel = InventoryModel, + Position = Position + }); } + + InventoryParent.Actions.Add(new InventoryAction + { + Action = "Drop on inventory -- unsuccessful", + InventoryModel = null, + Position = Position + }); + InventoryParent!.CurrentDragItem = null; } } @@ -79,17 +106,24 @@ namespace blazor_lab.Components NumberItem = 1, Position = -1 }; + ListParent.Parent.Actions.Add(new InventoryAction + { + Action = "Drag from list", + InventoryModel = ListParent.Parent.CurrentDragItem, + Position = ListParent.Parent.CurrentDragItem.Position + }); } - else if (IsInInventory) // delete item stack if it is dragged from inventory + else if (IsInInventory && InventoryParent!.CurrentDragItem is not null && InventoryModel is not null) + // delete item stack if it is dragged from inventory { - InventoryModel = new InventoryModel + InventoryParent.Actions.Add(new InventoryAction { - ImageBase64 = null, - ItemName = "", - NumberItem = 0, - Position = Position - }; - InventoryParent!.CurrentDragItem = null; + Action = "Drag from inventory (deleting)", + InventoryModel = InventoryParent.CurrentDragItem, + Position = InventoryParent.CurrentDragItem.Position + }); + InventoryParent.CurrentDragItem = null; + InventoryModel = null; } } } diff --git a/blazor_lab/Pages/_Layout.cshtml b/blazor_lab/Pages/_Layout.cshtml index 26c864a..068c4ca 100644 --- a/blazor_lab/Pages/_Layout.cshtml +++ b/blazor_lab/Pages/_Layout.cshtml @@ -30,6 +30,7 @@ +