From 18f5385ab44907680301b91c7473284404fc3e8c Mon Sep 17 00:00:00 2001 From: Thomas Chazot Date: Fri, 9 Dec 2022 10:13:13 +0100 Subject: [PATCH] Partie inventaire qui marche --- .../BlazorApp/Components/InventoryComp.razor | 60 +++++++++++-------- .../Components/InventoryComp.razor.cs | 12 ++-- .../BlazorApp/Components/InventoryItem.razor | 3 +- .../Components/InventoryItem.razor.cs | 43 +++++++++++-- 4 files changed, 82 insertions(+), 36 deletions(-) diff --git a/BlazorApp/BlazorApp/Components/InventoryComp.razor b/BlazorApp/BlazorApp/Components/InventoryComp.razor index 5e627f9..6223635 100644 --- a/BlazorApp/BlazorApp/Components/InventoryComp.razor +++ b/BlazorApp/BlazorApp/Components/InventoryComp.razor @@ -37,37 +37,45 @@
-
Available items:
+ @foreach (var item in Items) + { + + } -
- +
+
- \ No newline at end of file diff --git a/BlazorApp/BlazorApp/Components/InventoryComp.razor.cs b/BlazorApp/BlazorApp/Components/InventoryComp.razor.cs index 182d0ae..2470a44 100644 --- a/BlazorApp/BlazorApp/Components/InventoryComp.razor.cs +++ b/BlazorApp/BlazorApp/Components/InventoryComp.razor.cs @@ -28,11 +28,8 @@ namespace BlazorApp.Components public InventoryComp() { - this.InventoryItems = new List { }; - for (int i = 0; i < 18; i++) - { - this.InventoryItems.Append(null); - } + this.InventoryItems = new List(new Item[18]); + } private async Task OnReadData(DataGridReadDataEventArgs e) @@ -54,5 +51,10 @@ namespace BlazorApp.Components Items = await DataService.SortedList(); } + public void refresh() + { + StateHasChanged(); + } + } } \ No newline at end of file diff --git a/BlazorApp/BlazorApp/Components/InventoryItem.razor b/BlazorApp/BlazorApp/Components/InventoryItem.razor index e64e6cd..298b7eb 100644 --- a/BlazorApp/BlazorApp/Components/InventoryItem.razor +++ b/BlazorApp/BlazorApp/Components/InventoryItem.razor @@ -4,7 +4,8 @@ @ondragstart="@OnDragStart" @ondrop="@OnDrop" @ondragenter="@OnDragEnter" - @ondragleave="@OnDragLeave"> + @ondragleave="@OnDragLeave" + @ondragend="@OnDragEnd" > @if (Item != null) { diff --git a/BlazorApp/BlazorApp/Components/InventoryItem.razor.cs b/BlazorApp/BlazorApp/Components/InventoryItem.razor.cs index 4bb4d74..6af5477 100644 --- a/BlazorApp/BlazorApp/Components/InventoryItem.razor.cs +++ b/BlazorApp/BlazorApp/Components/InventoryItem.razor.cs @@ -44,15 +44,50 @@ namespace BlazorApp.Components return; } - Parent.InventoryItems[Parent.CurrentDragIndex] = this.Item; - this.Item = Parent.CurrentDragItem; - Parent.InventoryItems[this.Index] = this.Item; + if (Parent.CurrentDragItem != null) + { + Item tmp = this.Item; + this.Item = Parent.CurrentDragItem; + Parent.InventoryItems[this.Index] = this.Item; + Parent.CurrentDragItem = tmp; + Parent.CurrentDragIndex = this.Index; + } } private void OnDragStart() { - Parent.CurrentDragIndex = this.Index; + Parent.CurrentDragItem = this.Item; + if (this.Item != null) + { + if (!NoDrop) + { + Parent.CurrentDragIndex = this.Index; + this.Item = null; + Parent.InventoryItems[Index] = null; + } + else + { + Parent.CurrentDragIndex = -1; + } + + } + } + + internal void OnDragEnd() + { + if (NoDrop || Parent.CurrentDragItem==null) + { + return; + } + if (Parent.CurrentDragIndex != -1 && Parent.CurrentDragIndex!=this.Index) + { + Parent.InventoryItems[this.Index] = this.Item; + this.Item = Parent.CurrentDragItem; + } + Parent.CurrentDragIndex = -1; + Parent.CurrentDragItem = null; + } } }