DataColor.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #include "DataColor.h"
  2. #include "..\..\icommon\graphtime.h"
  3. #include "..\..\icommon\memfile.h"
  4. #include "..\..\..\common_h\core.h"
  5. #include "..\..\TextFile.h"
  6. #ifndef _XBOX
  7. #include "..\..\..\common_h\tinyxml\tinyxml.h"
  8. #endif
  9. #include "fieldlist.h"
  10. #include "..\..\..\Common_h\data_swizzle.h"
  11. //конструктор/деструктор
  12. DataColor::DataColor (FieldList* pMaster) : ColorGraph(_FL_), ZeroColor(0xFFFFFFFFL)
  13. {
  14. szName = NULL;
  15. szEditorName = NULL;
  16. Master = pMaster;
  17. }
  18. DataColor::~DataColor ()
  19. {
  20. }
  21. //Устанавливает "значение по умолчанию"
  22. //два индекса, Min=Max=Value
  23. void DataColor::SetDefaultValue (const Color& Value)
  24. {
  25. ColorGraph.DelAll();
  26. ColorVertex pMinVertex;
  27. pMinVertex.Time = MIN_GRAPH_TIME;
  28. pMinVertex.MinValue = Value;
  29. pMinVertex.MaxValue = Value;
  30. ColorGraph.Add(pMinVertex);
  31. ColorVertex pMaxVertex;
  32. pMinVertex.Time = 1.0f;
  33. pMinVertex.MinValue = Value;
  34. pMinVertex.MaxValue = Value;
  35. ColorGraph.Add(pMinVertex);
  36. }
  37. //Установить значения
  38. void DataColor::SetValues (const ColorVertex* Values, DWORD Count)
  39. {
  40. ColorGraph.DelAll();
  41. for (DWORD n = 0; n < Count; n++)
  42. {
  43. ColorGraph.Add(Values[n]);
  44. }
  45. if (Master) Master->UpdateCache();
  46. }
  47. //Получить кол-во значений
  48. DWORD DataColor::GetValuesCount ()
  49. {
  50. return ColorGraph.Size();
  51. }
  52. //Получить мин. значение (по индексу)
  53. const Color& DataColor::GetMinValue (DWORD Index)
  54. {
  55. return ColorGraph[Index].MinValue;
  56. }
  57. //Получить макс. значение (по индексу)
  58. const Color& DataColor::GetMaxValue (DWORD Index)
  59. {
  60. return ColorGraph[Index].MaxValue;
  61. }
  62. void DataColor::Load (MemFile* File)
  63. {
  64. dword dwColorCount = 0;
  65. File->ReadType(dwColorCount);
  66. XSwizzleDWord(dwColorCount);
  67. for (DWORD n = 0; n < dwColorCount; n++)
  68. {
  69. float Time = 0.0f;
  70. File->ReadType(Time);
  71. XSwizzleFloat(Time);
  72. Color clrMax;
  73. File->ReadType(clrMax);
  74. XSwizzleFloat(clrMax.r);
  75. XSwizzleFloat(clrMax.g);
  76. XSwizzleFloat(clrMax.b);
  77. XSwizzleFloat(clrMax.a);
  78. Color clrMin;
  79. File->ReadType(clrMin);
  80. XSwizzleFloat(clrMin.r);
  81. XSwizzleFloat(clrMin.g);
  82. XSwizzleFloat(clrMin.b);
  83. XSwizzleFloat(clrMin.a);
  84. ColorVertex pColor;
  85. pColor.Time = Time;
  86. pColor.MinValue = clrMin;
  87. pColor.MaxValue = clrMax;
  88. ColorGraph.Add(pColor);
  89. }
  90. //static char AttribueName[128];
  91. dword NameLength = 0;
  92. File->ReadType(NameLength);
  93. XSwizzleDWord(NameLength);
  94. Assert (NameLength < 128);
  95. //File->Read(AttribueName, NameLength);
  96. const char* AttribueName = File->GetPointerToString(NameLength);
  97. SetName (AttribueName, "a");
  98. if (Master) Master->UpdateCache();
  99. }
  100. void DataColor::SetName (const char* szName, const char* szEditorName)
  101. {
  102. //api->Trace("DataColor::SetName - '%s'", szName);
  103. this->szName = szName;
  104. this->szEditorName = szEditorName;
  105. }
  106. const char* DataColor::GetName ()
  107. {
  108. return szName;
  109. }
  110. const ColorVertex& DataColor::GetByIndex (DWORD Index)
  111. {
  112. return ColorGraph[Index];
  113. }
  114. void DataColor::Write (MemFile* File)
  115. {
  116. DWORD dwColorCount = ColorGraph.Size();
  117. File->WriteType(dwColorCount);
  118. for (DWORD n = 0; n < dwColorCount; n++)
  119. {
  120. float Time = ColorGraph[n].Time;
  121. File->WriteType(Time);
  122. Color clrMax = ColorGraph[n].MaxValue;
  123. File->WriteType(clrMax);
  124. Color clrMin = ColorGraph[n].MinValue;
  125. File->WriteType(clrMin);
  126. }
  127. //save name
  128. DWORD NameLength = crt_strlen (szName);
  129. DWORD NameLengthPlusZero = NameLength+1;
  130. File->WriteType(NameLengthPlusZero);
  131. Assert (NameLength < 128);
  132. File->Write(szName, NameLength);
  133. File->WriteZeroByte();
  134. }
  135. const char* DataColor::GetEditorName ()
  136. {
  137. return szEditorName;
  138. }
  139. #ifndef _XBOX
  140. void DataColor::WriteXML (TextFile* xmlFile, dword level)
  141. {
  142. xmlFile->Write((level+1), "<Name val = \"%s\" />\n", szName);
  143. xmlFile->Write((level+1), "<Graph>\n");
  144. for (dword n = 0; n < ColorGraph.Size(); n++)
  145. {
  146. xmlFile->Write((level+1), "<Key>\n");
  147. xmlFile->Write((level+2), "<Time val = \"%f\" />\n", ColorGraph[n].Time);
  148. xmlFile->Write((level+2), "<Max val = \"0x%08X\" />\n", ColorGraph[n].MaxValue.GetDword());
  149. xmlFile->Write((level+2), "<Min val = \"0x%08X\" />\n", ColorGraph[n].MinValue.GetDword());
  150. xmlFile->Write((level+1), "</Key>\n");
  151. }
  152. xmlFile->Write((level+1), "</Graph>\n");
  153. }
  154. void DataColor::LoadXML (TiXmlElement* root)
  155. {
  156. TiXmlElement* name = root->FirstChildElement("Name");
  157. if (name)
  158. {
  159. SetName (name->Attribute("val"), "a");
  160. }
  161. TiXmlElement* graphNode = NULL;
  162. graphNode = root->FirstChildElement("Graph");
  163. if (graphNode)
  164. {
  165. for(TiXmlElement* child = graphNode->FirstChildElement(); child; child = child->NextSiblingElement())
  166. {
  167. string NodeName = child->Value();
  168. if (NodeName == "Key")
  169. {
  170. ColorVertex pColor;
  171. TiXmlElement* time = child->FirstChildElement("Time");
  172. TiXmlElement* min = child->FirstChildElement("Min");
  173. TiXmlElement* max = child->FirstChildElement("Max");
  174. if (time)
  175. {
  176. pColor.Time = (float)atof (time->Attribute("val"));
  177. }
  178. if (min)
  179. {
  180. const char* hexVal = min->Attribute("val");
  181. hexVal += 4;
  182. long dwValue = strtol(hexVal, NULL, 16);
  183. pColor.MinValue = (dword)dwValue;
  184. }
  185. if (max)
  186. {
  187. const char* hexVal = max->Attribute("val");
  188. hexVal += 4;
  189. long dwValue = strtol(hexVal, NULL, 16);
  190. pColor.MaxValue = (dword)dwValue;
  191. }
  192. ColorGraph.Add(pColor);
  193. }
  194. }
  195. }
  196. }
  197. #endif