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

#include <vcl.h>
#pragma hdrstop

#include "PredefineProjectFormUnit.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "GeneralDialogPanel"
#pragma link "SkinTrackBar"
#pragma link "LineEdit"
#pragma link "ComboBoxComponent"
#pragma link "TextButton"
#pragma resource "*.dfm"
TPredefineProjectForm *PredefineProjectForm;

const BYTE u_arrPredefChords[3][2][8][8] = { //预设和弦
			//主歌
			{	{	{ 0, 37, 20, 28,  0, 37, 20, 30 }, // 1 6 4 5 1 6 4 5
					{ 0, 17, 37, 28,  0, 17, 37, 30 }, // 1 3 6 5 1 3 6 5
					{ 0, 37, 20, 17, 37, 20, 28,  0 }, // 1 6 4 3 6 4 5 1
					{ 0, 17, 37, 28, 20, 17,  9, 30 }, // 1 3 6 5 4 3 2 5
					{ 0, 28, 37, 28,  0, 28, 37, 30 }, // 1 5 6 5 1 5 6 5
					{ 0, 37, 28, 20, 17,  9, 28,  0 }, // 1 6 5 4 3 2 5 1
					{ 0,  9, 28,  0,  0,  9, 28,  0 }, // 1 2 5 1 1 2 5 1
					{ 0, 20, 28,  0,  0, 20, 28,  0 }  // 1 4 5 1 1 4 5 1
				},
				{	{37,  9, 17, 37, 37,  9, 17, 37 }, // 6 2 3 6 6 2 3 6
					{37, 28, 20, 17, 37, 28, 20, 19 }, // 6 5 4 3 6 5 4 3
					{37, 28, 17, 37, 37, 28, 17, 37 }, // 6 5 3 6 6 5 3 6
					{37, 20,  9, 17, 37, 20,  9, 19 }, // 6 4 2 3 6 4 2 3
					{37, 17, 37, 28, 37, 17, 37, 30 }, // 6 3 6 5 6 3 6 5
					{37,  9, 20, 28, 37,  9, 20, 30 }, // 6 2 4 5 6 2 4 5
					{37,  9, 28, 37, 37,  9, 28, 37 }, // 6 2 5 6 6 2 5 6
					{37, 20,  9, 37, 37, 20,  9, 37 }  // 6 4 2 6 6 4 2 6
				}
			},
			//副歌
			{	{	{ 0, 37, 20, 17, 37, 20, 28,  0 }, // 1 6 4 3 6 4 5 1
					{ 0, 37, 28, 20, 17,  9, 28,  0 }, // 1 6 5 4 3 2 5 1
					{ 0,  9, 28,  0,  0,  9, 28,  0 }, // 1 2 5 1 1 2 5 1
					{ 0, 20, 28,  0,  0, 20, 28,  0 }, // 1 4 5 1 1 4 5 1
					{20, 28, 37, 37, 20, 28,  0,  0 }, // 4 5 6 6 4 5 1 1
					{ 9, 28,  0, 28, 37,  9, 28,  0 }, // 2 5 1 5 6 2 5 1
					{20, 28, 17, 37, 20,  9, 28,  0 }, // 4 5 3 6 4 2 5 1
					{ 9, 28, 20, 28,  0, 20, 28,  0 }  // 2 5 4 5 1 4 5 1
				},
				{	{37,  9, 17, 37, 37,  9, 17, 37 }, // 6 2 3 6 6 2 3 6
					{37, 28, 17, 37, 37, 28, 17, 37 }, // 6 5 3 6 6 5 3 6
					{37,  9, 28, 37, 37,  9, 28, 37 }, // 6 2 5 6 6 2 5 6
					{37, 20,  9, 37, 37, 20,  9, 37 }, // 6 4 2 6 6 4 2 6
					{ 9, 17, 28, 37,  9, 17, 28, 37 }, // 2 3 5 6 2 3 5 6
					{37,  9, 28, 17, 37,  9, 28, 37 }, // 6 2 5 3 6 2 5 6
					{20, 28,  0,  0, 20, 28, 37, 37 }, // 4 5 1 1 4 5 6 6
					{17, 37,  9, 28, 17, 37, 17, 37 }  // 3 6 2 5 3 6 3 6
				}
			},
			//桥
			{	{	{ 9, 20, 28, 17,  9, 20, 28,  0 }, // 2 4 5 3 2 4 5 1
					{ 9, 28,  0, 28, 37, 28,  0, 30 }, // 2 5 1 5 6 5 1 5
					{20, 17,  9, 28,  0, 17,  9, 30 }, // 4 3 2 5 1 3 2 5
					{17,  9, 37, 17,  9, 28,  0, 30 }, // 3 2 6 3 2 5 1 5
					{20,  0, 20,  0, 20,  0, 20, 30 }, // 4 1 4 1 4 1 4 5
					{20, 28,  0,  0, 20, 28,  0,  0 }, // 4 5 1 1 4 5 1 1
					{20, 17,  9,  0, 20, 17,  9,  0 }, // 4 3 2 1 4 3 2 1
					{28, 20, 28, 20,  0, 28, 20, 30 }  // 5 4 5 4 1 5 4 5
				},
				{	{ 9, 17, 28, 37,  9, 17, 28, 37 }, // 2 3 5 6 2 3 5 6
					{17, 37, 28, 37,  9, 17, 20, 19 }, // 3 6 5 6 2 3 4 3
					{20,  0, 28, 37, 20,  0, 37, 19 }, // 4 1 5 6 4 1 6 3
					{20, 28,  0, 28, 20, 28,  0, 30 }, // 4 5 1 5 4 5 1 5
					{ 9, 17, 37, 17, 20, 37,  0, 37 }, // 2 3 6 3 4 6 1 6
					{ 9, 37,  9, 37,  9, 37,  9, 19 }, // 2 6 2 6 2 6 2 3
					{ 9, 37,  9, 17,  9, 37, 20, 30 }, // 2 6 2 3 2 6 4 5
					{17,  9, 17, 37, 20,  9, 17, 37 }  // 3 2 3 6 4 2 3 6
				}
			}
		};
