//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "ScoreFormUnit.h"
//#include "GlobalUnit.h"
#include "ProgressFormUnit.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "GeneralDialogPanel"
#pragma link "TextButton"
#pragma resource "*.dfm"
TScoreForm *ScoreForm;

#define WIDTH 245         //评分面板
#define	HEIGHT 33

TPoint FrameRoute[] = {
		TPoint(0, 5),
		TPoint(300, 5),
		TPoint(305, 0),
		TPoint(310, 5),
		TPoint(340, 5),
		TPoint(340, 120),
		TPoint(0, 120),
};
//---------------------------------------------------------------------------
__fastcall TScoreForm::TScoreForm(TComponent* Owner)
	: TForm(Owner)
{
	SetTransparent( this );

	m_PngScoreA = new TPngImage();
	m_PngScoreA->LoadFromResourceName(int(HInstance), L"imgScore");
	m_PngScoreB = new TPngImage();
	m_PngScoreB->LoadFromResourceName(int(HInstance), L"imgUnScore");

	m_PNGNetScore[0] = PNGButton1;
	m_PNGNetScore[1] = PNGButton2;
	m_PNGNetScore[2] = PNGButton3;
	m_PNGNetScore[3] = PNGButton4;
	m_PNGNetScore[4] = PNGButton5;
	m_PNGNetScore[5] = PNGButton6;
	m_PNGNetScore[6] = PNGButton7;
	m_PNGNetScore[7] = PNGButton8;
	m_PNGNetScore[8] = PNGButton9;
	m_PNGNetScore[9] = PNGButton10;

	m_PNGMyScore[0] = PNGButton11;
	m_PNGMyScore[1] = PNGButton12;
	m_PNGMyScore[2] = PNGButton13;
	m_PNGMyScore[3] = PNGButton14;
	m_PNGMyScore[4] = PNGButton15;
	m_PNGMyScore[5] = PNGButton16;
	m_PNGMyScore[6] = PNGButton17;
	m_PNGMyScore[7] = PNGButton18;
	m_PNGMyScore[8] = PNGButton19;
	m_PNGMyScore[9] = PNGButton20;

	for(int i=0;i<10;i++){
    	m_PNGMyScore[i]->Tag = i+1 + 100;
	}

	MyScore = -1;
}
//---------------------------------------------------------------------------

void TScoreForm::SetScoreWindow(bool DownorUp)
{
	if(DownorUp){
		FrameRoute[0] = TPoint(0, 5);
		FrameRoute[1] = TPoint(300, 5);
		FrameRoute[2] = TPoint(305, 0);
		FrameRoute[3] = TPoint(310, 5);
		FrameRoute[4] = TPoint(340, 5);
		FrameRoute[5] = TPoint(340, 120);
		FrameRoute[6] = TPoint(0, 120);
//		this->Repaint();
	}
	else{
		FrameRoute[0] = TPoint(0, 120);
		FrameRoute[1] = TPoint(300, 120);
		FrameRoute[2] = TPoint(305, 125);
		FrameRoute[3] = TPoint(310, 120);
		FrameRoute[4] = TPoint(340, 120);
		FrameRoute[5] = TPoint(340, 5);
		FrameRoute[6] = TPoint(0, 5);
//		this->Repaint();
	}

	HRGN hRgn;
	hRgn = CreatePolygonRgn(FrameRoute,7, ALTERNATE);
	SetWindowRgn(Handle, hRgn, true);
	DeleteObject(hRgn);
}
//---------------------------------------------------------------------------

void TScoreForm::SetNetScore( int iScore, const char* strName )
{
	char buf[8];
	int score10 =  (int)(iScore/10.0 + 0.5);
	sprintf( buf, "%.1f", score10 / 10.0 );

	m_StyleName->Caption = strName;
	m_NetScore->Caption = buf;

	int iStar = (iScore + 50)/100;
	for( int i=0;i<iStar;i++ ){
		m_PNGNetScore[i]->PngImg = m_PngScoreA;
		m_PNGNetScore[i]->Invalidate();
	}
	for( int i=iStar;i<10;i++ ){
		m_PNGNetScore[i]->PngImg = m_PngScoreB;
		m_PNGNetScore[i]->Invalidate();
	}
}
//---------------------------------------------------------------------------

