#include "PageDesign.h"

//Ʒ
void Draw_Box(Box_XY Box, uint32_t Color,int16_t Offset)
{
	LCD_DrawRectangle(Box.X1 + Offset, Box.Y1 + Offset,
			Box.X1 + Box.Width - Offset, Box.Y1 + Box.Height - Offset, Color);
}

//
void Fill_Box(Box_XY Box,uint32_t BackColor,int16_t Offset)
{
	LCD_Color_Fill(Box.X1+Offset,Box.Y1+Offset,
			Box.X1+Box.Width-Offset,Box.Y1+Box.Height-Offset,BackColor);
}

//ͨWaveڵĲֲԶȫ
void Get_Waveform_Window_Parameters(Wave_Page *Wave)
{
	Wave->Wave_Area.X1 = Wave->Window.X1 + 40;			//㲨X1
	Wave->Wave_Area.Y1 = Wave->Window.Y1 + 10;			//㲨Y1
	Wave->Wave_Area.Width = Wave->Window.Width - 65;	//㲨Ŀ
	Wave->Wave_Area.Height = Wave->Window.Height - 35;	//㲨ĸ߶
	//ηΧԸ߶ȵóÿλѹֵشС
	Wave->Precision = Wave->Wave_Area.Height / (float)((1 << Wave->ADC_Bits)*2);
}

//δں
void Draw_Waveform_Windows(Wave_Page Wave, uint32_t Sample_Rate)
{
	Display_Time_Scale(Wave,Sample_Rate);	//ʾXʱ̶
	Draw_Grid_Background(Wave);				//ʾ񱳾
	Display_Voltage_Scale(Wave);			//ʾYĵѹ̶
}

//Ʋ
void Draw_Waveform(Wave_Page Wave,int16_t *Pre_Wave_Data,int16_t *Wave_Data)
{
	uint16_t i,len;
	uint16_t XAxis_Y;

	len = Wave.Wave_Area.Width;
	XAxis_Y = Wave.Wave_Area.Y1 + Wave.Wave_Area.Height/2;

	//ȥɲβ²
	for(i=0;i<len-1;i++)
	{
		POINT_COLOR = PAGE_COLOR;
		LCD_DrawWaveLine(i + Wave.Wave_Area.X1, XAxis_Y - (int16_t)(Wave.Precision*Pre_Wave_Data[i]),
				i + 1 + Wave.Wave_Area.X1, XAxis_Y - (int16_t)(Wave.Precision*Pre_Wave_Data[i+1]));
		POINT_COLOR = LCD_YELLOW;
		LCD_DrawWaveLine(i + Wave.Wave_Area.X1, XAxis_Y - (int16_t)(Wave.Precision*Wave_Data[i]),
				i + 1 + Wave.Wave_Area.X1, XAxis_Y - (int16_t)(Wave.Precision*Wave_Data[i+1]));
	}

	LCD_Refresh();//ˢͼ
}

//񱳾
void Draw_Grid_Background(Wave_Page Wave)
{
	uint8_t i,j;
	//񱳾
	for(i=0;i<11;i++) {
		for(j=0;j<51;j++) {
			LCD_DrawPoint(Wave.Wave_Area.X1 + j*Wave.Wave_Area.Width/50,
					Wave.Wave_Area.Y1 + i*Wave.Wave_Area.Height/10,LCD_WHITE);//ƺ
			LCD_DrawPoint(Wave.Wave_Area.X1 + i*Wave.Wave_Area.Width/10,
					Wave.Wave_Area.Y1 + j*Wave.Wave_Area.Height/50,LCD_WHITE);//
			if(i==5) {
				LCD_DrawLine(Wave.Wave_Area.X1 + j*Wave.Wave_Area.Width/50,//ƺ
						Wave.Wave_Area.Y1 + i*Wave.Wave_Area.Height/10 - 1,
						Wave.Wave_Area.X1 + j*Wave.Wave_Area.Width/50,
						Wave.Wave_Area.Y1 + i*Wave.Wave_Area.Height/10 + 1,LCD_WHITE);
				LCD_DrawLine(Wave.Wave_Area.X1 + i*Wave.Wave_Area.Width/10 - 1,//
						Wave.Wave_Area.Y1 + j*Wave.Wave_Area.Height/50,
						Wave.Wave_Area.X1 + i*Wave.Wave_Area.Width/10 + 1,
						Wave.Wave_Area.Y1 + j*Wave.Wave_Area.Height/50,LCD_WHITE);
			}
		}
	}

	//LCD_Refresh();//ˢͼ
}

