Implement IsMouseButtonPressed / CursorPosition in JS (#53)

This commit is contained in:
Hajime Hoshi 2015-01-06 23:41:03 +09:00
parent 5fc8e624f5
commit 0ab90197a3
4 changed files with 62 additions and 5 deletions

View File

@ -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) {

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}