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

#include <vcl.h>
#pragma hdrstop

#include "SwitchTextureForm.h"
#include "ListViewText.h"
#include "GlobalUnit.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
//---------------------------------------------------------------------------
#pragma link "ComboBoxComponent"
#pragma link "PNGButton"
#pragma link "TextButton"
#pragma link "CKBox"
#pragma link "GeneralDialogPanel"
#pragma resource "*.dfm"
TSwitchTextureForm *SwitchPartternForm;
//---------------------------------------------------------------------------
#define FramColor			RGB(123, 123, 123)
#define BkClolor  			RGB(39, 42, 47)
#define FontClolor  		TColor(0xBDAA9C)
#define CapClolor  			RGB(29, 32, 37)

#define HEIGHT_TYPE   90
#define HEIGHT_PROG   90
#define HEIGHT_TITTLE 21
#define BORDER_LENGTH 1

#define TYPE_WIDTH          140

#define LSTDEPART      16

//gm 音色分类
char PROG_TYPE[75] = {
5, 7, //钢琴  2
10, 15, //音阶打击乐器  2
20, 23, //风琴  2
25, 27, 28, 30, 31,//吉他  5
39, //贝司  1
40, 41, 42, 43, 45, 46, 47,//弦乐 7
51, 54, 55, //合奏/合唱 3
56, 57, 58, 59, 60, 63,//铜管 6
67, 68, 69, 70, 71,//簧管  5
72, 73, 74, 75, 76, 77, 78, 79,//木管  8
87, //合成主音  1
95, //合成背景  1
96, 97, 98, 99, 100, 101, 102, 103, //合成效果   8
104, 105, 106, 107, 108, 109, 110, 111,//民间乐器 8
112, 113, 114, 115, 116, 117, 118, 119,//击乐器  8
120, 121, 122, 123, 124, 125, 126, 127 //声音效果 8
};
//2+2+2+5+1 +7+3+ 6+ 5+ 8+1+1+ 8*4= 43+32 = 75

TPngImage *pngType    = NULL;
TPngImage *pngCollect = NULL;

//---------------------------------------------------------------------------
void LoadImgParttern()
{
	if( !pngType )    pngType   = new TPngImage();
	if( !pngCollect ) pngCollect = new TPngImage();

	pngType->LoadFromResourceName(int(HInstance),     L"imgProg");
	pngCollect->LoadFromResourceName(int(HInstance),  L"imgCollect");
}
//---------------------------------------------------------------------------

void ReleaseImgParttern()
{
	SAFE_DELETE(pngType);
	SAFE_DELETE(pngCollect);
}
//---------------------------------------------------------------------------

FONTPRESET* InsGetDrums(int &num, int &index, char iProg=-1)
{
	FONTINFO* pFI = g_lstFonts[g_idFont];
	num = pFI->nDrumCount;
	FONTPRESET *pSet = pFI->aDrums;
	if( -1!=iProg ) {
		for( int i=0; i<num; i++ ){
			if( pSet[i].id==iProg ) {
				index = i;
				return NULL;
			}
		}
	}

	return pSet;
}
//---------------------------------------------------------------------------

int InsSearchType(char iProg, vector<char>& vcProg, bool bSameType=false, bool bChan9=false )
{
	vcProg.clear();
	if( /*!bChan9 &&*/ iProg>=0 ){
		int iStart = iProg, iEnd = iProg;
		if( bSameType ){
			if( bChan9 ){
				iStart = 0;
				iEnd = 127;
			}
			else{
				iStart = 0;
				for( int index=0; index<75; index++ ) {
					if( iProg<=PROG_TYPE[index] ) {
						iEnd   = PROG_TYPE[index];
						break;
					}
					else{
						iStart = PROG_TYPE[index]+1;
					}
				}
			}
		}

		vcProg.push_back( iStart );
		vcProg.push_back( iEnd );
	}
}
//---------------------------------------------------------------------------
/////////////////////////////////////////////////////////////////////////////
//---------------------------------------------------------------------------

__fastcall TSwitchTextureForm::TSwitchTextureForm(TComponent* Owner,
		char iSegType,    char iProgOri,
		char iFontCur, char iProgCur,
		TPROSTYLE *pStyleOri, char chanOri, char segNoOri,
		TStageList* lstStages, int iPlayBar )
 : TForm(Owner)
{
	m_bSubProg	= iProgCur>=0;
	m_bChan9    = chanOri==9;

	m_cSegType  = iSegType;

	m_cFontCur  = iFontCur;
	m_cProgCur  = iProgCur;

	m_cProgOri  = m_cProgOriBac = iProgOri;

	m_pStyleOri = pStyleOri;
	m_cChanOri  = chanOri;
	m_cSegNoOri = segNoOri;

	m_lstStages  = lstStages;
	m_iPlayBar = iPlayBar;

	m_InsIndex  = m_cProgOriType = -1;
	m_bSameType = m_bCollected   = false;

	m_cSetFont = m_cSetProg = m_cSetProgOri = m_cSetSegType
							 = m_cSetSegNo = m_cSetchan = -1;
	m_bSetCur   = true;
	m_pSetStyle = NULL;

    m_btnCol = m_btnSameType = NULL;
	m_LstParttern = m_LstProgType = m_LstProg = NULL;

	LoadImgParttern();

	if( !m_bSubProg ) {
		m_BKGPanel->Caption = L"乐器织体添加";
		m_lbIntro->Caption = L"所选织体仅添加至相应类型的段落";
		m_ckPlay->Enabled = false;
		m_ckReplaceProg->Enabled = false;
		m_ckPlay->Font->Color = RGB(93, 93, 93);
		m_ckReplaceProg->Font->Color = RGB(93, 93, 93);
		m_lbOri->Font->Color = RGB(93, 93, 93);
	}

	InitListPanel();

	m_btnInit->Visible = m_cChanOri>=0;
	m_cbDrum->Visible = (iSegType & 0x80) && m_cChanOri<0;
	m_lbIntro->Visible = lstStages && lstStages->size()>1;//iSegType==0x03;

	SetTransparent( this );
}
//---------------------------------------------------------------------------
__fastcall TSwitchTextureForm::~TSwitchTextureForm()
{
	f_MM_Stop();

	ReleasePt();

	SAFE_DELETE(m_LstParttern)
	SAFE_DELETE(m_LstProgType)
	SAFE_DELETE(m_LstProg)
	SAFE_DELETE(m_btnCol)
	SAFE_DELETE(m_btnSameType)

	ReleaseImgParttern();
}
//---------------------------------------------------------------------------