//---------------------------------------------------------------------------

__fastcall TPredefineProjectForm::TPredefineProjectForm(TComponent* Owner)
	: TForm(Owner)
{
	SetTransparent( this );
	InitVariable();
	InitUI();
}
//---------------------------------------------------------------------------

__fastcall TPredefineProjectForm::~TPredefineProjectForm()
{
	if(mTest) {
		delete mTest;
		mTest = NULL;
	}
}
//---------------------------------------------------------------------------

void __fastcall TPredefineProjectForm::InitUI()
{
    for(int i = 0; i < KEY_NUM; i++) {
		String KeyStr = String(g_strMelodyKey[i]);
		if(KeyStr.Length() == 1) {
            KeyStr = L"  " + KeyStr;
        }
		mKeyComboBox->AddItem(KeyStr);
	}

	mKeyTypeComboBox->AddItem(L"大调");
	mKeyTypeComboBox->AddItem(L"小调");

	mStageComboBox->AddItem(IntToStr(1));
	mStageComboBox->AddItem(IntToStr(2));
	mStageComboBox->AddItem(IntToStr(3));

	mStageSetComboBox->AddItem(IntToStr(1));
	mStageSetComboBox->AddItem(IntToStr(2));

	mTSComboBox->AddItem(String(g_strTimeSignature[0]));
	mTSComboBox->AddItem(String(g_strTimeSignature[1]));
	mTSComboBox->AddItem(String(g_strTimeSignature[2]));
	mTSComboBox->AddItem(String(g_strTimeSignature[3]));

	mTempoComboBox->AddItem(L"慢速");
	mTempoComboBox->AddItem(L"稍慢");
	mTempoComboBox->AddItem(L"中速");
	mTempoComboBox->AddItem(L"稍快");
	mTempoComboBox->AddItem(L"很快");
	mTempoComboBox->AddItem(L"自定义");
	mTempoComboBox->DefItemIndex = 2;
}
//---------------------------------------------------------------------------
void __fastcall TPredefineProjectForm::InitVariable()
{
	m_Ts		= g_iMelodyTimeSignature;
	m_Tempo		= g_nMelodyTempo;
	m_Key		= 0;
	m_KeyType	= 0;
	m_RavStage	= 2;

	mLordSong 	= rand() % 8;
	mBridge 	= rand() % 8;
	mRefrain 	= rand() % 8;

	mPaintBoxDefColor = TColor(0x4D4641);
	mPaintBoxFocColor = TColor(0xAA9C91);

	mPaintBoxFlag = true;
}
//---------------------------------------------------------------------------


