// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "ulaPag.h"



#include "MainFrm.h"
#include ".\mainfrm.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CMainFrame

IMPLEMENT_DYNAMIC(CMainFrame, CMDIFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
	ON_WM_CREATE()
	ON_BN_CLICKED(IDC_BUTTON1, OnBnClickedButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnBnClickedButton2)
	ON_BN_CLICKED(IDC_BUTTON3, OnBnClickedButton3)
	ON_BN_CLICKED(IDC_BUTTON4, OnBnClickedButton4)

	ON_BN_CLICKED(IDC_BUTTONPREV, OnBnClickedButtonPrev)
	ON_BN_CLICKED(IDC_BUTTONNEXT, OnBnClickedButtonNext)
	ON_BN_CLICKED(IDC_BUTTONLAST, OnBnClickedButtonLast)
	ON_BN_CLICKED(IDC_BUTTONGO, OnBnClickedButtonGo)
	ON_BN_CLICKED(IDC_BUTTONADDNEW, OnBnClickedButtonAddnew)
	ON_BN_CLICKED(IDC_BUTTONAPPLY, OnBnClickedButtonApply)

	ON_BN_CLICKED(IDC_BUTTONRESET, OnBnClickedButtonReset)

	ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SLIDER1, OnNMReleasedcaptureSlider1)
	ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SLIDER2, OnNMReleasedcaptureTimeLineSlider) //Slider 2 for timeline [e:12]

	ON_BN_CLICKED(IDC_BUTTON5, OnBnClickedButton5)
	ON_BN_CLICKED(IDC_CHECK1, OnBnClickedCheck1)
	ON_BN_CLICKED(IDC_CHECKSOUND, OnBnClickedChecksound)
	ON_BN_CLICKED(IDC_CHECKTEXTCONT, OnBnClickedChecktextcont)
	ON_BN_CLICKED(IDC_BUTTONFRAMETIME, OnBnClickedButtonframetime)
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};


// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
}

CMainFrame::~CMainFrame()
{
}

void CMainFrame::OnBnClickedButtonApply()
{
	CMainFrame *cFrame    = reinterpret_cast<CMainFrame *>(AfxGetMainWnd());
	CFormView * _formpro = reinterpret_cast<CFormView *>(cFrame->GetDlgItem(IDD_FORMVIEW3));
	CEdit * _proName = reinterpret_cast<CEdit *> (_formpro->GetDlgItem(IDC_EDITPRONAME));
	CEdit * _posX = reinterpret_cast<CEdit *> (_formpro->GetDlgItem(IDC_EDITPOSX));
	CEdit * _posY = reinterpret_cast<CEdit *> (_formpro->GetDlgItem(IDC_EDITPOSY));
	CEdit * _posZ = reinterpret_cast<CEdit *> (_formpro->GetDlgItem(IDC_EDITPOSZ));
	CEdit * _scaX = reinterpret_cast<CEdit *> (_formpro->GetDlgItem(IDC_EDITSCAX));
	CEdit * _scaY = reinterpret_cast<CEdit *> (_formpro->GetDlgItem(IDC_EDITSCAY));
	CEdit * _scaZ = reinterpret_cast<CEdit *> (_formpro->GetDlgItem(IDC_EDITSCAZ));
	
	if (programFlowManager->getEventManager()->getInputManager()->getSelectedObjectList().size() == 1)
		{
		osg::MatrixTransform *selectedTransNode = CSceneGraphUtilities::findSelectedNodesParentMatrixTransform(programFlowManager->getEventManager()->getInputManager()->getSelectedObjectList().at(0));
		osg::Node *selectedNode = CSceneGraphUtilities::findSelectedNodesParentASNode(programFlowManager->getEventManager()->getInputManager()->getSelectedObjectList().at(0));
		//osg::Vec3 *vector = new osg::Vec3();
		std::string objectName;
		std::string position;
		CString str;
		double x, y, z;

		_posX->GetWindowText(str);
		position = str;
		x = atof(position.c_str());
		_posY->GetWindowText(str);
		position = str;
		y = atof(position.c_str());
		_posZ->GetWindowText(str);
		position = str;
		z = atof(position.c_str());
		osg::Vec3 *vector = new osg::Vec3(x, y, z);
		osg::Matrix *tempMatrix = new osg::Matrix();
		tempMatrix->setTrans(*vector);
		selectedTransNode->setMatrix(*tempMatrix);
		_proName->GetWindowText(str);
		objectName = str;
		programFlowManager->getRenderManager()->getTimeline()->getCurrentFrame()->getScene()->findCorrespondingSceneObjectViaNameReference(selectedNode)->setName(objectName);
		selectedNode->setName(objectName.c_str());

		}

	//.............................
	// to continue from here

}



