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

Graph program

Development Platform:

Visual C++

  1. #include "stdafx.h"
  2. #include "SplineEdit.h"
  3. #include "..//resource.h"
  4. #include <math.h>
  5. #include <float.h>
  6. SplineEditCommand::SplineEditCommand(sgCSpline* es, IApplicationInterface* appI):
  7. m_editable_spline(es)
  8. , m_clone_spline(NULL)
  9. , m_geo(NULL)
  10. , m_app(appI)
  11. ,m_sel_point_panel(NULL)
  12. , m_get_point_panel(NULL)
  13. , m_choise_pnt(-1)
  14. , m_step(0)
  15. , m_was_started(false)
  16. {
  17. ASSERT(es);
  18. ASSERT(m_app);
  19. SWITCH_RESOURCE
  20. m_bitmap = new CBitmap;
  21. m_bitmap->LoadBitmap(IDB_SPL_EDIT);
  22. }
  23. SplineEditCommand::~SplineEditCommand()
  24. {
  25. m_app->GetCommandPanel()->RemoveAllDialogs();
  26. m_app->GetViewPort()->InvalidateViewPort();
  27. if (m_bitmap)
  28. {
  29. if (m_bitmap->m_hObject)
  30. m_bitmap->DeleteObject();
  31. delete m_bitmap;
  32. }
  33. if (m_clone_spline)
  34. sgDeleteObject(m_clone_spline);
  35. /*if (m_geo)
  36. SG_SPLINE::DeleteSplineGeo(m_geo);*/
  37. }
  38. void     SplineEditCommand::SendCommanderMessage(ICommander::COMMANDER_MESSAGE mes, 
  39.    void* params) 
  40. {
  41. if (mes==ICommander::CM_SWITCH_ROLLUP_DIALOG)
  42. {
  43. ASSERT(params!=NULL);
  44. int   newActiveDlg = *(reinterpret_cast<int*>(params));
  45. ASSERT(newActiveDlg<=1);
  46. m_step = (unsigned int)newActiveDlg;
  47. if (newActiveDlg==0)
  48. {
  49. SWITCH_RESOURCE
  50. m_app->GetCommandPanel()->EnableRadio(1,false);
  51. m_message.LoadString(IDS_CHOISE_SPLINE_PNT);
  52. m_app->PutMessage(IApplicationInterface::MT_MESSAGE,m_message);
  53. }
  54. m_app->GetViewPort()->InvalidateViewPort();
  55. return;
  56. }
  57. if (mes==ICommander::CM_UPDATE_COMMAND_PANEL)
  58. {
  59. ASSERT(params==NULL);
  60. if (m_step==0)
  61. {
  62. m_choise_pnt = m_sel_point_panel->GetCurrentPoint();
  63. m_app->GetViewPort()->InvalidateViewPort();
  64. }
  65. }
  66. }
  67. bool    SplineEditCommand::PreTranslateMessage(MSG* pMsg)
  68. {
  69. /*if (pMsg->message==WM_KEYUP||
  70. pMsg->message==WM_CHAR)
  71. return false;*/
  72. if (pMsg->message==WM_KEYUP||pMsg->message==WM_KEYDOWN || 
  73. pMsg->message==WM_CHAR)
  74. {
  75. if (pMsg->wParam==VK_RETURN)
  76. {
  77. OnEnter();
  78. return true;
  79. }
  80. if (pMsg->wParam==VK_ESCAPE)
  81. {
  82. m_app->StopCommander();
  83. return true;
  84. }
  85. switch(m_step) 
  86. {
  87. case 0:
  88. if (m_sel_point_panel)
  89. {
  90. m_sel_point_panel->GetWindow()->SendMessage(pMsg->message,
  91. pMsg->wParam,
  92. pMsg->lParam);
  93. }
  94. break;
  95. case 1:
  96. if (m_get_point_panel)
  97. {
  98. m_get_point_panel->GetWindow()->SendMessage(pMsg->message,
  99. pMsg->wParam,
  100. pMsg->lParam);
  101. }
  102. break;
  103. default:
  104. break;
  105. }
  106. if (pMsg->message==WM_KEYDOWN)
  107. return false;
  108. else 
  109. return true;
  110. }
  111. else
  112. {
  113. if (pMsg->hwnd == m_app->GetViewPort()->GetWindow()->m_hWnd)
  114. {
  115. switch(pMsg->message) 
  116. {
  117. case WM_MOUSEMOVE:
  118. MouseMove(pMsg->wParam,GET_X_LPARAM(pMsg->lParam),GET_Y_LPARAM(pMsg->lParam));
  119. return true;
  120. case WM_LBUTTONUP:
  121. LeftClick(pMsg->wParam,GET_X_LPARAM(pMsg->lParam),GET_Y_LPARAM(pMsg->lParam));
  122. return true;
  123. default:
  124. return false;
  125. }
  126. }
  127. }
  128. return false;
  129. }
  130. void  SplineEditCommand::Start()
  131. {
  132. ASSERT(m_editable_spline);
  133. SWITCH_RESOURCE
  134. CString lab;
  135. lab.LoadString(IDS_REDACTION);
  136. m_app->StartCommander(lab);
  137. if (m_clone_spline)
  138. sgDeleteObject(m_clone_spline);
  139. m_clone_spline = reinterpret_cast<sgCSpline*>(m_editable_spline->Clone());
  140. m_geo = const_cast<SG_SPLINE*>(m_clone_spline->GetGeometry());
  141. NewPanels();
  142. m_app->GetViewPort()->SetEditableObject(m_editable_spline);
  143. m_was_started = true;
  144. }
  145. void   SplineEditCommand::NewPanels()
  146. {
  147. SWITCH_RESOURCE
  148. CString lab;
  149. lab.LoadString(IDS_CHOISE_SPLINE_PNT);
  150. m_app->GetCommandPanel()->RemoveAllDialogs();
  151. m_sel_point_panel = 
  152. reinterpret_cast<ISelectPointPanel*>(m_app->GetCommandPanel()->
  153. AddDialog(IBaseInterfaceOfGetDialogs::SELECT_POINT_DLG,
  154. lab,true));
  155. if (m_geo)
  156. {
  157. int ssz = (m_geo->IsClosed())?(m_geo->GetKnotsCount()-1):(m_geo->GetKnotsCount());
  158. for (int i=0;i<ssz;i++)
  159. m_sel_point_panel->AddPoint(m_geo->GetKnots()[i].x,
  160. m_geo->GetKnots()[i].y,
  161. m_geo->GetKnots()[i].z);
  162. }
  163. lab.LoadString(IDS_NEW_COORDINATES);
  164. m_get_point_panel = 
  165. reinterpret_cast<IGetPointPanel*>(m_app->GetCommandPanel()->
  166. AddDialog(IBaseInterfaceOfGetDialogs::GET_POINT_DLG,
  167. lab,true));
  168. m_app->GetCommandPanel()->SetActiveRadio(0);
  169. m_step=0;
  170. m_choise_pnt =0;
  171. }
  172. void  SplineEditCommand::MouseMove(unsigned int nFlags,int pX,int pY)
  173. {
  174. if (!m_was_started)
  175. return;
  176. SWITCH_RESOURCE
  177. if (m_step==0)
  178. {
  179. ASSERT(m_sel_point_panel);
  180. ASSERT(m_editable_spline);
  181. ASSERT(m_geo);
  182. double    winX1,winY1,winZ1;
  183. double    minDist = FLT_MAX;
  184. m_choise_pnt = -1;
  185. for (int i=0;i<m_geo->GetKnotsCount();i++)
  186. {
  187. m_app->GetViewPort()->ProjectWorldPoint(m_geo->GetKnots()[i],winX1,winY1,winZ1);
  188. double d1;
  189. d1 = sqrt((winX1-pX)*(winX1-pX)+(winY1-pY)*(winY1-pY));
  190. if (d1<minDist)
  191. {
  192. minDist = d1;
  193. m_choise_pnt = i;
  194. }
  195. }
  196. if (m_choise_pnt>=0)
  197. {
  198. if (m_sel_point_panel)
  199. m_sel_point_panel->SetCurrentPoint(m_choise_pnt);
  200. }
  201. }
  202. else
  203. {
  204. if (m_choise_pnt<0)
  205. {
  206. ASSERT(0);
  207. return;
  208. }
  209. ASSERT(m_get_point_panel);
  210. IViewPort::GET_SNAP_IN in_arg;
  211. in_arg.scrX = pX;
  212. in_arg.scrY = pY;
  213. in_arg.snapType = SNAP_SYSTEM;
  214. in_arg.XFix = m_get_point_panel->IsXFixed();
  215. in_arg.YFix = m_get_point_panel->IsYFixed();
  216. in_arg.ZFix = m_get_point_panel->IsZFixed();
  217. double tmpFl[3];
  218. m_get_point_panel->GetPoint(tmpFl[0],tmpFl[1],tmpFl[2]);
  219. in_arg.FixPoint.x = tmpFl[0];
  220. in_arg.FixPoint.y = tmpFl[1];
  221. in_arg.FixPoint.z = tmpFl[2];
  222. IViewPort::GET_SNAP_OUT out_arg;
  223. m_app->GetViewPort()->GetWorldPointAfterSnap(in_arg,out_arg);
  224. m_cur_pnt = out_arg.result_point;
  225. m_get_point_panel->SetPoint(m_cur_pnt.x,m_cur_pnt.y,m_cur_pnt.z);
  226. m_geo->MoveKnot(m_choise_pnt,m_cur_pnt);
  227. }
  228. m_app->GetViewPort()->InvalidateViewPort();
  229. }
  230. void  SplineEditCommand::LeftClick(unsigned int nFlags,int pX,int pY)
  231. {
  232. if (!m_was_started)
  233. return;
  234. SWITCH_RESOURCE
  235. if (m_step==0)
  236. {
  237. if (m_choise_pnt>=0)
  238. {
  239. m_step++;
  240. m_app->GetCommandPanel()->SetActiveRadio(m_step);
  241. }
  242. else
  243. {
  244. ASSERT(0);
  245. }
  246. }
  247. else
  248. {
  249. if (m_choise_pnt<0)
  250. {
  251. ASSERT(0);
  252. return;
  253. }
  254. CString mes;
  255. if ((m_choise_pnt>0 && sgSpaceMath::PointsDistance((m_geo->GetKnots()[m_choise_pnt]),
  256. (m_geo->GetKnots()[m_choise_pnt-1]))<0.001) ||
  257. (m_choise_pnt<m_geo->GetKnotsCount()-1 && sgSpaceMath::PointsDistance((m_geo->GetKnots()[m_choise_pnt]),
  258. (m_geo->GetKnots()[m_choise_pnt+1]))<0.001))
  259. {
  260. m_message.LoadString(IDS_ERROR_SPL_KNOT_AS_PREV);
  261. m_app->PutMessage(IApplicationInterface::MT_ERROR,
  262. m_message);
  263. return;
  264. }
  265. sgCSpline* spl = sgCreateSpline(*m_geo);
  266. if (!spl)
  267. {
  268. if (m_clone_spline)
  269. {
  270. sgDeleteObject(m_clone_spline);
  271. m_clone_spline = NULL;
  272. }
  273. //m_geo = SG_SPLINE::CreateSplineGeo();
  274. m_message.LoadString(IDS_CREARE_SPL_ERROR);
  275. m_app->PutMessage(IApplicationInterface::MT_ERROR,
  276. m_message);
  277. m_app->GetViewPort()->InvalidateViewPort();
  278. m_app->StopCommander();
  279. return;
  280. }
  281. sgGetScene()->StartUndoGroup();
  282. sgGetScene()->DetachObject(m_editable_spline);
  283. sgGetScene()->AttachObject(spl);
  284. sgGetScene()->EndUndoGroup();
  285. m_app->CopyAttributes(*spl,*m_editable_spline);
  286. m_app->GetViewPort()->InvalidateViewPort();
  287. m_app->StopCommander();
  288. }
  289. }
  290. void  SplineEditCommand::Draw()
  291. {
  292. if (!m_was_started)
  293. return;
  294. if (!m_geo)
  295. return;
  296. float pC[3];
  297. if (m_choise_pnt>=0)
  298. {
  299. m_app->GetViewPort()->GetPainter()->GetUserColorPoints(pC[0],pC[1],pC[2]);
  300. m_app->GetViewPort()->GetPainter()->SetCurColor(pC[0],pC[1],pC[2]);
  301. m_app->GetViewPort()->GetPainter()->DrawPoint(m_geo->GetKnots()[m_choise_pnt]);
  302. }
  303. m_app->GetViewPort()->GetPainter()->GetUserColorLines(pC[0],pC[1],pC[2]);
  304. m_app->GetViewPort()->GetPainter()->SetCurColor(pC[0],pC[1],pC[2]);
  305. m_app->GetViewPort()->GetPainter()->DrawSplineFrame(m_geo);
  306. m_app->GetViewPort()->GetPainter()->SetCurColor(1.0,0.0,0.0);
  307. m_app->GetViewPort()->GetPainter()->DrawSpline(m_geo);
  308. }
  309. void  SplineEditCommand::OnEnter()
  310. {
  311. if (!m_was_started)
  312. return;
  313. SWITCH_RESOURCE
  314. if (m_step==0)
  315. {
  316. if (m_choise_pnt>=0)
  317. {
  318. m_step++;
  319. m_app->GetCommandPanel()->SetActiveRadio(m_step);
  320. }
  321. else
  322. {
  323. ASSERT(0);
  324. }
  325. }
  326. else
  327. {
  328. ASSERT(m_get_point_panel);
  329. m_get_point_panel->GetPoint(m_cur_pnt.x,m_cur_pnt.y, m_cur_pnt.z);
  330. if (m_choise_pnt<0)
  331. {
  332. ASSERT(0);
  333. return;
  334. }
  335. m_geo->MoveKnot(m_choise_pnt,m_cur_pnt);
  336. CString mes;
  337. if ((m_choise_pnt>0 && sgSpaceMath::PointsDistance((m_geo->GetKnots()[m_choise_pnt]),
  338. (m_geo->GetKnots()[m_choise_pnt-1]))<0.001) ||
  339. (m_choise_pnt<m_geo->GetKnotsCount()-1 && sgSpaceMath::PointsDistance((m_geo->GetKnots()[m_choise_pnt]),
  340. (m_geo->GetKnots()[m_choise_pnt+1]))<0.001))
  341. {
  342. m_message.LoadString(IDS_ERROR_SPL_KNOT_AS_PREV);
  343. m_app->PutMessage(IApplicationInterface::MT_ERROR,
  344. m_message);
  345. return;
  346. }
  347. sgCSpline* spl = sgCreateSpline(*m_geo);
  348. if (!spl)
  349. {
  350. if (m_clone_spline)
  351. {
  352. sgDeleteObject(m_clone_spline);
  353. m_clone_spline = NULL;
  354. }
  355. //m_geo = SG_SPLINE::CreateSplineGeo();
  356. m_message.LoadString(IDS_CREARE_SPL_ERROR);
  357. m_app->PutMessage(IApplicationInterface::MT_ERROR,
  358. m_message);
  359. m_app->GetViewPort()->InvalidateViewPort();
  360. m_app->StopCommander();
  361. return;
  362. }
  363. sgGetScene()->StartUndoGroup();
  364. sgGetScene()->DetachObject(m_editable_spline);
  365. sgGetScene()->AttachObject(spl);
  366. sgGetScene()->EndUndoGroup();
  367. m_app->CopyAttributes(*spl,*m_editable_spline);
  368. m_app->GetViewPort()->InvalidateViewPort();
  369. m_app->StopCommander();
  370. }
  371. }
  372. unsigned int  SplineEditCommand::GetItemsCount()
  373. {
  374. if (!m_was_started)
  375. return 1;
  376. else
  377. return 0;
  378. }
  379. void         SplineEditCommand::GetItem(unsigned int itemID, CString& itSrt)
  380. {
  381. if (m_was_started)
  382. {
  383. ASSERT(0);
  384. return;
  385. }
  386. SWITCH_RESOURCE
  387. if(itemID==0) 
  388. {
  389. itSrt.LoadString(IDS_MOVE_SPLINE_KNOT);
  390. }
  391. else
  392. ASSERT(0);
  393. }
  394. void     SplineEditCommand::GetItemState(unsigned int itemID, bool& enbl, bool& checked)
  395. {
  396. if (m_was_started)
  397. {
  398. ASSERT(0);
  399. return;
  400. }
  401. enbl = true;
  402. checked = false;
  403. }
  404. HBITMAP   SplineEditCommand::GetItemBitmap(unsigned int)
  405. {
  406. /*if (!m_was_started)
  407. return (HBITMAP)m_bitmap;
  408. else*/
  409. return NULL;
  410. }
  411. void         SplineEditCommand::Run(unsigned int itemID)
  412. {
  413. if (!m_was_started)
  414. {
  415. if (itemID==0 || itemID==1)
  416. {
  417. //m_scenario = itemID;
  418. Start();
  419. }
  420. else
  421. {
  422. ASSERT(0);
  423. }
  424. }
  425. /*else
  426. m_scenario = itemID;*/
  427. }