예제 #1
0
        public ButtonCoords(Random r, string buttonName, double maxX, double maxY, List <Rectangle> deniedRegions, Vector marginDimensions, Vector buttonDimensions, Vector parentDimensions) : base(maxX, maxY)
        {
            bool invalid = true;

            X                = 0;
            Y                = 0;
            ButtonName       = buttonName;
            ButtonDimensions = buttonDimensions;
            ParentDimensions = parentDimensions;
            while (invalid)
            {
                X         = r.Next((int)marginDimensions.X, (int)maxX);
                Y         = r.Next((int)marginDimensions.Y, (int)maxY);
                invalid   = false;
                Rectangle = new Rectangle(this, buttonDimensions);

                foreach (Rectangle rect in deniedRegions)
                {
                    if (rect.RectanglesOverlap(Rectangle))
                    {
                        invalid = true;
                    }
                }
            }
            MarginRectangle = new Rectangle(Rectangle.TopLeft, Rectangle.BottomRight);
            MarginRectangle.ResizeRectangle(Rectangle.Corners.TopLeft, marginDimensions.GetOppositeVector());
            MarginRectangle.ResizeRectangle(Rectangle.Corners.BottomRight, marginDimensions);
        }
예제 #2
0
        private void GenerateButtons_DoWork(object sender, DoWorkEventArgs e)
        {
            ButtonRegions = new List <Rectangle>();
            ButtonRegions.AddRange(DeniedRegions);
            Vector rootDimensions = Vector.Null;
            int    placesCount    = 0;
            Timer  fadeTimer      = new Timer();

            fadeTimer.Interval = 1000;
            fadeTimer.Elapsed += (object senderElapsed, ElapsedEventArgs eElapsed) =>
            {
                Debug.WriteLine("FadeTimer elapsed!");
                fadeTimer.Interval = 250;
                if (!GenerateButtons.IsBusy)
                {
                    fadeTimer.Stop();
                    Action invoke = () =>
                    {
                        foreach (UIElement element in MainGrid.Children)
                        {
                            element.Visibility = Visibility.Visible;
                        }
                        DoubleAnimation fade = new DoubleAnimation(1, new Duration(TimeSpan.FromMilliseconds(500)));
                        Root.BeginAnimation(OpacityProperty, fade);
                    };
                    Dispatcher.Invoke(invoke);
                }
            };

            Dispatcher.Invoke(new Action(() =>
            {
                rootDimensions       = new Vector(ActualWidth, ActualHeight);
                DoubleAnimation fade = new DoubleAnimation(0, new Duration(TimeSpan.FromMilliseconds(500)));
                Root.BeginAnimation(OpacityProperty, fade);
                fadeTimer.Start();
                MainGrid.Children.Clear();
            }));
            Debug.WriteLine("Window dimension vector is {0}.", rootDimensions);
            int locationIndex = Random.Next(0, Labels.Length - 1);

            foreach (Rectangle old in DeniedRegions)
            {
                if (displayDeniedRegions)
                {
                    Dispatcher.Invoke(new Action(() =>
                    {
                        Border border          = old.GenerateBorder(rootDimensions);
                        border.BorderBrush     = new SolidColorBrush(Colors.Red);
                        border.BorderThickness = new Thickness(3);
                        border.Background      = new SolidColorBrush(new Color()
                        {
                            A = 127, R = Colors.Red.R, G = Colors.Red.G, B = Colors.Red.B
                        });
                        border.SetValue(Panel.ZIndexProperty, 0);
                        border.Visibility = Visibility.Hidden;
                        MainGrid.Children.Add(border);
                    }));
                }
            }

            foreach (string label in Labels)
            {
                int active = Random.Next(-2, 9);

                if (active > 0 || label == Labels[locationIndex])
                {
                    placesCount++;
                    Debug.WriteLine("Generating button " + label);
                    ButtonCoords newCoords;
                    newCoords = new ButtonCoords(Random, label, rootDimensions.X - (ButtonDimensions.X + 2 * ButtonMargin.X), rootDimensions.Y - (ButtonDimensions.Y + 2 * ButtonMargin.Y), ButtonRegions, ButtonMargin, ButtonDimensions, rootDimensions);
                    ButtonRegions.Add(newCoords);

                    Dispatcher.Invoke(new Action(() =>
                    {
                        Button button = new Button()
                        {
                            Content = new Viewbox()
                            {
                                Child = new TextBlock(new Run(label))
                            },
                            Margin     = newCoords,
                            Tag        = label == Labels[locationIndex],
                            Visibility = Visibility.Hidden,
                            Template   = (ControlTemplate)Resources["GuessButtonStyle"],
                        };
                        button.SetValue(Panel.ZIndexProperty, 10);
                        button.Click += FindButton_Click;
                        MainGrid.Children.Add(button);
                        if (displayDeniedRegions)
                        {
                            Border inner          = newCoords.Rectangle.GenerateBorder(rootDimensions);
                            inner.BorderBrush     = new SolidColorBrush(Colors.Black);
                            inner.BorderThickness = new Thickness(3);
                            inner.Background      = new SolidColorBrush(new Color()
                            {
                                A = 127, R = Colors.Black.R, G = Colors.Black.G, B = Colors.Black.B
                            });
                            inner.SetValue(Panel.ZIndexProperty, 5);
                            inner.Visibility = Visibility.Hidden;
                            MainGrid.Children.Add(inner);

                            Border outer          = newCoords.MarginRectangle.GenerateBorder(rootDimensions);
                            outer.BorderBrush     = new SolidColorBrush(Colors.Green);
                            outer.BorderThickness = new Thickness(3);
                            outer.Background      = new SolidColorBrush(new Color()
                            {
                                A = 127, R = Colors.Green.R, G = Colors.Green.G, B = Colors.Green.B
                            });
                            outer.SetValue(Panel.ZIndexProperty, 0);
                            outer.Visibility = Visibility.Hidden;
                            MainGrid.Children.Add(outer);
                        }
                        Debug.WriteLine(string.Format("Adding button {0}.", newCoords));
                    }));
                }
                else
                {
                    Debug.WriteLine(string.Format("Button {0} skipped", label));
                }
                PlacesCount = placesCount;
            }
        }