void __fastcall TPredefineProjectForm::DoShowPreChordPaint(TObject *Sender)
{
	TPaintBox *PaintBox = (TPaintBox *)Sender;

	TRect cRect = PaintBox->ClientRect;
	PaintBox->Canvas->Brush->Color = mPaintBoxDefColor;

	if(PaintBox->Canvas->Pen->Color == mPaintBoxFocColor)
		PaintBox->Canvas->RoundRect(cRect, 5, 5);
	else
		PaintBox->Canvas->FillRect(cRect);

	cRect.Right--;
	cRect.Left++;
	cRect.Top++;
	cRect.Bottom--;

	PaintBox->Canvas->Brush->Color = TColor(0xC4B4A8);
	PaintBox->Canvas->Pen->Color = TColor(0x302E2C);
	PaintBox->Canvas->RoundRect(cRect, 5, 5);

	PaintBox->Canvas->Pen->Color = mPaintBoxDefColor;

	int Preset = 0;

	int Stage = mStageSetComboBox->DefItemIndex;

	if(mStageComboBox->DefItemIndex == 2) {
		if(mStageSetComboBox->DefItemIndex == 0) {
			Preset = mLordSong;
		}
		else if(mStageSetComboBox->DefItemIndex == 1) {
			Preset = mBridge;
			Stage = 2;
		}
		else if(mStageSetComboBox->DefItemIndex == 2){
			Preset = mRefrain;
			Stage = 1;
		}
	}
	else {
		if(mStageSetComboBox->DefItemIndex == 0) {
			Preset = mLordSong;
		}
		else if(mStageSetComboBox->DefItemIndex == 1) {
			Preset = mRefrain;
		}
	}

	BYTE *Chord = (BYTE *)u_arrPredefChords[Stage][m_KeyType][Preset];

	for(int i = 0; i < 8; i++) {
		TRect Rect = mChordRect[i];
		int ch =  Chord[i];

		int type = ch & 0x03;
		int key = (ch >> 2) + m_Key;

		if( key>11 ) key -= 12;

		DrawChord(PaintBox->Canvas, Rect, key, type);

		if(i != 7) {
			PaintBox->Canvas->MoveTo(Rect.Right - 1, Rect.Top);
			PaintBox->Canvas->LineTo(Rect.Right - 1, Rect.Bottom);
		}
	}
}
//---------------------------------------------------------------------------

void TPredefineProjectForm::DrawChord(TCanvas *Canvas, TRect Rect, int iKey, int iType)
{
	int iMainKey, iAjustKey; //调级数、升降
	TPrefineProjectSetItem::GetChordDisplayKey(iKey, m_Key, iMainKey, iAjustKey);
    TPrefineProjectSetItem::UniDrawChord(Canvas, Rect, iMainKey, iAjustKey, iType);
}
//---------------------------------------------------------------------------

void __fastcall TPredefineProjectForm::FormCreate(TObject *Sender)
{
	GeneralDialogPanel->Font->Size = g_szFont9;
	mTempoLineEdit->SetTextFont(g_strTxtFontName, g_szFont9);
	mCancelButton->FontSize = g_szFont9;
	mOkButton->FontSize = g_szFont9;

	mKeyComboBox->SetFont(g_strTxtFontName, g_szFont9);
	mKeyTypeComboBox->SetFont(g_strTxtFontName, g_szFont9);
	mStageComboBox->SetFont(g_strTxtFontName, g_szFont9);
	mTSComboBox->SetFont(g_strTxtFontName, g_szFont9);
	mTempoComboBox->SetFont(g_strTxtFontName, g_szFont9);
	mStageSetComboBox->SetFont(g_strTxtFontName, g_szFont9);

	int ChordWidth = mChordsBox->Width / 8;

	int ChordLeft;
	int ChordTop = 1;
	int ChordRight;
	int ChordBottom = 27;

	for(int i = 0; i < 8; i++) {
		ChordLeft = i * ChordWidth;
		ChordRight = ChordLeft + ChordWidth;
		mChordRect[i] = TRect(ChordLeft, ChordTop, ChordRight, ChordBottom);
	}

	mChordRect[0].Left++;
	mChordRect[7].Right--;

	mKeyComboBox->DefItemIndex = 0;
	mKeyTypeComboBox->DefItemIndex = 0;
	mStageComboBox->DefItemIndex = 1;
	mStageSetComboBox->DefItemIndex = 0;
	mTSComboBox->DefItemIndex = m_Ts;
	mTempoLineEdit->Text = IntToStr(m_Tempo);
}
//---------------------------------------------------------------------------

