Implement drag and drop

omg
pull/5/head
Alexis Drai 2 years ago
parent 8eda266e57
commit 7a27fe6299

@ -9,12 +9,9 @@ namespace blazor_lab.Components
{ {
[Inject] [Inject]
public IStringLocalizer<Inventory> Localizer { get; set; } public IStringLocalizer<Inventory> Localizer { get; set; }
[Parameter] [Parameter]
public List<Item> Items { get; set; } public List<Item> Items { get; set; }
public InventoryModel? CurrentDragItem { get; set; } = new();
public InventoryModel? CurrentDragItem { get; set; }
public List<InventoryModel> InventoryContent { get; set; } = Enumerable.Range(1, 18).Select(_ => new InventoryModel()).ToList(); public List<InventoryModel> InventoryContent { get; set; } = Enumerable.Range(1, 18).Select(_ => new InventoryModel()).ToList();
} }
} }

@ -1,7 +1,6 @@
<div ondragover="event.preventDefault();" <div ondragover="event.preventDefault();"
draggable="true" draggable="true"
@ondragstart="@OnDragStart" @ondragstart="@OnDragStart"
@ondragend="@OnDragEnd"
@ondrop="@OnDrop"> @ondrop="@OnDrop">
@if (Item is not null && IsInList) @if (Item is not null && IsInList)
{ {
@ -12,10 +11,12 @@
} }
@if (InventoryModel is not null && IsInInventory) @if (InventoryModel is not null && IsInInventory)
{ {
<div> <div class="inventory-grid-item">
@if (InventoryModel.NumberItem > 0) @if (InventoryModel.NumberItem > 0)
{ {
<div>@InventoryModel.ItemName</div> <div class="slot-image">
<img src="@($"data:image/png;base64,{InventoryModel.ImageBase64}")" alt="@InventoryModel.ItemName" />
</div>
} }
<div class="slot-count">@InventoryModel.NumberItem</div> <div class="slot-count">@InventoryModel.NumberItem</div>
</div> </div>

@ -1,9 +1,6 @@
using blazor_lab.Services; using Blazorise.Extensions;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using Minecraft.Crafting.Api;
using Minecraft.Crafting.Api.Models; using Minecraft.Crafting.Api.Models;
using System.Diagnostics;
using Item = blazor_lab.Models.Item; using Item = blazor_lab.Models.Item;
namespace blazor_lab.Components namespace blazor_lab.Components
@ -28,14 +25,16 @@ namespace blazor_lab.Components
[CascadingParameter] [CascadingParameter]
public Inventory? InventoryParent { get; set; } public Inventory? InventoryParent { get; set; }
public InventoryModel InventoryModel { get; set; } = new InventoryModel(); public InventoryModel? InventoryModel { get; set; } = new InventoryModel();
public InventoryItem() public InventoryItem()
{ {
if (Item is not null && IsInList) if (IsInInventory)
{ {
InventoryModel.ItemName = Item.DisplayName; InventoryModel.ImageBase64 = null;
InventoryModel.NumberItem = 1; InventoryModel.ItemName = "";
InventoryModel.NumberItem = 0;
InventoryModel.Position = Position;
} }
} }
@ -43,15 +42,19 @@ namespace blazor_lab.Components
{ {
if (IsInList) if (IsInList)
{ {
ListParent!.Parent.CurrentDragItem = null;
return; return;
} }
if (IsInInventory) if (IsInInventory)
{ {
if (InventoryModel.Position == -1) // new inventoryModel InventoryModel ??= new();
if (InventoryModel.ItemName.IsNullOrEmpty()) // new inventoryModel
{ {
InventoryModel = InventoryParent!.CurrentDragItem!; InventoryModel.ImageBase64 = InventoryParent!.CurrentDragItem!.ImageBase64;
InventoryModel.ItemName = InventoryParent!.CurrentDragItem!.ItemName;
InventoryModel.Position = Position; InventoryModel.Position = Position;
InventoryModel.NumberItem = 1;
InventoryParent.InventoryContent.Insert(Position, InventoryModel); InventoryParent.InventoryContent.Insert(Position, InventoryModel);
} }
else else
@ -61,34 +64,32 @@ namespace blazor_lab.Components
InventoryModel.NumberItem += 1; InventoryModel.NumberItem += 1;
} }
} }
}
StateHasChanged();
}
internal void OnDragEnd()
{
if (IsInList)
{
ListParent!.Parent.CurrentDragItem = null;
}
else if (IsInInventory)
{
InventoryParent!.CurrentDragItem = null; InventoryParent!.CurrentDragItem = null;
} }
} }
private void OnDragStart() private void OnDragStart()
{ {
if (InventoryModel is not null) if (IsInList)
{ {
if (IsInList) ListParent!.Parent.CurrentDragItem = new InventoryModel
{ {
ListParent!.Parent.CurrentDragItem = InventoryModel; ImageBase64 = Item!.ImageBase64,
} ItemName = Item!.DisplayName,
else if (IsInInventory) NumberItem = 1,
Position = -1
};
}
else if (IsInInventory) // delete item stack if it is dragged from inventory
{
InventoryModel = new InventoryModel
{ {
InventoryParent!.CurrentDragItem = InventoryModel; ImageBase64 = null,
} ItemName = "",
NumberItem = 0,
Position = Position
};
InventoryParent!.CurrentDragItem = null;
} }
} }
} }

@ -12,6 +12,15 @@
margin-right: 10px; margin-right: 10px;
} }
.slot-image {
font-weight : bold;
font-size: small;
}
.slot-count {
font-size: small;
}
.item-name { .item-name {
font-weight: bold; font-weight: bold;
} }

Loading…
Cancel
Save