nux-1.14.0
NodeNetProtocol.cpp
00001 /*
00002  * Copyright 2010 Inalogic® Inc.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License, as
00006  * published by the  Free Software Foundation; either version 2.1 or 3.0
00007  * of the License.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranties of
00011  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00012  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00013  * License for more details.
00014  *
00015  * You should have received a copy of both the GNU Lesser General Public
00016  * License along with this program. If not, see <http://www.gnu.org/licenses/>
00017  *
00018  * Authored by: Jay Taoko <jaytaoko@inalogic.com>
00019  *
00020  */
00021 
00022 
00023 #include "Nux.h"
00024 #include "NodeNetProtocol.h"
00025 #include "PropertyList.h"
00026 
00027 #include "PropertyItem/ColorGradientPropertyItem.h"
00028 #include "PropertyItem/RGBAProperty.h"
00029 #include "PropertyItem/RGBProperty.h"
00030 #include "PropertyItem/EditTextLinePropertyItem.h"
00031 #include "PropertyItem/Vector3Property.h"
00032 #include "PropertyItem/Vector4Property.h"
00033 #include "PropertyItem/CheckBoxProperty.h"
00034 #include "PropertyItem/RangeValuePropertyItem.h"
00035 #include "PropertyItem/RangeValueIntegerPropertyItem.h"
00036 #include "PropertyItem/DoubleValuatorPropertyItem.h"
00037 #include "PropertyItem/IntegerValuatorPropertyItem.h"
00038 #include "PropertyItem/SplineCurvePropertyItem.h"
00039 #include "PropertyItem/ColorPreviewPropertyItem.h"
00040 #include "PropertyItem/Matrix3PreviewPropertyItem.h"
00041 #include "PropertyItem/Matrix4PreviewPropertyItem.h"
00042 #include "PropertyItem/ComboBoxPropertyItem.h"
00043 #include "PropertyItem/ComboBoxListPropertyItem.h"
00044 #include "PropertyItem/SpinBoxPropertyItem.h"
00045 #include "PropertyItem/SpinBoxDoublePropertyItem.h"
00046 #include "PropertyItem/Vector3ValuatorPropertyItem.h"
00047 
00048 namespace nux
00049 {
00050 
00051   NUX_IMPLEMENT_OBJECT_TYPE (NodeNetCom);
00052 
00053   struct TypeString
00054   {
00055     int Type;
00056     const char *String;
00057   };
00058 
00059 
00060   TypeString TypeToString[] =
00061   {
00062     {NODE_TYPE_UNDEFINED,       "ROOT_NODE"},
00063     {NODE_TYPE_FOLDER,          "Folder"},
00064     {NODE_TYPE_BOOL,            "Bool"},
00065     {NODE_TYPE_CHECKBOX,        "Check"},
00066     {NODE_TYPE_SPINBOX,         "SpinBox"},
00067     {NODE_TYPE_DOUBLESPINBOX,   "DoubleSpinBox"},
00068     {NODE_TYPE_COMBOSIMPLE,     "ComboBoxSimple"},
00069     {NODE_TYPE_COMBOCOMPLEX,    "ComboBoxComplex"},
00070     {NODE_TYPE_VECTOR3,         "Vector3"},
00071     {NODE_TYPE_VECTOR4,         "Vector4"},
00072     {NODE_TYPE_RGB,             "RGB"},
00073     {NODE_TYPE_RGBA,            "RGBA"},
00074     {NODE_TYPE_STATICTEXT,      "StaticText"},
00075     {NODE_TYPE_SPLINE,          "Spline"},
00076     {NODE_TYPE_EDITTEXT,        "EditText"},
00077     {NODE_TYPE_RANGE,           "RangeValue"},
00078     {NODE_TYPE_RANGEINTEGER,    "RangeValueInteger"},
00079     {NODE_TYPE_INTVALUATOR,     "IntValuator"},
00080     {NODE_TYPE_DOUBLEVALUATOR,  "DoubleValuator"},
00081     {NODE_TYPE_VECTORVALUATOR,  "Vector3Valuator"},
00082     {NODE_TYPE_COLORPREVIEW,    "ColorPreview"},
00083     {NODE_TYPE_MATRIX4PREVIEW,  "Matrix4Preview"},
00084     {NODE_TYPE_MATRIX3PREVIEW,  "Matrix3Preview"},
00085 
00086     {0,                 0},
00087   };
00088 
00089   const char *ConvertTypeToString (NodeParameterType type)
00090   {
00091     int i = 0;
00092 
00093     while (TypeToString[i].Type)
00094     {
00095       if (type == TypeToString[i].Type)
00096         return TypeToString[i].String;
00097 
00098       ++i;
00099     }
00100 
00101     nuxAssertMsg (0, TEXT ("[ConvertTypeToString]: Cannot convert Type to String") );
00102     return 0;
00103   }
00104 
00105   int NodeNetCom::m_IDGenerator = 0x12345678;
00106 
00107   NodeNetCom::NodeNetCom (const TCHAR *Name, NodeParameterType type)
00108     :   m_Name (Name)
00109     ,   m_Type (type)
00110     ,   m_ID (-1)
00111   {
00112     m_ID = m_IDGenerator++;
00113 
00114     if (m_Name.Length() == 0)
00115       m_Name = "Undefined";
00116   }
00117 
00118 #if NODE_XML_NET_PROTOCOL
00119   TiXmlElement *NodeNetCom::ToXML() const
00120   {
00121     TiXmlElement *elementxml = new TiXmlElement ("Parameter");
00122     SetNodeXMLAttributes (elementxml);
00123 
00124     if (!SkipChild() )
00125     {
00126       NodeNetCom *first = (NodeNetCom *) FirstChildNode();
00127 
00128       while (first)
00129       {
00130         TiXmlElement *childxml = first->ToXML();
00131 
00132         if (childxml)
00133           elementxml->LinkEndChild (childxml);
00134 
00135         first = (NodeNetCom *) first->Next();
00136       }
00137     }
00138 
00139     return elementxml;
00140   }
00141 
00142   bool NodeNetCom::FromXML (const TiXmlElement *elementxml)
00143   {
00144     GetNodeXMLAttributes (elementxml);
00145     nuxDebugMsg (TEXT ("Updated Node %s - ID: %d - Type: %s"), m_Name.GetTCharPtr(), m_ID, ConvertTypeToString (m_Type) );
00146 
00147     const TiXmlElement *childelement = elementxml->FirstChildElement();
00148 
00149     if (!SkipChild() )
00150     {
00151       while (childelement)
00152       {
00153         int ID;
00154 
00155         if (childelement->QueryIntAttribute ("ID", &ID) != TIXML_SUCCESS)
00156         {
00157           nuxAssertMsg (0, TEXT ("[NodeNetCom::FromXML]: 'ID' Attribute not found") );
00158           return false;
00159         }
00160 
00161         NodeNetCom *childnode = FindChildNodeID (ID);
00162 
00163         if (childnode)
00164           childnode->FromXML (childelement);
00165         else
00166         {
00167           nuxAssertMsg (0, TEXT ("[NodeNetCom::FromXML]: Can't find child node with id %d"), ID);
00168           return false;
00169         }
00170 
00171         childelement = childelement->NextSiblingElement();
00172       }
00173     }
00174 
00175     return true;
00176   }
00177 
00178   bool NodeNetCom::CreateFromXML (const TiXmlElement *elementxml)
00179   {
00180     if (elementxml == 0)
00181       return false;
00182 
00183     //GetNodeXMLAttributes(elementxml);
00184     //nuxDebugMsg(TEXT("Created Node %s - ID: %d - Type: %s"), m_Name.c_str(), m_ID, ConvertTypeToString(m_Type));
00185 
00186     const TiXmlElement *childelement = elementxml->FirstChildElement();
00187 
00188     if (!SkipChild() )
00189     {
00190       while (childelement)
00191       {
00192         int Type;
00193         int Enable;
00194         int Visible;
00195         int ID;
00196         const char *Name;
00197 
00198         if (childelement->QueryIntAttribute ("Type", &Type) != TIXML_SUCCESS)
00199         {
00200           nuxAssertMsg (0, TEXT ("[NodeNetCom::CreateFromXML]: 'Type' Attribute not found") );
00201           return false;
00202         }
00203 
00204         if (childelement->QueryIntAttribute ("Enable", &Enable) != TIXML_SUCCESS)
00205         {
00206           nuxAssertMsg (0, TEXT ("[NodeNetCom::CreateFromXML]: 'Enable' Attribute not found") );
00207           return false;
00208         }
00209 
00210         if (childelement->QueryIntAttribute ("Visible", &Visible) != TIXML_SUCCESS)
00211         {
00212           nuxAssertMsg (0, TEXT ("[NodeNetCom::CreateFromXML]: 'Visible' Attribute not found") );
00213           return false;
00214         }
00215 
00216         if (childelement->QueryIntAttribute ("ID", &ID) != TIXML_SUCCESS)
00217         {
00218           nuxAssertMsg (0, TEXT ("[NodeNetCom::CreateFromXML]: 'ID' Attribute not found") );
00219           return false;
00220         }
00221 
00222         Name = childelement->Attribute ("Name");
00223 
00224         if (Name == 0)
00225         {
00226           nuxAssertMsg (0, TEXT ("[NodeNetCom::CreateFromXML]: 'Name' Attribute not found") );
00227           return false;
00228         }
00229 
00230 
00231         //         m_Name = xmlelt->Attribute("Name");
00232         //         nuxVerifyExpr( xmlelt->QueryIntAttribute("Enable", &m_Enable) == TIXML_SUCCESS);
00233         //         nuxVerifyExpr( xmlelt->QueryIntAttribute("Visible", &m_Visible) == TIXML_SUCCESS);
00234         //         nuxVerifyExpr( xmlelt->QueryIntAttribute("Type", (int*)&m_Type) == TIXML_SUCCESS);
00235         //         nuxVerifyExpr( xmlelt->QueryIntAttribute("ID", &m_ID) == TIXML_SUCCESS);
00236 
00237 
00238 
00239         NodeNetCom *child = 0;
00240 
00241         //        if(Type == TYPE_FOLDER)
00242         //        {
00243         //            child = inlFolder::CreateFromXML(childelement, this, Name, ID);
00244         //        }
00245         //        else if(Type == TYPE_BOOL)
00246         //        {
00247         //            child = inlBool::CreateFromXML(childelement, this, Name, ID);
00248         //        }
00249         //        else
00250         if (Type == NODE_TYPE_CHECKBOX)
00251         {
00252           child = (NodeNetCom *) CheckBoxPropertyItem::CreateFromXML (childelement, this, Name, ID);
00253         }
00254         else if (Type == NODE_TYPE_SPINBOX)
00255         {
00256           child = (NodeNetCom *) SpinBoxPropertyItem::CreateFromXML (childelement, this, Name, ID);
00257         }
00258         else if (Type == NODE_TYPE_DOUBLESPINBOX)
00259         {
00260           child = (NodeNetCom *) SpinBoxDoublePropertyItem::CreateFromXML (childelement, this, Name, ID);
00261         }
00262 
00263         else if (Type == NODE_TYPE_COMBOSIMPLE)
00264         {
00265           child = (NodeNetCom *) ComboBoxPropertyItem::CreateFromXML (childelement, this, Name, ID);
00266         }
00267         else if (Type == NODE_TYPE_VECTOR4)
00268         {
00269           child = (NodeNetCom *) Vector4PropertyItem::CreateFromXML (childelement, this, Name, ID);
00270         }
00271         else if (Type == NODE_TYPE_VECTOR3)
00272         {
00273           child = (NodeNetCom *) Vector3PropertyItem::CreateFromXML (childelement, this, Name, ID);
00274         }
00275         else if (Type == NODE_TYPE_RGBA)
00276         {
00277           child = (NodeNetCom *) RGBAPropertyItem::CreateFromXML (childelement, this, Name, ID);
00278         }
00279         else if (Type == NODE_TYPE_RGB)
00280         {
00281           child = (NodeNetCom *) RGBPropertyItem::CreateFromXML (childelement, this, Name, ID);
00282         }
00283         else if (Type == NODE_TYPE_STATICTEXT)
00284         {
00285           child = (NodeNetCom *) SectionProperty::CreateFromXML (childelement, this, Name, ID);
00286         }
00287         else if (Type == NODE_TYPE_SPLINE)
00288         {
00289           child = (NodeNetCom *) SplineCurvePropertyItem::CreateFromXML (childelement, this, Name, ID);
00290         }
00291         else if (Type == NODE_TYPE_EDITTEXT)
00292         {
00293           child = (NodeNetCom *) EditTextLinePropertyItem::CreateFromXML (childelement, this, Name, ID);
00294         }
00295         else if (Type == NODE_TYPE_INTVALUATOR)
00296         {
00297           child = (NodeNetCom *) IntValuatorPropertyItem::CreateFromXML (childelement, this, Name, ID);
00298         }
00299         else if (Type == NODE_TYPE_DOUBLEVALUATOR)
00300         {
00301           child = (NodeNetCom *) DoubleValuatorPropertyItem::CreateFromXML (childelement, this, Name, ID);
00302         }
00303         else if (Type == NODE_TYPE_VECTORVALUATOR)
00304         {
00305           child = (NodeNetCom *) Vector3ValuatorPropertyItem::CreateFromXML (childelement, this, Name, ID);
00306         }
00307         else if (Type == NODE_TYPE_COLORPREVIEW)
00308         {
00309           child = (NodeNetCom *) ColorPreviewPropertyItem::CreateFromXML (childelement, this, Name, ID);
00310         }
00311         else if (Type == NODE_TYPE_RANGE)
00312         {
00313           child = (NodeNetCom *) RangeValuePropertyItem::CreateFromXML (childelement, this, Name, ID);
00314         }
00315         else if (Type == NODE_TYPE_RANGEINTEGER)
00316         {
00317           child = (NodeNetCom *) RangeValueIntegerPropertyItem::CreateFromXML (childelement, this, Name, ID);
00318         }
00319         else
00320         {
00321           nuxAssertMsg (0, TEXT ("Unknown Type %s"), Type);
00322         }
00323 
00324         if (child)
00325           PushChildBack (child);
00326 
00327         child->CreateFromXML (childelement);
00328         childelement = childelement->NextSiblingElement();
00329       }
00330     }
00331 
00332     return true;
00333   }
00334 
00335   void NodeNetCom::SetNodeXMLAttributes (TiXmlElement *xmlelt) const
00336   {
00337     xmlelt->SetAttribute ("Name", m_Name.GetTCharPtr() );
00338     xmlelt->SetAttribute ("Enable", (m_Enable ? 1 : 0) );
00339     xmlelt->SetAttribute ("Visible", (m_Visible ? 1 : 0) );
00340     xmlelt->SetAttribute ("Type", m_Type);
00341     xmlelt->SetAttribute ("TypeString", ConvertTypeToString (m_Type) );
00342     xmlelt->SetAttribute ("ID", m_ID);
00343   }
00344 
00345   void NodeNetCom::GetNodeXMLAttributes (const TiXmlElement *xmlelt)
00346   {
00347     m_Name = xmlelt->Attribute ("Name");
00348     nuxVerifyExpr ( xmlelt->QueryIntAttribute ("Enable", &m_Enable) == TIXML_SUCCESS);
00349     nuxVerifyExpr ( xmlelt->QueryIntAttribute ("Visible", &m_Visible) == TIXML_SUCCESS);
00350     nuxVerifyExpr ( xmlelt->QueryIntAttribute ("Type", (int *) &m_Type) == TIXML_SUCCESS);
00351     nuxVerifyExpr ( xmlelt->QueryIntAttribute ("ID", &m_ID) == TIXML_SUCCESS);
00352   }
00353 
00354   bool QueryNodeXMLIntAttribute (const TiXmlElement *elementxml, const TCHAR *attribute, int *Value, int searchid)
00355   {
00356     int ID;
00357 
00358     if ( (searchid >= 0) && elementxml->QueryIntAttribute ("ID", &ID) != TIXML_SUCCESS)
00359     {
00360       nuxAssertMsg (0, TEXT ("[NodeNetCom::QueryNodeXMLIntAttribute]: 'ID' Attribute not found") );
00361       return false;
00362     }
00363     else
00364     {
00365       if ( (searchid >= 0) && (ID != searchid) )
00366       {
00367         nuxAssertMsg (0, TEXT ("[NodeNetCom::QueryNodeXMLIntAttribute]: Incorrect 'ID' number") );
00368         return false;
00369       }
00370       else
00371       {
00372         if (elementxml->QueryIntAttribute (attribute, Value) != TIXML_SUCCESS)
00373         {
00374           nuxAssertMsg (0, TEXT ("[NodeNetCom::QueryNodeXMLIntAttribute]: Attribute not found") );
00375           return false;
00376         }
00377       }
00378 
00379       return true;
00380     }
00381 
00382     return false;
00383   }
00384 
00385   bool QueryNodeXMLDoubleAttribute (const TiXmlElement *elementxml, const TCHAR *attribute, double *Value, int searchid)
00386   {
00387     int ID;
00388 
00389     if ( (searchid >= 0) && elementxml->QueryIntAttribute ("ID", &ID) != TIXML_SUCCESS)
00390     {
00391       nuxAssertMsg (0, TEXT ("[NodeNetCom::QueryNodeXMLDoubleAttribute]: 'ID' Attribute not found") );
00392       return false;
00393     }
00394     else
00395     {
00396       if ( (searchid >= 0) && (ID != searchid) )
00397       {
00398         nuxAssertMsg (0, TEXT ("[NodeNetCom::QueryNodeXMLDoubleAttribute]: Incorrect 'ID' number") );
00399         return false;
00400       }
00401       else
00402       {
00403         if (elementxml->QueryDoubleAttribute (attribute, Value) != TIXML_SUCCESS)
00404         {
00405           nuxAssertMsg (0, TEXT ("[NodeNetCom::QueryNodeXMLDoubleAttribute]: Attribute not found") );
00406           return false;
00407         }
00408       }
00409 
00410       return true;
00411     }
00412 
00413     return false;
00414   }
00415 
00416   bool QueryNodeXMLStringAttribute (const TiXmlElement *elementxml, const TCHAR *attribute, tstring &Value, int searchid)
00417   {
00418     int ID;
00419 
00420     if ( (searchid >= 0) && elementxml->QueryIntAttribute ("ID", &ID) != TIXML_SUCCESS)
00421     {
00422       nuxAssertMsg (0, TEXT ("[NodeNetCom::QueryNodeXMLStringAttribute]: 'ID' Attribute not found") );
00423       return false;
00424     }
00425     else
00426     {
00427       if ( (searchid >= 0) && (ID != searchid) )
00428       {
00429         nuxAssertMsg (0, TEXT ("[NodeNetCom::QueryNodeXMLStringAttribute]: Incorrect 'ID' number") );
00430         return false;
00431       }
00432       else
00433       {
00434         Value = elementxml->Attribute (attribute);
00435 
00436         if (Value.size() == 0)
00437         {
00438           // Do not break... this could be a null string which is allowed.
00439           // nuxAssertMsg(0, TEXT("[NodeNetCom::QueryNodeXMLStringAttribute]: Attribute not found"));
00440           return false;
00441         }
00442       }
00443 
00444       return true;
00445     }
00446 
00447     return false;
00448   }
00449 
00450   bool QueryNodeXMLAttribute (const TiXmlElement *elementxml, const TCHAR *attribute, std::string &Value, int searchid)
00451   {
00452     int ID;
00453 
00454     if ( (searchid >= 0) && elementxml->QueryIntAttribute ("ID", &ID) != TIXML_SUCCESS)
00455     {
00456       nuxAssertMsg (0, TEXT ("[NodeNetCom::QueryNodeXMLAttribute]: 'ID' Attribute not found") );
00457       return false;
00458     }
00459     else
00460     {
00461       if ( (searchid >= 0) && (ID != searchid) )
00462       {
00463         nuxAssertMsg (0, TEXT ("[NodeNetCom::QueryNodeXMLAttribute]: Incorrect 'ID' number") );
00464         return false;
00465       }
00466       else
00467       {
00468         const char *s = elementxml->Attribute (attribute);
00469 
00470         if (s == 0)
00471         {
00472           nuxAssertMsg (0, TEXT ("[NodeNetCom::QueryNodeXMLAttribute]: 'Check' Attribute not found") );
00473           return false;
00474         }
00475 
00476         Value = s;
00477       }
00478 
00479       return true;
00480     }
00481 
00482     return false;
00483   }
00484 
00485 #endif // NODE_XML_NET_PROTOCOL
00486 
00487   NodeNetCom *NodeNetCom::FindNodeID (int id)
00488   {
00489     if (this->m_ID != id)
00490     {
00491       NodeNetCom *child = (NodeNetCom *) FirstChildNode();
00492 
00493       if (child)
00494       {
00495         // we have a child node; recurse through it
00496         NodeNetCom *findnode =  child->FindNodeID (id);
00497 
00498         if (findnode)
00499           return findnode;
00500       }
00501     }
00502     else
00503     {
00504       return this;
00505     }
00506 
00507     // Not found in child node; check the siblings
00508     NodeNetCom *sibling = (NodeNetCom *) Next();
00509 
00510     if (sibling)
00511     {
00512       NodeNetCom *findnode = sibling->FindNodeID (id);
00513 
00514       if (findnode)
00515         return findnode;
00516     }
00517 
00518     return 0;
00519   }
00520 
00521   NodeNetCom *NodeNetCom::FindChildNodeID (int id)
00522   {
00523     //     if(id < 0)
00524     //         return 0;
00525 
00526     if (GetID() == id)
00527       return this;
00528 
00529     NodeNetCom *child = (NodeNetCom *) FirstChildNode();
00530 
00531     while (child)
00532     {
00533       // we have a child node; recurse through it
00534       NodeNetCom *findnode = child->FindNodeID (id);
00535 
00536       if (findnode)
00537         return findnode;
00538 
00539       child = (NodeNetCom *) child->Next();
00540     }
00541 
00542     return 0;
00543   }
00544 
00545   void NodeNetCom::DeleteChildren()
00546   {
00547     NodeNetCom *last = (NodeNetCom *) LastChildNode();
00548 
00549     while (last)
00550     {
00551       last->DeleteChildren();
00552       last->Unlink();
00553       delete last;
00554       last = (NodeNetCom *) LastChildNode();
00555     }
00556   }
00557 
00558 #if NODE_XML_NET_PROTOCOL
00559 
00560   SectionProperty *SectionProperty::CreateFromXML (const TiXmlElement *elementxml, NodeNetCom *parent, const char *Name, int id)
00561   {
00562     tstring text;
00563     QueryNodeXMLStringAttribute (elementxml, TEXT ("Text"),       text,     id);
00564     SectionProperty *node = new SectionProperty (Name);
00565     //node->setCaption(text.c_str());
00566     node->SetID (id);
00567     return node;
00568   }
00569 
00570   TiXmlElement *SectionProperty::ToXML() const
00571   {
00572     TiXmlElement *elementxml = NodeNetCom::ToXML();
00573     elementxml->SetAttribute (TEXT ("Text"), GetName() );
00574     return elementxml;
00575   }
00576 
00577   bool SectionProperty::FromXML (const TiXmlElement *elementxml)
00578   {
00579     tstring text;
00580     QueryNodeXMLStringAttribute (elementxml, TEXT ("Text"), text, GetID() );
00581     return NodeNetCom::FromXML (elementxml);
00582   }
00583 
00584 #endif
00585 
00586 
00587 }
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends