AttributeList.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. #include "AttributeList.h"
  2. #include "strutil.h"
  3. #include "..\missioneditor.h"
  4. #include "attrcreator.h"
  5. #include "..\textfile.h"
  6. #include "..\..\common_h\tinyxml\tinyxml.h"
  7. extern MissionEditor* sMission;
  8. AttributeList::AttributeList () : Storage (_FL_, 128)
  9. {
  10. Clear ();
  11. }
  12. AttributeList::~AttributeList ()
  13. {
  14. for (DWORD n = 0; n < Storage.Size (); n++)
  15. {
  16. BaseAttribute* pNew = Storage[n];
  17. DeleteAttribute(pNew);
  18. //delete (pNew);
  19. }
  20. Clear ();
  21. }
  22. //int AttributeList::Add (BaseAttribute* pPtr)
  23. void AttributeList::Clear ()
  24. {
  25. Storage.DelAll ();
  26. }
  27. //int AttributeList::GetCount ()
  28. int AttributeList::GetCountForSave ()
  29. {
  30. int cnt = 0;
  31. for (DWORD i = 0; i < Storage.Size(); i++)
  32. {
  33. IMOParams::Type tp = Storage[i]->GetType();
  34. if (tp == IMOParams::t_group)
  35. {
  36. cnt += GetAttributesInGroup ((GroupAttribute*)Storage[i]);
  37. cnt++;
  38. } else
  39. {
  40. cnt++;
  41. }
  42. }
  43. return cnt;
  44. }
  45. int AttributeList::GetAttributesInGroup(GroupAttribute* pattr)
  46. {
  47. int cnt = 0;
  48. for (dword i = 0; i < pattr->Childs.Size(); i++)
  49. {
  50. IMOParams::Type tp = pattr->Childs[i]->GetType();
  51. if (tp == IMOParams::t_group)
  52. {
  53. cnt += GetAttributesInGroup ((GroupAttribute*)pattr->Childs[i]);
  54. cnt++;
  55. } else
  56. {
  57. cnt++;
  58. }
  59. }
  60. return cnt;
  61. }
  62. //BaseAttribute* AttributeList::Get(int index)
  63. //void AttributeList::Insert (int index, BaseAttribute* pPtr)
  64. //void AttributeList::Delete (int index)
  65. //BaseAttribute* AttributeList::operator[] (int index)
  66. BaseAttribute* AttributeList::CreateFromParam (const IMOParams::Param* temp)
  67. {
  68. BaseAttribute* newAttr = NULL;
  69. newAttr = CreateFromMissionParam (temp);
  70. return newAttr;
  71. }
  72. void AttributeList::CreateFromParams (IMOParams* params)
  73. {
  74. int num = params->GetNumParams ();
  75. if (num <= 0) return;
  76. for (int n = 0; n < num; n++)
  77. {
  78. const IMOParams::Param* temp = params->GetParam (n);
  79. //api->Trace ("%s - %d", temp->name, temp->type);
  80. if (temp->type == 11)
  81. {
  82. int a = 0;
  83. }
  84. BaseAttribute* battr = CreateFromParam (temp);
  85. if (battr) Add (battr);
  86. }
  87. }
  88. void AttributeList::AddToWriter (MOPWriter& wrt)
  89. {
  90. for (int n =0; n < GetCount (); n++)
  91. {
  92. BaseAttribute* base_attr = Get (n);
  93. base_attr->AddToWriter (wrt);
  94. }
  95. }
  96. void AttributeList::AddToTree (GUITreeNode* node, TreeNodesPool* nodesPool)
  97. {
  98. for (int n = 0; n < GetCount (); n++)
  99. {
  100. BaseAttribute* base_attr = Get (n);
  101. base_attr->Add2Tree(&node->Childs, nodesPool);
  102. }
  103. }
  104. void AttributeList::AddParam (BaseAttribute* attr)
  105. {
  106. //----------------------------------------------------------
  107. BaseAttribute* newAttr = CreateAttributeCopy (attr);
  108. if (newAttr) Add (newAttr);
  109. //----------------------------------------------------------
  110. }
  111. void AttributeList::CreateFromList (AttributeList* list)
  112. {
  113. int num = list->GetCount ();
  114. if (num <= 0) return;
  115. for (int n = 0; n < num; n++)
  116. {
  117. BaseAttribute* attr = list->Get(n);
  118. AddParam (attr);
  119. }
  120. }
  121. void AttributeList::BeforeDelete ()
  122. {
  123. int num = GetCount ();
  124. if (num <= 0) return;
  125. for (int n = 0; n < num; n++)
  126. {
  127. BaseAttribute* attr = Get(n);
  128. attr->BeforeDelete();
  129. }
  130. }
  131. void AttributeList::SetMasterData (void* data)
  132. {
  133. for (int n = 0; n < GetCount(); n++)
  134. {
  135. Get(n)->SetMasterData (data);
  136. }
  137. }
  138. BaseAttribute* AttributeList::FindInArray (ArrayAttribute* pArray, const char *szAttrName, IMOParams :: Type Type)
  139. {
  140. for (DWORD i = 0; i < pArray->GetDefaultElement().elements.Size(); i++)
  141. {
  142. BaseAttribute* SourceAttr = pArray->GetDefaultElement().elements[i];
  143. if (SourceAttr->GetType() == Type)
  144. {
  145. if (crt_stricmp (SourceAttr->GetName(), szAttrName) == 0)
  146. {
  147. return SourceAttr;
  148. }
  149. }
  150. if (SourceAttr->GetType() == IMOParams::t_array)
  151. {
  152. ArrayAttribute* ArrayAttr = (ArrayAttribute*) SourceAttr;
  153. BaseAttribute* pRes = FindInArray (ArrayAttr, szAttrName, Type);
  154. if (pRes) return pRes;
  155. }
  156. if (SourceAttr->GetType() == IMOParams::t_group)
  157. {
  158. GroupAttribute* GroupAttr = (GroupAttribute*) SourceAttr;
  159. BaseAttribute* pRes = FindInGroup (GroupAttr, szAttrName, Type);
  160. if (pRes) return pRes;
  161. }
  162. }
  163. return NULL;
  164. }
  165. BaseAttribute* AttributeList::FindInGroup (GroupAttribute* pGroup, const char *szAttrName, IMOParams :: Type Type)
  166. {
  167. for (DWORD i = 0; i < pGroup->Childs.Size(); i++)
  168. {
  169. BaseAttribute* SourceAttr = pGroup->Childs[i];
  170. if (SourceAttr->GetType() == Type)
  171. {
  172. if (crt_stricmp (SourceAttr->GetName(), szAttrName) == 0)
  173. {
  174. return SourceAttr;
  175. }
  176. }
  177. if (SourceAttr->GetType() == IMOParams::t_array)
  178. {
  179. ArrayAttribute* ArrayAttr = (ArrayAttribute*) SourceAttr;
  180. BaseAttribute* pRes = FindInArray (ArrayAttr, szAttrName, Type);
  181. if (pRes) return pRes;
  182. }
  183. if (SourceAttr->GetType() == IMOParams::t_group)
  184. {
  185. GroupAttribute* GroupAttr = (GroupAttribute*) SourceAttr;
  186. BaseAttribute* pRes = FindInGroup (GroupAttr, szAttrName, Type);
  187. if (pRes) return pRes;
  188. }
  189. }
  190. return NULL;
  191. }
  192. const char* AttributeList::GetTextType (IMOParams::Type type)
  193. {
  194. switch (type)
  195. {
  196. case IMOParams::t_bool:
  197. return "bool";
  198. case IMOParams::t_long:
  199. return "long";
  200. case IMOParams::t_float:
  201. return "float";
  202. case IMOParams::t_string:
  203. return "string";
  204. case IMOParams::t_locstring:
  205. return "locstring";
  206. case IMOParams::t_position:
  207. return "position";
  208. case IMOParams::t_angles:
  209. return "rotation";
  210. case IMOParams::t_color:
  211. return "color";
  212. case IMOParams::t_array:
  213. return "array";
  214. case IMOParams::t_enum:
  215. return "enum";
  216. case IMOParams::t_group:
  217. return "group";
  218. }
  219. return "unknown";
  220. }
  221. void AttributeList::DebugLog ()
  222. {
  223. for (dword i = 0; i < Storage.Size(); i++)
  224. {
  225. IMOParams::Type t = Storage[i]->GetType();
  226. api->Trace("%s - %s", Storage[i]->GetName(), GetTextType(t));
  227. if (t == IMOParams::t_array)
  228. {
  229. ArrayAttribute* pArray = (ArrayAttribute*)Storage[i];
  230. pArray->DebugLog (0);
  231. }
  232. if (t == IMOParams::t_group)
  233. {
  234. GroupAttribute* pGroup = (GroupAttribute*)Storage[i];
  235. pGroup->DebugLog (0);
  236. }
  237. }
  238. }
  239. void AttributeList::GenerateFlatList(array<BaseAttribute*> &FlatList)
  240. {
  241. for (dword i = 0; i < Storage.Size(); i++)
  242. {
  243. IMOParams::Type t = Storage[i]->GetType();
  244. if (t == IMOParams::t_group)
  245. {
  246. GroupAttribute* pGroup = (GroupAttribute*)Storage[i];
  247. pGroup->GenerateFlatList(FlatList);
  248. continue;
  249. }
  250. FlatList.Add(Storage[i]);
  251. }
  252. }
  253. void AttributeList::WriteToXML (TextFile &file, int level)
  254. {
  255. file.Write(level, "<attributes>\n");
  256. for (int n =0; n < GetCount (); n++)
  257. {
  258. BaseAttribute* base_attr = Get (n);
  259. base_attr->WriteToXML (file, level+1);
  260. }
  261. file.Write(level, "</attributes>\n");
  262. }
  263. IMOParams::Type AttributeList::GetTypeFromXML (TiXmlElement* Root)
  264. {
  265. const char* v = Root->Value();
  266. if (v[0] == 'b') return IMOParams::t_bool;
  267. if (v[0] == 'l' && v[1] == 'o' && v[2] == 'n') return IMOParams::t_long;
  268. if (v[0] == 'l' && v[1] == 'o' && v[2] == 'c') return IMOParams::t_locstring;
  269. if (v[0] == 'f') return IMOParams::t_float;
  270. if (v[0] == 'p') return IMOParams::t_position;
  271. if (v[0] == 'r') return IMOParams::t_angles;
  272. if (v[0] == 's') return IMOParams::t_string;
  273. if (v[0] == 'c') return IMOParams::t_color;
  274. if (v[0] == 'e') return IMOParams::t_enum;
  275. if (v[0] == 'a') return IMOParams::t_array;
  276. /*
  277. if (crt_stricmp(Root->Value(),"bool") == 0) return IMOParams::t_bool;
  278. if (crt_stricmp(Root->Value(),"long") == 0) return IMOParams::t_long;
  279. if (crt_stricmp(Root->Value(),"float") == 0) return IMOParams::t_float;
  280. if (crt_stricmp(Root->Value(),"position") == 0) return IMOParams::t_position;
  281. if (crt_stricmp(Root->Value(),"rotation") == 0) return IMOParams::t_angles;
  282. if (crt_stricmp(Root->Value(),"string") == 0) return IMOParams::t_string;
  283. if (crt_stricmp(Root->Value(),"locstring") == 0) return IMOParams::t_locstring;
  284. if (crt_stricmp(Root->Value(),"color") == 0) return IMOParams::t_color;
  285. if (crt_stricmp(Root->Value(),"enum") == 0) return IMOParams::t_enum;
  286. if (crt_stricmp(Root->Value(),"array") == 0) return IMOParams::t_array;
  287. */
  288. throw ("unknown type");
  289. return IMOParams::t_bool;
  290. }
  291. void AttributeList::ReadXML (TiXmlElement* Root, const char* szMasterClass)
  292. {
  293. MissionEditor::tAvailableMO* objDesc = sMission->GetAvailableClassByName(szMasterClass);
  294. for(TiXmlElement* child = Root->FirstChildElement(); child; child = child->NextSiblingElement())
  295. {
  296. IMOParams::Type Type = GetTypeFromXML(child);
  297. BaseAttribute* newAttr = CreateEmptyAttribute ((IMOParams::Type)Type);
  298. newAttr->ReadXML (child, szMasterClass);
  299. //newAttr->LoadFromFile(pFile, ClassName);
  300. Add (newAttr);
  301. MissionEditor::tAvailableMO* objDesc = sMission->GetAvailableClassByName (szMasterClass);
  302. if (objDesc)
  303. {
  304. //New
  305. const IMOParams::Param* temp = objDesc->Params->FindParam(newAttr->GetName());
  306. if(temp)
  307. {
  308. newAttr->SetHint(temp->comment);
  309. }
  310. /*
  311. //Old
  312. long iNumParams = objDesc->Params->GetNumParams();
  313. for (long j = 0; j < iNumParams; j++)
  314. {
  315. const IMOParams::Param* temp = objDesc->Params->GetParam (j);
  316. if (crt_stricmp(newAttr->GetName(), temp->name) == 0)
  317. {
  318. newAttr->SetHint(temp->comment);
  319. break;
  320. }
  321. }
  322. */
  323. }
  324. }
  325. }