diff --git a/Minecraft.Crafting.Api/Models/InventoryModel.cs b/Minecraft.Crafting.Api/Models/InventoryModel.cs index 0c0cda5..2664492 100644 --- a/Minecraft.Crafting.Api/Models/InventoryModel.cs +++ b/Minecraft.Crafting.Api/Models/InventoryModel.cs @@ -25,5 +25,6 @@ namespace Minecraft.Crafting.Api.Models /// Gets or sets the position. /// public int Position { get; set; } + public string? ImageBase64 { get; set; } } } \ No newline at end of file diff --git a/blazor_lab/Components/Inventory.razor b/blazor_lab/Components/Inventory.razor new file mode 100644 index 0000000..5f0d3f3 --- /dev/null +++ b/blazor_lab/Components/Inventory.razor @@ -0,0 +1,29 @@ + + +
+ +
+ @for (int row = 0; row < 3; row++) + { +
+ @for (int col = 0; col < 6; col++) + { +
+ @if (InventoryContent != null && InventoryContent.Count > (row * 6 + col)) + { + + } +
+ } +
+ } +
+ +
+

@Localizer["list_of_items"]

+ +
+ +
+ +
\ No newline at end of file diff --git a/blazor_lab/Components/Inventory.razor.cs b/blazor_lab/Components/Inventory.razor.cs new file mode 100644 index 0000000..3f25207 --- /dev/null +++ b/blazor_lab/Components/Inventory.razor.cs @@ -0,0 +1,17 @@ +using Microsoft.AspNetCore.Components; +using Microsoft.Extensions.Localization; +using Minecraft.Crafting.Api.Models; +using Item = blazor_lab.Models.Item; + +namespace blazor_lab.Components +{ + public partial class Inventory + { + [Inject] + public IStringLocalizer Localizer { get; set; } + [Parameter] + public List Items { get; set; } + public InventoryModel? CurrentDragItem { get; set; } = new(); + public List InventoryContent { get; set; } = Enumerable.Range(1, 18).Select(_ => new InventoryModel()).ToList(); + } +} diff --git a/blazor_lab/Components/InventoryGrid.razor.css b/blazor_lab/Components/Inventory.razor.css similarity index 79% rename from blazor_lab/Components/InventoryGrid.razor.css rename to blazor_lab/Components/Inventory.razor.css index 155b226..dd61555 100644 --- a/blazor_lab/Components/InventoryGrid.razor.css +++ b/blazor_lab/Components/Inventory.razor.css @@ -1,4 +1,9 @@ -.inventory-grid { +.side-by-side { + display: flex; + flex-direction: row; +} + +.inventory-grid { display: flex; flex-direction: column; } @@ -18,8 +23,3 @@ align-items: center; overflow: hidden; } - - -.item-count { - margin-top: 5px; -} diff --git a/blazor_lab/Components/InventoryGrid.razor b/blazor_lab/Components/InventoryGrid.razor deleted file mode 100644 index 8ce7449..0000000 --- a/blazor_lab/Components/InventoryGrid.razor +++ /dev/null @@ -1,21 +0,0 @@ -
- @for (int row = 0; row < 3; row++) - { -
- @for (int col = 0; col < 6; col++) - { -
- @if (Inventory != null && Inventory.Count > (row * 6 + col)) - { - var slot = Inventory[row * 6 + col]; - @if (slot.NumberItem > 0) - { - @slot.ItemName -
@slot.NumberItem
- } - } -
- } -
- } -
\ No newline at end of file diff --git a/blazor_lab/Components/InventoryGrid.razor.cs b/blazor_lab/Components/InventoryGrid.razor.cs deleted file mode 100644 index 51c4e58..0000000 --- a/blazor_lab/Components/InventoryGrid.razor.cs +++ /dev/null @@ -1,25 +0,0 @@ -using Microsoft.AspNetCore.Components; -using Minecraft.Crafting.Api.Models; - - -namespace blazor_lab.Components -{ - - public partial class InventoryGrid - { - public List Inventory { get; set; } = Enumerable.Range(1, 18).Select(_ => new InventoryModel()).ToList(); - - /// - /// Used by GetItemImageBase64 in this component, rather than calling our DataService every time. - /// A very basic cache, not kept up to date in any way, but event listeners could be set up in the future - /// - [Parameter] - public List Items { get; set; } - - public string GetItemImageBase64(string displayName) - { - var item = Items.FirstOrDefault(i => i.DisplayName == displayName); - return item?.ImageBase64; - } - } -} diff --git a/blazor_lab/Components/InventoryItem.razor b/blazor_lab/Components/InventoryItem.razor new file mode 100644 index 0000000..27c0028 --- /dev/null +++ b/blazor_lab/Components/InventoryItem.razor @@ -0,0 +1,24 @@ +
+ @if (Item is not null && IsInList) + { +
+ @Item.DisplayName +
@Item.DisplayName
+
+ } + @if (InventoryModel is not null && IsInInventory) + { +
+ @if (InventoryModel.NumberItem > 0) + { +
+ @InventoryModel.ItemName +
+
@InventoryModel.NumberItem
+ } +
+ } +
diff --git a/blazor_lab/Components/InventoryItem.razor.cs b/blazor_lab/Components/InventoryItem.razor.cs new file mode 100644 index 0000000..c40dd1d --- /dev/null +++ b/blazor_lab/Components/InventoryItem.razor.cs @@ -0,0 +1,96 @@ +using Blazorise.Extensions; +using Microsoft.AspNetCore.Components; +using Minecraft.Crafting.Api.Models; +using Item = blazor_lab.Models.Item; + +namespace blazor_lab.Components +{ + public partial class InventoryItem + { + [Parameter] + public Item? Item { get; set; } + + [Parameter] + public int Position { get; set; } = -1; + + [Parameter] + public bool IsInList { get; set; } + + [Parameter] + public bool IsInInventory { get; set; } + + [CascadingParameter] + public InventoryList? ListParent { get; set; } + + [CascadingParameter] + public Inventory? InventoryParent { get; set; } + + public InventoryModel? InventoryModel { get; set; } = new InventoryModel(); + + public InventoryItem() + { + if (IsInInventory) + { + InventoryModel.ImageBase64 = null; + InventoryModel.ItemName = ""; + InventoryModel.NumberItem = 0; + InventoryModel.Position = Position; + } + } + + internal void OnDrop() + { + if (IsInList) + { + ListParent!.Parent.CurrentDragItem = null; + return; + } + + if (IsInInventory) + { + InventoryModel ??= new(); + if (InventoryModel.ItemName.IsNullOrEmpty()) // new inventoryModel + { + InventoryModel.ImageBase64 = InventoryParent!.CurrentDragItem!.ImageBase64; + InventoryModel.ItemName = InventoryParent!.CurrentDragItem!.ItemName; + InventoryModel.Position = Position; + InventoryModel.NumberItem = 1; + InventoryParent.InventoryContent.Insert(Position, InventoryModel); + } + else + { + if (InventoryModel.ItemName == InventoryParent!.CurrentDragItem!.ItemName) // adding to an existing stack + { + InventoryModel.NumberItem += 1; + } + } + InventoryParent!.CurrentDragItem = null; + } + } + + private void OnDragStart() + { + if (IsInList) + { + ListParent!.Parent.CurrentDragItem = new InventoryModel + { + ImageBase64 = Item!.ImageBase64, + ItemName = Item!.DisplayName, + NumberItem = 1, + Position = -1 + }; + } + else if (IsInInventory) // delete item stack if it is dragged from inventory + { + InventoryModel = new InventoryModel + { + ImageBase64 = null, + ItemName = "", + NumberItem = 0, + Position = Position + }; + InventoryParent!.CurrentDragItem = null; + } + } + } +} diff --git a/blazor_lab/Components/InventoryItem.razor.css b/blazor_lab/Components/InventoryItem.razor.css new file mode 100644 index 0000000..43cae0e --- /dev/null +++ b/blazor_lab/Components/InventoryItem.razor.css @@ -0,0 +1,39 @@ +.inventory-list-item { + display: flex; + flex-direction: row; + margin-bottom: 10px; + padding: 10px; + border: 1px solid #ddd; + border-radius: 5px; + box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05); +} + + .inventory-list-item img { + max-width: 64px; + max-height: 64px; + margin-right: 10px; + } + +.inventory-grid-item { + display: flex; + flex-grow: 1; + flex-direction: column; + justify-content: center; + align-items: center; + border: 1px solid #ddd; + height: 64px; + width: 64px; +} + + .inventory-grid-item .slot-image { + font-weight: bold; + font-size: small; + } + + .inventory-grid-item .slot-count { + font-size: small; + } + + .inventory-grid-item .item-name { + font-weight: bold; + } diff --git a/blazor_lab/Components/InventoryList.razor b/blazor_lab/Components/InventoryList.razor index 610ebfd..288aace 100644 --- a/blazor_lab/Components/InventoryList.razor +++ b/blazor_lab/Components/InventoryList.razor @@ -1,4 +1,6 @@ -
+ + +
@@ -12,32 +14,31 @@
- @foreach (var item in VisibleItems) - { -
- @item.DisplayName -
@item.DisplayName
-
- } + @foreach (var item in VisibleItems) + { + + }
- @for (var i = 1; i <= TotalPages; i++) - { - var pageNumber = i; // copy the loop variable to avoid closure issues - - } + @for (var i = 1; i <= TotalPages; i++) + { + var pageNumber = i; // copy the loop variable to avoid closure issues + + }
- @if (VisibleItems.Any()) - { - var firstItem = (currentPage - 1) * pageSize + 1; - var lastItem = Math.Min(currentPage * pageSize, TotalItems); - @firstItem - @lastItem @Localizer["out_of"] @TotalItems - } - else - { - @Localizer["no_items_found"] - } + @if (VisibleItems.Any()) + { + var firstItem = (currentPage - 1) * pageSize + 1; + var lastItem = Math.Min(currentPage * pageSize, TotalItems); + @firstItem - @lastItem @Localizer["out_of"] @TotalItems + } + else + { + @Localizer["no_items_found"] + }
-
\ No newline at end of file + + + \ No newline at end of file diff --git a/blazor_lab/Components/InventoryList.razor.cs b/blazor_lab/Components/InventoryList.razor.cs index 71334eb..6f0628f 100644 --- a/blazor_lab/Components/InventoryList.razor.cs +++ b/blazor_lab/Components/InventoryList.razor.cs @@ -11,6 +11,9 @@ namespace blazor_lab.Components } public partial class InventoryList { + [CascadingParameter] + public Inventory Parent { get; set; } + [Inject] public IStringLocalizer Localizer { get; set; } diff --git a/blazor_lab/Components/InventoryList.razor.css b/blazor_lab/Components/InventoryList.razor.css index 6756e33..04d1cf6 100644 --- a/blazor_lab/Components/InventoryList.razor.css +++ b/blazor_lab/Components/InventoryList.razor.css @@ -4,32 +4,4 @@ .search-container { margin-bottom: 20px; -} - -.inventory-list-item { - margin-bottom: 10px; - padding: 10px; - border: 1px solid #ddd; - border-radius: 5px; - box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05); -} - - .inventory-list-item img { - max-width: 64px; - max-height: 64px; - margin-right: 10px; - } - -.item-name { - font-weight: bold; -} - -.side-by-side { - display: flex; - flex-direction: row; -} - -button[disabled] { - opacity: 0.5; - cursor: default; } \ No newline at end of file diff --git a/blazor_lab/Pages/Inventory.razor b/blazor_lab/Pages/Inventory.razor deleted file mode 100644 index 74772ef..0000000 --- a/blazor_lab/Pages/Inventory.razor +++ /dev/null @@ -1,14 +0,0 @@ -@page "/inventory" -@using Minecraft.Crafting.Api.Models -@using blazor_lab.Components - -
-
-

@Localizer["my_inventory"]

- -
-
-

@Localizer["list_of_items"]

- -
-
\ No newline at end of file diff --git a/blazor_lab/Pages/Inventory.razor.cs b/blazor_lab/Pages/Inventory.razor.cs deleted file mode 100644 index f84cfee..0000000 --- a/blazor_lab/Pages/Inventory.razor.cs +++ /dev/null @@ -1,24 +0,0 @@ -using blazor_lab.Services; -using Microsoft.AspNetCore.Components; -using Microsoft.Extensions.Localization; -using Minecraft.Crafting.Api.Models; -using System.Diagnostics; - -namespace blazor_lab.Pages -{ - public partial class Inventory - { - private List Items = new(); - - [Inject] - public IStringLocalizer Localizer { get; set; } - - [Inject] - private DataApiService DataApiService { get; set; } - - protected override async Task OnInitializedAsync() - { - Items = await DataApiService.All(); - } - } -} diff --git a/blazor_lab/Pages/Inventory.razor.css b/blazor_lab/Pages/Inventory.razor.css deleted file mode 100644 index ff5602f..0000000 --- a/blazor_lab/Pages/Inventory.razor.css +++ /dev/null @@ -1,4 +0,0 @@ -.side-by-side { - display: flex; - flex-direction: row; -} diff --git a/blazor_lab/Pages/InventoryPage.razor b/blazor_lab/Pages/InventoryPage.razor new file mode 100644 index 0000000..021f98d --- /dev/null +++ b/blazor_lab/Pages/InventoryPage.razor @@ -0,0 +1,8 @@ +@page "/inventory" +@using blazor_lab.Components + +@Localizer["inventory"] + +

@Localizer["inventory"]

+ + \ No newline at end of file diff --git a/blazor_lab/Pages/InventoryPage.razor.cs b/blazor_lab/Pages/InventoryPage.razor.cs new file mode 100644 index 0000000..18c1909 --- /dev/null +++ b/blazor_lab/Pages/InventoryPage.razor.cs @@ -0,0 +1,32 @@ +using blazor_lab.Services; +using Microsoft.AspNetCore.Components; +using Microsoft.Extensions.Localization; + +namespace blazor_lab.Pages +{ + public partial class InventoryPage + { + [Inject] + public IStringLocalizer Localizer { get; set; } + + [Inject] + private IDataService DataService { get; set; } + + + private List Items = new(); + + protected override async Task OnAfterRenderAsync(bool firstRender) + { + base.OnAfterRenderAsync(firstRender); + + if (!firstRender) + { + return; + } + + Items = await DataService.List(0, await DataService.Count()); + + StateHasChanged(); + } + } +} diff --git a/blazor_lab/Pages/List.razor.cs b/blazor_lab/Pages/List.razor.cs index 43cca97..ab6c643 100644 --- a/blazor_lab/Pages/List.razor.cs +++ b/blazor_lab/Pages/List.razor.cs @@ -5,7 +5,6 @@ using Blazored.Modal; using Blazored.Modal.Services; using Blazorise.DataGrid; using Microsoft.AspNetCore.Components; -using Microsoft.Extensions.Localization; namespace blazor_lab.Pages { diff --git a/blazor_lab/Program.cs b/blazor_lab/Program.cs index af4cca6..52dbb2f 100644 --- a/blazor_lab/Program.cs +++ b/blazor_lab/Program.cs @@ -43,7 +43,7 @@ builder.Services.Configure(options => options.SupportedUICultures = new List { new CultureInfo("en-US"), new CultureInfo("fr-FR") }; }); -builder.Services.AddScoped(); +builder.Services.AddScoped(); var app = builder.Build(); diff --git a/blazor_lab/Resources/Pages.Inventory.fr-FR.resx b/blazor_lab/Resources/Components.Inventory.fr-FR.resx similarity index 100% rename from blazor_lab/Resources/Pages.Inventory.fr-FR.resx rename to blazor_lab/Resources/Components.Inventory.fr-FR.resx diff --git a/blazor_lab/Resources/Pages.Inventory.resx b/blazor_lab/Resources/Components.Inventory.resx similarity index 100% rename from blazor_lab/Resources/Pages.Inventory.resx rename to blazor_lab/Resources/Components.Inventory.resx diff --git a/blazor_lab/Resources/Components.InventoryList.fr-FR.resx b/blazor_lab/Resources/Components.InventoryList.fr-FR.resx index cccd282..9450ea6 100644 --- a/blazor_lab/Resources/Components.InventoryList.fr-FR.resx +++ b/blazor_lab/Resources/Components.InventoryList.fr-FR.resx @@ -130,9 +130,9 @@ Trier par - Trier par nom (Asc.) + nom (Asc.) - Trier par nom (Desc.) + nom (Desc.) \ No newline at end of file diff --git a/blazor_lab/Resources/Components.InventoryList.resx b/blazor_lab/Resources/Components.InventoryList.resx index 515e221..0e078f8 100644 --- a/blazor_lab/Resources/Components.InventoryList.resx +++ b/blazor_lab/Resources/Components.InventoryList.resx @@ -127,10 +127,10 @@ Search - Sort by name (Asc.) + name (Asc.) - Sort by name (Desc.) + name (Desc.) Sort diff --git a/blazor_lab/Resources/Pages.InventoryPage.fr-FR.resx b/blazor_lab/Resources/Pages.InventoryPage.fr-FR.resx new file mode 100644 index 0000000..dd8a16d --- /dev/null +++ b/blazor_lab/Resources/Pages.InventoryPage.fr-FR.resx @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Inventaire + + \ No newline at end of file diff --git a/blazor_lab/Resources/Pages.InventoryPage.resx b/blazor_lab/Resources/Pages.InventoryPage.resx new file mode 100644 index 0000000..6bf64db --- /dev/null +++ b/blazor_lab/Resources/Pages.InventoryPage.resx @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Inventory + + \ No newline at end of file diff --git a/blazor_lab/Services/DataApiService.cs b/blazor_lab/Services/DataApiService.cs index 2e5c4ab..8156ed9 100644 --- a/blazor_lab/Services/DataApiService.cs +++ b/blazor_lab/Services/DataApiService.cs @@ -31,11 +31,6 @@ namespace blazor_lab.Services return await _http.GetFromJsonAsync($"{_apiBaseUrl}/count"); } - public async Task> All() - { - return await _http.GetFromJsonAsync>($"{_apiBaseUrl}/all"); - } - public async Task> List(int currentPage, int pageSize) { return await _http.GetFromJsonAsync>($"{_apiBaseUrl}/?currentPage={currentPage}&pageSize={pageSize}"); diff --git a/blazor_lab/blazor_lab.csproj b/blazor_lab/blazor_lab.csproj index 2e58a00..9e1a300 100644 --- a/blazor_lab/blazor_lab.csproj +++ b/blazor_lab/blazor_lab.csproj @@ -1,34 +1,37 @@  - - net6.0 - enable - enable - + + net6.0 + enable + enable + - - - - - - - + + + + + + + - - - + + + - - - + + + - - - Designer - - - Designer - - + + + Designer + + + Designer + + + Designer + +