void __fastcall TSwitchTextureForm::InitListPanel()
{
	f_MM_Stop();

	m_cProgOri = m_cProgOriBac;

	if( -1==m_cProgOri ) {
		m_cProgOriType = -1;
		m_InsIndex = -1;
	}else {
		if( m_bChan9 ) {
			int num, index;
			InsGetDrums(num, index, m_cProgOri);
			m_cProgOriType = 0;
			m_InsIndex     = index;
		}
		else {
			m_cProgOriType = m_cProgOri/8;
			m_InsIndex     = m_cProgOri%8;
        }
	}

	InitTypePanel();
	InitProgPanel();

//	m_shapeOri->Left   = m_LstProg->Left;
//	m_shapeOri->Width  = m_LstProg->Width;
//	m_shapeOri->Height = 25;
//
//	m_shapecheck->Left   = m_LstProg->Left;
//	m_shapecheck->Width  = m_LstProg->Width;
//	m_shapecheck->Height = 34; //m_ckPlay->Height + 4;
//
//	m_lbOri->Left         = m_shapeOri->Left+(m_shapeOri->Width-m_lbOri->Width)/2;
//	m_ckPlay->Left        = m_shapecheck->Left+(m_shapecheck->Width/2-m_ckPlay->Width)/2;
//	m_ckReplaceProg->Left = m_shapecheck->Left+m_shapecheck->Width/2+(m_shapecheck->Width/2-m_ckReplaceProg->Width)/2;
//
//	m_shapeOri->Top   = m_LstProg->Top+m_LstProg->Height+LSTDEPART;
//	m_shapecheck->Top = m_shapeOri->Top+m_shapeOri->Height-1;
//	m_lbOri->Top      = m_shapeOri->Top+(m_shapeOri->Height-m_lbOri->Height)/2;
//	m_ckPlay->Top     = m_shapecheck->Top+(m_shapecheck->Height-m_ckPlay->Height)/2;
//	m_ckReplaceProg->Top = m_shapecheck->Top+(m_shapecheck->Height-m_ckReplaceProg->Height)/2;


	InitPtPanel();

/*	if( !m_bSubProg ) {
		m_ckPlay->Enabled = false;
		m_ckReplaceProg->Enabled = false;
		m_ckPlay->Font->Color = RGB(93, 93, 93);
		m_ckReplaceProg->Font->Color = RGB(93, 93, 93);
		m_lbOri->Font->Color = RGB(93, 93, 93);
	}
*/
}
//---------------------------------------------------------------------------
void __fastcall TSwitchTextureForm::DoDrawBKG(TPanelDB *Sender, TCanvas *pCanvas, TRect rtBKG)
{
	pCanvas->Brush->Color = BkClolor;
	pCanvas->FillRect(rtBKG);

	if( Sender==(TPanelDB *)m_LstParttern ) {

		int iLeft, iTop, iWidth, iHeight;

		iWidth  = rtBKG.Width();
		iHeight = rtBKG.Top+rtBKG.Height();
		iTop    = rtBKG.Top;
		pCanvas->Pen->Color = FramColor;

		iLeft = LEFT_CAPTION+WIDTH_PROG(Sender->Width)+WIDTH_LINE/2 ;
		pCanvas->MoveTo(iLeft, iTop);
		pCanvas->LineTo(iLeft, iHeight);

		iLeft = LEFT_STYLE(Sender->Width)+WIDTH_STYLE(Sender->Width)+WIDTH_LINE/2;
		pCanvas->MoveTo(iLeft, iTop);
		pCanvas->LineTo(iLeft, iHeight);

		iLeft = LEFT_SEG(Sender->Width)+WIDTH_SEG(Sender->Width)+WIDTH_LINE/2;
		pCanvas->MoveTo(iLeft, iTop);
		pCanvas->LineTo(iLeft, iHeight);
	}

	pCanvas->Pen->Color = FramColor;
	pCanvas->MoveTo(0, 0);    //上
	pCanvas->LineTo(Sender->Width, 0);
	pCanvas->MoveTo(0, Sender->Height-1); //下
	pCanvas->LineTo(Sender->Width, Sender->Height-1);
	pCanvas->MoveTo(0, 0); //左
	pCanvas->LineTo(0, Sender->Height);
	pCanvas->MoveTo(Sender->Width-1, 0); //右
	pCanvas->LineTo(Sender->Width-1, Sender->Height);
}
//---------------------------------------------------------------------------
int  __fastcall TSwitchTextureForm::DoTitle(TTableView *Sender)
{
	TPanelDB *pTitle = new TPanelDB(Sender);
	pTitle->OnDrawBKG = DoDrawTitle;

//	if( Sender==(TPanelDB *)m_LstParttern )
//		pTitle->SetBounds(0, 0, Sender->Width, HEIGHT_TITTLE-1);
//	else
		pTitle->SetBounds(0, 0, Sender->Width, 1);

	if( Sender==(TTableView *)m_LstParttern ) {
		SAFE_DELETE(m_btnCol)
		SAFE_DELETE(m_btnSameType)
		if(!m_btnCol) {
			m_btnCol = new TPanelDB(this);
			m_btnCol->Parent = this;
			m_btnCol->Width = pngCollect->Width/3;
			m_btnCol->Height = pngCollect->Height;
			m_btnCol->Left = m_ShapeTexture->Left + m_ShapeTexture->Width - m_btnCol->Width - 14;
			m_btnCol->Top  = m_ShapeTexture->Top + (m_ShapeTexture->Height - m_btnCol->Height) / 2;
			m_btnCol->Color = CapClolor;
			m_btnCol->ShowHint = true;
			m_btnCol->Hint     = L"点击显示收藏的相应乐器的织体";
			m_btnCol->OnPaintBKG = DoDrawBtn;
			m_btnCol->BringToFront();
		}

		if(!m_btnSameType) {
			m_btnSameType = new TPanelDB(this);
			m_btnSameType->Parent = this;
			m_btnSameType->Width = pngType->Width/3;
			m_btnSameType->Height = pngType->Height;
			m_btnSameType->Left = m_btnCol->Left - m_btnCol->Width - 7;
			m_btnSameType->Top  = m_btnCol->Top;
			m_btnSameType->Color = CapClolor;
			m_btnSameType->ShowHint = true;
			m_btnSameType->Hint     = L"点击显示相似乐器的织体";
			m_btnSameType->OnPaintBKG = DoDrawBtn;
			m_btnCol->BringToFront();
		}

		m_btnSameType->OnClick = DoPtChanged;
		m_btnCol->OnClick      = DoPtChanged;
		m_btnSameType->Visible = m_cProgOri>=0;
		m_btnSameType->Visible = !m_bChan9 && m_cProgOri>=0;
	}

	return (int)pTitle;
}
//---------------------------------------------------------------------------
void __fastcall TSwitchTextureForm::DoDrawTitle(TPanelDB *Sender, TCanvas *pCanvas, TRect rtBKG)
{
	int iLeft, iTop, iWidth, iHeight;
	TRect rtTmp;
	TSize sizeFont;
	String strCaption;
	TPanelDB *pBKG = (TPanelDB *)Sender->Parent;
//	if( pBKG==m_LstProgType ) strCaption = L"类别";
//	else if( pBKG==m_LstProg ) strCaption = m_bChan9 ? L"鼓组" : L"乐器";
//	else if( pBKG==m_LstParttern ) strCaption = L"织体";

	iLeft = rtBKG.Left;
	iTop  = rtBKG.Top;
	iWidth = rtBKG.Width();
	iHeight = rtBKG.Height();
	rtTmp = TRect(iLeft, iTop, iLeft+iWidth, iTop+iHeight);//(pBKG==m_LstParttern ? iHeight/2 : iHeight) );

	pCanvas->Brush->Color = CapClolor;
	pCanvas->FillRect(rtTmp);

//	sizeFont = pCanvas->TextExtent(strCaption);
//	iTop = pBKG==m_LstParttern ? (iHeight/2 - sizeFont.Height)/2
//								: (iHeight - sizeFont.Height)/2;
//	iLeft = (iWidth-sizeFont.Width)/2;
//	pCanvas->Brush->Style = bsClear;
//	pCanvas->Font->Color = FontClolor;
//	pCanvas->TextOutW(iLeft, iTop, strCaption);

//	if( pBKG==m_LstParttern ) {
//		iLeft = rtBKG.Left;
//		iTop  = iHeight/2;
//		iHeight = iHeight/2;
//		iWidth = pBKG->Width;
//
//		rtTmp = TRect(iLeft, iTop, iLeft+iWidth, iTop+iHeight );
//
//		pCanvas->Brush->Color = BkClolor;
//		pCanvas->FillRect(rtTmp);
//
//		pCanvas->Brush->Color = FramColor;
//		pCanvas->Pen->Color   = FramColor;
//		pCanvas->FrameRect(rtTmp);
//
//		pCanvas->Brush->Style = bsClear;
//		pCanvas->Font->Color = FontClolor;
//
////		pCanvas->Font->Size = 8;
//		strCaption = L"乐器名";
//		sizeFont = pCanvas->TextExtent(strCaption);
//		iTop     = iHeight+(iHeight - sizeFont.Height)/2;
//
//		iLeft = LEFT_CAPTION+(WIDTH_PROG(iWidth)-sizeFont.Width)/2+5;
//		pCanvas->TextOutW(iLeft, iTop, strCaption);
//
//		strCaption = L"风格";
//		iLeft = LEFT_STYLE(iWidth)+(WIDTH_STYLE(iWidth)-sizeFont.Width)/2+5;
//		pCanvas->TextOutW(iLeft, iTop, strCaption);
//
//		strCaption = L"片段";
//		iLeft = LEFT_SEG(iWidth)+(WIDTH_SEG(iWidth)-sizeFont.Width)/2+5;
//		pCanvas->TextOutW(iLeft, iTop, strCaption);
//
//		strCaption = L"收藏";
//		iLeft = LEFT_COL(iWidth);
//		iLeft = iLeft + (iWidth-iLeft-sizeFont.Width)/2;
//		pCanvas->TextOutW(iLeft, iTop, strCaption);
//	}

	pCanvas->Brush->Color = FramColor;
	pCanvas->Pen->Color   = FramColor;
	pCanvas->FrameRect(rtBKG);
}
//---------------------------------------------------------------------------
int  __fastcall TSwitchTextureForm::DoBottom(TTableView *Sender)
{
	TPanelDB *pBottom = new TPanelDB(Sender);
	pBottom->OnDrawBKG = DoDrawBottom;

	pBottom->SetBounds(0, Sender->Height-1, Sender->Width, 1);

	return (int)pBottom;
}
//---------------------------------------------------------------------------
void __fastcall TSwitchTextureForm::DoDrawBottom(TPanelDB *Sender, TCanvas *pCanvas, TRect rtBKG)
{
	pCanvas->Brush->Color = FramColor;
	pCanvas->FillRect(rtBKG);
}
//---------------------------------------------------------------------------
void __fastcall TSwitchTextureForm::DoDrawBtn(TPanelDB *Sender, TCanvas *pCanvas, TRect rtBKG)
{
	pCanvas->Brush->Color = CapClolor;
	pCanvas->FillRect(rtBKG);

	if( Sender==m_btnCol ) {

		int iWidth  = pngCollect->Width/3;
		int nTrans  = !m_bCollected ? 0: (-2*iWidth);

		pCanvas->Draw(rtBKG.Left+nTrans, rtBKG.Top, pngCollect);

	}else if( Sender==m_btnSameType ) {

		int iWidth  = pngType->Width/3;
		int nTrans  = !m_bSameType? 0: (-2*iWidth);

		pCanvas->Draw(rtBKG.Left+nTrans, rtBKG.Top, pngType);
	}
}
//---------------------------------------------------------------------------
void __fastcall TSwitchTextureForm::InitTypePanel()
{
	if( !m_LstProgType ) {
		m_LstProgType         = new TListViewText(this);
		m_LstProgType->Parent = m_panType;// this;
		m_LstProgType->Left   = 0;
		m_LstProgType->Top    = 0;
		m_LstProgType->Width  = m_panType->Width;
		m_LstProgType->Height = m_panType->Height;
		m_LstProgType->OnClick= DoProgChanged;
		m_LstProgType->OnDrawBKG  = DoDrawBKG;
		m_LstProgType->OnTitle    = DoTitle;
		m_LstProgType->OnBottom   = DoBottom;
		m_LstProgType->Border     = 1;
		m_LstProgType->SetTextFontSize(g_szFont9);

		m_LstProgType->OnMouseEnter = DoPanelFocus;
	}

	m_VCType.clear();
	if( m_bChan9 ) {
		m_VCType.push_back("GM鼓");
	}
	else {
		for(int i=0; i<16; i++)
			m_VCType.push_back(GM_GROUP[i]);
	}
	((TListViewText *)m_LstProgType)->m_VcText = &m_VCType;
	if( -1!=m_cProgOriType )
		m_LstProgType->IndSel = m_cProgOriType;
	m_LstProgType->ReloadData(m_LstProgType->IndSel);
}
//---------------------------------------------------------------------------
void __fastcall TSwitchTextureForm::InitProgPanel()
{
	if( !m_LstProg ) {
		m_LstProg         = new TListViewText(this);
		m_LstProg->Parent = m_panProg;// this;
		m_LstProg->Left   = 0;
		m_LstProg->Top    = 0; //m_LstProgType->Top + m_LstProgType->Height + LSTDEPART;
		m_LstProg->Height = m_panProg->Height;// HEIGHT_TYPE+HEIGHT_TITTLE;
		m_LstProg->Width  = m_panProg->Width;// TYPE_WIDTH;
		m_LstProg->OnClick= DoProgChanged;
		m_LstProg->OnDrawBKG  = DoDrawBKG;
		m_LstProg->OnTitle    = DoTitle;
		m_LstProg->OnBottom   = DoBottom;
		m_LstProg->Border    = 1;
		m_LstProg->OnMouseEnter = DoPanelFocus;
		m_LstProg->SetTextFontSize(g_szFont9);
	}

	m_VCProg.clear();
	if( -1!=m_cProgOriType ){
		if( m_bChan9 ) {
			int num, index;
			FONTPRESET* pSet = InsGetDrums(num, index);
			for( int i=0; i<num; i++ ){
				m_VCProg.push_back(pSet[i].text);
			}
		}
		else {
			for(int i=0; i<8; i++)
				m_VCProg.push_back(GM_INSTRUMENT[m_cProgOriType*8+i]);
		}
	}

	((TListViewText *)m_LstProg)->m_VcText = &m_VCProg;
	if( -1!=m_InsIndex )
		m_LstProg->IndSel = m_InsIndex;
	m_LstProg->ReloadData(m_LstProg->IndSel);
}
//---------------------------------------------------------------------------
void __fastcall TSwitchTextureForm::InitPtPanel()
{
	m_VCProgSearch.clear();
	ReleasePt();

	if( -1!=m_cProgOri ) {
		InsSearchType(m_cProgOri, m_VCProgSearch, m_bSameType, m_bChan9);

		InitPtStyle( g_aOriginStyles );
		InitPtStyle( g_aShareStyles );
		InitPtStyle( g_aLocalStyles );
	}

	if( !m_LstParttern ) {

		m_LstParttern            = new TListViewPattern(this, m_bChan9, m_lstStages, m_iPlayBar);
		m_LstParttern->Parent    = m_panTexture;//this;
		m_LstParttern->Left      = 0;//m_LstProgType->Left+m_LstProgType->Width+20;
		m_LstParttern->Top       = 0;//m_LstProgType->Top;
		m_LstParttern->Height    = m_panTexture->Height;// m_shapecheck->Top+m_shapecheck->Height-m_LstProgType->Top;
		m_LstParttern->Width     = m_panTexture->Width; // m_btnCancel->Left+m_btnCancel->Width-m_LstParttern->Left;
		m_LstParttern->Color     = BkClolor;
		m_LstParttern->OnDrawBKG = DoDrawBKG;
		m_LstParttern->OnTitle   = DoTitle;
		m_LstParttern->OnBottom  = DoBottom;
		m_LstParttern->Border    = 1;
		m_LstParttern->SetTextFontSize(g_szFont9);

		m_LstParttern->OnMouseEnter = DoPanelFocus;
		((TListViewPattern*)m_LstParttern)->OnTextureChanged = OnTextureChanged;
	}

	int index = -1;
	if( m_pStyleOri ){
		for( int i=0; i<m_VCPtInfo.size(); i++ ){
			TTextureInfo *pPtInfo = m_VCPtInfo[i];

			if( pPtInfo->idLoc==m_pStyleOri->idLoc && pPtInfo->idStyle==m_pStyleOri->idStyle
							&& ((1<<pPtInfo->segType) & m_cSegType)
							&& pPtInfo->segNo==m_cSegNoOri && pPtInfo->prog==m_cProgOriBac ){
				if( m_pStyleOri->idLoc==0 ) {
					if( m_pStyleOri->pOrigin->detail.tracks[pPtInfo->segType][m_cSegNoOri][pPtInfo->chNo].no==m_cChanOri ){
						index = i;
						break;
					}
				}
				else if( m_pStyleOri->idLoc!=0 && m_pStyleOri->idLoc!=1 ){
					if( m_pStyleOri->pLocal->trkRef[pPtInfo->segType][m_cSegNoOri][pPtInfo->chNo].chan==m_cChanOri ){
						index = i;
						break;
					}
				}
			}
		}
	}

	m_LstParttern->IndSel = index;
	((TListViewPattern *)m_LstParttern)->m_LstParttern = &m_VCPtInfo;
	((TListViewPattern *)m_LstParttern)->m_pPtInfo = NULL;
	m_LstParttern->ReloadData(index);

	if( m_btnInit->Visible )
		m_btnInit->Enabled = index>=0;
	m_btnTry->Enabled = false;
}
//---------------------------------------------------------------------------
void __fastcall TSwitchTextureForm::ReleasePt()
{
	for( int i=0; i<m_VCPtInfo.size(); i++ ){
		delete m_VCPtInfo[i];
	}
    m_VCPtInfo.clear();
}
//---------------------------------------------------------------------------
void __fastcall TSwitchTextureForm::InitPtStyle( STYLELIST& lstStyles )
{
	int iStart, iEnd, iStep;
	if( !lstStyles.empty() ) {

		iStart = lstStyles.size()-1;
		iEnd   = -1;
		iStep  = -1;

		for( int ind=iStart; ind!=iEnd; ind+=iStep ){

			TPROSTYLE* info = lstStyles[ind];
			InitPtIns(info);
		}
	}
}
//---------------------------------------------------------------------------
void __fastcall TSwitchTextureForm::InitPtIns( TPROSTYLE* info )
{
	int s = SEGTYPE_VAR;
	int sEdge = SEGTYPE_FILL;

	if( !(m_cSegType & 0x01) )
		s = SEGTYPE_FILL;
	if( !(m_cSegType & 0x02) )
		sEdge = SEGTYPE_VAR;

	for( ; s<=sEdge; s++ ){
		int rep = s<SEGTYPE_INTRO ? 1 : 2;

		for( int r=0; r<rep; r++ ){
			int nSegNum = info->idLoc==0 ? info->pOrigin->detail.nSegNum[s]
						: info->idLoc==1 ? info->pShare->nSegNum[s]
										 : info->pLocal->nSegNum[s];
			int num = s<SEGTYPE_INTRO ? nSegNum
							   : r==0 ? ( nSegNum & 0x0F )
									  : ( nSegNum>>4 );
			if( num>0 )
			{
				for( int i=0; i<num; i++ )
				{
					int sn = r==0 ? i : i+4;

//					DebugPrint("* * * * * * * * * * * * * * * * * * ");

					for( int j=0; j<16; j++ )
					{
						if( ( info->idLoc==0 && info->pOrigin->detail.tracks[s][sn][j].no>=0 ) ||
							( info->idLoc==1 && info->pShare->trkRef[s][sn][j].idStyle>0 ) ||
							( info->idLoc!=0 && info->idLoc!=1 && info->pLocal->trkRef[s][sn][j].idStyle>0 ) )
						{
							char chan = info->idLoc==0 ? info->pOrigin->detail.tracks[s][sn][j].no
									  : info->idLoc==1 ? info->pShare->trkRef[s][sn][j].chan
									  : info->pLocal->trkRef[s][sn][j].chan;

							if( (m_bChan9 && chan!=9) || (!m_bChan9 && chan==9) )
								continue;

							int state = info->idLoc==0 ? info->pOrigin->state
									  : info->idLoc==1 ? GetStyleInfo( 0, info->pShare->trkRef[s][sn][j].idStyle )->pOrigin->state
									  : e_Downloaded;
							if( state!=e_Downloaded ) continue;

							char prog = info->idLoc==0 ? info->pOrigin->detail.tracks[s][sn][j].prog
											 : info->idLoc==1 ? info->pShare->trkRef[s][sn][j].prog
											 : info->pLocal->trkRef[s][sn][j].prog,
								vol = info->idLoc==0 ? info->pOrigin->detail.tracks[s][sn][j].vol
											 : info->idLoc==1 ? info->pShare->trkRef[s][sn][j].vol
											 : info->pLocal->trkRef[s][sn][j].vol;

//							if( info->idLoc!=0 && info->idLoc!=1 ) {
//								DebugPrint("j=%d, s=%d, sn=%d, chan=%d, prog=%d", j, s, sn, chan, prog);
//							}

//							if( info->idLoc==1 ) {
//								DebugPrint("j=%d, s=%d, sn=%d, chan=%d, prog=%d", j, s, sn, chan, prog);
//							}

							bool bCollected = IsFavorTexture( info->idLoc, info->idStyle, s, sn, chan );
							bool bShow = !m_bCollected || bCollected;
							if( !m_VCProgSearch.empty()	){
								if( prog<m_VCProgSearch[0] || prog>m_VCProgSearch[1] )
									bShow= false;

							}

							if( bShow ){
								TTextureInfo* pPtInfo = new TTextureInfo( info->idLoc, info->idStyle,
																			s, sn, j, prog, vol, bCollected);

								m_VCPtInfo.push_back( pPtInfo );
							}
						}
					}
//					DebugPrint("* * * * * * * * * * * * * * * * * * ");
				}
			}
		}
	}
}
//---------------------------------------------------------------------------
void __fastcall TSwitchTextureForm::DoPtChanged(TObject *Sender)
{
	f_MM_Stop();

	TPanelDB *pBtnCur = (TPanelDB *)Sender;
	if( m_btnCol==pBtnCur ) {
		m_bCollected = !m_bCollected;
		if( m_bCollected ) m_btnCol->Hint = L"点击显示全部相应乐器的织体";
		else 			   m_btnCol->Hint = L"点击显示收藏的相应乐器的织体";

	}
	else if( m_btnSameType==pBtnCur ) {
		m_bSameType  = !m_bSameType;
		if( m_bSameType ) m_btnSameType->Hint = L"点击显示相同乐器的织体";
		else 			  m_btnSameType->Hint = L"点击显示相似乐器的织体";
	}

	InitPtPanel();

	m_btnCol->Refresh();
	m_btnSameType->Refresh();
}
//---------------------------------------------------------------------------
void __fastcall TSwitchTextureForm::DoProgChanged(TObject *Sender)
{
	if( m_LstProgType==(TTableView *)Sender ) {

		if( m_cProgOriType == m_LstProgType->IndSel && m_cProgOriType!=-1 )
			return;

		m_cProgOriType = m_LstProgType->IndSel;

		m_LstProg->IndSel = -1;
		m_InsIndex = -1;
		m_cProgOri = -1;

		InitProgPanel();

	}else if( m_LstProg==(TTableView *)Sender ) {

		if( m_InsIndex==m_LstProg->IndSel ) return;

		m_InsIndex = m_LstProg->IndSel;
		if( -1!=m_cProgOriType ) {
			if( m_bChan9 ) {
				int num, index;
				FONTPRESET* pSet = InsGetDrums(num, index);
				m_cProgOri = pSet[m_InsIndex].id;
            }
			else m_cProgOri = m_cProgOriType*8+m_InsIndex;
		}
	}

	f_MM_Stop();

	InitPtPanel();
}
//---------------------------------------------------------------------------
void __fastcall TSwitchTextureForm::DoPartternInit(TObject *Sender)
{
	f_MM_Stop();
	InitListPanel();

	m_cSetFont  = m_cSetProg = m_cSetProgOri = m_cSetSegType = m_cSetSegNo = -1;
	m_pSetStyle = NULL;

	m_btnTry->Enabled = false;
	if( m_btnInit->Visible )
		m_btnInit->Enabled = false;
}
//---------------------------------------------------------------------------

