func HandleInput() { for running := true; running; { e := <-inputChan switch C.eventType(&e) { case C.SDL_QUIT: go QuitFunc() running = false case C.SDL_KEYDOWN: { var ke KeyEvent ke.Key = int(C.eventKey(&e)) go KeyDown(ke) } case C.SDL_KEYUP: { var ke KeyEvent ke.Key = int(C.eventKey(&e)) go KeyUp(ke) } case C.SDL_MOUSEWHEEL: var mwe MouseWheelEvent var x, y C.int C.SDL_GetMouseState(&x, &y) mwe.X = int(x) mwe.Y = int(y) mwe.Up = C.eventMouseWheelY(&e) > 0 go MouseWheelFunc(mwe) case C.SDL_MOUSEBUTTONDOWN: x := int(C.eventMouseX(&e)) y := int(C.eventMouseY(&e)) var me MouseEvent me.X = x me.Y = y me.Button = int(C.eventMouseButton(&e)) go MouseButtonDown(me) case C.SDL_MOUSEBUTTONUP: var me MouseEvent me.Button = int(C.eventMouseButton(&e)) me.X = int(C.eventMouseX(&e)) me.Y = int(C.eventMouseY(&e)) me.Button = int(C.eventMouseButton(&e)) go MouseButtonUp(me) } } }
func HandleEvents() { for running := true; running; { var e C.SDL_Event C.SDL_WaitEvent(&e) switch C.eventType(&e) { case C.SDL_QUIT: inputChan <- e running = false case C.SDL_KEYDOWN, C.SDL_KEYUP, C.SDL_MOUSEBUTTONDOWN, C.SDL_MOUSEBUTTONUP, C.SDL_MOUSEWHEEL: inputChan <- e case Event_MainOpEvent: (<-mainOpChan)() case Event_DrawEvent: drawFunc() C.SDL_RenderPresent(renderer) drawComplete <- nil } } }
func (p *Phidget) setOnEventHandler(c chan bool, t eventType) (*C.handler, error) { h, err := createHandler(func(h *C.handler) C.int { return C.setOnEventHandler(p.handle, h, C.eventType(t)) }) if err != nil { return nil, err } go func() { for { C.onEventAwait(h) select { case c <- true: default: } } }() return h, nil }
// PollEvent polls for currently pending events. // // Events not supported by this binding set are discarded. func pollEvent() interface{} { for { var ev C.SDL_Event if C.SDL_PollEvent(&ev) == 0 { return nil } switch C.eventType(&ev) { case C.SDL_KEYDOWN, C.SDL_KEYUP: return newKeyboardEvent(&ev) case C.SDL_WINDOWEVENT: return newWindowEvent(&ev) case C.SDL_MOUSEWHEEL: return newMouseWheelEvent(&ev) case C.SDL_MOUSEMOTION: return newMouseMotionEvent(&ev) case C.SDL_MOUSEBUTTONDOWN, C.SDL_MOUSEBUTTONUP: return newMouseButtonEvent(&ev) } } }
func (p *Phidget) unsetOnEventHandler(t eventType, h **C.handler) { C.unsetOnEventHandler(p.handle, C.eventType(t)) C.handlerFree(*h) *h = nil }
func run() { scrollFunc := func(b bool, x, y int) { var e MouseWheelEvent e.Up = b e.X = x e.Y = y mouseWheelListenersLock.Lock() for _, v := range mouseWheelListeners { go v.MouseWheelScroll(e) } mouseWheelListenersLock.Unlock() } for running { var e C.SDL_Event C.SDL_WaitEvent(&e) switch C.eventType(&e) { case C.SDL_QUIT: go func() { quitListenersLock.Lock() for _, v := range quitListeners { go v.Quit() } quitListenersLock.Unlock() }() running = false case C.SDL_KEYDOWN: go func() { var ke KeyEvent ke.Key = int(C.eventKey(&e)) keyPressListenersLock.Lock() for _, v := range keyPressListeners { go v.KeyPress(ke) } keyPressListenersLock.Unlock() }() case C.SDL_KEYUP: go func() { var ke KeyEvent ke.Key = int(C.eventKey(&e)) keyReleaseListenersLock.Lock() for _, v := range keyReleaseListeners { go v.KeyRelease(ke) } keyReleaseListenersLock.Unlock() }() case C.SDL_MOUSEBUTTONDOWN: x := int(C.eventMouseX(&e)) y := int(C.eventMouseY(&e)) switch C.eventMouseButton(&e) { case C.SDL_BUTTON_WHEELUP: go scrollFunc(true, x, y) case C.SDL_BUTTON_WHEELDOWN: go scrollFunc(false, x, y) default: go func() { var me MouseEvent me.X = x me.Y = y me.Button = int(C.eventMouseButton(&e)) mousePressListenersLock.Lock() for _, v := range mousePressListeners { go v.MouseButtonPress(me) } mousePressListenersLock.Unlock() }() } case C.SDL_MOUSEBUTTONUP: x := int(C.eventMouseX(&e)) y := int(C.eventMouseY(&e)) switch C.eventMouseButton(&e) { case C.SDL_BUTTON_WHEELUP: go scrollFunc(true, x, y) case C.SDL_BUTTON_WHEELDOWN: go scrollFunc(false, x, y) default: go func() { var me MouseEvent me.Button = int(C.eventMouseButton(&e)) me.X = int(C.eventMouseX(&e)) me.Y = int(C.eventMouseY(&e)) mouseReleaseListenersLock.Lock() for _, v := range mouseReleaseListeners { go v.MouseButtonRelease(me) } mouseReleaseListenersLock.Unlock() }() } } } }