[WinRT] Storyboard とユーザーコントロールでモーダル風ダイアログを作る

花札ゲームで、役ができたときに数秒間だけダイアログを出そうとしているので、その実験です。

■Blend で Storyboard を作る

ダイアログを開くときの sbOpen と閉じるときの sbClose という二つの Storyboard を作っておきます。

本当は枚数がいろいろなのですが、簡単にするために5枚だけ配置しておきます。タネやカスが成立したときは、別のユーザーコントロールを使うようにしようかなと。

長いですが、Blend で作った storyboard を晒します。

<UserControl.Resources>
	<Storyboard x:Name=&quot;sbOpen&quot;>
		<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty=&quot;(UIElement.RenderTransform).(CompositeTransform.TranslateX)&quot; Storyboard.TargetName=&quot;pict1&quot;>
			<EasingDoubleKeyFrame KeyTime=&quot;0&quot; Value=&quot;634&quot;/>
			<EasingDoubleKeyFrame KeyTime=&quot;0:0:0.5&quot; Value=&quot;0&quot;/>
		</DoubleAnimationUsingKeyFrames>
		<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty=&quot;(UIElement.RenderTransform).(CompositeTransform.TranslateX)&quot; Storyboard.TargetName=&quot;pict2&quot;>
			<DiscreteDoubleKeyFrame KeyTime=&quot;0:0:0.3&quot; Value=&quot;565&quot;/>
			<EasingDoubleKeyFrame KeyTime=&quot;0:0:0.8&quot; Value=&quot;0&quot;/>
		</DoubleAnimationUsingKeyFrames>
		<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty=&quot;(UIElement.RenderTransform).(CompositeTransform.TranslateX)&quot; Storyboard.TargetName=&quot;pict3&quot;>
			<DiscreteDoubleKeyFrame KeyTime=&quot;0:0:0.6&quot; Value=&quot;494&quot;/>
			<EasingDoubleKeyFrame KeyTime=&quot;0:0:1&quot; Value=&quot;0&quot;/>
		</DoubleAnimationUsingKeyFrames>
		<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty=&quot;(UIElement.RenderTransform).(CompositeTransform.TranslateX)&quot; Storyboard.TargetName=&quot;pict4&quot;>
			<DiscreteDoubleKeyFrame KeyTime=&quot;0:0:0.6&quot; Value=&quot;426&quot;/>
			<EasingDoubleKeyFrame KeyTime=&quot;0:0:1&quot; Value=&quot;0&quot;/>
		</DoubleAnimationUsingKeyFrames>
		<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty=&quot;(UIElement.RenderTransform).(CompositeTransform.TranslateX)&quot; Storyboard.TargetName=&quot;pict5&quot;>
			<DiscreteDoubleKeyFrame KeyTime=&quot;0:0:0.6&quot; Value=&quot;357&quot;/>
			<EasingDoubleKeyFrame KeyTime=&quot;0:0:1&quot; Value=&quot;0&quot;/>
		</DoubleAnimationUsingKeyFrames>
		<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty=&quot;(UIElement.Opacity)&quot; Storyboard.TargetName=&quot;pict1&quot;>
			<EasingDoubleKeyFrame KeyTime=&quot;0&quot; Value=&quot;0&quot;/>
			<EasingDoubleKeyFrame KeyTime=&quot;0:0:0.5&quot; Value=&quot;1&quot;/>
		</DoubleAnimationUsingKeyFrames>
		<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty=&quot;(UIElement.Opacity)&quot; Storyboard.TargetName=&quot;pict2&quot;>
			<EasingDoubleKeyFrame KeyTime=&quot;0&quot; Value=&quot;0&quot;/>
			<DiscreteDoubleKeyFrame KeyTime=&quot;0:0:0.3&quot; Value=&quot;0&quot;/>
			<EasingDoubleKeyFrame KeyTime=&quot;0:0:0.8&quot; Value=&quot;1&quot;/>
		</DoubleAnimationUsingKeyFrames>
		<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty=&quot;(UIElement.Opacity)&quot; Storyboard.TargetName=&quot;pict3&quot;>
			<EasingDoubleKeyFrame KeyTime=&quot;0&quot; Value=&quot;0&quot;/>
			<DiscreteDoubleKeyFrame KeyTime=&quot;0:0:0.6&quot; Value=&quot;0&quot;/>
			<EasingDoubleKeyFrame KeyTime=&quot;0:0:1&quot; Value=&quot;1&quot;/>
		</DoubleAnimationUsingKeyFrames>
		<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty=&quot;(UIElement.Opacity)&quot; Storyboard.TargetName=&quot;pict4&quot;>
			<EasingDoubleKeyFrame KeyTime=&quot;0&quot; Value=&quot;0&quot;/>
			<DiscreteDoubleKeyFrame KeyTime=&quot;0:0:0.6&quot; Value=&quot;0&quot;/>
			<EasingDoubleKeyFrame KeyTime=&quot;0:0:1&quot; Value=&quot;1&quot;/>
		</DoubleAnimationUsingKeyFrames>
		<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty=&quot;(UIElement.Opacity)&quot; Storyboard.TargetName=&quot;pict5&quot;>
			<EasingDoubleKeyFrame KeyTime=&quot;0&quot; Value=&quot;0&quot;/>
			<DiscreteDoubleKeyFrame KeyTime=&quot;0:0:0.6&quot; Value=&quot;0&quot;/>
			<EasingDoubleKeyFrame KeyTime=&quot;0:0:1&quot; Value=&quot;1&quot;/>
		</DoubleAnimationUsingKeyFrames>
		<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty=&quot;(UIElement.Opacity)&quot; Storyboard.TargetName=&quot;textYaku&quot;>
			<EasingDoubleKeyFrame KeyTime=&quot;0&quot; Value=&quot;0&quot;/>
			<EasingDoubleKeyFrame KeyTime=&quot;0:0:0.3&quot; Value=&quot;1&quot;/>
		</DoubleAnimationUsingKeyFrames>
		<ColorAnimationUsingKeyFrames Storyboard.TargetProperty=&quot;(TextBlock.Foreground).(SolidColorBrush.Color)&quot; Storyboard.TargetName=&quot;textYaku&quot;>
			<EasingColorKeyFrame KeyTime=&quot;0&quot; Value=&quot;White&quot;/>
			<EasingColorKeyFrame KeyTime=&quot;0:0:0.3&quot; Value=&quot;#FFDAD002&quot;/>
			<EasingColorKeyFrame KeyTime=&quot;0:0:0.5&quot; Value=&quot;#FFFBFBFB&quot;/>
		</ColorAnimationUsingKeyFrames>
	</Storyboard>
	<Storyboard x:Name=&quot;sbClose&quot;>
		<DoubleAnimation Duration=&quot;0:0:0.4&quot; To=&quot;0&quot; Storyboard.TargetProperty=&quot;(UIElement.Opacity)&quot; Storyboard.TargetName=&quot;textYaku&quot; d:IsOptimized=&quot;True&quot;>
			<DoubleAnimation.EasingFunction>
				<CubicEase EasingMode=&quot;EaseIn&quot;/>
			</DoubleAnimation.EasingFunction>
		</DoubleAnimation>
		<DoubleAnimation Duration=&quot;0:0:0.5&quot; To=&quot;0&quot; Storyboard.TargetProperty=&quot;(UIElement.Opacity)&quot; Storyboard.TargetName=&quot;pict1&quot; d:IsOptimized=&quot;True&quot;>
			<DoubleAnimation.EasingFunction>
				<CubicEase EasingMode=&quot;EaseIn&quot;/>
			</DoubleAnimation.EasingFunction>
		</DoubleAnimation>
		<DoubleAnimation Duration=&quot;0:0:0.5&quot; To=&quot;0&quot; Storyboard.TargetProperty=&quot;(UIElement.Opacity)&quot; Storyboard.TargetName=&quot;pict2&quot; d:IsOptimized=&quot;True&quot;>
			<DoubleAnimation.EasingFunction>
				<CubicEase EasingMode=&quot;EaseIn&quot;/>
			</DoubleAnimation.EasingFunction>
		</DoubleAnimation>
		<DoubleAnimation Duration=&quot;0:0:0.5&quot; To=&quot;0&quot; Storyboard.TargetProperty=&quot;(UIElement.Opacity)&quot; Storyboard.TargetName=&quot;pict3&quot; d:IsOptimized=&quot;True&quot;>
			<DoubleAnimation.EasingFunction>
				<CubicEase EasingMode=&quot;EaseIn&quot;/>
			</DoubleAnimation.EasingFunction>
		</DoubleAnimation>
		<DoubleAnimation Duration=&quot;0:0:0.5&quot; To=&quot;0&quot; Storyboard.TargetProperty=&quot;(UIElement.Opacity)&quot; Storyboard.TargetName=&quot;pict4&quot; d:IsOptimized=&quot;True&quot;>
			<DoubleAnimation.EasingFunction>
				<CubicEase EasingMode=&quot;EaseIn&quot;/>
			</DoubleAnimation.EasingFunction>
		</DoubleAnimation>
		<DoubleAnimation Duration=&quot;0:0:0.5&quot; To=&quot;0&quot; Storyboard.TargetProperty=&quot;(UIElement.Opacity)&quot; Storyboard.TargetName=&quot;pict5&quot; d:IsOptimized=&quot;True&quot;>
			<DoubleAnimation.EasingFunction>
				<CubicEase EasingMode=&quot;EaseIn&quot;/>
			</DoubleAnimation.EasingFunction>
		</DoubleAnimation>
	</Storyboard>