void __fastcall TSwitchTextureForm::OnTextureChanged(TObject *Sender)
{
	TTextureInfo *pPtInfo = ((TListViewPattern *)m_LstParttern)->m_pPtInfo;

	if( !pPtInfo )
		return;

//	TINSREF   *pInsRef  = pPtInfo->insRef;
//	TPROSTYLE *pStyle   = pPtInfo->styleInfo;
	UINT iStyleId, iStyleLoc;
	char st = pPtInfo->segType,
		sn = pPtInfo->segNo,
		tn = pPtInfo->chNo;

	m_cSetSegType = st;
	m_cSetSegNo = sn;
	m_cSetFont  = m_bSetCur ? -1 : m_cFontCur;
	m_cSetProg  = m_cSetProgOri = pPtInfo->prog;
	m_cSetVol = m_cSetVolOri = pPtInfo->vol;

	TPROSTYLE* pStyle = GetStyleInfo( pPtInfo->idLoc, pPtInfo->idStyle );

	if( pPtInfo->idLoc==0 ){
		m_pSetStyle = pStyle;
		m_cSetchan = pStyle->pOrigin->detail.tracks[st][sn][tn].no;
	}
	else if( pPtInfo->idLoc==1 ){
		TSTYLESHARE* pShare = pStyle->pShare;
		TSHARETRACKREF& trkRef = pShare->trkRef[st][sn][tn];

		m_cSetchan = trkRef.chan;

		int osn = trkRef.segNo;
		char chan = trkRef.chan;
		m_pSetStyle = GetStyleInfo( 0, trkRef.idStyle );
		if( m_pSetStyle!=pStyle ){
			for( int t=0; t<16; t++ ){
				if( m_pSetStyle->pOrigin->detail.tracks[st][osn][t].no==chan ){
					m_cSetProgOri = m_pSetStyle->pOrigin->detail.tracks[st][osn][t].prog;
					m_cSetVolOri = m_pSetStyle->pOrigin->detail.tracks[st][osn][t].vol;
					break;
				}
			}
		}
	}
	else{
		TSTYLELOCAL* pLocal = pStyle->pLocal;
		TTRACKREF& trkRef = pLocal->trkRef[st][sn][tn];

		iStyleLoc = trkRef.idLoc;
		iStyleId = trkRef.idStyle;

		m_cSetchan = trkRef.chan;

		int osn = trkRef.segNo;
		char chan = trkRef.chan;
		m_pSetStyle = GetStyleInfo( iStyleLoc, iStyleId );
		if( m_pSetStyle!=pStyle ){
			for( int t=0; t<16; t++ ){
				if( iStyleLoc==0 ){
					if( m_pSetStyle->pOrigin->detail.tracks[st][osn][t].no==chan ){
						m_cSetProgOri = m_pSetStyle->pOrigin->detail.tracks[st][osn][t].prog;
						m_cSetVolOri = m_pSetStyle->pOrigin->detail.tracks[st][osn][t].vol;
						break;
					}
				}
				else{
					int nSegNum = pLocal->nSegNum[st];
					int iStart = ( st<SEGTYPE_INTRO || sn<4 ) ? 0 : 4,
						iEnd = iStart + ( iStart==0 ? ( nSegNum & 0x0F ) : ( nSegNum>>4 ));

					bool bFind = false;
					for( int i=iStart; !bFind && i<iEnd; i++ ){
						for( int j=0; j<16; j++ ){
							if( m_pSetStyle->pLocal->trkRef[st][i][j].idLoc==iStyleLoc
										&& m_pSetStyle->pLocal->trkRef[st][i][j].idStyle==iStyleId
										&& m_pSetStyle->pLocal->trkRef[st][i][j].segNo==osn
										&& m_pSetStyle->pLocal->trkRef[st][i][j].chan==chan ){
								m_cSetProgOri = m_pSetStyle->pLocal->trkRef[st][i][j].prog;
								m_cSetVolOri = m_pSetStyle->pLocal->trkRef[st][i][j].vol;

								bFind = true;
								break;
							}
							else if( m_pSetStyle->pLocal->trkRef[st][i][j].idLoc==0
										&& m_pSetStyle->pLocal->trkRef[st][i][j].idStyle==0 ){
								break;
							}
						}
					}
				}
			}
		}
	}

	bool bChange = m_pSetStyle!=m_pStyleOri || m_cSetSegNo!=m_cSegNoOri
												|| m_cSetProg!=m_cProgOri || m_cSetchan!=m_cChanOri;
	if( m_btnInit->Visible )
		m_btnInit->Enabled = bChange;
	m_btnTry->Enabled = bChange || m_cSetFont!=m_cFontCur;
}
//---------------------------------------------------------------------------

