<commit_before>#include "Buttongrid.h"

Buttongrid::Buttongrid(){
	if(text = (char *)malloc(DISPLAY_SIZE+1)) memset(text,0,DISPLAY_SIZE+1); //Had to add one more, to avoid some bug
}

Buttongrid::Buttongrid(unsigned char size){
	if(text = (char *)malloc(DISPLAY_SIZE+1)) memset(text,0,DISPLAY_SIZE+1); //Had to add one more, to avoid some bug
	columns = size;
	rows = size;
	gridSize = size;
	
	//Allocate memory for labels
	if(gridSize!=NULL){
		if(labels = (unsigned char**)malloc(gridSize * sizeof(unsigned char*))){
			for(int i=0; i<gridSize; i++)
			{
				if(labels[i] = (unsigned char*)malloc(gridSize)){
					memset(labels[i],0,gridSize);
				}
			}
		}
	}
	
	this->init();
}

Buttongrid::Buttongrid(unsigned char r, unsigned char c){
	if(text = (char *)malloc(DISPLAY_SIZE+1)) memset(text,0,DISPLAY_SIZE+1); //Had to add one more, to avoid some bug
	columns = c;
	rows = r;
	//gridSize = size;
	
	//Allocate memory for labels
	if(rows!=NULL){
		if(labels = (unsigned char**)malloc(rows * sizeof(unsigned char*))){
			for(int i=0; i<rows; i++)
			{
				if(labels[i] = (unsigned char*)malloc(columns)){
					memset(labels[i],0,columns);
				}
			}
		}
	}
	
	//Allocate memory for names
	if(rows >= 0 && columns >= 0){
		char qty = rows * columns;
		if(names = (const char**) malloc(qty * 8 * sizeof(char))){
			memset(names,0,qty*8*sizeof(char));
		}
	}
	
	//this->init();

}

Buttongrid::Buttongrid(unsigned int width, unsigned int height, int backgroundColor, int textColor, int borderColor){
	if(text = (char *)malloc(DISPLAY_SIZE+1)) memset(text,0,DISPLAY_SIZE+1); //Had to add one more, to avoid some bug
	x = 0;
	y = 0;
	this->setSize(width,height);
	this->setColors(backgroundColor,textColor,borderColor);
	this->init();
}

Buttongrid::~Buttongrid(){
	if(text) free(text);
}

void Buttongrid::init(){
	Button::init();
	type = 0x31;
	borderWidth = 2;
	charPos = 0;
	
	// Initialize labels with position ID
	for(byte r=1; r<=rows; r++)
	{
		for(byte c=1; c<=columns; c++)
		{
			labels[r-1][c-1] = c+columns*(r-1);
			//Serial.print("Row ");
			//Serial.print(r);
			//Serial.print(" Col ");
			//Serial.print(c);
			//Serial.print(" Num ");
			//Serial.println(c+columns*(r-1));
		}
	}
}