void __fastcall TPredefineProjectForm::DoKeyChange(TComboBoxItem *Sender, int Index)

{
	m_Key = Index;
	mChordsBox->Repaint();
}
//---------------------------------------------------------------------------

void __fastcall TPredefineProjectForm::DoKeyTypeChange(TComboBoxItem *Sender, int Index)

{
	m_KeyType = Index;
	mChordsBox->Repaint();
}
//---------------------------------------------------------------------------

void __fastcall TPredefineProjectForm::StageSetChange(TComboBoxItem *Sender, int Index)
{
	mChordsBox->Repaint();
}
//---------------------------------------------------------------------------

void __fastcall TPredefineProjectForm::DoStageCountChange(TComboBoxItem *Sender, int Index)

{
	mStageSetComboBox->Clear();
	for(int i = 0; i <= Index; i++) {
		mStageSetComboBox->AddItem(IntToStr(i + 1));
	}
	mStageSetComboBox->DefItemIndex = 0;
	StageSetChange(NULL, 0);
	m_RavStage = Index + 1;
}
//---------------------------------------------------------------------------

void __fastcall TPredefineProjectForm::DoPaintBoxMouseEnter(TObject *Sender)
{
//	TPaintBox *PaintBox = (TPaintBox *)Sender;
//	PaintBox->Canvas->Pen->Color = mPaintBoxFocColor;
//	PaintBox->Repaint();

	mPresetMouseOver = true;
}
//---------------------------------------------------------------------------

void __fastcall TPredefineProjectForm::DoPaintBoxMouseLeave(TObject *Sender)
{
//	if(!mIsPreSetShow) {
//		TPaintBox *PaintBox = (TPaintBox *)Sender;
//		PaintBox->Canvas->Pen->Color = mPaintBoxDefColor;
//		PaintBox->Repaint();
//	}

	mPresetMouseOver = false;
}
//---------------------------------------------------------------------------
void __fastcall TPredefineProjectForm::DoPaintBoxMouseDown(TObject *Sender, TMouseButton Button,
		  TShiftState Shift, int X, int Y)
{
	if(Button == 0) {
		if(mPaintBoxFlag) {
			if(mTest) {
				delete mTest;
				mTest = NULL;
			}

			mTest = new TPrefineProjectSet(NULL);
			mTest->Parent = this;

			int Preset = 0;

			int Stage = mStageSetComboBox->DefItemIndex;

			if(mStageComboBox->DefItemIndex == 2) {
				if(mStageSetComboBox->DefItemIndex == 0) {
					Preset = mLordSong;
				}
				else if(mStageSetComboBox->DefItemIndex == 1) {
					Preset = mBridge;
					Stage = 2;
				}
				else if(mStageSetComboBox->DefItemIndex == 2){
					Preset = mRefrain;
					Stage = 1;
				}
			}
			else {
				if(mStageSetComboBox->DefItemIndex == 0) {
					Preset = mLordSong;
				}
				else if(mStageSetComboBox->DefItemIndex == 1) {
					Preset = mRefrain;
				}
			}

			for(int i = 0; i < 8; i++) {
				BYTE *Chord = (BYTE *)u_arrPredefChords[Stage][m_KeyType][i];
				TPrefineProjectSetItem *Item = new TPrefineProjectSetItem(mTest);
				Item->Key = m_Key;
				Item->Index = i;
				memcpy(Item->PredefChord, Chord, 8 * sizeof(BYTE));
				mTest->AddItem(Item);
			}

			mTest->SelectedIndex = Preset;

			TPoint posClient(0, mChordsBox->Height);

			mTest->OnShow = DoPresetChordShow;
			mTest->OnHide = DoPresetChordHide;

			TPoint pos = mChordsBox->ClientToScreen(posClient);

			mTest->Top = pos.Y;
			mTest->Left = pos.X + 1;
			mTest->Width = mChordsBox->Width - 2;
			mTest->Height = 200;
			mTest->Show();
		}

		mPaintBoxFlag = !mPaintBoxFlag;
    }
}
//---------------------------------------------------------------------------

void __fastcall TPredefineProjectForm::DoPresetChordShow(TObject *Sender)
{
	mChordsBox->Canvas->Pen->Color == mPaintBoxFocColor;

	mIsPreSetShow = true;
}
//---------------------------------------------------------------------------