//[e:12]    .. slider on change event handler
void CMainFrame::OnNMReleasedcaptureTimeLineSlider(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);

	CFormView * _formTimeline = (CFormView *) GetDlgItem(IDD_FORMVIEW2);
	CSliderCtrl * _timeline = (CSliderCtrl *) _formTimeline->GetDlgItem(IDC_SLIDER2);
	CEdit * _currentFrameNo = (CEdit *) _formTimeline->GetDlgItem(IDC_EDITCURRENT);
	char buffer[33];
	int _m_currentFrame;

	_m_currentFrame = _timeline->GetPos();
	itoa(_m_currentFrame,buffer,10);
	_currentFrameNo->SetWindowText(buffer);

    //...............................................timeline controls

	programFlowManager->getRenderManager()->getTimeline()->setCurrentFrameNo(_timeline->GetPos());


	*pResult = 0;

}
// ** [e:12]\







void CMainFrame::OnBnClickedButtonReset()
{
	char buffer[33];
	CFormView * _formpro = (CFormView *)GetDlgItem(IDD_FORMVIEW3);
	CEdit * _proName = (CEdit *) _formpro->GetDlgItem(IDC_EDITPRONAME);
	CEdit * _posX = (CEdit *) _formpro->GetDlgItem(IDC_EDITPOSX);
	CEdit * _posY = (CEdit *) _formpro->GetDlgItem(IDC_EDITPOSY);
	CEdit * _posZ = (CEdit *) _formpro->GetDlgItem(IDC_EDITPOSZ);
	CEdit * _scaX = (CEdit *) _formpro->GetDlgItem(IDC_EDITSCAX);
	CEdit * _scaY = (CEdit *) _formpro->GetDlgItem(IDC_EDITSCAY);
	CEdit * _scaZ = (CEdit *) _formpro->GetDlgItem(IDC_EDITSCAZ);

	_proName->SetWindowText("");
	_posX->SetWindowText("");
	_posY->SetWindowText("");
	_posZ->SetWindowText("");

	_scaX->SetWindowText("");
	_scaY->SetWindowText("");
	_scaZ->SetWindowText("");

	
	


}

//time line buttons -- timeline controls

void CMainFrame::OnBnClickedButtonAddnew()
{
	CMainFrame *cFrame    = reinterpret_cast<CMainFrame *>(AfxGetMainWnd());
	CFormView * _formtimeline = reinterpret_cast<CFormView *>(cFrame->GetDlgItem(IDD_FORMVIEW2));
	CEdit * _currentFrame = reinterpret_cast<CEdit *> (_formtimeline->GetDlgItem(IDC_EDITCURRENT));
	CEdit * _maxnoFrame = reinterpret_cast<CEdit *> (_formtimeline->GetDlgItem(IDC_EDITMAX));
	CSliderCtrl * _timeline = reinterpret_cast<CSliderCtrl *>( _formtimeline->GetDlgItem(IDC_SLIDER2));
	char buffer[33];
	
	unsigned int  m_maxFrame = _timeline->GetRangeMax();
	m_maxFrame++;
	itoa(m_maxFrame,buffer,10);
    _maxnoFrame->SetWindowText(buffer);
	unsigned int m_currentFrame = m_maxFrame;
	_currentFrame->SetWindowText(buffer);

	
	_timeline->SetRangeMax(m_maxFrame);
	_timeline->SetPos(m_maxFrame);
	
	programFlowManager->getEventManager()->getKeyMouseEventHandler()->clearSelectionGuiElements();
	programFlowManager->getEventManager()->getInputManager()->clearSelection();
	programFlowManager->getRenderManager()->getTimeline()->addFrameToTimeline(0);
	updateGUIForNewFrame();
}


