Skip to content

Commit

Permalink
v0.0.7: Added keyboard system
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkauzh committed Oct 8, 2023
1 parent 2d2c287 commit 49f1cd7
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/backend/document/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
9 changes: 9 additions & 0 deletions src/webzen/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -29,4 +31,11 @@ func Init() {
canvas.Set("height", document.DocumentElement.ClientHeight())
return nil
}))

keys.SetupEventListeners()
}

func Update() {
time.Sleep(time.Millisecond * 16)

}
39 changes: 39 additions & 0 deletions src/webzen/keys/keys.go
Original file line number Diff line number Diff line change
@@ -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]
}
17 changes: 13 additions & 4 deletions tests/test1/test1.go
Original file line number Diff line number Diff line change
@@ -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()
}
}

0 comments on commit 49f1cd7

Please sign in to comment.