From 4ed1eb0d494175b3936fdca3f3be62b818f6a67b Mon Sep 17 00:00:00 2001 From: runtenick Date: Sun, 13 Nov 2022 11:07:38 +0100 Subject: [PATCH] Utilisation du composant, erreur: java interops calls --- BlazorTp1/Components/Crafting.razor | 53 +++++++++++++ BlazorTp1/Components/Crafting.razor.cs | 86 ++++++++++++++++++++++ BlazorTp1/Components/Crafting.razor.css | 19 +++++ BlazorTp1/Components/Crafting.razor.js | 16 ++++ BlazorTp1/Components/CraftingAction.cs | 11 +++ BlazorTp1/Components/CraftingItem.razor | 16 ++++ BlazorTp1/Components/CraftingItem.razor.cs | 64 ++++++++++++++++ BlazorTp1/Components/CraftingRecipe.cs | 10 +++ BlazorTp1/Pages/Index.razor | 6 +- BlazorTp1/Pages/Index.razor.cs | 24 ++++++ BlazorTp1/Pages/_Layout.cshtml | 2 + BlazorTp1/Services/DataLocalService.cs | 20 +++++ BlazorTp1/Services/IDataService.cs | 5 +- BlazorTp1/wwwroot/index.html | 12 +++ 14 files changed, 342 insertions(+), 2 deletions(-) create mode 100644 BlazorTp1/Components/Crafting.razor create mode 100644 BlazorTp1/Components/Crafting.razor.cs create mode 100644 BlazorTp1/Components/Crafting.razor.css create mode 100644 BlazorTp1/Components/Crafting.razor.js create mode 100644 BlazorTp1/Components/CraftingAction.cs create mode 100644 BlazorTp1/Components/CraftingItem.razor create mode 100644 BlazorTp1/Components/CraftingItem.razor.cs create mode 100644 BlazorTp1/Components/CraftingRecipe.cs create mode 100644 BlazorTp1/Pages/Index.razor.cs create mode 100644 BlazorTp1/wwwroot/index.html diff --git a/BlazorTp1/Components/Crafting.razor b/BlazorTp1/Components/Crafting.razor new file mode 100644 index 0000000..b6c8ec7 --- /dev/null +++ b/BlazorTp1/Components/Crafting.razor @@ -0,0 +1,53 @@ +

Crafting

