WPF Shooting Star


 

This is something like shooting star implemented by WPF/C#. Here is the code. It’s messy, but it is just for my coding memo.

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Storyboard  x:Key="OnClick2">
            <!–<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="r1" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)">
                <SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
                <SplineDoubleKeyFrame KeyTime="00:00:11" Value="-120"/>
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="r1" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)">
                <SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
                <SplineDoubleKeyFrame KeyTime="00:00:11" Value="32"/>
            </DoubleAnimationUsingKeyFrames>–>
            <!–<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="r1" Storyboard.TargetProperty="(UIElement.Opacity)">
                <SplineDoubleKeyFrame KeyTime="00:00:00" Value="1"/>
                <SplineDoubleKeyFrame KeyTime="00:00:01" Value="0"/>
            </DoubleAnimationUsingKeyFrames>–>
        </Storyboard>
    </Window.Resources>
    <Window.Triggers>
        <EventTrigger RoutedEvent="ButtonBase.Click" SourceName="button2">
            <BeginStoryboard x:Name="OnClick2_BeginStoryboard" Storyboard="{StaticResource OnClick2}"/>
        </EventTrigger>
        <EventTrigger RoutedEvent="FrameworkElement.Loaded"/>
    </Window.Triggers>

    <Grid Background="Black">
        <Canvas Height="100" HorizontalAlignment="Left" Margin="120,87,0,0" Name="canvas1" VerticalAlignment="Top" Width="200">
            <Canvas.RenderTransform>
                <TransformGroup>
                    <ScaleTransform/>
                    <SkewTransform/>
                    <RotateTransform/>
                    <TranslateTransform/>
                </TransformGroup>
            </Canvas.RenderTransform>
            <Rectangle Height="4" Name="r" Stroke="Black" Width="1000" Canvas.Left="-50" Canvas.Top="29" >
                <Rectangle.Fill>
                    <LinearGradientBrush MappingMode="RelativeToBoundingBox" EndPoint="0.5,1" StartPoint="0.5,0" >
                        <LinearGradientBrush.RelativeTransform>
                            <TransformGroup>
                                <ScaleTransform CenterX="0.5" CenterY="0.5"/>
                                <SkewTransform CenterX="0.5" CenterY="0.5"/>
                                <RotateTransform Angle="-90" CenterX="0.5" CenterY="0.5"/>
                                <TranslateTransform/>
                            </TransformGroup>
                        </LinearGradientBrush.RelativeTransform>
                        <GradientStop Color="Black" Offset="1"/>
                        <GradientStop Color="Magenta"/>
                    </LinearGradientBrush>
                </Rectangle.Fill>
                <Rectangle.RenderTransform>
                    <TransformGroup>
                        <ScaleTransform/>
                        <SkewTransform/>
                        <RotateTransform/>
                        <TranslateTransform/>
                    </TransformGroup>
                </Rectangle.RenderTransform>
            </Rectangle>
        </Canvas>
        <Button Content="Button" Height="31" HorizontalAlignment="Left" Margin="50,268,0,0" Name="button1" VerticalAlignment="Top" Width="96" Click="button1_Click" />
        <Button Content="Button" Height="31" HorizontalAlignment="Left" Margin="210,268,0,0" Name="button2" VerticalAlignment="Top" Width="96"/>
    </Grid>
