SphereEdit.cpp
Upload User: kairuinn
Upload Date: 2009-02-07
Package Size: 2922k
Code Size: 8k
Category:

Graph program

Development Platform:

Visual C++

  1. #include "stdafx.h"
  2. #include "SphereEdit.h"
  3. #include "..//resource.h"
  4. #include <math.h>
  5. CSphereEditCommand::CSphereEditCommand(sgCSphere* edS, IApplicationInterface* appI):
  6. m_editable_sphere(edS)
  7. , m_app(appI)
  8. , m_r_panel(NULL)
  9. , m_was_started(false)
  10. ,m_other_params_dlg(NULL)
  11. , m_matr(NULL)
  12. {
  13. ASSERT(edS);
  14. ASSERT(m_app);
  15. m_center.x = m_center.y = m_center.z = 0.0;
  16. }
  17. CSphereEditCommand::~CSphereEditCommand()
  18. {
  19. m_app->GetCommandPanel()->RemoveAllDialogs();
  20. if(m_matr)
  21. delete m_matr;
  22. m_app->GetViewPort()->InvalidateViewPort();
  23. }
  24. bool    CSphereEditCommand::PreTranslateMessage(MSG* pMsg)
  25. {
  26. /*if (pMsg->message==WM_KEYUP||
  27. pMsg->message==WM_CHAR)
  28. return false;*/
  29. if (pMsg->message==WM_KEYUP||pMsg->message==WM_KEYDOWN || 
  30. pMsg->message==WM_CHAR)
  31. {
  32. if (pMsg->wParam==VK_RETURN)
  33. {
  34. OnEnter();
  35. return true;
  36. }
  37. if (pMsg->wParam==VK_ESCAPE)
  38. {
  39. m_app->StopCommander();
  40. return true;
  41. }
  42. if (m_r_panel)
  43. {
  44. m_r_panel->GetWindow()->SendMessage(pMsg->message,
  45. pMsg->wParam,
  46. pMsg->lParam);
  47. }
  48. if (m_other_params_dlg)
  49. {
  50. m_other_params_dlg->SendMessage(pMsg->message,
  51. pMsg->wParam,
  52. pMsg->lParam);
  53. return false;
  54. }
  55. if (pMsg->message==WM_KEYDOWN)
  56. return false;
  57. else 
  58. return true;
  59. }
  60. else
  61. {
  62. if (pMsg->hwnd == m_app->GetViewPort()->GetWindow()->m_hWnd)
  63. {
  64. switch(pMsg->message) 
  65. {
  66. case WM_MOUSEMOVE:
  67. MouseMove(pMsg->wParam,GET_X_LPARAM(pMsg->lParam),GET_Y_LPARAM(pMsg->lParam));
  68. return true;
  69. case WM_LBUTTONUP:
  70. LeftClick(pMsg->wParam,GET_X_LPARAM(pMsg->lParam),GET_Y_LPARAM(pMsg->lParam));
  71. return true;
  72. default:
  73. return false;
  74. }
  75. }
  76. }
  77. return false;
  78. }
  79. void  CSphereEditCommand::Start()
  80. {
  81. ASSERT(m_editable_sphere);
  82. m_matr = new sgCMatrix(m_editable_sphere->GetWorldMatrixData());
  83. m_matr->Transparent();
  84. SWITCH_RESOURCE
  85. m_app->GetCommandPanel()->RemoveAllDialogs();
  86. CString lab;
  87. lab.LoadString(IDS_NEW_RADIUS);
  88. m_r_panel = reinterpret_cast<IGetNumberPanel*>(m_app->
  89. GetCommandPanel()->
  90. AddDialog(IBaseInterfaceOfGetDialogs::GET_NUMBER_DLG,lab,false));
  91. lab.LoadString(IDS_REDACTION);
  92. m_app->StartCommander(lab);
  93. lab.LoadString(IDS_ENTER_NEW_RAD);
  94. m_app->PutMessage(IApplicationInterface::MT_MESSAGE,lab);
  95. m_editable_sphere->GetGeometry(m_sph_geo);
  96. m_r_panel->SetNumber(m_sph_geo.Radius);
  97. sgCMatrix spM(m_editable_sphere->GetWorldMatrixData());
  98. spM.Transparent();
  99. spM.ApplyMatrixToPoint(m_center);
  100. m_app->GetCommandPanel()->SetActiveRadio(0);
  101. m_app->GetViewPort()->SetEditableObject(m_editable_sphere);
  102. m_was_started = true;
  103. }
  104. void  CSphereEditCommand::MouseMove(unsigned int nFlags,int pX,int pY)
  105. {
  106. if (!m_was_started)
  107. return;
  108. SWITCH_RESOURCE
  109. ASSERT(m_r_panel);
  110. double plD;
  111. SG_VECTOR PlNorm;
  112. m_app->GetViewPort()->GetViewPortNormal(PlNorm);
  113. sgSpaceMath::PlaneFromNormalAndPoint(m_center,PlNorm,plD);
  114. if (m_app->GetViewPort()->ProjectScreenPointOnPlane(pX,pY,PlNorm,plD,m_cur_pnt))
  115. {
  116. m_rad = m_app->ApplyPrecision(
  117. sgSpaceMath::PointsDistance(m_center,m_cur_pnt)
  118. );
  119. }
  120. else
  121. m_rad = 0.0;
  122. m_r_panel->SetNumber(m_rad);
  123. m_app->GetViewPort()->InvalidateViewPort();
  124. }
  125. void  CSphereEditCommand::LeftClick(unsigned int nFlags,int pX,int pY)
  126. {
  127. if (!m_was_started)
  128. return;
  129. SWITCH_RESOURCE
  130. ASSERT(m_editable_sphere);
  131. CString m_message;
  132. double rd = m_app->ApplyPrecision(m_rad);
  133. if (fabs(rd)<0.0001)
  134. {
  135. m_message.LoadString(IDS_ERROR_ZERO_RAD);
  136. m_app->PutMessage(IApplicationInterface::MT_ERROR,
  137. m_message);
  138. return;
  139. }
  140. else
  141. if (rd<-0.0001)
  142. {
  143. m_message.LoadString(IDS_ERROR_RAD_MUST_BE_POSIT);
  144. m_app->PutMessage(IApplicationInterface::MT_ERROR,
  145. m_message);
  146. return;
  147. }
  148. sgCSphere* sp = sgCreateSphere(rd,m_sph_geo.MeridiansCount,m_sph_geo.ParallelsCount);
  149. if (!sp)
  150. return;
  151. sp->InitTempMatrix()->Multiply(*m_matr);
  152. sp->ApplyTempMatrix();
  153. sp->DestroyTempMatrix();
  154. sgGetScene()->StartUndoGroup();
  155. sgGetScene()->DetachObject(m_editable_sphere);
  156. sgGetScene()->AttachObject(sp);
  157. sgGetScene()->EndUndoGroup();
  158. m_app->CopyAttributes(*sp,*m_editable_sphere);
  159. m_app->GetViewPort()->InvalidateViewPort();
  160. m_app->StopCommander();
  161. }
  162. void  CSphereEditCommand::Draw()
  163. {
  164. if (!m_was_started)
  165. return;
  166. float pC[3];
  167. m_app->GetViewPort()->GetPainter()->GetUserColorPoints(pC[0],pC[1],pC[2]);
  168. m_app->GetViewPort()->GetPainter()->SetCurColor(pC[0],pC[1],pC[2]);
  169. m_app->GetViewPort()->GetPainter()->DrawPoint(m_cur_pnt);
  170. m_app->GetViewPort()->GetPainter()->DrawPoint(m_center);
  171. m_app->GetViewPort()->GetPainter()->GetUserColorLines(pC[0],pC[1],pC[2]);
  172. m_app->GetViewPort()->GetPainter()->SetCurColor(pC[0],pC[1],pC[2]);
  173. m_app->GetViewPort()->GetPainter()->SetTransformMatrix(m_matr);
  174. m_app->GetViewPort()->GetPainter()->DrawSphere(m_rad);
  175. m_app->GetViewPort()->GetPainter()->SetTransformMatrix(NULL);
  176. }
  177. void  CSphereEditCommand::OnEnter()
  178. {
  179. if (!m_was_started)
  180. return;
  181. SWITCH_RESOURCE
  182. ASSERT(m_editable_sphere);
  183. CString m_message;
  184. double rd = m_r_panel->GetNumber();
  185. if (fabs(rd)<0.0001)
  186. {
  187. m_message.LoadString(IDS_ERROR_ZERO_RAD);
  188. m_app->PutMessage(IApplicationInterface::MT_ERROR,
  189. m_message);
  190. return;
  191. }
  192. else
  193. if (rd<-0.0001)
  194. {
  195. m_message.LoadString(IDS_ERROR_RAD_MUST_BE_POSIT);
  196. m_app->PutMessage(IApplicationInterface::MT_ERROR,
  197. m_message);
  198. return;
  199. }
  200. sgCSphere* sp = sgCreateSphere(rd,m_sph_geo.MeridiansCount,m_sph_geo.ParallelsCount);
  201. if (!sp)
  202. return;
  203. sp->InitTempMatrix()->Multiply(*m_matr);
  204. sp->ApplyTempMatrix();
  205. sp->DestroyTempMatrix();
  206. sgGetScene()->StartUndoGroup();
  207. sgGetScene()->DetachObject(m_editable_sphere);
  208. sgGetScene()->AttachObject(sp);
  209. sgGetScene()->EndUndoGroup();
  210. m_app->CopyAttributes(*sp,*m_editable_sphere);
  211. m_app->GetViewPort()->InvalidateViewPort();
  212. m_app->StopCommander();
  213. }
  214. unsigned int  CSphereEditCommand::GetItemsCount()
  215. {
  216. return 2;
  217. }
  218. void         CSphereEditCommand::GetItem(unsigned int itemID, CString& itSrt)
  219. {
  220. SWITCH_RESOURCE
  221. switch(itemID) {
  222. case 0:
  223. itSrt.LoadString(IDS_CHANGE_SPH_RAD);
  224. break;
  225. case 1:
  226. itSrt.LoadString(IDS_OTHER_PARAMS);
  227. break;
  228. /*case 2:
  229. itSrt.LoadString(IDS_CHANGE_NORM);
  230. break;*/
  231. default:
  232. ASSERT(0);
  233. }
  234. }
  235. void     CSphereEditCommand::GetItemState(unsigned int itemID, 
  236. bool& enbl, bool& checked)
  237. {
  238. enbl = true;
  239. if (!m_was_started)
  240. {
  241. checked = false;
  242. }
  243. else
  244. {
  245. checked = true;
  246. /*if (itemID==m_scenario)
  247. checked = true;*/
  248. }
  249. }
  250. HBITMAP     CSphereEditCommand::GetItemBitmap(unsigned int)
  251. {
  252. return NULL;
  253. }
  254. void         CSphereEditCommand::Run(unsigned int itemID)
  255. {
  256. if (itemID==1)
  257. {
  258. SWITCH_RESOURCE
  259. CSphereParamsDlg dlg;
  260. m_other_params_dlg = &dlg;
  261. ASSERT(m_editable_sphere);
  262. m_editable_sphere->GetGeometry(m_sph_geo);
  263. dlg.SetParams(m_sph_geo.MeridiansCount, m_sph_geo.ParallelsCount);
  264. if (dlg.DoModal()==IDOK)
  265. {
  266. m_other_params_dlg = NULL;
  267. int mC,pC;
  268. dlg.GetParams(mC,pC);
  269. sgCMatrix spM(m_editable_sphere->GetWorldMatrixData());
  270. spM.Transparent();
  271. spM.ApplyMatrixToPoint(m_center);
  272. sgCSphere* sp = sgCreateSphere(m_sph_geo.Radius,mC,pC);
  273. if (!sp)
  274. return;
  275. sp->InitTempMatrix()->Multiply(spM);
  276. sp->ApplyTempMatrix();
  277. sp->DestroyTempMatrix();
  278. sgGetScene()->StartUndoGroup();
  279. sgGetScene()->DetachObject(m_editable_sphere);
  280. sgGetScene()->AttachObject(sp);
  281. sgGetScene()->EndUndoGroup();
  282. m_app->CopyAttributes(*sp,*m_editable_sphere);
  283. m_app->GetViewPort()->InvalidateViewPort();
  284. }
  285. m_app->StopCommander();
  286. return;
  287. }
  288. if (!m_was_started)
  289. {
  290. /*if (itemID==0 || itemID==1 || itemID==2)
  291. {
  292. m_scenario = itemID;*/
  293. Start();
  294. /*}
  295. else
  296. {
  297. ASSERT(0);
  298. }*/
  299. }
  300. else
  301. {
  302. /* m_scenario = itemID;
  303. NewScenario();*/
  304. }
  305. }