Пример #1
0
func (w *Window) EventChan() (events <-chan interface{}) {
	downKeys := make(map[string]bool)
	ec := make(chan interface{})
	go func(ec chan<- interface{}) {

		noXY := image.Point{-1, -1}
		lastXY := noXY

	eventloop:
		for {
			e := C.getNextEvent(w.cw)
			switch e.kind {
			case C.GMDNoop:
				continue
			case C.GMDMouseDown:
				var mde wde.MouseDownEvent
				mde.Where.X = int(e.data[0])
				mde.Where.Y = int(e.data[1])
				mde.Which = getButton(int(e.data[2]))
				lastXY = mde.Where
				ec <- mde
			case C.GMDMouseUp:
				var mue wde.MouseUpEvent
				mue.Where.X = int(e.data[0])
				mue.Where.Y = int(e.data[1])
				mue.Which = getButton(int(e.data[2]))
				lastXY = mue.Where
				ec <- mue
			case C.GMDMouseDragged:
				var mde wde.MouseDraggedEvent
				mde.Where.X = int(e.data[0])
				mde.Where.Y = int(e.data[1])
				mde.Which = getButton(int(e.data[2]))
				if lastXY != noXY {
					mde.From = lastXY
				} else {
					mde.From = mde.Where
				}
				lastXY = mde.Where
				ec <- mde
			case C.GMDMouseMoved:
				var mme wde.MouseMovedEvent
				mme.Where.X = int(e.data[0])
				mme.Where.Y = int(e.data[1])
				if lastXY != noXY {
					mme.From = lastXY
				} else {
					mme.From = mme.Where
				}
				lastXY = mme.Where
				ec <- mme
			case C.GMDMouseEntered:
				w.hasMouse = true
				setCursor(w.cursor)
				var me wde.MouseEnteredEvent
				me.Where.X = int(e.data[0])
				me.Where.Y = int(e.data[1])
				if lastXY != noXY {
					me.From = lastXY
				} else {
					me.From = me.Where
				}
				lastXY = me.Where
				ec <- me
			case C.GMDMouseExited:
				w.hasMouse = false
				setCursor(wde.NormalCursor)
				var me wde.MouseExitedEvent
				me.Where.X = int(e.data[0])
				me.Where.Y = int(e.data[1])
				if lastXY != noXY {
					me.From = lastXY
				} else {
					me.From = me.Where
				}
				lastXY = me.Where
				ec <- me
			case C.GMDMainFocus:
				// for some reason Cocoa resets the cursor to normal when the window
				// becomes the "main" window, so we have to set it back to what we want
				setCursor(w.cursor)
			case C.GMDKeyDown:
				var letter string
				var ke wde.KeyEvent
				keycode := int(e.data[1])

				blankLetter := containsInt(blankLetterCodes, keycode)
				if !blankLetter {
					letter = fmt.Sprintf("%c", e.data[0])
				}

				ke.Key = keyMapping[keycode]

				if !downKeys[ke.Key] {
					ec <- wde.KeyDownEvent(ke)
				}

				downKeys[ke.Key] = true

				ec <- wde.KeyTypedEvent{
					KeyEvent: ke,
					Chord:    wde.ConstructChord(downKeys),
					Glyph:    letter,
				}

			case C.GMDKeyUp:
				var ke wde.KeyUpEvent
				ke.Key = keyMapping[int(e.data[1])]
				delete(downKeys, ke.Key)
				ec <- ke
			case C.GMDResize:
				var re wde.ResizeEvent
				re.Width = int(e.data[0])
				re.Height = int(e.data[1])
				ec <- re
			case C.GMDClose:
				ec <- wde.CloseEvent{}
				break eventloop
				return
			}
		}
		close(ec)
	}(ec)
	events = ec
	return
}
Пример #2
0
func (w *Window) EventChan() (events <-chan interface{}) {
	downKeys := make(map[string]bool)
	ec := make(chan interface{})
	go func(ec chan<- interface{}) {
	eventloop:
		for {
			e := C.getNextEvent(w.cw)
			switch e.kind {
			case C.GMDNoop:
				continue
			case C.GMDMouseDown:
				var mde wde.MouseDownEvent
				mde.Where.X = int(e.data[0])
				mde.Where.Y = int(e.data[1])
				mde.Which = getButton(int(e.data[2]))
				ec <- mde
			case C.GMDMouseUp:
				var mue wde.MouseUpEvent
				mue.Where.X = int(e.data[0])
				mue.Where.Y = int(e.data[1])
				mue.Which = getButton(int(e.data[2]))
				ec <- mue
			case C.GMDMouseDragged:
				var mde wde.MouseDraggedEvent
				mde.Where.X = int(e.data[0])
				mde.Where.Y = int(e.data[1])
				mde.Which = getButton(int(e.data[2]))
				ec <- mde
			case C.GMDMouseMoved:
				var me wde.MouseMovedEvent
				me.Where.X = int(e.data[0])
				me.Where.Y = int(e.data[1])
				ec <- me
			case C.GMDMouseEntered:
				var me wde.MouseEnteredEvent
				me.Where.X = int(e.data[0])
				me.Where.Y = int(e.data[1])
				ec <- me
			case C.GMDMouseExited:
				var me wde.MouseExitedEvent
				me.Where.X = int(e.data[0])
				me.Where.Y = int(e.data[1])
				ec <- me
			case C.GMDKeyDown:
				var letter string
				var ke wde.KeyEvent
				keycode := int(e.data[1])

				blankLetter := containsInt(blankLetterCodes, keycode)
				if !blankLetter {
					letter = fmt.Sprintf("%c", e.data[0])
				}

				ke.Key = keyMapping[keycode]

				if !downKeys[ke.Key] {
					ec <- wde.KeyDownEvent(ke)
				}

				downKeys[ke.Key] = true

				ec <- wde.KeyTypedEvent{
					KeyEvent: ke,
					Chord:    wde.ConstructChord(downKeys),
					Glyph:    letter,
				}

			case C.GMDKeyUp:
				var ke wde.KeyUpEvent
				ke.Key = keyMapping[int(e.data[1])]
				delete(downKeys, ke.Key)
				ec <- ke
			case C.GMDResize:
				var re wde.ResizeEvent
				re.Width = int(e.data[0])
				re.Height = int(e.data[1])
				ec <- re
			case C.GMDClose:
				ec <- wde.CloseEvent{}
				break eventloop
				return
			}
		}
		close(ec)
	}(ec)
	events = ec
	return
}
Пример #3
0
func (w *Window) EventChan() (events <-chan interface{}) {
	downKeys := make(map[string]bool)
	ec := make(chan interface{})
	go func(ec chan<- interface{}) {

		noXY := image.Point{-1, -1}
		lastXY := noXY
		suppressDrag := false

	eventloop:
		for {
			e := C.getNextEvent(w.cw)
			switch e.kind {
			case C.GMDNoop:
				continue
			case C.GMDMouseDown:
				var mde wde.MouseDownEvent
				mde.Where.X = int(e.data[0])
				mde.Where.Y = int(e.data[1])
				mde.Which = getButton(int(e.data[2]))
				lastXY = mde.Where
				ec <- mde
				suppressDrag = true
			case C.GMDMouseUp:
				var mue wde.MouseUpEvent
				mue.Where.X = int(e.data[0])
				mue.Where.Y = int(e.data[1])
				mue.Which = getButton(int(e.data[2]))
				lastXY = mue.Where
				ec <- mue
			case C.GMDMouseDragged:
				if suppressDrag {
					/* Cocoa emits a drag event immediately after a mouse down.
					 * Other backends only do so after the mouse actually moves, which
					 * is the behaviour we emulate here. */
					suppressDrag = false
					continue
				}
				var mde wde.MouseDraggedEvent
				mde.Where.X = int(e.data[0])
				mde.Where.Y = int(e.data[1])
				mde.Which = getButton(int(e.data[2]))
				if lastXY != noXY {
					mde.From = lastXY
				} else {
					mde.From = mde.Where
				}
				lastXY = mde.Where
				ec <- mde
			case C.GMDMouseMoved:
				var mme wde.MouseMovedEvent
				mme.Where.X = int(e.data[0])
				mme.Where.Y = int(e.data[1])
				if lastXY != noXY {
					mme.From = lastXY
				} else {
					mme.From = mme.Where
				}
				lastXY = mme.Where
				ec <- mme
			case C.GMDMouseEntered:
				w.hasMouse = true
				setCursor(w.cursor)
				var me wde.MouseEnteredEvent
				me.Where.X = int(e.data[0])
				me.Where.Y = int(e.data[1])
				if lastXY != noXY {
					me.From = lastXY
				} else {
					me.From = me.Where
				}
				lastXY = me.Where
				ec <- me
			case C.GMDMouseExited:
				w.hasMouse = false
				setCursor(wde.NormalCursor)
				var me wde.MouseExitedEvent
				me.Where.X = int(e.data[0])
				me.Where.Y = int(e.data[1])
				if lastXY != noXY {
					me.From = lastXY
				} else {
					me.From = me.Where
				}
				lastXY = me.Where
				ec <- me
			case C.GMDMouseWheel:
				var me wde.MouseEvent
				me.Where.X = int(e.data[0])
				me.Where.Y = int(e.data[1])
				// TODO e.data[2] contains horizontal scroll info; what do?
				if e.data[3] != 0 {
					button := wde.WheelUpButton
					if e.data[3] == -1 {
						button = wde.WheelDownButton
					}
					ec <- wde.MouseDownEvent{me, button}
					ec <- wde.MouseUpEvent{me, button}
				}
			case C.GMDMagnify:
				var mge wde.MagnifyEvent
				mge.Where.X = int(e.data[0])
				mge.Where.Y = int(e.data[1])
				mge.Magnification = 1 + float64(e.data[2])/65536
				ec <- mge
			case C.GMDRotate:
				var rte wde.RotateEvent
				rte.Where.X = int(e.data[0])
				rte.Where.Y = int(e.data[1])
				rte.Rotation = float64(e.data[2]) / 65536
				ec <- rte
			case C.GMDScroll:
				var se wde.ScrollEvent
				se.Where.X = int(e.data[0])
				se.Where.Y = int(e.data[1])
				se.Delta.X = int(e.data[2])
				se.Delta.Y = int(e.data[3])
				ec <- se
			case C.GMDMainFocus:
				// for some reason Cocoa resets the cursor to normal when the window
				// becomes the "main" window, so we have to set it back to what we want
				setCursor(w.cursor)
			case C.GMDKeyDown:
				var letter string
				var ke wde.KeyEvent
				keycode := int(e.data[1])

				blankLetter := containsInt(blankLetterCodes, keycode)
				if !blankLetter {
					letter = fmt.Sprintf("%c", e.data[0])
				}

				ke.Key = keyMapping[keycode]

				if !downKeys[ke.Key] {
					ec <- wde.KeyDownEvent(ke)
				}

				downKeys[ke.Key] = true

				ec <- wde.KeyTypedEvent{
					KeyEvent: ke,
					Chord:    wde.ConstructChord(downKeys),
					Glyph:    letter,
				}

			case C.GMDKeyUp:
				var ke wde.KeyUpEvent
				ke.Key = keyMapping[int(e.data[1])]
				delete(downKeys, ke.Key)
				ec <- ke
			case C.GMDResize:
				var re wde.ResizeEvent
				re.Width = int(e.data[0])
				re.Height = int(e.data[1])
				ec <- re
			case C.GMDClose:
				ec <- wde.CloseEvent{}
				break eventloop
				return
			}
		}
		close(ec)
	}(ec)
	events = ec
	return
}
Пример #4
0
func (w *Window) EventChan() (events <-chan interface{}) {
	ec := make(chan interface{})
	go func() {
		for {
			println("polling event")
			e := C.getNextEvent(w.cw)
			println("got event")
			var ei interface{}
			switch e.kind {
			case C.GMDNoop:
				continue
			case C.GMDMouseDown:
				var mde MouseDownEvent
				mde.X = int(e.data[0])
				mde.Y = int(e.data[1])
				mde.Button = int(e.data[2])
				ei = mde
			case C.GMDMouseUp:
				var mue MouseUpEvent
				mue.X = int(e.data[0])
				mue.Y = int(e.data[1])
				mue.Button = int(e.data[2])
				ei = mue
			case C.GMDMouseDragged:
				var mue MouseDraggedEvent
				mue.X = int(e.data[0])
				mue.Y = int(e.data[1])
				mue.Button = int(e.data[2])
				ei = mue
			case C.GMDMouseMoved:
				var me MouseMovedEvent
				me.X = int(e.data[0])
				me.Y = int(e.data[1])
				ei = me
			case C.GMDMouseEntered:
				var me MouseEnteredEvent
				me.X = int(e.data[0])
				me.Y = int(e.data[1])
				ei = me
			case C.GMDMouseExited:
				var me MouseExitedEvent
				me.X = int(e.data[0])
				me.Y = int(e.data[1])
				ei = me
			case C.GMDKeyDown:
				var ke KeyDownEvent
				ke.Letter = fmt.Sprintf("%c", e.data[0])
				ke.Code = int(e.data[1])
				ei = ke
			case C.GMDKeyUp:
				var ke KeyUpEvent
				ke.Letter = fmt.Sprintf("%c", e.data[0])
				ke.Code = int(e.data[1])
				ei = ke
			case C.GMDResize:
				var re ResizeEvent
				re.Width = int(e.data[0])
				re.Height = int(e.data[1])
				ei = re
			case C.GMDClose:
				ei = CloseEvent{}
			}
			select {
			case ec <- ei:
			}
		}
	}()
	events = ec
	return
}
Пример #5
0
func (w *Window) EventChan() (events <-chan interface{}) {
	downKeys := make(map[int]bool)
	ec := make(chan interface{})
	go func(ec chan<- interface{}) {
	eventloop:
		for {
			e := C.getNextEvent(w.cw)
			switch e.kind {
			case C.GMDNoop:
				continue
			case C.GMDMouseDown:
				var mde wde.MouseDownEvent
				mde.Where.X = int(e.data[0])
				mde.Where.Y = int(e.data[1])
				mde.Which = getButton(int(e.data[2]))
				ec <- mde
			case C.GMDMouseUp:
				var mue wde.MouseUpEvent
				mue.Where.X = int(e.data[0])
				mue.Where.Y = int(e.data[1])
				mue.Which = getButton(int(e.data[2]))
				ec <- mue
			case C.GMDMouseDragged:
				var mde wde.MouseDraggedEvent
				mde.Where.X = int(e.data[0])
				mde.Where.Y = int(e.data[1])
				mde.Which = getButton(int(e.data[2]))
				ec <- mde
			case C.GMDMouseMoved:
				var me wde.MouseMovedEvent
				me.Where.X = int(e.data[0])
				me.Where.Y = int(e.data[1])
				ec <- me
			case C.GMDMouseEntered:
				var me wde.MouseEnteredEvent
				me.Where.X = int(e.data[0])
				me.Where.Y = int(e.data[1])
				ec <- me
			case C.GMDMouseExited:
				var me wde.MouseExitedEvent
				me.Where.X = int(e.data[0])
				me.Where.Y = int(e.data[1])
				ec <- me
			case C.GMDKeyDown:
				var ke wde.KeyEvent
				ke.Letter = fmt.Sprintf("%c", e.data[0])
				ke.Code = int(e.data[1])
				if !downKeys[ke.Code] {
					ec <- wde.KeyDownEvent(ke)
				}
				ec <- wde.KeyTypedEvent(ke)
				downKeys[ke.Code] = true
			case C.GMDKeyUp:
				var ke wde.KeyUpEvent
				ke.Letter = fmt.Sprintf("%c", e.data[0])
				ke.Code = int(e.data[1])
				ec <- ke
				downKeys[ke.Code] = false
			case C.GMDResize:
				var re wde.ResizeEvent
				re.Width = int(e.data[0])
				re.Height = int(e.data[1])
				ec <- re
			case C.GMDClose:
				ec <- wde.CloseEvent{}
				break eventloop
				return
			}
		}
		close(ec)
	}(ec)
	events = ec
	return
}