home *** CD-ROM | disk | FTP | other *** search
/ 3D Games - Real-time Rend…ng & Software Technology / 3D Games - Real-time Rendering & Software Technology.iso / flysdk / frontend / flyEditor / flyEditorView.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-11-21  |  13.8 KB  |  597 lines

  1. // flyEditorView.cpp : implementation of the CFlyEditorView class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "flyEditor.h"
  6. #include "flyEditorDoc.h"
  7. #include "flyEditorView.h"
  8. #include "flyRenderView.h"
  9. #include "LeftView.h"
  10. #include "DlgEditValue.h"
  11. #include "DlgEditList.h"
  12.  
  13. #ifdef _DEBUG
  14. #define new DEBUG_NEW
  15. #undef THIS_FILE
  16. static char THIS_FILE[] = __FILE__;
  17. #endif
  18.  
  19. extern CFlyRenderView *rv;
  20. CFlyEditorView *lv=0;
  21. extern CLeftView *tv;
  22. extern int active_mode;
  23. extern bsp_object *active_sel;
  24.  
  25. /////////////////////////////////////////////////////////////////////////////
  26. // CFlyEditorView
  27.  
  28. IMPLEMENT_DYNCREATE(CFlyEditorView, CListView)
  29.  
  30. BEGIN_MESSAGE_MAP(CFlyEditorView, CListView)
  31.     //{{AFX_MSG_MAP(CFlyEditorView)
  32.     ON_NOTIFY_REFLECT(LVN_ITEMCHANGED, OnItemchanged)
  33.     ON_NOTIFY_REFLECT(NM_DBLCLK, OnDblclk)
  34.     ON_NOTIFY_REFLECT(LVN_KEYDOWN, OnKeydown)
  35.     //}}AFX_MSG_MAP
  36. END_MESSAGE_MAP()
  37.  
  38. /////////////////////////////////////////////////////////////////////////////
  39. // CFlyEditorView construction/destruction
  40.  
  41. CFlyEditorView::CFlyEditorView()
  42. {
  43.     lv=this;
  44.     colsizes[0]=80;
  45.     colsizes[1]=80;
  46.     colsizes[2]=80;
  47.     ncol=0;
  48. }
  49.  
  50. CFlyEditorView::~CFlyEditorView()
  51. {
  52.     lv=0;
  53. }
  54.  
  55. BOOL CFlyEditorView::PreCreateWindow(CREATESTRUCT& cs)
  56. {
  57.     return CListView::PreCreateWindow(cs);
  58. }
  59.  
  60. /////////////////////////////////////////////////////////////////////////////
  61. // CFlyEditorView drawing
  62.  
  63. void CFlyEditorView::OnDraw(CDC* pDC)
  64. {
  65. }
  66.  
  67. void CFlyEditorView::OnInitialUpdate()
  68. {
  69.     CListView::OnInitialUpdate();
  70. }
  71.  
  72. /////////////////////////////////////////////////////////////////////////////
  73. // CFlyEditorView diagnostics
  74.  
  75. #ifdef _DEBUG
  76. void CFlyEditorView::AssertValid() const
  77. {
  78.     CListView::AssertValid();
  79. }
  80.  
  81. void CFlyEditorView::Dump(CDumpContext& dc) const
  82. {
  83.     CListView::Dump(dc);
  84. }
  85.  
  86. CFlyEditorDoc* CFlyEditorView::GetDocument() // non-debug version is inline
  87. {
  88.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CFlyEditorDoc)));
  89.     return (CFlyEditorDoc*)m_pDocument;
  90. }
  91. #endif //_DEBUG
  92.  
  93. /////////////////////////////////////////////////////////////////////////////
  94. // CFlyEditorView message handlers
  95. void CFlyEditorView::OnStyleChanged(int nStyleType, LPSTYLESTRUCT lpStyleStruct)
  96. {
  97. }
  98.  
  99. void CFlyEditorView::OnItemchanged(NMHDR* pNMHDR, LRESULT* pResult) 
  100. {
  101.     NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
  102.     *pResult = 0;
  103.  
  104.     CTreeCtrl& tree=tv->GetTreeCtrl();
  105.     HTREEITEM item=tree.GetSelectedItem();
  106.     if (item==0) return;
  107.     DWORD type=tree.GetItemData(item);
  108.     if (type==TREE_TYPE_TEXTURES ||
  109.         type==TREE_TYPE_LIGHTMAPS ||
  110.         type==TREE_TYPE_OBJECTS)
  111.         rv->Invalidate(1);
  112. }
  113.  
  114. void CFlyEditorView::reset_param_list()
  115. {
  116.     nparam=0;
  117.     CListCtrl& list=GetListCtrl();
  118.     list.InsertColumn( 0, "Param", LVCFMT_LEFT, colsizes[0],0 );
  119.     list.InsertColumn( 1, "Value", LVCFMT_LEFT, colsizes[1],0 );
  120.     ncol=2;
  121.     list.DeleteAllItems();
  122. }
  123.  
  124. void CFlyEditorView::insert_param(char *name, int type, void *data)
  125. {
  126.     CListCtrl& list=GetListCtrl();
  127.     strcpy(pd[nparam].name,name);
  128.     pd[nparam].type=type; 
  129.     pd[nparam].data=data;
  130.     nparam++;
  131. }
  132.  
  133. char *CFlyEditorView::get_param_formated(int i)
  134. {
  135.     static char str[256];
  136.     str[0]=0;
  137.     if (pd[i].type>255 || pd[i].type<-255)
  138.         {
  139.         if (*((bsp_object **)pd[i].data)!=0)
  140.             strcpy(str,(*((bsp_object **)pd[i].data))->long_name);
  141.         }
  142.     else
  143.     switch(pd[i].type)
  144.     {
  145.     case ' ':
  146.         break;
  147.     case 'f':
  148.         sprintf(str,"%.5f",*((float *)pd[i].data));
  149.         break;
  150.     case 'a':
  151.         sprintf(str,"%.5f",acos(*((float *)pd[i].data))*PiUnder180);
  152.         break;
  153.     case 'i':
  154.         sprintf(str,"%i",*((int *)pd[i].data));
  155.         break;
  156.     case 'c':
  157.     case 'v':
  158.         sprintf(str,"%.4f %.4f %.4f",((float *)pd[i].data)[0],((float *)pd[i].data)[1],((float *)pd[i].data)[2]);
  159.         break;
  160.     case 'p':
  161.         if (*((int *)pd[i].data)!=-1)
  162.             sprintf(str,"%s",flyengine->piclib[*((int *)pd[i].data)]->name);
  163.         break;
  164.     case 'b':
  165.     case 's':
  166.         strcpy(str,(char *)pd[i].data);
  167.         break;
  168.     case '3':
  169.         if (*((mesh **)pd[i].data)!=0)
  170.             strcpy(str,(*((mesh **)pd[i].data))->name);
  171.         break;
  172.     case 'm':
  173.         if (*((mesh **)pd[i].data)!=0)
  174.             strcpy(str,(*((mesh **)pd[i].data))->name);
  175.         break;
  176.     case 'z':
  177.         if (*((bezier_curve **)pd[i].data)!=0)
  178.             strcpy(str,(*((bezier_curve **)pd[i].data))->name);
  179.         break;
  180.     case 'h':
  181.         if (*((bezier_patch **)pd[i].data)!=0)
  182.             strcpy(str,(*((bezier_patch **)pd[i].data))->name);
  183.         break;
  184.     case 'w':
  185.         if (*((sound **)pd[i].data)!=0)
  186.             strcpy(str,(*((sound **)pd[i].data))->name);
  187.         break;
  188.     case 'o':
  189.         if (*((bsp_object **)pd[i].data)!=0)
  190.             strcpy(str,(*((bsp_object **)pd[i].data))->long_name);
  191.         break;
  192.     case 'd':
  193.         if (*((bsp_object **)pd[i].data)!=0)
  194.             strcpy(str,(*((bsp_object **)pd[i].data))->long_name);
  195.         break;
  196.     }
  197.     return str;
  198. }
  199.  
  200. void CFlyEditorView::fill_param_list()
  201. {
  202.     CListCtrl& list=GetListCtrl();
  203.     int i;
  204.     for( i=0;i<nparam;i++ )
  205.         {
  206.         list.InsertItem(i,pd[i].name);
  207.         char *buf=get_param_formated(i);
  208.         list.SetItem(i,1,LVIF_TEXT,buf,0,0,0,0);
  209.         }
  210. }
  211.  
  212. void CFlyEditorView::OnDblclk(NMHDR* pNMHDR, LRESULT* pResult) 
  213. {
  214.     if (pResult)
  215.         *pResult = 0;
  216.  
  217.     CTreeCtrl& tree=tv->GetTreeCtrl();
  218.     HTREEITEM item=tree.GetSelectedItem();
  219.     if (item==0) return;
  220.     DWORD type=tree.GetItemData(item);
  221.     if (type==TREE_TYPE_TEXTURES ||
  222.         type==TREE_TYPE_LIGHTMAPS ||
  223.         type==TREE_TYPE_OBJECTS)
  224.         return;
  225.     
  226.     CListCtrl& list=GetListCtrl();
  227.     POSITION pos=list.GetFirstSelectedItemPosition();
  228.     if (pos==0)    return;
  229.     int i=list.GetNextSelectedItem(pos);
  230.  
  231.     if (type==TREE_TYPE_SOUNDS)
  232.         {
  233.         CString str=list.GetItemText(i,0);
  234.         sound *s=flyengine->get_sound_object((char *)(LPCSTR)str);
  235.         if (s && s->buf[0])
  236.             s->buf[0]->Play(0,0,0);
  237.         return;
  238.         }
  239.  
  240.     if (GetAsyncKeyState(VK_SHIFT)&(1<<16))
  241.     {
  242.         if (pd[i].type=='o' || pd[i].type>255)
  243.             tv->SelectObject(tree.GetRootItem(),*((bsp_object **)pd[i].data));
  244.         else if (pd[i].type=='d' || pd[i].type<255)
  245.             tv->SelectObject(tree.GetRootItem(),(*((bsp_object **)pd[i].data))->source);
  246.     }
  247.     else
  248.         {
  249.         edit_param(i);
  250.         char *buf=get_param_formated(i);
  251.         list.SetItem(i,1,LVIF_TEXT,buf,0,0,0,0);
  252.         }
  253. }
  254.  
  255. void CFlyEditorView::update_instances(int i)
  256. {
  257.     CTreeCtrl& tree=tv->GetTreeCtrl();
  258.     HTREEITEM item=tree.GetSelectedItem();
  259.     if (item==0) return;
  260.     DWORD type=tree.GetItemData(item);
  261.     
  262.     if (type==TREE_TYPE_GLOBAL)
  263.     {
  264.         bsp_object *obj;
  265.         obj=flyengine->stock_obj0;
  266.         while(obj)
  267.         {
  268.             obj->message(vector(0,0,0),0,FLYOBJM_CHANGEPARAM,0,0);
  269.             obj=(bsp_object *)obj->next_obj;
  270.         }
  271.         obj=flyengine->active_obj0;
  272.         while(obj)
  273.         {
  274.             obj->message(vector(0,0,0),0,FLYOBJM_CHANGEPARAM,0,0);
  275.             obj=(bsp_object *)obj->next_obj;
  276.         }
  277.     }
  278.     else
  279.     if (type>255)
  280.     {
  281.         bsp_object *o=(bsp_object *)type;
  282.         o->message(vector(0,0,0),0,FLYOBJM_CHANGEPARAM,0,0);
  283.         bsp_object *obj=flyengine->active_obj0;
  284.         while(obj)
  285.         {
  286.             if (obj->source==o)
  287.                 {
  288.                 char *c;
  289.                 int len=4;
  290.  
  291.                 c=((char *)pd[i].data)-((char *)o)+((char *)obj);
  292.                 if (pd[i].type=='v' || pd[i].type=='c')
  293.                     len=12;
  294.                 else if (pd[i].type=='s')
  295.                     len=strlen((char *)pd[i].data)+1;
  296.  
  297.                 memcpy(c,pd[i].data,len);
  298.                 obj->message(vector(0,0,0),0,FLYOBJM_CHANGEPARAM,0,0);
  299.                 }
  300.  
  301.             obj=(bsp_object *)obj->next_obj;
  302.         }
  303.     }
  304. }
  305.  
  306. void CFlyEditorView::edit_param(int i)
  307. {
  308.     int flag=0;
  309.     if (pd[i].type>255 || pd[i].type<-255)
  310.         {
  311.         edit_list(i);
  312.         flag=1;
  313.         }
  314.     else
  315.     switch(pd[i].type)
  316.     {
  317.     case ' ': 
  318.         break;
  319.     case 'p':
  320.         {
  321.         char file[256];
  322.         strcpy(file,flyengine->flydatapath);
  323.         strcat(file,"maps\\");
  324.         if (*((int *)pd[i].data)==-1)
  325.             strcat(file,"*.jpg;*.tga");
  326.         else
  327.             strcat(file,flyengine->piclib[*((int *)pd[i].data)]->name);
  328.         char *c=edit_files("","All image files (*.jpg;*.tga)|*.jpg;*.tga|TGA image files (*.tga)|*.tga|JPG image files (*.jpg)|*.jpg||",file);
  329.         if (c)
  330.             {
  331.             int np=flyengine->npiclib,p;
  332.             p=flyengine->get_picture(c);
  333.             *((int *)pd[i].data)=p;
  334.             if (p==np)
  335.                 {
  336.                 flyengine->close_texture_cache();
  337.                 flyengine->init_texture_cache();
  338.                 }
  339.             flag=1;
  340.             }
  341.         }
  342.         break;
  343.     case 'w':
  344.         {
  345.         char file[256];
  346.         strcpy(file,flyengine->flydatapath);
  347.         strcat(file,"sounds\\");
  348.         if (*((sound **)pd[i].data)==0)
  349.             strcat(file,"*.wav");
  350.         else strcat(file,(*((sound **)pd[i].data))->name);
  351.         char *c=edit_files("wav","Wave sound files (*.wav)|*.wav||",file);
  352.         if (c)
  353.             {
  354.             *((sound **)pd[i].data)=flyengine->get_sound_object(c);
  355.             flag=1;
  356.             }
  357.         }
  358.         break;
  359.     case '3':
  360.         {
  361.         char file[256];
  362.         strcpy(file,flyengine->flydatapath);
  363.         strcat(file,"objects\\");
  364.         if (*((mesh **)pd[i].data)==0)
  365.             strcat(file,"*.3ds");
  366.         else strcat(file,(*((mesh **)pd[i].data))->name);
  367.         char *c=edit_files("3ds","3DStudio model files (*.3ds)|*.3ds||",file);
  368.         if (c)
  369.             {
  370.             *((mesh **)pd[i].data)=flyengine->get_model_object(c);
  371.             flyengine->close_texture_cache();
  372.             flyengine->init_texture_cache();
  373.             flag=1;
  374.             }
  375.         }
  376.         break;
  377.     case 'm':
  378.         {
  379.         char file[256];
  380.         strcpy(file,flyengine->flydatapath);
  381.         strcat(file,"objects\\");
  382.         if (*((mesh **)pd[i].data)==0)
  383.             strcat(file,"*.fao");
  384.         else strcat(file,(*((mesh **)pd[i].data))->name);
  385.         char *c=edit_files("fao","Fly3D Animated Object (*.fao)|*.fao||",file);
  386.         if (c)
  387.             {
  388.             *((mesh **)pd[i].data)=flyengine->get_model_object(c);
  389.             flyengine->close_texture_cache();
  390.             flyengine->init_texture_cache();
  391.             flag=1;
  392.             }
  393.         }
  394.         break;
  395.     case 'z':
  396.         {
  397.         char file[256];
  398.         strcpy(file,flyengine->flydatapath);
  399.         strcat(file,"objects\\");
  400.         if (*((bezier_curve **)pd[i].data)==0)
  401.             strcat(file,"*.bez");
  402.         else strcat(file,(*((bezier_curve**)pd[i].data))->name);
  403.         char *c=edit_files("bez","Fly3D Bezier Curve (*.bez)|*.bez||",file);
  404.         if (c)
  405.             {
  406.             *((bezier_curve **)pd[i].data)=flyengine->get_bezier_curve(c);
  407.             flag=1;
  408.             }
  409.         }
  410.         break;
  411.     case 'h':
  412.         {
  413.         char file[256];
  414.         strcpy(file,flyengine->flydatapath);
  415.         strcat(file,"objects\\");
  416.         if (*((bezier_patch **)pd[i].data)==0)
  417.             strcat(file,"*.pch");
  418.         else strcat(file,(*((bezier_patch**)pd[i].data))->name);
  419.         char *c=edit_files("pch","Fly3D Bezier Patch (*.pch)|*.pch||",file);
  420.         if (c)
  421.             {
  422.             *((bezier_patch **)pd[i].data)=flyengine->get_bezier_patch(c);
  423.             flag=1;
  424.             }
  425.         }
  426.         break;
  427.     case 'c':
  428.         {
  429.         vector *v=(vector *)pd[i].data;
  430.         COLORREF c=RGB(v->x*255,v->y*255,v->z*255);
  431.         CColorDialog cd(c,CC_FULLOPEN,this);
  432.         if (cd.DoModal()==IDOK)
  433.             {
  434.             c=cd.GetColor();
  435.             v->x=GetRValue(c)/255.0f;
  436.             v->y=GetGValue(c)/255.0f;
  437.             v->z=GetBValue(c)/255.0f;
  438.             flag=1;
  439.             }
  440.         }
  441.         break;
  442.     case 'f':
  443.     case 'v':
  444.     case 's':
  445.     case 'i':
  446.     case 'a':
  447.         edit_value(i);
  448.         flag=1;
  449.         break;
  450.     case 'o':
  451.     case 'd':
  452.         edit_list(i);
  453.         flag=1;
  454.         break;
  455.     case 'b':
  456.         char file[256];
  457.         CFlyEditorDoc *doc=GetDocument();
  458.         if (doc->GetPathName().IsEmpty())
  459.             {
  460.             AfxMessageBox("Changing this parameter requires saving the current document.",MB_OK|MB_ICONINFORMATION);
  461.             break;
  462.             }
  463.  
  464.         if (IDNO==AfxMessageBox("Changing the bsp file requires saving the current document and restarting.\nDo you want to proceed ?",MB_YESNO|MB_ICONQUESTION))
  465.             break;
  466.  
  467.         strcpy(file,flyengine->flydatapath);
  468.         strcat(file,"*.bsp");
  469.         char *c=edit_files("bsp","Fly bsp files (*.bsp)|*.bsp||",file);
  470.         if (c)
  471.             {
  472.             *strrchr(c,'.')=0;
  473.             strcpy((char *)pd[i].data,c);
  474.             doc->OnSaveDocument((char *)(LPCSTR)doc->GetPathName());
  475.             doc->OnOpenDocument((char *)(LPCSTR)doc->GetPathName());
  476.             }
  477.         break;
  478.     }
  479.     update_instances(i);
  480.     GetDocument()->SetModifiedFlag(flag);
  481.     rv->Invalidate(0);
  482. }
  483.  
  484. char *CFlyEditorView::edit_files(char *ext,char *filter,char *cur)
  485. {
  486.     static char file[256];
  487.     CFileDialog fd(TRUE,ext,cur,OFN_NOCHANGEDIR|OFN_PATHMUSTEXIST|OFN_FILEMUSTEXIST,filter,this);
  488.     if (fd.DoModal()==IDOK)
  489.     {
  490.         strcpy(file,fd.GetPathName());
  491.         strlwr(file);
  492.  
  493.         if (strrchr(cur,'\\'))
  494.             *(strrchr(cur,'\\')+1)=0;
  495.         else if (strrchr(cur,'/'))
  496.             *(strrchr(cur,'/')+1)=0;
  497.         int len=strlen(cur);
  498.         if (!strncmp(file,cur,len))
  499.             return &file[len];
  500.     }
  501.     return 0;
  502. }
  503.  
  504. void CFlyEditorView::edit_list(int i)
  505. {
  506.     CDlgEditList dlg;
  507.     dlg.pd=&pd[i];
  508.     dlg.DoModal();
  509. }
  510.  
  511. void CFlyEditorView::edit_value(int i)
  512. {
  513.     CDlgEditValue dlg;
  514.     dlg.pd=&pd[i];
  515.     strcpy(dlg.value,get_param_formated(i));
  516.     dlg.DoModal();
  517. }
  518.  
  519. void CFlyEditorView::OnKeydown(NMHDR* pNMHDR, LRESULT* pResult) 
  520. {
  521.     LV_KEYDOWN* pLVKeyDown = (LV_KEYDOWN*)pNMHDR;
  522.  
  523.     if (pLVKeyDown->wVKey==VK_RETURN)
  524.         OnDblclk(0,0);
  525.     else if (pLVKeyDown->wVKey==VK_DELETE)
  526.     {
  527.     CListCtrl& list=GetListCtrl();
  528.     POSITION pos=list.GetFirstSelectedItemPosition();
  529.     if (pos==0)    return;
  530.     int i=list.GetNextSelectedItem(pos);
  531.     char str[256];
  532.     sprintf(str,"Clear parameter '%s' ?",pd[i].name);
  533.     if (IDYES==AfxMessageBox(str,MB_YESNO|MB_ICONQUESTION))
  534.         {
  535.         int len=4;
  536.         if (pd[i].type=='v' || pd[i].type=='c')
  537.             len=12;
  538.         else if (pd[i].type=='s' || pd[i].type=='b')
  539.             len=1;
  540.         if (pd[i].type=='p')
  541.             *((int *)pd[i].data)=-1;
  542.         else memset(pd[i].data,0,len);
  543.         
  544.         char *buf=get_param_formated(i);
  545.         list.SetItem(i,1,LVIF_TEXT,buf,0,0,0,0);
  546.         GetDocument()->SetModifiedFlag();
  547.         }
  548.     }
  549.     
  550.     *pResult = 0;
  551. }
  552.  
  553. void CFlyEditorView::set_param_list(bsp_object *o)
  554. {
  555.     char buf[256];
  556.     int i,j,k;
  557.     reset_param_list();    
  558.     if (o!=0)
  559.         {
  560.         k=0;
  561.         for( j=0;j<NUM_DEFAULT_PARAM;j++ )
  562.             {
  563.             o->get_param_desc(j,&pd[k]);
  564.             sprintf(buf,"(%s)",pd[k].name);
  565.             insert_param(buf,pd[k].type,pd[k].data);
  566.             k++;
  567.             }
  568.         i=o->get_param_desc(0,0);
  569.         for( j=NUM_DEFAULT_PARAM;j<i;j++ )
  570.             o->get_param_desc(j,&pd[k++]);
  571.         nparam=k;
  572.         fill_param_list();
  573.         }
  574. }
  575.  
  576. void CFlyEditorView::update_param_list()
  577. {
  578.     GetDocument()->find_active();
  579.     if (active_sel)
  580.         {
  581.         int i,j,k=0;
  582.         for( j=0;j<NUM_DEFAULT_PARAM;j++ )
  583.             {
  584.             active_sel->get_param_desc(j,&pd[k]);
  585.             k++;
  586.             }
  587.         i=active_sel->get_param_desc(0,0);
  588.         for( j=NUM_DEFAULT_PARAM;j<i;j++ )
  589.             active_sel->get_param_desc(j,&pd[k++]);
  590.         if (k==nparam)
  591.             {
  592.             CListCtrl& list=GetListCtrl();
  593.             for( i=0;i<nparam;i++ )
  594.                 list.SetItem(i,1,LVIF_TEXT,pd[i].get_string(),0,0,0,0);
  595.             }
  596.         }
  597. }