From 0ab90197a372bd49181affe9f4a15a8deebbdd41 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Tue, 6 Jan 2015 23:41:03 +0900 Subject: [PATCH] Implement IsMouseButtonPressed / CursorPosition in JS (#53) --- ebitenutil/debugprint.go | 4 ++-- example/paint/main.go | 12 +++++++++--- internal/ui/input_js.go | 28 ++++++++++++++++++++++++++++ internal/ui/ui_js.go | 23 +++++++++++++++++++++++ 4 files changed, 62 insertions(+), 5 deletions(-) diff --git a/ebitenutil/debugprint.go b/ebitenutil/debugprint.go index 7ad3b0d1b..8215aeb11 100644 --- a/ebitenutil/debugprint.go +++ b/ebitenutil/debugprint.go @@ -59,8 +59,8 @@ type debugPrintState struct { var defaultDebugPrintState = &debugPrintState{} -func DebugPrint(r *ebiten.Image, str string) { - defaultDebugPrintState.DebugPrint(r, str) +func DebugPrint(r *ebiten.Image, str string) error { + return defaultDebugPrintState.DebugPrint(r, str) } func (d *debugPrintState) drawText(rt *ebiten.Image, str string, x, y int, c color.Color) { diff --git a/example/paint/main.go b/example/paint/main.go index 53d2520c9..f9e471b14 100644 --- a/example/paint/main.go +++ b/example/paint/main.go @@ -47,12 +47,18 @@ func Update(screen *ebiten.Image) error { op.ColorM.Scale(1.0, 0.25, 0.25, 1.0) theta := 2.0 * math.Pi * float64(count%60) / 60.0 op.ColorM.Concat(ebiten.RotateHue(theta)) - canvasRenderTarget.DrawImage(brushRenderTarget, op) + if err := canvasRenderTarget.DrawImage(brushRenderTarget, op); err != nil { + return err + } } - screen.DrawImage(canvasRenderTarget, nil) + if err := screen.DrawImage(canvasRenderTarget, nil); err != nil { + return err + } - ebitenutil.DebugPrint(screen, fmt.Sprintf("(%d, %d)", mx, my)) + if err := ebitenutil.DebugPrint(screen, fmt.Sprintf("(%d, %d)", mx, my)); err != nil { + return err + } return nil } diff --git a/internal/ui/input_js.go b/internal/ui/input_js.go index 7acce39ae..aaccef39a 100644 --- a/internal/ui/input_js.go +++ b/internal/ui/input_js.go @@ -47,3 +47,31 @@ func (i *input) keyUp(key int) { k := keyCodeToKey[key] i.keyPressed[k] = false } + +func (i *input) mouseDown(button int) { + p := &i.mouseButtonPressed + switch button { + case 0: + p[MouseButtonLeft] = true + case 1: + p[MouseButtonMiddle] = true + case 2: + p[MouseButtonRight] = true + } +} + +func (i *input) mouseUp(button int) { + p := &i.mouseButtonPressed + switch button { + case 0: + p[MouseButtonLeft] = false + case 1: + p[MouseButtonMiddle] = false + case 2: + p[MouseButtonRight] = false + } +} + +func (i *input) mouseMove(x, y int) { + i.cursorX, i.cursorY = x, y +} diff --git a/internal/ui/ui_js.go b/internal/ui/ui_js.go index e6adec405..a8ac04387 100644 --- a/internal/ui/ui_js.go +++ b/internal/ui/ui_js.go @@ -98,6 +98,19 @@ func init() { code := e.Get("keyCode").Int() currentInput.keyUp(code) }) + canvas.Set("onmousedown", func(e js.Object) { + defer e.Call("preventDefault") + button := e.Get("button").Int() + currentInput.mouseDown(button) + }) + canvas.Set("onmouseup", func(e js.Object) { + defer e.Call("preventDefault") + button := e.Get("button").Int() + currentInput.mouseUp(button) + }) + canvas.Set("oncontextmenu", func(e js.Object) { + defer e.Call("preventDefault") + }) } func Start(width, height, scale int, title string) (actualScale int, err error) { @@ -108,5 +121,15 @@ func Start(width, height, scale int, title string) (actualScale int, err error) canvasStyle := canvas.Get("style") canvasStyle.Set("left", "calc(50% - "+strconv.Itoa(width*scale/2)+"px)") canvasStyle.Set("top", "calc(50% - "+strconv.Itoa(height*scale/2)+"px)") + + canvas.Set("onmousemove", func(e js.Object) { + defer e.Call("preventDefault") + rect := canvas.Call("getBoundingClientRect") + x, y := e.Get("clientX").Int(), e.Get("clientY").Int() + x -= rect.Get("left").Int() + y -= rect.Get("top").Int() + currentInput.mouseMove(x/scale, y/scale) + }) + return scale, nil }