void __fastcall TSwitchTextureForm::DoPartternUse(TObject *Sender)
{
	f_MM_Stop();
}
//---------------------------------------------------------------------------
void __fastcall TSwitchTextureForm::DoPanelFocus(TObject *Sender)
{
	TPanelDB *pPanel = (TPanelDB *)Sender;
	if( pPanel==m_LstParttern )
		m_LstParttern->SetFocus();
	if( pPanel==m_LstProgType )
		m_LstProgType->SetFocus();
	if( pPanel==m_LstProg )
		m_LstProg->SetFocus();
}
//---------------------------------------------------------------------------
void __fastcall TSwitchTextureForm::CurProgPlayClick(TObject *Sender)
{
	int iFont = -1, iProg = -1;
	if( !m_ckPlay->Checked ){
		iFont = m_cFontCur;
		iProg = m_cProgCur;
	}

	if( ((TListViewPattern *)m_LstParttern)->m_iFont!=iFont
					|| ((TListViewPattern *)m_LstParttern)->m_iProg!=iProg ){
		f_MM_Stop();

		((TListViewPattern *)m_LstParttern)->m_iFont = iFont;
		((TListViewPattern *)m_LstParttern)->m_iProg = iProg;
	}
}
//---------------------------------------------------------------------------


void __fastcall TSwitchTextureForm::ReplaceProgClick(TObject *Sender)
{
	if( m_ckReplaceProg->Checked ) {
		m_bSetCur = true;

		if( m_ckPlay->Checked==false ) {
			m_ckPlay->Checked = true;
			CurProgPlayClick(NULL);
        }
	}
	else {
		m_bSetCur = false;
	}


	if( m_pSetStyle ){
		m_cSetFont  = m_bSetCur ? -1 : m_cFontCur;

		bool bChange = m_pSetStyle!=m_pStyleOri || m_cSetFont!=m_cFontCur || m_cSetSegNo!=m_cSegNoOri
													|| m_cSetProg!=m_cProgOri || m_cSetchan!=m_cChanOri;
		m_btnTry->Enabled = bChange;
	}
}
//---------------------------------------------------------------------------

void __fastcall TSwitchTextureForm::DrumClick(TObject *Sender)
{
	f_MM_Stop();

	m_cChanOri = m_cbDrum->Checked ? 9 : -1;
	m_bChan9 = m_cChanOri==9;
	((TListViewPattern *)m_LstParttern)->m_bChan9 = m_bChan9;

	InitListPanel();

	m_cSetFont  = m_cSetProg  = m_cSetSegType = m_cSetSegNo = -1;
	m_pSetStyle = NULL;

	m_btnTry->Enabled = false;
	if( m_btnInit->Visible )
		m_btnInit->Enabled = false;
}
//---------------------------------------------------------------------------

void __fastcall TSwitchTextureForm::FormCreate(TObject *Sender)
{
	m_BKGPanel->Font->Size = g_szFont9;
	m_btnTry->FontSize = g_szFont9;
	m_btnCancel->FontSize = g_szFont9;
	m_btnInit->FontSize = g_szFont9;
	m_ckPlay->Font->Size = g_szFont9;
	m_ckReplaceProg->Font->Size = g_szFont9;
	m_cbDrum->Font->Size = g_szFont9;

    Canvas->Font->Size = g_szFont9;
}
//---------------------------------------------------------------------------

