EventHorizon.Blazor.Mascot 0.2.0

dotnet add package EventHorizon.Blazor.Mascot --version 0.2.0
NuGet\Install-Package EventHorizon.Blazor.Mascot -Version 0.2.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="EventHorizon.Blazor.Mascot" Version="0.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EventHorizon.Blazor.Mascot --version 0.2.0
#r "nuget: EventHorizon.Blazor.Mascot, 0.2.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install EventHorizon.Blazor.Mascot as a Cake Addin
#addin nuget:?package=EventHorizon.Blazor.Mascot&version=0.2.0

// Install EventHorizon.Blazor.Mascot as a Cake Tool
#tool nuget:?package=EventHorizon.Blazor.Mascot&version=0.2.0

EventHorizon.Blazor.Mascot

About EventHorizon Blazor Mascot

All you need to display a little mascot on your website built with Blazor.

Usage

Below is a good starting point to displaying the mascot in your Blazor application.

dotnet install EventHorizon.Blazor.Mascot

A Full Screen Razor Page (FullScreenAvatar.razor):

@page "/full-screen"
@implements IDisposable
@layout FullScreenLayout
@using BlazorPro.BlazorSize
@using EventHorizon.Blazor.Mascot.Actions
@using EventHorizon.Blazor.Mascot.Character
@using EventHorizon.Blazor.Mascot.Components

<PageTitle>Full Screen Avatar</PageTitle>

@if (Canvas != null)
{
    <div class="canvas__wrapper">
        <div class="canvas__container">
            <MascotCanvas Avatar="Avatar"
                      Environment="Environment"
                      SpeechBubbleDetails="SpeechBubbleDetails" />
        </div>
    </div>
}
else
{
    <div>Loading...</div>
}

The Code Behind of the Full Screen Razor Page (FullScreenAvatar.razor.cs):

namespace Mascot.Pages;

using BlazorPro.BlazorSize;

using EventHorizon.Blazor.Mascot.Actions;
using EventHorizon.Blazor.Mascot.Character;

using Microsoft.AspNetCore.Components;

public partial class FullScreenAvatar
{
    [Inject]
    public IHttpClientFactory HttpClientFactory { get; set; } = null!;
    [Inject]
    public NavigationManager NavigationManager { get; set; } = null!;
    [Inject]
    public IResizeListener ResizeListener { get; set; } = null!;
    protected Canvas? Canvas { get; set; }

    protected CharacterConfig? Character { get; set; }
    protected CanvasEnvironment? Environment => Canvas?.Environment;
    protected MascotAvatar? Avatar;
    // Supports a SpeechBubble, shown above the correlated Avatar.
    protected SpeechBubbleDetails SpeechBubbleDetails = new();

    protected string AvatarConfigUrl { get; set; } = "/hinata/character.json";
    protected string AvatarConfigBaseUrl { get; set; } = string.Empty;
    protected int EnvironmentHeight { get; set; } = 300;
    protected int EnvironmentWidth { get; set; } = 1000;

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await base.OnAfterRenderAsync(firstRender);

        if(firstRender)
        {
            ResizeListener.OnResized += HandleWindowResize;
            await Setup();
        }
    }

    private void HandleWindowResize(
        object? obj,
        BrowserWindowSize args
    )
    {
        Avatar?.Dispose();
        InvokeAsync(async () =>
        {
            await Setup();
        });
    }

    public void Dispose()
    {
        Avatar?.Dispose();
    }

    public async Task Setup()
    {
        var browserSize = await ResizeListener.GetBrowserWindowSize();
        // 200 is the top padding, so we can see the Avatar walk on the top
        EnvironmentHeight = browserSize.Height - 200;
        EnvironmentWidth = browserSize.Width;

        var client = HttpClientFactory.CreateClient();
        AvatarConfigBaseUrl = NavigationManager.BaseUri.ToString();
        Character = await (await client.GetAsync(
            NavigationManager.ToAbsoluteUri(
                AvatarConfigUrl
            )
        )).Content.ReadFromJsonAsync<CharacterConfig>();
        if (Character is null)
        {
            return;
        }

        if (!string.IsNullOrEmpty(AvatarConfigBaseUrl))
        {
            Character.BaseUrl = $"{AvatarConfigBaseUrl}{Character.BaseUrl}";
        }
        Avatar = new MascotAvatar(
            Canvas = new Canvas(
                new CanvasEnvironment(
                    "zbox",
                    EnvironmentHeight,
                    EnvironmentWidth
                )
            ),
            Character
        );
        Canvas.OnChange += () =>
        {
            InvokeAsync(StateHasChanged);
        };
        Avatar.Init(200, 200);
        Avatar.Behavior("zbox", new StandardAction("zbox", Avatar));
        Avatar.Action("fall", 40);
    }
}

Deployment

# Create NuGet Package
dotnet pack --output packages --configuration Release -p:Version=$PACKAGE_VERSION -p:PackageVersion=$PACKAGE_VERSION

# Push NuGet Package (Require NuGet API)
dotnet nuget push packages/EventHorizon.Blazor.Mascot.nuget --source https://api.nuget.org/v3/index.json --api-key $NUGET_API_KEY 

License

MIT License

Copyright (c) 2021 Cody Merritt Anhorn

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.2.0 579 11/20/2021
0.1.0 289 11/19/2021
0.0.1-alpha002 216 11/19/2021
0.0.1-alpha 213 11/19/2021