Ejemplo n.º 1
0
        /// <summary>
        /// Applies the provided <paramref name="methodInvocation"/> on the provided <paramref name="grainInstance"/>
        /// using reflection.
        /// </summary>
        /// <param name="grainInstance">
        /// The Grain instance.
        /// </param>
        /// <param name="methodInvocation">
        /// The method invocation being applied.
        /// </param>
        /// <returns>
        /// A <see cref="Task{T}"/> containing the results of invocation.
        /// </returns>
        private static async Task <object> ApplyRequestViaReflection(GrainBase grainInstance, MethodInvocation methodInvocation)
        {
            // Get grain's ActivationData.
            var dataField = typeof(GrainBase).GetField("_Data", BindingFlags.NonPublic | BindingFlags.Instance);

            if (dataField == null)
            {
                return(null);
            }

            var dataValue = dataField.GetValue(grainInstance);

            if (dataValue == null)
            {
                return(null);
            }

            // Get the invoker for the method's interface.
            var method  = dataValue.GetType().GetMethod("GetInvoker");
            var invoker = (IGrainMethodInvoker)method.Invoke(dataValue, new object[] { methodInvocation.InterfaceId, null });

            if (invoker == null)
            {
                return(null);
            }

            // Invoke the invoker.
            return(await invoker.Invoke(grainInstance, methodInvocation.InterfaceId, methodInvocation.MethodId, methodInvocation.Arguments));
        }
Ejemplo n.º 2
0
        public void Next()
        {
            Children.Clear();

            for (int i = 0; i < grains.Count; i++)
            {
                GrainBase grain = grains[i];
                if (grain.X == null || grain.Y == null)
                {
                    continue;
                }
                // 粒子位移
                grain.X += grain.Xa;
                grain.Y += grain.Ya;
                // 遇到边界将加速度反向
                grain.Xa *= (grain.X.Value > this.ActualWidth || grain.X.Value < 0) ? -1 : 1;
                grain.Ya *= (grain.Y.Value > this.ActualHeight || grain.Y.Value < 0) ? -1 : 1;
                // 绘制点
                Ellipse elip = ellipses[i];

                SetLeft(elip, grain.X.Value);
                SetTop(elip, grain.Y.Value);
                Children.Add(elip);
            }
        }
Ejemplo n.º 3
0
 private void GrainsBackground_Loaded(object sender, System.Windows.RoutedEventArgs e)
 {
     //// 添加粒子
     //// x,y为粒子坐标,xa, ya为粒子xy轴加速度,max为连线的最大距离
     for (int i = 0; i < GrainsCount; i++)
     {
         GrainBase gb = new GrainBase();
         gb.X   = rand.NextDouble() * this.ActualWidth;
         gb.Y   = rand.NextDouble() * this.ActualHeight;
         gb.Xa  = rand.NextDouble() * Acceleration - 1;
         gb.Ya  = rand.NextDouble() * Acceleration - 1;
         gb.Max = MaxLineLength;
         grains.Add(gb);
     }
 }
Ejemplo n.º 4
0
        private void Cav_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.MouseDevice.LeftButton == MouseButtonState.Pressed)
            {
                grains.RemoveAt(rand.Next(0, grains.Count));
                GrainBase gb = new GrainBase();
                var       ui = e.GetPosition(this);
                gb.X = ui.X;
                gb.Y = ui.Y;

                gb.Xa  = rand.NextDouble() * Acceleration - 1;
                gb.Ya  = rand.NextDouble() * Acceleration - 1;
                gb.Max = MaxLineLength;
                grains.Add(gb);
            }
        }
