Beispiel #1
0
        public void Draw()
        {
            surface.Children.Clear();
            List <TextBlock> h_ticks;
            List <TextBlock> v_ticks;

            width  = (int)surface.ActualWidth;
            height = (int)surface.ActualHeight;
            if (width <= 0 || height <= 0)
            {
                return;
            }

            h_ticks = new List <TextBlock>();
            v_ticks = new List <TextBlock>();
            int tick_width  = 0;
            int tick_height = 0;

            for (int i = 0; i < Settings.GRID_W; i++)
            {
                TextBlock t = new TextBlock
                {
                    Text     = i.ToString(),
                    FontSize = textSize
                };
                t.Measure(new System.Windows.Size(Double.PositiveInfinity, Double.PositiveInfinity));
                tick_width = Math.Max(tick_width, (int)t.DesiredSize.Width);
                h_ticks.Add(t);
            }
            for (int i = 0; i < Settings.GRID_H; i++)
            {
                TextBlock t = new TextBlock
                {
                    Text     = i.ToString(),
                    FontSize = textSize
                };
                t.Measure(new System.Windows.Size(Double.PositiveInfinity, Double.PositiveInfinity));
                tick_height = Math.Max(tick_height, (int)t.DesiredSize.Height);
                v_ticks.Add(t);
            }

            cellW = (width - (borderWidth * 3) - tick_width) / Settings.GRID_W;
            cellH = (height - (borderHeight * 3) - tick_height) / Settings.GRID_H;
            cellW = cellH = Math.Min(cellW, cellH);

            int x = (width - tick_width - (borderWidth * 3) - (cellW * Settings.GRID_W)) / 2;
            int y = (height - tick_height - (borderHeight * 3) - (cellH * Settings.GRID_H)) / 2;

            this.x = x;
            this.y = y;

            // Won't get a click event for any part of the canvas you haven't drawn on, so fill the background with window background color.
            DrawUtil.Rectangle(surface, this.x, this.y, cellW * Settings.GRID_W, cellH * Settings.GRID_H, SystemColors.Window);

            for (int i = 0; i <= Settings.GRID_H; ++i)
            {
                Line line = new Line
                {
                    X1     = x,
                    X2     = x + (cellW * Settings.GRID_W),
                    Y1     = y + (cellH * i),
                    Y2     = y + (cellH * i),
                    Stroke = DrawUtil.CreateBrush(SystemColors.WindowText)
                };
                surface.Children.Add(line);
                if (i < Settings.GRID_H)
                {
                    // Move tick to x - tick_height - this.border_width, y + (Settings.GRID_H - i) * this.cellH) - (this.cellH / 2);
                    v_ticks[i].Foreground = DrawUtil.CreateBrush(SystemColors.WindowText);
                    Canvas.SetLeft(v_ticks[i], x - tick_width - (borderWidth * 2));
                    Canvas.SetTop(v_ticks[i], y + ((Settings.GRID_H - i) * cellH) - (cellH / 2) - borderWidth);
                    surface.Children.Add(v_ticks[i]);
                }
            }

            for (int i = 0; i <= Settings.GRID_W; ++i)
            {
                Line line = new Line
                {
                    X1     = x + (cellW * i),
                    X2     = x + (cellW * i),
                    Y1     = y,
                    Y2     = y + (cellH * Settings.GRID_H),
                    Stroke = DrawUtil.CreateBrush(SystemColors.WindowText)
                };
                surface.Children.Add(line);
                if (i < Settings.GRID_W)
                {
                    // Move tick to x + (this.cellW / 2) + (i * this.cellW) - (tick_width / 2), y + (Settings.GRID_H - i) * this.cellH) + (this.borderWidth);
                    h_ticks[i].Foreground = DrawUtil.CreateBrush(SystemColors.WindowText);
                    Canvas.SetLeft(h_ticks[i], x + (cellW / 2) + (i * cellW) - (tick_width / 2));
                    Canvas.SetTop(h_ticks[i], y + (Settings.GRID_H * cellH) + borderWidth);
                    surface.Children.Add(h_ticks[i]);
                }
            }

            if (pos.X >= 0 && pos.X < Settings.GRID_W && pos.Y >= 0 && pos.Y < Settings.GRID_H)
            {
                DrawUtil.Rectangle(surface, x + (cellW * pos.X) + Settings.INSET_1, y + (cellH * pos.Y) + Settings.INSET_1, cellW - (2 * Settings.INSET_1), cellH - (2 * Settings.INSET_1), Settings.TEAL);
            }

            foreach (Point guess in guesses)
            {
                DrawUtil.Rectangle(surface, x + (cellW * guess.X) + Settings.INSET_2, y + (cellH * guess.Y) + Settings.INSET_2, cellW - (2 * Settings.INSET_2), cellH - (2 * Settings.INSET_2), Settings.RED);
            }

            foreach (Mugwump mugwump in mugwumps)
            {
                if (mugwump.Found)
                {
                    mugwump.Draw(surface, x + (cellW * mugwump.X) + 1, y + (cellH * mugwump.Y) + 1, cellW - 2);
                }
            }

            /*
             * Mugwump test = new Mugwump(1, 1, true, Settings.VIOLET, Settings.WHITE, Settings.RED, Settings.BLACK);
             * test.Draw(this.surface, 100, 100, 100);
             */
        }