📄 dxutgui.cpp
字号:
DrawSprite9( &m_CapElement, &rc );
rc.left += 5; // Make a left margin
WCHAR wszOutput[256];
StringCchCopy( wszOutput, 256, m_wszCaption );
if( m_bMinimized )
StringCchCat( wszOutput, 256, L" (Minimized)" );
DrawText9( wszOutput, &m_CapElement, &rc, true );
}
// If the dialog is minimized, skip rendering
// its controls.
if( !m_bMinimized )
{
for( int i=0; i < m_Controls.GetSize(); i++ )
{
CDXUTControl* pControl = m_Controls.GetAt(i);
// Focused control is drawn last
if( pControl == s_pControlFocus )
continue;
pControl->Render( fElapsedTime );
}
if( s_pControlFocus != NULL && s_pControlFocus->m_pDialog == this )
s_pControlFocus->Render( fElapsedTime );
}
m_pManager->m_pSprite->End();
m_pManager->m_pStateBlock->Apply();
return S_OK;
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTDialog::OnRender10( float fElapsedTime )
{
// If this assert triggers, you need to call CDXUTDialogResourceManager::On*Device() from inside
// the application's device callbacks. See the SDK samples for an example of how to do this.
assert( m_pManager->GetD3D10Device() && L"To fix hook up CDXUTDialogResourceManager to device callbacks. See comments for details" );
// See if the dialog needs to be refreshed
if( m_fTimeLastRefresh < s_fTimeRefresh )
{
m_fTimeLastRefresh = DXUTGetTime();
Refresh();
}
// For invisible dialog, out now.
if( !m_bVisible ||
( m_bMinimized && !m_bCaption ) )
return S_OK;
ID3D10Device* pd3dDevice = m_pManager->GetD3D10Device();
// Set up a state block here and restore it when finished drawing all the controls
m_pManager->m_pStateBlock10->Capture();
BOOL bBackgroundIsVisible = ( m_colorTopLeft | m_colorTopRight | m_colorBottomRight | m_colorBottomLeft ) & 0xff000000;
if( !m_bMinimized && bBackgroundIsVisible )
{
// Convert the draw rectangle from screen coordinates to clip space coordinates.
float Left, Right, Top, Bottom;
Left = m_x * 2.0f / m_pManager->m_nBackBufferWidth - 1.0f;
Right = ( m_x + m_width ) * 2.0f / m_pManager->m_nBackBufferWidth - 1.0f;
Top = 1.0f - m_y * 2.0f / m_pManager->m_nBackBufferHeight;
Bottom = 1.0f - ( m_y + m_height ) * 2.0f / m_pManager->m_nBackBufferHeight;
DXUT_SCREEN_VERTEX_10 vertices[4] =
{
Left, Top, 0.5f, D3DCOLOR_TO_D3DCOLORVALUE( m_colorTopLeft ), 0.0f, 0.0f,
Right, Top, 0.5f, D3DCOLOR_TO_D3DCOLORVALUE( m_colorTopRight ), 1.0f, 0.0f,
Left, Bottom, 0.5f, D3DCOLOR_TO_D3DCOLORVALUE( m_colorBottomLeft ), 0.0f, 1.0f,
Right, Bottom, 0.5f, D3DCOLOR_TO_D3DCOLORVALUE( m_colorBottomRight ), 1.0f, 1.0f,
};
DXUT_SCREEN_VERTEX_10 *pVB;
if( SUCCEEDED( m_pManager->m_pVBScreenQuad10->Map( D3D10_MAP_WRITE_DISCARD, 0, (LPVOID*)&pVB ) ) )
{
CopyMemory( pVB, vertices, sizeof( vertices ) );
m_pManager->m_pVBScreenQuad10->Unmap();
}
// Set the quad VB as current
UINT stride = sizeof( DXUT_SCREEN_VERTEX_10 );
UINT offset = 0;
pd3dDevice->IASetVertexBuffers( 0, 1, &m_pManager->m_pVBScreenQuad10, &stride, &offset );
pd3dDevice->IASetInputLayout( m_pManager->m_pInputLayout10 );
pd3dDevice->IASetPrimitiveTopology( D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP );
// Draw dialog background
D3D10_TECHNIQUE_DESC techDesc;
m_pManager->m_pTechRenderUIUntex10->GetDesc( &techDesc );
for( UINT p = 0; p < techDesc.Passes; ++p )
{
m_pManager->m_pTechRenderUIUntex10->GetPassByIndex( p )->Apply(0);
pd3dDevice->Draw( 4, 0 );
}
}
DXUTTextureNode* pTextureNode = GetTexture( 0 );
m_pManager->m_pFxTexture10->SetResource( pTextureNode->pTexResView );
// Sort depth back to front
m_pManager->m_pSprite10->Begin( 0 );
m_pManager->m_pTechRenderUI10->GetPassByIndex( 0 )->Apply(0);
// Render the caption if it's enabled.
if( m_bCaption )
{
// DrawSprite will offset the rect down by
// m_nCaptionHeight, so adjust the rect higher
// here to negate the effect.
RECT rc = { 0, -m_nCaptionHeight, m_width, 0 };
DrawSprite10( &m_CapElement, &rc, 0.99f );
rc.left += 5; // Make a left margin
WCHAR wszOutput[256];
StringCchCopy( wszOutput, 256, m_wszCaption );
if( m_bMinimized )
StringCchCat( wszOutput, 256, L" (Minimized)" );
DrawText10( wszOutput, &m_CapElement, &rc, true );
}
// If the dialog is minimized, skip rendering
// its controls.
if( !m_bMinimized )
{
for( int i=0; i < m_Controls.GetSize(); i++ )
{
CDXUTControl* pControl = m_Controls.GetAt(i);
// Focused control is drawn last
if( pControl == s_pControlFocus )
continue;
pControl->Render( fElapsedTime );
}
if( s_pControlFocus != NULL && s_pControlFocus->m_pDialog == this )
s_pControlFocus->Render( fElapsedTime );
}
m_pManager->m_pSprite10->End();
m_pManager->m_pStateBlock10->Apply();
// Restore depth stencil state
m_pManager->m_pEffect10->GetTechniqueByName("RestoreState")->GetPassByIndex( 0 )->Apply(0);
return S_OK;
}
//--------------------------------------------------------------------------------------
VOID CDXUTDialog::SendEvent( UINT nEvent, bool bTriggeredByUser, CDXUTControl* pControl )
{
// If no callback has been registered there's nowhere to send the event to
if( m_pCallbackEvent == NULL )
return;
// Discard events triggered programatically if these types of events haven't been
// enabled
if( !bTriggeredByUser && !m_bNonUserEvents )
return;
m_pCallbackEvent( nEvent, pControl->GetID(), pControl, m_pCallbackEventUserContext );
}
//--------------------------------------------------------------------------------------
int CDXUTDialogResourceManager::AddFont( LPCWSTR strFaceName, LONG height, LONG weight )
{
// See if this font already exists
for( int i=0; i < m_FontCache.GetSize(); i++ )
{
DXUTFontNode* pFontNode = m_FontCache.GetAt(i);
size_t nLen = 0;
StringCchLength( strFaceName, MAX_PATH, &nLen );
if( 0 == _wcsnicmp( pFontNode->strFace, strFaceName, nLen ) &&
pFontNode->nHeight == height &&
pFontNode->nWeight == weight )
{
return i;
}
}
// Add a new font and try to create it
DXUTFontNode* pNewFontNode = new DXUTFontNode();
if( pNewFontNode == NULL )
return -1;
ZeroMemory( pNewFontNode, sizeof(DXUTFontNode) );
StringCchCopy( pNewFontNode->strFace, MAX_PATH, strFaceName );
pNewFontNode->nHeight = height;
pNewFontNode->nWeight = weight;
m_FontCache.Add( pNewFontNode );
int iFont = m_FontCache.GetSize()-1;
// If a device is available, try to create immediately
if( m_pd3d9Device )
CreateFont9( iFont );
if( m_pd3d10Device )
CreateFont10( iFont );
return iFont;
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTDialog::SetFont( UINT index, LPCWSTR strFaceName, LONG height, LONG weight )
{
// If this assert triggers, you need to call CDXUTDialog::Init() first. This change
// was made so that the DXUT's GUI could become seperate and optional from DXUT's core. The
// creation and interfacing with CDXUTDialogResourceManager is now the responsibility
// of the application if it wishes to use DXUT's GUI.
assert( m_pManager != NULL && L"To fix call CDXUTDialog::Init() first. See comments for details." );
// Make sure the list is at least as large as the index being set
UINT i;
for( i=m_Fonts.GetSize(); i <= index; i++ )
{
m_Fonts.Add( -1 );
}
int iFont = m_pManager->AddFont( strFaceName, height, weight );
m_Fonts.SetAt( index, iFont );
return S_OK;
}
//--------------------------------------------------------------------------------------
DXUTFontNode* CDXUTDialog::GetFont( UINT index )
{
if( NULL == m_pManager )
return NULL;
return m_pManager->GetFontNode( m_Fonts.GetAt( index ) );
}
//--------------------------------------------------------------------------------------
int CDXUTDialogResourceManager::AddTexture( LPCWSTR strFilename )
{
// See if this texture already exists
for( int i=0; i < m_TextureCache.GetSize(); i++ )
{
DXUTTextureNode* pTextureNode = m_TextureCache.GetAt(i);
size_t nLen = 0;
StringCchLength( strFilename, MAX_PATH, &nLen );
if( pTextureNode->bFileSource && // Sources must match
0 == _wcsnicmp( pTextureNode->strFilename, strFilename, nLen ) )
{
return i;
}
}
// Add a new texture and try to create it
DXUTTextureNode* pNewTextureNode = new DXUTTextureNode();
if( pNewTextureNode == NULL )
return -1;
ZeroMemory( pNewTextureNode, sizeof(DXUTTextureNode) );
pNewTextureNode->bFileSource = true;
StringCchCopy( pNewTextureNode->strFilename, MAX_PATH, strFilename );
m_TextureCache.Add( pNewTextureNode );
int iTexture = m_TextureCache.GetSize()-1;
// If a device is available, try to create immediately
if( m_pd3d9Device )
CreateTexture9( iTexture );
return iTexture;
}
//--------------------------------------------------------------------------------------
int CDXUTDialogResourceManager::AddTexture( LPCWSTR strResourceName, HMODULE hResourceModule )
{
// See if this texture already exists
for( int i=0; i < m_TextureCache.GetSize(); i++ )
{
DXUTTextureNode* pTextureNode = m_TextureCache.GetAt(i);
if( !pTextureNode->bFileSource && // Sources must match
pTextureNode->hResourceModule == hResourceModule ) // Module handles must match
{
if( IS_INTRESOURCE( strResourceName ) )
{
// Integer-based ID
if( (INT_PTR)strResourceName == pTextureNode->nResourceID )
return i;
}
else
{
// String-based ID
size_t nLen = 0;
StringCchLength( strResourceName, MAX_PATH, &nLen );
if( 0 == _wcsnicmp( pTextureNode->strFilename, strResourceName, nLen ) )
return i;
}
}
}
// Add a new texture and try to create it
DXUTTextureNode* pNewTextureNode = new DXUTTextureNode();
if( pNewTextureNode == NULL )
return -1;
ZeroMemory( pNewTextureNode, sizeof(DXUTTextureNode) );
pNewTextureNode->hResourceModule = hResourceModule;
if( IS_INTRESOURCE( strResourceName ) )
{
pNewTextureNode->nResourceID = (int)(size_t)strResourceName;
}
else
{
pNewTextureNode->nResourceID = 0;
StringCchCopy( pNewTextureNode->strFilename, MAX_PATH, strResourceName );
}
m_TextureCache.Add( pNewTextureNode );
int iTexture = m_TextureCache.GetSize()-1;
// If a device is available, try to create immediately
if( m_pd3d9Device )
CreateTexture9( iTexture );
return iTexture;
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTDialog::SetTexture( UINT index, LPCWSTR strFilename )
{
// If this assert triggers, you need to call CDXUTDialog::Init() first. This change
// was made so that the DXUT's GUI could become seperate and optional from DXUT's core. The
// creation and interfacing with CDXUTDialogResourceManager is now the responsibility
// of the application if it wishes to use DXUT's GUI.
assert( m_pManager != NULL && L"To fix this, call CDXUTDialog::Init() first. See comments for details." );
// Make sure the list is at least as large as the index being set
for( UINT i=m_Textures.GetSize(); i <= index; i++ )
{
m_Textures.Add( -1 );
}
int iTexture = m_pManager->AddTexture( strFilename );
m_Textures.SetAt( index, iTexture );
return S_OK;
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTDialog::SetTexture( UINT index, LPCWSTR strResourceName, HMODULE hResourceModule )
{
// If this assert triggers, you need to call CDXUTDialog::Init() first. This change
// was made so that the DXUT's GUI could become seperate and optional from DXUT's core. The
// creation and interfacing with CDXUTDialogResourceManager is now the responsibility
// of the application if it wishes to use DXUT's GUI.
assert( m_pManager != NULL && L"To fix this, call CDXUTDialog::Init() first. See comments for details." );
// Make sure the list is at least as large as the index being set
for( UINT i=m_Textures.GetSize(); i <= index; i++ )
{
m_Textures.Add( -1 );
}
int iTexture = m_pManager->AddTexture( strResourceName, hResourceModule );
m_Textures.SetAt( index, iTexture );
return S_OK;
}
//--------------------------------------------------------------------------------------
DXUTTextureNode* CDXUTDialog::GetTexture( UINT index )
{
if( NULL == m_pManager )
return NULL;
return m_pManager->GetTextureNode( m_Textures.GetAt( index ) );
}
//--------------------------------------------------------------------------------------
bool CDXUTDialog::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
bool bHandled = false;
// For invisible dialog, do not handle anything.
if( !m_bVisible )
return false;
// If automation command-line switch is on, enable this dialog's keyboard input
// upon any key press or mouse click.
if( DXUTGetAutomation() &&
( WM_LBUTTONDOWN == uMsg || WM_LBUTTONDBLCLK == uMsg || WM_KEYDOWN == uMsg ) )
{
m_pManager->EnableKeyboardInputForAllDialogs();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -