Bug fix: input

This commit is contained in:
Hajime Hoshi 2013-07-05 21:39:50 +09:00
parent 572f4057a8
commit 50f335d0e9

View File

@ -26,11 +26,13 @@ package main
// #include <GLUT/glut.h> // #include <GLUT/glut.h>
// //
// void display(void); // void display(void);
// void mouse(int button, int state, int x, int y);
// void motion(int x, int y); // void motion(int x, int y);
// void idle(void); // void idle(void);
// //
// static void setGlutFuncs(void) { // static void setGlutFuncs(void) {
// glutDisplayFunc(display); // glutDisplayFunc(display);
// glutMouseFunc(mouse);
// glutMotionFunc(motion); // glutMotionFunc(motion);
// glutIdleFunc(idle); // glutIdleFunc(idle);
// } // }
@ -53,6 +55,7 @@ import (
) )
type GlutInputEvent struct { type GlutInputEvent struct {
IsActive bool
X int X int
Y int Y int
} }
@ -73,9 +76,18 @@ func display() {
C.glutSwapBuffers() C.glutSwapBuffers()
} }
//export mouse
func mouse(button, state, x, y C.int) {
event := GlutInputEvent{false, -1, -1}
if state == C.GLUT_UP {
currentUI.glutInputting <- event
}
}
//export motion //export motion
func motion(x, y C.int) { func motion(x, y C.int) {
currentUI.glutInputting <- GlutInputEvent{ currentUI.glutInputting <- GlutInputEvent{
IsActive: true,
X: int(x), X: int(x),
Y: int(y), Y: int(y),
} }
@ -173,21 +185,29 @@ func main() {
ch := currentUI.glutInputting ch := currentUI.glutInputting
for { for {
event := <-ch event := <-ch
x := event.X / screenScale if event.IsActive {
y := event.Y / screenScale x := event.X / screenScale
if x < 0 { y := event.Y / screenScale
x = 0 if x < 0 {
} else if screenWidth <= x { x = 0
x = screenWidth - 1 } else if screenWidth <= x {
} x = screenWidth - 1
if y < 0 { }
y = 0 if y < 0 {
} else if screenHeight <= y { y = 0
y = screenHeight - 1 } else if screenHeight <= y {
} y = screenHeight - 1
input <- ebiten.InputState{ }
X: x, input <- ebiten.InputState{
Y: y, X: x,
Y: y,
}
} else {
input <- ebiten.InputState{
X: -1,
Y: -1,
}
} }
} }
}() }()