+ + +
+
+
+ +
Available items:
+
+
+ + @foreach (var item in Items) + { + + } +
+
+ +
+ +
+
Recipe
+ +
+ +
+ + + + + + + + + +
+
+ +
Result
+
+ +
+
+ +
+
Actions
+
+ +
+
+
+
+
diff --git a/BlazorTp1/Components/Crafting.razor.cs b/BlazorTp1/Components/Crafting.razor.cs new file mode 100644 index 0000000..8f7d934 --- /dev/null +++ b/BlazorTp1/Components/Crafting.razor.cs @@ -0,0 +1,86 @@ +using BlazorTp1.Models; +using Microsoft.AspNetCore.Components; +using Microsoft.JSInterop; +using System.Collections.ObjectModel; +using System.Collections.Specialized; + +namespace BlazorTp1.Components +{ + public partial class Crafting + { + private Item _recipeResult; + + public Crafting() + { + Actions = new ObservableCollection(); + Actions.CollectionChanged += OnActionsCollectionChanged; + this.RecipeItems = new List { null, null, null, null, null, null, null, null, null }; + } + + public ObservableCollection Actions { get; set; } + public Item CurrentDragItem { get; set; } + + [Parameter] + public List Items { get; set; } + + public List RecipeItems { get; set; } + + //se der ruim tirar esse attributo + [CascadingParameter] + public Crafting Parent { get; set; } + + public Item RecipeResult + { + get => this._recipeResult; + set + { + if (this._recipeResult == value) + { + return; + } + + this._recipeResult = value; + this.StateHasChanged(); + } + } + + [Parameter] + public List Recipes { get; set; } + + + /// + /// Gets or sets the java script runtime. + /// + [Inject] + internal IJSRuntime JavaScriptRuntime { get; set; } + + public void CheckRecipe() + { + RecipeResult = null; + + // Get the current model + var currentModel = string.Join("|", this.RecipeItems.Select(s => s != null ? s.Name : string.Empty)); + + this.Actions.Add(new CraftingAction { Action = $"Items : {currentModel}" }); + + foreach (var craftingRecipe in Recipes) + { + // Get the recipe model + var recipeModel = string.Join("|", craftingRecipe.Have.SelectMany(s => s)); + + this.Actions.Add(new CraftingAction { Action = $"Recipe model : {recipeModel}" }); + + if (currentModel == recipeModel) + { + RecipeResult = craftingRecipe.Give; + } + } + } + + private void OnActionsCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) + { + JavaScriptRuntime.InvokeVoidAsync("Crafting.AddActions", e.NewItems); + } + } +} + diff --git a/BlazorTp1/Components/Crafting.razor.css b/BlazorTp1/Components/Crafting.razor.css new file mode 100644 index 0000000..2a388f2 --- /dev/null +++ b/BlazorTp1/Components/Crafting.razor.css @@ -0,0 +1,19 @@ +.css-grid { + grid-template-columns: repeat(4,minmax(0,1fr)); + gap: 10px; + display: grid; + width: 286px; +} + +.css-recipe { + grid-template-columns: repeat(3,minmax(0,1fr)); + gap: 10px; + display: grid; + width: 212px; +} + +.actions { + border: 1px solid black; + height: 250px; + overflow: scroll; +} diff --git a/BlazorTp1/Components/Crafting.razor.js b/BlazorTp1/Components/Crafting.razor.js new file mode 100644 index 0000000..8fdb58e --- /dev/null +++ b/BlazorTp1/Components/Crafting.razor.js @@ -0,0 +1,16 @@ +window.Crafting = +{ + AddActions: function (data) { + + data.forEach(element => { + var div = document.createElement('div'); + div.innerHTML = 'Action: ' + element.action + ' - Index: ' + element.index; + + if (element.item) { + div.innerHTML += ' - Item Name: ' + element.item.name; + } + + document.getElementById('actions').appendChild(div); + }); + } +} \ No newline at end of file diff --git a/BlazorTp1/Components/CraftingAction.cs b/BlazorTp1/Components/CraftingAction.cs new file mode 100644 index 0000000..f682f85 --- /dev/null +++ b/BlazorTp1/Components/CraftingAction.cs @@ -0,0 +1,11 @@ +using BlazorTp1.Models; + +namespace BlazorTp1.Components +{ + public class CraftingAction + { + public string Action { get; set; } + public int Index { get; set; } + public Item Item { get; set; } + } +} diff --git a/BlazorTp1/Components/CraftingItem.razor b/BlazorTp1/Components/CraftingItem.razor new file mode 100644 index 0000000..b9c9fd3 --- /dev/null +++ b/BlazorTp1/Components/CraftingItem.razor @@ -0,0 +1,16 @@ +

CraftingItem

+ +
+ + @if (Item != null) + { + @Item.DisplayName + } +
diff --git a/BlazorTp1/Components/CraftingItem.razor.cs b/BlazorTp1/Components/CraftingItem.razor.cs new file mode 100644 index 0000000..e4d225b --- /dev/null +++ b/BlazorTp1/Components/CraftingItem.razor.cs @@ -0,0 +1,64 @@ +using Blazorise; +using BlazorTp1.Models; +using Microsoft.AspNetCore.Components; + +namespace BlazorTp1.Components +{ + public partial class CraftingItem + { + [Parameter] + public int Index { get; set; } + + [Parameter] + public Item Item { get; set; } + + [Parameter] + public bool NoDrop { get; set; } + + [CascadingParameter] + public Crafting Parent { get; set; } + + internal void OnDragEnter() + { + if (NoDrop) + { + return; + } + + Parent.Actions.Add(new CraftingAction { Action = "Drag Enter", Item = this.Item, Index = this.Index }); + } + + internal void OnDragLeave() + { + if (NoDrop) + { + return; + } + + Parent.Actions.Add(new CraftingAction { Action = "Drag Leave", Item = this.Item, Index = this.Index }); + } + + internal void OnDrop() + { + if (NoDrop) + { + return; + } + + this.Item = Parent.CurrentDragItem; + Parent.RecipeItems[this.Index] = this.Item; + + Parent.Actions.Add(new CraftingAction { Action = "Drop", Item = this.Item, Index = this.Index }); + + // Check recipe + Parent.CheckRecipe(); + } + + private void OnDragStart() + { + Parent.CurrentDragItem = this.Item; + + Parent.Actions.Add(new CraftingAction { Action = "Drag Start", Item = this.Item, Index = this.Index }); + } + } +} diff --git a/BlazorTp1/Components/CraftingRecipe.cs b/BlazorTp1/Components/CraftingRecipe.cs new file mode 100644 index 0000000..2da1ed1 --- /dev/null +++ b/BlazorTp1/Components/CraftingRecipe.cs @@ -0,0 +1,10 @@ +using BlazorTp1.Models; + +namespace BlazorTp1.Components +{ + public class CraftingRecipe + { + public Item Give { get; set; } + public List> Have { get; set; } + } +} diff --git a/BlazorTp1/Pages/Index.razor b/BlazorTp1/Pages/Index.razor index 9fa6e36..c4e48ca 100644 --- a/BlazorTp1/Pages/Index.razor +++ b/BlazorTp1/Pages/Index.razor @@ -3,7 +3,7 @@ Index -

Hello, world!

+

Tp Blazor Nicolas Franco

Welcome to your new app. @@ -11,4 +11,8 @@ Welcome to your new app. CurrentCulture: @CultureInfo.CurrentCulture

+
+ +
+ diff --git a/BlazorTp1/Pages/Index.razor.cs b/BlazorTp1/Pages/Index.razor.cs new file mode 100644 index 0000000..1a5c3f8 --- /dev/null +++ b/BlazorTp1/Pages/Index.razor.cs @@ -0,0 +1,24 @@ +using BlazorTp1.Components; +using BlazorTp1.Models; +using Microsoft.AspNetCore.Components; + +namespace BlazorTp1.Pages +{ + public partial class Index + { + [Inject] + public IDataService DataService { get; set; } + + public List Items { get; set; } = new List(); + + private List Recipes { get; set; } = new List(); + + protected override async Task OnInitializedAsync() + { + await base.OnInitializedAsync(); + + Items = await DataService.List(0, await DataService.Count()); + Recipes = await DataService.GetRecipes(); + } + } +} diff --git a/BlazorTp1/Pages/_Layout.cshtml b/BlazorTp1/Pages/_Layout.cshtml index a30151a..ea9b1bb 100644 --- a/BlazorTp1/Pages/_Layout.cshtml +++ b/BlazorTp1/Pages/_Layout.cshtml @@ -37,5 +37,7 @@ + + diff --git a/BlazorTp1/Services/DataLocalService.cs b/BlazorTp1/Services/DataLocalService.cs index 367f128..13f109d 100644 --- a/BlazorTp1/Services/DataLocalService.cs +++ b/BlazorTp1/Services/DataLocalService.cs @@ -1,4 +1,5 @@ using Blazored.LocalStorage; +using BlazorTp1.Components; using BlazorTp1.Factories; using BlazorTp1.Models; using Microsoft.AspNetCore.Components; @@ -162,4 +163,23 @@ public class DataLocalService : IDataService // Save the data await _localStorage.SetItemAsync("data", currentData); } + + public Task> GetRecipes() + { + var items = new List + { + new CraftingRecipe + { + Give = new Item { DisplayName = "Diamond", Name = "diamond" }, + Have = new List> + { + new List { "dirt", "dirt", "dirt" }, + new List { "dirt", null, "dirt" }, + new List { "dirt", "dirt", "dirt" } + } + } + }; + + return Task.FromResult(items); + } } \ No newline at end of file diff --git a/BlazorTp1/Services/IDataService.cs b/BlazorTp1/Services/IDataService.cs index c621463..a03b7ad 100644 --- a/BlazorTp1/Services/IDataService.cs +++ b/BlazorTp1/Services/IDataService.cs @@ -1,4 +1,5 @@ -using BlazorTp1.Models; +using BlazorTp1.Components; +using BlazorTp1.Models; using System; public interface IDataService @@ -14,4 +15,6 @@ public interface IDataService Task Update(int id, ItemModel item); Task Delete(int id); + + Task> GetRecipes(); } diff --git a/BlazorTp1/wwwroot/index.html b/BlazorTp1/wwwroot/index.html new file mode 100644 index 0000000..8cb9f7a --- /dev/null +++ b/BlazorTp1/wwwroot/index.html @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file