📄 controldialog.cpp
字号:
// ControlDialog.cpp : 实现文件
//
#include "stdafx.h"
#include "3D-tree.h"
#include "ControlDialog.h"
#include <GL/glut.h>
#include <sstream>
// CControlDialog 对话框
IMPLEMENT_DYNAMIC(CControlDialog, CDialog)
CControlDialog::CControlDialog(CWnd* pParent /*=NULL*/)
: CDialog(CControlDialog::IDD, pParent)
{
ini = new ChIni("config.ini");
}
CControlDialog::~CControlDialog()
{
}
void CControlDialog::setControlTree( MyTree* tree, Land* land, bool* showProcess,
float* rotateX, float* rotateY,
float* translateX, float* translateY, float* translateZ,
float* changeStep, bool* isAutoRotate, bool* isDrawForest, bool* reGenerateRandomTree ) {
this->tree = tree;
this->land = land;
this->showGrowProcess = showProcess;
this->rotateX = rotateX;
this->rotateY = rotateY;
this->translateX = translateX;
this->translateY = translateY;
this->translateZ = translateZ;
this->changeStep = changeStep;
this->isAutoRotate = isAutoRotate;
this->isDrawForest = isDrawForest;
this->reGenerateRandomTree = reGenerateRandomTree;
land->setVisible( m_landVisible.GetCheck() );
tree->setLeafVisible( m_leafVisible.GetCheck() );
tree->setLeafShakeble( m_leafShakeble.GetCheck() );
}
// CControlDialog 消息处理程序
BOOL CControlDialog::OnInitDialog()
{
CDialog::OnInitDialog();
int depth = ini->readValueAsInt( "Config", "Depth" );
int radius = ini->readValueAsInt( "Config", "Radius" );
int BranchLength = ini->readValueAsInt( "Config", "BranchLength" );
int BaseLength = ini->readValueAsInt( "Config", "BaseLength" );
int Slices = ini->readValueAsInt( "Config", "Slices" );
int Twist = ini->readValueAsInt( "Config", "Twist" );
int LeftExpand = ini->readValueAsInt( "Config", "LeftExpand" );
int RightExpand = ini->readValueAsInt( "Config", "RightExpand" );
int LeafSize = ini->readValueAsInt( "Config", "LeafSize" );
bool leafVisible = ini->readValueAsBoolean( "Config", "LeafVisible" );
bool leafShakeble = ini->readValueAsBoolean( "Config", "LeafShakeble" );
bool landVisible = ini->readValueAsBoolean( "Config", "LandVisible" );
m_treeDepth.SetRange( 0, 12, 1 );
m_treeDepth.SetPos( depth );
m_radius.SetRange( 1, 40, 1 );
m_radius.SetPos( radius );
m_treeLength.SetRange( 0, 20, 1 );
m_treeLength.SetPos( BranchLength );
m_baseLength.SetRange( 0, 60, 1 );
m_baseLength.SetPos( BaseLength );
m_treeSlices.SetRange( 4, 30, 1 );
m_treeSlices.SetPos( Slices );
m_leftBranchTwist.SetRange( 0, 90, 1 );
m_leftBranchTwist.SetPos( Twist );
m_leftBranchExpand.SetRange( 5, 80, 1 );
m_leftBranchExpand.SetPos( LeftExpand );
m_rightBranchExpand.SetRange( 5, 80, 1 );
m_rightBranchExpand.SetPos( RightExpand );
m_leafSize.SetRange( 0, 15, 1 );
m_leafSize.SetPos( LeafSize );
m_leafVisible.SetCheck( leafVisible );
m_leafShakeble.SetCheck( leafShakeble );
m_landVisible.SetCheck( landVisible );
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
void CControlDialog::reset() {
m_treeDepth.SetRange( 0, 12, 1 );
m_treeDepth.SetPos( 10 );
m_radius.SetRange( 1, 40, 1 );
m_radius.SetPos( 10 );
m_treeLength.SetRange( 0, 20, 1 );
m_treeLength.SetPos( 10 );
m_baseLength.SetRange( 0, 60, 1 );
m_baseLength.SetPos( 30 );
m_treeSlices.SetRange( 4, 30, 1 );
m_treeSlices.SetPos( 8 );
m_leftBranchTwist.SetRange( 0, 90, 1 );
m_leftBranchTwist.SetPos( 80 );
m_leftBranchExpand.SetRange( 5, 80, 1 );
m_leftBranchExpand.SetPos( 25 );
m_rightBranchExpand.SetRange( 5, 80, 1 );
m_rightBranchExpand.SetPos( 15 );
m_leafSize.SetRange( 0, 15, 1 );
m_leafSize.SetPos( 10 );
m_leafVisible.SetCheck( true );
tree->setLeafVisible(true);
m_leafShakeble.SetCheck( false );
tree->setLeafShakeble( false );
m_landVisible.SetCheck( true );
land->setVisible( true );
}
void CControlDialog::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_TREE_DEPTH, m_treeDepth);
DDX_Control(pDX, IDC_TREE_BASE_LENGTH, m_baseLength);
DDX_Control(pDX, IDC_TREE_LENGTH, m_treeLength);
DDX_Control(pDX, IDC_BRANCH_SLICES, m_treeSlices);
DDX_Control(pDX, IDC_TREE_RADIUS, m_radius);
DDX_Control(pDX, IDC_LEAF_SIZE, m_leafSize);
DDX_Control(pDX, IDC_LEFT_BRANCH_TWIST, m_leftBranchTwist);
DDX_Control(pDX, IDC_LEFT_BRANCH_EXPAND, m_leftBranchExpand);
DDX_Control(pDX, IDC_RIGHT_BRANCH_EXPAND2, m_rightBranchExpand);
DDX_Control(pDX, IDC_SHOWLAND, m_landVisible);
DDX_Control(pDX, IDC_SHOWLEAF, m_leafVisible);
DDX_Control(pDX, IDC_SHAKELEAF, m_leafShakeble);
DDX_Control(pDX, IDC_BUTTON_SHOWGROW, m_showGrow);
DDX_Control(pDX, IDOK, m_reset);
}
BEGIN_MESSAGE_MAP(CControlDialog, CDialog)
ON_NOTIFY(NM_CUSTOMDRAW, IDC_TREE_DEPTH, &CControlDialog::OnNMCustomdrawTreeDepth)
ON_NOTIFY(NM_CUSTOMDRAW, IDC_TREE_BASE_LENGTH, &CControlDialog::OnNMCustomdrawTreeBaseLength)
ON_NOTIFY(NM_CUSTOMDRAW, IDC_TREE_LENGTH, &CControlDialog::OnNMCustomdrawTreeLength)
ON_NOTIFY(NM_CUSTOMDRAW, IDC_BRANCH_SLICES, &CControlDialog::OnNMCustomdrawBranchSlices)
ON_NOTIFY(NM_CUSTOMDRAW, IDC_TREE_RADIUS, &CControlDialog::OnNMCustomdrawTreeRadius)
ON_NOTIFY(NM_CUSTOMDRAW, IDC_LEAF_SIZE, &CControlDialog::OnNMCustomdrawLeafSize)
ON_NOTIFY(NM_CUSTOMDRAW, IDC_LEFT_BRANCH_TWIST, &CControlDialog::OnNMCustomdrawLeftBranchTwist)
ON_NOTIFY(NM_CUSTOMDRAW, IDC_LEFT_BRANCH_EXPAND, &CControlDialog::OnNMCustomdrawLeftBranchExpand)
ON_NOTIFY(NM_CUSTOMDRAW, IDC_RIGHT_BRANCH_EXPAND2, &CControlDialog::OnNMCustomdrawRightBranchExpand2)
ON_BN_CLICKED(IDC_SHOWLAND, &CControlDialog::OnBnClickedShowland)
ON_BN_CLICKED(IDC_SHOWLEAF, &CControlDialog::OnBnClickedShowleaf)
ON_BN_CLICKED(IDC_BUTTON_SHOWGROW, &CControlDialog::OnBnClickedButtonShowgrow)
ON_BN_CLICKED(IDCANCEL, &CControlDialog::OnBnClickedCancel)
ON_BN_CLICKED(IDC_SHAKELEAF, &CControlDialog::OnBnClickedShakeleaf)
ON_BN_CLICKED(IDOK, &CControlDialog::OnBnClickedOk)
// ON_WM_CLOSE()
ON_WM_DESTROY()
ON_BN_CLICKED(IDC_BUTTON_ROTATE, &CControlDialog::OnBnClickedButtonRotate)
ON_BN_CLICKED(IDC_BUTTON_FORSET, &CControlDialog::OnBnClickedButtonForset)
END_MESSAGE_MAP()
// CControlDialog 消息处理程序
BOOL CControlDialog::PreTranslateMessage(MSG* pMsg)
{
/*if( pMsg->message == WM_KEYDOWN ) {
switch(pMsg->wParam) {
case VK_UP :
*translateY -= *changeStep;
break;
case VK_DOWN :
*translateY += *changeStep;
break;
case VK_LEFT :
*translateX += *changeStep;
break;
case VK_RIGHT :
*translateX -= *changeStep;
break;
case VK_SPACE :
if ( tree->isGrowing() == true ) {
tree->pauseGrow();
} else {
tree->playGrow();
}
break;
case VK_ESCAPE :
AfxGetMainWnd()->SendMessage(WM_CLOSE);
break;
case VK_F11 :
break;
case 'w' :
case 'W' :
*translateZ += 0.1f;
break;
case 's' :
case 'S' :
*translateZ -= 0.1f;
break;
case 'a' :
case 'A' :
*rotateX += 3;
break;
case 'd' :
case 'D' :
*rotateX -= 3;
break;
case 'l' :
case 'L' :
if ( glIsEnabled( GL_LIGHTING ) == GL_FALSE ) {
glEnable( GL_LIGHTING );
} else {
glDisable( GL_LIGHTING );
}
break;
case 't' :
case 'T' :
if ( glIsEnabled( GL_TEXTURE_2D ) == GL_FALSE ) {
glEnable( GL_TEXTURE_2D );
} else {
glDisable( GL_TEXTURE_2D );
}
break;
default:
break;
}
return true;
}*/
return CDialog::PreTranslateMessage(pMsg);
}
void CControlDialog::OnNMCustomdrawTreeDepth(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
tree->setMaxBranchDepth( m_treeDepth.GetPos() );
this->GetParent()->SetFocus();
*pResult = 0;
}
void CControlDialog::OnNMCustomdrawTreeBaseLength(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
tree->setMaxBaseLength( m_baseLength.GetPos() * 6.0f / m_baseLength.GetRangeMax() );
this->GetParent()->SetFocus();
*pResult = 0;
}
void CControlDialog::OnNMCustomdrawTreeLength(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
tree->setMaxBranchLength( ( float )m_treeLength.GetPos() * 2 / m_treeLength.GetRangeMax() );
this->GetParent()->SetFocus();
*pResult = 0;
}
void CControlDialog::OnNMCustomdrawBranchSlices(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
tree->setBranchSlices( m_treeSlices.GetPos() );
this->GetParent()->SetFocus();
*pResult = 0;
}
void CControlDialog::OnNMCustomdrawTreeRadius(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
tree->setBaseBranchRadius( ( float )m_radius.GetPos() / 2.5f / m_radius.GetRangeMax() );
this->GetParent()->SetFocus();
*pResult = 0;
}
void CControlDialog::OnNMCustomdrawLeafSize(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
tree->setLeafSize( m_leafSize.GetPos() / 100.0f );
this->GetParent()->SetFocus();
*pResult = 0;
}
void CControlDialog::OnNMCustomdrawLeftBranchTwist(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
tree->setLeftBranchTwist( ( float )m_leftBranchTwist.GetPos() );
this->GetParent()->SetFocus();
*pResult = 0;
}
void CControlDialog::OnNMCustomdrawLeftBranchExpand(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
tree->setLeftBranchExpand( ( float )m_leftBranchExpand.GetPos() );
this->GetParent()->SetFocus();
*pResult = 0;
}
void CControlDialog::OnNMCustomdrawRightBranchExpand2(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
tree->setRightBranchExpand( ( float )m_rightBranchExpand.GetPos() );
this->GetParent()->SetFocus();
*pResult = 0;
}
void CControlDialog::OnBnClickedShowland()
{
land->setVisible( m_landVisible.GetCheck() );
this->GetParent()->SetFocus();
}
void CControlDialog::OnBnClickedShowleaf()
{
tree->setLeafVisible( m_leafVisible.GetCheck() );
this->GetParent()->SetFocus();
}
void CControlDialog::OnBnClickedShakeleaf()
{
tree->setLeafShakeble( m_leafShakeble.GetCheck() );
this->GetParent()->SetFocus();
}
void CControlDialog::OnBnClickedButtonShowgrow()
{
tree->rebuild();
*showGrowProcess = true;
tree->playGrow();
this->GetParent()->SetFocus();
}
void CControlDialog::OnBnClickedButtonRotate()
{
*isAutoRotate = !*isAutoRotate;
this->GetParent()->SetFocus();
}
void CControlDialog::OnBnClickedButtonForset()
{
*isDrawForest = !*isDrawForest;
if ( *isDrawForest == true ) {
GetDlgItem( IDC_BUTTON_FORSET )->SetWindowTextW( _T( "恢复到单棵树" ) );
*reGenerateRandomTree = true;
enableControls( false );
} else {
GetDlgItem( IDC_BUTTON_FORSET )->SetWindowTextW( _T( "随机生成100棵树(注意,很卡!!)" ) );
*reGenerateRandomTree = false;
enableControls( true );
}
this->GetParent()->SetFocus();
}
void CControlDialog::enableControls( bool bEnable ) {
this->m_baseLength.EnableWindow( bEnable );
this->m_leafShakeble.EnableWindow( bEnable );
this->m_leafSize.EnableWindow( bEnable );
this->m_leafVisible.EnableWindow( bEnable );
this->m_leftBranchExpand.EnableWindow( bEnable );
this->m_leftBranchTwist.EnableWindow( bEnable );
this->m_radius.EnableWindow( bEnable );
this->m_rightBranchExpand.EnableWindow( bEnable );
this->m_treeDepth.EnableWindow( bEnable );
this->m_treeLength.EnableWindow( bEnable );
this->m_treeSlices.EnableWindow( bEnable );
this->m_showGrow.EnableWindow( bEnable );
this->m_reset.EnableWindow( bEnable );
}
void CControlDialog::OnBnClickedCancel()
{
AfxGetMainWnd()->SendMessage(WM_CLOSE);
//OnCancel();
}
void CControlDialog::OnBnClickedOk()
{
reset();
//OnOK();
}
void CControlDialog::saveConfig() {
int depth = m_treeDepth.GetPos();
int radius = m_radius.GetPos();
int branchLength = m_treeLength.GetPos();
int baseLength = m_baseLength.GetPos();
int slices = m_treeSlices.GetPos();
int twist = m_leftBranchTwist.GetPos();
int leftExpand = m_leftBranchExpand.GetPos();
int rightExpand = m_rightBranchExpand.GetPos();
int leafSize = m_leafSize.GetPos();
bool leafVisible = m_leafVisible.GetCheck();
bool leafShakeble =m_leafShakeble.GetCheck();
bool landVisible = m_landVisible.GetCheck();
ini->writeValue( "Config", "Depth", depth );
ini->writeValue( "Config", "Radius", radius );
ini->writeValue( "Config", "BranchLength", branchLength );
ini->writeValue( "Config", "BaseLength", baseLength );
ini->writeValue( "Config", "Slices", slices );
ini->writeValue( "Config", "Twist", twist );
ini->writeValue( "Config", "LeftExpand", leftExpand );
ini->writeValue( "Config", "RightExpand", rightExpand );
ini->writeValue( "Config", "LeafSize", leafSize );
ini->writeValue( "Config", "LeafVisible", leafVisible );
ini->writeValue( "Config", "LeafShakeble", leafShakeble );
ini->writeValue( "Config", "LandVisible", landVisible );
}
void CControlDialog::OnDestroy()
{
CDialog::OnDestroy();
saveConfig();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -