D:/Zythum/DinoKod/MapEditor/BrushUnitsDialog.cpp

00001 // BrushUnitsDialog.cpp : fichier d'implémentation
00002 //
00003 
00004 #include "stdafx.h"
00005 #include "MapEditor.h"
00006 #include "Common/Parser.h"
00007 #include "Common/Error.h"
00008 #include "BrushUnitsDialog.h"
00009 
00010 
00011 // Boîte de dialogue CBrushUnitsDialog
00012 
00013 IMPLEMENT_DYNAMIC(CBrushUnitsDialog, CDialog)
00014 CBrushUnitsDialog::CBrushUnitsDialog(CWnd* pParent /*=NULL*/)
00015         : CDialog(CBrushUnitsDialog::IDD, pParent)
00016 {
00017         m_pEntityDef    = NULL;
00018         m_pSelectedKey  = NULL;
00019         m_pLandEntity   = NULL;
00020 }
00021 
00022 CBrushUnitsDialog::~CBrushUnitsDialog()
00023 {
00024         CEntityDef* pEntityDef;
00025 
00026         while( pEntityDef = m_EntityDefList.GetFirst() )
00027         {
00028                 m_EntityDefList.Remove( pEntityDef );
00029                 Deletep( pEntityDef );          
00030         }
00031 }
00032 
00033 void CBrushUnitsDialog::DoDataExchange(CDataExchange* pDX)
00034 {
00035         CDialog::DoDataExchange(pDX);
00036         DDX_Control(pDX, IDC_COMBO_CLASSNAME, m_ComboClassName);
00037         DDX_Control(pDX, IDC_RADIO_CREATE, m_ButtonCreate);
00038         DDX_Control(pDX, IDC_RADIO_EDIT, m_ButtonEdit);
00039         DDX_Control(pDX, IDC_RADIO_DELETE2, m_ButtonDelete);
00040         DDX_Control(pDX, IDC_LIST_ENTITY, m_ListEntity);
00041         DDX_Control(pDX, IDC_EDIT_VALUE, m_EditValue);
00042         DDX_Control(pDX, IDC_BUTTON_SET, m_ButtonSet);
00043 }
00044 
00045 
00046 BEGIN_MESSAGE_MAP(CBrushUnitsDialog, CDialog)
00047         ON_CBN_SELCHANGE(IDC_COMBO_CLASSNAME, OnCbnSelchangeComboClassname)
00048         ON_BN_CLICKED(IDC_RADIO_CREATE, OnBnClickedRadioCreate)
00049         ON_BN_CLICKED(IDC_RADIO_EDIT, OnBnClickedRadioEdit)
00050         ON_BN_CLICKED(IDC_RADIO_DELETE2, OnBnClickedRadioDelete2)
00051         ON_NOTIFY(NM_CLICK, IDC_LIST_ENTITY, OnNMClickListEntity)
00052         ON_BN_CLICKED(IDC_BUTTON_SET, OnBnClickedButtonSet)
00053 END_MESSAGE_MAP()
00054 
00055 
00056 // Gestionnaires de messages CBrushUnitsDialog
00057 #define FINDTOKEN               Parser.FindToken()
00058 #define TOKEN                   Parser.GetToken()
00059 #define CMPTOKEN(__s)   (stricmp(__s,Parser.GetToken()) == 0)
00060 
00061 BOOL CBrushUnitsDialog::OnInitDialog()
00062 {
00063         CDialog::OnInitDialog();
00064 
00065         // TODO :  Ajoutez ici une initialisation supplémentaire
00066         KParser Parser( "Entities.def" );
00067         
00068         FINDTOKEN;
00069         while( TOKEN )
00070         {
00071                 if( Parser.IsEOF() )
00072                         break;
00073 
00074                 // ClassName
00075                 CEntityDef*     pEntityDef              = new CEntityDef();
00076                 pEntityDef->m_pClassName        = strdup( TOKEN );
00077                 m_ComboClassName.AddString( TOKEN );
00078 
00079                 // {
00080                 FINDTOKEN;
00081                 if( !CMPTOKEN( "{" ) )
00082                 {
00083                         KError::Error( GetSafeHwnd(), "CBrushUnitsDialog::OnInitDialog() : Entities File Corrupted on line %i\nToken %s found, '{' expected", Parser.GetCurrentLine(), TOKEN );
00084                         break;
00085                 }
00086 
00087                 // Key
00088                 FINDTOKEN;
00089                 while( !CMPTOKEN( "}" ) )
00090                 {
00091                         CEntityKey*     pEntityKey                      = new CEntityKey();
00092                         pEntityKey->m_pKey                              = strdup( TOKEN );
00093                         FINDTOKEN;
00094                         
00095                         pEntityKey->m_pValue                    = NULL;
00096                         if( TOKEN[0] == '\"' )
00097                         {
00098                                 // Chaine de caratere
00099                                 do
00100                                 {
00101                                         char*   pTemp = pEntityKey->m_pValue ? pEntityKey->m_pValue : strdup( "" );
00102                                         pEntityKey->m_pValue    = (char*)malloc( strlen( pTemp ) + strlen( TOKEN ) + 2 );
00103                                         strcpy( pEntityKey->m_pValue, pTemp );
00104                                         Freep( pTemp );
00105                                         strcat( pEntityKey->m_pValue, (TOKEN[0] == '\"') ? &TOKEN[1] : TOKEN );
00106 
00107                                         int l = (int)strlen( TOKEN );
00108                                         if( TOKEN[l - 1] == '\"' )
00109                                         {
00110                                                 pEntityKey->m_pValue[strlen( pEntityKey->m_pValue ) - 1] = '\0';
00111                                                 break;
00112                                         }
00113                                         strcat( pEntityKey->m_pValue, " " );
00114                                         FINDTOKEN;
00115                                 }while( !Parser.IsEOF() );
00116                         }
00117                         else
00118                                 pEntityKey->m_pValue            = strdup( TOKEN );
00119                         FINDTOKEN;
00120                         
00121 
00122                         pEntityDef->m_KeyList.Add( pEntityKey );
00123                 }
00124 
00125                 m_EntityDefList.Add( pEntityDef );
00126                 
00127                 FINDTOKEN;
00128         }
00129 
00130         m_pLandEntity = &m_NewLandEntity;
00131 
00132         m_ComboClassName.SetCurSel( 0 );
00133         m_ListEntity.InsertColumn( 0, "Key", LVCFMT_LEFT, 100 );
00134         m_ListEntity.InsertColumn( 1, "Value", LVCFMT_LEFT, 139 );
00135         m_ButtonCreate.SetCheck( BST_CHECKED );
00136         m_ListEntity.SetExtendedStyle( LVS_EX_FULLROWSELECT );
00137         m_EditValue.EnableWindow( FALSE );
00138         m_ButtonSet.EnableWindow( FALSE );
00139 
00140         OnCbnSelchangeComboClassname();
00141 
00142         return TRUE;  // return TRUE unless you set the focus to a control
00143         // EXCEPTION : les pages de propriétés OCX devraient retourner FALSE
00144 }
00145 
00146 void CBrushUnitsDialog::OnOK()
00147 {
00148         // TODO : ajoutez ici votre code spécialisé et/ou l'appel de la classe de base
00149 
00150         //CDialog::OnOK();
00151 }
00152 
00153 void CBrushUnitsDialog::OnCancel()
00154 {
00155         // TODO : ajoutez ici votre code spécialisé et/ou l'appel de la classe de base
00156 
00157         //CDialog::OnCancel();
00158 }
00159 
00160 void CBrushUnitsDialog::OnCbnSelchangeComboClassname()
00161 {
00162         // TODO : ajoutez ici le code de votre gestionnaire de notification de contrôle
00163         char    pText[1024];
00164 
00165         m_ComboClassName.GetLBText( m_ComboClassName.GetCurSel(), pText );
00166 
00167         m_pSelectedKey = NULL;
00168 
00169         for( CEntityDef* pEntityDef = m_EntityDefList.GetFirst(); pEntityDef; pEntityDef = m_EntityDefList.GetNext( pEntityDef ) )
00170         {
00171                 if( strcmp( pEntityDef->m_pClassName, pText ) == 0 )
00172                 {
00173                         // Creation de l'entité
00174                         CreateLandEntity( pText );
00175                         Refresh();
00176 /*
00177                         for( KLandEntityKey* pKey = m_NewLandEntity.GetFirst(); pKey; pKey = m_NewLandEntity.GetNext( pKey ) )
00178                         {
00179                                 int     i = m_ListEntity.GetItemCount();
00180                                 
00181                                 m_ListEntity.InsertItem( i, pKey->m_pKey );
00182                                 m_ListEntity.SetItemText( i, 1, pKey->m_pValue );
00183                                 m_ListEntity.SetItemData( i, (DWORD_PTR)pKey );
00184                         }*/
00185                 }
00186         }
00187 
00188 }
00189 
00190 void CBrushUnitsDialog::CreateLandEntity( char* pClassName )
00191 {
00192         // Recherche la définition de cette entité
00193         for( CEntityDef* pEntityDef = m_EntityDefList.GetFirst(); pEntityDef; pEntityDef = m_EntityDefList.GetNext( pEntityDef ) )
00194         {
00195                 if( strcmp( pEntityDef->m_pClassName, pClassName ) == 0 )
00196                 {
00197                         // Definit la definition d'entité pour l'entité courante
00198                         m_pEntityDef = pEntityDef;
00199 
00200                         m_NewLandEntity.Flush();
00201                         m_NewLandEntity.SetClassName( pClassName );
00202                                                 
00203                         for( CEntityKey* pEntityKey = pEntityDef->m_KeyList.GetFirst(); pEntityKey; pEntityKey = pEntityDef->m_KeyList.GetNext( pEntityKey ) )
00204                         {
00205                                 // Determine une valeur par defaut en fonction du type
00206                                 char    pDefaultValue[256];
00207                                 
00208                                 strcpy( pDefaultValue, "0" );
00209 
00210                                 // Int
00211                                 if( GetEntityType( pEntityKey->m_pKey ) == ET_INT )
00212                                         strcpy( pDefaultValue, "0" );
00213 
00214                                 // Float
00215                                 if( GetEntityType( pEntityKey->m_pKey ) == ET_FLOAT )
00216                                         strcpy( pDefaultValue, "0.0" );
00217 
00218                                 // String
00219                                 if( GetEntityType( pEntityKey->m_pKey ) == ET_STRING )
00220                                         strcpy( pDefaultValue, "" );
00221 
00222                                 // Vector 3D
00223                                 if( GetEntityType( pEntityKey->m_pKey ) == ET_VECTOR3D )
00224                                         strcpy( pDefaultValue, "0.0 0.0 0.0" );
00225 
00226                                 m_NewLandEntity.AddKey( pEntityKey->m_pValue, pDefaultValue );
00227                         }
00228                         break;
00229                 }
00230         }
00231 }
00232 
00233 void CBrushUnitsDialog::SetLandEntity( KLandEntity* pLandEntity )
00234 {
00235         if( pLandEntity )
00236         {
00237                 bool bEnable = m_ListEntity.GetSelectedCount() ? true : false;
00238                 m_pLandEntity = pLandEntity;
00239                 m_ComboClassName.EnableWindow( TRUE );
00240                 m_EditValue.EnableWindow( bEnable );
00241                 m_ButtonSet.EnableWindow( bEnable );
00242                 m_ListEntity.EnableWindow( TRUE );
00243 
00244                 // Flush
00245                 m_ListEntity.DeleteAllItems();
00246                 m_pSelectedKey = NULL;
00247 
00248                 // Cherche le bon ClassName
00249                 char    pText[1024];
00250                 for( int i = 0; i < m_ComboClassName.GetCount(); i ++ )
00251                 {
00252                         m_ComboClassName.GetLBText( i, pText );
00253 
00254                         if( strcmp( m_pLandEntity->GetClassName(), pText ) == 0 )
00255                         {
00256                                 m_ComboClassName.SetCurSel( i );
00257                                 break;
00258                         }
00259                 }
00260 
00261                 for( KLandEntityKey* pKey = m_pLandEntity->GetFirst(); pKey; pKey = m_pLandEntity->GetNext( pKey ) )
00262                 {
00263                         int     i = m_ListEntity.GetItemCount();
00264                         
00265                         m_ListEntity.InsertItem( i, pKey->m_pKey );
00266                         m_ListEntity.SetItemText( i, 1, pKey->m_pValue );
00267                         m_ListEntity.SetItemData( i, (DWORD_PTR)pKey );
00268                 }
00269 
00270         }
00271         else
00272         {
00273 //              m_LandEntity.Flush();
00274 //              m_ListEntity.DeleteAllItems();
00275 
00276                 m_ComboClassName.EnableWindow( FALSE );
00277                 m_EditValue.EnableWindow( FALSE );
00278                 m_ButtonSet.EnableWindow( FALSE );
00279                 m_ListEntity.EnableWindow( FALSE );
00280         }
00281 }
00282 
00283 
00284 ENTITYTYPE CBrushUnitsDialog::GetEntityType( char* pKeyType )
00285 {
00286         // Int
00287         if( !stricmp( pKeyType, "Int" ) )
00288                 return ET_INT;
00289 
00290         // Float
00291         if( !stricmp( pKeyType, "Float" ) )
00292                 return ET_FLOAT;
00293 
00294         // String
00295         if( !stricmp( pKeyType, "String" ) )
00296                 return ET_STRING;
00297 
00298         // Vector 3D
00299         if( !stricmp( pKeyType, "Vector3D" ) )
00300                 return ET_VECTOR3D;
00301 
00302         return ET_INT;
00303 }
00304 
00305 void CBrushUnitsDialog::OnBnClickedRadioCreate()
00306 {
00307         bool bEnable = m_ListEntity.GetSelectedCount() ? true : false;
00308         m_ListEntity.EnableWindow( TRUE );
00309         m_ComboClassName.EnableWindow( TRUE );
00310         m_EditValue.EnableWindow( bEnable );
00311         m_ButtonSet.EnableWindow( bEnable );
00312 
00313         m_pLandEntity = &m_NewLandEntity;
00314         Refresh();
00315 }
00316 
00317 void CBrushUnitsDialog::OnBnClickedRadioEdit()
00318 {
00319         m_ListEntity.EnableWindow( FALSE );
00320         m_ComboClassName.EnableWindow( FALSE );
00321         m_EditValue.EnableWindow( FALSE );
00322         m_ButtonSet.EnableWindow( FALSE );
00323 
00324         m_pLandEntity = NULL;
00325         Refresh();
00326 }
00327 
00328 void CBrushUnitsDialog::OnBnClickedRadioDelete2()
00329 {
00330         m_ListEntity.EnableWindow( FALSE );
00331         m_ComboClassName.EnableWindow( FALSE );
00332         m_EditValue.EnableWindow( FALSE );
00333         m_ButtonSet.EnableWindow( FALSE );
00334 
00335         m_pLandEntity = NULL;
00336         Refresh();
00337 }
00338 
00339 void CBrushUnitsDialog::OnNMClickListEntity(NMHDR *pNMHDR, LRESULT *pResult)
00340 {
00341         bool bEnable = m_ListEntity.GetSelectedCount() ? true : false;
00342         m_EditValue.EnableWindow( bEnable );
00343         m_ButtonSet.EnableWindow( bEnable );
00344 
00345         POSITION pos = m_ListEntity.GetFirstSelectedItemPosition();
00346         if( pos )
00347         {
00348                 int nItem = m_ListEntity.GetNextSelectedItem( pos );
00349                 m_pSelectedKey = (KLandEntityKey*)m_ListEntity.GetItemData( nItem );
00350                 m_EditValue.SetWindowText( m_pSelectedKey->m_pValue );
00351         }
00352 
00353         *pResult = 0;
00354 }
00355 
00356 void CBrushUnitsDialog::OnBnClickedButtonSet()
00357 {
00358         if( m_pSelectedKey )
00359         {
00360                 CString s;
00361                 m_EditValue.GetWindowText( s );
00362                 
00363                 Freep( m_pSelectedKey->m_pValue );
00364                 m_pSelectedKey->m_pValue = strdup( s );
00365 
00366                 //int nItem = 0;
00367                 //POSITION pos = m_ListEntity.GetFirstSelectedItemPosition();
00368                 //if( pos )
00369                 //      nItem = m_ListEntity.GetNextSelectedItem( pos );
00370 
00371                 Refresh();
00372 
00373                 //m_ListEntity.SetSelectionMark( nItem );
00374         }
00375 
00376         bool bEnable = m_ListEntity.GetSelectedCount() ? true : false;
00377         m_EditValue.EnableWindow( bEnable );
00378         m_ButtonSet.EnableWindow( bEnable );
00379 }
00380 
00381 void CBrushUnitsDialog::Refresh()
00382 {
00383         int     i = 0;
00384         
00385         m_ListEntity.DeleteAllItems();
00386         if( m_pLandEntity )
00387         {
00388                 for( KLandEntityKey* pKey = m_pLandEntity->GetFirst(); pKey; pKey = m_pLandEntity->GetNext( pKey ) )
00389                 {
00390                         m_ListEntity.InsertItem( i, pKey->m_pKey );
00391                         m_ListEntity.SetItemText( i, 1, pKey->m_pValue );
00392                         m_ListEntity.SetItemData( i, (DWORD_PTR)pKey );
00393                         i ++;
00394                 }
00395         }
00396 }

Generated on Sun Mar 25 20:02:12 2007 for Zythum Project by  doxygen 1.5.1-p1