void CMainFrame::OnBnClickedButtonGo()
{
	CMainFrame *cFrame    = reinterpret_cast<CMainFrame *>(AfxGetMainWnd());
	CFormView * _formtimeline = reinterpret_cast<CFormView *>(cFrame->GetDlgItem(IDD_FORMVIEW2));
	CEdit * _gotoFrame = reinterpret_cast<CEdit *> (_formtimeline->GetDlgItem(IDC_EDITGOTO));
	CEdit * _currentFrame = reinterpret_cast<CEdit *> (_formtimeline->GetDlgItem(IDC_EDITCURRENT));
	CSliderCtrl * _timeline = reinterpret_cast<CSliderCtrl *>( _formtimeline->GetDlgItem(IDC_SLIDER2));
	char buffer[33];

	_gotoFrame->GetWindowText(buffer,32);
	unsigned int  m_maxFrame = _timeline->GetRangeMax();
	unsigned int m_gotoFrame = atoi(buffer);
	unsigned int m_currentFrame;
	if((m_gotoFrame <= m_maxFrame) && (m_gotoFrame >= 1))
	{
		m_currentFrame = m_gotoFrame;
		itoa(m_currentFrame,buffer,10);
		_currentFrame->SetWindowText(buffer);
		_timeline->SetPos(m_currentFrame);
		programFlowManager->getEventManager()->getKeyMouseEventHandler()->clearSelectionGuiElements();
		programFlowManager->getEventManager()->getInputManager()->clearSelection();
		programFlowManager->getRenderManager()->getTimeline()->setCurrentFrameNo(m_currentFrame);
		updateGUIForNewFrame();
	}

}



void CMainFrame::OnBnClickedButtonPrev()
{
	CMainFrame *cFrame    = reinterpret_cast<CMainFrame *>(AfxGetMainWnd());
	CFormView * _formtimeline = reinterpret_cast<CFormView *>(cFrame->GetDlgItem(IDD_FORMVIEW2));
	CEdit * _currentFrame = reinterpret_cast<CEdit *> (_formtimeline->GetDlgItem(IDC_EDITCURRENT));
	CSliderCtrl * _timeline = reinterpret_cast<CSliderCtrl *>( _formtimeline->GetDlgItem(IDC_SLIDER2));
	char buffer[33];
	unsigned int m_currentFrame = _timeline->GetPos();
	if(m_currentFrame > 1)
	{
		m_currentFrame--;
		itoa(m_currentFrame,buffer,10);
		_currentFrame->SetWindowText(buffer);
		_timeline->SetPos(m_currentFrame);
		programFlowManager->getEventManager()->getKeyMouseEventHandler()->clearSelectionGuiElements();
		programFlowManager->getEventManager()->getInputManager()->clearSelection();
		programFlowManager->getRenderManager()->getTimeline()->deterateTimeLineHeader();
		updateGUIForNewFrame();
	}
}

void CMainFrame::OnBnClickedButtonNext()
{
	CMainFrame *cFrame    = reinterpret_cast<CMainFrame *>(AfxGetMainWnd());
	CFormView * _formtimeline = reinterpret_cast<CFormView *>(cFrame->GetDlgItem(IDD_FORMVIEW2));
	CEdit * _currentFrame = reinterpret_cast<CEdit *> (_formtimeline->GetDlgItem(IDC_EDITCURRENT));
	CSliderCtrl * _timeline = reinterpret_cast<CSliderCtrl *>( _formtimeline->GetDlgItem(IDC_SLIDER2));
	char buffer[33];
	unsigned int m_currentFrame = _timeline->GetPos();
	unsigned int  m_maxFrame = _timeline->GetRangeMax();
	if(m_currentFrame < m_maxFrame)
	{
		m_currentFrame++;
		itoa(m_currentFrame,buffer,10);
		_currentFrame->SetWindowText(buffer);
		_timeline->SetPos(m_currentFrame);
		programFlowManager->getEventManager()->getKeyMouseEventHandler()->clearSelectionGuiElements();
		programFlowManager->getEventManager()->getInputManager()->clearSelection();
		programFlowManager->getRenderManager()->getTimeline()->iterateTimeLineHeader();
		updateGUIForNewFrame();
	}
}




void CMainFrame::OnBnClickedButtonLast()
{
	CMainFrame *cFrame    = reinterpret_cast<CMainFrame *>(AfxGetMainWnd());
	CFormView * _formtimeline = reinterpret_cast<CFormView *>(cFrame->GetDlgItem(IDD_FORMVIEW2));
	CEdit * _currentFrame = reinterpret_cast<CEdit *> (_formtimeline->GetDlgItem(IDC_EDITCURRENT));
	CSliderCtrl * _timeline = reinterpret_cast<CSliderCtrl *>( _formtimeline->GetDlgItem(IDC_SLIDER2));
	char buffer[33];
	
	unsigned int  m_maxFrame = _timeline->GetRangeMax();
	unsigned int m_currentFrame = m_maxFrame;
	itoa(m_currentFrame,buffer,10);
	_currentFrame->SetWindowText(buffer);
	_timeline->SetPos(m_currentFrame);
	programFlowManager->getEventManager()->getKeyMouseEventHandler()->clearSelectionGuiElements();
	programFlowManager->getEventManager()->getInputManager()->clearSelection();
	programFlowManager->getRenderManager()->getTimeline()->setCurrentFrameNo(m_currentFrame);	
	
	updateGUIForNewFrame();

}


