"Acion handled on list of items"

master
Aurian JAULT 3 years ago
parent ee46227910
commit 9cf768b4bd

@ -1,35 +1,35 @@
using Blazor.Models; using Blazor.Models;
using Blazor.Pages; using Blazor.Pages;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop; using Microsoft.JSInterop;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Collections.Specialized; using System.Collections.Specialized;
namespace Blazor.Components namespace Blazor.Components
{ {
partial class Inventory partial class Inventory
{ {
public Models.InventoryList inventory = new Models.InventoryList(); public Models.InventoryList inventory = new Models.InventoryList();
public Models.InventoryItem CurrentDragItem { get; set; } public Models.InventoryItem CurrentDragItem { get; set; }
public ObservableCollection<InventoryAction> Actions { get; set; } public ObservableCollection<InventoryAction> Actions { get; set; }
[Inject] [Inject]
internal IJSRuntime JavaScriptRuntime { get; set; } internal IJSRuntime JavaScriptRuntime { get; set; }
public Inventory() public Inventory()
{ {
Actions = new ObservableCollection<InventoryAction>(); Actions = new ObservableCollection<InventoryAction>();
Actions.CollectionChanged += OnActionsCollectionChanged; Actions.CollectionChanged += OnActionsCollectionChanged;
} }
public void update() public void update()
{ {
this.StateHasChanged(); this.StateHasChanged();
return; return;
} }
private void OnActionsCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) private void OnActionsCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{ {
JavaScriptRuntime.InvokeVoidAsync("Crafting.AddActions", e.NewItems); JavaScriptRuntime.InvokeVoidAsync("Crafting.AddActions", e.NewItems);
} }
} }
} }

@ -1,21 +1,22 @@
@using Blazor.Models @using Blazor.Models
<CascadingValue Value="@this.Parent">
<DataGrid TItem="Item" <DataGrid TItem="Item"
Data="@items" Data="@items"
TotalItems="@totalItem" TotalItems="@totalItem"
PageSize="10" PageSize="10"
ShowPager ShowPager
Filterable Filterable
Responsive> Responsive>
<DataGridColumn TItem="Item" Field="@nameof(Item.Id)" Caption="#" Width="15%"/> <DataGridColumn TItem="Item" Field="@nameof(Item.Id)" Caption="#" Width="15%"/>
<DataGridColumn TItem="Item" Field="@nameof(Item.Id)" Caption="Image" Filterable="false"> <DataGridColumn TItem="Item" Field="@nameof(Item.Id)" Caption="Image" Filterable="false">
<DisplayTemplate> <DisplayTemplate>
<InventoryListImage Item="context"/> <InventoryListImage Item="context"/>
</DisplayTemplate> </DisplayTemplate>
</DataGridColumn>
<DataGridColumn TItem="Item" Field="@nameof(Item.DisplayName)" Caption="Display name">
<DisplayTemplate>
<span style="font-weight: bold; font-size: 2rem; display: flex; justify-content: center;">@context.DisplayName</span>
</DisplayTemplate>
</DataGridColumn> </DataGridColumn>
</DataGrid> <DataGridColumn TItem="Item" Field="@nameof(Item.DisplayName)" Caption="Display name">
<DisplayTemplate>
<span style="font-weight: bold; font-size: 2rem; display: flex; justify-content: center;">@context.DisplayName</span>
</DisplayTemplate>
</DataGridColumn>
</DataGrid>
</CascadingValue>

@ -10,23 +10,32 @@ namespace Blazor.Components
public Item Item { get; set; } public Item Item { get; set; }
[CascadingParameter] [CascadingParameter]
public Crafting Parent { get; set; } public Inventory Parent { get; set; }
internal void OnDragEnter() internal void OnDragEnter()
{ {
Parent.CurrentDragItem = new InventoryItem(Item,1);
Parent.Actions.Add(new InventoryAction("On drag enter", Item.Id, Item));
return;
} }
internal void OnDragLeave() internal void OnDragLeave()
{ {
Parent.Actions.Add(new InventoryAction("On drag leave", Item.Id, Item));
return;
} }
internal void OnDrop() internal void OnDrop()
{ {
Parent.Actions.Add(new InventoryAction("On drop", Item.Id, Item));
Parent.CurrentDragItem = null;
return;
} }
private void OnDragStart() private void OnDragStart()
{ {
Parent.Actions.Add(new InventoryAction("On drag start", Item.Id, Item));
return;
} }
} }
} }

@ -20,7 +20,7 @@ namespace Blazor.Components
public void OnDragStart() public void OnDragStart()
{ {
if(!NoDrop) if(!this.NoDrop)
{ {
Parent.CurrentDragItem = null; Parent.CurrentDragItem = null;
return; return;
@ -36,12 +36,13 @@ namespace Blazor.Components
public void OnDrop() public void OnDrop()
{ {
if (!NoDrop) if (!this.NoDrop)
{ {
this.Items = Parent.CurrentDragItem; this.Items = Parent.CurrentDragItem;
NoDrop = true; NoDrop = true;
Parent.CurrentDragItem = null; Parent.CurrentDragItem = null;
if (this.Items == null) return; if (this.Items == null) return;
Parent.inventory.inventoryItems[this.Index] = this.Items;
Parent.Actions.Add(new InventoryAction("On Drop",this.Index,Items.item)); Parent.Actions.Add(new InventoryAction("On Drop",this.Index,Items.item));
return; return;
} }
@ -55,6 +56,8 @@ namespace Blazor.Components
this.NoDrop= true; this.NoDrop= true;
Parent.CurrentDragItem = null; Parent.CurrentDragItem = null;
Parent.Actions.Add(new InventoryAction("On drag start",this.Index,Items.item)); Parent.Actions.Add(new InventoryAction("On drag start",this.Index,Items.item));
Parent.inventory.inventoryItems[this.Index] = this.Items;
Parent.update();
return; return;
} }
else else
@ -64,11 +67,15 @@ namespace Blazor.Components
{ {
this.Items.Stack = this.Items.item.StackSize; this.Items.Stack = this.Items.item.StackSize;
Parent.Actions.Add(new InventoryAction("On drag start",this.Index,Items.item)); Parent.Actions.Add(new InventoryAction("On drag start",this.Index,Items.item));
Parent.inventory.inventoryItems[this.Index] = this.Items;
Parent.update();
return; return;
} }
else else
{ {
this.Items.Stack = total; this.Items.Stack = total;
Parent.inventory.inventoryItems[this.Index] = this.Items;
Parent.update();
return; return;
} }
} }

@ -11,5 +11,10 @@
item = new Item(); item = new Item();
Stack = 64; Stack = 64;
} }
public InventoryItem(Item item, int stack)
{
this.item = item;
this.stack = stack;
}
} }
} }

Loading…
Cancel
Save