Add input.Mouse

This commit is contained in:
Hajime Hoshi 2014-12-08 22:51:40 +09:00
parent eb8c592269
commit 5fc5acd3d9
7 changed files with 72 additions and 7 deletions

View File

@ -46,7 +46,7 @@ or without modification, either commercially and noncommercially.
THESE FONTS ARE PROVIDED "AS IS" WITHOUT WARRANTY. THESE FONTS ARE PROVIDED "AS IS" WITHOUT WARRANTY.
``` ```
## other files ## Other files
``` ```
Copyright 2014 Hajime Hoshi Copyright 2014 Hajime Hoshi

View File

@ -57,11 +57,7 @@ func createNativeTexture(textureWidth, textureHeight int, pixels []uint8, filter
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, glFilter) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, glFilter)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, glFilter) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, glFilter)
ptr := interface{}(nil) gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, textureWidth, textureHeight, 0, gl.RGBA, gl.UNSIGNED_BYTE, pixels)
if pixels != nil {
ptr = pixels
}
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, textureWidth, textureHeight, 0, gl.RGBA, gl.UNSIGNED_BYTE, ptr)
return nativeTexture return nativeTexture
} }

View File

@ -2,6 +2,7 @@ package input
type Key int type Key int
// TODO: Add more keys.
const ( const (
KeyUp Key = iota KeyUp Key = iota
KeyDown KeyDown
@ -23,7 +24,7 @@ func SetKeyboard(keyboard Keyboard) {
func IsKeyPressed(key Key) bool { func IsKeyPressed(key Key) bool {
if currentKeyboard == nil { if currentKeyboard == nil {
panic("ui.IsKeyPressed: currentKeyboard is not set") panic("input.IsKeyPressed: currentKeyboard is not set")
} }
return currentKeyboard.IsKeyPressed(key) return currentKeyboard.IsKeyPressed(key)
} }

35
input/mouse.go Normal file
View File

@ -0,0 +1,35 @@
package input
type MouseButton int
const (
MouseButtonLeft MouseButton = iota
MouseButtonRight
MouseButtonMiddle
MouseButtonMax
)
var currentMouse Mouse
type Mouse interface {
CurrentPosition() (x, y int)
IsMouseButtonPressed(mouseButton MouseButton) bool
}
func SetMouse(mouse Mouse) {
currentMouse = mouse
}
func CurrentPosition() (x, y int) {
if currentMouse == nil {
panic("input.CurrentPosition: currentMouse is not set")
}
return currentMouse.CurrentPosition()
}
func IsMouseButtonPressed(button MouseButton) bool {
if currentMouse == nil {
panic("input.IsMouseButtonPressed: currentMouse is not set")
}
return currentMouse.IsMouseButtonPressed(button)
}

View File

@ -13,6 +13,7 @@ type canvas struct {
window *glfw.Window window *glfw.Window
contextUpdater *opengl.ContextUpdater contextUpdater *opengl.ContextUpdater
keyboard keyboard keyboard keyboard
mouse mouse
funcs chan func() funcs chan func()
funcsDone chan struct{} funcsDone chan struct{}
} }
@ -67,4 +68,5 @@ func (c *canvas) use(f func()) {
func (c *canvas) update() { func (c *canvas) update() {
c.keyboard.update(c.window) c.keyboard.update(c.window)
c.mouse.update(c.window)
} }

30
ui/glfw/mouse.go Normal file
View File

@ -0,0 +1,30 @@
package glfw
import (
glfw "github.com/go-gl/glfw3"
"github.com/hajimehoshi/ebiten/input"
"math"
)
type mouse struct {
buttonPressed [input.MouseButtonMax]bool
x int
y int
}
func (m *mouse) CurrentPosition() (x, y int) {
return m.x, m.y
}
func (m *mouse) IsMouseButtonPressed(button input.MouseButton) bool {
return m.buttonPressed[button]
}
func (m *mouse) update(window *glfw.Window) {
x, y := window.GetCursorPosition()
m.x = int(math.Floor(x))
m.y = int(math.Floor(y))
for i := input.MouseButtonLeft; i < input.MouseButtonMax; i++ {
m.buttonPressed[i] = window.GetMouseButton(glfw.MouseButton(i)) == glfw.Press
}
}

View File

@ -36,6 +36,7 @@ func (u *UI) Start(width, height, scale int, title string) (ui.Canvas, error) {
funcsDone: make(chan struct{}), funcsDone: make(chan struct{}),
} }
input.SetKeyboard(&c.keyboard) input.SetKeyboard(&c.keyboard)
input.SetMouse(&c.mouse)
graphics.SetTextureFactory(c) graphics.SetTextureFactory(c)
c.run(width, height, scale) c.run(width, height, scale)