Microsoft Small Basic

Program Listing: TCG608
' NUI Grab (Right Hand Version)
' Copyright (c) 2015 Nonki Takahashi. The MIT License.
' Version 0.1
'
debug = "False"
gw = 598
gh = 428
scale = 600 ' graphics / sensor
GraphicsWindow.Title = "NUI Grab 0.1"
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
GraphicsWindow.FontSize = 20
GraphicsWindow.PenWidth = 0
GraphicsWindow.BrushColor = "Gray"
size = 100
ell = Shapes.AddEllipse(size, size)
ex = gw / 2
ey = gh / 2
Shapes.Move(ell, ex - size / 2, ey - size / 2)
GraphicsWindow.BrushColor = "Black"
gl = GraphicsWindow.Left
gt = GraphicsWindow.Top
Mouse.MouseX = gl
Mouse.MouseY = gt
tw = 31
bw = 8
gl = Mouse.MouseX - GraphicsWindow.MouseX + bw
gt = Mouse.MouseY - GraphicsWindow.MouseY + tw
Mouse.MouseX = gw / 2 + gl
Mouse.MouseY = gh / 2 + gt
If debug Then
KinectWindow.IsBackgroundVisible = "True"
KinectWindow.Show("Color")
EndIf
KinectBodyList.BodiesChanged = OnBodiesChanged
KinectBodyList.StartTracking()
While "True"
If changed Then
count = KinectBodyList.Count
For i = 1 To count
If KinectBodyList.IsTracked(i) Then
state = KinectBodyList.GetHandState(i, "right")
If Text.IsSubText("Closed|Lasso|Open", state) Then
pos = KinectBodyList.GetJointPosition(i, "HandRight")
Mouse.ShowCursor()
x = pos["X"] * scale + gw / 2
y = gh / 2 - pos["Y"] * scale
Mouse.MouseX = x + gl
Mouse.MouseY = y + gt
dx = ex - x
dy = ey - y
dist = Math.SquareRoot(dx * dx + dy * dy)
grab = "Closed"
If (state = grab) And (dist <= size / 2) Then
Sound.PlayClick()
While state = grab
cx = x + dx
If cx < 0 Then
ex = 0
ElseIf gw < cx Then
ex = gw
Else
ex = cx
EndIf
cy = y + dy
If cy < 0 Then
ey = 0
ElseIf gh < cy Then
ey = gh
Else
ey = cy
EndIf
Shapes.Move(ell, ex - size / 2, ey - size / 2)
state = KinectBodyList.GetHandState(i, "right")
If state = grab Then
pos = KinectBodyList.GetJointPosition(i, "HandRight")
x = pos["X"] * scale + gw / 2
y = gh / 2 - pos["Y"] * scale
Mouse.MouseX = x + gl
Mouse.MouseY = y + gt
EndIf
EndWhile
EndIf
Else
Mouse.HideCursor()
right2 = ""
EndIf
changed = "False"
EndIf
EndFor
EndIf
EndWhile
Sub OnBodiesChanged
changed = "True"
EndSub