Skip to content

Commit 975e97e

Browse files
committed
update flipbook scripts
1 parent e15c839 commit 975e97e

2 files changed

Lines changed: 90 additions & 19 deletions

File tree

SeeSharp.Blazor/FlipViewer.razor

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,30 @@
1515
public SimpleImageIO.FlipBook Flip { get; set; }
1616
SimpleImageIO.FlipBook lastFlip;
1717

18-
public record struct OnClickEventArgs
18+
public record struct OnEventArgs
1919
(
20-
int X,
21-
int Y,
22-
bool CtrlKey
20+
int mouseButton,
21+
// Mouse X/Y pixel coordinates relative to an image in Flipbook (Pixel (0, 0) at top left of image)
22+
int mouseX,
23+
int mouseY,
24+
int deltaY,
25+
26+
string FlipbookID,
27+
int selectedIndex,
28+
29+
HashSet<String> keysPressed
2330
)
2431
{
2532
}
2633

2734
[Parameter]
28-
public EventCallback<OnClickEventArgs> OnClick { get; set; }
35+
public EventCallback<OnEventArgs> OnClick { get; set; }
36+
[Parameter]
37+
public EventCallback<OnEventArgs> OnWheel { get; set; }
38+
[Parameter]
39+
public EventCallback<OnEventArgs> OnMouseOver { get; set; }
40+
[Parameter]
41+
public EventCallback<OnEventArgs> OnKey { get; set; }
2942

3043
protected override async Task OnParametersSetAsync()
3144
{
@@ -52,23 +65,53 @@
5265
lastFlip = Flip;
5366
}
5467

55-
public struct _OnFlipClickArgs
68+
[JSInvokable]
69+
public void _OnFlipClick(int mouseButton, int mouseX, int mouseY, string ID, int selectedIdx, String[] keysPressed)
70+
{
71+
HashSet<string> set = new HashSet<string>(keysPressed);
72+
OnClick.InvokeAsync(new(mouseButton: mouseButton, mouseX: mouseX, mouseY: mouseY, deltaY: -1, FlipbookID: ID, selectedIndex: selectedIdx, keysPressed: set)).Wait();
73+
}
74+
75+
[JSInvokable]
76+
77+
public void _OnFlipWheel(int mouseX, int mouseY, int deltaY, string ID, int selectedIdx, String[] keysPressed)
78+
{
79+
HashSet<string> set = new HashSet<string>(keysPressed);
80+
OnWheel.InvokeAsync(new(mouseButton: -1, mouseX: mouseX, mouseY: mouseY, deltaY: deltaY, FlipbookID: ID, selectedIndex: selectedIdx, keysPressed: set)).Wait();
81+
}
82+
83+
[JSInvokable]
84+
public void _OnFlipMouseOver(int mouseX, int mouseY, string ID, int selectedIdx, String[] keysPressed)
5685
{
57-
[JsonInclude] public bool ctrlKey;
86+
HashSet<string> set = new HashSet<string>(keysPressed);
87+
OnMouseOver.InvokeAsync(new(mouseButton: -1, mouseX: mouseX, mouseY: mouseY, deltaY: -1, FlipbookID: ID, selectedIndex: selectedIdx, keysPressed: set)).Wait();
5888
}
5989

6090
[JSInvokable]
61-
public void _OnFlipClick(int x, int y, _OnFlipClickArgs eventArgs)
91+
92+
public void _OnFlipKey(int mouseX, int mouseY, string ID, int selectedIdx, String[] keysPressed)
6293
{
63-
OnClick.InvokeAsync(new(x, y, eventArgs.ctrlKey)).Wait();
94+
HashSet<string> set = new HashSet<string>(keysPressed);
95+
OnKey.InvokeAsync(new(mouseButton: -1, mouseX: mouseX, mouseY: mouseY, deltaY: -1, FlipbookID: ID, selectedIndex: selectedIdx, keysPressed: set)).Wait();
6496
}
6597

