LeapListener.vbのプログラム・コード
リスト6(LeapListener.vb)
02 | Public Overrides Sub OnFrame(ctlr As Controller) |
04 | If (Not fingers.IsEmpty) Then |
05 | Dim gestures As GestureList = currentFrame.Gestures |
06 | For Each gst As Gesture In gestures |
07 | ' GestureTypeがTYPEKEYTAPであった場合は、 |
09 | If gst.Type = Gesture.GestureType.TYPEKEYTAP Then |
10 | TapAction(fingers, TapDirection.Tap) |
17 | ' TapEventデリゲート経由で、LeapTapメソッドを呼び出す。 |
18 | Public Delegate Sub TapEvent(ByVal sd As TapDirection) |
19 | Public Event LeapTap As TapEvent |
21 | ' GestureTypeがTYPEKEYTAPであった場合のTapActionメソッドの処理。 |
22 | Private Sub TapAction(ByVal fingers As FingerList, ByVal sd As TapDirection) |
23 | fingersCount = fingers.Count |
24 | If (fingersCount = 1) Then |
27 | ' RaiseEventでLeapTapイベントを発生させる。 |
28 | RaiseEvent LeapTap(TapDirection.Tap) |
クラスの作成(ToraModel.vb)
第1回のサンプルの、SwipeDirection.vbを作成した方法で、ToraModel.vbクラスを作成します。
第1回の「UpDownModel.vb」と同じコードが多いので異なる個所のみ解説します。
(1) 「Private dispatcher = Application.Current.Dispatcher」メンバー変数を宣言しし、ワーカースレッドからUIスレッドに処理を任せます。
(2) Public Sub New内に「AddHandler listener.LeapTap, AddressOf TapAction」を追加します。
上記の変更を行った後、リスト7のコードを作成します。
ToraModel.vbのプログラム・コード
リスト7(ToraModel.vb)
03 | Implements INotifyPropertyChanged |
05 | Private ctlr As Controller |
06 | Private listener As LeapListener |
07 | Private dispatcher = Application.Current.Dispatcher |
09 | ' Newメソッド内で、AddHandlerステートメントに、listenerオブジェクトのLeapTap |
10 | ' イベントに、TapActionベントハンドラ—を追加する。 |
13 | AddHandler listener.LeapTap, AddressOf TapAction |
TapActionメソッドの処理
TapDirection列挙体の値がTapであった時の処理を記述します。
モジュール変数Indexが1の場合の処理、すなわち、張り子の虎のheadの座標値が取得された場合の処理です。
dispatcher.Invokeを使って、ワーカースレッドからUIスレッドに処理を任せます。
StoryboardをBeginメソッドで開始します。
具体的なコードはリスト8のようになります。
リスト8 張り子の虎の頭をタップしてストーリーボードが実行される処理(TapActionメソッド)
01 | Private Sub TapAction(ByVal sd As TapDirection) |
05 | dispatcher.Invoke(Sub()' dispatcher.Invokeを使って、ワーカースレッドからUIスレッドに処理を任せる。 |
06 | strb.Begin()' Storyboardを開始する |
MainWindow.xaml内に[ToraTapModel]を取り込む。
まず名前空間として「xmlns:local="clr-namespace:ToraTapLeapMotion"」を定義します。
次にプロパティ要素内に「」と記述します。MainWindow.xaml内に「ToraModel」クラスが取り込まれます。リスト9のようになります。
イージング処理も記述されています(ストーリーボードコード内の太字部分)
リスト9 「ToraModel」を取り込んだMainWindo.xaml
01 | <Window x:Class="MainWindow" |
04 | xmlns:local="clr-namespace:ToraTapLeapMotion" |
05 | Title="MainWindow" Height="350" Width="525"> |
11 | <!--Blendから設定したStoryboadの設定--> |
12 | <Storyboard x:Key="Storyboard1"> |
13 | <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[1].(SkewTransform.AngleY)" Storyboard.TargetName="head"> |
14 | <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="-20"/> |
15 | <EasingDoubleKeyFrame KeyTime="0:0:1" Value="0"> |
16 | <EasingDoubleKeyFrame.EasingFunction> |
17 | <BounceEase EasingMode="EaseIn" Bounces="2" Bounciness="1"/> |
18 | </EasingDoubleKeyFrame.EasingFunction> |
19 | </EasingDoubleKeyFrame> |
20 | </DoubleAnimationUsingKeyFrames> |
21 | <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" Storyboard.TargetName="head"> |
22 | <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="2"/> |
23 | <EasingDoubleKeyFrame KeyTime="0:0:1" Value="5"> |
24 | <EasingDoubleKeyFrame.EasingFunction> |
25 | <BounceEase EasingMode="EaseIn" Bounces="2" Bounciness="1"/> |
26 | </EasingDoubleKeyFrame.EasingFunction> |
27 | </EasingDoubleKeyFrame> |
28 | </DoubleAnimationUsingKeyFrames> |
29 | <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" Storyboard.TargetName="head"> |
30 | <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="10"/> |
31 | <EasingDoubleKeyFrame KeyTime="0:0:1" Value="3"> |
32 | <EasingDoubleKeyFrame.EasingFunction> |
33 | <BounceEase EasingMode="EaseIn" Bounces="2" Bounciness="1"/> |
34 | </EasingDoubleKeyFrame.EasingFunction> |
35 | </EasingDoubleKeyFrame> |
36 | </DoubleAnimationUsingKeyFrames> |
41 | <Image x:Name="head" HorizontalAlignment="Left" Height="74" VerticalAlignment="Top" Width="90" Source="Images/虎頭.png" Canvas.Left="165" Canvas.Top="128" Stretch="None"> |
42 | <Image.RenderTransform> |
49 | </Image.RenderTransform> |
52 | <Image x:Name="body" HorizontalAlignment="Left" Height="182" VerticalAlignment="Top" Width="126" Source="Images/虎身体.png" Canvas.Left="221" Canvas.Top="69" Stretch="None" /> |
54 | <InkPresenter Name="paintCanvas"/> |
※注意
今回紹介したサンプルコードを動かす際には、「LeapCSharp.NET4.0.dll」や「LeapCSharp.dll」、「Leap.dll」を読者自身のフォルダ内にあるDLLファイルに指定し直さなければ動かない可能性があるので、動かない場合は再指定してください。