クラスの作成(ToraModel.vb)
LeapListener.vbのプログラム・コード
リスト6(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がTYPEKEYTAPであった場合は、
' TapActionメソッドを実行する。
If gst.Type = Gesture.GestureType.TYPEKEYTAP Then
TapAction(fingers, TapDirection.Tap)
End If
Next
End If
End If
End Sub
' TapEventデリゲート経由で、LeapTapメソッドを呼び出す。
Public Delegate Sub TapEvent(ByVal sd As TapDirection)
Public Event LeapTap As TapEvent
' GestureTypeがTYPEKEYTAPであった場合のTapActionメソッドの処理。
Private Sub TapAction(ByVal fingers As FingerList, ByVal sd As TapDirection)
fingersCount = fingers.Count
If (fingersCount = 1) Then
Select Case sd
Case TapDirection.Tap
' RaiseEventでLeapTapイベントを発生させる。
RaiseEvent LeapTap(TapDirection.Tap)
Exit Select
End Select
End If
End Sub
クラスの作成(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)
・・・・コード略・・・・
Public Class ToraModel
Implements INotifyPropertyChanged
' メンバー変数の宣言
Private ctlr As Controller
Private listener As LeapListener
Private dispatcher = Application.Current.Dispatcher
' Newメソッド内で、AddHandlerステートメントに、listenerオブジェクトのLeapTap
' イベントに、TapActionベントハンドラ—を追加する。
Public Sub New()
・・・コード略・・・
AddHandler listener.LeapTap, AddressOf TapAction
End Sub
・・・・コード略・・・・
TapActionメソッドの処理
TapDirection列挙体の値がTapであった時の処理を記述します。
モジュール変数Indexが1の場合の処理、すなわち、張り子の虎のheadの座標値が取得された場合の処理です。
dispatcher.Invokeを使って、ワーカースレッドからUIスレッドに処理を任せます。
StoryboardをBeginメソッドで開始します。
具体的なコードはリスト8のようになります。
リスト8 張り子の虎の頭をタップしてストーリーボードが実行される処理(TapActionメソッド)
Private Sub TapAction(ByVal sd As TapDirection)
Select Case sd
Case TapDirection.Tap
If Index = 1 Then
dispatcher.Invoke(Sub()' dispatcher.Invokeを使って、ワーカースレッドからUIスレッドに処理を任せる。
strb.Begin()' Storyboardを開始する
End Sub)
End If
Exit Select
End Select
End Sub
MainWindow.xaml内に[ToraTapModel]を取り込む。
まず名前空間として「xmlns:local="clr-namespace:ToraTapLeapMotion"」を定義します。
次に
イージング処理も記述されています(ストーリーボードコード内の太字部分)
リスト9 「ToraModel」を取り込んだMainWindo.xaml
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ToraTapLeapMotion"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<local:ToraModel/>
</Window.DataContext>
<Window.Resources>
<!--Blendから設定したStoryboadの設定-->
<Storyboard x:Key="Storyboard1">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[1].(SkewTransform.AngleY)" Storyboard.TargetName="head">
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="-20"/>
<EasingDoubleKeyFrame KeyTime="0:0:1" Value="0">
<EasingDoubleKeyFrame.EasingFunction>
<BounceEase EasingMode="EaseIn" Bounces="2" Bounciness="1"/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" Storyboard.TargetName="head">
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="2"/>
<EasingDoubleKeyFrame KeyTime="0:0:1" Value="5">
<EasingDoubleKeyFrame.EasingFunction>
<BounceEase EasingMode="EaseIn" Bounces="2" Bounciness="1"/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" Storyboard.TargetName="head">
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="10"/>
<EasingDoubleKeyFrame KeyTime="0:0:1" Value="3">
<EasingDoubleKeyFrame.EasingFunction>
<BounceEase EasingMode="EaseIn" Bounces="2" Bounciness="1"/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</Window.Resources>
<Canvas>
<Image x:Name="head" HorizontalAlignment="Left" Height="74" VerticalAlignment="Top" Width="90" Source="Images/虎頭.png" Canvas.Left="165" Canvas.Top="128" Stretch="None">
<Image.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Image.RenderTransform>
</Image>
<Image x:Name="body" HorizontalAlignment="Left" Height="182" VerticalAlignment="Top" Width="126" Source="Images/虎身体.png" Canvas.Left="221" Canvas.Top="69" Stretch="None" />
<InkPresenter Name="paintCanvas"/>
</Canvas>
</Window>
※注意
今回紹介したサンプルコードを動かす際には、「LeapCSharp.NET4.0.dll」や「LeapCSharp.dll」、「Leap.dll」を読者自身のフォルダ内にあるDLLファイルに指定し直さなければ動かない可能性があるので、動かない場合は再指定してください。
- この記事のキーワード