const int MAX = 1000;

void CMainFrame::OnBnClickedButton2()				//Library add button
{
	CString _selectedFilePath  ;
	CString _separator ;
	CString _fileName;
	CString _returnPath;					//this _returnPath returns if selected file is valid
	CString _currentPath;
	
	CFileFind _fileFind;
	char dirbuffer[MAX];



	CFormView * _formLibrary = (CFormView *) GetDlgItem(IDD_FORMVIEW);
	CTreeCtrl * _treeCtrl = (CTreeCtrl *) _formLibrary->GetDlgItem(IDC_TREE1 );
	CEdit * _nodeName = (CEdit *) _formLibrary->GetDlgItem(IDC_EDITNODENAME);
	
	HTREEITEM _item;
	HTREEITEM _parent;


	_separator = "\\";
	_selectedFilePath = "";
	_fileName = "";

	_item = _treeCtrl->GetSelectedItem();

	if(_item)
	{
		_parent = _treeCtrl->GetParentItem(_item);


		while(_item)
		{
			_fileName = _treeCtrl->GetItemText(_item);
			_selectedFilePath =  _separator + _fileName  + _selectedFilePath ;
				
			
			_parent = _treeCtrl->GetParentItem(_item);
			_item = _parent;
		}

		_getcwd(dirbuffer,MAX);
		

		_currentPath = dirbuffer + _selectedFilePath;

		if(_fileFind.FindFile(_currentPath))
		{
			if(!_fileFind.FindNextFile())
				if(!_fileFind.IsDirectory()) 
					_returnPath = _currentPath;
		}
		
		
	}



	//..............................................
	if(!_returnPath.IsEmpty())
	{

//..
	std::string fileToAdd = _returnPath;

	std::string nodeName;
	CString str;
	_nodeName->GetWindowText(str);
	nodeName = str;
	programFlowManager->getRenderManager()->addObjectToProject(fileToAdd, nodeName);
	}

}

 void CMainFrame::OnNMReleasedcaptureSlider1(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
	// TODO: Add your control notification handler code here
	
CFormView * _formSound = (CFormView *) GetDlgItem(IDD_FORMVIEW1);
CSliderCtrl * _slider = (CSliderCtrl *) _formSound->GetDlgItem(IDC_SLIDER1);

float _sound_volume = (float)_slider->GetPos() / 100.0;	

if(alSource)
		alSourcef(alSource, AL_GAIN, _sound_volume);

*pResult = 0;

}
void CMainFrame::OnBnClickedButton3()              //Sound play button
{
	char* alBuffer;            //data for the buffer
ALenum alFormatBuffer;     //for the buffer format
ALsizei alFreqBuffer;      //for the frequency of the buffer
long alBufferLen;          //the bit depth
ALboolean alLoop;          //looped
char * _fileloc;
float _sound_volume = 0.0;


CFormView * _formSound = (CFormView *) GetDlgItem(IDD_FORMVIEW1);
CButton * _toogle_repeat = (CButton *) _formSound->GetDlgItem(IDC_RADIO1);
CSliderCtrl * _slider = (CSliderCtrl *) _formSound->GetDlgItem(IDC_SLIDER1);

_sound_volume = (float)_slider->GetPos() / 100.0;






unsigned int alSampleSet; 


OnBnClickedButton4();            //first stop what is playing [optional]

if(!m_cstrFilePath.IsEmpty())
{
	
	CButton * _loop = (CButton *)this->GetDlgItem(IDC_RADIO1);

	//load the wave file
	//	alutLoadWAVFile(m_cstrFilePath,&alFormatBuffer, (void**) &alBuffer, (ALsizei *)&alBufferLen, &alFreqBuffer, &alLoop);
	_fileloc = m_cstrFilePath.GetBuffer();
	alutLoadWAVFile((ALbyte *)_fileloc,&alFormatBuffer, (void**) &alBuffer, (ALsizei *)&alBufferLen, &alFreqBuffer, &alLoop);

	//create 1 source
	alGenSources(1, &alSource);

	//create 1 buffer
	alGenBuffers(1, &alSampleSet);

	//fills sample set with buffer data from alBuffer
	alBufferData(alSampleSet, alFormatBuffer, alBuffer, alBufferLen, alFreqBuffer);

	//assign the buffer to this source
	alSourcei(alSource, AL_BUFFER, alSampleSet);

	
	//volume control test
	alSourcef(alSource, AL_GAIN, _sound_volume);

	//release the data
	alutUnloadWAV(alFormatBuffer, alBuffer, alBufferLen, alFreqBuffer);

	//tell the sound if? to loop continuously
	
	int _isLoop =0;
	if(_toogle_repeat->GetCheck()) 
									_isLoop = 1;
	

	alSourcei(alSource,AL_LOOPING,_isLoop);

	//play the sound
	alSourcePlay(alSource);

	
}
}