6698
protected override async Task OnAfterRenderAsync(bool firstRender)
6799
{
68100
// Need to wait with invoking the JS code until the HTML got added to the DOM on the client side
69101
if (flipJson != null)
70102
{
71-
await JSRuntime.InvokeVoidAsync("makeFlipBook", flipJson, DotNetObjectReference.Create(this), nameof(_OnFlipClick));
103+
await JSRuntime.InvokeVoidAsync(
104+
"makeFlipBook",
105+
flipJson,
106+
DotNetObjectReference.Create(this),
107+
nameof(_OnFlipClick),
108+
DotNetObjectReference.Create(this),
109+
nameof(_OnFlipWheel),
110+
DotNetObjectReference.Create(this),
111+
nameof(_OnFlipMouseOver),
112+
DotNetObjectReference.Create(this),
113+
nameof(_OnFlipKey)
114+
);
72115
flipJson = null;
73116
}
74117
}

SeeSharp.Blazor/Scripts.cs

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1+
using Microsoft.AspNetCore.StaticAssets;
12
using Microsoft.JSInterop;
3+
using System.Net.Http.Headers;
24
using System.Reflection;
35

46
namespace SeeSharp.Blazor;
57

6-
public static class Scripts
7-
{
8-
static string ReadResourceText(string filename)
9-
{
8+
public static class Scripts {
9+
static string ReadResourceText(string filename) {
1010
var assembly = typeof(Scripts).GetTypeInfo().Assembly;
1111
var stream = assembly.GetManifestResourceStream("SeeSharp.Blazor." + filename)
1212
?? throw new FileNotFoundException("resource file not found", filename);
@@ -20,14 +20,32 @@ static string ReadResourceText(string filename)
2020
$$"""
2121
<script>
2222
{{SimpleImageIO.FlipBook.HeaderScript}}
23-
function makeFlipBook(jsonArgs, onClickObj, onClickMethodName) {
23+
function makeFlipBook(jsonArgs, onClickObj, onClickMethodName, onWheelObj, onWheelMethodName, onMouseOverObj, onMouseOverMethodName, onKeyObj, onKeyMethodName) {
2424
let onClick = null;
2525
if (onClickObj && onClickMethodName) {
26-
onClick = (col, row, evt) =>
27-
onClickObj.invokeMethodAsync(onClickMethodName, col, row, { ctrlKey: evt.ctrlKey })
26+
onClick = (mouseButton, mouseX, mouseY, ID, selectedIdx, keysPressed) =>
27+
onClickObj.invokeMethodAsync(onClickMethodName, mouseButton, mouseX, mouseY, ID, selectedIdx, keysPressed)
2828
}
2929
30-
window['flipbook']['MakeFlipBook'](jsonArgs, onClick);
30+
let onWheel = null
31+
if (onWheelObj && onWheelMethodName) {
32+
onWheel = (mouseX, mouseY, deltaY, ID, selectedIdx, keysPressed) =>
33+
onWheelObj.invokeMethodAsync(onWheelMethodName, mouseX, mouseY, deltaY, ID, selectedIdx, keysPressed)
34+
}
35+
36+
let onMouseOver = null
37+
if (onMouseOverObj && onMouseOverMethodName) {
38+
onMouseOver = (mouseX, mouseY, ID, selectedIdx, keysPressed) =>
39+
onMouseOverObj.invokeMethodAsync(onMouseOverMethodName, mouseX, mouseY, ID, selectedIdx, keysPressed)
40+
}
41+
42+
let onKey = null
43+
if (onKeyObj && onKeyMethodName) {
44+
onKey = (mouseX, mouseY, ID, selectedIdx, keysPressed) =>
45+
onKeyObj.invokeMethodAsync(onKeyMethodName, mouseX, mouseY, ID, selectedIdx, keysPressed)
46+
}
47+
48+
window['flipbook']['MakeFlipBook'](jsonArgs, onClick, onWheel, onMouseOver, onKey);
3149
}
3250
</script>
3351
""";
@@ -59,7 +77,17 @@ function makeFlipBook(jsonArgs, onClickObj, onClickMethodName) {
5977
</script>
6078
""";
6179

62-
public static readonly string AllScripts = FlipBookScript + DownloadScript + WidgetScripts;
80+
public static readonly string UpdateImageScript =
81+
$$"""
82+
<script>
83+
function updateImage(jsonArgs) {
84+
window['flipbook']['UpdateImage'](jsonArgs);
85+
return;
86+
}
87+
</script>
88+
""";
89+
90+
public static readonly string AllScripts = FlipBookScript + DownloadScript + WidgetScripts + UpdateImageScript;
6391

6492
/// <summary>
6593
/// Downloads a stream to the client with the given file name. Requires that <see cref="DownloadScript" />

0 commit comments

Comments
 (0)