-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathFlipViewer.razor
More file actions
118 lines (99 loc) · 3.73 KB
/
FlipViewer.razor
File metadata and controls
118 lines (99 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
@using Microsoft.JSInterop
@using System.Text.Json.Serialization
@inject IJSRuntime JSRuntime
@namespace SeeSharp.Blazor
@if (flipCode != null)
@((MarkupString)flipCode)
@code {
string flipCode;
string flipJson;
[Parameter]
public SimpleImageIO.FlipBook Flip { get; set; }
SimpleImageIO.FlipBook lastFlip;
public record struct OnEventArgs
(
int mouseButton,
// Mouse X/Y pixel coordinates relative to an image in Flipbook (Pixel (0, 0) at top left of image)
int mouseX,
int mouseY,
int deltaY,
string FlipbookID,
int selectedIndex,
HashSet<String> keysPressed
)
{
}
[Parameter]
public EventCallback<OnEventArgs> OnClick { get; set; }
[Parameter]
public EventCallback<OnEventArgs> OnWheel { get; set; }
[Parameter]
public EventCallback<OnEventArgs> OnMouseOver { get; set; }
[Parameter]
public EventCallback<OnEventArgs> OnKey { get; set; }
protected override async Task OnParametersSetAsync()
{
if (Flip == null)
{
flipCode = null;
lastFlip = null;
return;
}
if (lastFlip == Flip) return;
await Task.Run(() => {
if (Flip.Count == 0) {
flipJson = null;
flipCode = "<p>empty flip book</p>";
return;
}
var code = Flip.Generate();
flipCode = code.Html;
flipJson = code.Data;
});
lastFlip = Flip;
}
[JSInvokable]
public void _OnFlipClick(int mouseButton, int mouseX, int mouseY, string ID, int selectedIdx, String[] keysPressed)
{
HashSet<string> set = new HashSet<string>(keysPressed);
OnClick.InvokeAsync(new(mouseButton: mouseButton, mouseX: mouseX, mouseY: mouseY, deltaY: -1, FlipbookID: ID, selectedIndex: selectedIdx, keysPressed: set)).Wait();
}
[JSInvokable]
public void _OnFlipWheel(int mouseX, int mouseY, int deltaY, string ID, int selectedIdx, String[] keysPressed)
{
HashSet<string> set = new HashSet<string>(keysPressed);
OnWheel.InvokeAsync(new(mouseButton: -1, mouseX: mouseX, mouseY: mouseY, deltaY: deltaY, FlipbookID: ID, selectedIndex: selectedIdx, keysPressed: set)).Wait();
}
[JSInvokable]
public void _OnFlipMouseOver(int mouseX, int mouseY, string ID, int selectedIdx, String[] keysPressed)
{
HashSet<string> set = new HashSet<string>(keysPressed);
OnMouseOver.InvokeAsync(new(mouseButton: -1, mouseX: mouseX, mouseY: mouseY, deltaY: -1, FlipbookID: ID, selectedIndex: selectedIdx, keysPressed: set)).Wait();
}
[JSInvokable]
public void _OnFlipKey(int mouseX, int mouseY, string ID, int selectedIdx, String[] keysPressed)
{
HashSet<string> set = new HashSet<string>(keysPressed);
OnKey.InvokeAsync(new(mouseButton: -1, mouseX: mouseX, mouseY: mouseY, deltaY: -1, FlipbookID: ID, selectedIndex: selectedIdx, keysPressed: set)).Wait();
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
// Need to wait with invoking the JS code until the HTML got added to the DOM on the client side
if (flipJson != null)
{
await JSRuntime.InvokeVoidAsync(
"makeFlipBook",
flipJson,
DotNetObjectReference.Create(this),
nameof(_OnFlipClick),
DotNetObjectReference.Create(this),
nameof(_OnFlipWheel),
DotNetObjectReference.Create(this),
nameof(_OnFlipMouseOver),
DotNetObjectReference.Create(this),
nameof(_OnFlipKey)
);
flipJson = null;
}
}
}