Action handler in inventory

aurian
Aurian JAULT 2 years ago
parent 4f10a6acdb
commit 14c9004c26

@ -1,10 +1,18 @@
@using Models; @using Models;
<h3>Inventory</h3> <h3>Inventory</h3>
<CascadingValue Value="@this">
<div class="css-grid"> <div class="css-grid">
@for(int i = 0; i<InventoryList.size;i++) @for(int i = 0; i<InventoryList.size;i++)
{ {
<ItemInventory items="@inventory.inventoryItems[i]" index="@i"/> @if (inventory.inventoryItems[i] != null)
{
<ItemInventory items="@inventory.inventoryItems[i]" Index="@i" NoDrop=true />
}
else
{
<ItemInventory items="@inventory.inventoryItems[i]" Index="@i"/>
}
} }
</div> </div>
</CascadingValue>

@ -7,12 +7,18 @@ namespace Blazor.Components
{ {
partial class Inventory partial class Inventory
{ {
public InventoryItem CurrentDragItem { get; set; }
public ObservableCollection<InventoryAction> Actions { get; set; }
public Inventory() public Inventory()
{ {
Actions = new ObservableCollection<InventoryAction>(); Actions = new ObservableCollection<InventoryAction>();
} }
public ObservableCollection<InventoryAction> Actions { get; set; } public void update()
{
this.StateHasChanged();
}
[Parameter] [Parameter]
public InventoryList inventory { get; set; } public InventoryList inventory { get; set; }
public void Add(Item item) public void Add(Item item)

@ -6,6 +6,6 @@ namespace Blazor.Components
{ {
public string Action { get; set; } public string Action { get; set; }
public int Index { get; set; } public int Index { get; set; }
public Item Item { get; set; } public ItemInventory Item { get; set; }
} }
} }

@ -1,13 +1,18 @@
<div class="item"> <div class="item"
@if(items != null) ondragover="event.preventDefault();"
@ondragstart="@OnDragStart"
@ondrop="@OnDrop"
@ondragenter="@OnDragEnter"
@ondragleave="@OnDragLeave">
@if(Items != null)
{ {
@if(!string.IsNullOrWhiteSpace(items.item.ImageBase64)) @if(!string.IsNullOrWhiteSpace(Items.item.ImageBase64))
{ {
<img src="data:image/png;base64, @(items.item.ImageBase64)" class="img-thumbnail" title="@items.item.DisplayName" alt="@items.item.DisplayName" style="min-width: 50px;" /> <img src="data:image/png;base64, @(Items.item.ImageBase64)" class="img-thumbnail" title="@Items.item.DisplayName" alt="@Items.item.DisplayName" style="min-width: 50px;" />
} }
else else
{ {
<img src="images/default.png" class="img-thumbnail" title="@items.item.DisplayName" alt="@items.item.DisplayName" style="fill" /> <img src="images/default.png" class="img-thumbnail" title="@Items.item.DisplayName" alt="@Items.item.DisplayName" style="fill" />
} }
} }
else else

@ -1,4 +1,5 @@
using Blazor.Models; using Blazor.Models;
using Blazorise.Extensions;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
namespace Blazor.Components namespace Blazor.Components
@ -6,9 +7,65 @@ namespace Blazor.Components
public partial class ItemInventory public partial class ItemInventory
{ {
[Parameter] [Parameter]
public InventoryItem items { get; set; } public bool NoDrop { get; set; }
[Parameter] [Parameter]
public int index { get; set; } public InventoryItem Items { get; set; }
[Parameter]
public int Index { get; set; }
[CascadingParameter]
public Inventory Parent { get; set; }
public void OnDragStart()
{
if(!NoDrop)
{
return;
}
Parent.CurrentDragItem = this.Items;
Parent.inventory.inventoryItems[Index] = null;
Parent.update();
}
public void OnDrop()
{
if (!NoDrop)
{
this.Items = Parent.CurrentDragItem;
NoDrop = true;
}
if (Parent.CurrentDragItem != this.Items)
{
InventoryItem tmp = Parent.CurrentDragItem;
Parent.CurrentDragItem = this.Items;
this.Items = tmp;
}
else
{
int total = Parent.CurrentDragItem.Stack + this.Items.Stack;
if (total >this.Items.item.StackSize)
{
this.Items.Stack = this.Items.item.StackSize;
Parent.CurrentDragItem.Stack=total - this.Items.item.StackSize;
}
else
{
this.Items.Stack = total;
}
}
}
internal void OnDragEnter()
{
Parent.Actions.Add(new InventoryAction { Action = "Drag Enter", Item = this, Index = this.Index });
}
internal void OnDragLeave()
{
Parent.Actions.Add(new InventoryAction { Action = "Drag Leave", Item = this, Index = this.Index });
}
} }
} }

@ -8,6 +8,7 @@
<meta charset="utf-8" /> <meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" />
@*<base href="https://codefirst.iut.uca.fr/containers/blazor-web-remiarnal/" />*@ @*<base href="https://codefirst.iut.uca.fr/containers/blazor-web-remiarnal/" />*@
<base href="~/"/>
<link rel="stylesheet" href="css/bootstrap/bootstrap.min.css" /> <link rel="stylesheet" href="css/bootstrap/bootstrap.min.css" />
<link href="css/site.css" rel="stylesheet" /> <link href="css/site.css" rel="stylesheet" />
<link href="Blazor.styles.css" rel="stylesheet" /> <link href="Blazor.styles.css" rel="stylesheet" />

Loading…
Cancel
Save