//ʾXʱ̶
void Display_Time_Scale(Wave_Page Wave,uint32_t Sample_Rate)
{
	char str[30];
	uint8_t i;
	uint16_t Byte;
	uint16_t X_Offset;
	uint16_t Y_Offset = 10;
	Box_XY Box_TimeScale;	//̶
	Box_TimeScale.X1 = Wave.Window.X1 + 1;
	Box_TimeScale.Y1 = Wave.Wave_Area.Y1 + Wave.Wave_Area.Height + Y_Offset;
	Box_TimeScale.Width = Wave.Window.Width - 2;
	Box_TimeScale.Height = 12;
	POINT_COLOR = LCD_WHITE;//Xɫ
	BACK_COLOR = PAGE_COLOR;//ȵıɫҳһ
	//ԭеĿ̶
	Fill_Box(Box_TimeScale,PAGE_COLOR, 0);
	//ʾXʱ̶
	for(i=0;i<11;i++){
		if(i <= 5)
			sprintf(str,"%gms",(float)(i-5)*Wave.Wave_Area.Width/(Sample_Rate*10));
		else
			sprintf(str,"+%gms",(float)(i-5)*Wave.Wave_Area.Width/(Sample_Rate*10));//Ӹ

		//ȡַȣʹ̶ȱעж
		Byte = strlen(str);
		X_Offset = Byte*3;//һַ6Ҫƫһ룬3
		LCD_ShowString(Wave.Wave_Area.X1 + i * Wave.Wave_Area.Width/10 - X_Offset,
				Wave.Wave_Area.Y1 + Wave.Wave_Area.Height + Y_Offset,
				150, 30, 12, 0, (u8*)str);
	}

	LCD_Refresh();//ˢͼ
}
//ʾYĵѹ̶
void Display_Voltage_Scale(Wave_Page Wave)
{
	uint8_t i;
	char str[30];

	//ʾYĵѹ̶
	for (i=0;i<11;i++) {
		if (i < 5)
			sprintf(str,"+%.1fV",Wave.Range - i * Wave.Range/5);
		else if(i == 5)
			sprintf(str," %.1fV",Wave.Range - i * Wave.Range/5);
		else
			sprintf(str,"%.1fV",Wave.Range - i * Wave.Range/5);

		LCD_ShowString(Wave.Window.X1+5,
				Wave.Window.Y1+5 + i * Wave.Wave_Area.Height/10,
				150, 30, 12, 0, (u8*)str);
	}

	LCD_Refresh();//ˢͼ
}

//ͨ
void Draw_Normal_Button(Button Button)
{
	uint8_t Byte = 0;
	uint8_t Rows = 0;
	uint8_t i;
	uint16_t X_Offset,Y_Offset;
	uint8_t Border_LineWidth = 2;//߿߿
	POINT_COLOR = Button.TextColor;
	BACK_COLOR = Button.BackColor;

	//ɫ߿
	for(i=0;i<Border_LineWidth;i++)
		Draw_Box(Button.Box, LCD_WHITE, i);

	//ɫ߿
	for(i=Border_LineWidth;i<Border_LineWidth*2;i++)
		Draw_Box(Button.Box,PAGE_COLOR, i);


	//䰴
	Fill_Box(Button.Box,Button.BackColor,Border_LineWidth*2);

	//Text
	while (strlen(Button.Text[Rows]))
		Rows++;

	for(i=0;i<Rows;i++){
		Byte = strlen(Button.Text[i]);
		X_Offset = (Button.Box.Width - Byte*Button.TextSize/2)/2;
		Y_Offset = (Button.Box.Height - Button.TextSize*Rows)/(Rows+1)*(i+1) + Button.TextSize*i;
		LCD_ShowString(Button.Box.X1+X_Offset,Button.Box.Y1+Y_Offset,
				150,Button.TextSize+2,Button.TextSize,0,(u8*)Button.Text[i]);
	}

	//LCD_Refresh();//ˢͼ
}