</UserControl.Resources>

花札は、右から左に流れる感じで出てきます。1枚1枚ちょっと違うタイミングで出てくるとゲームらしいですよね。あと、役の名前が出るときにちょっとだけ黄色から白に色が変化します。このあたりグラフィックの「アクセラレーター」が有効に働くように注意しないとダメなのよで、たとえば、フォントの大きさを変えようとするとパフォーマンスが落ちますという警告が出ます。

これに注意するとスムースなアニメーションが作れるかなと。

あと、札を表示するときに一瞬だけ光を付けたかったのですが(ちょっとだけ光るやつ)WinRT の XAML には、Effect 関係がなくなっているので(コードで追加するんだっけ?)、これはあとで調節します。代案としては、花札の大きさが固定なのであらかじめ、縁をぼかした画像を用意して貼り付けておくのがよいかなと思っています。

■ユーザコントロールを配置する

役を表示するためのダイアログと、テストようのボタンを配置します。

<Grid Background=&quot;{StaticResource ApplicationPageBackgroundThemeBrush}&quot;>
    <local:YakuModal
        x:Name=&quot;yakuDlg&quot;
        HorizontalAlignment=&quot;Left&quot; Margin=&quot;281,93,0,0&quot; VerticalAlignment=&quot;Top&quot;/>
    <Button 
        Click=&quot;StartClick&quot;
        Content=&quot;開始&quot; HorizontalAlignment=&quot;Left&quot; Margin=&quot;65,55,0,0&quot; VerticalAlignment=&quot;Top&quot;/>
    <Button
        Click=&quot;EndClick&quot;
        Content=&quot;終了&quot; HorizontalAlignment=&quot;Left&quot; Margin=&quot;134,55,0,0&quot; VerticalAlignment=&quot;Top&quot;/>
    <Button 
        Click=&quot;OpenClick&quot;
        Content=&quot;開く&quot; HorizontalAlignment=&quot;Left&quot; Margin=&quot;65,93,0,0&quot; VerticalAlignment=&quot;Top&quot;/>
    <Button 
        Click=&quot;InoClick&quot;
        Content=&quot;猪鹿蝶&quot; HorizontalAlignment=&quot;Left&quot; Margin=&quot;134,93,0,0&quot; VerticalAlignment=&quot;Top&quot;/>
