コード例 #1
0
ファイル: sdl.go プロジェクト: gtalent/starfish
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)
		}
	}
}
コード例 #2
0
ファイル: input.go プロジェクト: griffy/starfish
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()
				}()
			}
		}
	}
}