Files
AccompanyCube/PredefineProjectFormUnit.cpp
2026-06-13 00:10:11 +08:00

454 lines
13 KiB
C++

//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "PredefineProjectFormUnit.h"
#include "GlobalUnit.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "PNGButton"
#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)
{
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;
mPaintBoxFlag = true;
mIsPreSetShow = false;
}
//---------------------------------------------------------------------------
__fastcall TPredefineProjectForm::~TPredefineProjectForm()
{
if(mPreChordSet) {
delete mPreChordSet;
mPreChordSet = NULL;
}
}
//---------------------------------------------------------------------------
void __fastcall TPredefineProjectForm::WMNCHitTest(TWMNCHitTest &Message)
{
TForm::Dispatch(&Message);
if( Message.Result==HTCLIENT && Message.YPos<Top+30 && Message.XPos<Left+520) {
if(!mCloseBtn->BoundsRect.Contains(TPoint(Message.XPos - Left, Message.YPos - Top))) {
Message.Result = HTCAPTION;
}
}
}
//---------------------------------------------------------------------------
void __fastcall TPredefineProjectForm::DoFormCreate(TObject *Sender)
{
HRGN hRgn = CreateRoundRectRgn(0, 0, Width+1, Height+1, 16, 16);
SetWindowRgn(Handle, hRgn, false);
DeleteObject(hRgn);
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--;
mTSComboBox->ItemIndex = m_Ts;
mTempoSpinEdit->Value = m_Tempo;
}
//---------------------------------------------------------------------------
void __fastcall TPredefineProjectForm::DoChordPaint(TObject *Sender)
{
TPaintBox *PaintBox = (TPaintBox *)Sender;
TRect cRect = PaintBox->ClientRect;
cRect.Right--;
cRect.Left++;
cRect.Top++;
cRect.Bottom--;
PaintBox->Canvas->Brush->Color = TColor(0xC8F0FF);
PaintBox->Canvas->Pen->Color = TColor(0x4673A4);
PaintBox->Canvas->RoundRect(cRect, 5, 5);
int Preset = 0;
int Stage = mStageSetComboBox->ItemIndex;
if(mStageComboBox->ItemIndex == 2) {
if(mStageSetComboBox->ItemIndex == 0) {
Preset = mLordSong;
}
else if(mStageSetComboBox->ItemIndex == 1) {
Preset = mBridge;
Stage = 2;
}
else if(mStageSetComboBox->ItemIndex == 2){
Preset = mRefrain;
Stage = 1;
}
}
else {
if(mStageSetComboBox->ItemIndex == 0) {
Preset = mLordSong;
}
else if(mStageSetComboBox->ItemIndex == 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 __fastcall TPredefineProjectForm::DoPaintBoxMouseLeave(TObject *Sender)
{
mPresetMouseOver = false;
}
//---------------------------------------------------------------------------
void __fastcall TPredefineProjectForm::DoPaintBoxMouseEnter(TObject *Sender)
{
mPresetMouseOver = true;
}
//---------------------------------------------------------------------------
void __fastcall TPredefineProjectForm::DoPresetChordShow(TObject *Sender)
{
mIsPreSetShow = true;
}
//---------------------------------------------------------------------------
void __fastcall TPredefineProjectForm::DoPresetChordHide(TObject *Sender)
{
if(mStageComboBox->ItemIndex == 2) {
if(mStageSetComboBox->ItemIndex == 0) {
mLordSong = mPreChordSet->SelectedIndex;
}
else if(mStageSetComboBox->ItemIndex == 1) {
mBridge = mPreChordSet->SelectedIndex;
}
else if(mStageSetComboBox->ItemIndex == 2){
mRefrain = mPreChordSet->SelectedIndex;
}
}
else {
if(mStageSetComboBox->ItemIndex == 0) {
mLordSong = mPreChordSet->SelectedIndex;
}
else if(mStageSetComboBox->ItemIndex == 1) {
mRefrain = mPreChordSet->SelectedIndex;
}
}
if(GetActiveWindow() != Handle) {
mPaintBoxFlag = true;
}
if(!mPresetMouseOver)
mPaintBoxFlag = true;
mIsPreSetShow = false;
mChordsBox->Repaint();
}
//---------------------------------------------------------------------------
void TPredefineProjectForm::DrawChord(TCanvas *Canvas, TRect Rect, int iKey, int iType)
{
int iMainKey, iAjustKey; //调级数、升降
TPrefineProjectSet::GetChordDisplayKey(iKey, m_Key, iMainKey, iAjustKey);
UniDrawChord(Canvas, Rect, iMainKey, iAjustKey, iType);
}
//---------------------------------------------------------------------------
void __fastcall TPredefineProjectForm::DoStageCountChange(TObject *Sender)
{
mStageSetComboBox->Clear();
for(int i = 0; i <= mStageComboBox->ItemIndex; i++) {
mStageSetComboBox->AddItem(IntToStr(i + 1), NULL);
}
mStageSetComboBox->ItemIndex = 0;
DoStageSetChange(NULL);
m_RavStage = mStageComboBox->ItemIndex + 1;
mChordsBox->Repaint();
}
//---------------------------------------------------------------------------
void __fastcall TPredefineProjectForm::DoStageSetChange(TObject *Sender)
{
// if(m_RavStage == 3 && mStageSetComboBox->ItemIndex == 1)
// mSelectedStage = 2;
// else if(m_RavStage == 3 && mStageSetComboBox->ItemIndex == 2)
// mSelectedStage = 1;
// else
// mSelectedStage = mStageSetComboBox->ItemIndex;
//
mChordsBox->Repaint();
}
//---------------------------------------------------------------------------
void __fastcall TPredefineProjectForm::DoKeyChange(TObject *Sender)
{
TComboBox *Combo = (TComboBox *)Sender;
m_Key = Combo->ItemIndex;
mChordsBox->Repaint();
}
//---------------------------------------------------------------------------
void __fastcall TPredefineProjectForm::DoKeyTypeChange(TObject *Sender)
{
TComboBox *Combo = (TComboBox *)Sender;
m_KeyType = Combo->ItemIndex;
mChordsBox->Repaint();
}
//---------------------------------------------------------------------------
void __fastcall TPredefineProjectForm::DoTsChange(TObject *Sender)
{
TComboBox *Combo = (TComboBox *)Sender;
m_Ts = Combo->ItemIndex;
}
//---------------------------------------------------------------------------
void __fastcall TPredefineProjectForm::DoPaintBoxMouseDown(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
if(mPaintBoxFlag) {
if(mPreChordSet) {
delete mPreChordSet;
mPreChordSet = NULL;
}
mPreChordSet = new TPrefineProjectSet(NULL);
mPreChordSet->Parent = this;
int Preset = 0;
int Stage = mStageSetComboBox->ItemIndex;
if(mStageComboBox->ItemIndex == 2) {
if(mStageSetComboBox->ItemIndex == 0) {
Preset = mLordSong;
}
else if(mStageSetComboBox->ItemIndex == 1) {
Preset = mBridge;
Stage = 2;
}
else if(mStageSetComboBox->ItemIndex == 2){
Preset = mRefrain;
Stage = 1;
}
}
else {
if(mStageSetComboBox->ItemIndex == 0) {
Preset = mLordSong;
}
else if(mStageSetComboBox->ItemIndex == 1) {
Preset = mRefrain;
}
}
for(int i = 0; i < 8; i++) {
BYTE *Chord = (BYTE *)u_arrPredefChords[Stage][m_KeyType][i];
TPrefineProjectSetItem *Item = new TPrefineProjectSetItem(mPreChordSet);
Item->Key = m_Key;
Item->Index = i;
memcpy(Item->PredefChord, Chord, 8 * sizeof(BYTE));
mPreChordSet->AddItem(Item);
}
mPreChordSet->SelectedIndex = Preset;
TPoint posClient(0, mChordsBox->Height);
mPreChordSet->OnShow = DoPresetChordShow;
mPreChordSet->OnHide = DoPresetChordHide;
TPoint pos = mChordsBox->ClientToScreen(posClient);
mPreChordSet->Top = pos.Y;
mPreChordSet->Left = pos.X + 1;
mPreChordSet->Width = mChordsBox->Width - 2;
mPreChordSet->Height = 200;
mPreChordSet->Show();
}
mPaintBoxFlag = !mPaintBoxFlag;
}
//---------------------------------------------------------------------------
void __fastcall TPredefineProjectForm::OnTempoEditChange(TObject *Sender)
{
TSpinEdit *Edit = (TSpinEdit *)Sender;
if(Edit->Value == 55) {
mTempoComboBox->ItemIndex = 0;
}
else if(Edit->Value == 65){
mTempoComboBox->ItemIndex = 1;
}
else if(Edit->Value == 80) {
mTempoComboBox->ItemIndex = 2;
}
else if(Edit->Value == 110) {
mTempoComboBox->ItemIndex = 3;
}
else if(Edit->Value == 120) {
mTempoComboBox->ItemIndex = 4;
}
else {
mTempoComboBox->ItemIndex = 5;
}
m_Tempo = Edit->Value;
}
//---------------------------------------------------------------------------
void __fastcall TPredefineProjectForm::DoTempoComboChange(TObject *Sender)
{
TComboBox *Combo = (TComboBox *)Sender;
if(Combo->ItemIndex == 0) {
mTempoSpinEdit->Value = 55;
}
else if(Combo->ItemIndex == 1){
mTempoSpinEdit->Value = 65;
}
else if(Combo->ItemIndex == 2) {
mTempoSpinEdit->Value = 80;
}
else if(Combo->ItemIndex == 3) {
mTempoSpinEdit->Value = 110;
}
else if(Combo->ItemIndex == 4) {
mTempoSpinEdit->Value = 120;
}
}
//---------------------------------------------------------------------------
int __fastcall TPredefineProjectForm::ShowModal()
{
int ret = TForm::ShowModal();
m_Preset[0] = mLordSong;
if(mStageComboBox->ItemIndex == 2) {
m_Preset[1] = mBridge;
m_Preset[2] = mRefrain;
}
else {
m_Preset[1] = mRefrain;
m_Preset[2] = 0;
}
return ret;
}
void __fastcall TPredefineProjectForm::DoFormActivate(TObject *Sender)
{
if(mStageComboBox->ItemIndex != (mStageSetComboBox->Items->Count - 1)) {
DoStageCountChange(NULL);
}
}
//---------------------------------------------------------------------------