using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace TestProject
{
/// <summary>
/// 메인 윈도우
/// </summary>
public partial class MainWindow : Window
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// 탐색 시간 텍스트 박스
/// </summary>
private TextBox seekAmountTextBox;
/// <summary>
/// 탐색 위치 리스트 박스
/// </summary>
private ListBox seekOriginListBox;
/// <summary>
/// 탐색 버튼
/// </summary>
private Button seekButton;
/// <summary>
/// 애니메이션 클럭
/// </summary>
private AnimationClock animationClock;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - MainWindow()
/// <summary>
/// 생성자
/// </summary>
public MainWindow()
{
InitializeComponent();
#region 타겟 사각형을 생성한다.
Rectangle targetRectangle = new Rectangle();
targetRectangle.Width = 100;
targetRectangle.Height = 100;
targetRectangle.Fill = Brushes.Orange;
#endregion
#region 시작 버튼을 생성한다.
Button beginButton = new Button();
beginButton.Width = 100;
beginButton.Height = 30;
beginButton.Content = "시작";
#endregion
#region 중지 버튼을 생성한다.
Button pauseButton = new Button();
pauseButton.Margin = new Thickness(10, 0, 0, 0);
pauseButton.Width = 100;
pauseButton.Height = 30;
pauseButton.Content = "중지";
#endregion
#region 재개 버튼을 생성한다.
Button resumeButton = new Button();
resumeButton.Margin = new Thickness(10, 0, 0, 0);
resumeButton.Width = 100;
resumeButton.Height = 30;
resumeButton.Content = "재개";
#endregion
#region 건너뛰기 버튼을 생성한다.
Button skipToFillButton = new Button();
skipToFillButton.Margin = new Thickness(10, 0, 0, 0);
skipToFillButton.Width = 100;
skipToFillButton.Height = 30;
skipToFillButton.Content = "건너뛰기";
#endregion
#region 중단 버튼을 생성한다.
Button stopButton = new Button();
stopButton.Margin = new Thickness(10, 0, 0, 0);
stopButton.Width = 100;
stopButton.Height = 30;
stopButton.Content = "중단";
#endregion
#region 버튼 스택 패널을 생성한다.
StackPanel buttonStackPanel = new StackPanel();
buttonStackPanel.HorizontalAlignment = HorizontalAlignment.Center;
buttonStackPanel.Margin = new Thickness(0, 10 , 0, 0);
buttonStackPanel.Orientation = Orientation.Horizontal;
buttonStackPanel.Children.Add(beginButton);
buttonStackPanel.Children.Add(pauseButton);
buttonStackPanel.Children.Add(resumeButton);
buttonStackPanel.Children.Add(skipToFillButton);
buttonStackPanel.Children.Add(stopButton);
#endregion
#region 탐색 시간 레이블을 생성한다.
Label seekAmountLabel = new Label();
seekAmountLabel.VerticalAlignment = VerticalAlignment.Center;
seekAmountLabel.Content = "탐색 시간";
#endregion
#region 탐색 시간 텍스트 박스를 생성한다.
this.seekAmountTextBox = new TextBox();
this.seekAmountTextBox.VerticalAlignment = VerticalAlignment.Center;
this.seekAmountTextBox.Width = 80;
this.seekAmountTextBox.Height = 25;
this.seekAmountTextBox.HorizontalContentAlignment = HorizontalAlignment.Center;
this.seekAmountTextBox.VerticalContentAlignment = VerticalAlignment.Center;
this.seekAmountTextBox.Text = "00:00:01";
#endregion
#region 탐색 위치 레이블을 생성한다.
Label seekOriginLabel = new Label();
seekOriginLabel.Margin = new Thickness(10, 0, 0, 0);
seekOriginLabel.VerticalAlignment = VerticalAlignment.Center;
seekOriginLabel.Content = "탐색 위치";
#endregion
#region 탐색 위치 리스트 박스를 생성한다.
this.seekOriginListBox = new ListBox();
this.seekOriginListBox.Padding = new Thickness(5);
this.seekOriginListBox.Items.Add("BeginTime");
this.seekOriginListBox.Items.Add("Duration");
this.seekOriginListBox.SelectedIndex = 0;
#endregion
#region 탐색 버튼을 생성한다.
this.seekButton = new Button();
this.seekButton.VerticalAlignment = VerticalAlignment.Center;
this.seekButton.Margin = new Thickness(20, 0, 0, 0);
this.seekButton.Width = 100;
this.seekButton.Height = 30;
this.seekButton.Content = "탐색";
#endregion
#region 탐색 스택 패널을 생성한다.
StackPanel seekStackPanel = new StackPanel();
seekStackPanel.Margin = new Thickness(0, 20, 0, 0);
seekStackPanel.Orientation = Orientation.Horizontal;
seekStackPanel.Children.Add(seekAmountLabel);
seekStackPanel.Children.Add(this.seekAmountTextBox);
seekStackPanel.Children.Add(seekOriginLabel);
seekStackPanel.Children.Add(this.seekOriginListBox);
seekStackPanel.Children.Add(this.seekButton);
#endregion
#region 메인 스택 패널을 생성한다.
StackPanel mainStackPanel = new StackPanel();
mainStackPanel.HorizontalAlignment = HorizontalAlignment.Center;
mainStackPanel.VerticalAlignment = VerticalAlignment.Center;
mainStackPanel.Children.Add(targetRectangle);
mainStackPanel.Children.Add(buttonStackPanel);
mainStackPanel.Children.Add(seekStackPanel);
#endregion
Content = mainStackPanel;
#region 이벤트를 추가한다.
beginButton.Click += beginButton_Click;
pauseButton.Click += pauseButton_Click;
resumeButton.Click += resumeButton_Click;
skipToFillButton.Click += skipToFillButton_Click;
stopButton.Click += stopButton_Click;
this.seekAmountTextBox.TextChanged += seekAmountTextBox_TextChanged;
this.seekButton.Click += seekButton_Click;
#endregion
#region 너비 애니메이션을 생성한다.
DoubleAnimation widthAnimation = new DoubleAnimation
(
100,
500,
new Duration(TimeSpan.FromSeconds(5))
);
#endregion
this.animationClock = widthAnimation.CreateClock();
targetRectangle.ApplyAnimationClock(Rectangle.WidthProperty, this.animationClock);
this.animationClock.Controller.Stop();
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Private
#region 시작 버튼 클릭시 처리하기 - beginButton_Click(sender, e)
/// <summary>
/// 시작 버튼 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void beginButton_Click(object sender, RoutedEventArgs e)
{
this.animationClock.Controller.Begin();
}
#endregion
#region 중지 버튼 클릭시 처리하기 - pauseButton_Click(sender, e)
/// <summary>
/// 중지 버튼 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void pauseButton_Click(object sender, RoutedEventArgs e)
{
this.animationClock.Controller.Pause();
}
#endregion
#region 재개 버튼 클릭시 처리하기 - resumeButton_Click(sender, e)
/// <summary>
/// 재개 버튼 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void resumeButton_Click(object sender, RoutedEventArgs e)
{
this.animationClock.Controller.Resume();
}
#endregion
#region 건너뛰기 버튼 클릭시 처리하기 - skipToFillButton_Click(sender, e)
/// <summary>
/// 건너뛰기 버튼 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void skipToFillButton_Click(object sender, RoutedEventArgs e)
{
this.animationClock.Controller.SkipToFill();
}
#endregion
#region 중단 버튼 클릭시 처리하기 - stopButton_Click(sender, e)
/// <summary>
/// 중단 버튼 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void stopButton_Click(object sender, RoutedEventArgs e)
{
this.animationClock.Controller.Stop();
}
#endregion
#region 탐색 시간 텍스트 박스 텍스트 변경시 처리하기 - seekAmountTextBox_TextChanged(sender, e)
/// <summary>
/// 탐색 시간 텍스트 박스 텍스트 변경시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void seekAmountTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
TextBox textBox = e.Source as TextBox;
if(textBox.Text == null || textBox.Text.Length < 1)
{
this.seekButton.IsEnabled = false;
}
else
{
this.seekButton.IsEnabled = true;
}
}
#endregion
#region 탐색 버튼 클릭시 처리하기 - seekButton_Click(sender, e)
/// <summary>
/// 탐색 버튼 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void seekButton_Click(object sender, RoutedEventArgs e)
{
try
{
TimeSpan seekAmount = TimeSpan.Parse(this.seekAmountTextBox.Text);
TimeSeekOrigin selectedOrigin = (TimeSeekOrigin)Enum.Parse(typeof(TimeSeekOrigin), (string)this.seekOriginListBox.SelectedItem);
this.animationClock.Controller.Seek(seekAmount, selectedOrigin);
}
catch(FormatException)
{
MessageBox.Show($"{this.seekAmountTextBox.Text} 항목 값이 유효한 시간 범위가 아닙니다.");
this.seekButton.IsEnabled = false;
}
}
#endregion
}
}