Beispiel #1
0
func onTouch(glctx gl.Context, e touch.Event) {
	touchX = e.X
	touchY = e.Y

	// When touch occurs we need to figure out which key is pressed.
	// Using color picking for this.
	// That is, scene is redrawn with each key given a unique color.
	// And then the color is read on the touched pixel to figure out which key
	// is pressed or if a key is pressed at all.

	glctx.ClearColor(0.73, 0.5, 0.75, 0.5)
	glctx.Clear(gl.DEPTH_BUFFER_BIT)
	glctx.Clear(gl.COLOR_BUFFER_BIT)
	glctx.UseProgram(program)

	board.DrawI() // Draw the board with each key in a unique color

	c := make([]byte, 12, 12)

	glctx.ReadPixels(c, int(e.X), int(e.Y), 1, 1, gl.RGB, gl.UNSIGNED_BYTE)
	// gl.RGB, gl.UNSIGNED_BYTE is the combination that is preffered by my Android
	// phone. And is said to be the one preffered by many.

	r := (float32(c[0]) / 255) * 100                 // Convert byte to float
	out := float32(math.Floor(float64(r)+0.5)) / 100 // and round up
	key, ok := board.idColorKey[out]

	if !ok {
		curKey, ok := keystate[e.Sequence] //stop already playing sound
		if ok {
			StopSound(curKey)
		}
		return
	}

	if e.Type == touch.TypeBegin {
		keystate[e.Sequence] = key
		PlaySound(key)
	} else if e.Type == touch.TypeEnd {
		delete(keystate, e.Sequence)
		StopSound(key)
	} else if e.Type == touch.TypeMove {
		if keystate[e.Sequence] != key {
			// Drag has moved out of initial key
			curKey, ok := keystate[e.Sequence] //stop already playing sound
			if ok {
				StopSound(curKey)
			}
			PlaySound(key) // play new key's sound
			keystate[e.Sequence] = key
		}
	}
}