void CMainFrame::OnBnClickedButton4()			//Sound stop button
{
	alSourceStop(alSource);
}

void CMainFrame::GuiInit()
{
	char buffer[33];

	
	CFormView * _formSound = (CFormView *)GetDlgItem(IDD_FORMVIEW1);
	CButton * _radio_play_once = (CButton *)_formSound->GetDlgItem(IDC_RADIO2);

	_radio_play_once->SetCheck(BST_CHECKED);
 
	CSliderCtrl * _slider = (CSliderCtrl *) _formSound->GetDlgItem(IDC_SLIDER1);
	_slider->SetRange(0,100);
	_slider->SetPos(50);
	
	CFormView * _formtimeline = (CFormView *)GetDlgItem(IDD_FORMVIEW2);
	CSliderCtrl * _timeline = (CSliderCtrl *) _formtimeline->GetDlgItem(IDC_SLIDER2);
	_timeline->SetRange(1,2);
	CEdit * _maxnoFrame = (CEdit *) _formtimeline->GetDlgItem(IDC_EDITMAX);
	CEdit * _currentFrame = (CEdit *) _formtimeline->GetDlgItem(IDC_EDITCURRENT);

	buffer[0]=0;
	unsigned int m_currentFrame = 1;
	unsigned int m_maxFrame = 1;
	unsigned int m_gotoFrame = 1;

	itoa(m_maxFrame,buffer,10);
    _maxnoFrame->SetWindowText(buffer);
	itoa(m_currentFrame,buffer,10);
	_currentFrame->SetWindowText(buffer);
    
	
}
void CMainFrame::OpenALInit()
{
	Device = alcOpenDevice("DirectSound3D");

	if (Device == NULL)
	{
		;	//to do something
	}

	alSource = 0;
	//Create context(s)
	Context=alcCreateContext(Device, NULL);
	//Set active context
	alcMakeContextCurrent(Context);
	// Clear Error Code
	alGetError();

	



}


void CMainFrame::OnBnClickedButton1()
{
	// TODO: Add your control notification handler code here
	static char BASED_CODE lpszFilter[] = "My Files (*.wav)|*.wav|All Files|*.*|";
	const DWORD dwFlags = OFN_HIDEREADONLY; 
	// TODO: Open a File Dialog to read .txt files 
	CFileDialog m_MyFileDialog(TRUE, _T(".wav"),NULL,dwFlags, lpszFilter ,NULL ); 


	if (m_MyFileDialog.DoModal() == IDOK)  // This calls the dialog box 
	{ 
		m_cstrFileName = m_MyFileDialog.GetFileName(); 
		m_cstrFilePath = m_MyFileDialog.GetPathName(); 
		

		CFormView * _formSound = (CFormView *)GetDlgItem(IDD_FORMVIEW1);
		CEdit * hEditBox = (CEdit *)_formSound->GetDlgItem(IDC_EDIT1 );
	
		if ( hEditBox  )
		{
			hEditBox->SetWindowText(m_cstrFilePath);
			
		} 

	}
}




void CMainFrame::LeftFormLibraryInit()
{
	pmyImageList = new CImageList();
	pmyImageList->Detach();	// For MDI
	pmyImageList->Create(IDB_BITMAP1, 16, 2, RGB(255, 255, 255));

}
//[e:00.00]03.16.06
#define DIRECTORY	0
#define OBJFILE		1
static char LibraryDir[] = "ObjLibrary";

int ListDirectoryTree(const char * directoryName, HTREEITEM htree, CTreeCtrl* pCtrl)
  {
	int _filetype = DIRECTORY;
	
	CFileFind finder;
	CString _fileName;
	char currentbuffer[MAX];
	char changedbuffer[MAX];

	_getcwd(currentbuffer,MAX);
	_getcwd(changedbuffer,MAX);
	
	strcat(changedbuffer, "\\");
	strcat(changedbuffer , directoryName);
	if(chdir(changedbuffer) <0 ) return -1;
	
	
   BOOL bWorking = finder.FindFile("*.*");
   while (bWorking)
   {
      bWorking = finder.FindNextFile();
      _fileName = finder.GetFileName();
	  if(_fileName.Compare(".") && _fileName.Compare(".."))
	  { 
		//cout <<"file name :" << _fileName << endl;
		if(finder.IsDirectory()) _filetype = DIRECTORY;
		else _filetype = OBJFILE;
		HTREEITEM hPA = pCtrl->InsertItem(_fileName, _filetype, _filetype,  htree, TVI_LAST);
		if(finder.IsDirectory()) ListDirectoryTree((LPCTSTR)_fileName ,hPA , pCtrl);
	  }
   }

	if(chdir(currentbuffer) < 0 ) return -1;

	return 0;

}

