クラスの作成(LeapListener.vb)
クラスの作成(LeapListener.vb)
次に、第1回でSwipeDirection.vbを作成した方法で、LeapListener.vbクラスを作成しますが、第1回のサンプルと同じコードが含まれているので第1回のLeapListener.vbクラスを使用します。
ソリューション・エクスプローラー内で、gunLeapMotionプロジェクトを選択し、マウスの右クリックで表示されるメニューから、[追加]−[既存の項目]と選択して、第1回で作成しておいたLeapListener.vbを取り込みます。
以下、第1回のサンプルのLeapListener.vbと異なる点のみ解説します。
(1)OnConnectコールバックメソッド内で、GestureTypeにTYPESCREENTAPを指定します。
それ以外のコードは、今回は不要ですので、削除してください。
(2)OnFrameコールバックメソッド内で、GestureType.TYPESWIPEの個所を、GestureType.TYPESCREENTAPに変更します。
(3)If (swipe.Direction.y > 0) ThenからEnd Ifまでを削除し「 Dim myScreenTouch As New ScreenTapGesture(gst)」、「ScreenTapAction(fingers, ScreenTapDirection.ScreenTap)」を記述します。
(4)デリゲートのコードを「Public Delegate Sub ScreenTapEvent(ByVal sd As ScreenTapDirection)」と記述します。
(5)「Public Event LeapSwipe As SwipeEvent」の部分を「Public Event LeapTap As ScreenTapEvent」に書き換えます。
(6)「ScreenTapAction」メソッド内の「Case SwipeDirection.Up」の部分を「Case ScreenTapDirection.ScreenTap」に書き換えます。
(7)「RaiseEvent LeapSwipe(SwipeDirection.Up)」の部分を「 RaiseEvent LeapTap(ScreenTapDirection.ScreenTap)」に書き換えます。
上記の変更を行った後、リスト3のコードを作成します。
LeapListener.vbのプログラム・コード
リスト3 (LeapListener.vb)
・・・・コード略・・・・
Public Overrides Sub OnFrame(ctlr As Controller)
・・・・コード略・・・・
If (Not fingers.IsEmpty) Then
Dim gestures As GestureList = currentFrame.Gestures
For Each gst As Gesture In gestures
' GestureTypeがTYPESCREENTAPであった場合は、
' ScreenTapActionメソッドを実行する。
If gst.Type = Gesture.GestureType.TYPESCREENTAP Then
Dim myScreenTouch As New ScreenTapGesture(gst)
ScreenTapAction(fingers, ScreenTapDirection.ScreenTap)
End If
Next
End If
・・・・コード略・・・・
End Sub
' ScreenTapEventデリゲート経由で、LeapTapメソッドを呼び出す。
Public Delegate Sub ScreenTapEvent(ByVal sd As ScreenTapDirection)
Public Event LeapTap As ScreenTapEvent
' GestureTypeがTYPESCREENTAPであった場合のScreenTapActionメソッドの処理。
Private Sub ScreenTapAction(ByVal fingers As FingerList, ByVal sd As ScreenTapDirection)
fingersCount = fingers.Count
If (fingersCount = 1) Then' ディスプレイに表示される指が1本であった場合
Select Case sd
Case ScreenTapDirection.ScreenTap
' RaiseEventでLeapTapイベントを発生させる。
RaiseEvent LeapTap(ScreenTapDirection.ScreenTap)
Exit Select
End Select
End If
End Sub
クラスの作成(gunModel.vb)
第1回のサンプルでSwipeDirection.vbを作成した方法で、gunModel.vbクラスを作成します。
第1回の「UpDownModel.vb」と同じコードが多いので異なる個所のみ解説します。
(1)ワーカースレッドからUIスレッドに処理を任せるために、「Private dispatcher = Application.Current.Dispatcher」メンバー変数を宣言します。
(2)「Private windowWidth As Double = 1920」と「Private windowHeight As Double = 1080」メンバー変数を宣言します。
(3)デジタイザーとスタイラスから収集された単一のデータ・ポイントを表すStylusPoint構造体の「Private touchPoint As StylusPoint」を宣言します。
(4)「Private tx As Double」と「Private ty As Double」メンバー変数を宣言します。
(5)Imageクラス型の「Private myImage As Image」メンバー変数を宣言します。
(6)MediaElementクラス型の「Private myMedia As MediaElement」メンバー変数を宣言します。
(7)Public Sub New内に「AddHandler listener.LeapTap, AddressOf ScreenTapAction」を追加します。
上記の変更を行った後、リスト4のコードを作成します。
gunModel.vbのプログラム・コード
リスト4 (gunModel.vb)
・・・・コード略・・・・
Public Class gunModel
Implements INotifyPropertyChanged
' 各メンバー変数の宣言
Private leap As New Controller
Private ctlr As Controller
Private listener As LeapListener
Private dispatcher = Application.Current.Dispatcher
Private windowWidth As Double = 1920
Private windowHeight As Double = 1080
Private touchPoint As StylusPoint
Private tx As Double
Private ty As Double
Private myImage As Image
Private myMedia As MediaElement
' Newメソッド内で、AddHandlerステートメントに、listenerオブジェクトのLeapTap
'イベントに、ScreenTapActionベント・ハンドラ—を追加する。
Public Sub New()
・・・コード略・・・
AddHandler listener.LeapTap, AddressOf ScreenTapAction
End Sub
・・・・コード略・・・・
- この記事のキーワード