home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 03 Pathfinding with Astar / 01 Matthews / ase / NodeView.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-07-04  |  5.4 KB  |  221 lines

  1. // LeftView.cpp : implementation of the CNodeView class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "ase.h"
  6.  
  7. #include "aseDoc.h"
  8. #include "NodeView.h"
  9.  
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15.  
  16. /////////////////////////////////////////////////////////////////////////////
  17. // CNodeView
  18.  
  19. IMPLEMENT_DYNCREATE(CNodeView, CTreeView)
  20.  
  21. BEGIN_MESSAGE_MAP(CNodeView, CTreeView)
  22.     //{{AFX_MSG_MAP(CNodeView)
  23.     ON_NOTIFY_REFLECT(TVN_SELCHANGED, OnSelChanged)
  24.     //}}AFX_MSG_MAP
  25. END_MESSAGE_MAP()
  26.  
  27. /////////////////////////////////////////////////////////////////////////////
  28. // CNodeView construction/destruction
  29.  
  30. TVSORTCB CNodeView::m_tvSortData;
  31.  
  32. CNodeView::CNodeView()
  33. {
  34.     m_cImageList.Create(IDB_NODEICONS,16,16,RGB(255,0,255));
  35.  
  36.     m_cTahoma.CreatePointFont(80, "Verdana");
  37. }
  38.  
  39. CNodeView::~CNodeView()
  40. {
  41. }
  42.  
  43. BOOL CNodeView::PreCreateWindow(CREATESTRUCT& cs)
  44. {
  45.     // TODO: Modify the Window class or styles here by modifying
  46.     //  the CREATESTRUCT cs
  47.  
  48.     return CTreeView::PreCreateWindow(cs);
  49. }
  50.  
  51. /////////////////////////////////////////////////////////////////////////////
  52. // CNodeView drawing
  53.  
  54. void CNodeView::OnDraw(CDC* pDC)
  55. {
  56.     CAseDoc* pDoc = GetDocument();
  57.     ASSERT_VALID(pDoc);
  58.  
  59.     // TODO: add draw code for native data here
  60. }
  61.  
  62.  
  63. void CNodeView::OnInitialUpdate()
  64. {
  65.     CTreeView::OnInitialUpdate();
  66.  
  67.     CTreeCtrl &ctrl = GetTreeCtrl();
  68.  
  69.     ctrl.SetFont(&m_cTahoma, true);
  70.     ctrl.SetImageList(&m_cImageList, TVSIL_NORMAL);
  71.     ctrl.DeleteAllItems();
  72.  
  73.     m_tvSortData.hParent = NULL;
  74.     m_tvSortData.lpfnCompare = SortByF;
  75.     m_tvSortData.lParam = 0;
  76. }
  77.  
  78. /////////////////////////////////////////////////////////////////////////////
  79. // CNodeView diagnostics
  80.  
  81. #ifdef _DEBUG
  82. void CNodeView::AssertValid() const
  83. {
  84.     CTreeView::AssertValid();
  85. }
  86.  
  87. void CNodeView::Dump(CDumpContext& dc) const
  88. {
  89.     CTreeView::Dump(dc);
  90. }
  91.  
  92. CAseDoc* CNodeView::GetDocument() // non-debug version is inline
  93. {
  94.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CAseDoc)));
  95.     return (CAseDoc*)m_pDocument;
  96. }
  97. #endif //_DEBUG
  98.  
  99. /////////////////////////////////////////////////////////////////////////////
  100. // CNodeView message handlers
  101.  
  102. BOOL CNodeView::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext) 
  103. {
  104.     dwStyle |= TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS | TVS_SHOWSELALWAYS;
  105.     
  106.     return CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
  107. }
  108.  
  109. int CNodeView::OnNotifyChild(_asNode *parent, _asNode *child, int data, void *pThis)
  110. {
  111.     CNodeView *me = reinterpret_cast<CNodeView *>(pThis);
  112.     CTreeCtrl &treectrl = me->GetTreeCtrl();
  113.  
  114.     CString str;
  115.     str.Format("%d,%d: f=%d, g=%d, h=%d", child->x, child->y, child->f, child->g, child->h);
  116.  
  117.     HTREEITEM hNew;
  118.     HTREEITEM hTree = (parent) ? HTREEITEM(parent->dataptr) : treectrl.GetRootItem();
  119.  
  120.     me->GetDocument()->NodeAdded(child, data);
  121.  
  122.     int icon = data;
  123.     hNew = treectrl.InsertItem(str, icon, icon, hTree);
  124.  
  125.     child->dataptr = (void *)(hNew);
  126.     treectrl.SetItemData(hNew, DWORD(child));
  127.  
  128.     return 0;
  129. }
  130.  
  131. int CNodeView::OnNotifyList(_asNode *previous, _asNode *addnode, int data, void *pThis)
  132. {
  133.     CNodeView *me = reinterpret_cast<CNodeView *>(pThis);
  134.     
  135.     HTREEITEM hItem;
  136.     HTREEITEM hPrevious = (previous) ? HTREEITEM(previous->dataptr) : NULL;
  137.  
  138.     CString str;
  139.     CTreeCtrl &treectrl = me->GetTreeCtrl();
  140.     
  141.     if (data < 3) {
  142.         hItem = treectrl.GetNextItem(treectrl.GetRootItem(), TVGN_NEXT);
  143.         str.Format("%d,%d: f = %d", addnode->x, addnode->y, addnode->f);
  144.  
  145.         if (data == 0) {
  146.             hItem = treectrl.InsertItem(str, 2, 2, hItem, hPrevious);
  147.             treectrl.SetItemData(hItem, DWORD(addnode));
  148.         } else if (data == 1) {
  149.             hItem = treectrl.InsertItem(str, 2, 2, hItem, hItem);
  150.             treectrl.SetItemData(hItem, DWORD(addnode));
  151.         } else if (data == 2) {
  152.             hItem = treectrl.GetChildItem(hItem);
  153.             if (hItem) treectrl.DeleteItem(hItem);
  154.         }
  155.     } else if (data == 3) {
  156.         hItem = treectrl.GetRootItem();
  157.         hItem = treectrl.GetNextItem(hItem, TVGN_NEXT);
  158.         hItem = treectrl.GetNextItem(hItem, TVGN_NEXT);
  159.  
  160.         str.Format("%d,%d", addnode->x, addnode->y);
  161.  
  162.         hItem = treectrl.InsertItem(str, 4, 4, hItem, NULL);
  163.         treectrl.SetItemData(hItem, DWORD(addnode));
  164.     }
  165.  
  166.     return 0;
  167. }
  168.  
  169. void CNodeView::OnSelChanged(NMHDR* pNMHDR, LRESULT* pResult) 
  170. {
  171.     NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
  172.  
  173.     HTREEITEM hTreeItem = pNMTreeView->itemNew.hItem;
  174.     _asNode *node = (_asNode *)(GetTreeCtrl().GetItemData(hTreeItem));
  175.  
  176.     GetDocument()->DrawNode(node);
  177.  
  178.     *pResult = 0;
  179. }
  180.  
  181. void CNodeView::OnPreAStar()
  182. {
  183.     m_hGoalNode = NULL;
  184.     GetTreeCtrl().DeleteAllItems();
  185. }
  186.  
  187. void CNodeView::OnPostAStar(_asNode *end)
  188. {
  189.     ASSERT(end);
  190.  
  191.     m_hGoalNode = HTREEITEM(end->dataptr);
  192.     GetTreeCtrl().SetItemState(m_hGoalNode, TVIS_BOLD, TVIS_BOLD);
  193. }
  194.  
  195. void CNodeView::DisplayGoalNode()
  196. {
  197.     if (m_hGoalNode) GetTreeCtrl().EnsureVisible(m_hGoalNode);
  198. }
  199.  
  200. int CNodeView::SortByF(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
  201. {
  202.     _asNode *item1 = reinterpret_cast<_asNode *>(lParam1);
  203.     _asNode *item2 = reinterpret_cast<_asNode *>(lParam2);
  204.  
  205.     if (item1->f <= item2->f) return -1;
  206.  
  207.     return 1;
  208. }
  209.  
  210. void CNodeView::SortOpen()
  211. {
  212.     HTREEITEM hItem;
  213.     hItem = GetTreeCtrl().GetNextItem(GetTreeCtrl().GetRootItem(), TVGN_NEXT);
  214.  
  215.     if (hItem) {
  216.         m_tvSortData.hParent = hItem;
  217.  
  218.         GetTreeCtrl().SortChildrenCB(&m_tvSortData);
  219.     }
  220. }
  221.