00001 #include <conio.h>
00002
00003 #include "Common/Error.h"
00004 #include "Common/Console.h"
00005 #include "Common/Ini.h"
00006 #include "Common/Time.h"
00007 #include "Common/Directory.h"
00008
00009 #include "Render/Camera.h"
00010 #include "Render/Render.h"
00011 #include "Render/RenderDefs.h"
00012
00013 #include "Landscape/LandObject.h"
00014 #include "Landscape/LandEntity.h"
00015 #include "Landscape/LandscapeDraw.h"
00016
00017 #include "MapEditor/GameMapEditor.h"
00018
00019 #include "Input/InputDI.h"
00020
00021 #include "MapEditor/MapEditorInterface.h"
00022
00023
00024 KMapEditorInterface::KMapEditorInterface( HINSTANCE hInstance, HWND hWnd, u32 Adapter, u32 Width, u32 Height, u32 Bpp, u32 bFullScreen, u32 RefreshRate )
00025 : KInterface( hInstance, hWnd, Adapter, Width, Height, Bpp, bFullScreen, RefreshRate )
00026 {
00027 m_pLandscape = NULL;
00028 m_bFreeCamera = false;
00029 m_LastFrameTime = 0;
00030
00031 m_CamPos = KVector( 0.0f, 0.0f, 100.0f );
00032 m_CamRot = KVector( 0.0f, 0.0f, 0.0f );
00033 m_CamZoom = 0.0f;
00034
00035 m_pSelectedModel = NULL;
00036 m_sSelectedModelName= "";
00037 m_RotAngle = 0.0f;
00038
00039 m_bHighLight = false;
00040
00041 m_SunTime = 0.0f;
00042
00043 m_pModelBank = NULL;
00044
00045 m_pCamera->SetFarPlane( 1000.0f );
00046 }
00047
00048
00049 KMapEditorInterface::~KMapEditorInterface()
00050 {
00051 KLandModelObject* pLandModelObject;
00052
00053 while( pLandModelObject = m_LandModels.GetFirst() )
00054 {
00055 m_LandModels.Remove( pLandModelObject );
00056 Deletep( pLandModelObject );
00057 }
00058
00059 if( m_pSelectedModel )
00060 Deletep( m_pSelectedModel );
00061 }
00062
00063
00064 bool KMapEditorInterface::Init()
00065 {
00066 if( !KInterface::Init() )
00067 return false;
00068
00069 m_pModelBank = new KClientModelBank( GetpRender() );
00070
00071 GetpRender()->SetClear( true );
00072 GetpRender()->SetClearColor( KRGB( 128, 128, 255 ) );
00073
00074 m_pCamera->Translate( 0.0f, -700.0f, 300.0f );
00075
00076 return true;
00077 }
00078
00079
00080 void KMapEditorInterface::End()
00081 {
00082 if( m_pModelBank )
00083 Deletep( m_pModelBank );
00084
00085 KInterface::End();
00086 }
00087
00088
00089 void KMapEditorInterface::Display()
00090 {
00091 KInterface::Display();
00092
00093
00094 if( m_pLandscape )
00095 m_pLandscape->Draw( m_SunTime );
00096
00097
00098 KLandModelObject* pLandModelObject;
00099
00100 for( pLandModelObject = m_LandModels.GetFirst(); pLandModelObject; pLandModelObject = m_LandModels.GetNext( pLandModelObject ) )
00101 {
00102 m_pRender->SetWorldMatrix( pLandModelObject->m_WorldMatrix );
00103
00104 KModelDraw* pModel = (KModelDraw*)m_pModelBank->GetpModel( pLandModelObject->m_hModel );
00105 if( pModel )
00106 pModel->Draw();
00107 }
00108
00109
00110 if( m_pSelectedModel )
00111 {
00112 KMatrix Matrix;
00113
00114 Matrix.LoadIdentity();
00115 Matrix.RotZ( m_RotAngle );
00116 Matrix.Trans( m_pLandscape->GetBrushVector().x, m_pLandscape->GetBrushVector().y, m_pLandscape->GetBrushVector().z );
00117 m_pRender->SetWorldMatrix( Matrix );
00118 m_pSelectedModel->Draw();
00119 }
00120 }
00121
00122
00123 void KMapEditorInterface::SetCameraPos( float x, float y, float z )
00124 {
00125 m_CamPos.x = x;
00126 m_CamPos.y = y;
00127 m_CamPos.z = z;
00128
00129 GetpRender()->GetCamera()->SetPos( x, y, z );
00130 }
00131
00132
00133 void KMapEditorInterface::MoveFrame()
00134 {
00135 KCamera* pCamera = GetpRender()->GetCamera();
00136 KASSERT( pCamera );
00137
00138 float TransVelocity = 1.0f;
00139 float RotVelocity = 0.01f;
00140
00141 if( m_LastFrameTime != 0 )
00142 {
00143 TransVelocity *= ((float)(g_Time.GetTime() - m_LastFrameTime) / 10.0f);
00144 RotVelocity *= ((float)(g_Time.GetTime() - m_LastFrameTime) / 10.0f);
00145 }
00146
00147 m_LastFrameTime = g_Time.GetTime();
00148
00149 if( m_bFreeCamera )
00150 {
00151
00152 if( GetAsyncKeyState( 'Z' ) ) pCamera->Translate( 0, TransVelocity, 0 );
00153 if( GetAsyncKeyState( 'S' ) ) pCamera->Translate( 0, -TransVelocity, 0 );
00154 if( GetAsyncKeyState( 'Q' ) ) pCamera->Translate( -TransVelocity, 0, 0 );
00155 if( GetAsyncKeyState( 'D' ) ) pCamera->Translate( TransVelocity, 0, 0 );
00156 if( GetAsyncKeyState( VK_NUMPAD8 ) ) pCamera->RotateX( -RotVelocity );
00157 if( GetAsyncKeyState( VK_NUMPAD5 ) ) pCamera->RotateX( RotVelocity );
00158 if( GetAsyncKeyState( VK_NUMPAD4 ) ) pCamera->RotateZ( -RotVelocity );
00159 if( GetAsyncKeyState( VK_NUMPAD6 ) ) pCamera->RotateZ( RotVelocity );
00160 if( GetAsyncKeyState( VK_NUMPAD1 ) ) pCamera->Translate( 0, 0, TransVelocity );
00161 if( GetAsyncKeyState( VK_NUMPAD0 ) ) pCamera->Translate( 0, 0, -TransVelocity );
00162 if( GetAsyncKeyState( VK_HOME ) ) pCamera->Reset();
00163
00164 KInputMouseState MouseState;
00165
00166 if( m_pInput )
00167 {
00168 m_pInput->GetMouseState( &MouseState );
00169
00170 if( MouseState.m_X )
00171 pCamera->RotateZ( (MouseState.m_X / 2.0f) * RotVelocity );
00172
00173 if( MouseState.m_Y )
00174 pCamera->RotateX( -(MouseState.m_Y / 2.0f) * RotVelocity );
00175 }
00176 }
00177 else
00178 {
00179
00180 if( GetAsyncKeyState( 'Z' ) ) m_CamPos.y += TransVelocity;
00181 if( GetAsyncKeyState( 'S' ) ) m_CamPos.y -= TransVelocity;
00182 if( GetAsyncKeyState( 'Q' ) ) m_CamPos.x -= TransVelocity;
00183 if( GetAsyncKeyState( 'D' ) ) m_CamPos.x += TransVelocity;
00184 if( GetAsyncKeyState( 'A' ) ) m_CamZoom -= TransVelocity;
00185 if( GetAsyncKeyState( 'E' ) ) m_CamZoom += TransVelocity;
00186
00187 m_CamRot.x = -(PI/180.0f) * 60.0f;
00188
00189 pCamera->Reset();
00190 pCamera->Translate( m_CamPos.x, m_CamPos.y, m_CamPos.z );
00191 pCamera->RotateX( m_CamRot.x );
00192 pCamera->RotateY( m_CamRot.y );
00193 pCamera->RotateZ( m_CamRot.z );
00194 pCamera->Translate( 0.0f, m_CamZoom, 0.0f );
00195 }
00196 }
00197
00198
00199 void KMapEditorInterface::TranslateCamera( float x, float y, float z )
00200 {
00201 KCamera* pCamera = GetpRender()->GetCamera();
00202 KASSERT( pCamera );
00203
00204 pCamera->Translate( x, y, z );
00205 }
00206
00207
00208 void KMapEditorInterface::RotateCamera( float x, float y, float z )
00209 {
00210 KCamera* pCamera = GetpRender()->GetCamera();
00211 KASSERT( pCamera );
00212
00213 if( x != 0.0f )
00214 pCamera->RotateX( x );
00215 if( y != 0.0f )
00216 pCamera->RotateY( y );
00217 if( z != 0.0f )
00218 pCamera->RotateZ( z );
00219 }
00220
00221
00222 bool KMapEditorInterface::LoadLandscape( char* pFileName )
00223 {
00224 KASSERT( !m_pLandscape );
00225
00226 KLandModelObject* pLandModelObject;
00227
00228 m_pLandscape = new KLandscapeDraw( m_pRender );
00229
00230 if( !m_pLandscape->LoadLandscape( pFileName ) )
00231 {
00232 Deletep( m_pLandscape );
00233 return false;
00234 }
00235
00236
00237 KLandObject* pLandObject;
00238 for( pLandObject = m_pLandscape->GetFirstLandObject(); pLandObject; pLandObject = m_pLandscape->GetNextLandObject( pLandObject ) )
00239 {
00240 char pDirectory[1024];
00241
00242 strcpy( pDirectory, "Objects/" );
00243 strcat( pDirectory, pLandObject->GetpObjectName() );
00244
00245
00246 for( u32 i = 0; i < strlen( pDirectory ); i ++ )
00247 {
00248 if( pDirectory[i] == '\\' )
00249 pDirectory[i] = '/';
00250 }
00251
00252
00253 KMODEL hModel = m_pModelBank->LoadModel( KStr( pDirectory ) );
00254 if( hModel == KMODEL_NO )
00255 return NULL;
00256
00257
00258 pLandModelObject = new KLandModelObject();
00259 pLandModelObject->m_hModel = hModel;
00260 pLandModelObject->m_pLandEntity = NULL;
00261 pLandModelObject->m_pLandObject = pLandObject;
00262 pLandModelObject->m_WorldMatrix = pLandObject->GetMatrix();
00263 pLandModelObject->m_sModelName = pLandObject->GetpObjectName();
00264 m_LandModels.Add( pLandModelObject );
00265 }
00266
00267
00268 KLandEntity* pLandEntity;
00269 for( pLandEntity = m_pLandscape->GetFirstLandEntity(); pLandEntity; pLandEntity = m_pLandscape->GetNextLandEntity( pLandEntity ) )
00270 {
00271 char pDirectory[1024];
00272
00273 strcpy( pDirectory, "Objects/Entity/Entity" );
00274
00275
00276 for( u32 i = 0; i < strlen( pDirectory ); i ++ )
00277 {
00278 if( pDirectory[i] == '\\' )
00279 pDirectory[i] = '/';
00280 }
00281
00282
00283 KMODEL hModel = m_pModelBank->LoadModel( KStr( pDirectory ) );
00284 if( hModel == KMODEL_NO )
00285 return NULL;
00286
00287
00288 pLandModelObject = new KLandModelObject();
00289 pLandModelObject->m_hModel = hModel;
00290 pLandModelObject->m_pLandEntity = pLandEntity;
00291 pLandModelObject->m_pLandObject = NULL;
00292 pLandModelObject->m_WorldMatrix = pLandEntity->GetMatrix();
00293 pLandModelObject->m_sModelName = KStr( "Entity" );
00294 m_LandModels.Add( pLandModelObject );
00295 }
00296
00297 m_pLandscape->DrawSky( false );
00298 m_pLandscape->DrawMiniMap( false );
00299
00300 return true;
00301 }
00302
00303
00304 bool KMapEditorInterface::SaveLandscape( char* pFileName )
00305 {
00306 KASSERT( m_pLandscape );
00307
00308 if( !m_pLandscape->SaveLandscape( pFileName ) )
00309 return false;
00310
00311 return true;
00312 }
00313
00314
00315 bool KMapEditorInterface::CloseLandscape()
00316 {
00317 if( m_pLandscape)
00318 Deletep( m_pLandscape );
00319
00320 return true;
00321 }
00322
00323
00324 bool KMapEditorInterface::NewLandscape( s32 Width, s32 Height )
00325 {
00326 KASSERT( !m_pLandscape );
00327
00328 m_pLandscape = new KLandscapeDraw( m_pRender );
00329
00330 if( !m_pLandscape->NewLandscape( Width, Height ) )
00331 {
00332 Deletep( m_pLandscape );
00333 return false;
00334 }
00335
00336 m_pLandscape->DrawSky( false );
00337 m_pLandscape->DrawMiniMap( false );
00338
00339 return true;
00340 }
00341
00342
00343 void KMapEditorInterface::SetBrush( s32 x, s32 y, s32 Size )
00344 {
00345 m_pLandscape->SetBrush( x, y, Size );
00346 }
00347
00348
00349 void KMapEditorInterface::ChangeLevel( s32 Height )
00350 {
00351 m_pLandscape->ChangeLevel( Height );
00352 }
00353
00354
00355 void KMapEditorInterface::ChangeTexture( char* pTextureName )
00356 {
00357 m_pLandscape->ChangeTexture( pTextureName );
00358 }
00359
00360
00361 void KMapEditorInterface::RotateTexture( float Angle )
00362 {
00363 m_pLandscape->RotateTexture( Angle );
00364 }
00365
00366
00367 KLandModelObject* KMapEditorInterface::AddModel( char* pModelName )
00368 {
00369 KMatrix Matrix;
00370 char pDirectory[1024];
00371
00372 strcpy( pDirectory, "Objects/" );
00373 strcat( pDirectory, pModelName );
00374
00375
00376 for( u32 i = 0; i < strlen( pDirectory ); i ++ )
00377 {
00378 if( pDirectory[i] == '\\' )
00379 pDirectory[i] = '/';
00380 }
00381
00382
00383 KMODEL hModel = m_pModelBank->LoadModel( KStr( pDirectory ) );
00384 if( hModel == KMODEL_NO )
00385 return NULL;
00386
00387 Matrix.LoadIdentity();
00388 Matrix.RotZ( m_RotAngle );
00389 Matrix.Trans( m_pLandscape->GetBrushVector().x, m_pLandscape->GetBrushVector().y, m_pLandscape->GetBrushVector().z );
00390
00391
00392 KLandObject* pLandObject;
00393 pLandObject = new KLandObject();
00394 pLandObject->SetMatrix( Matrix );
00395 pLandObject->SetpObjectName( pModelName );
00396 m_pLandscape->AddLandObject( pLandObject );
00397
00398
00399 KLandModelObject* pLandModelObject;
00400 pLandModelObject = new KLandModelObject();
00401 pLandModelObject->m_hModel = hModel;
00402 pLandModelObject->m_WorldMatrix = Matrix;
00403 pLandModelObject->m_pLandObject = pLandObject;
00404 pLandModelObject->m_pLandEntity = NULL;
00405 pLandModelObject->m_sModelName = KStr( pModelName );
00406 m_LandModels.Add( pLandModelObject );
00407
00408 return pLandModelObject;
00409 }
00410
00411
00412 void KMapEditorInterface::DelModel( KLandModelObject& LandModelObject )
00413 {
00414
00415 float NearDistance = 100.0f;
00416 float Distance;
00417 KLandModelObject* pLandModelObject;
00418 KLandModelObject* pNearLandModelObject = NULL;
00419 KStr sEntity = KStr( "Entity" );
00420
00421 for( pLandModelObject = m_LandModels.GetFirst(); pLandModelObject; pLandModelObject = m_LandModels.GetNext( pLandModelObject ) )
00422 {
00423
00424 if( pLandModelObject->m_sModelName == sEntity )
00425 continue;
00426
00427 Distance = KVector::Distance( m_pLandscape->GetBrushVector(), KVector( pLandModelObject->m_WorldMatrix._41, pLandModelObject->m_WorldMatrix._42, pLandModelObject->m_WorldMatrix._43 ) );
00428
00429 if( Distance < NearDistance )
00430 {
00431 NearDistance = Distance;
00432 pNearLandModelObject = pLandModelObject;
00433 }
00434 }
00435
00436
00437 if( pNearLandModelObject )
00438 {
00439 LandModelObject.m_hModel = KMODEL_NO;
00440 LandModelObject.m_sModelName = pNearLandModelObject->m_sModelName;
00441 LandModelObject.m_WorldMatrix = pNearLandModelObject->m_WorldMatrix;
00442
00443
00444 m_pLandscape->RemoveLandObject( pNearLandModelObject->m_pLandObject );
00445 Deletep( pNearLandModelObject->m_pLandObject );
00446
00447
00448 m_LandModels.Remove( pNearLandModelObject );
00449 Deletep( pNearLandModelObject );
00450 }
00451 }
00452
00453
00454 void KMapEditorInterface::SetSelectedModel( KStr& sModelName )
00455 {
00456 if( m_sSelectedModelName == sModelName )
00457 return;
00458
00459 if( m_pSelectedModel )
00460 Deletep( m_pSelectedModel );
00461
00462 if( sModelName == KStr( "" ) )
00463 {
00464 m_sSelectedModelName = sModelName;
00465 return;
00466 }
00467
00468 KStr sDirectory = g_Directory.GetPath( KStr( "Models" ) );
00469 sDirectory += "Objects/";
00470 sDirectory += sModelName;
00471
00472
00473 for( u32 i = 0; i < sDirectory.GetLength(); i ++ )
00474 {
00475 if( sDirectory[i] == '\\' )
00476 sDirectory[i] = '/';
00477 }
00478
00479 m_sSelectedModelName = sModelName;
00480
00481
00482 m_pSelectedModel = new KModelDraw( m_pRender );
00483 if( !m_pSelectedModel->LoadModel( sDirectory ) )
00484 {
00485 Deletep( m_pSelectedModel );
00486 return;
00487 }
00488 }
00489
00490
00491 void KMapEditorInterface::SetRotModel( float Angle )
00492 {
00493 m_RotAngle = Angle;
00494 }
00495
00496
00497 KLandModelObject* KMapEditorInterface::GetNearLandModelObject( bool bEntity )
00498 {
00499
00500 float NearDistance = 100.0f;
00501 float Distance;
00502 KStr sEntity = KStr( "Entity" );
00503 KLandModelObject* pNearModelObject = NULL;
00504
00505 for( KLandModelObject* pLandModelObject = m_LandModels.GetFirst(); pLandModelObject; pLandModelObject = m_LandModels.GetNext( pLandModelObject ) )
00506 {
00507
00508 if( ( bEntity && ( pLandModelObject->m_sModelName != sEntity ) ) || ( !bEntity && ( pLandModelObject->m_sModelName == sEntity ) ) )
00509 continue;
00510
00511 Distance = KVector::Distance( m_pLandscape->GetBrushVector(), KVector( pLandModelObject->m_WorldMatrix._41, pLandModelObject->m_WorldMatrix._42, pLandModelObject->m_WorldMatrix._43 ) );
00512
00513 if( Distance < NearDistance )
00514 {
00515 NearDistance = Distance;
00516 pNearModelObject = pLandModelObject;
00517 }
00518 }
00519
00520 return pNearModelObject;
00521 }
00522
00523
00524 void KMapEditorInterface::HighLightNearModel( bool bHighLight, bool bEntity )
00525 {
00526 if( bHighLight )
00527 {
00528 KLandModelObject* pLandModelObject;
00529 KMDLMESHRENDEREFFECT Effect;
00530
00531
00532 KLandModelObject* pNearLandModelObject = GetNearLandModelObject( bEntity );
00533
00534 for( pLandModelObject = m_LandModels.GetFirst(); pLandModelObject; pLandModelObject = m_LandModels.GetNext( pLandModelObject ) )
00535 {
00536 if( pNearLandModelObject == pLandModelObject )
00537 Effect = KMMRE_NORMALWIREFRAME;
00538 else
00539 Effect = KMMRE_NORMAL;
00540
00541 KModelDraw* pModel = (KModelDraw*)m_pModelBank->GetpModel( pLandModelObject->m_hModel );
00542 for( u32 m = 0; m < pModel->GetnMeshes(); m ++ )
00543 pModel->GetpMesh( m )->m_RenderEffect = Effect;
00544 }
00545
00546 m_bHighLight = true;
00547 }
00548 else
00549 {
00550 if( !m_bHighLight )
00551 return;
00552
00553
00554 KLandModelObject* pLandModelObject;
00555
00556 for( pLandModelObject = m_LandModels.GetFirst(); pLandModelObject; pLandModelObject = m_LandModels.GetNext( pLandModelObject ) )
00557 {
00558 KModelDraw* pModel = (KModelDraw*)m_pModelBank->GetpModel( pLandModelObject->m_hModel );
00559
00560 for( u32 m = 0; m < pModel->GetnMeshes(); m ++ )
00561 pModel->GetpMesh( m )->m_RenderEffect = KMMRE_NORMAL;
00562 }
00563
00564 m_bHighLight = false;
00565 }
00566 }
00567
00568
00569
00570
00571 void KMapEditorInterface::AddEntity( KLandEntity* pEntity )
00572 {
00573 KASSERT( m_pLandscape );
00574 m_pLandscape->AddLandEntity( pEntity );
00575 }
00576
00577
00578 void KMapEditorInterface::RemoveEntity( KLandEntity* pEntity )
00579 {
00580 KASSERT( m_pLandscape );
00581 m_pLandscape->RemoveLandEntity( pEntity );
00582 }
00583
00584
00585 KLandEntity* KMapEditorInterface::GetFirstEntity()
00586 {
00587 KASSERT( m_pLandscape );
00588 return m_pLandscape->GetFirstLandEntity();
00589 }
00590
00591
00592 KLandEntity* KMapEditorInterface::GetNextEntity( KLandEntity* pEntity )
00593 {
00594 KASSERT( m_pLandscape );
00595 return m_pLandscape->GetNextLandEntity( pEntity );
00596 }
00597
00598
00599 void KMapEditorInterface::AddLandEntity( KLandEntity& LandEntity )
00600 {
00601 KMatrix Matrix;
00602 KLandEntity* pLandEntity = new KLandEntity();
00603
00604 *pLandEntity = LandEntity;
00605
00606 Matrix.LoadIdentity();
00607 Matrix.RotZ( m_RotAngle );
00608 Matrix.Trans( m_pLandscape->GetBrushVector().x, m_pLandscape->GetBrushVector().y, m_pLandscape->GetBrushVector().z );
00609
00610 pLandEntity->SetMatrix( Matrix );
00611 AddEntity( pLandEntity );
00612
00613 KMODEL hModel = m_pModelBank->LoadModel( KStr( "Objects/Entity/Entity" ) );
00614
00615
00616 KLandModelObject* pLandModelObject;
00617
00618 pLandModelObject = new KLandModelObject();
00619 pLandModelObject->m_hModel = hModel;
00620 pLandModelObject->m_WorldMatrix = Matrix;
00621 pLandModelObject->m_pLandObject = NULL;
00622 pLandModelObject->m_pLandEntity = pLandEntity;
00623 pLandModelObject->m_sModelName = KStr( "Entity" );
00624 m_LandModels.Add( pLandModelObject );
00625 }
00626
00627
00628 void KMapEditorInterface::DelLandEntity()
00629 {
00630
00631 KLandModelObject* pNearLandModelObject = GetNearLandModelObject( true );
00632
00633
00634 if( pNearLandModelObject )
00635 {
00636
00637 RemoveEntity( pNearLandModelObject->m_pLandEntity );
00638 Deletep( pNearLandModelObject->m_pLandEntity );
00639
00640
00641 m_LandModels.Remove( pNearLandModelObject );
00642 Deletep( pNearLandModelObject );
00643 }
00644 }
00645
00646
00647 KLandEntity* KMapEditorInterface::GetSelectedLandEntity()
00648 {
00649
00650 KLandModelObject* pNearLandModelObject = GetNearLandModelObject( true );
00651
00652 if( pNearLandModelObject )
00653 return pNearLandModelObject->m_pLandEntity;
00654
00655 return NULL;
00656 }
00657
00658
00659 void KMapEditorInterface::SetSelectedLandEntity( KLandEntity& LandEntity )
00660 {
00661 }