Update Drag item

master
Louis DUFOUR 2 years ago
parent 8a1d6cea09
commit 9b0fa999bb

@ -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; }
}
}

@ -1,14 +1,23 @@
<div
class="item"
ondragover="event.preventDefault();"
draggable="true"
@ondragstart="@OnDragStart"
@ondrop="@OnDrop"
@ondragenter="@OnDragEnter"
@ondragleave="@OnDragLeave">
<div class="item"
ondragover="event.preventDefault();"
draggable="true"
@ondragstart="@OnDragStart"
@ondrop="@OnDrop"
@ondragenter="@OnDragEnter"
@ondragleave="@OnDragLeave"
@ondragend="@OnDragEnd">
@if (Item != null)
{
<img src="data:image/png;base64, @(Item.ImageBase64)" class="img-thumbnail imageList" title="@Item.DisplayName" alt="@Item.DisplayName"/>
<div style="position:relative">
<img src="data:image/png;base64, @(Item.ImageBase64)" class="img-thumbnail imageList" title="@Item.DisplayName" alt="@Item.DisplayName" /> @if (Item.Num > 0)
{
<div class="number">
@(
Item.Num
)
</div>
}
</div>
}
</div>

@ -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; }
/// <summary>
/// method call when a drag enter a slot and send an action
/// </summary>
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 });
}
/// <summary>
/// method call when a drag leave a slot and send an action
/// </summary>
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 });
}
/// <summary>
/// method that manage a drop and send an action
/// </summary>
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 });
}
/// <summary>
/// method call when darg start and send an action
/// </summary>
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 });
}
/// <summary>
/// method call when drag end and send an action specialy when outside the inventory
/// </summary>
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);
}
}
}

@ -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;
}
}
}

@ -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]

@ -12,5 +12,7 @@
public DateTime CreatedDate { get; set; }
public DateTime? UpdatedDate { get; set; }
public string ImageBase64 { get; set; }
public int Num { get; set; }
}
}

@ -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<Item> items;
private int totalItem;
private List<Inventory> invents;
public List<Item> Jitems;
private DataGrid<Item> dataGrid;
private string _searchText;
@ -45,14 +46,18 @@ namespace ValblazeProject.Pages
// Gestion logs
public ObservableCollection<CraftingAction> Actions { get; set; }
public ObservableCollection<InventoryAction> Actions { get; set; }
public Item CurrentDragItem { get; set; }
public Inventaire()
{
Actions = new ObservableCollection<CraftingAction>();
Actions = new ObservableCollection<InventoryAction>();
Actions.CollectionChanged += OnActionsCollectionChanged;
string fileName = "Inventory.json";
string jsonString = File.ReadAllText(fileName);
this.Jitems = JsonSerializer.Deserialize<List<Item>>(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

@ -1,17 +1,17 @@
{
"format": 1,
"restore": {
"E:\\Documents\\Cloud\\DUT\\Blazor\\Projet\\ValblazeProject\\ValblazeProject.csproj": {}
"E:\\Temporaire\\MyProject\\ValblazeProject\\ValblazeProject.csproj": {}
},
"projects": {
"E:\\Documents\\Cloud\\DUT\\Blazor\\Projet\\ValblazeProject\\ValblazeProject.csproj": {
"E:\\Temporaire\\MyProject\\ValblazeProject\\ValblazeProject.csproj": {
"version": "1.0.0",
"restore": {
"projectUniqueName": "E:\\Documents\\Cloud\\DUT\\Blazor\\Projet\\ValblazeProject\\ValblazeProject.csproj",
"projectUniqueName": "E:\\Temporaire\\MyProject\\ValblazeProject\\ValblazeProject.csproj",
"projectName": "ValblazeProject",
"projectPath": "E:\\Documents\\Cloud\\DUT\\Blazor\\Projet\\ValblazeProject\\ValblazeProject.csproj",
"projectPath": "E:\\Temporaire\\MyProject\\ValblazeProject\\ValblazeProject.csproj",
"packagesPath": "C:\\Users\\louis\\.nuget\\packages\\",
"outputPath": "E:\\Documents\\Cloud\\DUT\\Blazor\\Projet\\ValblazeProject\\obj\\",
"outputPath": "E:\\Temporaire\\MyProject\\ValblazeProject\\obj\\",
"projectStyle": "PackageReference",
"fallbackFolders": [
"C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\NuGetPackages"

@ -959,11 +959,11 @@
"project": {
"version": "1.0.0",
"restore": {
"projectUniqueName": "E:\\Documents\\Cloud\\DUT\\Blazor\\Projet\\ValblazeProject\\ValblazeProject.csproj",
"projectUniqueName": "E:\\Temporaire\\MyProject\\ValblazeProject\\ValblazeProject.csproj",
"projectName": "ValblazeProject",
"projectPath": "E:\\Documents\\Cloud\\DUT\\Blazor\\Projet\\ValblazeProject\\ValblazeProject.csproj",
"projectPath": "E:\\Temporaire\\MyProject\\ValblazeProject\\ValblazeProject.csproj",
"packagesPath": "C:\\Users\\louis\\.nuget\\packages\\",
"outputPath": "E:\\Documents\\Cloud\\DUT\\Blazor\\Projet\\ValblazeProject\\obj\\",
"outputPath": "E:\\Temporaire\\MyProject\\ValblazeProject\\obj\\",
"projectStyle": "PackageReference",
"fallbackFolders": [
"C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\NuGetPackages"

Loading…
Cancel
Save