FloatAttr.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. #include "floatattr.h"
  2. #include "..\strutil.h"
  3. #include "..\..\missioneditor.h"
  4. #include "..\AttributeList.h"
  5. #include "..\..\fast_atof.h"
  6. extern IGUIManager* igui;
  7. #include "..\..\forms\globalParams.h"
  8. FloatAttribute & FloatAttribute::operator = (const FloatAttribute & source)
  9. {
  10. SetValue (source.GetValue());
  11. SetMax (source.GetMax());
  12. SetMin (source.GetMin());
  13. BaseAttribute::Copy(*this, source);
  14. return *this;
  15. }
  16. FloatAttribute & FloatAttribute::operator = (const IMOParams::Float& source)
  17. {
  18. SetValue (source.def);
  19. SetMax (source.max);
  20. SetMin (source.min);
  21. SetName (source.name);
  22. SetIsLimit (source.isLimit);
  23. return *this;
  24. }
  25. FloatAttribute::FloatAttribute ()
  26. {
  27. EditedObject = NULL;
  28. RealTimeValue = NULL;
  29. RealTimeDesc = NULL;
  30. value = 33.0f;
  31. min = 0.0f;
  32. max = 100.0f;
  33. Type = IMOParams::t_float;
  34. }
  35. FloatAttribute::~FloatAttribute ()
  36. {
  37. }
  38. void FloatAttribute::SetValue (float val)
  39. {
  40. value = val;
  41. }
  42. float FloatAttribute::GetValue () const
  43. {
  44. return value;
  45. }
  46. void FloatAttribute::SetMin (float val)
  47. {
  48. min = val;
  49. }
  50. float FloatAttribute::GetMin () const
  51. {
  52. return min;
  53. }
  54. void FloatAttribute::SetMax (float val)
  55. {
  56. max = val;
  57. }
  58. float FloatAttribute::GetMax () const
  59. {
  60. return max;
  61. }
  62. void FloatAttribute::PopupEdit (int pX, int pY)
  63. {
  64. //GUIWindow* wnd = igui->FindWindow (FLOATEDIT_WINDOWNAME);
  65. //if (wnd) igui->Close (wnd);
  66. Form = NEW TFloatEdit (0, 0);
  67. Form->SetPosition (pX, pY);
  68. // Form->lDescription->Caption = GetName ();
  69. Form->eValue->Text = FloatToStr (value);
  70. Form->eValue->Hint = GetName ();
  71. if (GetIsLimit())
  72. {
  73. Form->eValue->Hint += string ("\nmin:") + string (FloatToStr(min));
  74. Form->eValue->Hint += string ("\nmax:") + string (FloatToStr(max));
  75. }
  76. Form->MasterAttrib = this;
  77. igui->ShowModal (Form);
  78. // Обязательно нужно сделать...
  79. pForm = Form;
  80. }
  81. void FloatAttribute::AddToWriter (MOPWriter& wrt)
  82. {
  83. wrt.AddFloat (value);
  84. }
  85. void FloatAttribute::WriteToFile (IFile* pFile)
  86. {
  87. DWORD written = 0;
  88. DWORD slen = strlen (GetName ());
  89. written = pFile->Write(&slen, sizeof (DWORD));
  90. Assert (written == sizeof (DWORD));
  91. written = pFile->Write(GetName (), slen);
  92. Assert (written == slen);
  93. written = pFile->Write(&value, sizeof (float));
  94. Assert (written == sizeof (float));
  95. written = pFile->Write(&min, sizeof (float));
  96. Assert (written == sizeof (float));
  97. written = pFile->Write(&max, sizeof (float));
  98. Assert (written == sizeof (float));
  99. DWORD sLimit = IsLimit;
  100. written = pFile->Write(&sLimit, sizeof (DWORD));
  101. Assert (written == sizeof (DWORD));
  102. }
  103. #pragma warning (disable : 4800)
  104. void FloatAttribute::LoadFromFile (IFile* pFile, const char* ClassName)
  105. {
  106. DWORD loaded = 0;
  107. DWORD slen = 0;
  108. loaded = pFile->Read(&slen, sizeof (DWORD));
  109. Assert (loaded == sizeof (DWORD));
  110. char* ldName = NEW char[slen+1];
  111. ldName[slen] = 0;
  112. loaded = pFile->Read(ldName, slen);
  113. Assert (loaded == slen);
  114. SetName (ldName);
  115. delete ldName;
  116. float ldValue, ldMin, ldMax;
  117. ldValue = ldMin = ldMax = 0.0f;
  118. loaded = pFile->Read(&ldValue, sizeof (float));
  119. Assert (loaded == sizeof (float));
  120. loaded = pFile->Read(&ldMin, sizeof (float));
  121. Assert (loaded == sizeof (float));
  122. loaded = pFile->Read(&ldMax, sizeof (float));
  123. Assert (loaded == sizeof (float));
  124. value = ldValue;
  125. min = ldMin;
  126. max = ldMax;
  127. //---------------------------------
  128. DWORD sLimit = 0;
  129. loaded = pFile->Read(&sLimit, sizeof (DWORD));
  130. Assert (loaded == sizeof (DWORD));
  131. IsLimit = (bool)sLimit;
  132. }
  133. void FloatAttribute::UpdateTree(GUITreeNode * node, string * v)
  134. {
  135. gp->__tmpText = GetName ();
  136. gp->__tmpText += "#c808080";
  137. gp->__tmpText += string (" ") + string (FloatToStr(value));
  138. if (v && v->IsEmpty())
  139. {
  140. *v = string (FloatToStr(value));
  141. }
  142. node->Image->Load("meditor\\float");
  143. node->Tag = TAG_ATTRIBUTE;
  144. node->CanDrag = false;
  145. node->CanDrop = false;
  146. node->CanCopy = false;
  147. node->Data = this;
  148. node->SetText( gp->__tmpText);
  149. }
  150. void FloatAttribute::Add2Tree (GUITreeNodes* nodes, TreeNodesPool* nodesPool, string * v)
  151. {
  152. GUITreeNode* nNode = nodesPool->CreateNode();
  153. UpdateTree(nNode, v);
  154. nodes->Add(nNode);
  155. }
  156. void FloatAttribute::BeginRTEdit (MissionEditor::tCreatedMO* MissionObject, GUIControl* parent)
  157. {
  158. EditedObject = MissionObject;
  159. SavedValue = value;
  160. RealTimeValue = NEW GUIEdit (parent, 10, 10, 220, 19);
  161. RealTimeValue->Text = FloatToStr(value);
  162. RealTimeValue->pFont->SetName("arialcyrsmall");
  163. RealTimeValue->OnAdvNumChange = (CONTROL_EVENT)&FloatAttribute::OnRTAdvChange;
  164. RealTimeValue->OnAccept = (CONTROL_EVENT)&FloatAttribute::OnRTValueChange;
  165. RealTimeValue->Hint = "Drag value to smooth change";
  166. RealTimeDesc = NEW GUILabel (parent, 10, 40, 220, 19);
  167. RealTimeDesc->Caption = "#b#cFFFFFF";
  168. RealTimeDesc->Caption += MissionObject->pObject.Ptr()->GetObjectID().c_str();
  169. RealTimeDesc->Caption += "#cC0FFFF.";
  170. RealTimeDesc->Caption += GetName ();
  171. RealTimeDesc->pFont->SetName("arialcyrsmall");
  172. RealTimeDesc->Caption += "@c@b";
  173. if (!IsLimit)
  174. {
  175. RealTimeDesc->Caption += "\nNot limited attribute";
  176. } else
  177. {
  178. RealTimeDesc->Caption += "\nLimited attribute";
  179. RealTimeDesc->Caption += "\nMin: #cFFFFFF";
  180. RealTimeDesc->Caption += FloatToStr (min);
  181. RealTimeDesc->Caption += "\n@cMax: #cFFFFFF";
  182. RealTimeDesc->Caption += FloatToStr (max);
  183. }
  184. RealTimeDesc->Layout = GUILABELLAYOUT_Left;
  185. }
  186. void FloatAttribute::ApplyRTEdit ()
  187. {
  188. CloseRTEdit ();
  189. }
  190. void FloatAttribute::CancelRTEdit ()
  191. {
  192. value = SavedValue;
  193. SetupMissionObject ();
  194. CloseRTEdit ();
  195. }
  196. void FloatAttribute::CloseRTEdit ()
  197. {
  198. if (RealTimeValue)
  199. {
  200. delete RealTimeValue;
  201. RealTimeValue = NULL;
  202. }
  203. if (RealTimeDesc)
  204. {
  205. delete RealTimeDesc;
  206. RealTimeDesc = NULL;
  207. }
  208. }
  209. void _cdecl FloatAttribute::OnRTValueChange (GUIControl* sender)
  210. {
  211. value = fast_atof (RealTimeValue->Text.GetBuffer());
  212. Clamp ();
  213. RealTimeValue->Text = FloatToStr(value);
  214. SetupMissionObject ();
  215. }
  216. void FloatAttribute::Clamp ()
  217. {
  218. if (!IsLimit) return;
  219. if (value < min) value = min;
  220. if (value > max) value = max;
  221. if (RealTimeValue) RealTimeValue->Text = FloatToStr(value);
  222. }
  223. void FloatAttribute::SetupMissionObject ()
  224. {
  225. if (!EditedObject) return;
  226. MOPWriter wrt(EditedObject->Level, EditedObject->pObject.Ptr()->GetObjectID().c_str());
  227. EditedObject->AttrList->AddToWriter (wrt);
  228. #ifndef NO_TOOLS
  229. miss->EditorUpdateObject(EditedObject->pObject.Ptr(), wrt);
  230. #endif
  231. //EditedObject->pObject->EditMode_Update (wrt.Reader ());
  232. }
  233. void _cdecl FloatAttribute::OnRTAdvChange (GUIControl* sender)
  234. {
  235. igui->SetKeyboardFocus(NULL);
  236. float offset = igui->GetCursor()->DeltaY;
  237. if (api->DebugKeyState(VK_SHIFT))
  238. {
  239. offset = offset * 10.0f;
  240. }
  241. // Вано добавил, чтобы от 0.0 можно было мышкой увеличивать/уменьшать аттрибут
  242. if (fabsf(value) < 0.0001f)
  243. {
  244. value = 0.0001f * ((value < 0.0f) ? -1.0f : 1.0f);
  245. }
  246. value -= ((offset / 50.0f) * value) / 10.0f;
  247. Clamp ();
  248. RealTimeValue->Text = FloatToStr(value);
  249. SetupMissionObject ();
  250. }
  251. void FloatAttribute::WriteToXML (TextFile &file, int level)
  252. {
  253. file.Write(level, "<float val = \"%s\">\n", GetName ());
  254. file.Write(level+1, "<value val = \"%f\" />\n", value);
  255. file.Write(level, "</float>\n");
  256. }
  257. void FloatAttribute::ReadXML (TiXmlElement* Root, const char* szMasterClass)
  258. {
  259. const char* objectName = Root->Attribute("val");
  260. SetName(objectName);
  261. //Минимум и максимум берем из исходного
  262. MissionEditor::tAvailableMO* pObject = sMission->GetAvailableClassByName(szMasterClass);
  263. if (pObject)
  264. {
  265. BaseAttribute* SourceAttr = pObject->AttrList->FindInAttrList(GetName(), GetType());
  266. if (SourceAttr)
  267. {
  268. FloatAttribute* SrcAttr = (FloatAttribute*)SourceAttr;
  269. *this = *SrcAttr;
  270. }
  271. }
  272. TiXmlElement* node = Root->FirstChildElement("value");
  273. if (node) value = fast_atof (node->Attribute("val"));
  274. }