From 49f1cd79b7eb17604c03186aec0b4a071dcf0829 Mon Sep 17 00:00:00 2001 From: Dima Marhitych Date: Sun, 8 Oct 2023 12:11:40 +0200 Subject: [PATCH] v0.0.7: Added keyboard system --- src/backend/document/document.go | 4 ++++ src/webzen/init.go | 9 ++++++++ src/webzen/keys/keys.go | 39 ++++++++++++++++++++++++++++++++ tests/test1/test1.go | 17 ++++++++++---- 4 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 src/webzen/keys/keys.go diff --git a/src/backend/document/document.go b/src/backend/document/document.go index 981829f..c541147 100644 --- a/src/backend/document/document.go +++ b/src/backend/document/document.go @@ -34,6 +34,10 @@ func CreateCanvasElement() canvas { return canvas{cd} } +func AddEventListener(event string, f js.Func) { + js.Global().Get("document").Call("addEventListener", event, f) +} + func (e *element) Set(key string, value interface{}) { e.el.Set(key, value) } diff --git a/src/webzen/init.go b/src/webzen/init.go index 40adff0..05a5c94 100644 --- a/src/webzen/init.go +++ b/src/webzen/init.go @@ -5,9 +5,11 @@ package webzen import ( "syscall/js" + "time" "github.com/dimkauzh/webzen/src/backend/document" "github.com/dimkauzh/webzen/src/backend/window" + "github.com/dimkauzh/webzen/src/webzen/keys" ) func Init() { @@ -29,4 +31,11 @@ func Init() { canvas.Set("height", document.DocumentElement.ClientHeight()) return nil })) + + keys.SetupEventListeners() +} + +func Update() { + time.Sleep(time.Millisecond * 16) + } diff --git a/src/webzen/keys/keys.go b/src/webzen/keys/keys.go new file mode 100644 index 0000000..06aef74 --- /dev/null +++ b/src/webzen/keys/keys.go @@ -0,0 +1,39 @@ +//go:build js && wasm +// +build js,wasm + +package keys + +import ( + "syscall/js" + + "github.com/dimkauzh/webzen/src/backend/document" +) + +var keyPressed = make(map[string]bool) + +func SetupEventListeners() { + // Initialize event listeners once + document.AddEventListener("keydown", js.FuncOf(func(this js.Value, p []js.Value) interface{} { + event := p[0] + key := event.Get("key").String() + + // Set the key's state to pressed. + keyPressed[key] = true + + return nil + })) + + document.AddEventListener("keyup", js.FuncOf(func(this js.Value, p []js.Value) interface{} { + event := p[0] + key := event.Get("key").String() + + // Set the key's state to released. + keyPressed[key] = false + + return nil + })) +} + +func KeyPressed(key string) bool { + return keyPressed[key] +} diff --git a/tests/test1/test1.go b/tests/test1/test1.go index ba4a8a3..0f78869 100644 --- a/tests/test1/test1.go +++ b/tests/test1/test1.go @@ -1,14 +1,23 @@ package main import ( + "github.com/dimkauzh/webzen/src/webzen" "github.com/dimkauzh/webzen/src/webzen/draw" + "github.com/dimkauzh/webzen/src/webzen/keys" + "github.com/dimkauzh/webzen/src/webzen/tools" ) func main() { webzen.Init() - draw.FillBackground([4]int{255, 255, 188, 255}) - draw.DrawText("Hello under the world!", 21, 100, 100) - draw.DrawRect(50, 500, 400, 400, [4]int{146, 255, 123, 255}) - draw.DrawRect(200, 200, 100, 400, [4]int{146, 255, 123, 255}) + for { + draw.FillBackground([4]int{255, 255, 188, 255}) + draw.DrawText("Hello under the world!", 21, 100, 100) + draw.DrawRect(50, 500, 400, 400, [4]int{146, 255, 123, 255}) + draw.DrawRect(200, 200, 100, 400, [4]int{146, 255, 123, 255}) + if keys.KeyPressed("a") { + tools.Print("A key pressed") + } + webzen.Update() + } }