Linux multi-monitor fullscreen support
[ryzomcore.git] / nel / tools / logic / logic_editor_dll / Condition.cpp
blob8caa9d96770e37fbe5f5bd5507a1b17692c77c14
1 // Condition.cpp: implementation of the CCondition class.
2 //
3 //////////////////////////////////////////////////////////////////////
5 #include "stdafx.h"
6 #include "logic_editor.h"
7 #include "condition.h"
8 #include "nel/logic/logic_condition.h"
10 #include <vector>
12 #ifdef _DEBUG
13 #undef THIS_FILE
14 static char THIS_FILE[]=__FILE__;
15 #define new DEBUG_NEW
16 #endif
18 using namespace std;
19 using namespace NLLOGIC;
21 //////////////////////////////////////////////////////////////////////
22 // CConditionNode implementation
23 //////////////////////////////////////////////////////////////////////
25 CConditionNode::CConditionNode(const CConditionNode &node)
27 this->m_dComparand = node.m_dComparand;
28 this->m_pParentNode = node.m_pParentNode;
29 this->m_sConditionName = node.m_sConditionName;
30 this->m_sOperator = node.m_sOperator;
31 this->m_sVariableName = node.m_sVariableName;
32 this->m_type = node.m_type;
34 // copy sub condition node tree
35 CConditionNode *pNode, *newNode;
36 POSITION pos = node.m_ctSubTree.GetHeadPosition();
37 while (pos != NULL)
39 pNode = m_ctSubTree.GetNext( pos );
41 if (pNode != NULL)
43 newNode = new CConditionNode( *pNode );
44 this->m_ctSubTree.AddTail( newNode );
45 newNode->m_pParentNode = this;
53 CConditionNode::~CConditionNode()
55 CConditionNode *pNode = NULL;
57 // delete all sub nodes
58 POSITION pos = m_ctSubTree.GetHeadPosition();
59 while (pos != NULL)
61 pNode = m_ctSubTree.GetNext( pos );
63 if (pNode != NULL)
65 delete pNode;
66 pNode = NULL;
71 const CString & CConditionNode::getNodeAsString() const
73 m_sNodeString.Empty();
75 if (m_type == NOT)
76 m_sNodeString = "NOT";
77 else if (m_type == TERMINATOR)
78 m_sNodeString = "term";
79 else if (m_type == SUB_CONDITION)
80 m_sNodeString = m_sConditionName;
81 else // comparison
83 m_sNodeString.Format(_T("%s %s %g"),LPCTSTR(m_sVariableName),LPCTSTR(m_sOperator), m_dComparand );
86 return m_sNodeString;
91 void CConditionNode::changeConditionName( const CString &old, const CString &newName)
93 CConditionNode *pNode = NULL;
95 POSITION pos = m_ctSubTree.GetHeadPosition();
96 while (pos != NULL)
98 pNode = m_ctSubTree.GetNext( pos );
100 if (pNode != NULL)
102 pNode->changeConditionName(old, newName);
107 if ( m_sConditionName == old)
108 m_sConditionName = newName;
111 void CConditionNode::conditionDeleted( const CString &name)
113 CConditionNode *pNode = NULL;
115 POSITION oldpos;
116 POSITION pos = m_ctSubTree.GetHeadPosition();
117 while (pos != NULL)
119 oldpos = pos;
120 pNode = m_ctSubTree.GetNext( pos );
122 if (pNode != NULL)
124 if ( pNode->m_sConditionName != name)
125 pNode->conditionDeleted(name);
126 else
128 this->m_ctSubTree.RemoveAt( oldpos );
129 delete pNode;
130 pNode = NULL;
140 //////////////////////////////////////////////////////////////////////
141 // CCondition implementation
142 //////////////////////////////////////////////////////////////////////
143 CCondition::CCondition()
148 CCondition::CCondition( const CCondition &cond)
150 this->m_sName = cond.m_sName;
152 // copy sub condition node tree
153 CConditionNode *pNode, *newNode;
154 POSITION pos = cond.m_ctConditionTree.GetHeadPosition();
155 while (pos != NULL)
157 pNode = m_ctConditionTree.GetNext( pos );
159 if (pNode != NULL)
161 newNode = new CConditionNode( *pNode );
162 this->m_ctConditionTree.AddTail( newNode );
169 CCondition::~CCondition()
171 CConditionNode *pNode = NULL;
173 // delete all sub nodes
174 POSITION pos = m_ctConditionTree.GetHeadPosition();
175 while (pos != NULL)
177 pNode = m_ctConditionTree.GetNext( pos );
179 if (pNode != NULL)
181 delete pNode;
182 pNode = NULL;
189 void CCondition::changeConditionName( CString old, const CString &newName) const
191 CConditionNode *pNode = NULL;
193 POSITION pos = m_ctConditionTree.GetHeadPosition();
194 while (pos != NULL)
196 pNode = m_ctConditionTree.GetNext( pos );
198 if (pNode != NULL)
200 pNode->changeConditionName( old, newName);
206 void CCondition::conditionDeleted( CString name)
208 CConditionNode *pNode = NULL;
210 POSITION oldpos;
211 POSITION pos = m_ctConditionTree.GetHeadPosition();
212 while (pos != NULL)
214 oldpos = pos;
215 pNode = m_ctConditionTree.GetNext( pos );
217 if (pNode != NULL)
219 if ( pNode->m_sConditionName != name)
220 pNode->conditionDeleted(name);
221 else
223 this->m_ctConditionTree.RemoveAt( oldpos );
224 delete pNode;
225 pNode = NULL;
233 //-----------------------------------------------------
234 // cConditionNodeToCLogicConditionNode (Editor --> Service)
236 //-----------------------------------------------------
237 void cConditionNodeToCLogicConditionNode(CConditionNode * conditionNode, CLogicConditionNode * logicConditionNode )
239 // if this node is a terminator node
240 if( conditionNode->m_type == CConditionNode::TERMINATOR )
242 logicConditionNode->Type = CLogicConditionNode::TERMINATOR;
244 else
245 // this node is a logic node
247 logicConditionNode->Type = CLogicConditionNode::LOGIC_NODE;
249 // part 1 : a logic block(not/comparison/subcondition)
250 switch( conditionNode->m_type )
252 case CConditionNode::NOT :
254 logicConditionNode->LogicBlock.Type = CLogicConditionLogicBlock::NOT;
257 break;
259 case CConditionNode::COMPARISON :
261 logicConditionNode->LogicBlock.Type = CLogicConditionLogicBlock::COMPARISON;
263 logicConditionNode->LogicBlock.ComparisonBlock.VariableName = NLMISC::tStrToUtf8(conditionNode->m_sVariableName);
264 logicConditionNode->LogicBlock.ComparisonBlock.Operator = NLMISC::tStrToUtf8(conditionNode->m_sOperator);
265 logicConditionNode->LogicBlock.ComparisonBlock.Comparand = (sint64)conditionNode->m_dComparand;
267 break;
269 case CConditionNode::SUB_CONDITION :
271 logicConditionNode->LogicBlock.Type = CLogicConditionLogicBlock::SUB_CONDITION;
273 logicConditionNode->LogicBlock.SubCondition = NLMISC::tStrToUtf8(conditionNode->m_sConditionName);
275 break;
278 // part 2 : a condition sub tree
279 POSITION pos;
280 for( pos = conditionNode->m_ctSubTree.GetHeadPosition(); pos != NULL; )
282 CConditionNode * pConditionNode = conditionNode->m_ctSubTree.GetNext( pos );
283 CLogicConditionNode * logicConditionNodeTmp = new CLogicConditionNode();
284 cConditionNodeToCLogicConditionNode( pConditionNode, logicConditionNodeTmp );
285 logicConditionNode->addNode( logicConditionNodeTmp );
289 } // cConditionNodeToCLogicConditionNode //
294 //-----------------------------------------------------
295 // cConditionToCLogicCondition (Editor --> Service)
297 //-----------------------------------------------------
298 void cConditionToCLogicCondition( CCondition& condition, CLogicCondition& logicCondition )
300 // condition name
301 logicCondition.setName(NLMISC::tStrToUtf8(condition.m_sName));
303 // nodes
304 POSITION pos;
305 for( pos = condition.m_ctConditionTree.GetHeadPosition(); pos != NULL; )
307 // get the node
308 CConditionNode * pConditionNode = condition.m_ctConditionTree.GetNext( pos );
310 // convert the node
311 CLogicConditionNode * logicConditionNode = new CLogicConditionNode();
312 cConditionNodeToCLogicConditionNode( pConditionNode, logicConditionNode );
314 // add the node
315 logicCondition.addNode( *logicConditionNode );
318 } // cConditionToCLogicCondition //
325 //-----------------------------------------------------
326 // cLogicConditionNodeToCConditionNode (Service --> Editor)
328 //-----------------------------------------------------
329 void cLogicConditionNodeToCConditionNode( const CLogicConditionNode * logicConditionNode, CConditionNode * node )
331 // terminator node
332 if(logicConditionNode->Type == CLogicConditionNode::TERMINATOR)
334 node->m_type = CConditionNode::TERMINATOR;
336 // logic block with condition sub tree
337 else
339 // part 1 : a logic block(not/comparison/subcondition)
340 switch( logicConditionNode->LogicBlock.Type )
342 case CLogicConditionLogicBlock::NOT :
344 node->m_type = CConditionNode::NOT;
346 break;
348 case CLogicConditionLogicBlock::COMPARISON :
350 node->m_type = CConditionNode::COMPARISON;
352 node->m_sVariableName = CString(logicConditionNode->LogicBlock.ComparisonBlock.VariableName.c_str());
353 node->m_sOperator = CString(logicConditionNode->LogicBlock.ComparisonBlock.Operator.c_str());
354 node->m_dComparand = (double)logicConditionNode->LogicBlock.ComparisonBlock.Comparand;
357 break;
359 case CLogicConditionLogicBlock::SUB_CONDITION :
361 node->m_type = CConditionNode::SUB_CONDITION;
362 node->m_sConditionName = CString(logicConditionNode->LogicBlock.SubCondition.c_str());
364 break;
366 default :
368 node->m_type = CConditionNode::TERMINATOR;
373 // part 2 : a condition sub tree
374 vector<CLogicConditionNode *>::const_iterator itNode;
375 for( itNode = logicConditionNode->_Nodes.begin(); itNode != logicConditionNode->_Nodes.end(); ++itNode )
377 CConditionNode * nodeTmp = new CConditionNode();
378 cLogicConditionNodeToCConditionNode( *itNode, nodeTmp );
379 nodeTmp->m_pParentNode = node;
380 node->m_ctSubTree.AddTail( nodeTmp );
385 } // cLogicConditionNodeToCConditionNode //
390 //-----------------------------------------------
391 // cLogicConditionToCCondition
393 //-----------------------------------------------
394 void cLogicConditionToCCondition( const CLogicCondition& logicCondition, CCondition& condition )
396 // condition name
397 condition.m_sName = CString( logicCondition.getName().c_str() );
399 // condition tree
400 vector<CLogicConditionNode>::const_iterator itNode;
401 for( itNode = logicCondition.Nodes.begin(); itNode != logicCondition.Nodes.end(); ++itNode )
403 // convert the node
404 CConditionNode * node = new CConditionNode();
405 cLogicConditionNodeToCConditionNode( &(*itNode), node );
407 // add the node
408 condition.m_ctConditionTree.AddTail( node );
411 } // cLogicConditionToCCondition //