void __fastcall TPredefineProjectForm::DoPresetChordHide(TObject *Sender)
{
	if(GetActiveWindow() != Handle) {
		mPaintBoxFlag = true;
	}

	if(!mPresetMouseOver)
		mPaintBoxFlag = true;

	mChordsBox->Canvas->Pen->Color == mPaintBoxDefColor;

	if(mStageComboBox->DefItemIndex == 2) {
		if(mStageSetComboBox->DefItemIndex == 0) {
			mLordSong = mTest->SelectedIndex;
		}
		else if(mStageSetComboBox->DefItemIndex == 1) {
			mBridge = mTest->SelectedIndex;
		}
		else if(mStageSetComboBox->DefItemIndex == 2){
			mRefrain = mTest->SelectedIndex;
		}
	}
	else {
		if(mStageSetComboBox->DefItemIndex == 0) {
			mLordSong = mTest->SelectedIndex;
		}
		else if(mStageSetComboBox->DefItemIndex == 1) {
			mRefrain = mTest->SelectedIndex;
		}
	}

	mIsPreSetShow = false;

	mChordsBox->Repaint();
}
//---------------------------------------------------------------------------

void __fastcall TPredefineProjectForm::DoTsChange(TComboBoxItem *Sender, int Index)
{
	m_Ts = Index;
}
//---------------------------------------------------------------------------

void __fastcall TPredefineProjectForm::DoTempoComboChange(TComboBoxItem *Sender, int Index)
{
	if(Index == 0) {
		mTempoLineEdit->Text = IntToStr(55);
	}
	else if(Index == 1){
		mTempoLineEdit->Text = IntToStr(65);
	}
	else if(Index == 2) {
		mTempoLineEdit->Text = IntToStr(80);
	}
	else if(Index == 3) {
		mTempoLineEdit->Text = IntToStr(110);
	}
	else if(Index == 4){
		mTempoLineEdit->Text = IntToStr(120);
	}
}
//---------------------------------------------------------------------------

void __fastcall TPredefineProjectForm::DoTempoEditChange(TObject *Sender)
{
	if(mTempoLineEdit->Text.IsEmpty())
//		mTempoLineEdit->Text = IntToStr(0);
		return;

	int Value = StrToInt(mTempoLineEdit->Text);

	if(Value == 55) {
		mTempoComboBox->DefItemIndex = 0;
	}
	else if(Value == 65){
		mTempoComboBox->DefItemIndex = 1;
	}
	else if(Value == 80) {
		mTempoComboBox->DefItemIndex = 2;
	}
	else if(Value == 110) {
		mTempoComboBox->DefItemIndex = 3;
	}
	else if(Value == 120) {
		mTempoComboBox->DefItemIndex = 4;
	}
	else {
		mTempoComboBox->DefItemIndex = 5;
	}

	m_Tempo = Value;
}
//---------------------------------------------------------------------------

void __fastcall TPredefineProjectForm::mTempoUpDownClick(TObject *Sender, TUDBtnType Button)
{
	int Value = g_nMelodyTempo;

	if(!mTempoLineEdit->Text.IsEmpty())
		Value = StrToInt(mTempoLineEdit->Text);

	if(Button == btNext)
		Value += 1;
	else
		Value -= 1;

	if(Value < 50) {
        Value = 50;
	}

	if(Value > 200) {
        Value = 200;
	}

	mTempoLineEdit->Text = IntToStr(Value);
}
//---------------------------------------------------------------------------

void __fastcall TPredefineProjectForm::DoGeneraDialogPanelMouseDown(TObject *Sender,
          TMouseButton Button, TShiftState Shift, int X, int Y)
{
	GeneralDialogPanel->SetFocus();
}
//---------------------------------------------------------------------------
int __fastcall TPredefineProjectForm::ShowModal()
{
	int ret = TForm::ShowModal();
	m_Preset[0] = mLordSong;

	if(mStageComboBox->DefItemIndex == 2) {
		m_Preset[1] = mBridge;
		m_Preset[2] = mRefrain;
	}
	else {
		m_Preset[1] = mRefrain;
		m_Preset[2] = 0;
	}

	return ret;
}

void __fastcall TPredefineProjectForm::FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
	switch( Key ){
	case VK_RETURN:
		ModalResult = mrOk;
		break;

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