</Grid>

■モーダルダイアログの呼び出しコードを書く

MessageDialog 風に、ShowAsync メソッドを使って await で非同期待ちをしたいので、こんな風にできたらいいなぁ、という感じで書きます。OpenClick のところでデバッグ出力をしているのは、start/end がきちんと待ちになっているかどうかのチェック用です。

/// <summary>
/// ダイアログを開くだけ
/// </summary>
/// <param name=&quot;sender&quot;></param>
/// <param name=&quot;e&quot;></param>
private async void StartClick(object sender, RoutedEventArgs e)
{
	await this.yakuDlg.OpenAsync();
}
/// <summary>
/// ダイアログを閉じるだけ
/// </summary>
/// <param name=&quot;sender&quot;></param>
/// <param name=&quot;e&quot;></param>
private async void EndClick(object sender, RoutedEventArgs e)
{
	await this.yakuDlg.CloseAsync();
}
/// <summary>
/// Open/Close が連続した ShowAsync メソッドを使う
/// </summary>
/// <param name=&quot;sender&quot;></param>
/// <param name=&quot;e&quot;></param>
private async void OpenClick(object sender, RoutedEventArgs e)
{
	System.Diagnostics.Debug.WriteLine(&quot;start&quot;);
	await this.yakuDlg.ShowAsync(5000);
	System.Diagnostics.Debug.WriteLine(&quot;end&quot;);
}

