Пример #1
0
        /// <summary>
        /// Draws the hex grid on the given graphics.
        /// </summary>
        /// <param name="gr">The graphics to draw on</param>
        /// <param name="pen">The pen to draw the lines of the grid</param>
        /// <param name="xOffset">The offset to the right from the top left corner</param>
        /// <param name="yOffset">The offset to the bottom from the top left corner</param>
        public void DrawHexGrid(Graphics gr, Pen pen, float xOffset = 0, float yOffset = 0)
        {
            // Allow easier access to the points for drawing
            PointF[]       points;
            RegularHexagon hex;

            // Create our arguments outside of the loop to avoid extra garbage collection
            SingleHexagonDrawEventArgs singleArgs = new SingleHexagonDrawEventArgs();

            singleArgs.gr   = gr;
            singleArgs.pen  = pen;
            singleArgs.grid = this;

            HexagonGridDrawEventArgs gridArgs = new HexagonGridDrawEventArgs();

            gridArgs.gr   = gr;
            gridArgs.pen  = pen;
            gridArgs.grid = this;

            // Setup for drawing
            ResizeGrid();
            OnBeforeGridDraw(gridArgs);

            // Draw
            for (int row = 0; row < rows; row++)
            {
                for (int col = 0; col < columns; col++)
                {
                    // Calculate and draw the hexagon
                    hex = HexToPoints(row, col, xOffset, yOffset);
                    _hexagons[row, col] = hex;
                    points = hex.vertices;
                    gr.DrawPolygon(pen, points);

                    // Update our event args
                    singleArgs.hex    = hex;
                    singleArgs.row    = row;
                    singleArgs.column = col;

                    // Run our hex events
                    OnSingleHexDraw(singleArgs);
                }
            }

            // Run our grid draw events
            OnHexagonGridDraw(gridArgs);
        }
Пример #2
0
 /// <summary>
 /// Runs the events for SingleHexDraw, protecting against null values.
 /// </summary>
 /// <param name="e">The arguments to be passed on</param>
 protected virtual void OnSingleHexDraw(SingleHexagonDrawEventArgs e)
 {
     SingleHexDraw?.Invoke(this, e);
 }