void Buttongrid::drawGrid(){
  int xPos,yPos,width,height;
  int btnWidth,btnHeight;
  
  xPos = x;	
  yPos = y;
  width = w;
  height = h;
  btnWidth = w / columns;
  btnHeight = h / rows;
  
  //--nums background rectangle
  Tft.fillRectangle(xPos,yPos,width,height,bgColor);
  
	//-- outer border
  for(byte i=borderWidth; i!=0;i--){
    Tft.drawRectangle(xPos++,yPos++,width--,height--,borderColor);
    width--;
    height--;
  }
  
  //-- horizontal lines
  for(byte j=1;j<rows;j++)
  {
	  // draw # horizontal lines depending on borderWidth
  	for(byte i=0; i<borderWidth;i++){
    	Tft.drawHorizontalLine(x,y+btnHeight*j+i-vGap,width+borderWidth,borderColor);
  	}
  	
		//Only draw gaps in between edges
		if(vGap>0){  	
			//Tft.fillRectangle(xPos,y-vGap+btnHeight*j+1,width,btnHeight-1,BLACK);
			for(byte i=0; i<borderWidth;i++){
				Tft.drawHorizontalLine(x,y+btnHeight*j+i+borderWidth+vGap,width+borderWidth,borderColor);
			}
		}		

  }
  
  //-- vertical lines
  for(byte j=1;j<columns;j++)
  {
	// draw # of vertical lines depending on borderWidth
  	for(byte i=0; i<borderWidth;i++){
    	Tft.drawVerticalLine(x+btnWidth*j+i-hGap,y+borderWidth,height+borderWidth,borderColor);
  	}
  	
		//Only draw gaps in between edges
		if(hGap>0){  	
			//Tft.fillRectangle(xPos,y-vGap+btnHeight*j+1,width,btnHeight-1,BLACK);
			for(byte i=0; i<borderWidth;i++){
				Tft.drawVerticalLine(x+btnWidth*j+i+borderWidth+hGap,y+borderWidth,height+borderWidth,borderColor);
			}
		}		  	
  	
  }

  //-- draw contents  
  byte colIndex=0;
  byte rowIndex=0;
  for(byte r=1; r<=rows; r++)
  {
	  for(byte c=1; c<=columns; c++)
	  {
		  //byte num = getNumber(r,c); //c+(r-1)*columns;
		  //Serial.print(" r: ");
		  //Serial.print(r);
		  //Serial.print(" c: ");
		  //Serial.print(c);
		  //Serial.print(" num: ");
		  //Serial.print(num);
		  //Serial.print(" label: ");
		  //Serial.print(labels[r-1][c-1]);
		  //Serial.print(",");
		  
		  //setLabel(getNumber(r,c),labels[r-1][c-1]);
		  printName(getNumber(r,c));
	  }
	  //Serial.println();
  }
  
  //-- Gaps fill
  if(hGap > 0){
		for(byte j=1;j<columns;j++){
			Tft.fillRectangle(x+j*btnWidth+borderWidth-hGap,y,hGap*2,h,myCanvas->bgColor);
		}
  }
  if(vGap > 0){
		for(byte j=1;j<rows;j++){
			Tft.fillRectangle(x,y+j*btnHeight+borderWidth-vGap,w,vGap*2,myCanvas->bgColor);
		}
  }  
}

void Buttongrid::setEventHandler(void (*functionPointer)(Buttongrid *, unsigned char)){
	eventHandler = functionPointer;
}

