Linux multi-monitor fullscreen support
[ryzomcore.git] / ryzom / tools / leveldesign / variable_parser / variable_parserDlg.cpp
blob60360d47b78ed3628cde0965463f41dd3398e6b6
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 // VariableParserDlg.cpp : implementation file
20 #include "stdafx.h"
21 #include "variable_parser.h"
22 #include "variable_parserDlg.h"
24 /*#ifdef _DEBUG
25 #define new DEBUG_NEW
26 #undef THIS_FILE
27 static char THIS_FILE[] = __FILE__;
28 #endif*/
30 using namespace NLMISC;
31 using namespace std;
33 /////////////////////////////////////////////////////////////////////////////
34 // CVariableParserDlg dialog
36 CVariableParserDlg::CVariableParserDlg(CWnd* pParent /*=NULL*/)
37 : CDialog(CVariableParserDlg::IDD, pParent)
39 //{{AFX_DATA_INIT(CVariableParserDlg)
40 // NOTE: the ClassWizard will add member initialization here
41 //}}AFX_DATA_INIT
42 // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
43 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
46 CVariableParserDlg::~CVariableParserDlg()
48 m_nomVariables.clear();
49 for ( uint i=0; i<m_variables.size(); i++ )
51 m_variables[i].clear();
54 m_variables.clear();
57 void CVariableParserDlg::DoDataExchange(CDataExchange* pDX)
59 CDialog::DoDataExchange(pDX);
60 //{{AFX_DATA_MAP(CVariableParserDlg)
61 DDX_Control(pDX, IDC_VARDEF, m_varDefList);
62 //}}AFX_DATA_MAP
65 BEGIN_MESSAGE_MAP(CVariableParserDlg, CDialog)
66 //{{AFX_MSG_MAP(CVariableParserDlg)
67 ON_WM_PAINT()
68 ON_WM_QUERYDRAGICON()
69 ON_BN_CLICKED(IDC_HDR_BROWSE, OnHdrBrowse)
70 ON_BN_CLICKED(IDC_TMPL_BROWSE, OnTmplBrowse)
71 ON_BN_CLICKED(IDC_FOOT_BROWSE, OnFootBrowse)
72 ON_BN_CLICKED(IDC_OUTPUT_BROWSE, OnOutputBrowse)
73 ON_BN_CLICKED(IDC_GENERATE, OnGenerate)
74 ON_BN_CLICKED(IDC_GEN_BROWSE, OnGenBrowse)
75 ON_BN_CLICKED(IDC_LUA_BROWSE, OnLUABrowse)
76 ON_BN_CLICKED(IDC_ADDVARDEF, OnAddvardef)
77 ON_BN_CLICKED(IDC_REMVARDEF, OnRemvardef)
78 //}}AFX_MSG_MAP
79 END_MESSAGE_MAP()
81 /////////////////////////////////////////////////////////////////////////////
82 // CVariableParserDlg message handlers
84 BOOL CVariableParserDlg::OnInitDialog()
86 CDialog::OnInitDialog();
88 // Set the icon for this dialog. The framework does this automatically
89 // when the application's main window is not a dialog
90 SetIcon(m_hIcon, TRUE); // Set big icon
91 SetIcon(m_hIcon, FALSE); // Set small icon
93 // TODO: Add extra initialization here
95 return TRUE; // return TRUE unless you set the focus to a control
98 // If you add a minimize button to your dialog, you will need the code below
99 // to draw the icon. For MFC applications using the document/view model,
100 // this is automatically done for you by the framework.
102 void CVariableParserDlg::OnPaint()
104 if (IsIconic())
106 CPaintDC dc(this); // device context for painting
108 SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
110 // Center icon in client rectangle
111 int cxIcon = GetSystemMetrics(SM_CXICON);
112 int cyIcon = GetSystemMetrics(SM_CYICON);
113 CRect rect;
114 GetClientRect(&rect);
115 int x = (rect.Width() - cxIcon + 1) / 2;
116 int y = (rect.Height() - cyIcon + 1) / 2;
118 // Draw the icon
119 dc.DrawIcon(x, y, m_hIcon);
121 else
123 CDialog::OnPaint();
127 // The system calls this to obtain the cursor to display while the user drags
128 // the minimized window.
129 HCURSOR CVariableParserDlg::OnQueryDragIcon()
131 return (HCURSOR) m_hIcon;
135 void CVariableParserDlg::OnHdrBrowse()
137 CFileDialog fileDlg( TRUE, NULL, NULL, OFN_OVERWRITEPROMPT, "Primitive file (*.primitive)|*.primitive||");
139 if ( fileDlg.DoModal() == IDOK )
141 GetDlgItem( IDC_HDR_FILE )->SetWindowText( fileDlg.GetPathName() );
146 void CVariableParserDlg::OnTmplBrowse()
148 CFileDialog fileDlg( TRUE, NULL, NULL, OFN_OVERWRITEPROMPT, "Primitive file (*.primitive)|*.primitive||");
150 if ( fileDlg.DoModal() == IDOK )
152 GetDlgItem( IDC_TMPL_FILE )->SetWindowText( fileDlg.GetPathName() );
157 void CVariableParserDlg::OnFootBrowse()
159 CFileDialog fileDlg( TRUE, NULL, NULL, OFN_OVERWRITEPROMPT, "Primitive file (*.primitive)|*.primitive||");
161 if ( fileDlg.DoModal() == IDOK )
163 GetDlgItem( IDC_FOOT_FILE )->SetWindowText( fileDlg.GetPathName() );
168 void CVariableParserDlg::OnOutputBrowse()
170 CFileDialog fileDlg( FALSE, NULL, NULL, OFN_OVERWRITEPROMPT, "Primitive file (*.primitive)|*.primitive||");
172 if ( fileDlg.DoModal() == IDOK )
174 GetDlgItem( IDC_OUTPUT_FILE )->SetWindowText( fileDlg.GetPathName() );
179 void CVariableParserDlg::OnGenBrowse()
181 CFileDialog fileDlg( TRUE, NULL, NULL, OFN_OVERWRITEPROMPT, "Comma Separated Values (*.csv)|*.csv||");
183 if ( fileDlg.DoModal() == IDOK )
185 GetDlgItem( IDC_GEN_FILE )->SetWindowText( fileDlg.GetPathName() );
189 void CVariableParserDlg::OnLUABrowse()
191 CFileDialog fileDlg( TRUE, NULL, NULL, OFN_OVERWRITEPROMPT, "Lua files (*.lua)|*.lua||");
193 if ( fileDlg.DoModal() == IDOK )
195 GetDlgItem( IDC_LUA_FILE )->SetWindowText( fileDlg.GetPathName() );
199 void CleanString( CSString &str )
201 int i = (int)str.size();
202 bool ok = false;
204 while ( !ok && ( i > 0 ) )
206 char c = str.c_str()[i-1];
207 if ( !isalpha(c) && !isdigit(c) && ( c != '_' ) && ( c != ')' ) )
209 i--;
211 else
212 ok = true;
215 str = str.left( i );
218 void CVariableParserDlg::ProcessGeneratorFile( const string& generatorFile )
220 CSString genData, ligne;
221 int colonneVar = 0;
223 genData.readFromFile( generatorFile );
225 ligne = genData.splitTo( "\n", true );
227 while ( ligne != "" )
229 CSString var = ligne.splitTo( ";", true );
230 CleanString( var );
232 if ( var != "" )
234 m_nomVariables.push_back( var ) ;
235 vector<string> vec;
236 m_variables.push_back( vec );
240 while ( genData != "" )
242 ligne = genData.splitTo( "\n", true );
244 if ( ligne != "" )
246 for ( uint i=0;i<m_variables.size(); i++)
248 CSString var = ligne.splitTo( ";", true );
249 CleanString( var );
251 if ( var != "" )
253 m_variables[i].push_back( var );
260 ParseParameters params;
261 BuildParseParameters( params, 0, 0 );
265 void CVariableParserDlg::BuildParseParameters( ParseParameters& params, uint ligne, uint colonne )
267 CSString left = CSString( m_nomVariables[ colonne ] ).left(2);
269 if ( left == "@@" )
271 //if ( colonne+1 >= params.capacity() )
272 params.push_back( "" );
274 CString str = m_variables[colonne][ligne].c_str();
275 params[colonne] = str;
277 if ( colonne+1 == m_variables.size() )
279 ParseTemplate( params );
281 else
283 BuildParseParameters( params, ligne, colonne+1 );
286 else if ( left == "##" )
288 params.push_back( "" );
289 CString str = m_variables[colonne][0].c_str();
291 for ( uint j=(uint)m_nomVariables.size(); j>0; j-- )
293 str.Replace( toString( "C%d", j-1 ).c_str(), params[j-1].c_str() );
296 try
298 m_luaState.executeScript( str.GetBuffer( str.GetLength() ) );
300 catch (ELuaError &e)
302 MessageBox( toString( "%s", e.luaWhat().c_str() ).c_str() );
303 return;
307 m_luaState.push("VAL");
308 m_luaState.getTable(LUA_GLOBALSINDEX);
309 bool ok= false;
310 sint type= m_luaState.type();
312 if ( type == LUA_TBOOLEAN )
314 // get and pop
315 bool val= m_luaState.toBoolean();
316 m_luaState.pop();
317 // set result
318 if ( val )
319 str = "TRUE";
320 else
321 str = "FALSE";
322 ok = true;
324 else if ( type == LUA_TNUMBER )
326 // get and pop
327 double val= m_luaState.toNumber();
328 m_luaState.pop();
329 // set double or integer?
330 if ( val == floor( val ) )
331 str.Format( "%d", sint64( floor( val ) ) );
332 else
333 str.Format( "%.3f", val );
334 ok = true;
336 else if ( type == LUA_TSTRING )
338 // get and pop
339 std::string val;
340 m_luaState.toString(-1, val);
341 m_luaState.pop();
342 // set result
343 str = val.c_str();
344 ok = true;
347 if ( !ok )
349 MessageBox( toString( "VAL is not defined on column %d", colonne ).c_str() );
351 else
353 params[colonne] = str;
354 if ( colonne+1 == m_variables.size() )
356 ParseTemplate( params );
358 else
360 BuildParseParameters( params, ligne, colonne+1 );
364 else
366 for ( uint i=0; i<m_variables[colonne].size(); i++ )
368 //if ( colonne+1 >= params.capacity() )
369 params.push_back( "" );
371 CString str;
373 str = m_variables[colonne][i].c_str();
375 params[colonne] = str;
377 if ( colonne+1 == m_variables.size() )
379 ParseTemplate( params );
381 else
383 BuildParseParameters( params, i, colonne+1 );
390 void CVariableParserDlg::ParseTemplate( const ParseParameters& params )
392 CString tmp = m_templateText.c_str();
394 for ( uint i=0; i< m_nomVariables.size(); i++ )
396 CSString toFind;
397 if ( CSString( m_nomVariables[ i ] ).left(2) == "@@" )
399 toFind = toString( "%s", m_nomVariables[i].c_str() );
401 else if ( CSString( m_nomVariables[ i ] ).left(2) == "##" )
403 toFind = toString( "%s", m_nomVariables[i].c_str() );
405 else
406 toFind = toString( "$$%s", m_nomVariables[i].c_str() );
408 tmp.Replace( toFind.c_str(), params[i].c_str() );
411 m_outputText += tmp;
414 void CVariableParserDlg::OnGenerate()
416 CString error = "";
418 CSString hdr, foot;
419 CString hdrFilename, templFileName, footFileName,
420 outputFileName, luaFileName;
423 GetDlgItem( IDC_HDR_FILE )->GetWindowText( hdrFilename );
424 if ( hdrFilename == "" )
425 error += "Header file name not set.\n";
427 GetDlgItem( IDC_FOOT_FILE )->GetWindowText( footFileName );
428 if ( footFileName == "" )
429 error += "Foot file name not set.\n";
431 GetDlgItem( IDC_TMPL_FILE )->GetWindowText( templFileName );
432 if ( templFileName == "" )
433 error += "Template file name not set.\n";
435 if ( m_varDefList.GetCount() == 0 )
436 error += "Generator file name not set.\n";
438 GetDlgItem( IDC_OUTPUT_FILE )->GetWindowText( outputFileName );
439 if ( outputFileName == "" )
440 error += "Output file name not set.";
442 if ( error != "" )
444 MessageBox( error, NULL, MB_OK );
446 else
448 GetDlgItem( IDC_LUA_FILE )->GetWindowText( luaFileName );
449 if ( luaFileName != "" )
451 m_luaState.executeFile( std::string( luaFileName ) );
454 hdr.readFromFile( std::string( hdrFilename ) );
455 foot.readFromFile( std::string( footFileName ) );
456 m_templateText.readFromFile( std::string( templFileName ) );
458 m_outputText = hdr;
459 for ( int i=0; i<m_varDefList.GetCount(); i++ )
461 CString str;
462 m_varDefList.GetText( i, str );
463 ProcessGeneratorFile( std::string( str ) );
464 m_variables.clear();
467 m_outputText += foot;
468 m_outputText.writeToFile( std::string( outputFileName ) );
470 MessageBox( "Done !" );
474 void CVariableParserDlg::OnAddvardef()
476 CFileDialog fileDlg( TRUE, NULL, NULL, OFN_OVERWRITEPROMPT|OFN_ALLOWMULTISELECT,
477 "Comma Separated Values (*.csv)|*.csv||");
479 if ( fileDlg.DoModal() == IDOK )
481 for ( POSITION pos = fileDlg.GetStartPosition(); pos != NULL; )
483 CString str = fileDlg.GetNextPathName(pos);
484 int index = m_varDefList.FindStringExact( 0, str );
485 if ( index == LB_ERR )
487 m_varDefList.SetCurSel( m_varDefList.AddString( str ) );
489 else
490 m_varDefList.SetCurSel( index );
495 void CVariableParserDlg::OnRemvardef()
497 int index = m_varDefList.GetCurSel();
498 if ( index != LB_ERR )
499 m_varDefList.DeleteString( index );