void TScoreForm::SetMyScore(int iScore,bool bChange)
{
	int iStar = 0;

	if( iScore<0 )
		iScore = 0;

	if( bChange )
		MyScore = iScore;

	if( iScore==0 ){
		m_MyScore->Visible = false;
	}
	else{
		char buf[8];
		iStar = iScore/100;
		sprintf( buf, "%d", iStar );
		m_MyScore->Caption = buf;
		m_MyScore->Visible = true;
	}

	for( int i=0;i<iStar;i++ ){        //Tag/100 为 1 是空星星 为 0 是满星星
		if( m_PNGMyScore[i]->Tag/100 == 1 ){
			m_PNGMyScore[i]->PngImg = m_PngScoreA;
			m_PNGMyScore[i]->Tag = m_PNGMyScore[i]->Tag%100;
			m_PNGMyScore[i]->Invalidate();
		}
	}
	for( int i=iStar;i<10;i++ ){
		if( m_PNGMyScore[i]->Tag/100 == 0 ){
			m_PNGMyScore[i]->PngImg = m_PngScoreB;
			m_PNGMyScore[i]->Tag = m_PNGMyScore[i]->Tag + 100;
			m_PNGMyScore[i]->Invalidate();
		}
	}
}
//---------------------------------------------------------------------------

void __fastcall TScoreForm::BtnMouseEnter(TObject *Sender)
{
	int number  = ((TPNGButton*)Sender)->Tag%100*100;
	SetMyScore( number,false );
}
//---------------------------------------------------------------------------
void __fastcall TScoreForm::BtnMouseLeave(TObject *Sender)
{
	TPoint pt;
	pt = Mouse->CursorPos;
	TPoint pt2;
	pt2 = ScreenToClient(pt);
	if( pt2.Y<m_PNGMyScore[1]->Top+2 || pt2.Y>m_PNGMyScore[1]->Top + m_PNGMyScore[1]->Height-2
		|| pt2.X < (m_PNGMyScore[0]->Left+2)
		|| pt2.X > (m_PNGMyScore[9]->Left + m_PNGMyScore[9]->Width-2)   ){
		SetMyScore( MyScore );
	}
}
//---------------------------------------------------------------------------

void __fastcall TScoreForm::BtnClick(TObject *Sender)
{
	MyScore = ((TPNGButton*)Sender)->Tag*100;
	m_btnOk->Enabled = true;
}
//---------------------------------------------------------------------------

void __fastcall TScoreForm::FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
	switch( Key ){
		case VK_RETURN:
		case ' ':
			if( m_btnOk->Enabled )
				ModalResult = mrOk;
			break;

		case VK_ESCAPE:
			ModalResult = mrCancel;
			break;
	}
}
//---------------------------------------------------------------------------

void __fastcall TScoreForm::FormPaint(TObject *Sender)
{
	Canvas->Brush->Color = RGB(39, 42, 47);
	Canvas->FillRect(GetClientRect());
	/* 绘制边框 */
	Canvas->Brush->Color = RGB(100, 108, 119);
	TRect ret = this->GetClientRect();
	ret.Top = 5;
	ret.Bottom = 120;
	Canvas->FrameRect(ret);

	Canvas->Brush->Color = RGB(39, 42, 47);
	ret.init( FrameRoute[1].X,FrameRoute[1].Y,FrameRoute[3].X,FrameRoute[3].Y );
	Canvas->FrameRect(ret);

	Canvas->Pen->Color = RGB(100, 108, 119);

	Canvas->MoveTo(FrameRoute[1].X, FrameRoute[1].Y);
	Canvas->LineTo(FrameRoute[2].X, FrameRoute[2].Y);

	Canvas->MoveTo(FrameRoute[2].X-1, FrameRoute[2].Y);
	Canvas->LineTo(FrameRoute[3].X-1, FrameRoute[3].Y);
}
//---------------------------------------------------------------------------

void __fastcall TScoreForm::FormClose(TObject *Sender, TCloseAction &Action)
{
	if(m_PngScoreA){
		delete m_PngScoreA;
		m_PngScoreA = NULL;
	}
	if(m_PngScoreB){
		delete m_PngScoreB;
		m_PngScoreB = NULL;
	}
}
//---------------------------------------------------------------------------

void __fastcall TScoreForm::FormCreate(TObject *Sender)
{
	m_btnOk->FontSize = g_szFont9;
	m_btnCanel->FontSize = g_szFont9;
}
//---------------------------------------------------------------------------