void CMainFrame::LeftFormLibraryOnAir()
{
	const int MAX = 1000;
		
		
    CFormView * mWnd = (CFormView *) GetDlgItem(IDD_FORMVIEW);
	CTreeCtrl* pCtrl = (CTreeCtrl*) mWnd->GetDlgItem(IDC_TREE1);
		//CTreeCtrl* pCtrl = &treeCtrl_;
	ASSERT(pCtrl != NULL);
		
	pCtrl->SetImageList(pmyImageList, TVSIL_NORMAL);
	

	
	// Insert a root item using the structure. We must
	// initialize a TVINSERTSTRUCT structure and pass its
	// address to the call. 
	//	pCtrl->SetImageList(pmyImageList,TVSIL_STATE);

	HTREEITEM hRoot = pCtrl->InsertItem(LibraryDir, 0 ,0);

	ListDirectoryTree(LibraryDir, hRoot, pCtrl);


}
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	/*
	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}
*/
	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}

	
    if(!m_wndLeftFormLibrary.Create(this,IDD_FORMVIEW,CBRS_LEFT | CBRS_TOOLTIPS | CBRS_FLYBY,IDD_FORMVIEW))
	{
	TRACE0("Failed to create toolbar\n");
	return -1;
	}

	if(!m_wndBottomFormSound.Create(this,IDD_FORMVIEW1, CBRS_BOTTOM,IDD_FORMVIEW1))
	{
	TRACE0("Failed to create toolbar\n");
	return -1;
	}


	if(!m_wndTopFormTimeLine.Create(this,IDD_FORMVIEW2, CBRS_TOP,IDD_FORMVIEW2))
	{
	TRACE0("Failed to create toolbar\n");
	return -1;
	}

	if(!m_wndRightFormProperties.Create(this,IDD_FORMVIEW3, CBRS_RIGHT,IDD_FORMVIEW3))
	{
	TRACE0("Failed to create toolbar\n");
	return -1;
	}
	

/*	
//////////////////////////////
	if
	(
	!m_wndDialogBar1.Create
	(
	this,
	IDD_DIALOGBAR1,
	CBRS_RIGHT | CBRS_TOOLTIPS | CBRS_FLYBY,
	IDD_DIALOGBAR1
	)
	){
	TRACE0("Failed to create toolbar\n");
	return -1;
	}
	
//////////////////////////////////////
	if
	(
	!m_wndDialogBar2.Create
	(
	this,
	IDD_DIALOGBAR2,
	CBRS_BOTTOM | CBRS_TOOLTIPS | CBRS_FLYBY,
	IDD_DIALOGBAR2
	)
	){
	TRACE0("Failed to create toolbar\n");
	return -1;
	}
////////////////////////////////
	if
	(
	!m_wndDialogBar3.Create
	(
	this,
	IDD_DIALOGBAR3,
	CBRS_RIGHT | CBRS_TOOLTIPS | CBRS_FLYBY,
	IDD_DIALOGBAR3
	)
	){
	TRACE0("Failed to create toolbar\n");
	return -1;
	}
*/
	// TODO: Delete these three lines if you don't want the toolbar to be dockable
//	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
//	EnableDocking(CBRS_ALIGN_ANY);
//	DockControlBar(&m_wndToolBar);
    
//	m_wndDialogBar0.EnableDocking(CBRS_ALIGN_ANY);
//	DockControlBar(&m_wndDialogBar0);

	
	
/*	  
	m_wndDialogBar1.EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndDialogBar1);
	
	m_wndDialogBar2.EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndDialogBar2);
	
	m_wndDialogBar3.EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndDialogBar3);
	*/

//necessary initializations

	LeftFormLibraryInit();
	LeftFormLibraryOnAir();
	GuiInit();
    OpenALInit();
	programFlowManager = new CProgramFlowManager();
	createGUIElements();
	return 0;
}