//ͨı
void Draw_Normal_Text(Text Text, char *str)

{
	uint16_t Byte;
	uint16_t X_Offset,Y_Offset;
	Box_XY Box = Text.Box;

	Byte = strlen(str);
	X_Offset = (Text.Box.Width - Byte*Text.TextSize/2)/2;
	Y_Offset = (Text.Box.Height - Text.TextSize)/2;

	POINT_COLOR = Text.TextColor;
	BACK_COLOR = Text.BackColor;
	Box.Width = X_Offset;
	Fill_Box(Box,Text.BackColor,0);//Ȳ
	Box.X1 = Text.Box.X1 + Text.Box.Width - X_Offset;
	Fill_Box(Box,Text.BackColor,0);//Ȳ
	LCD_ShowString(Text.Box.X1 + X_Offset,Text.Box.Y1 + Y_Offset,
			Text.Box.Width,Text.TextSize+2,Text.TextSize,0,(u8*)str);

	//LCD_Refresh();//ˢͼ
}

//ưЧ
void Draw_Button_Effect(Button Button)
{
	uint8_t Byte = 0;
	uint8_t Rows = 0;
	uint8_t i;
	uint16_t X_Offset,Y_Offset;
	uint8_t Border_LineWidth = 2;	//߿߿ֵԽԽ
	uint8_t Effect_Width = 3;	//ЧЧֵԽЧԽ
	POINT_COLOR = Button.TextColor;
	BACK_COLOR = Button.BackColor;

	//
	for(i=0;i<Border_LineWidth*4;i++)
		Draw_Box(Button.Box, PAGE_COLOR, i);

	//ɫ߿
	for(i=0;i<Border_LineWidth;i++)
		Draw_Box(Button.Box, LCD_WHITE, i+Effect_Width);

	//䰴
	Fill_Box(Button.Box,Button.BackColor,Border_LineWidth*2+Effect_Width);

	//Text
	while (strlen(Button.Text[Rows]))
		Rows++;

	for(i=0;i<Rows;i++){
		Byte = strlen(Button.Text[i]);
		X_Offset = (Button.Box.Width - Byte*Button.TextSize/2)/2;
		Y_Offset = (Button.Box.Height - Button.TextSize*Rows)/(Rows+1)*(i+1) + Button.TextSize*i;
		LCD_ShowString(Button.Box.X1+X_Offset,Button.Box.Y1+Y_Offset,
				150,Button.TextSize+2,Button.TextSize,0,(u8*)Button.Text[i]);
	}

	LCD_Refresh();//ˢͼ
}

//ƵıߣDirectionΪ0ʾߣΪ1ʾ
void Draw_Mark_Line(uint8_t Direction,uint16_t Point, uint16_t Start,uint16_t End,uint32_t Color)
{
	uint8_t i;
	uint8_t Size = 8;


	if(Direction) {
		LCD_DrawLine(Point,Start,Point,End,Color);//
		//Ǳ
		for(i=0;i<Size;i++) {
			LCD_DrawLine(Point-i,Start-i,Point+i,Start-i,Color);//
			LCD_DrawLine(Point-i,Start-i,Point,Start,Color);//\
			LCD_DrawLine(Point,Start,Point+i,Start-i,Color);///
		}
	}
	else {
		LCD_DrawLine(Start,Point,End,Point,Color);//
		//Ǳ
		for(i=0;i<Size;i++) {
			LCD_DrawLine(End+i,Point-i,End+i,Point+i,Color);//|
			LCD_DrawLine(End+i,Point-i,End,Point,Color);///
			LCD_DrawLine(End,Point,End+i,Point+i,Color);//\
		}
	}
}

