diff --git a/src/backend/document/canvas.go b/src/backend/document/canvas.go index ae58f49..fa3c043 100644 --- a/src/backend/document/canvas.go +++ b/src/backend/document/canvas.go @@ -29,6 +29,10 @@ func (c *canvas) IsNull() bool { return c.canvas.IsNull() } +func (c *canvas) AddEventListener(ev string, fn js.Func) { + js.Global().Get("window").Call("addEventListener", ev, fn) +} + func (c *context) Set(key string, value interface{}) { c.context.Set(key, value) } diff --git a/src/webzen/entity/entity.go b/src/webzen/entity/entity.go index 606d7f1..f2981bf 100644 --- a/src/webzen/entity/entity.go +++ b/src/webzen/entity/entity.go @@ -4,27 +4,27 @@ package entity import ( - "github.com/dimkauzh/webzen/src/webzen/image" - "github.com/dimkauzh/webzen/src/webzen/shape" + "github.com/dimkauzh/webzen/src/webzen/image" + "github.com/dimkauzh/webzen/src/webzen/shape" ) type Entity struct { - x int - y int - width int - height int - image image.Image - rect shape.Rect + x int + y int + width int + height int + image image.Image + rect shape.Rect } func NewEntity(x int, y int, width int, height int) Entity { - return Entity{x, y, width, height, nil, nil} + return Entity{x, y, width, height, nil, nil} } func (e *Entity) SetImage(image image.Image) { - e.image = image + e.image = image } func (e *Entity) SetRect(rect shape.Rect) { - e.rect = rect + e.rect = rect } diff --git a/src/webzen/ui/button.go b/src/webzen/ui/button.go new file mode 100644 index 0000000..6326e2e --- /dev/null +++ b/src/webzen/ui/button.go @@ -0,0 +1,48 @@ +//go:build js && wasm +// +build js,wasm + +package ui + +import ( + "strconv" + "syscall/js" + + "github.com/dimkauzh/webzen/src/backend/colors" + "github.com/dimkauzh/webzen/src/backend/document" +) + +var buttonClicked = false + +func NewButton(text string, textSize int, x, y, width, height float64, clickCallback func()) { + canvas := document.GetElementById("webzen") + context := canvas.GetContext("2d") + + rgba := colors.GetRGBA([4]int{146, 255, 123, 255}) + + context.Set("fillStyle", rgba) + context.FillRect(x, y, width, height) + + textX := x + width/2 - float64(textSize*len(text)/4) + textY := y + height/2 + float64(textSize)/3 + + context.Set("font", strconv.Itoa(textSize)+"px Arial") + context.Set("fillStyle", "black") + context.FillText(text, textX, textY) + + if !buttonClicked { + canvas.AddEventListener("click", js.FuncOf(func(this js.Value, p []js.Value) interface{} { + // Check if the click event occurred within the button area + mouseEvent := p[0] + mouseX := mouseEvent.Get("offsetX").Int() + mouseY := mouseEvent.Get("offsetY").Int() + + if mouseX >= int(x) && mouseX <= int(x)+int(width) && mouseY >= int(y) && mouseY <= int(y)+int(height) { + // Call the provided clickCallback function when the button area is clicked + clickCallback() + } + return nil + })) + + buttonClicked = true + } +} diff --git a/src/webzen/vector/vector.go b/src/webzen/vector/vector.go index 10fe449..227cb0d 100644 --- a/src/webzen/vector/vector.go +++ b/src/webzen/vector/vector.go @@ -4,10 +4,10 @@ package vector type Vector2D struct { - x int - y int + x int + y int } func NewVector2D(x, y int) Vector2D { - return Vector2D{x, y} + return Vector2D{x, y} } diff --git a/tests/test1/test1.go b/tests/test1/test1.go index 0f78869..778beb0 100644 --- a/tests/test1/test1.go +++ b/tests/test1/test1.go @@ -1,11 +1,11 @@ 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" + "github.com/dimkauzh/webzen/src/webzen/ui" ) func main() { @@ -15,9 +15,14 @@ func main() { 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}) + + ui.NewButton("texting rrnrrh", 10, 100, 100, 200, 100, func() { + tools.Print("Button pressed") + }) + if keys.KeyPressed("a") { tools.Print("A key pressed") } - webzen.Update() + webzen.Update() } }