/// <summary>
/// 猪鹿蝶の役が揃った場合
/// </summary>
/// <param name=&quot;sender&quot;></param>
/// <param name=&quot;e&quot;></param>
private async void InoClick(object sender, RoutedEventArgs e)
{
	var lst = new List<Card>();
	lst.Add(new Card(&quot;G1&quot;));
	lst.Add(new Card(&quot;J1&quot;));
	lst.Add(new Card(&quot;F1&quot;));
	this.yakuDlg.Cards = lst;
	this.yakuDlg.Message = &quot;猪鹿蝶&quot;;
	await this.yakuDlg.ShowAsync(3000);
}

■モーダルダイアログ側のコードを書く

役を表示するモーダルダイアログのコードを書きます。
アニメーションが、sbOpen と sbClose でわかれているので、それを連続させるために Completed イベントを使っています。そして、コードを簡単にするために await Task.Delay(100); を使って完了待ちをします。
最適化するならば、sbOpen.Completed イベントの中で、sbClose.Begin() を呼び出せばよいのですが、そこは、ShowSync の実装で await の羅列を使いためにこうしています。

public sealed partial class YakuModal : UserControl
{
	public YakuModal()
	{
		this.InitializeComponent();

		// アニメーションの完了イベント
		this.sbOpen.Completed += (s, e) => { _completed = true; };
		this.sbClose.Completed += (s, e) => {
			_completed = true;
			this.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
		};

		_picts = new List<Image>();
		_picts.Add( this.pict1 );
		_picts.Add( this.pict2 );
		_picts.Add( this.pict3 );
		_picts.Add( this.pict4 );
		_picts.Add( this.pict5 );
	}

	private List<Card> _cards;
	/// <summary>
	/// 表示する花札を設定する
	/// </summary>
	public List<Card> Cards {
		get { return _cards; }
		set
		{
			_cards = value;
			if (value != null)
			{
				int i = 0;
				foreach (var c in value)
				{
					if (i < _picts.Count)
					{
						_picts[i].Visibility = Windows.UI.Xaml.Visibility.Visible;
						_picts[i].Source = CardUI.GetResName(c.ID);
					}
					i++;
				}
				for (; i < _picts.Count; i++)
				{
					_picts[i].Visibility = Windows.UI.Xaml.Visibility.Collapsed;
				}
			}

		}
	}
	string _message;
	/// <summary>
	/// 表示するメッセージを設定する
	/// </summary>
	public string Message
	{
		get
		{
			return _message;
		}
		set
		{
			_message = value;
			this.textYaku.Text = value;
		}
	}

	// アニメーションの完了フラグ
	private bool _completed = false;
	private List<Image> _picts;

	/// <summary>
	/// ダイアログを開く
	/// </summary>
	/// <returns></returns>
	public Task OpenAsync()
	{
		_completed = false;
		this.sbOpen.Begin();
		this.Visibility = Windows.UI.Xaml.Visibility.Visible;
		Task t = Task.Run(async () =>
		{
			while (_completed == false)
			{
				await Task.Delay(100);
			}
		});
		return t;
	}

	/// <summary>
	/// ダイアログを閉じる
	/// </summary>
	/// <returns></returns>
	public Task CloseAsync()
	{
		_completed = false;
		this.sbClose.Begin();
		Task t = Task.Run(async () =>
		{
			while (_completed == false)
			{
				await Task.Delay(100);
			}
		});
		return t;
	}

	/// <summary>
	/// 指定した時間表示して閉じる
	/// </summary>
	/// <param name=&quot;wait&quot;></param>
	public async Task<Task> ShowAsync(int wait = 0)
	{
		await OpenAsync();
		await Task.Delay(wait);
		await CloseAsync();
		// await が使いたいので、空のタスクを返す
		return new Task(() => { }); 
		// 最後のタスクを返すのでも ok
		// return CloseAsync();
	}
}

ShowAsync メソッドでは指定したミリ秒数で表示待ちをします。こんな風に、OpenAsync, Task.Delay, CloseAsync の羅列で書けるから良いかなと。
このあたりの内部実装はさておき、外部から使うときは、

	await this.yakuDlg.ShowAsync(3000);

な風にしようというのが UIDD なところです。

■実行してみる

ローカルコンピュータで実際に実行してみます。


右からすすーっと花札が出てきて、真ん中で猪鹿蝶が確定。しばくすると、すっと消えます。というモーダルダイアログができます。カードゲームにありがちなアニメーションだし、業務アプリのメッセージでも取り入れできそうな雰囲気です(自画自賛)。ためしに、acer w500 なタブレットPC で動かしてみましたが、スムースに動きます。
ただ、これだとゲームとしてかなり寂しい感じなので、影とかぼかしのエフェクトが入れたいですね…ちょっと調査しますか。

カテゴリー: C#, WinRT, 花札ゲーム パーマリンク