mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-26 03:38:55 +01:00
Add input.Mouse
This commit is contained in:
parent
eb8c592269
commit
5fc5acd3d9
@ -46,7 +46,7 @@ or without modification, either commercially and noncommercially.
|
||||
THESE FONTS ARE PROVIDED "AS IS" WITHOUT WARRANTY.
|
||||
```
|
||||
|
||||
## other files
|
||||
## Other files
|
||||
|
||||
```
|
||||
Copyright 2014 Hajime Hoshi
|
||||
|
@ -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_MIN_FILTER, glFilter)
|
||||
|
||||
ptr := interface{}(nil)
|
||||
if pixels != nil {
|
||||
ptr = pixels
|
||||
}
|
||||
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, textureWidth, textureHeight, 0, gl.RGBA, gl.UNSIGNED_BYTE, ptr)
|
||||
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, textureWidth, textureHeight, 0, gl.RGBA, gl.UNSIGNED_BYTE, pixels)
|
||||
|
||||
return nativeTexture
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package input
|
||||
|
||||
type Key int
|
||||
|
||||
// TODO: Add more keys.
|
||||
const (
|
||||
KeyUp Key = iota
|
||||
KeyDown
|
||||
@ -23,7 +24,7 @@ func SetKeyboard(keyboard Keyboard) {
|
||||
|
||||
func IsKeyPressed(key Key) bool {
|
||||
if currentKeyboard == nil {
|
||||
panic("ui.IsKeyPressed: currentKeyboard is not set")
|
||||
panic("input.IsKeyPressed: currentKeyboard is not set")
|
||||
}
|
||||
return currentKeyboard.IsKeyPressed(key)
|
||||
}
|
||||
|
35
input/mouse.go
Normal file
35
input/mouse.go
Normal 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)
|
||||
}
|
@ -13,6 +13,7 @@ type canvas struct {
|
||||
window *glfw.Window
|
||||
contextUpdater *opengl.ContextUpdater
|
||||
keyboard keyboard
|
||||
mouse mouse
|
||||
funcs chan func()
|
||||
funcsDone chan struct{}
|
||||
}
|
||||
@ -67,4 +68,5 @@ func (c *canvas) use(f func()) {
|
||||
|
||||
func (c *canvas) update() {
|
||||
c.keyboard.update(c.window)
|
||||
c.mouse.update(c.window)
|
||||
}
|
||||
|
30
ui/glfw/mouse.go
Normal file
30
ui/glfw/mouse.go
Normal 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
|
||||
}
|
||||
}
|
@ -36,6 +36,7 @@ func (u *UI) Start(width, height, scale int, title string) (ui.Canvas, error) {
|
||||
funcsDone: make(chan struct{}),
|
||||
}
|
||||
input.SetKeyboard(&c.keyboard)
|
||||
input.SetMouse(&c.mouse)
|
||||
graphics.SetTextureFactory(c)
|
||||
|
||||
c.run(width, height, scale)
|
||||
|
Loading…
Reference in New Issue
Block a user