From ce33f8d425e2fea074f95e8f30076d3050c5f3f0 Mon Sep 17 00:00:00 2001 From: aujault Date: Tue, 20 Dec 2022 13:53:15 +0100 Subject: [PATCH] display number --- .../Blazor/Components/CraftingItem.razor.css | 19 ++++++-- Blazor/Blazor/Components/Inventory.razor.cs | 47 ++++++++++++++++++- .../Blazor/Components/InventoryList.razor.cs | 22 --------- .../Components/InventoryListImage.razor.cs | 4 +- Blazor/Blazor/Components/ItemInventory.razor | 16 +++++-- .../Blazor/Components/ItemInventory.razor.cs | 6 ++- Blazor/Blazor/Models/InventoryItem.cs | 4 +- 7 files changed, 82 insertions(+), 36 deletions(-) diff --git a/Blazor/Blazor/Components/CraftingItem.razor.css b/Blazor/Blazor/Components/CraftingItem.razor.css index 1499794..3db9040 100644 --- a/Blazor/Blazor/Components/CraftingItem.razor.css +++ b/Blazor/Blazor/Components/CraftingItem.razor.css @@ -1,6 +1,17 @@ .item { - width: 64px; - height: 64px; - border: 1px solid; - overflow: hidden; + position: absolute; + text-align: center; + width: 50px; + height: 50px; + color: white; +} + +.number-item{ + position: absolute; + bottom: 5px; + right: 5px; + font-size: 3em; + font-weight: bold; + -webkit-text-stroke: 2px black; + font-family: 'Segoe UI', Tahoma, Verdana, Geneva, Tahoma, sans-serif } \ No newline at end of file diff --git a/Blazor/Blazor/Components/Inventory.razor.cs b/Blazor/Blazor/Components/Inventory.razor.cs index e792288..88e2f9e 100644 --- a/Blazor/Blazor/Components/Inventory.razor.cs +++ b/Blazor/Blazor/Components/Inventory.razor.cs @@ -1,9 +1,14 @@ using Blazor.Models; using Blazor.Pages; +using Blazored.LocalStorage; +using Blazorise.DataGrid; +using Blazorise; using Microsoft.AspNetCore.Components; using Microsoft.JSInterop; using System.Collections.ObjectModel; using System.Collections.Specialized; +using static System.Net.WebRequestMethods; +using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage; namespace Blazor.Components { @@ -16,20 +21,60 @@ namespace Blazor.Components [Inject] internal IJSRuntime JavaScriptRuntime { get; set; } + [Inject] + public ILocalStorageService LocalStorage { get; set; } + public Inventory() { Actions = new ObservableCollection(); Actions.CollectionChanged += OnActionsCollectionChanged; } - public void update() + public async void update() { + await LocalStorage.SetItemAsync("list", inventory); this.StateHasChanged(); return; } + private void OnActionsCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) { JavaScriptRuntime.InvokeVoidAsync("Crafting.AddActions", e.NewItems); } + + + protected override async Task OnAfterRenderAsync(bool firstRender) + { + // Do not treat this action if is not the first render + if (!firstRender) + { + return; + } + + var currentData = await LocalStorage.GetItemAsync("data"); + + // Check if data exist in the local storage + if (currentData == null) + { + // this code add in the local storage the fake data (we load the data sync for initialize the data before load the OnReadData method) + await LocalStorage.SetItemAsync("list", inventory); + } + } + + private async Task OnReadData(DataGridReadDataEventArgs e) + { + if (e.CancellationToken.IsCancellationRequested) + { + return; + } + + var response = (await LocalStorage.GetItemAsync("list")); + + if (!e.CancellationToken.IsCancellationRequested) + { + inventory = response; + } + } + } } diff --git a/Blazor/Blazor/Components/InventoryList.razor.cs b/Blazor/Blazor/Components/InventoryList.razor.cs index 2660e9c..f0dc2a0 100644 --- a/Blazor/Blazor/Components/InventoryList.razor.cs +++ b/Blazor/Blazor/Components/InventoryList.razor.cs @@ -32,28 +32,6 @@ namespace Blazor.Components [CascadingParameter] public Inventory Parent { get; set; } - - public void OnDragStart() - { - - } - - - public void OnDrop() - { - Parent.CurrentDragItem = null; - return; - } - internal void OnDragEnter() - { - Parent.Actions.Add(new InventoryAction("Drag Enter", this.items[0].Id, items[0])); - } - - internal void OnDragLeave() - { - Parent.Actions.Add(new InventoryAction("Drag Leave", this.items[0].Id, items[0])); - } - protected override async Task OnInitializedAsync() { items = await DataService.getAll(); diff --git a/Blazor/Blazor/Components/InventoryListImage.razor.cs b/Blazor/Blazor/Components/InventoryListImage.razor.cs index b90a328..a492163 100644 --- a/Blazor/Blazor/Components/InventoryListImage.razor.cs +++ b/Blazor/Blazor/Components/InventoryListImage.razor.cs @@ -14,7 +14,7 @@ namespace Blazor.Components internal void OnDragEnter() { - Parent.CurrentDragItem = new InventoryItem(Item,1); + Parent.Actions.Add(new InventoryAction("On drag enter", Item.Id, Item)); return; } @@ -34,7 +34,9 @@ namespace Blazor.Components private void OnDragStart() { + Parent.CurrentDragItem = new InventoryItem(Item); Parent.Actions.Add(new InventoryAction("On drag start", Item.Id, Item)); + Parent.update(); return; } } diff --git a/Blazor/Blazor/Components/ItemInventory.razor b/Blazor/Blazor/Components/ItemInventory.razor index 950c574..dda1f9d 100644 --- a/Blazor/Blazor/Components/ItemInventory.razor +++ b/Blazor/Blazor/Components/ItemInventory.razor @@ -8,15 +8,23 @@ { @if(!string.IsNullOrWhiteSpace(Items.item.ImageBase64)) { - @Items.item.DisplayName +
+ @Items.item.DisplayName + @Items.Stack +
} else { - @Items.item.DisplayName - } +
+ @Items.item.DisplayName + @Items.Stack +
+ } } else { - empty slot +
+ empty slot +
} \ No newline at end of file diff --git a/Blazor/Blazor/Components/ItemInventory.razor.cs b/Blazor/Blazor/Components/ItemInventory.razor.cs index 98446ac..b408b8c 100644 --- a/Blazor/Blazor/Components/ItemInventory.razor.cs +++ b/Blazor/Blazor/Components/ItemInventory.razor.cs @@ -39,7 +39,7 @@ namespace Blazor.Components if (!this.NoDrop) { this.Items = Parent.CurrentDragItem; - NoDrop = true; + this.NoDrop = true; Parent.CurrentDragItem = null; if (this.Items == null) return; Parent.inventory.inventoryItems[this.Index] = this.Items; @@ -55,8 +55,8 @@ namespace Blazor.Components this.Items = Parent.CurrentDragItem; this.NoDrop= true; Parent.CurrentDragItem = null; - Parent.Actions.Add(new InventoryAction("On drag start",this.Index,Items.item)); Parent.inventory.inventoryItems[this.Index] = this.Items; + Parent.Actions.Add(new InventoryAction("On drag start",this.Index,Items.item)); Parent.update(); return; } @@ -66,6 +66,7 @@ namespace Blazor.Components if (total >this.Items.item.StackSize) { this.Items.Stack = this.Items.item.StackSize; + Parent.CurrentDragItem = null; Parent.Actions.Add(new InventoryAction("On drag start",this.Index,Items.item)); Parent.inventory.inventoryItems[this.Index] = this.Items; Parent.update(); @@ -74,6 +75,7 @@ namespace Blazor.Components else { this.Items.Stack = total; + Parent.CurrentDragItem = null; Parent.inventory.inventoryItems[this.Index] = this.Items; Parent.update(); return; diff --git a/Blazor/Blazor/Models/InventoryItem.cs b/Blazor/Blazor/Models/InventoryItem.cs index c4a138c..f3a0369 100644 --- a/Blazor/Blazor/Models/InventoryItem.cs +++ b/Blazor/Blazor/Models/InventoryItem.cs @@ -11,10 +11,10 @@ item = new Item(); Stack = 64; } - public InventoryItem(Item item, int stack) + public InventoryItem(Item item) { this.item = item; - this.stack = stack; + Stack = 1; } } }