// CellMaker v3.3  10/02/2008. 
//
// Makes a grid of cells inside a given selection. 
// The selection may be more than one area. 
// The corners of each cell are entirely contained within the selection.
// Cell walls drawn in Primary color.
//  

int Amount1=12;  // [2,255] Internal Cell Width
int Amount2=12;  // [2,255] Internal Cell Height
int Amount3=0;  // [0,2] Corners: Hard, Soft, Rounded
// Soft corners makes subsequent wall removal easier as the corner 
// pixels are not drawn making the four cell walls discontinuous.

void Render(Surface dst, Surface src, Rectangle rect)
{

//     Other variables
        PdnRegion selectionRegion = EnvironmentParameters.GetSelection(src.Bounds);
        Rectangle selection = this.EnvironmentParameters.GetSelection(src.Bounds).GetBoundsInt(); 
    	ColorBgra PrimaryColor = (ColorBgra)EnvironmentParameters.PrimaryColor;

        int GridWidth, GridHeight, Top, Bottom, Left, Right, CellWidth, CellHeight;
        
        CellWidth = (int) Amount1 + 1 ;
        CellHeight = (int) Amount2 + 1;
        GridWidth = (int) (dst.Width / CellWidth);
        GridHeight = (int) (dst.Height / CellHeight); 
        
//  Clear the canvas in the case of changed parameters

        for(int y = rect.Top; y < rect.Bottom; y++) 
            for (int x = rect.Left; x < rect.Right; x++) 
                dst[x,y] = src[x,y]; 
           
// Loop through all the cells in the selection

   for ( int y = 0; y < GridHeight; y++ )
        {       // Find the values for the top & bottom cell vertices  
        Top = selection.Top + ( y * CellHeight );
        Bottom = Top + CellHeight;
                        		            
   for ( int x = 0; x < GridWidth; x++ )
        {       // Find the values for the left & right cell vertices      
        Left = selection.Left + ( x * CellWidth );
        Right = Left + CellWidth;
        
// Check the cell wall intersections (vertices) are all visible, only entire cells should be drawn
            
        if (selectionRegion.IsVisible(Left,Top) && selectionRegion.IsVisible(Right, Top) && selectionRegion.IsVisible(Left, Bottom) && selectionRegion.IsVisible(Right, Bottom))	 
            {  
            int LA = Left+Amount3;
            int RA = Right-Amount3;
            int TA = Top+Amount3;
            int BA = Bottom-Amount3;
            
            switch (Amount3)
            {
                case 0:
                case 1:
                    // Top wall
                    for ( int j = LA; j <= RA; j++ ) 
                        { dst[j,Top]=PrimaryColor; } 
                        // Sides
                    for ( int k = TA; k <= BA; k++ )
                        {
                        dst[Left,k]=PrimaryColor;
                        dst[Right,k]=PrimaryColor;
                        }
                        // Bottom wall
                    for ( int j = LA; j <= RA; j++ ) 
                        { dst[j,Bottom]=PrimaryColor; } 
                    break;
                
                case 2:
                    // Top wall
                    for ( int j = LA; j <= RA; j++ ) 
                        { dst[j,Top]=PrimaryColor; } 
                        // Sides
                    for ( int k = TA; k <= BA; k++ )
                        {
                        dst[Left,k]=PrimaryColor;
                        dst[Right,k]=PrimaryColor;
                        }
                        // Bottom wall
                    for ( int j = LA; j <= RA; j++ ) 
                        { dst[j,Bottom]=PrimaryColor; }
                    int L1 = Left+1;
                    int R1 = Right-1;
                    int T1 = Top+1;
                    int B1 = Bottom-1;
                    dst[ L1,T1 ] = PrimaryColor;   // fill in the extra squares
                    dst[ R1,T1 ] = PrimaryColor;
                    dst[ L1,B1 ] = PrimaryColor;
                    dst[ R1,B1 ] = PrimaryColor;    
                    break;
                
                    default:
                    break;
            }
            }
        }
        }

}   