void Buttongrid::configure(byte size, byte f){
	columns = size;
	rows = size;
	gridSize = size;<commit_msg>Changed order of drawing buttongrid.<commit_after>#include "Buttongrid.h"

Buttongrid::Buttongrid(){
	if(text = (char *)malloc(DISPLAY_SIZE+1)) memset(text,0,DISPLAY_SIZE+1); //Had to add one more, to avoid some bug
}

Buttongrid::Buttongrid(unsigned char size){
	if(text = (char *)malloc(DISPLAY_SIZE+1)) memset(text,0,DISPLAY_SIZE+1); //Had to add one more, to avoid some bug
	columns = size;
	rows = size;
	gridSize = size;
	
	//Allocate memory for labels
	if(gridSize!=NULL){
		if(labels = (unsigned char**)malloc(gridSize * sizeof(unsigned char*))){
			for(int i=0; i<gridSize; i++)
			{
				if(labels[i] = (unsigned char*)malloc(gridSize)){
					memset(labels[i],0,gridSize);
				}
			}
		}
	}
	
	this->init();
}

Buttongrid::Buttongrid(unsigned char r, unsigned char c){
	if(text = (char *)malloc(DISPLAY_SIZE+1)) memset(text,0,DISPLAY_SIZE+1); //Had to add one more, to avoid some bug
	columns = c;
	rows = r;
	//gridSize = size;
	
	//Allocate memory for labels
	if(rows!=NULL){
		if(labels = (unsigned char**)malloc(rows * sizeof(unsigned char*))){
			for(int i=0; i<rows; i++)
			{
				if(labels[i] = (unsigned char*)malloc(columns)){
					memset(labels[i],0,columns);
				}
			}
		}
	}
	
	//Allocate memory for names
	if(rows >= 0 && columns >= 0){
		char qty = rows * columns;
		if(names = (const char**) malloc(qty * 8 * sizeof(char))){
			memset(names,0,qty*8*sizeof(char));
		}
	}
	
	//this->init();

}

Buttongrid::Buttongrid(unsigned int width, unsigned int height, int backgroundColor, int textColor, int borderColor){
	if(text = (char *)malloc(DISPLAY_SIZE+1)) memset(text,0,DISPLAY_SIZE+1); //Had to add one more, to avoid some bug
	x = 0;
	y = 0;
	this->setSize(width,height);
	this->setColors(backgroundColor,textColor,borderColor);
	this->init();
}

Buttongrid::~Buttongrid(){
	if(text) free(text);
}

void Buttongrid::init(){
	Button::init();
	type = 0x31;
	borderWidth = 2;
	charPos = 0;
	
	// Initialize labels with position ID
	for(byte r=1; r<=rows; r++)
	{
		for(byte c=1; c<=columns; c++)
		{
			labels[r-1][c-1] = c+columns*(r-1);
			//Serial.print("Row ");
			//Serial.print(r);
			//Serial.print(" Col ");
			//Serial.print(c);
			//Serial.print(" Num ");
			//Serial.println(c+columns*(r-1));
		}
	}
}

void Buttongrid::drawGrid(){
  int xPos,yPos,width,height;
  int btnWidth,btnHeight;
  
  xPos = x;	
  yPos = y;
  width = w;
  height = h;
  btnWidth = w / columns;
  btnHeight = h / rows;
  
  //--nums background rectangle
  Tft.fillRectangle(xPos,yPos,width,height,bgColor);
  
	//-- outer border
  for(byte i=borderWidth; i!=0;i--){
    Tft.drawRectangle(xPos++,yPos++,width--,height--,borderColor);
    width--;
    height--;
  }
  
  //-- horizontal lines
  for(byte j=1;j<rows;j++)
  {
	  // draw # horizontal lines depending on borderWidth
  	for(byte i=0; i<borderWidth;i++){
    	Tft.drawHorizontalLine(x,y+btnHeight*j+i-vGap,width+borderWidth,borderColor);
  	}
  	
		//Only draw gaps in between edges
		if(vGap>0){  	
			//Tft.fillRectangle(xPos,y-vGap+btnHeight*j+1,width,btnHeight-1,BLACK);
			for(byte i=0; i<borderWidth;i++){
				Tft.drawHorizontalLine(x,y+btnHeight*j+i+borderWidth+vGap,width+borderWidth,borderColor);
			}
		}		

  }
  
  //-- vertical lines
  for(byte j=1;j<columns;j++)
  {
	// draw # of vertical lines depending on borderWidth
  	for(byte i=0; i<borderWidth;i++){
    	Tft.drawVerticalLine(x+btnWidth*j+i-hGap,y+borderWidth,height+borderWidth,borderColor);
  	}
  	
		//Only draw gaps in between edges
		if(hGap>0){  	
			//Tft.fillRectangle(xPos,y-vGap+btnHeight*j+1,width,btnHeight-1,BLACK);
			for(byte i=0; i<borderWidth;i++){
				Tft.drawVerticalLine(x+btnWidth*j+i+borderWidth+hGap,y+borderWidth,height+borderWidth,borderColor);
			}
		}		  	
  	
  }

  //-- Gaps fill
  if(hGap > 0){
		for(byte j=1;j<columns;j++){
			Tft.fillRectangle(x+j*btnWidth+borderWidth-hGap,y,hGap*2,h,myCanvas->bgColor);
		}
  }
  if(vGap > 0){
		for(byte j=1;j<rows;j++){
			Tft.fillRectangle(x,y+j*btnHeight+borderWidth-vGap,w,vGap*2,myCanvas->bgColor);
		}
  }  
  
  //-- draw contents  
  byte colIndex=0;
  byte rowIndex=0;
  for(byte r=1; r<=rows; r++)
  {
	  for(byte c=1; c<=columns; c++)
	  {
		  printName(getNumber(r,c));
	  }
  }
    
}

void Buttongrid::setEventHandler(void (*functionPointer)(Buttongrid *, unsigned char)){
	eventHandler = functionPointer;
}

void Buttongrid::configure(byte size, byte f){
	columns = size;
	rows = size;
	gridSize = size;
{'fraction_non_alphanumeric': 0.1824302312954718, 'fraction_numerical': 0.012922141383429253, 'mean_word_length': 3.786902286902287, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1401744', 'n_tokens_mistral': 3910, 'n_tokens_neox': 3664, 'n_words': 656}