Skip to content

Commit

Permalink
Adding keyboard
Browse files Browse the repository at this point in the history
  • Loading branch information
xafero committed Sep 2, 2023
1 parent fe029eb commit 069c9aa
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 3 deletions.
35 changes: 34 additions & 1 deletion try/Digger.Demo/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<SKGLView Width="1024px" Height="1024px" @ref="_form" />

@code {
private WebDigger _web;
private static WebDigger _web;
private SKGLView _form;
private Dig _game;
private Timer _timer;
Expand Down Expand Up @@ -40,4 +40,37 @@
_timer.Enabled = false;
await _web._digger.RunAsync();
}

[JSInvokable]
public static void OnKeyUp(KeyboardEventArgs e)
{
var code = TranslateKey(e.Key);
if (code == null) return;
_web.KeyUp(code.Value);
}

[JSInvokable]
public static void OnKeyDown(KeyboardEventArgs e)
{
var code = TranslateKey(e.Key);
if (code == null) return;
_web.KeyDown(code.Value);
}

private static int? TranslateKey(string key)
{
switch (key)
{
case "ArrowLeft": return AppletCompat.Key_Left;
case "ArrowRight": return AppletCompat.Key_Right;
case "ArrowUp": return AppletCompat.Key_Up;
case "ArrowDown": return AppletCompat.Key_Down;
case "Control": return AppletCompat.Key_F1;
case "Escape": return AppletCompat.Key_F10;
case "+": return AppletCompat.Key_Plus;
case "-": return AppletCompat.Key_Minus;
}
if (key.Length == 1) return key[0];
return null;
}
}
4 changes: 3 additions & 1 deletion try/Digger.Demo/wwwroot/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
<meta charset="utf-8" />
<title>Digger Remastered</title>
<base href="/" />
<link href="css/app.css" rel="stylesheet" />
<link href="css/app.css" rel="stylesheet" />

<link rel="apple-touch-icon" sizes="180x180" href="./apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="./favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="./favicon-16x16.png">
<link rel="manifest" href="./site.webmanifest">

<script src="js/app.js"></script>
</head>

<body>
Expand Down
29 changes: 29 additions & 0 deletions try/Digger.Demo/wwwroot/js/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

function serializeEvent(e) {
if (e) {
var o = {
altKey: e.altKey,
charCode: e.charCode,
code: e.code,
composed: e.composed,
ctrlKey: e.ctrlKey,
metaKey: e.metaKey,
key: e.key,
keyCode: e.keyCode,
repeat: e.repeat,
shiftKey: e.shiftKey,
timeStamp: e.timeStamp,
type: e.type,
which: e.which
};
return o;
}
};

document.addEventListener('keydown', function (e) {
DotNet.invokeMethodAsync('Digger.Demo', 'OnKeyDown', serializeEvent(e));
});

document.addEventListener('keyup', function (e) {
DotNet.invokeMethodAsync('Digger.Demo', 'OnKeyUp', serializeEvent(e));
});
1 change: 0 additions & 1 deletion tst/Digger.Tests/ScoreTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.IO;
using DiggerAPI;
using DiggerClassic.Score;
Expand Down

0 comments on commit 069c9aa

Please sign in to comment.