Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blazor Sample App #779

Merged
merged 17 commits into from
Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Samples/BlazorSampleApp/App.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Router AppAssembly="@typeof(App).Assembly">
<Found Context="routeData">
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
<FocusOnNavigate RouteData="@routeData" Selector="h1" />
</Found>
<NotFound>
<PageTitle>Not found</PageTitle>
<LayoutView Layout="@typeof(MainLayout)">
<p role="alert">Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
18 changes: 18 additions & 0 deletions Samples/BlazorSampleApp/BlazorSampleApp.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<Folder Include="MandelbrotExplorer\" />
<Folder Include="ILGPUWebHost\" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Src\ILGPU\ILGPU.csproj" />
</ItemGroup>

</Project>
3 changes: 3 additions & 0 deletions Samples/BlazorSampleApp/Components/BasicCanvas.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@


<canvas id="@CanvasId" width="@Width" height="@Height" @ref="_canvasRef"></canvas>
246 changes: 246 additions & 0 deletions Samples/BlazorSampleApp/Components/BasicCanvas.razor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@

// ---------------------------------------------------------------------------------------
// ILGPU Samples
// Copyright (c) 2021 ILGPU Project
// www.ilgpu.net
//
// File: BasicCanvas.razor.cs
//
// This file is part of ILGPU and is distributed under the University of Illinois Open
// Source License. See LICENSE.txt for details.
// ---------------------------------------------------------------------------------------

using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;

namespace BlazorSampleApp.Components
{

#nullable disable
public partial class BasicCanvas : ComponentBase, IAsyncDisposable
{

private IJSObjectReference asyncModule = null;

private IJSInProcessObjectReference module = null;

protected IJSRuntime _jsRuntime;

protected IJSInProcessRuntime _jsInProcessRuntime = null;

public event Action<BasicCanvas> CanvasInitComplete = null;




[Parameter]
public bool IsTransparent { get; set; } = false;

[Parameter]
public bool IsDesyncronized { get; set; } = false;

[Parameter]
public int Height { get; set; } = 600;

[Parameter]
public int Width { get; set; } = 800;

[Parameter]
public bool IsFullScreen { get; set; } = false;


[Parameter]
public string CanvasId { get; set; } = Guid.NewGuid().ToString();

protected ElementReference _canvasRef;

public ElementReference CanvasReference => this._canvasRef;

public bool IsWebAssembley { get { return (_jsRuntime is IJSInProcessRuntime); } }

private bool IsDisposing = false;

[Inject]
public IJSRuntime JS_Runtime
{
get
{
return _jsRuntime;
}
set
{
_jsRuntime = value;

if (IsWebAssembley)
{
_jsInProcessRuntime = (IJSInProcessRuntime)value;

}
}
}





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

try
{
if (IsWebAssembley)
{

module = await _jsInProcessRuntime.InvokeAsync<IJSInProcessObjectReference>("import", "./Scripts/BasicCanvas.js");

module.InvokeVoid("initializeBasicCanvas", CanvasId, IsWebAssembley, IsTransparent, IsDesyncronized);
}
else
{
asyncModule = await JS_Runtime.InvokeAsync<IJSObjectReference>("import", "./Scripts/BasicCanvas.js");

await asyncModule.InvokeVoidAsync("initializeBasicCanvas", CanvasId, IsWebAssembley, IsTransparent, IsDesyncronized);

}

if (CanvasInitComplete != null)
CanvasInitComplete(this);

}
catch (Exception ex)
{
var crap = ex.Message;
}

}

}

public async ValueTask DisposeAsync()
{
IsDisposing = true;

if (asyncModule != null)
{
await asyncModule.DisposeAsync();
}
module?.Dispose();
}

public async ValueTask InjectScript(string scriptText)
{
if (IsDisposing) return;

if (module != null)
{
module.InvokeVoid("InjectScript", scriptText);
}
else
{
await asyncModule.InvokeVoidAsync("InjectScript", scriptText);
}
}

#nullable enable
public async ValueTask SetValueBasicContext(string ValueName, params object?[]? args)
{
#nullable disable
if (IsDisposing) return;

if (module != null)
{
module.InvokeVoid("setValueBasicContext", CanvasReference, ValueName, args);
}
else
{
await asyncModule.InvokeVoidAsync("setValueBasicContext", CanvasReference, ValueName, args);
}
}


#nullable enable
public async ValueTask<T> GetValueBasicContext<T>(string ValueName, params object?[]? args)
{
#nullable disable
if (IsDisposing) return default(T);

if (module != null)
{
return module.Invoke<T>("getValueBasicContext", CanvasReference, ValueName, args);
}
else
{
return await asyncModule.InvokeAsync<T>("getValueBasicContext", CanvasReference, ValueName, args);
}
}


#nullable enable
public async ValueTask SetFunctionBasicContext(string FunctionName, params object?[]? args)
{
#nullable disable
if (IsDisposing) return;

if (module != null)
{
module.InvokeVoid("setFunctionBasicContext", CanvasReference, FunctionName, args);
}
else
{
await asyncModule.InvokeVoidAsync("setFunctionBasicContext", CanvasReference, FunctionName, args);
}
}

#nullable enable
public async ValueTask<T> GetFunctionBasicContext<T>(string FunctionName, params object?[]? args)
{
#nullable disable
if (IsDisposing) return default(T);

if (module != null)
{
return module.Invoke<T>("getFunctionBasicContext", CanvasReference, FunctionName, args);
}
else
{
return await asyncModule.InvokeAsync<T>("getFunctionBasicContext", CanvasReference, FunctionName, args);
}
}

#nullable enable
public async ValueTask SetFunctionDrawingBasis(string FunctionName, params object?[]? args)
{
#nullable disable
if (IsDisposing) return;

if (module != null)
{
module.InvokeVoid("setFunctionDrawingBasis", CanvasReference, FunctionName, args);
}
else
{
await asyncModule.InvokeVoidAsync("setFunctionDrawingBasis", CanvasReference, FunctionName, args);
}
}

#nullable enable
public async ValueTask<T> GetFunctionDrawingBasis<T>(string FunctionName, params object?[]? args)
{
#nullable disable
if (IsDisposing) return default(T);

if (module != null)
{
return module.Invoke<T>("getFunctionDrawingBasis", CanvasReference, FunctionName, args);
}
else
{
return await asyncModule.InvokeAsync<T>("getFunctionDrawingBasis", CanvasReference, FunctionName, args);
}
}

}
}
3 changes: 3 additions & 0 deletions Samples/BlazorSampleApp/Components/BasicCanvas.razor.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
canvas {
border: 5px dotted blue;
}
27 changes: 27 additions & 0 deletions Samples/BlazorSampleApp/Components/BasicCanvasEnums.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// ---------------------------------------------------------------------------------------
// ILGPU Samples
// Copyright (c) 2021 ILGPU Project
// www.ilgpu.net
//
// File: BasicCanvasEnums.cs
//
// This file is part of ILGPU and is distributed under the University of Illinois Open
// Source License. See LICENSE.txt for details.
// ---------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BlazorSampleApp.Components
{
public enum CanvasLineCap { butt, round, square };
public enum CanvasLineJoin { round, bevel, miter };
public enum CanvasTextAlign { start, end, left, right, center };
public enum CanvasTextBaseline { top, hanging, middle, alphabetic, ideographic, bottom };
public enum CanvasDirection { ltr, rtl, inherit };


}
Loading