</Window>

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            int size = 2;

            Storyboard sb = (Storyboard)this.Resources["OnClick2"];
            SBMoveCanvas(sb, "canvas1");
            SBLength(sb, "r");

            for (int i = 0; i < 200; i++)
            {
                string name = "r" + i.ToString();
                Rectangle r = new Rectangle();
                r.Fill = new SolidColorBrush(Colors.Magenta);
                r.Height = size;
                r.Width = size;

                ScaleTransform st = new ScaleTransform();
                SkewTransform skt = new SkewTransform();
                RotateTransform rt = new RotateTransform();
                TranslateTransform tt = new TranslateTransform();
                TransformGroup tg = new TransformGroup();
                tg.Children.Add(st);
                tg.Children.Add(skt);
                tg.Children.Add(rt);
                tg.Children.Add(tt);

                r.RenderTransform = tg;
                this.RegisterName(name, r);
                Canvas.SetLeft(r, i * size);
                Canvas.SetTop(r, 30);
                SBDissolve(sb, name, i);
                canvas1.Children.Add(r);
            }
        }

        System.Random rnd = new System.Random();

        private void SBDissolve(Storyboard sb, string name, int pos)
        {
            DoubleAnimationUsingKeyFrames daukfX = new DoubleAnimationUsingKeyFrames();

            daukfX.BeginTime = new TimeSpan(0, 0, 0);
            daukfX.Duration = new TimeSpan(0, 0, duration);
            daukfX.KeyFrames.Add(
                 new SplineDoubleKeyFrame(0, KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0))));
            daukfX.KeyFrames.Add(
                new SplineDoubleKeyFrame(rnd.Next(6000, 8000), KeyTime.FromTimeSpan(new TimeSpan(0, 0, duration + (200-pos)))));

            Storyboard.SetTargetName(daukfX, name);
            Storyboard.SetTargetProperty(daukfX, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)"));

            sb.Children.Add(daukfX);

            DoubleAnimationUsingKeyFrames daukfY = new DoubleAnimationUsingKeyFrames();

            daukfY.BeginTime = new TimeSpan(0, 0, 0);
            daukfY.Duration = new TimeSpan(0, 0, duration);
            daukfY.KeyFrames.Add(
                 new SplineDoubleKeyFrame(0, KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0))));
            daukfY.KeyFrames.Add(
                new SplineDoubleKeyFrame(rnd.Next(-500, 500), KeyTime.FromTimeSpan(new TimeSpan(0, 0, duration + (200-pos)))));

            Storyboard.SetTargetName(daukfY, name);
            Storyboard.SetTargetProperty(daukfY, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)"));

            sb.Children.Add(daukfY);

            DoubleAnimationUsingKeyFrames daukfOp = new DoubleAnimationUsingKeyFrames();

            daukfOp.BeginTime = new TimeSpan(0, 0, 0);
            daukfOp.Duration = new TimeSpan(0, 0, duration);
            daukfOp.KeyFrames.Add(
                 new SplineDoubleKeyFrame(1.0, KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0))));
            daukfOp.KeyFrames.Add(
                new SplineDoubleKeyFrame(0.0, KeyTime.FromTimeSpan(new TimeSpan(0, 0, duration))));

            Storyboard.SetTargetName(daukfOp, name);
            Storyboard.SetTargetProperty(daukfOp, new PropertyPath("(UIElement.Opacity)"));

            sb.Children.Add(daukfOp);
        }

        const int duration = 5;

        private void SBMoveCanvas(Storyboard sb, string name)
        {
            DoubleAnimationUsingKeyFrames daukfX = new DoubleAnimationUsingKeyFrames();

            daukfX.BeginTime = new TimeSpan(0, 0, 0);
            daukfX.Duration = new TimeSpan(0, 0, duration);
            daukfX.KeyFrames.Add(
                 new SplineDoubleKeyFrame(500, KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0))));
            daukfX.KeyFrames.Add(
                new SplineDoubleKeyFrame(-500, KeyTime.FromTimeSpan(new TimeSpan(0, 0, duration))));

            Storyboard.SetTargetName(daukfX, name);
            Storyboard.SetTargetProperty(daukfX, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)"));

            sb.Children.Add(daukfX);

            DoubleAnimationUsingKeyFrames daukfY = new DoubleAnimationUsingKeyFrames();

            daukfY.BeginTime = new TimeSpan(0, 0, 0);
            daukfY.Duration = new TimeSpan(0, 0, duration);
            daukfY.KeyFrames.Add(
                 new SplineDoubleKeyFrame(30, KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0))));
            daukfY.KeyFrames.Add(
                new SplineDoubleKeyFrame(30, KeyTime.FromTimeSpan(new TimeSpan(0, 0, duration))));

            Storyboard.SetTargetName(daukfY, name);
            Storyboard.SetTargetProperty(daukfY, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)"));

            sb.Children.Add(daukfY);
        }

        private void SBLength(Storyboard sb, string name)
        {
            DoubleAnimationUsingKeyFrames daukfX = new DoubleAnimationUsingKeyFrames();

            daukfX.BeginTime = new TimeSpan(0, 0, 0);
            daukfX.Duration = new TimeSpan(0, 0, duration);
            daukfX.KeyFrames.Add(
                 new SplineDoubleKeyFrame(1, KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0))));
            daukfX.KeyFrames.Add(
                new SplineDoubleKeyFrame(0, KeyTime.FromTimeSpan(new TimeSpan(0, 0, duration))));

            Storyboard.SetTargetName(daukfX, name);
            Storyboard.SetTargetProperty(daukfX, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"));

            sb.Children.Add(daukfX);
        }
    }

広告

コメントを残す

以下に詳細を記入するか、アイコンをクリックしてログインしてください。

WordPress.com ロゴ

WordPress.com アカウントを使ってコメントしています。 ログアウト / 変更 )

Twitter 画像

Twitter アカウントを使ってコメントしています。 ログアウト / 変更 )

Facebook の写真

Facebook アカウントを使ってコメントしています。 ログアウト / 変更 )

Google+ フォト

Google+ アカウントを使ってコメントしています。 ログアウト / 変更 )

%s と連携中


%d人のブロガーが「いいね」をつけました。