Merge branch 'fixes' into main/rendor-staging
[ryzomcore.git] / ryzom / tools / leveldesign / georges_convert / item.cpp
blob0f703828a43f595bf27f8f07af825fec80e7189f
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 #include "stdgeorgesconvert.h"
18 #include "item.h"
19 #include "georges_loader.h"
20 #include "mold_elt_define.h"
21 #include "mold_elt_type.h"
22 #include "item_elt_atom.h"
23 #include "item_elt_struct.h"
24 #include "item_elt_list.h"
25 #include "common.h"
27 namespace NLOLDGEORGES
30 //////////////////////////////////////////////////////////////////////
31 // Construction/Destruction
32 //////////////////////////////////////////////////////////////////////
34 CItem:: CItem()
36 pitemes = 0;
37 // pitemelparents = 0;
38 // pitemeacomments = 0;
41 CItem::~CItem()
43 Clear();
46 void CItem::Clear()
48 if( pitemes )
49 delete( pitemes );
50 pitemes = 0;
51 vsxparents.clear();
53 if( pitemelparents )
54 delete( pitemelparents );
55 pitemes = 0;
56 if( pitemeacomments )
57 delete( pitemeacomments );
58 pitemes = 0;
62 void CItem::SetLoader( CLoader* const _pl )
64 nlassert( _pl );
65 pl = _pl;
68 void CItem::Load( const CStringEx& _sxfullname )
70 Clear();
72 // Load the form
73 CForm form, formcurrent, formparent;
74 pl->LoadForm( formcurrent, _sxfullname );
76 // Load Parents
77 unsigned int i = 0;
78 CStringEx sxparent = formcurrent.GetParent( 0 );
79 CStringEx sxactivity = formcurrent.GetActivity( 0 );
80 while( !sxparent.empty() )
82 vsxparents.push_back( std::make_pair( sxactivity, sxparent ) );
83 if( sxactivity == "true" )
85 pl->LoadSearchForm( form, sxparent );
86 formparent += form;
88 i++;
89 sxparent = formcurrent.GetParent( i );
90 sxactivity = formcurrent.GetActivity( i );
93 // Find the name of the dfn file
94 int ipos = _sxfullname.reverse_find('.');
95 if( ipos == -1 )
96 return;
97 moldfilename = _sxfullname.get_right(_sxfullname.length()-ipos-1);
98 moldfilename += ".dfn";
100 // Load the mold and build the item's tree
101 CMoldElt* pme = pl->LoadMold( moldfilename );
102 CMoldEltDefine* pmed = dynamic_cast< CMoldEltDefine* >( pme );
103 nlassert( pmed );
104 pitemes = new CItemEltStruct( pl );
105 pitemes->BuildItem( pmed );
107 // Fill the tree with parents' form fields
108 pitemes->FillParent( formparent.GetBody() );
110 // Fill the tree with current's form
111 pitemes->FillCurrent( formcurrent.GetBody() );
114 // Convert CItem to a CForm (in is this)
115 void CItem::MakeForm (CForm &out)
117 if( !pitemes )
118 return;
119 pitemes->BuildForm( out.GetBody(), vsxparents );
122 // Convert CForm to CItem (out is this)
123 void CItem::MakeItem (CForm &in)
125 CForm form, formparent;
127 Clear();
128 unsigned int i = 0;
129 CStringEx sxparent = in.GetParent( 0 );
130 CStringEx sxactivity = in.GetActivity( 0 );
131 while( !sxparent.empty() )
133 vsxparents.push_back( std::make_pair( sxactivity, sxparent ) );
134 if( sxactivity == "true" )
136 pl->LoadSearchForm( form, sxparent );
137 formparent += form;
139 i++;
140 sxparent = in.GetParent( i );
141 sxactivity = in.GetActivity( i );
144 CMoldElt* pme = pl->LoadMold( moldfilename );
145 CMoldEltDefine* pmed = dynamic_cast< CMoldEltDefine* >( pme );
146 nlassert( pmed );
147 pitemes = new CItemEltStruct( pl );
148 pitemes->BuildItem( pmed );
150 pitemes->FillParent( formparent.GetBody() );
151 pitemes->FillCurrent( in.GetBody() );
154 void CItem::VirtualSaveLoad()
156 //-------save
157 if( !pitemes )
158 return;
159 CForm form, formcurrent, formparent;
160 pitemes->BuildForm( formcurrent.GetBody(), vsxparents );
161 //------load
162 Clear();
163 unsigned int i = 0;
164 CStringEx sxparent = formcurrent.GetParent( 0 );
165 CStringEx sxactivity = formcurrent.GetActivity( 0 );
166 while( !sxparent.empty() )
168 vsxparents.push_back( std::make_pair( sxactivity, sxparent ) );
169 if( sxactivity == "true" )
171 pl->LoadSearchForm( form, sxparent );
172 formparent += form;
174 i++;
175 sxparent = formcurrent.GetParent( i );
176 sxactivity = formcurrent.GetActivity( i );
179 CMoldElt* pme = pl->LoadMold( moldfilename );
180 CMoldEltDefine* pmed = dynamic_cast< CMoldEltDefine* >( pme );
181 nlassert( pmed );
182 pitemes = new CItemEltStruct( pl );
183 pitemes->BuildItem( pmed );
185 pitemes->FillParent( formparent.GetBody() );
186 pitemes->FillCurrent( formcurrent.GetBody() );
189 void CItem::New( const CStringEx& _sxdfnfilename )
191 Clear();
193 moldfilename = _sxdfnfilename;
195 CMoldElt* pme = pl->LoadMold( _sxdfnfilename );
196 CMoldEltDefine* pmed = dynamic_cast< CMoldEltDefine* >( pme );
197 nlassert( pmed );
198 pitemes = new CItemEltStruct( pl );
199 pitemes->BuildItem( pmed );
202 void CItem::Load( const CStringEx& _sxfilename, const CStringEx _sxdate )
206 void CItem::Save( const CStringEx& _sxfilename )
208 if( !pitemes )
209 return;
210 CForm form;
211 pitemes->BuildForm( form.GetBody(), vsxparents );
212 pl->SaveForm( form, _sxfilename );
215 CItemElt* CItem::GetElt( const unsigned int _index ) const
217 if( pitemes )
218 return( pitemes->GetElt( _index ) );
219 return( 0 );
222 CItemElt* CItem::GetElt( const CStringEx _sxname ) const
224 if( pitemes )
225 return( pitemes->GetElt( _sxname ) );
226 return( 0 );
229 void CItem::SetCurrentValue( const unsigned int _index, const CStringEx s )
231 CItemElt* pie = GetElt( _index );
232 if( !pie )
233 return;
234 pie->SetCurrentValue( s );
235 pitemes->SetModified( _index );
238 uint CItem::GetNbElt() const
240 if( pitemes )
241 return( pitemes->GetNbElt() );
242 return( 0 );
245 uint CItem::GetNbParents() const
247 return (uint)vsxparents.size();
250 uint CItem::GetNbElt( const unsigned int _index ) const
252 CItemElt* pie = GetElt( _index );
253 if( !pie )
254 return( 0 );
255 return( pie->GetNbElt() );
258 uint CItem::GetInfos( const unsigned int _index ) const
260 CItemElt* pie = GetElt( _index );
261 if( !pie )
262 return( 0 );
263 return( pie->GetInfos() );
266 CStringEx CItem::GetName( const unsigned int _index ) const
269 CItemElt* pie = GetElt( _index );
270 if( !pie )
272 CStringEx object;
273 return( object );
275 return( pie->GetName() );
278 CStringEx CItem::GetCurrentResult( const unsigned int _index ) const
280 CItemElt* pie = GetElt( _index );
281 if( !pie )
283 CStringEx object;
284 return( object );
286 return( pie->GetCurrentResult() );
289 CStringEx CItem::GetCurrentValue( const unsigned int _index ) const
291 CItemElt* pie = GetElt( _index );
292 if( !pie )
294 CStringEx object;
295 return( object );
297 return( pie->GetCurrentValue() );
300 bool CItem::Update()
302 if( !pitemes )
303 return false;
304 bool b = false;
305 unsigned int i = 0;
306 CItemElt* pie = pitemes->GetElt( i++ );
307 while( pie )
309 CStringEx sxold = pie->GetCurrentValue();
310 pie->SetParentValue( pie->GetParentValue() );
311 pie->SetCurrentValue( pie->GetCurrentValue() );
312 b |= ( sxold != pie->GetCurrentValue() );
313 pie = pitemes->GetElt( i++ );
315 return b;
318 CStringEx CItem::GetFormula( const unsigned int _index ) const
320 CItemElt* pie = GetElt( _index );
321 if( !pie )
323 CStringEx object;
324 return( object );
326 return( pie->GetFormula() );
329 bool CItem::IsEnum( const unsigned int _index ) const
331 CItemElt* pie = GetElt( _index );
332 if( !pie )
333 return( false );
334 return( ( pie->GetInfos() & ITEM_ISENUM ) != 0 );
337 bool CItem::IsPredef( const unsigned int _index ) const
339 CItemElt* pie = GetElt( _index );
340 if( !pie )
341 return( false );
342 CItemEltAtom* piea = dynamic_cast< CItemEltAtom* >( pie );
343 if( !piea )
344 return( false );
345 CMoldEltType* pmet = piea->GetMoldType();
346 CStringEx sx = pmet->GetPredefDesignation( 0 );
347 return( !sx.empty() );
350 bool CItem::CanEdit( const unsigned int _index ) const
352 CItemElt* pie = GetElt( _index );
353 if( !pie )
354 return( false );
355 CItemEltAtom* piea = dynamic_cast< CItemEltAtom* >( pie );
356 if( !piea )
357 return( false );
358 return( !( pie->GetInfos() & ITEM_ISENUM ) );
361 void CItem::GetListPredef( const unsigned int _index, std::vector< CStringEx >& _vsx ) const
363 CItemElt* pie = GetElt( _index );
364 if( !pie )
365 return;
366 CItemEltAtom* piea = dynamic_cast< CItemEltAtom* >( pie );
367 if( !piea )
368 return;
370 CMoldEltType* pmet = piea->GetMoldType();
372 unsigned int i = 0;
373 CStringEx sx = pmet->GetPredefDesignation( i++ );
374 while( !sx.empty() )
376 _vsx.push_back( sx );
377 sx = pmet->GetPredefDesignation( i++ );
381 CStringEx CItem::GetParent( const unsigned int _index ) const
383 if( _index >= vsxparents.size() )
385 CStringEx object;
386 return( object );
388 return( vsxparents[_index].second );
391 void CItem::SetParent( const unsigned int _index, const CStringEx _sx )
393 if( _index >= vsxparents.size() )
394 return;
395 vsxparents[_index].second = _sx;
398 CStringEx CItem::GetActivity( const unsigned int _index ) const
400 if( _index >= vsxparents.size() )
402 CStringEx object;
403 return( object );
405 return( vsxparents[_index].first );
408 void CItem::SetActivity( const unsigned int _index, const CStringEx _sx )
410 if( _index >= vsxparents.size() )
411 return;
412 vsxparents[_index].first = _sx;
415 void CItem::AddList( const unsigned int _index ) const
417 CItemElt* pie = GetElt( _index );
418 if( !pie )
419 return;
420 CItemEltList* piel = dynamic_cast< CItemEltList* >( pie );
421 if( !piel )
422 return;
423 piel->NewElt();
426 void CItem::AddListChild( const unsigned int _index ) const
428 CItemElt* pie = GetElt( _index );
429 if( !pie )
430 return;
431 CItemElt* piep = pie->GetListParent();
432 if( !piep )
433 return;
434 CItemEltList* piel = dynamic_cast< CItemEltList* >( piep );
435 if( !piel )
436 return;
437 piel->AddElt( pie );
440 void CItem::DelListChild( const unsigned int _index ) const
442 CItemElt* pie = GetElt( _index );
443 if( !pie )
444 return;
445 CItemElt* piep = pie->GetListParent();
446 if( !piep )
447 return;
448 CItemEltList* piel = dynamic_cast< CItemEltList* >( piep );
449 if( !piel )
450 return;
451 piel->DelElt( pie );
454 void CItem::AddParent( const unsigned int _index )
456 vsxparents.push_back( std::make_pair( CStringEx("false"), CStringEx("filename.extension") ) );
459 void CItem::DelParent( const unsigned int _index )
461 unsigned int i = 0;
462 for( std::vector< std::pair< CStringEx, CStringEx > >::iterator it = vsxparents.begin(); it != vsxparents.end(); ++it )
463 if( i++ == _index )
465 vsxparents.erase( it );
466 return;
470 void CItem::ClearParents ()
472 vsxparents.clear();