// OAGMFCDoc.cpp : implementation of the COAGMFCDoc class
//
#include "stdafx.h"
#include "OAGMFC.h"
#include "OAGMFCDoc.h"
#include <OAGPrimitives.h>
#include <OAGTextureLoader.h>
#include <OAGFontLoader.h>
#include <OAGParser.h>
#include <XmlDocument.h>
#include <XmlWriterDocument.h>
#include "OAGMFCView.h"
//#include <OAGFont2D.h>
//#include <OAGTextureLoader.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// COAGMFCDoc
IMPLEMENT_DYNCREATE(COAGMFCDoc, CDocument)
BEGIN_MESSAGE_MAP(COAGMFCDoc, CDocument)
ON_COMMAND(ID_FILE_OPEN, &COAGMFCDoc::OnFileOpen)
ON_COMMAND(ID_FILE_SAVE, &COAGMFCDoc::OnFileSave)
ON_COMMAND(ID_INSERT_TEXTURE, &COAGMFCDoc::OnInsertTexture)
ON_COMMAND(ID_INSERT_FONT, &COAGMFCDoc::OnInsertFont)
END_MESSAGE_MAP()
// COAGMFCDoc construction/destruction
COAGMFCDoc::COAGMFCDoc()
{
CreateLibraryObjects();
}
COAGMFCDoc::~COAGMFCDoc()
{
UnloadLibraryObjects();
}
BOOL COAGMFCDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
//Deleting all library objects when a new document is started.
//The Fonts must be deleted and created again,
//if not they are not drawn properly
UnloadLibraryObjects();
CreateLibraryObjects();
UpdateAllViews(NULL);
return TRUE;
}
// COAGMFCDoc serialization
void COAGMFCDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
}
// COAGMFCDoc diagnostics
#ifdef _DEBUG
void COAGMFCDoc::AssertValid() const
{
CDocument::AssertValid();
}
void COAGMFCDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
// COAGMFCDoc commands
void COAGMFCDoc::CreateLibraryObjects()
{
m_pScene = new oag::OAGScene();
m_pTextureMappingTable = new oag::TextureMappingTable();
m_pScene->SetTextureMappingTable( m_pTextureMappingTable );
m_pFontMappingTable = new oag::FontMappingTable();
m_pScene->SetFontMappingTable( m_pFontMappingTable );
}
void COAGMFCDoc::UnloadLibraryObjects()
{
//Deletes the scene and all objects from the memory
if ( m_pScene )
{
m_pScene->DeleteAllObjects();
delete m_pScene;
m_pScene = NULL;
}
if( m_pTextureMappingTable )
{
delete m_pTextureMappingTable;
m_pTextureMappingTable = NULL;
}
if( m_pFontMappingTable )
{
delete m_pFontMappingTable;
m_pFontMappingTable = NULL;
}
}
void COAGMFCDoc::CreateNewGeometry2D(int nType)
{
switch( nType )
{
case 1: //Line
{
oag::OAGPrimitives* pGeo = new oag::OAGPrimitives();
pGeo->SetGeometryType( GL_LINES );
m_pScene->AddObject( pGeo );
}
break;
case 2: //Polyline
{
oag::OAGPrimitives* pGeo = new oag::OAGPrimitives();
pGeo->SetGeometryType( GL_LINE_STRIP );
m_pScene->AddObject( pGeo );
}
break;
case 3: //Rectangle
{
oag::OAGPrimitives* pGeo = new oag::OAGPrimitives();
pGeo->SetGeometryType( GL_QUADS );
m_pScene->AddObject( pGeo );
}
break;
case 4: //Triangle
{
oag::OAGPrimitives* pGeo = new oag::OAGPrimitives();
pGeo->SetGeometryType( GL_TRIANGLES );
m_pScene->AddObject( pGeo );
}
break;
}
}
void COAGMFCDoc::OnFileOpen()
{
CString filter;
filter.LoadString( IDS_OAGXML );
CFileDialog dlg(TRUE, "*.oagxml", NULL, OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST, filter );
if(dlg.DoModal() == IDOK)
{
m_pScene->DeleteAllObjects();
SetPathName( dlg.GetPathName() );
POSITION pos = GetFirstViewPosition();
COAGMFCView* pView = (COAGMFCView*) GetNextView(pos);
if ( pView->m_pWinGraphicContext->MakeCurrent() )
{
oag::OAGParser parser;
parser.LoadFile( dlg.GetPathName().GetString(), m_pScene );
pView->m_pWinGraphicContext->DeleteCurrent();
UpdateAllViews(NULL);
}
AfxMessageBox("File Loaded");
}
}
void COAGMFCDoc::OnFileSave()
{
#if defined(LIBXML_WRITER_ENABLED) && defined(LIBXML_OUTPUT_ENABLED)
#define MY_ENCODING "ISO-8859-1"
CString fileSave = GetPathName();
if ( fileSave.IsEmpty() )
{
CString filter;
filter.LoadString( IDS_OAGXML );
CFileDialog dlg(FALSE, "*.oagxml", NULL, OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST, filter );
if( dlg.DoModal() == IDOK )
{
fileSave = dlg.GetPathName().GetString();
}
}
if ( !fileSave.IsEmpty() )
{
CXmlWriterDocument writerDoc;
CXmlNode* nodeRoot = new CXmlNode("OAGLibrary");
CXmlNode* nodeScene = new CXmlNode("Scene");
nodeRoot->AppendChild( nodeScene );
CXmlNode* pNodeTables = new CXmlNode("Tables");
nodeScene->AppendChild( pNodeTables );
m_pScene->GetTextureMappingTable()->SaveNodeXML( pNodeTables );
m_pScene->GetFontMappingTable()->SaveNodeXML( pNodeTables );
CXmlNode* NodeObj = new CXmlNode("Objects");
nodeScene->AppendChild(NodeObj);
for( vector<int>::size_type i = 0; i < m_pScene->GetObjectCount(); ++i )
{
oag::OAGObject* obj = m_pScene->GetObjectAt(i);
obj->SaveNodeXML(NodeObj);
}
writerDoc.WriteFile( fileSave.GetString(), nodeRoot);
delete nodeRoot;
}
#endif
}
void COAGMFCDoc::OnInsertTexture()
{
CString filter;
filter.LoadString( IDS_TEXTURE_FILTER );
CFileDialog dlg(TRUE, "*.*", NULL, OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST, filter );
if(dlg.DoModal() == IDOK)
{
CString strFileName = dlg.GetPathName().GetString();
POSITION pos = GetFirstViewPosition();
COAGMFCView* pView = (COAGMFCView*) GetNextView(pos);
if ( pView->m_pWinGraphicContext->MakeCurrent() )
{
oag::OAGTexture* pTexture = oag::OAGTextureLoader::LoadTextureFromDisk( strFileName.GetString() );
if( pTexture )
{
pTexture->SetDefaultFilter();
pTexture->BuildTexture();
pTexture->SetTextureName("Image1");
m_pScene->GetTextureMappingTable()->AddTexture( pTexture );
}
pView->m_pWinGraphicContext->DeleteCurrent();
}
}
}
void COAGMFCDoc::OnInsertFont()
{
CString filter;
filter.LoadString( IDS_FONT_FILTER );
CFileDialog dlg(TRUE, "*.ttf", NULL, OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST, filter );
if(dlg.DoModal() == IDOK)
{
CString strFileName = dlg.GetPathName().GetString();
POSITION pos = GetFirstViewPosition();
COAGMFCView* pView = (COAGMFCView*) GetNextView(pos);
if ( pView->m_pWinGraphicContext->MakeCurrent() )
{
oag::OAGFont* pFont = oag::OAGFontLoader::LoadFontFromDisk( strFileName.GetString(), OAG_FONT_MAP_2D );
if( pFont )
{
pFont->SetFontSize(24);
pFont->SetFontName("arial");
m_pScene->GetFontMappingTable()->AddFont( pFont );
}
pView->m_pWinGraphicContext->DeleteCurrent();
}
}
}