void CMainFrame::updateGUIForNewFrame()
	{
	CMainFrame *cFrameW    = this;//reinterpret_cast<CMainFrame *>(AfxGetMainWnd());
	CFormView * _formView1W = reinterpret_cast<CFormView *>(cFrameW->GetDlgItem(IDD_FORMVIEW1));

	CEdit * soundFileNameW = reinterpret_cast<CEdit *> (_formView1W->GetDlgItem(IDC_EDIT1));
	CEdit *textObjectW = reinterpret_cast<CEdit *> (_formView1W->GetDlgItem(IDC_EDIT2));
	CButton *attachSoundCheckW = reinterpret_cast<CButton *> (_formView1W->GetDlgItem(IDC_CHECKSOUND));
	CButton *attachTextCheckW = reinterpret_cast<CButton *> (_formView1W->GetDlgItem(IDC_CHECKTEXTCONT));
	
	CFormView * _formView2W = reinterpret_cast<CFormView *>(cFrameW->GetDlgItem(IDD_FORMVIEW2));
	CEdit *frameTimeW = reinterpret_cast<CEdit *> (_formView2W->GetDlgItem(IDC_EDITFRAMETIME));

	int position = programFlowManager->getRenderManager()->getTimeline()->getCurrentFrame()->getPositionInTimeline();
	std::string sound = programFlowManager->getRenderManager()->getTimeline()->getCurrentFrame()->getSoundFileName();
	std::string text = programFlowManager->getRenderManager()->getTimeline()->getCurrentFrame()->getFrameMessage();
	
	if(sound == "")
		{
		attachSoundCheckW->SetCheck(0);
		}
	else
		{
		attachSoundCheckW->SetCheck(1);
		}

	if(text == "")
		{
		attachTextCheckW->SetCheck(0);
		}
	else
		{
		attachTextCheckW->SetCheck(1);
		}

	soundFileNameW->SetWindowText(sound.c_str());
	textObjectW->SetWindowText(text.c_str());

	char buff[33];
	itoa(position, buff, 10);
	frameTimeW->SetWindowText(buff);
	}

CmfcGuiElements* CMainFrame::createGUIElements()
	{

	

	CMainFrame *cFrame    = this;//reinterpret_cast<CMainFrame *>(AfxGetMainWnd());
	CFormView * _formView = reinterpret_cast<CFormView *>(cFrame->GetDlgItem(IDD_FORMVIEW));
	CFormView * _formView1 = reinterpret_cast<CFormView *>(cFrame->GetDlgItem(IDD_FORMVIEW1));
	CFormView * _formView2 = reinterpret_cast<CFormView *>(cFrame->GetDlgItem(IDD_FORMVIEW2));
	CFormView * _formView3 = reinterpret_cast<CFormView *>(cFrame->GetDlgItem(IDD_FORMVIEW3));

	CmfcGuiElements *mfcGui = new CmfcGuiElements(); 

	/////For FormView (Library)///////////
	/////////////////////////////////

	/////For FormView1 (Sound - Text)////////////
	mfcGui->soundFileName = reinterpret_cast<CEdit *> (_formView1->GetDlgItem(IDC_EDIT1));
	mfcGui->textObject = reinterpret_cast<CEdit *> (_formView1->GetDlgItem(IDC_EDIT2));
	mfcGui->attachSoundCheck = reinterpret_cast<CButton *> (_formView1->GetDlgItem(IDC_CHECKSOUND));
	mfcGui->attachTextCheck = reinterpret_cast<CButton *> (_formView1->GetDlgItem(IDC_CHECKTEXTCONT));
	 
	/////////////////////////////////

	/////For FormView2 (TimeLine)////////////
	
	mfcGui->currentFrameText = reinterpret_cast<CEdit *> (_formView2->GetDlgItem(IDC_EDITCURRENT));
	mfcGui->maxnoFrameText = reinterpret_cast<CEdit *> (_formView2->GetDlgItem(IDC_EDITMAX));
	mfcGui->timelineControl = reinterpret_cast<CSliderCtrl *>(_formView2->GetDlgItem(IDC_SLIDER2));
	mfcGui->frameTime = reinterpret_cast<CEdit *> (_formView2->GetDlgItem(IDC_EDITFRAMETIME));
	/////////////////////////////////

	/////For FormView3 (Object)////////////

	mfcGui->objectName = reinterpret_cast<CEdit *> (_formView3->GetDlgItem(IDC_EDITPRONAME));
	mfcGui->objectPositionX = reinterpret_cast<CEdit *> (_formView3->GetDlgItem(IDC_EDITPOSX));
	mfcGui->objectPositionY = reinterpret_cast<CEdit *> (_formView3->GetDlgItem(IDC_EDITPOSY));
	mfcGui->objectPositionZ = reinterpret_cast<CEdit *> (_formView3->GetDlgItem(IDC_EDITPOSZ));
	mfcGui->tweenCheck = reinterpret_cast<CButton *> (_formView3->GetDlgItem(IDC_CHECK1));
	mfcGui->tweenCheck->EnableWindow(FALSE);
	/*
	CEdit * _scaX = reinterpret_cast<CEdit *> (_formpro->GetDlgItem(IDC_EDITSCAX));
	CEdit * _scaY = reinterpret_cast<CEdit *> (_formpro->GetDlgItem(IDC_EDITSCAY));
	CEdit * _scaZ = reinterpret_cast<CEdit *> (_formpro->GetDlgItem(IDC_EDITSCAZ));
	*/
	/////////////////////////////////
	/*
	programFlowManager->getMfcGuiElements()->attachSoundCheck = attachSoundCheck;
	programFlowManager->getMfcGuiElements()->textObject = textObject;
	programFlowManager->getMfcGuiElements()->soundFileName = soundFileName;
	*/
	
	return mfcGui;
	}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CMDIFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
	

	return TRUE;
}


// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CMDIFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CMDIFrameWnd::Dump(dc);
}

#endif //_DEBUG


// CMainFrame message handlers


void CMainFrame::OnBnClickedButton5()
	{
	// TODO: Add your control notification handler code here
	}

void CMainFrame::OnBnClickedCheck1()
	{
	if (programFlowManager->getEventManager()->getInputManager()->getSelectedObjectList().size() == 1)
		{
		osg::Node *nodeSelected = programFlowManager->getEventManager()->getInputManager()->getSelectedObjectList().at(0);
		nodeSelected = CSceneGraphUtilities::findSelectedNodesParentASNode(nodeSelected);
		std::string nodeName = nodeSelected->getName();
		programFlowManager->getRenderManager()->getTimeline()->getCurrentFrame()->getScene()->addTweenToObject(nodeName, nodeSelected);
		}
	}

void CMainFrame::OnBnClickedChecksound()
	{
	CMainFrame *cFrame = this;
	CFormView * _formView = reinterpret_cast<CFormView *>(cFrame->GetDlgItem(IDD_FORMVIEW1));
	CButton * attachSoundCheck = reinterpret_cast<CButton *> (_formView->GetDlgItem(IDC_CHECKSOUND));
	CEdit * soundFileName = reinterpret_cast<CEdit *> (_formView->GetDlgItem(IDC_EDIT1));
	std::string soundName;
	int isChecked = attachSoundCheck->GetCheck();
	if(isChecked)
		{
		CString str;
		soundFileName->GetWindowText(str);
		soundName = str;
		programFlowManager->getRenderManager()->getTimeline()->getCurrentFrame()->setSoundFileName(soundName);
		}
	else
		{
		soundName = "";
		programFlowManager->getRenderManager()->getTimeline()->getCurrentFrame()->setSoundFileName(soundName);
		}
	}

void CMainFrame::OnBnClickedChecktextcont()
	{
	CMainFrame *cFrame = this;
	CFormView * _formView = reinterpret_cast<CFormView *>(cFrame->GetDlgItem(IDD_FORMVIEW1));
	CButton * attachSoundCheck = reinterpret_cast<CButton *> (_formView->GetDlgItem(IDC_CHECKTEXTCONT));
	CEdit * soundFileName = reinterpret_cast<CEdit *> (_formView->GetDlgItem(IDC_EDIT2));
	std::string soundName;
	int isChecked = attachSoundCheck->GetCheck();
	if(isChecked)
		{
		CString str;
		soundFileName->GetWindowText(str);
		soundName = str;
		programFlowManager->getRenderManager()->getTimeline()->getCurrentFrame()->setFrameMessage(soundName);
		}
	else
		{
		soundName = "";
		programFlowManager->getRenderManager()->getTimeline()->getCurrentFrame()->setFrameMessage(soundName);
		}
	}

void CMainFrame::OnBnClickedButtonframetime()
	{
	CMainFrame *cFrameW    = this;
	CFormView * _formView2W = reinterpret_cast<CFormView *>(cFrameW->GetDlgItem(IDD_FORMVIEW2));
	CEdit *frameTimeW = reinterpret_cast<CEdit *> (_formView2W->GetDlgItem(IDC_EDITFRAMETIME));
	
	CString str;
	frameTimeW->GetWindowText(str);
	std::string frameString = str;
	int frameTime;
	frameTime = atoi(frameString.c_str());
	

	programFlowManager->getRenderManager()->getTimeline()->getCurrentFrame()->setPositionInTimeline(frameTime);
	}
