From 9b0fa999bb19611409599a143bb9dda7957583a4 Mon Sep 17 00:00:00 2001 From: Louis DUFOUR Date: Fri, 30 Dec 2022 18:26:19 +0100 Subject: [PATCH] Update Drag item --- ValblazeProject/Components/InventoryAction.cs | 11 +++ .../Components/InventoryItem.razor | 27 +++++--- .../Components/InventoryItem.razor.cs | 65 +++++++++++++++--- ValblazeProject/Factories/ItemFactory.cs | 37 +++++++++- ValblazeProject/Inventory.json | 1 + ValblazeProject/Models/Item.cs | 2 + ValblazeProject/Pages/Inventaire.razor.cs | 17 +++-- .../Debug/net6.0/ValblazeProject.assets.cache | Bin 13304 -> 13304 bytes .../ValblazeProject.csproj.nuget.dgspec.json | 10 +-- ValblazeProject/obj/project.assets.json | 6 +- 10 files changed, 142 insertions(+), 34 deletions(-) create mode 100644 ValblazeProject/Components/InventoryAction.cs create mode 100644 ValblazeProject/Inventory.json diff --git a/ValblazeProject/Components/InventoryAction.cs b/ValblazeProject/Components/InventoryAction.cs new file mode 100644 index 0000000..b768eda --- /dev/null +++ b/ValblazeProject/Components/InventoryAction.cs @@ -0,0 +1,11 @@ +using ValblazeProject.Models; + +namespace ValblazeProject.Components +{ + public class InventoryAction + { + public string Action { get; set; } + public int Index { get; set; } + public Item Item { get; set; } + } +} diff --git a/ValblazeProject/Components/InventoryItem.razor b/ValblazeProject/Components/InventoryItem.razor index d11a60a..a29e6fe 100644 --- a/ValblazeProject/Components/InventoryItem.razor +++ b/ValblazeProject/Components/InventoryItem.razor @@ -1,14 +1,23 @@ -
+
@if (Item != null) { - @Item.DisplayName +
+ @Item.DisplayName @if (Item.Num > 0) + { +
+ @( + Item.Num + ) +
+ } +
}
\ No newline at end of file diff --git a/ValblazeProject/Components/InventoryItem.razor.cs b/ValblazeProject/Components/InventoryItem.razor.cs index fff03de..88d27c6 100644 --- a/ValblazeProject/Components/InventoryItem.razor.cs +++ b/ValblazeProject/Components/InventoryItem.razor.cs @@ -1,4 +1,6 @@ using Microsoft.AspNetCore.Components; +using System.Text.Json; +using ValblazeProject.Factories; using ValblazeProject.Models; using ValblazeProject.Pages; @@ -18,6 +20,9 @@ namespace ValblazeProject.Components [CascadingParameter] public Inventaire Parent { get; set; } + /// + /// method call when a drag enter a slot and send an action + /// internal void OnDragEnter() { if (NoDrop) @@ -25,9 +30,11 @@ namespace ValblazeProject.Components return; } - Parent.Actions.Add(new CraftingAction { Action = "Drag Enter", Item = this.Item, Index = this.Index }); + Parent.Actions.Add(new InventoryAction { Action = "Drag Enter", Item = this.Item, Index = this.Index }); } - + /// + /// method call when a drag leave a slot and send an action + /// internal void OnDragLeave() { if (NoDrop) @@ -35,27 +42,67 @@ namespace ValblazeProject.Components return; } - Parent.Actions.Add(new CraftingAction { Action = "Drag Leave", Item = this.Item, Index = this.Index }); + Parent.Actions.Add(new InventoryAction { Action = "Drag Leave", Item = this.Item, Index = this.Index }); } - + /// + /// method that manage a drop and send an action + /// internal void OnDrop() { - if (NoDrop) + + if (NoDrop == true || Parent.CurrentDragItem == null) { return; } + if (this.Item == null) + { + this.Item = ItemFactory.Create(Parent.CurrentDragItem); + } + else if (this.Item.Id == Parent.CurrentDragItem.Id) + { + if (this.Item.StackSize > this.Item.Num) + { + ItemFactory.Add1(this.Item, this.Item); + } + + } - this.Item = Parent.CurrentDragItem; + Parent.Jitems[this.Index] = this.Item; + string fileName = "Inventory.json"; + string jsonString = JsonSerializer.Serialize(Parent.Jitems); + File.WriteAllText(fileName, jsonString); + + + Parent.Actions.Add(new InventoryAction { Action = "Drop", Item = this.Item, Index = this.Index }); - Parent.Actions.Add(new CraftingAction { Action = "Drop", Item = this.Item, Index = this.Index }); } + /// + /// method call when darg start and send an action + /// - internal void OnDragStart() + private void OnDragStart() { + Parent.CurrentDragItem = this.Item; - Parent.Actions.Add(new CraftingAction { Action = "Drag Start", Item = this.Item, Index = this.Index }); + Parent.Actions.Add(new InventoryAction { Action = "Drag Start", Item = this.Item, Index = this.Index }); + } + /// + /// method call when drag end and send an action specialy when outside the inventory + /// + private void OnDragEnd() + { + if (Parent.Actions.Last().Action == "Drag Leave") + { + this.Item = null; + } + Parent.Actions.Add(new InventoryAction { Action = "Delete", Item = this.Item, Index = this.Index }); + + Parent.Jitems[this.Index] = null; + string fileName = "Inventory.json"; + string jsonString = JsonSerializer.Serialize(Parent.Jitems); + File.WriteAllText(fileName, jsonString); } } } diff --git a/ValblazeProject/Factories/ItemFactory.cs b/ValblazeProject/Factories/ItemFactory.cs index 8c49500..5bf0b6b 100644 --- a/ValblazeProject/Factories/ItemFactory.cs +++ b/ValblazeProject/Factories/ItemFactory.cs @@ -32,7 +32,8 @@ namespace ValblazeProject.Factories MaxDurability = model.MaxDurability, StackSize = model.StackSize, CreatedDate = DateTime.Now, - ImageBase64 = Convert.ToBase64String(model.ImageContent) + ImageBase64 = Convert.ToBase64String(model.ImageContent), + Num = 1 }; } @@ -47,5 +48,39 @@ namespace ValblazeProject.Factories item.UpdatedDate = DateTime.Now; item.ImageBase64 = Convert.ToBase64String(model.ImageContent); } + public static Item Create(Item model) + { + return new Item + { + Id = model.Id, + DisplayName = model.DisplayName, + Name = model.Name, + RepairWith = model.RepairWith, + EnchantCategories = model.EnchantCategories, + MaxDurability = model.MaxDurability, + StackSize = model.StackSize, + CreatedDate = DateTime.Now, + ImageBase64 = model.ImageBase64, + Num = 1, + }; + } + + public static void Update(Item item, Item model) + { + item.DisplayName = model.DisplayName; + item.Name = model.Name; + item.RepairWith = model.RepairWith; + item.EnchantCategories = model.EnchantCategories; + item.MaxDurability = model.MaxDurability; + item.StackSize = model.StackSize; + item.UpdatedDate = DateTime.Now; + item.ImageBase64 = model.ImageBase64; + item.Num = model.Num; + } + + public static void Add1(Item item, Item model) + { + item.Num = model.Num + 1; + } } } diff --git a/ValblazeProject/Inventory.json b/ValblazeProject/Inventory.json new file mode 100644 index 0000000..36b0943 --- /dev/null +++ b/ValblazeProject/Inventory.json @@ -0,0 +1 @@ +[null,null,null,null,null,null,{"Id":4,"DisplayName":"Cobblestone","Name":"cobblestone","StackSize":64,"MaxDurability":0,"EnchantCategories":[],"RepairWith":[],"CreatedDate":"2022-12-30T18:25:35.8494594+01:00","UpdatedDate":null,"ImageBase64":"iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAA0ElEQVQ4y22S0Q2FMAwDMyMbMABvjcck3YD5QKl01WHxUVDcxElc1\u002B847n3f7zHGPNu2zRjsuq75P//nxPpvrJzYQJ8m6diJ4JB04xdBB/33RBQaT5JiXICMjUPGOk1W/XECYxNDCM7hrvqiCVIHjwxmTZYGqbB390o0Md6E5WRYc2\u002BvRQ53ZTbUzmJIrQnalYtTeU9Ed686Rcyi7EKhReUllgZO8J7e3S\u002BCS5cGTk5N0okWeVk5PW8rf73My8p2Vlr3q9i\u002BmSKmZb9I3DVf7AEDw33Q7E89pwAAAABJRU5ErkJggg==","Num":1},{"Id":4,"DisplayName":"Cobblestone","Name":"cobblestone","StackSize":64,"MaxDurability":0,"EnchantCategories":[],"RepairWith":[],"CreatedDate":"2022-12-30T18:25:28.241733+01:00","UpdatedDate":null,"ImageBase64":"iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAA0ElEQVQ4y22S0Q2FMAwDMyMbMABvjcck3YD5QKl01WHxUVDcxElc1\u002B847n3f7zHGPNu2zRjsuq75P//nxPpvrJzYQJ8m6diJ4JB04xdBB/33RBQaT5JiXICMjUPGOk1W/XECYxNDCM7hrvqiCVIHjwxmTZYGqbB390o0Md6E5WRYc2\u002BvRQ53ZTbUzmJIrQnalYtTeU9Ed686Rcyi7EKhReUllgZO8J7e3S\u002BCS5cGTk5N0okWeVk5PW8rf73My8p2Vlr3q9i\u002BmSKmZb9I3DVf7AEDw33Q7E89pwAAAABJRU5ErkJggg==","Num":2},null,null,null,null,null,null,null,null,null,null] \ No newline at end of file diff --git a/ValblazeProject/Models/Item.cs b/ValblazeProject/Models/Item.cs index 3d8d9de..cfb6037 100644 --- a/ValblazeProject/Models/Item.cs +++ b/ValblazeProject/Models/Item.cs @@ -12,5 +12,7 @@ public DateTime CreatedDate { get; set; } public DateTime? UpdatedDate { get; set; } public string ImageBase64 { get; set; } + public int Num { get; set; } + } } diff --git a/ValblazeProject/Pages/Inventaire.razor.cs b/ValblazeProject/Pages/Inventaire.razor.cs index 0cf59f8..6d9e9fd 100644 --- a/ValblazeProject/Pages/Inventaire.razor.cs +++ b/ValblazeProject/Pages/Inventaire.razor.cs @@ -15,6 +15,7 @@ using System.Linq; using Microsoft.JSInterop; using System.Collections.Specialized; using Microsoft.AspNetCore.Components.Web; +using System.Text.Json; namespace ValblazeProject.Pages { @@ -36,7 +37,7 @@ namespace ValblazeProject.Pages private List items; private int totalItem; - private List invents; + public List Jitems; private DataGrid dataGrid; private string _searchText; @@ -45,14 +46,18 @@ namespace ValblazeProject.Pages // Gestion logs - public ObservableCollection Actions { get; set; } + public ObservableCollection Actions { get; set; } public Item CurrentDragItem { get; set; } public Inventaire() { - Actions = new ObservableCollection(); + Actions = new ObservableCollection(); Actions.CollectionChanged += OnActionsCollectionChanged; + + string fileName = "Inventory.json"; + string jsonString = File.ReadAllText(fileName); + this.Jitems = JsonSerializer.Deserialize>(jsonString)!; } private void OnActionsCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) @@ -61,7 +66,7 @@ namespace ValblazeProject.Pages } // Drag -/* protected override async Task OnAfterRenderAsync(bool firstRender) + protected override async Task OnAfterRenderAsync(bool firstRender) { base.OnAfterRenderAsync(firstRender); @@ -70,10 +75,8 @@ namespace ValblazeProject.Pages return; } - //invents = await DataService.List(0, await DataService.Count()); - StateHasChanged(); - }*/ + } /******************* Attribut modifier *******************/ private string search diff --git a/ValblazeProject/obj/Debug/net6.0/ValblazeProject.assets.cache b/ValblazeProject/obj/Debug/net6.0/ValblazeProject.assets.cache index 303f5d439899b855c97dd9e79efcdd224c4e6f50..d3c0373900130e98de97634b32741503e97e682e 100644 GIT binary patch delta 49 zcmV-10M7sTXZU9*P)kQa3jhEB{wf%MLqtjv_xSd|orw8;L|6?g0iAONDIQ)_-I(6t Hu_(|p^6(Vt delta 49 zcmV-10M7sTXZU9*P)kQa3jhEB+jQIO6m3PGQFIw{Z!)F{@G~d`1IZ5BfH#|;9YeKT Hu_(|p