Ejemplo n.º 5
0
        private void AddGrains()
        {
            GrainBase gb = new GrainBase();

            gb.X     = rand.NextDouble() * ActualWidth;
            gb.Y     = rand.NextDouble() * ActualHeight;
            gb.Xa    = rand.NextDouble() * Acceleration - 1;
            gb.Ya    = rand.NextDouble() * Acceleration - 1;
            gb.Size  = rand.Next(MinSize, MaxSize);
            gb.Color = GetColor(rand.Next(0, 6));
            Ellipse elip = new Ellipse();

            elip.OpacityMask = opacityMask;
            elip.Height      = elip.Width = gb.Size;
            elip.Fill        = new SolidColorBrush(gb.Color);
            ellipses.Add(elip);
            grains.Add(gb);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Returns the request which the provided <paramref name="grainInstance"/> is currently executing, obtained
        /// using reflection.
        /// </summary>
        /// <param name="grainInstance">
        /// The grain instance.
        /// </param>
        /// <returns>
        /// The request which the provided <paramref name="grainInstance"/> is currently executing.
        /// </returns>
        private static MethodInvocation GetCurrentRequestViaReflection(GrainBase grainInstance)
        {
            // Get the grain's ActivationData.
            var dataField = typeof(GrainBase).GetField("_Data", BindingFlags.NonPublic | BindingFlags.Instance);

            if (dataField == null)
            {
                return(null);
            }

            var dataValue = dataField.GetValue(grainInstance);

            if (dataValue == null)
            {
                return(null);
            }

            // Get the message currently being processed.
            var messageField = dataValue.GetType().GetProperty("Running");
            var messageValue = messageField.GetValue(dataValue, null);

            if (messageValue == null)
            {
                return(null);
            }

            // Get the body of the message.
            var bodyObjectField = messageValue.GetType().GetProperty("BodyObject");
            var bodyObjectValue = bodyObjectField.GetValue(messageValue);

            if (bodyObjectValue == null)
            {
                return(null);
            }

            // Convert the body into a method invocation.
            var invoked = (InvokeMethodRequest)bodyObjectValue;

            return(new MethodInvocation {
                InterfaceId = invoked.InterfaceId, MethodId = invoked.MethodId, Arguments = invoked.Arguments
            });
        }
Ejemplo n.º 7
0
        public void Next()
        {
            this.Children.Clear();
            Color lineColor = Color.FromRgb(255, 255, 255);

            if (OpacityMask is SolidColorBrush)
            {
                lineColor = (OpacityMask as SolidColorBrush).Color;
            }
            else if (OpacityMask is LinearGradientBrush)
            {
                lineColor = (OpacityMask as LinearGradientBrush).GradientStops[0].Color;
            }

            for (int i = 0; i < grains.Count; i++)
            {
                GrainBase dot = grains[i];
                if (dot.X == null || dot.Y == null)
                {
                    continue;
                }
                #region 创建碰撞粒子
                // 粒子位移
                dot.X += dot.Xa;
                dot.Y += dot.Ya;
                // 遇到边界将加速度反向
                dot.Xa *= (dot.X.Value > this.ActualWidth || dot.X.Value < 0) ? -1 : 1;
                dot.Ya *= (dot.Y.Value > this.ActualHeight || dot.Y.Value < 0) ? -1 : 1;
                // 绘制点
                Ellipse elip = new Ellipse();
                elip.Width  = 2;
                elip.Height = 2;
                Canvas.SetLeft(elip, dot.X.Value - 0.5);
                Canvas.SetTop(elip, dot.Y.Value - 0.5);
                elip.Fill = new SolidColorBrush(Colors.Transparent);
                this.Children.Add(elip);
                #endregion


                //判断是不是最后一个,就不用两两比较了
                int endIndex = i + 1;
                if (endIndex == grains.Count)
                {
                    continue;
                }
                for (int j = endIndex; j < grains.Count; j++)
                {
                    GrainBase d2 = grains[j];
                    double    xc = dot.X.Value - d2.X.Value;
                    double    yc = dot.Y.Value - d2.Y.Value;
                    // 两个粒子之间的距离
                    double dis = xc * xc + yc * yc;
                    // 距离比
                    double ratio;
                    // 如果两个粒子之间的距离小于粒子对象的max值,则在两个粒子间画线
                    if (dis < d2.Max)
                    {
                        elip.Fill = new SolidColorBrush(lineColor);
                        // 计算距离比
                        ratio = (d2.Max - dis) / d2.Max;
                        Line   line    = new Line();
                        double opacity = ratio + 0.2;
                        if (opacity > 1)
                        {
                            opacity = 1;
                        }
                        byte ar = (byte)(opacity * 255);
                        line.Stroke          = new SolidColorBrush(Color.FromArgb(ar, lineColor.R, lineColor.G, lineColor.B));
                        line.StrokeThickness = ratio / 2;
                        line.X1 = dot.X.Value;
                        line.Y1 = dot.Y.Value;
                        line.X2 = d2.X.Value;
                        line.Y2 = d2.Y.Value;
                        this.Children.Add(line);
                    }
                }
            }
        }
Ejemplo n.º 8
0
 /// <summary>
 /// Applies the provided <paramref name="methodInvocation"/> on the provided <paramref name="grainInstance"/>.
 /// </summary>
 /// <param name="grainInstance">
 /// The Grain instance.
 /// </param>
 /// <param name="methodInvocation">
 /// The method invocation being applied.
 /// </param>
 /// <returns>
 /// A <see cref="Task{T}"/> containing the results of invocation.
 /// </returns>
 public static async Task <object> ApplyRequest(GrainBase grainInstance, MethodInvocation methodInvocation)
 {
     return(await ApplyRequestDelegate(grainInstance, methodInvocation));
 }
Ejemplo n.º 9
0
 /// <summary>
 /// Returns the request which the provided <paramref name="grainInstance"/> is currently executing.
 /// </summary>
 /// <param name="grainInstance">
 /// The grain instance.
 /// </param>
 /// <returns>
 /// The request which the provided <paramref name="grainInstance"/> is currently executing.
 /// </returns>
 public static MethodInvocation CurrentRequest(GrainBase grainInstance)
 {
     return(CurrentRequestDelegate(grainInstance));
 }