//л,NumɫͻNum1ʼ
void Draw_Toggle_Button(Button Button,uint8_t Num,uint32_t TextColor,uint32_t BackColor)
{
	uint8_t Byte = 0;
	uint8_t Rows = 0;
	uint8_t i;
	uint16_t X_Offset,Y_Offset;
	uint8_t Border_LineWidth = 2;//߿߿
	POINT_COLOR = Button.TextColor;
	BACK_COLOR = Button.BackColor;

	//ɫ߿
	for(i=0;i<Border_LineWidth;i++)
		Draw_Box(Button.Box, LCD_WHITE, i);

	//ɫ߿
	for(i=Border_LineWidth;i<Border_LineWidth*2;i++)
		Draw_Box(Button.Box,PAGE_COLOR, i);


	//䰴
	Fill_Box(Button.Box,Button.BackColor,Border_LineWidth*2);

	//Text
	while (strlen(Button.Text[Rows]))
		Rows++;

	for(i=0;i<Rows;i++){
		Byte = strlen(Button.Text[i]);
		X_Offset = (Button.Box.Width - Byte*Button.TextSize/2)/2;
		Y_Offset = (Button.Box.Height - Button.TextSize*Rows)/(Rows+1)*(i+1) + Button.TextSize*i;
		if(i == Num - 1) {
			POINT_COLOR = TextColor;
			BACK_COLOR = BackColor;
		} else {
			POINT_COLOR = Button.TextColor;
			BACK_COLOR = Button.BackColor;
		}

		LCD_ShowString(Button.Box.X1+X_Offset,Button.Box.Y1+Y_Offset,
				150,Button.TextSize+2,Button.TextSize,0,(u8*)Button.Text[i]);
	}

	//LCD_Refresh();//ˢͼ
}

//л,NumɫͻNum1ʼ
void Draw_Toggle_Button_Effect(Button Button,uint8_t Num,uint32_t TextColor,uint32_t BackColor)
{
	uint8_t Byte = 0;
	uint8_t Rows = 0;
	uint8_t i;
	uint16_t X_Offset,Y_Offset;
	uint8_t Effect_Width = 3;	//ЧЧֵԽЧԽ
	uint8_t Border_LineWidth = 2;//߿߿
	POINT_COLOR = Button.TextColor;
	BACK_COLOR = Button.BackColor;

	//ɫ߿
	for(i=0;i<Border_LineWidth;i++)
		Draw_Box(Button.Box, LCD_WHITE, i);

	//ɫ߿
	for(i=0;i<Border_LineWidth;i++)
		Draw_Box(Button.Box, LCD_WHITE, i+Effect_Width);

	//䰴
	Fill_Box(Button.Box,Button.BackColor,Border_LineWidth*2+Effect_Width);

	//Text
	while (strlen(Button.Text[Rows]))
		Rows++;

	for(i=0;i<Rows;i++){
		Byte = strlen(Button.Text[i]);
		X_Offset = (Button.Box.Width - Byte*Button.TextSize/2)/2;
		Y_Offset = (Button.Box.Height - Button.TextSize*Rows)/(Rows+1)*(i+1) + Button.TextSize*i;
		if(i == Num - 1) {
			POINT_COLOR = TextColor;
			BACK_COLOR = BackColor;
		} else {
			POINT_COLOR = Button.TextColor;
			BACK_COLOR = Button.BackColor;
		}

		LCD_ShowString(Button.Box.X1+X_Offset,Button.Box.Y1+Y_Offset,
				150,Button.TextSize+2,Button.TextSize,0,(u8*)Button.Text[i]);
	}

	//LCD_Refresh();//ˢͼ
}
