rotatecontroller.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. #include "missioneditor.h"
  2. #include "rotatecontroller.h"
  3. #include "forms\mainwindow.h"
  4. #include "forms\panel.h"
  5. #include "attributes\AttributeList.h"
  6. #define OFFSET_FROM_ZERO 0.0f
  7. #define AXIS_SIZE 1.00f
  8. #define OFFSET_TO_PRINT 1.25f
  9. #define OFFSET_ADD 0.35f
  10. extern BaseAttribute* pEditableNode;
  11. extern TMainWindow* MainWindow;
  12. extern Matrix ViewPortProjectionMatrix;
  13. RotateController::RotateController ()
  14. {
  15. AxisScale = 1.0f;
  16. newCursorPos = Vector (0.0f);
  17. sNormal = Vector (0.0f);
  18. v1 = Vector (0.0f);
  19. v2 = Vector (0.0f);
  20. bPressed = false;
  21. Mode = DISABLE;
  22. Active = false;
  23. Mode = 0;
  24. pRS = (IRender*)api->GetService("DX9Render");
  25. p3DFont = pRS->CreateFont("Arial", 18.0f, 0xFFFFFF, "EditorFont");
  26. }
  27. RotateController::~RotateController ()
  28. {
  29. if (p3DFont) p3DFont->Release();
  30. }
  31. void RotateController::Activate (bool Active)
  32. {
  33. this->Active = Active;
  34. }
  35. MissionEditor::tCreatedMO* RotateController::GetSelectedObject ()
  36. {
  37. GUITreeNode* sNode = MainWindow->TreeView1->GetSelectedNode ();
  38. if (sNode == NULL) return NULL;
  39. if (sNode->bReadOnly) return NULL;
  40. if (sNode->Tag == TAG_ATTRIBUTE)
  41. {
  42. BaseAttribute* pBaseNode = (BaseAttribute*)sNode->Data;
  43. MissionEditor::tCreatedMO* curNode = (MissionEditor::tCreatedMO*)pBaseNode->GetMasterData ();
  44. return curNode;
  45. }
  46. if (sNode->Tag != TAG_OBJECT) return NULL;
  47. MissionEditor::tCreatedMO* pMo = (MissionEditor::tCreatedMO*)sNode->Data;
  48. if (!pMo->pObject.Validate()) return NULL;
  49. return pMo;
  50. }
  51. RotationAttribute* RotateController::GetFirstRO_FromSelectedObject ()
  52. {
  53. GUITreeNode* sNode = MainWindow->TreeView1->GetSelectedNode ();
  54. if (sNode == NULL) return NULL;
  55. if (sNode->bReadOnly) return NULL;
  56. if (sNode->Tag == TAG_ATTRIBUTE)
  57. {
  58. BaseAttribute* pBaseNode = (BaseAttribute*)sNode->Data;
  59. if (pBaseNode->GetType() != IMOParams::t_angles) return NULL;
  60. RotationAttribute* rot = (RotationAttribute*)pBaseNode;
  61. return rot;
  62. }
  63. if (sNode->Tag != TAG_OBJECT) return NULL;
  64. MissionEditor::tCreatedMO* pMo = (MissionEditor::tCreatedMO*)sNode->Data;
  65. if (!pMo->pObject.Validate()) return NULL;
  66. RotationAttribute* finded_attr = NULL;
  67. for (int n =0; n < pMo->AttrList->GetCount(); n++)
  68. {
  69. BaseAttribute* bAttr = pMo->AttrList->Get(n);
  70. if (bAttr->GetType() == IMOParams::t_angles)
  71. {
  72. finded_attr = (RotationAttribute*)bAttr;
  73. return finded_attr;
  74. }
  75. }
  76. return NULL;
  77. }
  78. void RotateController::Draw ()
  79. {
  80. if (!Active) return;
  81. RotationAttribute* f_attr = GetFirstRO_FromSelectedObject ();
  82. if (f_attr == NULL) return;
  83. Vector cPos, cAngles;
  84. cAngles = f_attr->GetValue();
  85. PositionAttribute* f_posattr = GetFirstPO_FromSelectedObject ();
  86. if (f_posattr)
  87. cPos = f_posattr->GetValue();
  88. else
  89. cPos = Vector(0.0f);
  90. nMatrix = Matrix().Build (cAngles);
  91. nMatrix.pos = cPos;
  92. //pRS->Print(0, 0, 0xFFFFFFFF, "cPos.x")
  93. if (!bPressed)
  94. {
  95. Vector tobject_pos = cPos * pRS->GetView();
  96. float fDistance = tobject_pos.z;
  97. // float fDistance = sqrtf(~(cPos - .GetCamPos()));
  98. AxisScale = fDistance / 10.0f;
  99. if (AxisScale < 1.0f) AxisScale = 1.0f;
  100. }
  101. /*
  102. Matrix matObj;
  103. pMo->pObject->GetMatrix(matObj);
  104. Vector cPos = matObj.pos;
  105. */
  106. pRS->SetWorld (Matrix());
  107. //X
  108. DWORD color = 0xFFFF0000;
  109. if (Mode == X_AXIS) color = 0xFFFFFF00;
  110. pRS->DrawVector (Vector (OFFSET_FROM_ZERO, 0.0f, 0.0f)*nMatrix, Vector (AXIS_SIZE*AxisScale, 0.0f, 0.0f)*nMatrix, color, "EditorLineNoZ");
  111. p3DFont->SetColor (color);
  112. p3DFont->Print (Vector (OFFSET_TO_PRINT*AxisScale, 0.0f, 0.0f)*nMatrix, 1000.0f, 0.0f, "X");
  113. //Y
  114. color = 0xFF00FF00;
  115. if (Mode == Y_AXIS) color = 0xFFFFFF00;
  116. pRS->DrawVector (Vector (0.0f, OFFSET_FROM_ZERO, 0.0f)*nMatrix, Vector (0.0f, AXIS_SIZE*AxisScale, 0.0f)*nMatrix, color, "EditorLineNoZ");
  117. p3DFont->SetColor (color);
  118. p3DFont->Print (Vector (0.0f, OFFSET_TO_PRINT*AxisScale, 0.0f)*nMatrix, 1000.0f, 0.0f, "Y");
  119. //Z
  120. color = 0xFF0000FF;
  121. if (Mode == Z_AXIS) color = 0xFFFFFF00;
  122. pRS->DrawVector (Vector (0.0f, 0.0f, OFFSET_FROM_ZERO)*nMatrix, Vector (0.0f, 0.0f, AXIS_SIZE*AxisScale)*nMatrix, color, "EditorLineNoZ");
  123. p3DFont->SetColor (color);
  124. p3DFont->Print (Vector (0.0f, 0.0f, OFFSET_TO_PRINT*AxisScale)*nMatrix, 1000.0f, 0.0f, "Z");
  125. /*
  126. // additional draw...
  127. // BLUE
  128. color = 0xFF0000FF;
  129. pRS->DrawLine (cPos + Vector (0.0f, 0.0f, OFFSET_ADD), color, cPos + Vector (OFFSET_ADD, 0.0f, OFFSET_ADD), color, false, "EditorLineNoZ");
  130. pRS->DrawLine (cPos + Vector (0.0f, 0.0f, OFFSET_ADD), color, cPos + Vector (0.0f, OFFSET_ADD, OFFSET_ADD), color, false, "EditorLineNoZ");
  131. // GREEN
  132. color = 0xFF00FF00;
  133. pRS->DrawLine (cPos + Vector (0.0f, OFFSET_ADD, 0.0f), color, cPos + Vector (0.0f, OFFSET_ADD, OFFSET_ADD), color, false, "EditorLineNoZ");
  134. pRS->DrawLine (cPos + Vector (0.0f, OFFSET_ADD, 0.0f), color, cPos + Vector (OFFSET_ADD, OFFSET_ADD, 0.0f), color, false, "EditorLineNoZ");
  135. // RED
  136. color = 0xFFFF0000;
  137. pRS->DrawLine (cPos + Vector (OFFSET_ADD, 0.0f, 0.0f), color, cPos + Vector (OFFSET_ADD, 0.0f, OFFSET_ADD), color, false, "EditorLineNoZ");
  138. pRS->DrawLine (cPos + Vector (OFFSET_ADD, 0.0f, 0.0f), color, cPos + Vector (OFFSET_ADD, OFFSET_ADD, 0.0f), color, false, "EditorLineNoZ");
  139. */
  140. AxisX = Line(Vector (0.0f, 0.0f, 0.0f)*nMatrix, Vector (AXIS_SIZE*AxisScale, 0.0f, 0.0f)*nMatrix);
  141. AxisY = Line(Vector (0.0f, 0.0f, 0.0f)*nMatrix, Vector (0.0f, AXIS_SIZE*AxisScale, 0.0f)*nMatrix);
  142. AxisZ = Line(Vector (0.0f, 0.0f, 0.0f)*nMatrix, Vector (0.0f, 0.0f, AXIS_SIZE*AxisScale)*nMatrix);
  143. //pRS->Print(v1.x, v1.y, 0xFFFFFFFF, "X");
  144. //pRS->Print(v2.x, v2.y, 0xFFFFFFFF, "X");
  145. //pRS->Print(newCursorPos.x, newCursorPos.y, 0xFFFFFFFF, "O");
  146. }
  147. void RotateController::ExtractRay (const RENDERVIEWPORT& viewport, const GUIPoint &ptCursor, Vector& raystart, Vector& rayend)
  148. {
  149. Matrix matProj = ViewPortProjectionMatrix;
  150. Vector v;
  151. v.x = ( ( ( 2.0f * ptCursor.x ) / viewport.Width ) - 1 ) / matProj.m[0][0];
  152. v.y = -( ( ( 2.0f * ptCursor.y ) / viewport.Height ) - 1 ) / matProj.m[1][1];
  153. v.z = 1.0f;
  154. Matrix mView = pRS->GetView();
  155. mView.Inverse ();
  156. Vector raydir;
  157. Vector rayorig;
  158. raydir = mView.MulNormal(v);
  159. rayorig = mView.pos;
  160. raystart = rayorig;
  161. rayend = (rayorig + (raydir * 100.f));
  162. }
  163. void RotateController::MouseMove (const GUIPoint &ptCursor, const RENDERVIEWPORT& viewport)
  164. {
  165. if (!Active) return;
  166. //--------------------------
  167. if (!bPressed)
  168. {
  169. Vector r1, r2;
  170. ExtractRay (viewport, ptCursor, r1, r2);
  171. Line mouse_line(r1, r2);
  172. Mode = DISABLE;
  173. Matrix nMatrixInv = nMatrix;
  174. nMatrixInv.Inverse();
  175. bool result = false;
  176. float CurDistance = 999999999.0f;
  177. Vector CamPos = pRS->GetView().GetCamPos();
  178. result = AxisX.IntersectionLines(mouse_line, pivot, 0.05f*AxisScale);
  179. if (result)
  180. {
  181. Vector nPivot = pivot * nMatrixInv;
  182. Vector nAxis1 = AxisX.p1 * nMatrixInv;
  183. Vector nAxis2 = AxisX.p2 * nMatrixInv;
  184. if ((nPivot.x >= nAxis1.x) && (nPivot.x <= nAxis2.x))
  185. {
  186. float cDistnace = (CamPos-pivot).GetLength();
  187. if (cDistnace < CurDistance)
  188. {
  189. Mode = X_AXIS;
  190. CurDistance = cDistnace;
  191. }
  192. }
  193. }
  194. result = AxisY.IntersectionLines(mouse_line, pivot, 0.05f*AxisScale);
  195. if (result)
  196. {
  197. Vector nPivot = pivot * nMatrixInv;
  198. Vector nAxis1 = AxisY.p1 * nMatrixInv;
  199. Vector nAxis2 = AxisY.p2 * nMatrixInv;
  200. if ((nPivot.y >= nAxis1.y) && (nPivot.y <= nAxis2.y))
  201. {
  202. float cDistnace = (CamPos-pivot).GetLength();
  203. if (cDistnace < CurDistance)
  204. {
  205. Mode = Y_AXIS;
  206. CurDistance = cDistnace;
  207. }
  208. }
  209. }
  210. result = AxisZ.IntersectionLines(mouse_line, pivot, 0.05f*AxisScale);
  211. if (result)
  212. {
  213. Vector nPivot = pivot * nMatrixInv;
  214. Vector nAxis1 = AxisZ.p1 * nMatrixInv;
  215. Vector nAxis2 = AxisZ.p2 * nMatrixInv;
  216. if ((nPivot.z >= nAxis1.z) && (nPivot.z <= nAxis2.z))
  217. {
  218. float cDistnace = (CamPos-pivot).GetLength();
  219. if (cDistnace < CurDistance)
  220. {
  221. Mode = Z_AXIS;
  222. CurDistance = cDistnace;
  223. }
  224. }
  225. }
  226. return;
  227. }
  228. //---------------------------------------------
  229. Vector mCursor;
  230. mCursor.x = (float)ptCursor.x;
  231. mCursor.y = (float)ptCursor.y;
  232. mCursor.z = 0.0f;
  233. //Vector relCursor = (mCursor - v1.v);
  234. //Vector flipedNormal = sNormal;
  235. //flipedNormal.x = sNormal.y;
  236. //flipedNormal.y = -sNormal.x;
  237. //newCursorPos = v1.v + ((relCursor | flipedNormal) * flipedNormal);
  238. //float dx = (float)HotSpot2D.x - newCursorPos.x;
  239. //float dy = (float)HotSpot2D.x - newCursorPos.y;
  240. float dist = (float)ptCursor.x - (float)HotSpot2D.x;
  241. //float dist = -dx;
  242. dist *= -0.01f;
  243. RotationAttribute* r_attr = GetFirstRO_FromSelectedObject();
  244. //Max
  245. if(!r_attr) return;
  246. //*********
  247. Vector cAngles;
  248. cAngles = r_attr->GetValue();
  249. Matrix OriginalMatrix = Matrix().Build (cAngles);
  250. Matrix DeltaMatrix;
  251. if (Mode == X_AXIS)
  252. {
  253. Vector angles = Vector (0.0f);
  254. angles.x = dist;
  255. DeltaMatrix.Build(angles);
  256. }
  257. if (Mode == Y_AXIS)
  258. {
  259. Vector angles = Vector (0.0f);
  260. angles.y = dist;
  261. DeltaMatrix.Build(angles);
  262. }
  263. if (Mode == Z_AXIS)
  264. {
  265. Vector angles = Vector (0.0f);
  266. angles.z = dist;
  267. DeltaMatrix.Build(angles);
  268. }
  269. Vector realangles;
  270. Matrix RealRotate = DeltaMatrix * OriginalMatrix;
  271. RealRotate.GetAngles (realangles.x, realangles.y, realangles.z);
  272. ClampAndLimit (realangles.x, r_attr->GetMin().x, r_attr->GetMax().x, r_attr->GetIsLimit());
  273. ClampAndLimit (realangles.y, r_attr->GetMin().y, r_attr->GetMax().y, r_attr->GetIsLimit());
  274. ClampAndLimit (realangles.z, r_attr->GetMin().z, r_attr->GetMax().z, r_attr->GetIsLimit());
  275. r_attr->SetValue(realangles);
  276. MissionEditor::tCreatedMO* curNode = GetSelectedObject ();
  277. MOPWriter wrt(curNode->Level, curNode->pObject.SPtr()->GetObjectID().c_str());
  278. curNode->AttrList->AddToWriter (wrt);
  279. #ifndef NO_TOOLS
  280. miss->EditorUpdateObject(curNode->pObject.SPtr(), wrt);
  281. #endif
  282. // curNode->pObject->EditMode_Update (wrt.Reader ());
  283. HotSpot2D = ptCursor;
  284. //HotSpot2D.y = (int)newCursorPos.y;
  285. //---------------------------------------------
  286. }
  287. bool RotateController::ButtonIsPressed (bool bPressed, const GUIPoint &ptCursor, const RENDERVIEWPORT& viewport)
  288. {
  289. if (!Active) return true;
  290. if (bPressed == false)
  291. {
  292. RotationAttribute* r_attr = GetFirstRO_FromSelectedObject();
  293. if (r_attr)
  294. {
  295. pEditableNode = r_attr;
  296. //MainWindow->UpdateTree(NULL);
  297. GUITreeNode* sNode = MainWindow->TreeView1->GetSelectedNode ();
  298. if (sNode->Tag == TAG_ATTRIBUTE)
  299. {
  300. BaseAttribute* pBaseNode = (BaseAttribute*)sNode->Data;
  301. pBaseNode->UpdateTree(sNode);
  302. }
  303. }
  304. }
  305. if (Mode == DISABLE) return false;
  306. if (Mode == X_AXIS)
  307. {
  308. Matrix mVP(pRS->GetView(), ViewPortProjectionMatrix);
  309. v1 = mVP.Projection(AxisX.p1, viewport.Width * 0.5f, viewport.Height * 0.5f);
  310. v2 = mVP.Projection(AxisX.p2, viewport.Width * 0.5f, viewport.Height * 0.5f);
  311. }
  312. if (Mode == Y_AXIS)
  313. {
  314. Matrix mVP(pRS->GetView(), ViewPortProjectionMatrix);
  315. v1 = mVP.Projection(AxisY.p1, viewport.Width * 0.5f, viewport.Height * 0.5f);
  316. v2 = mVP.Projection(AxisY.p2, viewport.Width * 0.5f, viewport.Height * 0.5f);
  317. }
  318. if (Mode == Z_AXIS)
  319. {
  320. Matrix mVP(pRS->GetView(), ViewPortProjectionMatrix);
  321. v1 = mVP.Projection(AxisZ.p1, viewport.Width * 0.5f, viewport.Height * 0.5f);
  322. v2 = mVP.Projection(AxisZ.p2, viewport.Width * 0.5f, viewport.Height * 0.5f);
  323. }
  324. HotSpot2D = ptCursor;
  325. sNormal = !(v2.v - v1.v);
  326. sNormal.z = 0.0f;
  327. this->bPressed = bPressed;
  328. return true;
  329. }
  330. bool RotateController::ValidValue (const Vector &vec)
  331. {
  332. float fDistance = sqrtf(~(vec - pRS->GetView().GetCamPos()));
  333. if (fDistance < 0.01f) return false;
  334. if (fDistance > 100.0f) return false;
  335. return true;
  336. }
  337. void RotateController::ClampAndLimit (float& angle, float minLimit, float maxLimit, bool IsLimit)
  338. {
  339. float DegAng = Rad2Deg(angle);
  340. // clamp angles
  341. int cX = (int)DegAng / 360;
  342. float ostX = (DegAng - (cX * 360));
  343. if (ostX < 0) ostX = 360.0f + ostX;
  344. // clamp angles
  345. DegAng = ostX;
  346. angle = Deg2Rad (DegAng);
  347. if (IsLimit)
  348. {
  349. if (angle < minLimit) angle = minLimit;
  350. if (angle > maxLimit) angle = maxLimit;
  351. }
  352. }
  353. PositionAttribute* RotateController::GetFirstPO_FromSelectedObject ()
  354. {
  355. GUITreeNode* sNode = MainWindow->TreeView1->GetSelectedNode ();
  356. if (sNode == NULL) return NULL;
  357. if (sNode->Tag == TAG_ATTRIBUTE)
  358. {
  359. int my_index = -1;
  360. for (int n = 0; n < sNode->Parent->Childs.GetCount(); n++)
  361. {
  362. if (sNode->Parent->Childs[n] == sNode)
  363. {
  364. my_index = n;
  365. break;
  366. }
  367. }
  368. if (my_index == -1) return NULL;
  369. for (int i = my_index; i >= 0; i--)
  370. {
  371. if (sNode->Parent->Childs[i]->Tag == TAG_ATTRIBUTE)
  372. {
  373. // Узел который мы проверяем являеться аттрибутом...
  374. BaseAttribute* pBaseNode = (BaseAttribute*)sNode->Parent->Childs[i]->Data;
  375. if (pBaseNode->GetType() == IMOParams::t_position)
  376. {
  377. PositionAttribute* pos = (PositionAttribute*)pBaseNode;
  378. return pos;
  379. }
  380. }
  381. }
  382. return NULL;
  383. }
  384. if (sNode->Tag != TAG_OBJECT) return NULL;
  385. MissionEditor::tCreatedMO* pMo = (MissionEditor::tCreatedMO*)sNode->Data;
  386. if (!pMo->pObject.Validate()) return NULL;
  387. PositionAttribute* finded_attr = NULL;
  388. for (int n =0; n < pMo->AttrList->GetCount(); n++)
  389. {
  390. BaseAttribute* bAttr = pMo->AttrList->Get(n);
  391. if (bAttr->GetType() == IMOParams::t_position)
  392. {
  393. finded_attr = (PositionAttribute*)bAttr;
  394. return finded_attr;
  395. }
  396. }
  397. return NULL;
  398. }