diff options
| author | Vishal More <[email protected]> | 2020-06-11 11:11:17 +0530 |
|---|---|---|
| committer | Vishal More <[email protected]> | 2020-06-11 11:11:17 +0530 |
| commit | bbe353230727568d3c1999af2701d2e150ff232f (patch) | |
| tree | 4b75466fc0f5b8fb70bc020a9da22d2b924d0aab /samples/DX_APIUsage/DXUT/Optional/DXUTgui.cpp | |
| parent | Documentation Update (diff) | |
| download | gfesdk-bbe353230727568d3c1999af2701d2e150ff232f.tar.xz gfesdk-bbe353230727568d3c1999af2701d2e150ff232f.zip | |
Picking up lastest bug fixes & SHA2 signing1.1.232
[SNG-2803] GFE-SDK : SHA-2 sign task
Diffstat (limited to 'samples/DX_APIUsage/DXUT/Optional/DXUTgui.cpp')
| -rw-r--r-- | samples/DX_APIUsage/DXUT/Optional/DXUTgui.cpp | 3770 |
1 files changed, 1590 insertions, 2180 deletions
diff --git a/samples/DX_APIUsage/DXUT/Optional/DXUTgui.cpp b/samples/DX_APIUsage/DXUT/Optional/DXUTgui.cpp index cb83fff..7a7c17c 100644 --- a/samples/DX_APIUsage/DXUT/Optional/DXUTgui.cpp +++ b/samples/DX_APIUsage/DXUT/Optional/DXUTgui.cpp @@ -2,6 +2,9 @@ // File: DXUTgui.cpp // // Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +// +// http://go.microsoft.com/fwlink/?LinkId=320437 //-------------------------------------------------------------------------------------- #include "DXUT.h" #include "DXUTgui.h" @@ -10,8 +13,9 @@ #include "SDKMisc.h" -#undef min // use __min instead -#undef max // use __max instead +#include "DDSTextureLoader.h" + +using namespace DirectX; #ifndef WM_XBUTTONDOWN #define WM_XBUTTONDOWN 0x020B // (not always defined) @@ -38,28 +42,14 @@ #define DXUT_MAX_GUI_SPRITES 500 -D3DCOLORVALUE D3DCOLOR_TO_D3DCOLORVALUE( D3DCOLOR c ) +inline XMFLOAT4 D3DCOLOR_TO_D3DCOLORVALUE( DWORD c ) { - D3DCOLORVALUE cv = - { - ( ( c >> 16 ) & 0xFF ) / 255.0f, - ( ( c >> 8 ) & 0xFF ) / 255.0f, - ( c & 0xFF ) / 255.0f, - ( ( c >> 24 ) & 0xFF ) / 255.0f - }; - return cv; + return XMFLOAT4 ( ( ( c >> 16 ) & 0xFF ) / 255.0f, + ( ( c >> 8 ) & 0xFF ) / 255.0f, + ( c & 0xFF ) / 255.0f, + ( ( c >> 24 ) & 0xFF ) / 255.0f ); } -#define UNISCRIBE_DLLNAME L"usp10.dll" - -#define GETPROCADDRESS( Module, APIName, Temp ) \ - Temp = GetProcAddress( Module, #APIName ); \ - if( Temp ) \ - *(FARPROC*)&_##APIName = Temp - -#define PLACEHOLDERPROC( APIName ) \ - _##APIName = Dummy_##APIName - #define IMM32_DLLNAME L"imm32.dll" #define VER_DLLNAME L"version.dll" @@ -164,44 +154,36 @@ const UINT g_uUIEffectFileSize = sizeof( g_strUIEffectFile ); // DXUT_MAX_EDITBOXLENGTH is the maximum string length allowed in edit boxes, -// including the NULL terminator. -// +// including the nul terminator. +// // Uniscribe does not support strings having bigger-than-16-bits length. // This means that the string must be less than 65536 characters long, -// including the NULL terminator. +// including the nul terminator. #define DXUT_MAX_EDITBOXLENGTH 0xFFFF double CDXUTDialog::s_fTimeRefresh = 0.0f; -CDXUTControl* CDXUTDialog::s_pControlFocus = NULL; // The control which has focus -CDXUTControl* CDXUTDialog::s_pControlPressed = NULL; // The control currently pressed +CDXUTControl* CDXUTDialog::s_pControlFocus = nullptr; // The control which has focus +CDXUTControl* CDXUTDialog::s_pControlPressed = nullptr; // The control currently pressed struct DXUT_SCREEN_VERTEX { float x, y, z, h; - D3DCOLOR color; + DWORD color; float tu, tv; - - static DWORD FVF; }; -DWORD DXUT_SCREEN_VERTEX::FVF = D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1; - struct DXUT_SCREEN_VERTEX_UNTEX { float x, y, z, h; - D3DCOLOR color; - - static DWORD FVF; + DWORD color; }; -DWORD DXUT_SCREEN_VERTEX_UNTEX::FVF = D3DFVF_XYZRHW | D3DFVF_DIFFUSE; - struct DXUT_SCREEN_VERTEX_10 { float x, y, z; - D3DCOLORVALUE color; + XMFLOAT4 color; float tu, tv; }; @@ -216,682 +198,353 @@ inline int RectHeight( RECT& rc ) } -HRESULT InitFont11( ID3D11Device* pd3d11Device, ID3D11InputLayout* pInputLayout ); -void EndFont11(); - -//-------------------------------------------------------------------------------------- -// CDXUTDialog class -//-------------------------------------------------------------------------------------- - -//-------------------------------------------------------------------------------------- -CDXUTDialog::CDXUTDialog() -{ - m_x = 0; - m_y = 0; - m_width = 0; - m_height = 0; - - m_pManager = NULL; - m_bVisible = true; - m_bCaption = false; - m_bMinimized = false; - m_bDrag = false; - m_wszCaption[0] = L'\0'; - m_nCaptionHeight = 18; - - m_colorTopLeft = 0; - m_colorTopRight = 0; - m_colorBottomLeft = 0; - m_colorBottomRight = 0; - - m_pCallbackEvent = NULL; - m_pCallbackEventUserContext = NULL; - - m_fTimeLastRefresh = 0; - - m_pControlMouseOver = NULL; - - m_pNextDialog = this; - m_pPrevDialog = this; - - m_nDefaultControlID = 0xffff; - m_bNonUserEvents = false; - m_bKeyboardInput = false; - m_bMouseInput = true; -} +//====================================================================================== +// Font11 +//====================================================================================== +ID3D11Buffer* g_pFontBuffer11 = nullptr; +UINT g_FontBufferBytes11 = 0; +std::vector<DXUTSpriteVertex> g_FontVertices; +ID3D11ShaderResourceView* g_pFont11 = nullptr; +ID3D11InputLayout* g_pInputLayout11 = nullptr; //-------------------------------------------------------------------------------------- -CDXUTDialog::~CDXUTDialog() +HRESULT InitFont11( _In_ ID3D11Device* pd3d11Device, _In_ ID3D11InputLayout* pInputLayout ) { - int i = 0; - - RemoveAllControls(); - - m_Fonts.RemoveAll(); - m_Textures.RemoveAll(); + HRESULT hr = S_OK; + WCHAR str[MAX_PATH]; + V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, L"UI\\Font.dds" ) ); - for( i = 0; i < m_DefaultElements.GetSize(); i++ ) - { - DXUTElementHolder* pElementHolder = m_DefaultElements.GetAt( i ); - SAFE_DELETE( pElementHolder ); - } + V_RETURN( CreateDDSTextureFromFile( pd3d11Device, str, nullptr, &g_pFont11 ) ); - m_DefaultElements.RemoveAll(); + g_pInputLayout11 = pInputLayout; + return hr; } //-------------------------------------------------------------------------------------- -void CDXUTDialog::Init( CDXUTDialogResourceManager* pManager, bool bRegisterDialog ) +void EndFont11() { - m_pManager = pManager; - if( bRegisterDialog ) - pManager->RegisterDialog( this ); - - SetTexture( 0, MAKEINTRESOURCE( 0xFFFF ), ( HMODULE )0xFFFF ); - InitDefaultElements(); + SAFE_RELEASE( g_pFontBuffer11 ); + g_FontBufferBytes11 = 0; + SAFE_RELEASE( g_pFont11 ); } //-------------------------------------------------------------------------------------- -void CDXUTDialog::Init( CDXUTDialogResourceManager* pManager, bool bRegisterDialog, LPCWSTR pszControlTextureFilename ) +void BeginText11() { - m_pManager = pManager; - if( bRegisterDialog ) - pManager->RegisterDialog( this ); - SetTexture( 0, pszControlTextureFilename ); - InitDefaultElements(); + g_FontVertices.clear(); } //-------------------------------------------------------------------------------------- -void CDXUTDialog::Init( CDXUTDialogResourceManager* pManager, bool bRegisterDialog, - LPCWSTR szControlTextureResourceName, HMODULE hControlTextureResourceModule ) +_Use_decl_annotations_ +void DrawText11DXUT( ID3D11Device* pd3dDevice, ID3D11DeviceContext* pd3d11DeviceContext, + LPCWSTR strText, const RECT& rcScreen, XMFLOAT4 vFontColor, + float fBBWidth, float fBBHeight, bool bCenter ) { - m_pManager = pManager; - if( bRegisterDialog ) - pManager->RegisterDialog( this ); - - SetTexture( 0, szControlTextureResourceName, hControlTextureResourceModule ); - InitDefaultElements(); -} + float fCharTexSizeX = 0.010526315f; + //float fGlyphSizeX = 14.0f / fBBWidth; + //float fGlyphSizeY = 32.0f / fBBHeight; + float fGlyphSizeX = 15.0f / fBBWidth; + float fGlyphSizeY = 42.0f / fBBHeight; -//-------------------------------------------------------------------------------------- -void CDXUTDialog::SetCallback( PCALLBACKDXUTGUIEVENT pCallback, void* pUserContext ) -{ - // 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." ); + float fRectLeft = rcScreen.left / fBBWidth; + float fRectTop = 1.0f - rcScreen.top / fBBHeight; - m_pCallbackEvent = pCallback; - m_pCallbackEventUserContext = pUserContext; -} + fRectLeft = fRectLeft * 2.0f - 1.0f; + fRectTop = fRectTop * 2.0f - 1.0f; + int NumChars = (int)wcslen( strText ); + if (bCenter) { + float fRectRight = rcScreen.right / fBBWidth; + fRectRight = fRectRight * 2.0f - 1.0f; + float fRectBottom = 1.0f - rcScreen.bottom / fBBHeight; + fRectBottom = fRectBottom * 2.0f - 1.0f; + float fcenterx = ((fRectRight - fRectLeft) - (float)NumChars*fGlyphSizeX) *0.5f; + float fcentery = ((fRectTop - fRectBottom) - (float)1*fGlyphSizeY) *0.5f; + fRectLeft += fcenterx ; + fRectTop -= fcentery; + } + float fOriginalLeft = fRectLeft; + float fTexTop = 0.0f; + float fTexBottom = 1.0f; -//-------------------------------------------------------------------------------------- -void CDXUTDialog::RemoveControl( int ID ) -{ - for( int i = 0; i < m_Controls.GetSize(); i++ ) + float fDepth = 0.5f; + for( int i=0; i<NumChars; i++ ) { - CDXUTControl* pControl = m_Controls.GetAt( i ); - if( pControl->GetID() == ID ) + if( strText[i] == '\n' ) { - // Clean focus first - ClearFocus(); - - // Clear references to this control - if( s_pControlFocus == pControl ) - s_pControlFocus = NULL; - if( s_pControlPressed == pControl ) - s_pControlPressed = NULL; - if( m_pControlMouseOver == pControl ) - m_pControlMouseOver = NULL; - - SAFE_DELETE( pControl ); - m_Controls.Remove( i ); + fRectLeft = fOriginalLeft; + fRectTop -= fGlyphSizeY; - return; + continue; + } + else if( strText[i] < 32 || strText[i] > 126 ) + { + continue; } - } -} - - -//-------------------------------------------------------------------------------------- -void CDXUTDialog::RemoveAllControls() -{ - if( s_pControlFocus && s_pControlFocus->m_pDialog == this ) - s_pControlFocus = NULL; - if( s_pControlPressed && s_pControlPressed->m_pDialog == this ) - s_pControlPressed = NULL; - m_pControlMouseOver = NULL; - - for( int i = 0; i < m_Controls.GetSize(); i++ ) - { - CDXUTControl* pControl = m_Controls.GetAt( i ); - SAFE_DELETE( pControl ); - } - m_Controls.RemoveAll(); -} + // Add 6 sprite vertices + DXUTSpriteVertex SpriteVertex = {}; + float fRectRight = fRectLeft + fGlyphSizeX; + float fRectBottom = fRectTop - fGlyphSizeY; + float fTexLeft = ( strText[i] - 32 ) * fCharTexSizeX; + float fTexRight = fTexLeft + fCharTexSizeX; + // tri1 + SpriteVertex.vPos = XMFLOAT3( fRectLeft, fRectTop, fDepth ); + SpriteVertex.vTex = XMFLOAT2( fTexLeft, fTexTop ); + SpriteVertex.vColor = vFontColor; + g_FontVertices.push_back( SpriteVertex ); -//-------------------------------------------------------------------------------------- -CDXUTDialogResourceManager::CDXUTDialogResourceManager() -{ - // Begin D3D9-specific - m_pd3d9Device = NULL; - m_pStateBlock = NULL; - m_pSprite = NULL; + SpriteVertex.vPos = XMFLOAT3( fRectRight, fRectTop, fDepth ); + SpriteVertex.vTex = XMFLOAT2( fTexRight, fTexTop ); + SpriteVertex.vColor = vFontColor; + g_FontVertices.push_back( SpriteVertex ); - // Begin D3D11-specific - // Shaders - m_pVSRenderUI11 = NULL; - m_pPSRenderUI11 = NULL; - m_pPSRenderUIUntex11 = NULL; + SpriteVertex.vPos = XMFLOAT3( fRectLeft, fRectBottom, fDepth ); + SpriteVertex.vTex = XMFLOAT2( fTexLeft, fTexBottom ); + SpriteVertex.vColor = vFontColor; + g_FontVertices.push_back( SpriteVertex ); - // States - m_pDepthStencilStateUI11 = NULL; - m_pRasterizerStateUI11 = NULL; - m_pBlendStateUI11 = NULL; - m_pSamplerStateUI11 = NULL; - m_pDepthStencilStateStored11 = NULL; - m_pRasterizerStateStored11 = NULL; - m_pBlendStateStored11 = NULL; - m_pSamplerStateStored11 = NULL; + // tri2 + SpriteVertex.vPos = XMFLOAT3( fRectRight, fRectTop, fDepth ); + SpriteVertex.vTex = XMFLOAT2( fTexRight, fTexTop ); + SpriteVertex.vColor = vFontColor; + g_FontVertices.push_back( SpriteVertex ); - m_pInputLayout11 = NULL; - m_pVBScreenQuad11 = NULL; - m_pSpriteBuffer11 = NULL; -} + SpriteVertex.vPos = XMFLOAT3( fRectRight, fRectBottom, fDepth ); + SpriteVertex.vTex = XMFLOAT2( fTexRight, fTexBottom ); + SpriteVertex.vColor = vFontColor; + g_FontVertices.push_back( SpriteVertex ); + SpriteVertex.vPos = XMFLOAT3( fRectLeft, fRectBottom, fDepth ); + SpriteVertex.vTex = XMFLOAT2( fTexLeft, fTexBottom ); + SpriteVertex.vColor = vFontColor; + g_FontVertices.push_back( SpriteVertex ); -//-------------------------------------------------------------------------------------- -CDXUTDialogResourceManager::~CDXUTDialogResourceManager() -{ - int i; - for( i = 0; i < m_FontCache.GetSize(); i++ ) - { - DXUTFontNode* pFontNode = m_FontCache.GetAt( i ); - SAFE_DELETE( pFontNode ); - } - m_FontCache.RemoveAll(); + fRectLeft += fGlyphSizeX; - for( i = 0; i < m_TextureCache.GetSize(); i++ ) - { - DXUTTextureNode* pTextureNode = m_TextureCache.GetAt( i ); - SAFE_DELETE( pTextureNode ); } - m_TextureCache.RemoveAll(); - CUniBuffer::Uninitialize(); + // We have to end text after every line so that rendering order between sprites and fonts is preserved + EndText11( pd3dDevice, pd3d11DeviceContext ); } - -//-------------------------------------------------------------------------------------- -HRESULT CDXUTDialogResourceManager::OnD3D9CreateDevice( LPDIRECT3DDEVICE9 pd3dDevice ) +_Use_decl_annotations_ +void EndText11( ID3D11Device* pd3dDevice, ID3D11DeviceContext* pd3d11DeviceContext ) { - HRESULT hr = S_OK; - int i = 0; - - m_pd3d9Device = pd3dDevice; - - for( i = 0; i < m_FontCache.GetSize(); i++ ) - { - hr = CreateFont9( i ); - if( FAILED( hr ) ) - return hr; - } + if ( g_FontVertices.empty() ) + return; - for( i = 0; i < m_TextureCache.GetSize(); i++ ) + // ensure our buffer size can hold our sprites + UINT FontDataBytes = static_cast<UINT>( g_FontVertices.size() * sizeof( DXUTSpriteVertex ) ); + if( g_FontBufferBytes11 < FontDataBytes ) { - hr = CreateTexture9( i ); - if( FAILED( hr ) ) - return hr; - } - - hr = D3DXCreateSprite( pd3dDevice, &m_pSprite ); - if( FAILED( hr ) ) - return DXUT_ERR( L"D3DXCreateSprite", hr ); - - return S_OK; -} + SAFE_RELEASE( g_pFontBuffer11 ); + g_FontBufferBytes11 = FontDataBytes; + D3D11_BUFFER_DESC BufferDesc; + BufferDesc.ByteWidth = g_FontBufferBytes11; + BufferDesc.Usage = D3D11_USAGE_DYNAMIC; + BufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; + BufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; + BufferDesc.MiscFlags = 0; -//-------------------------------------------------------------------------------------- -HRESULT CDXUTDialogResourceManager::OnD3D9ResetDevice() -{ - HRESULT hr = S_OK; + if (FAILED(pd3dDevice->CreateBuffer(&BufferDesc, nullptr, &g_pFontBuffer11))) + { + g_pFontBuffer11 = nullptr; + g_FontBufferBytes11 = 0; + return; + } + DXUT_SetDebugName( g_pFontBuffer11, "DXUT Text11" ); + } - for( int i = 0; i < m_FontCache.GetSize(); i++ ) + // Copy the sprites over + D3D11_BOX destRegion; + destRegion.left = 0; + destRegion.right = FontDataBytes; + destRegion.top = 0; + destRegion.bottom = 1; + destRegion.front = 0; + destRegion.back = 1; + D3D11_MAPPED_SUBRESOURCE MappedResource; + if ( S_OK == pd3d11DeviceContext->Map( g_pFontBuffer11, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedResource ) ) { - DXUTFontNode* pFontNode = m_FontCache.GetAt( i ); - - if( pFontNode->pFont9 ) - pFontNode->pFont9->OnResetDevice(); + memcpy( MappedResource.pData, (void*)&g_FontVertices[0], FontDataBytes ); + pd3d11DeviceContext->Unmap(g_pFontBuffer11, 0); } - if( m_pSprite ) - m_pSprite->OnResetDevice(); + ID3D11ShaderResourceView* pOldTexture = nullptr; + pd3d11DeviceContext->PSGetShaderResources( 0, 1, &pOldTexture ); + pd3d11DeviceContext->PSSetShaderResources( 0, 1, &g_pFont11 ); - V_RETURN( m_pd3d9Device->CreateStateBlock( D3DSBT_ALL, &m_pStateBlock ) ); + // Draw + UINT Stride = sizeof( DXUTSpriteVertex ); + UINT Offset = 0; + pd3d11DeviceContext->IASetVertexBuffers( 0, 1, &g_pFontBuffer11, &Stride, &Offset ); + pd3d11DeviceContext->IASetInputLayout( g_pInputLayout11 ); + pd3d11DeviceContext->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST ); + pd3d11DeviceContext->Draw( static_cast<UINT>( g_FontVertices.size() ), 0 ); - return S_OK; -} + pd3d11DeviceContext->PSSetShaderResources( 0, 1, &pOldTexture ); + SAFE_RELEASE( pOldTexture ); -//-------------------------------------------------------------------------------------- -bool CDXUTDialogResourceManager::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) -{ - return false; + g_FontVertices.clear(); } -//-------------------------------------------------------------------------------------- -void CDXUTDialogResourceManager::OnD3D9LostDevice() +//====================================================================================== +// CDXUTDialog class +//====================================================================================== + +CDXUTDialog::CDXUTDialog() : + m_x( 0 ), + m_y( 0 ), + m_width( 0 ), + m_height( 0 ), + m_pManager( nullptr ), + m_bVisible( true ), + m_bCaption( false ), + m_bMinimized( false ), + m_bDrag( false ), + m_nCaptionHeight( 18 ), + m_colorTopLeft( 0 ), + m_colorTopRight( 0 ), + m_colorBottomLeft( 0 ), + m_colorBottomRight( 0 ), + m_pCallbackEvent( nullptr ), + m_pCallbackEventUserContext( nullptr ), + m_fTimeLastRefresh( 0 ), + m_pControlMouseOver( nullptr ), + m_nDefaultControlID( 0xffff ), + m_bNonUserEvents( false ), + m_bKeyboardInput( false ), + m_bMouseInput( true ) { - for( int i = 0; i < m_FontCache.GetSize(); i++ ) - { - DXUTFontNode* pFontNode = m_FontCache.GetAt( i ); - - if( pFontNode->pFont9 ) - pFontNode->pFont9->OnLostDevice(); - } - - if( m_pSprite ) - m_pSprite->OnLostDevice(); + m_wszCaption[0] = L'\0'; - SAFE_RELEASE( m_pStateBlock ); + m_pNextDialog = this; + m_pPrevDialog = this; } //-------------------------------------------------------------------------------------- -void CDXUTDialogResourceManager::OnD3D9DestroyDevice() -{ - int i = 0; - - m_pd3d9Device = NULL; - - // Release the resources but don't clear the cache, as these will need to be - // recreated if the device is recreated - for( i = 0; i < m_FontCache.GetSize(); i++ ) - { - DXUTFontNode* pFontNode = m_FontCache.GetAt( i ); - SAFE_RELEASE( pFontNode->pFont9 ); - } - - for( i = 0; i < m_TextureCache.GetSize(); i++ ) - { - DXUTTextureNode* pTextureNode = m_TextureCache.GetAt( i ); - SAFE_RELEASE( pTextureNode->pTexture9 ); - } - - SAFE_RELEASE( m_pSprite ); -} - - - -HRESULT CDXUTDialogResourceManager::OnD3D11CreateDevice( ID3D11Device* pd3dDevice, ID3D11DeviceContext* pd3d11DeviceContext ) +CDXUTDialog::~CDXUTDialog() { - m_pd3d11Device = pd3dDevice; - m_pd3d11DeviceContext = pd3d11DeviceContext; - - HRESULT hr = S_OK; - - // Compile Shaders - ID3DBlob* pVSBlob = NULL; - ID3DBlob* pPSBlob = NULL; - ID3DBlob* pPSUntexBlob = NULL; - V_RETURN( D3DCompile( g_strUIEffectFile, g_uUIEffectFileSize, "none", NULL, NULL, "VS", "vs_4_0_level_9_1", - D3D10_SHADER_ENABLE_BACKWARDS_COMPATIBILITY, 0, &pVSBlob, NULL ) ); - V_RETURN( D3DCompile( g_strUIEffectFile, g_uUIEffectFileSize, "none", NULL, NULL, "PS", "ps_4_0_level_9_1", - D3D10_SHADER_ENABLE_BACKWARDS_COMPATIBILITY, 0, &pPSBlob, NULL ) ); - V_RETURN( D3DCompile( g_strUIEffectFile, g_uUIEffectFileSize, "none", NULL, NULL, "PSUntex", "ps_4_0_level_9_1", - D3D10_SHADER_ENABLE_BACKWARDS_COMPATIBILITY, 0, &pPSUntexBlob, NULL ) ); - - // Create Shaders - V_RETURN( pd3dDevice->CreateVertexShader( pVSBlob->GetBufferPointer(), pVSBlob->GetBufferSize(), NULL, &m_pVSRenderUI11 ) ); - DXUT_SetDebugName( m_pVSRenderUI11, "CDXUTDialogResourceManager" ); - - V_RETURN( pd3dDevice->CreatePixelShader( pPSBlob->GetBufferPointer(), pPSBlob->GetBufferSize(), NULL, &m_pPSRenderUI11 ) ); - DXUT_SetDebugName( m_pPSRenderUI11, "CDXUTDialogResourceManager" ); - - V_RETURN( pd3dDevice->CreatePixelShader( pPSUntexBlob->GetBufferPointer(), pPSUntexBlob->GetBufferSize(), NULL, &m_pPSRenderUIUntex11 ) ); - DXUT_SetDebugName( m_pPSRenderUIUntex11, "CDXUTDialogResourceManager" ); - - // States - D3D11_DEPTH_STENCIL_DESC DSDesc; - ZeroMemory( &DSDesc, sizeof( D3D11_DEPTH_STENCIL_DESC ) ); - DSDesc.DepthEnable = FALSE; - DSDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL; - DSDesc.DepthFunc = D3D11_COMPARISON_LESS; - DSDesc.StencilEnable = FALSE; - V_RETURN( pd3dDevice->CreateDepthStencilState( &DSDesc, &m_pDepthStencilStateUI11 ) ); - DXUT_SetDebugName( m_pDepthStencilStateUI11, "CDXUTDialogResourceManager" ); - - D3D11_RASTERIZER_DESC RSDesc; - RSDesc.AntialiasedLineEnable = FALSE; - RSDesc.CullMode = D3D11_CULL_BACK; - RSDesc.DepthBias = 0; - RSDesc.DepthBiasClamp = 0.0f; - RSDesc.DepthClipEnable = TRUE; - RSDesc.FillMode = D3D11_FILL_SOLID; - RSDesc.FrontCounterClockwise = FALSE; - RSDesc.MultisampleEnable = TRUE; - RSDesc.ScissorEnable = FALSE; - RSDesc.SlopeScaledDepthBias = 0.0f; - V_RETURN( pd3dDevice->CreateRasterizerState( &RSDesc, &m_pRasterizerStateUI11 ) ); - DXUT_SetDebugName( m_pRasterizerStateUI11, "CDXUTDialogResourceManager" ); - - D3D11_BLEND_DESC BSDesc; - ZeroMemory( &BSDesc, sizeof( D3D11_BLEND_DESC ) ); - - BSDesc.RenderTarget[0].BlendEnable = TRUE; - BSDesc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA; - BSDesc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA; - BSDesc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD; - BSDesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE; - BSDesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO; - BSDesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD; - BSDesc.RenderTarget[0].RenderTargetWriteMask = 0x0F; - - V_RETURN( pd3dDevice->CreateBlendState( &BSDesc, &m_pBlendStateUI11 ) ); - DXUT_SetDebugName( m_pBlendStateUI11, "CDXUTDialogResourceManager" ); - - D3D11_SAMPLER_DESC SSDesc; - ZeroMemory( &SSDesc, sizeof( D3D11_SAMPLER_DESC ) ); - SSDesc.Filter = D3D11_FILTER_ANISOTROPIC ; - SSDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP; - SSDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP; - SSDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; - SSDesc.ComparisonFunc = D3D11_COMPARISON_NEVER; - SSDesc.MaxAnisotropy = 16; - SSDesc.MinLOD = 0; - SSDesc.MaxLOD = D3D11_FLOAT32_MAX; - if ( pd3dDevice->GetFeatureLevel() < D3D_FEATURE_LEVEL_9_3 ) { - SSDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; - SSDesc.MaxAnisotropy = 0; - } - V_RETURN( pd3dDevice->CreateSamplerState( &SSDesc, &m_pSamplerStateUI11 ) ); - DXUT_SetDebugName( m_pSamplerStateUI11, "CDXUTDialogResourceManager" ); + RemoveAllControls(); - // Create the font and texture objects in the cache arrays. - int i = 0; - for( i = 0; i < m_FontCache.GetSize(); i++ ) - { - hr = CreateFont11( i ); - if( FAILED( hr ) ) - return hr; - } + m_Fonts.clear(); + m_Textures.clear(); - for( i = 0; i < m_TextureCache.GetSize(); i++ ) + for( auto it = m_DefaultElements.begin(); it != m_DefaultElements.end(); ++it ) { - hr = CreateTexture11( i ); - if( FAILED( hr ) ) - return hr; + SAFE_DELETE( *it ); } - // Create input layout - const D3D11_INPUT_ELEMENT_DESC layout[] = - { - { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, - { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }, - { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 28, D3D11_INPUT_PER_VERTEX_DATA, 0 }, - }; - - V_RETURN( pd3dDevice->CreateInputLayout( layout, ARRAYSIZE( layout ), pVSBlob->GetBufferPointer(), pVSBlob->GetBufferSize(), &m_pInputLayout11 ) ); - DXUT_SetDebugName( m_pInputLayout11, "CDXUTDialogResourceManager" ); - - // Release the blobs - SAFE_RELEASE( pVSBlob ); - SAFE_RELEASE( pPSBlob ); - SAFE_RELEASE( pPSUntexBlob ); - - // Create a vertex buffer quad for rendering later - D3D11_BUFFER_DESC BufDesc; - BufDesc.ByteWidth = sizeof( DXUT_SCREEN_VERTEX_10 ) * 4; - BufDesc.Usage = D3D11_USAGE_DYNAMIC; - BufDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; - BufDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; - BufDesc.MiscFlags = 0; - V_RETURN( pd3dDevice->CreateBuffer( &BufDesc, NULL, &m_pVBScreenQuad11 ) ); - DXUT_SetDebugName( m_pVBScreenQuad11, "CDXUTDialogResourceManager" ); - - // Init the D3D11 font - InitFont11( pd3dDevice, m_pInputLayout11 ); - - return S_OK; + m_DefaultElements.clear(); } //-------------------------------------------------------------------------------------- -HRESULT CDXUTDialogResourceManager::OnD3D11ResizedSwapChain( ID3D11Device* pd3dDevice, - const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc ) +_Use_decl_annotations_ +void CDXUTDialog::Init( CDXUTDialogResourceManager* pManager, bool bRegisterDialog ) { - HRESULT hr = S_OK; - - m_nBackBufferWidth = pBackBufferSurfaceDesc->Width; - m_nBackBufferHeight = pBackBufferSurfaceDesc->Height; - - return hr; -} - + m_pManager = pManager; + if( bRegisterDialog ) + pManager->RegisterDialog( this ); -//-------------------------------------------------------------------------------------- -void CDXUTDialogResourceManager::OnD3D11ReleasingSwapChain() -{ + SetTexture( 0, MAKEINTRESOURCE( 0xFFFF ), ( HMODULE )0xFFFF ); + InitDefaultElements(); } //-------------------------------------------------------------------------------------- -void CDXUTDialogResourceManager::OnD3D11DestroyDevice() +_Use_decl_annotations_ +void CDXUTDialog::Init( CDXUTDialogResourceManager* pManager, bool bRegisterDialog, LPCWSTR pszControlTextureFilename ) { - int i; - - // Release the resources but don't clear the cache, as these will need to be - // recreated if the device is recreated - - for( i = 0; i < m_TextureCache.GetSize(); i++ ) - { - DXUTTextureNode* pTextureNode = m_TextureCache.GetAt( i ); - SAFE_RELEASE( pTextureNode->pTexResView11 ); - SAFE_RELEASE( pTextureNode->pTexture11 ); - } - - // D3D11 - SAFE_RELEASE( m_pVBScreenQuad11 ); - SAFE_RELEASE( m_pSpriteBuffer11 ); - m_SpriteBufferBytes11 = 0; - SAFE_RELEASE( m_pInputLayout11 ); - - // Shaders - SAFE_RELEASE( m_pVSRenderUI11 ); - SAFE_RELEASE( m_pPSRenderUI11 ); - SAFE_RELEASE( m_pPSRenderUIUntex11 ); - - // States - SAFE_RELEASE( m_pDepthStencilStateUI11 ); - SAFE_RELEASE( m_pRasterizerStateUI11 ); - SAFE_RELEASE( m_pBlendStateUI11 ); - SAFE_RELEASE( m_pSamplerStateUI11 ); - - SAFE_RELEASE( m_pDepthStencilStateStored11 ); - SAFE_RELEASE( m_pRasterizerStateStored11 ); - SAFE_RELEASE( m_pBlendStateStored11 ); - SAFE_RELEASE( m_pSamplerStateStored11 ); - - EndFont11(); + m_pManager = pManager; + if( bRegisterDialog ) + pManager->RegisterDialog( this ); + SetTexture( 0, pszControlTextureFilename ); + InitDefaultElements(); } -//-------------------------------------------------------------------------------------- -void CDXUTDialogResourceManager::StoreD3D11State( ID3D11DeviceContext* pd3dImmediateContext ) -{ - pd3dImmediateContext->OMGetDepthStencilState( &m_pDepthStencilStateStored11, &m_StencilRefStored11 ); - pd3dImmediateContext->RSGetState( &m_pRasterizerStateStored11 ); - pd3dImmediateContext->OMGetBlendState( &m_pBlendStateStored11, m_BlendFactorStored11, &m_SampleMaskStored11 ); - pd3dImmediateContext->PSGetSamplers( 0, 1, &m_pSamplerStateStored11 ); -} //-------------------------------------------------------------------------------------- -void CDXUTDialogResourceManager::RestoreD3D11State( ID3D11DeviceContext* pd3dImmediateContext ) +_Use_decl_annotations_ +void CDXUTDialog::Init( CDXUTDialogResourceManager* pManager, bool bRegisterDialog, + LPCWSTR szControlTextureResourceName, HMODULE hControlTextureResourceModule ) { - pd3dImmediateContext->OMSetDepthStencilState( m_pDepthStencilStateStored11, m_StencilRefStored11 ); - pd3dImmediateContext->RSSetState( m_pRasterizerStateStored11 ); - pd3dImmediateContext->OMSetBlendState( m_pBlendStateStored11, m_BlendFactorStored11, m_SampleMaskStored11 ); - pd3dImmediateContext->PSSetSamplers( 0, 1, &m_pSamplerStateStored11 ); + m_pManager = pManager; + if( bRegisterDialog ) + pManager->RegisterDialog( this ); - SAFE_RELEASE( m_pDepthStencilStateStored11 ); - SAFE_RELEASE( m_pRasterizerStateStored11 ); - SAFE_RELEASE( m_pBlendStateStored11 ); - SAFE_RELEASE( m_pSamplerStateStored11 ); + SetTexture( 0, szControlTextureResourceName, hControlTextureResourceModule ); + InitDefaultElements(); } -//-------------------------------------------------------------------------------------- -void CDXUTDialogResourceManager::ApplyRenderUI11( ID3D11DeviceContext* pd3dImmediateContext ) -{ - // Shaders - pd3dImmediateContext->VSSetShader( m_pVSRenderUI11, NULL, 0 ); - pd3dImmediateContext->HSSetShader( NULL, NULL, 0 ); - pd3dImmediateContext->DSSetShader( NULL, NULL, 0 ); - pd3dImmediateContext->GSSetShader( NULL, NULL, 0 ); - pd3dImmediateContext->PSSetShader( m_pPSRenderUI11, NULL, 0 ); - - // States - pd3dImmediateContext->OMSetDepthStencilState( m_pDepthStencilStateUI11, 0 ); - pd3dImmediateContext->RSSetState( m_pRasterizerStateUI11 ); - float BlendFactor[4] = { 0, 0, 0, 0 }; - pd3dImmediateContext->OMSetBlendState( m_pBlendStateUI11, BlendFactor, 0xFFFFFFFF ); - pd3dImmediateContext->PSSetSamplers( 0, 1, &m_pSamplerStateUI11 ); -} //-------------------------------------------------------------------------------------- -void CDXUTDialogResourceManager::ApplyRenderUIUntex11( ID3D11DeviceContext* pd3dImmediateContext ) +_Use_decl_annotations_ +void CDXUTDialog::SetCallback( PCALLBACKDXUTGUIEVENT pCallback, void* pUserContext ) { - // Shaders - pd3dImmediateContext->VSSetShader( m_pVSRenderUI11, NULL, 0 ); - pd3dImmediateContext->HSSetShader( NULL, NULL, 0 ); - pd3dImmediateContext->DSSetShader( NULL, NULL, 0 ); - pd3dImmediateContext->GSSetShader( NULL, NULL, 0 ); - pd3dImmediateContext->PSSetShader( m_pPSRenderUIUntex11, NULL, 0 ); + // 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 && L"To fix call CDXUTDialog::Init() first. See comments for details." ); - // States - pd3dImmediateContext->OMSetDepthStencilState( m_pDepthStencilStateUI11, 0 ); - pd3dImmediateContext->RSSetState( m_pRasterizerStateUI11 ); - float BlendFactor[4] = { 0, 0, 0, 0 }; - pd3dImmediateContext->OMSetBlendState( m_pBlendStateUI11, BlendFactor, 0xFFFFFFFF ); - pd3dImmediateContext->PSSetSamplers( 0, 1, &m_pSamplerStateUI11 ); + m_pCallbackEvent = pCallback; + m_pCallbackEventUserContext = pUserContext; } -//-------------------------------------------------------------------------------------- -void CDXUTDialogResourceManager::BeginSprites11( ) -{ - m_SpriteVertices.Reset(); -} //-------------------------------------------------------------------------------------- -void CDXUTDialogResourceManager::EndSprites11( ID3D11Device* pd3dDevice, ID3D11DeviceContext* pd3dImmediateContext ) +void CDXUTDialog::RemoveControl( _In_ int ID ) { - - // ensure our buffer size can hold our sprites - UINT SpriteDataBytes = m_SpriteVertices.GetSize() * sizeof( DXUTSpriteVertex ); - if( m_SpriteBufferBytes11 < SpriteDataBytes ) + for( auto it = m_Controls.begin(); it != m_Controls.end(); ++it ) { - SAFE_RELEASE( m_pSpriteBuffer11 ); - m_SpriteBufferBytes11 = SpriteDataBytes; - - D3D11_BUFFER_DESC BufferDesc; - BufferDesc.ByteWidth = m_SpriteBufferBytes11; - BufferDesc.Usage = D3D11_USAGE_DYNAMIC; - BufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; - BufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; - BufferDesc.MiscFlags = 0; - - pd3dDevice->CreateBuffer( &BufferDesc, NULL, &m_pSpriteBuffer11 ); - DXUT_SetDebugName( m_pSpriteBuffer11, "CDXUTDialogResourceManager" ); - } - - // Copy the sprites over - D3D11_BOX destRegion; - destRegion.left = 0; - destRegion.right = SpriteDataBytes; - destRegion.top = 0; - destRegion.bottom = 1; - destRegion.front = 0; - destRegion.back = 1; - D3D11_MAPPED_SUBRESOURCE MappedResource; - if ( S_OK == pd3dImmediateContext->Map( m_pSpriteBuffer11, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedResource ) ) { - CopyMemory( MappedResource.pData, (void*)m_SpriteVertices.GetData(), SpriteDataBytes ); - pd3dImmediateContext->Unmap(m_pSpriteBuffer11, 0); - } - - // Draw - UINT Stride = sizeof( DXUTSpriteVertex ); - UINT Offset = 0; - pd3dImmediateContext->IASetVertexBuffers( 0, 1, &m_pSpriteBuffer11, &Stride, &Offset ); - pd3dImmediateContext->IASetInputLayout( m_pInputLayout11 ); - pd3dImmediateContext->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST ); - pd3dImmediateContext->Draw( m_SpriteVertices.GetSize(), 0 ); - - m_SpriteVertices.Reset(); -} - -//-------------------------------------------------------------------------------------- -bool CDXUTDialogResourceManager::RegisterDialog( CDXUTDialog* pDialog ) -{ - // Check that the dialog isn't already registered. - for( int i = 0; i < m_Dialogs.GetSize(); ++i ) - if( m_Dialogs.GetAt( i ) == pDialog ) - return true; - - // Add to the list. - if( FAILED( m_Dialogs.Add( pDialog ) ) ) - return false; - - // Set up next and prev pointers. - if( m_Dialogs.GetSize() > 1 ) - m_Dialogs[m_Dialogs.GetSize() - 2]->SetNextDialog( pDialog ); - m_Dialogs[m_Dialogs.GetSize() - 1]->SetNextDialog( m_Dialogs[0] ); - - return true; -} - - -//-------------------------------------------------------------------------------------- -void CDXUTDialogResourceManager::UnregisterDialog( CDXUTDialog* pDialog ) -{ - // Search for the dialog in the list. - for( int i = 0; i < m_Dialogs.GetSize(); ++i ) - if( m_Dialogs.GetAt( i ) == pDialog ) + if( (*it)->GetID() == ID ) { - m_Dialogs.Remove( i ); - if( m_Dialogs.GetSize() > 0 ) - { - int l, r; + // Clean focus first + ClearFocus(); - if( 0 == i ) - l = m_Dialogs.GetSize() - 1; - else - l = i - 1; + // Clear references to this control + if( s_pControlFocus == (*it) ) + s_pControlFocus = nullptr; + if( s_pControlPressed == (*it) ) + s_pControlPressed = nullptr; + if( m_pControlMouseOver == (*it) ) + m_pControlMouseOver = nullptr; - if( m_Dialogs.GetSize() == i ) - r = 0; - else - r = i; + SAFE_DELETE( (*it) ); + m_Controls.erase( it ); - m_Dialogs[l]->SetNextDialog( m_Dialogs[r] ); - } return; } + } } //-------------------------------------------------------------------------------------- -void CDXUTDialogResourceManager::EnableKeyboardInputForAllDialogs() +void CDXUTDialog::RemoveAllControls() { - // Enable keyboard input for all registered dialogs - for( int i = 0; i < m_Dialogs.GetSize(); ++i ) - m_Dialogs[i]->EnableKeyboardInput( true ); + if( s_pControlFocus && s_pControlFocus->m_pDialog == this ) + s_pControlFocus = nullptr; + if( s_pControlPressed && s_pControlPressed->m_pDialog == this ) + s_pControlPressed = nullptr; + m_pControlMouseOver = nullptr; + + for( auto it = m_Controls.begin(); it != m_Controls.end(); ++it ) + { + SAFE_DELETE( *it ); + } + + m_Controls.clear(); } @@ -904,14 +557,13 @@ void CDXUTDialog::Refresh() if( m_pControlMouseOver ) m_pControlMouseOver->OnMouseLeave(); - s_pControlFocus = NULL; - s_pControlPressed = NULL; - m_pControlMouseOver = NULL; + s_pControlFocus = nullptr; + s_pControlPressed = nullptr; + m_pControlMouseOver = nullptr; - for( int i = 0; i < m_Controls.GetSize(); i++ ) + for( auto it = m_Controls.begin(); it != m_Controls.end(); ++it ) { - CDXUTControl* pControl = m_Controls.GetAt( i ); - pControl->Refresh(); + (*it)->Refresh(); } if( m_bKeyboardInput ) @@ -920,150 +572,7 @@ void CDXUTDialog::Refresh() //-------------------------------------------------------------------------------------- -HRESULT CDXUTDialog::OnRender( float fElapsedTime ) -{ - if( m_pManager->GetD3D9Device() ) - return OnRender9( fElapsedTime ); - else - return OnRender11( fElapsedTime ); -} - - -//-------------------------------------------------------------------------------------- -HRESULT CDXUTDialog::OnRender9( 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->GetD3D9Device() && m_pManager->m_pStateBlock && - 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; - - IDirect3DDevice9* pd3dDevice = m_pManager->GetD3D9Device(); - - // Set up a state block here and restore it when finished drawing all the controls - m_pManager->m_pStateBlock->Capture(); - - //pd3dDevice->SetSamplerState(0, D3DSAMP_SRGBTEXTURE, TRUE); - //pd3dDevice->SetRenderState( D3DRS_SRGBWRITEENABLE, TRUE ); - - pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE ); - pd3dDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA ); - pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA ); - pd3dDevice->SetRenderState( D3DRS_ALPHATESTENABLE, FALSE ); - pd3dDevice->SetRenderState( D3DRS_SEPARATEALPHABLENDENABLE, FALSE ); - pd3dDevice->SetRenderState( D3DRS_BLENDOP, D3DBLENDOP_ADD ); - pd3dDevice->SetRenderState( D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_ALPHA | D3DCOLORWRITEENABLE_BLUE | - D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_RED ); - pd3dDevice->SetRenderState( D3DRS_SHADEMODE, D3DSHADE_GOURAUD ); - pd3dDevice->SetRenderState( D3DRS_FOGENABLE, FALSE ); - pd3dDevice->SetRenderState( D3DRS_ZWRITEENABLE, FALSE ); - pd3dDevice->SetRenderState( D3DRS_FILLMODE, D3DFILL_SOLID ); - pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW ); - - pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG2 ); - pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE ); - pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 ); - pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE ); - pd3dDevice->SetTextureStageState( 0, D3DTSS_RESULTARG, D3DTA_CURRENT ); - pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_DISABLE ); - pd3dDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE ); - - BOOL bBackgroundIsVisible = ( m_colorTopLeft | m_colorTopRight | m_colorBottomRight | m_colorBottomLeft ) & - 0xff000000; - if( !m_bMinimized && bBackgroundIsVisible ) - { - DXUT_SCREEN_VERTEX_UNTEX vertices[4] = - { - ( float )m_x, ( float )m_y, 0.5f, 1.0f, m_colorTopLeft, - ( float )m_x + m_width, ( float )m_y, 0.5f, 1.0f, m_colorTopRight, - ( float )m_x + m_width, ( float )m_y + m_height, 0.5f, 1.0f, m_colorBottomRight, - ( float )m_x, ( float )m_y + m_height, 0.5f, 1.0f, m_colorBottomLeft, - }; - - pd3dDevice->SetVertexShader( NULL ); - pd3dDevice->SetPixelShader( NULL ); - - pd3dDevice->SetRenderState( D3DRS_ZENABLE, FALSE ); - - pd3dDevice->SetFVF( DXUT_SCREEN_VERTEX_UNTEX::FVF ); - pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, vertices, sizeof( DXUT_SCREEN_VERTEX_UNTEX ) ); - } - - pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE ); - pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE ); - pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE ); - - pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE ); - pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE ); - pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE ); - - pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR ); - - DXUTTextureNode* pTextureNode = GetTexture( 0 ); - pd3dDevice->SetTexture( 0, pTextureNode->pTexture9 ); - - m_pManager->m_pSprite->Begin( D3DXSPRITE_DONOTSAVESTATE ); - - // 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 - }; - DrawSprite9( &m_CapElement, &rc ); - rc.left += 5; // Make a left margin - WCHAR wszOutput[256]; - wcscpy_s( wszOutput, 256, m_wszCaption ); - if( m_bMinimized ) - wcscat_s( 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::OnRender11( float fElapsedTime ) +HRESULT CDXUTDialog::OnRender( _In_ 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. @@ -1082,14 +591,13 @@ HRESULT CDXUTDialog::OnRender11( float fElapsedTime ) ( m_bMinimized && !m_bCaption ) ) return S_OK; - ID3D11Device* pd3dDevice = m_pManager->GetD3D11Device(); - ID3D11DeviceContext* pd3dDeviceContext = m_pManager->GetD3D11DeviceContext(); + auto pd3dDevice = m_pManager->GetD3D11Device(); + auto pd3dDeviceContext = m_pManager->GetD3D11DeviceContext(); // Set up a state block here and restore it when finished drawing all the controls m_pManager->StoreD3D11State( pd3dDeviceContext ); - BOOL bBackgroundIsVisible = ( m_colorTopLeft | m_colorTopRight | m_colorBottomRight | m_colorBottomLeft ) & - 0xff000000; + 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. @@ -1112,7 +620,7 @@ HRESULT CDXUTDialog::OnRender11( float fElapsedTime ) if( SUCCEEDED( pd3dDeviceContext->Map( m_pManager->m_pVBScreenQuad11, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedData ) ) ) { - CopyMemory( MappedData.pData, vertices, sizeof( vertices ) ); + memcpy( MappedData.pData, vertices, sizeof( vertices ) ); pd3dDeviceContext->Unmap( m_pManager->m_pVBScreenQuad11, 0 ); } @@ -1128,7 +636,7 @@ HRESULT CDXUTDialog::OnRender11( float fElapsedTime ) pd3dDeviceContext->Draw( 4, 0 ); } - DXUTTextureNode* pTextureNode = GetTexture( 0 ); + auto pTextureNode = GetTexture( 0 ); pd3dDeviceContext->PSSetShaderResources( 0, 1, &pTextureNode->pTexResView11 ); // Sort depth back to front @@ -1144,31 +652,29 @@ HRESULT CDXUTDialog::OnRender11( float fElapsedTime ) // m_nCaptionHeight, so adjust the rect higher // here to negate the effect. RECT rc = { 0, -m_nCaptionHeight, m_width, 0 }; - DrawSprite11( &m_CapElement, &rc, 0.99f ); + DrawSprite( &m_CapElement, &rc, 0.99f ); rc.left += 5; // Make a left margin WCHAR wszOutput[256]; wcscpy_s( wszOutput, 256, m_wszCaption ); if( m_bMinimized ) wcscat_s( wszOutput, 256, L" (Minimized)" ); - DrawText11( pd3dDevice, pd3dDeviceContext, wszOutput, &m_CapElement, &rc, true ); + DrawText( 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++ ) + for( auto it = m_Controls.cbegin(); it != m_Controls.cend(); ++it ) { - CDXUTControl* pControl = m_Controls.GetAt( i ); - // Focused control is drawn last - if( pControl == s_pControlFocus ) + if( *it == s_pControlFocus ) continue; - pControl->Render( fElapsedTime ); + (*it)->Render( fElapsedTime ); } - if( s_pControlFocus != NULL && s_pControlFocus->m_pDialog == this ) + if( s_pControlFocus && s_pControlFocus->m_pDialog == this ) s_pControlFocus->Render( fElapsedTime ); } @@ -1183,11 +689,13 @@ HRESULT CDXUTDialog::OnRender11( float fElapsedTime ) return S_OK; } + //-------------------------------------------------------------------------------------- +_Use_decl_annotations_ 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 ) + if( !m_pCallbackEvent ) return; // Discard events triggered programatically if these types of events haven't been @@ -1200,223 +708,97 @@ VOID CDXUTDialog::SendEvent( UINT nEvent, bool bTriggeredByUser, CDXUTControl* p //-------------------------------------------------------------------------------------- -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; - nLen = wcsnlen( strFaceName, MAX_PATH); - 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 ) ); - wcscpy_s( 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 ); - - return iFont; -} - - -//-------------------------------------------------------------------------------------- +_Use_decl_annotations_ 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 + // 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." ); + assert( m_pManager && L"To fix call CDXUTDialog::Init() first. See comments for details." ); + _Analysis_assume_( m_pManager ); // Make sure the list is at least as large as the index being set - UINT i; - for( i = m_Fonts.GetSize(); i <= index; i++ ) + for( size_t i = m_Fonts.size(); i <= index; i++ ) { - m_Fonts.Add( -1 ); + m_Fonts.push_back( -1 ); } int iFont = m_pManager->AddFont( strFaceName, height, weight ); - m_Fonts.SetAt( index, iFont ); + m_Fonts[ 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 ) +DXUTFontNode* CDXUTDialog::GetFont( _In_ UINT index ) const { - // 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; - nLen = wcsnlen( strFilename, MAX_PATH); - 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; - wcscpy_s( 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; - nLen = wcsnlen ( strResourceName, MAX_PATH ); - 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; - wcscpy_s( 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; + if( !m_pManager ) + return nullptr; + return m_pManager->GetFontNode( m_Fonts[ index ] ); } //-------------------------------------------------------------------------------------- +_Use_decl_annotations_ 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 + // 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." ); + assert( m_pManager && L"To fix this, call CDXUTDialog::Init() first. See comments for details." ); + _Analysis_assume_( m_pManager ); // Make sure the list is at least as large as the index being set - for( UINT i = m_Textures.GetSize(); i <= index; i++ ) + for( size_t i = m_Textures.size(); i <= index; i++ ) { - m_Textures.Add( -1 ); + m_Textures.push_back( -1 ); } int iTexture = m_pManager->AddTexture( strFilename ); - m_Textures.SetAt( index, iTexture ); + m_Textures[ index] = iTexture; return S_OK; } //-------------------------------------------------------------------------------------- +_Use_decl_annotations_ 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 + // 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." ); + assert( m_pManager && L"To fix this, call CDXUTDialog::Init() first. See comments for details." ); + _Analysis_assume_( m_pManager ); // Make sure the list is at least as large as the index being set - for( UINT i = m_Textures.GetSize(); i <= index; i++ ) + for( size_t i = m_Textures.size(); i <= index; i++ ) { - m_Textures.Add( -1 ); + m_Textures.push_back( -1 ); } int iTexture = m_pManager->AddTexture( strResourceName, hResourceModule ); - m_Textures.SetAt( index, iTexture ); + m_Textures[ index ] = iTexture; return S_OK; } //-------------------------------------------------------------------------------------- -DXUTTextureNode* CDXUTDialog::GetTexture( UINT index ) +DXUTTextureNode* CDXUTDialog::GetTexture( _In_ UINT index ) const { - if( NULL == m_pManager ) - return NULL; - return m_pManager->GetTextureNode( m_Textures.GetAt( index ) ); + if( !m_pManager ) + return nullptr; + return m_pManager->GetTextureNode( m_Textures[ index ] ); } - //-------------------------------------------------------------------------------------- +_Use_decl_annotations_ bool CDXUTDialog::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) { bool bHandled = false; @@ -1526,6 +908,7 @@ bool CDXUTDialog::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) if( s_pControlFocus && s_pControlFocus->m_pDialog == this && s_pControlFocus->GetEnabled() ) + for( auto it = m_Controls.cbegin(); it != m_Controls.cend(); ++it ) { if( s_pControlFocus->HandleKeyboard( uMsg, wParam, lParam ) ) return true; @@ -1538,12 +921,11 @@ bool CDXUTDialog::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) ( s_pControlFocus->GetType() != DXUT_CONTROL_EDITBOX && s_pControlFocus->GetType() != DXUT_CONTROL_IMEEDITBOX ) ) ) { - for( int i = 0; i < m_Controls.GetSize(); i++ ) + for( auto it = m_Controls.begin(); it != m_Controls.end(); ++it ) { - CDXUTControl* pControl = m_Controls.GetAt( i ); - if( pControl->GetHotkey() == wParam ) + if( (*it)->GetHotkey() == wParam ) { - pControl->OnHotkey(); + (*it)->OnHotkey(); return true; } } @@ -1560,7 +942,7 @@ bool CDXUTDialog::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) { case VK_RIGHT: case VK_DOWN: - if( s_pControlFocus != NULL ) + if( s_pControlFocus ) { return OnCycleFocus( true ); } @@ -1568,7 +950,7 @@ bool CDXUTDialog::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) case VK_LEFT: case VK_UP: - if( s_pControlFocus != NULL ) + if( s_pControlFocus ) { return OnCycleFocus( false ); } @@ -1602,7 +984,7 @@ bool CDXUTDialog::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) case WM_XBUTTONDBLCLK: case WM_MOUSEWHEEL: { - // If not accepting mouse input, return false to indicate the message should still + // If not accepting mouse input, return false to indicate the message should still // be handled by the application (usually to move the camera). if( !m_bMouseInput ) return false; @@ -1629,8 +1011,8 @@ bool CDXUTDialog::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) } // Not yet handled, see if the mouse is over any controls - CDXUTControl* pControl = GetControlAtPoint( mousePoint ); - if( pControl != NULL && pControl->GetEnabled() ) + auto pControl = GetControlAtPoint( mousePoint ); + if( pControl && pControl->GetEnabled() ) { bHandled = pControl->HandleMouse( uMsg, mousePoint, wParam, lParam ); if( bHandled ) @@ -1645,7 +1027,7 @@ bool CDXUTDialog::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) s_pControlFocus->m_pDialog == this ) { s_pControlFocus->OnFocusOut(); - s_pControlFocus = NULL; + s_pControlFocus = nullptr; } } @@ -1676,16 +1058,15 @@ bool CDXUTDialog::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) return false; } + //-------------------------------------------------------------------------------------- -CDXUTControl* CDXUTDialog::GetControlAtPoint( POINT pt ) +CDXUTControl* CDXUTDialog::GetControlAtPoint( _In_ const POINT& pt ) const { // Search through all child controls for the first one which // contains the mouse point - for( int i = 0; i < m_Controls.GetSize(); i++ ) + for( auto it = m_Controls.cbegin(); it != m_Controls.cend(); ++it ) { - CDXUTControl* pControl = m_Controls.GetAt( i ); - - if( pControl == NULL ) + if( !*it ) { continue; } @@ -1693,21 +1074,21 @@ CDXUTControl* CDXUTDialog::GetControlAtPoint( POINT pt ) // We only return the current control if it is visible // and enabled. Because GetControlAtPoint() is used to do mouse // hittest, it makes sense to perform this filtering. - if( pControl->ContainsPoint( pt ) && pControl->GetEnabled() && pControl->GetVisible() ) + if( (*it)->ContainsPoint( pt ) && (*it)->GetEnabled() && (*it)->GetVisible() ) { - return pControl; + return *it; } } - return NULL; + return nullptr; } //-------------------------------------------------------------------------------------- -bool CDXUTDialog::GetControlEnabled( int ID ) +bool CDXUTDialog::GetControlEnabled( _In_ int ID ) const { - CDXUTControl* pControl = GetControl( ID ); - if( pControl == NULL ) + auto pControl = GetControl( ID ); + if( !pControl ) return false; return pControl->GetEnabled(); @@ -1716,10 +1097,10 @@ bool CDXUTDialog::GetControlEnabled( int ID ) //-------------------------------------------------------------------------------------- -void CDXUTDialog::SetControlEnabled( int ID, bool bEnabled ) +void CDXUTDialog::SetControlEnabled( _In_ int ID, _In_ bool bEnabled ) { - CDXUTControl* pControl = GetControl( ID ); - if( pControl == NULL ) + auto pControl = GetControl( ID ); + if( !pControl ) return; pControl->SetEnabled( bEnabled ); @@ -1727,18 +1108,19 @@ void CDXUTDialog::SetControlEnabled( int ID, bool bEnabled ) //-------------------------------------------------------------------------------------- -void CDXUTDialog::OnMouseUp( POINT pt ) +void CDXUTDialog::OnMouseUp( _In_ const POINT& pt ) { - s_pControlPressed = NULL; - m_pControlMouseOver = NULL; + UNREFERENCED_PARAMETER(pt); + s_pControlPressed = nullptr; + m_pControlMouseOver = nullptr; } //-------------------------------------------------------------------------------------- -void CDXUTDialog::OnMouseMove( POINT pt ) +void CDXUTDialog::OnMouseMove( _In_ const POINT& pt ) { // Figure out which control the mouse is over now - CDXUTControl* pControl = GetControlAtPoint( pt ); + auto pControl = GetControlAtPoint( pt ); // If the mouse is still over the same control, nothing needs to be done if( pControl == m_pControlMouseOver ) @@ -1750,77 +1132,72 @@ void CDXUTDialog::OnMouseMove( POINT pt ) // Handle mouse entering the new control m_pControlMouseOver = pControl; - if( pControl != NULL ) + if( pControl ) m_pControlMouseOver->OnMouseEnter(); } //-------------------------------------------------------------------------------------- +_Use_decl_annotations_ HRESULT CDXUTDialog::SetDefaultElement( UINT nControlType, UINT iElement, CDXUTElement* pElement ) { // If this Element type already exist in the list, simply update the stored Element - for( int i = 0; i < m_DefaultElements.GetSize(); i++ ) + for( auto it = m_DefaultElements.begin(); it != m_DefaultElements.end(); ++it ) { - DXUTElementHolder* pElementHolder = m_DefaultElements.GetAt( i ); - - if( pElementHolder->nControlType == nControlType && - pElementHolder->iElement == iElement ) + if( (*it)->nControlType == nControlType && + (*it)->iElement == iElement ) { - pElementHolder->Element = *pElement; + (*it)->Element = *pElement; return S_OK; } } // Otherwise, add a new entry DXUTElementHolder* pNewHolder; - pNewHolder = new DXUTElementHolder; - if( pNewHolder == NULL ) + pNewHolder = new (std::nothrow) DXUTElementHolder; + if( !pNewHolder ) return E_OUTOFMEMORY; pNewHolder->nControlType = nControlType; pNewHolder->iElement = iElement; pNewHolder->Element = *pElement; - HRESULT hr = m_DefaultElements.Add( pNewHolder ); - if( FAILED( hr ) ) - { - delete pNewHolder; - } - return hr; + m_DefaultElements.push_back( pNewHolder ); + + return S_OK; } //-------------------------------------------------------------------------------------- -CDXUTElement* CDXUTDialog::GetDefaultElement( UINT nControlType, UINT iElement ) +_Use_decl_annotations_ +CDXUTElement* CDXUTDialog::GetDefaultElement( UINT nControlType, UINT iElement ) const { - for( int i = 0; i < m_DefaultElements.GetSize(); i++ ) + for( auto it = m_DefaultElements.cbegin(); it != m_DefaultElements.cend(); ++it ) { - DXUTElementHolder* pElementHolder = m_DefaultElements.GetAt( i ); - - if( pElementHolder->nControlType == nControlType && - pElementHolder->iElement == iElement ) + if( (*it)->nControlType == nControlType && + (*it)->iElement == iElement ) { - return &pElementHolder->Element; + return &(*it)->Element; } } - return NULL; + return nullptr; } - //-------------------------------------------------------------------------------------- +_Use_decl_annotations_ HRESULT CDXUTDialog::AddStatic( int ID, LPCWSTR strText, int x, int y, int width, int height, bool bIsDefault, CDXUTStatic** ppCreated ) { HRESULT hr = S_OK; - CDXUTStatic* pStatic = new CDXUTStatic( this ); + auto pStatic = new (std::nothrow) CDXUTStatic( this ); - if( ppCreated != NULL ) + if( ppCreated ) *ppCreated = pStatic; - if( pStatic == NULL ) + if( !pStatic ) return E_OUTOFMEMORY; hr = AddControl( pStatic ); @@ -1839,17 +1216,18 @@ HRESULT CDXUTDialog::AddStatic( int ID, LPCWSTR strText, int x, int y, int width //-------------------------------------------------------------------------------------- +_Use_decl_annotations_ HRESULT CDXUTDialog::AddButton( int ID, LPCWSTR strText, int x, int y, int width, int height, UINT nHotkey, bool bIsDefault, CDXUTButton** ppCreated ) { HRESULT hr = S_OK; - CDXUTButton* pButton = new CDXUTButton( this ); + auto pButton = new (std::nothrow) CDXUTButton( this ); - if( ppCreated != NULL ) + if( ppCreated ) *ppCreated = pButton; - if( pButton == NULL ) + if( !pButton ) return E_OUTOFMEMORY; hr = AddControl( pButton ); @@ -1867,17 +1245,19 @@ HRESULT CDXUTDialog::AddButton( int ID, LPCWSTR strText, int x, int y, int width return S_OK; } +//-------------------------------------------------------------------------------------- +_Use_decl_annotations_ HRESULT CDXUTDialog::AddButtonCallback(std::function<void()> cb, LPCWSTR strText, int x, int y, int width, int height, UINT nHotkey, - bool bIsDefault, CDXUTButton** ppCreated) + bool bIsDefault, CDXUTButton** ppCreated) { HRESULT hr = S_OK; - CDXUTButton* pButton = new CDXUTButton(this); + auto pButton = new (std::nothrow) CDXUTButton(this); - if (ppCreated != NULL) + if (ppCreated) *ppCreated = pButton; - if (pButton == NULL) + if (!pButton) return E_OUTOFMEMORY; hr = AddControl(pButton); @@ -1896,18 +1276,20 @@ HRESULT CDXUTDialog::AddButtonCallback(std::function<void()> cb, LPCWSTR strText return S_OK; } + //-------------------------------------------------------------------------------------- +_Use_decl_annotations_ HRESULT CDXUTDialog::AddCheckBox( int ID, LPCWSTR strText, int x, int y, int width, int height, bool bChecked, UINT nHotkey, bool bIsDefault, CDXUTCheckBox** ppCreated ) { HRESULT hr = S_OK; - CDXUTCheckBox* pCheckBox = new CDXUTCheckBox( this ); + auto pCheckBox = new (std::nothrow) CDXUTCheckBox( this ); - if( ppCreated != NULL ) + if( ppCreated ) *ppCreated = pCheckBox; - if( pCheckBox == NULL ) + if( !pCheckBox ) return E_OUTOFMEMORY; hr = AddControl( pCheckBox ); @@ -1927,19 +1309,19 @@ HRESULT CDXUTDialog::AddCheckBox( int ID, LPCWSTR strText, int x, int y, int wid } - //-------------------------------------------------------------------------------------- +_Use_decl_annotations_ HRESULT CDXUTDialog::AddRadioButton( int ID, UINT nButtonGroup, LPCWSTR strText, int x, int y, int width, int height, bool bChecked, UINT nHotkey, bool bIsDefault, CDXUTRadioButton** ppCreated ) { HRESULT hr = S_OK; - CDXUTRadioButton* pRadioButton = new CDXUTRadioButton( this ); + auto pRadioButton = new (std::nothrow) CDXUTRadioButton( this ); - if( ppCreated != NULL ) + if( ppCreated ) *ppCreated = pRadioButton; - if( pRadioButton == NULL ) + if( !pRadioButton ) return E_OUTOFMEMORY; hr = AddControl( pRadioButton ); @@ -1961,20 +1343,19 @@ HRESULT CDXUTDialog::AddRadioButton( int ID, UINT nButtonGroup, LPCWSTR strText, } - - //-------------------------------------------------------------------------------------- +_Use_decl_annotations_ HRESULT CDXUTDialog::AddComboBox( int ID, int x, int y, int width, int height, UINT nHotkey, bool bIsDefault, CDXUTComboBox** ppCreated ) { HRESULT hr = S_OK; - CDXUTComboBox* pComboBox = new CDXUTComboBox( this ); + auto pComboBox = new (std::nothrow) CDXUTComboBox( this ); - if( ppCreated != NULL ) + if( ppCreated ) *ppCreated = pComboBox; - if( pComboBox == NULL ) + if( !pComboBox ) return E_OUTOFMEMORY; hr = AddControl( pComboBox ); @@ -1992,19 +1373,19 @@ HRESULT CDXUTDialog::AddComboBox( int ID, int x, int y, int width, int height, U } - //-------------------------------------------------------------------------------------- +_Use_decl_annotations_ HRESULT CDXUTDialog::AddSlider( int ID, int x, int y, int width, int height, int min, int max, int value, bool bIsDefault, CDXUTSlider** ppCreated ) { HRESULT hr = S_OK; - CDXUTSlider* pSlider = new CDXUTSlider( this ); + auto pSlider = new (std::nothrow) CDXUTSlider( this ); - if( ppCreated != NULL ) + if( ppCreated ) *ppCreated = pSlider; - if( pSlider == NULL ) + if( !pSlider ) return E_OUTOFMEMORY; hr = AddControl( pSlider ); @@ -2024,19 +1405,19 @@ HRESULT CDXUTDialog::AddSlider( int ID, int x, int y, int width, int height, int } - //-------------------------------------------------------------------------------------- +_Use_decl_annotations_ HRESULT CDXUTDialog::AddEditBox( int ID, LPCWSTR strText, int x, int y, int width, int height, bool bIsDefault, CDXUTEditBox** ppCreated ) { HRESULT hr = S_OK; - CDXUTEditBox* pEditBox = new CDXUTEditBox( this ); + auto pEditBox = new (std::nothrow) CDXUTEditBox( this ); - if( ppCreated != NULL ) + if( ppCreated ) *ppCreated = pEditBox; - if( pEditBox == NULL ) + if( !pEditBox ) return E_OUTOFMEMORY; hr = AddControl( pEditBox ); @@ -2057,15 +1438,16 @@ HRESULT CDXUTDialog::AddEditBox( int ID, LPCWSTR strText, int x, int y, int widt //-------------------------------------------------------------------------------------- +_Use_decl_annotations_ HRESULT CDXUTDialog::AddListBox( int ID, int x, int y, int width, int height, DWORD dwStyle, CDXUTListBox** ppCreated ) { HRESULT hr = S_OK; - CDXUTListBox* pListBox = new CDXUTListBox( this ); + auto pListBox = new (std::nothrow) CDXUTListBox( this ); - if( ppCreated != NULL ) + if( ppCreated ) *ppCreated = pListBox; - if( pListBox == NULL ) + if( !pListBox ) return E_OUTOFMEMORY; hr = AddControl( pListBox ); @@ -2082,23 +1464,21 @@ HRESULT CDXUTDialog::AddListBox( int ID, int x, int y, int width, int height, DW } - //-------------------------------------------------------------------------------------- -HRESULT CDXUTDialog::InitControl( CDXUTControl* pControl ) +HRESULT CDXUTDialog::InitControl( _In_ CDXUTControl* pControl ) { HRESULT hr; - if( pControl == NULL ) + if( !pControl ) return E_INVALIDARG; - pControl->m_Index = m_Controls.GetSize(); + pControl->m_Index = static_cast<UINT>( m_Controls.size() ); // Look for a default Element entries - for( int i = 0; i < m_DefaultElements.GetSize(); i++ ) + for( auto it = m_DefaultElements.begin(); it != m_DefaultElements.end(); ++it ) { - DXUTElementHolder* pElementHolder = m_DefaultElements.GetAt( i ); - if( pElementHolder->nControlType == pControl->GetType() ) - pControl->SetElement( pElementHolder->iElement, &pElementHolder->Element ); + if( (*it)->nControlType == pControl->GetType() ) + pControl->SetElement( (*it)->iElement, &(*it)->Element ); } V_RETURN( pControl->OnInit() ); @@ -2107,9 +1487,8 @@ HRESULT CDXUTDialog::InitControl( CDXUTControl* pControl ) } - //-------------------------------------------------------------------------------------- -HRESULT CDXUTDialog::AddControl( CDXUTControl* pControl ) +HRESULT CDXUTDialog::AddControl( _In_ CDXUTControl* pControl ) { HRESULT hr = S_OK; @@ -2118,81 +1497,72 @@ HRESULT CDXUTDialog::AddControl( CDXUTControl* pControl ) return DXTRACE_ERR( L"CDXUTDialog::InitControl", hr ); // Add to the list - hr = m_Controls.Add( pControl ); - if( FAILED( hr ) ) - { - return DXTRACE_ERR( L"CGrowableArray::Add", hr ); - } + m_Controls.push_back( pControl ); return S_OK; } //-------------------------------------------------------------------------------------- -CDXUTControl* CDXUTDialog::GetControl( int ID ) +CDXUTControl* CDXUTDialog::GetControl( _In_ int ID ) const { // Try to find the control with the given ID - for( int i = 0; i < m_Controls.GetSize(); i++ ) + for( auto it = m_Controls.cbegin(); it != m_Controls.cend(); ++it ) { - CDXUTControl* pControl = m_Controls.GetAt( i ); - - if( pControl->GetID() == ID ) + if( (*it)->GetID() == ID ) { - return pControl; + return *it; } } // Not found - return NULL; + return nullptr; } - //-------------------------------------------------------------------------------------- -CDXUTControl* CDXUTDialog::GetControl( int ID, UINT nControlType ) +CDXUTControl* CDXUTDialog::GetControl( _In_ int ID, _In_ UINT nControlType ) const { // Try to find the control with the given ID - for( int i = 0; i < m_Controls.GetSize(); i++ ) + for( auto it = m_Controls.cbegin(); it != m_Controls.cend(); ++it ) { - CDXUTControl* pControl = m_Controls.GetAt( i ); - - if( pControl->GetID() == ID && pControl->GetType() == nControlType ) + if( (*it)->GetID() == ID && (*it)->GetType() == nControlType ) { - return pControl; + return *it; } } // Not found - return NULL; + return nullptr; } - //-------------------------------------------------------------------------------------- -CDXUTControl* CDXUTDialog::GetNextControl( CDXUTControl* pControl ) +CDXUTControl* CDXUTDialog::GetNextControl( _In_ CDXUTControl* pControl ) { int index = pControl->m_Index + 1; - CDXUTDialog* pDialog = pControl->m_pDialog; + auto pDialog = pControl->m_pDialog; // Cycle through dialogs in the loop to find the next control. Note // that if only one control exists in all looped dialogs it will // be the returned 'next' control. - while( index >= ( int )pDialog->m_Controls.GetSize() ) + while( index >= ( int )pDialog->m_Controls.size() ) { pDialog = pDialog->m_pNextDialog; index = 0; } - return pDialog->m_Controls.GetAt( index ); + return pDialog->m_Controls[ index ]; } + //-------------------------------------------------------------------------------------- -CDXUTControl* CDXUTDialog::GetPrevControl( CDXUTControl* pControl ) +CDXUTControl* CDXUTDialog::GetPrevControl( _In_ CDXUTControl* pControl ) { int index = pControl->m_Index - 1; - CDXUTDialog* pDialog = pControl->m_pDialog; + auto pDialog = pControl->m_pDialog; // Cycle through dialogs in the loop to find the next control. Note // that if only one control exists in all looped dialogs it will @@ -2200,27 +1570,25 @@ CDXUTControl* CDXUTDialog::GetPrevControl( CDXUTControl* pControl ) while( index < 0 ) { pDialog = pDialog->m_pPrevDialog; - if( pDialog == NULL ) + if( !pDialog ) pDialog = pControl->m_pDialog; - index = pDialog->m_Controls.GetSize() - 1; + index = int( pDialog->m_Controls.size() ) - 1; } - return pDialog->m_Controls.GetAt( index ); + return pDialog->m_Controls[ index ]; } //-------------------------------------------------------------------------------------- -void CDXUTDialog::ClearRadioButtonGroup( UINT nButtonGroup ) +void CDXUTDialog::ClearRadioButtonGroup( _In_ UINT nButtonGroup ) { // Find all radio buttons with the given group number - for( int i = 0; i < m_Controls.GetSize(); i++ ) + for( auto it = m_Controls.cbegin(); it != m_Controls.cend(); ++it ) { - CDXUTControl* pControl = m_Controls.GetAt( i ); - - if( pControl->GetType() == DXUT_CONTROL_RADIOBUTTON ) + if( (*it)->GetType() == DXUT_CONTROL_RADIOBUTTON ) { - CDXUTRadioButton* pRadioButton = ( CDXUTRadioButton* )pControl; + auto pRadioButton = ( CDXUTRadioButton* )*it; if( pRadioButton->GetButtonGroup() == nButtonGroup ) pRadioButton->SetChecked( false, false ); @@ -2229,22 +1597,19 @@ void CDXUTDialog::ClearRadioButtonGroup( UINT nButtonGroup ) } - //-------------------------------------------------------------------------------------- -void CDXUTDialog::ClearComboBox( int ID ) +void CDXUTDialog::ClearComboBox( _In_ int ID ) { - CDXUTComboBox* pComboBox = GetComboBox( ID ); - if( pComboBox == NULL ) + auto pComboBox = GetComboBox( ID ); + if( !pComboBox ) return; pComboBox->RemoveAllItems(); } - - //-------------------------------------------------------------------------------------- -void CDXUTDialog::RequestFocus( CDXUTControl* pControl ) +void CDXUTDialog::RequestFocus( _In_ CDXUTControl* pControl ) { if( s_pControlFocus == pControl ) return; @@ -2261,156 +1626,22 @@ void CDXUTDialog::RequestFocus( CDXUTControl* pControl ) //-------------------------------------------------------------------------------------- -HRESULT CDXUTDialog::DrawRect( RECT* pRect, D3DCOLOR color ) +_Use_decl_annotations_ +HRESULT CDXUTDialog::DrawRect( const RECT* pRect, DWORD color ) { - if( m_pManager->GetD3D9Device() ) - return DrawRect9( pRect, color ); + UNREFERENCED_PARAMETER(pRect); + UNREFERENCED_PARAMETER(color); + // TODO - return E_FAIL; } //-------------------------------------------------------------------------------------- -HRESULT CDXUTDialog::DrawRect9( RECT* pRect, D3DCOLOR color ) -{ - RECT rcScreen = *pRect; - OffsetRect( &rcScreen, m_x, m_y ); - - // If caption is enabled, offset the Y position by its height. - if( m_bCaption ) - OffsetRect( &rcScreen, 0, m_nCaptionHeight ); - - DXUT_SCREEN_VERTEX vertices[4] = - { - ( float )rcScreen.left - 0.5f, ( float )rcScreen.top - 0.5f, 0.5f, 1.0f, color, 0, 0, - ( float )rcScreen.right - 0.5f, ( float )rcScreen.top - 0.5f, 0.5f, 1.0f, color, 0, 0, - ( float )rcScreen.right - 0.5f, ( float )rcScreen.bottom - 0.5f, 0.5f, 1.0f, color, 0, 0, - ( float )rcScreen.left - 0.5f, ( float )rcScreen.bottom - 0.5f, 0.5f, 1.0f, color, 0, 0, - }; - - IDirect3DDevice9* pd3dDevice = m_pManager->GetD3D9Device(); - - // Since we're doing our own drawing here we need to flush the sprites - m_pManager->m_pSprite->Flush(); - IDirect3DVertexDeclaration9* pDecl = NULL; - pd3dDevice->GetVertexDeclaration( &pDecl ); // Preserve the sprite's current vertex decl - pd3dDevice->SetFVF( DXUT_SCREEN_VERTEX::FVF ); - - pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG2 ); - pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG2 ); - - pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, vertices, sizeof( DXUT_SCREEN_VERTEX ) ); - - pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE ); - pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE ); - - // Restore the vertex decl - pd3dDevice->SetVertexDeclaration( pDecl ); - pDecl->Release(); - - return S_OK; -} - - -//-------------------------------------------------------------------------------------- -HRESULT CDXUTDialog::DrawPolyLine( POINT* apPoints, UINT nNumPoints, D3DCOLOR color ) -{ - DXUT_SCREEN_VERTEX* vertices = new DXUT_SCREEN_VERTEX[ nNumPoints ]; - if( vertices == NULL ) - return E_OUTOFMEMORY; - - DXUT_SCREEN_VERTEX* pVertex = vertices; - POINT* pt = apPoints; - for( UINT i = 0; i < nNumPoints; i++ ) - { - pVertex->x = m_x + ( float )pt->x; - pVertex->y = m_y + ( float )pt->y; - pVertex->z = 0.5f; - pVertex->h = 1.0f; - pVertex->color = color; - pVertex->tu = 0.0f; - pVertex->tv = 0.0f; - - pVertex++; - pt++; - } - - IDirect3DDevice9* pd3dDevice = m_pManager->GetD3D9Device(); - - // Since we're doing our own drawing here we need to flush the sprites - m_pManager->m_pSprite->Flush(); - IDirect3DVertexDeclaration9* pDecl = NULL; - pd3dDevice->GetVertexDeclaration( &pDecl ); // Preserve the sprite's current vertex decl - pd3dDevice->SetFVF( DXUT_SCREEN_VERTEX::FVF ); - - pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG2 ); - pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG2 ); - - pd3dDevice->DrawPrimitiveUP( D3DPT_LINESTRIP, nNumPoints - 1, vertices, sizeof( DXUT_SCREEN_VERTEX ) ); - - pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE ); - pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE ); - - // Restore the vertex decl - pd3dDevice->SetVertexDeclaration( pDecl ); - pDecl->Release(); - - SAFE_DELETE_ARRAY( vertices ); - return S_OK; -} - - -//-------------------------------------------------------------------------------------- -HRESULT CDXUTDialog::DrawSprite( CDXUTElement* pElement, RECT* prcDest, float fDepth ) -{ - if( m_pManager->GetD3D9Device() ) - return DrawSprite9( pElement, prcDest ); - else - return DrawSprite11( pElement, prcDest, fDepth ); -} - - -//-------------------------------------------------------------------------------------- -HRESULT CDXUTDialog::DrawSprite9( CDXUTElement* pElement, RECT* prcDest ) -{ - // No need to draw fully transparent layers - if( pElement->TextureColor.Current.a == 0 ) - return S_OK; - - RECT rcTexture = pElement->rcTexture; - - RECT rcScreen = *prcDest; - OffsetRect( &rcScreen, m_x, m_y ); - - // If caption is enabled, offset the Y position by its height. - if( m_bCaption ) - OffsetRect( &rcScreen, 0, m_nCaptionHeight ); - - DXUTTextureNode* pTextureNode = GetTexture( pElement->iTexture ); - if( pTextureNode == NULL ) - return E_FAIL; - - float fScaleX = ( float )RectWidth( rcScreen ) / RectWidth( rcTexture ); - float fScaleY = ( float )RectHeight( rcScreen ) / RectHeight( rcTexture ); - - D3DXMATRIXA16 matTransform; - D3DXMatrixScaling( &matTransform, fScaleX, fScaleY, 1.0f ); - - m_pManager->m_pSprite->SetTransform( &matTransform ); - - D3DXVECTOR3 vPos( ( float )rcScreen.left, ( float )rcScreen.top, 0.0f ); - - vPos.x /= fScaleX; - vPos.y /= fScaleY; - - return m_pManager->m_pSprite->Draw( pTextureNode->pTexture9, &rcTexture, NULL, &vPos, - pElement->TextureColor.Current ); -} - -//-------------------------------------------------------------------------------------- -HRESULT CDXUTDialog::DrawSprite11( CDXUTElement* pElement, RECT* prcDest, float fDepth ) +_Use_decl_annotations_ +HRESULT CDXUTDialog::DrawSprite( CDXUTElement* pElement, const RECT* prcDest, float fDepth ) { // No need to draw fully transparent layers - if( pElement->TextureColor.Current.a == 0 ) + if( pElement->TextureColor.Current.w == 0 ) return S_OK; RECT rcTexture = pElement->rcTexture; @@ -2422,8 +1653,8 @@ HRESULT CDXUTDialog::DrawSprite11( CDXUTElement* pElement, RECT* prcDest, float if( m_bCaption ) OffsetRect( &rcScreen, 0, m_nCaptionHeight ); - DXUTTextureNode* pTextureNode = GetTexture( pElement->iTexture ); - if( pTextureNode == NULL ) + auto pTextureNode = GetTexture( pElement->iTexture ); + if( !pTextureNode ) return E_FAIL; float fBBWidth = ( float )m_pManager->m_nBackBufferWidth; @@ -2440,46 +1671,46 @@ HRESULT CDXUTDialog::DrawSprite11( CDXUTElement* pElement, RECT* prcDest, float fRectTop = fRectTop * 2.0f - 1.0f; fRectRight = fRectRight * 2.0f - 1.0f; fRectBottom = fRectBottom * 2.0f - 1.0f; - + float fTexLeft = rcTexture.left / fTexWidth; float fTexTop = rcTexture.top / fTexHeight; float fTexRight = rcTexture.right / fTexWidth; float fTexBottom = rcTexture.bottom / fTexHeight; // Add 6 sprite vertices - DXUTSpriteVertex SpriteVertex; + DXUTSpriteVertex SpriteVertex = {}; // tri1 - SpriteVertex.vPos = D3DXVECTOR3( fRectLeft, fRectTop, fDepth ); - SpriteVertex.vTex = D3DXVECTOR2( fTexLeft, fTexTop ); + SpriteVertex.vPos = XMFLOAT3( fRectLeft, fRectTop, fDepth ); + SpriteVertex.vTex = XMFLOAT2( fTexLeft, fTexTop ); SpriteVertex.vColor = pElement->TextureColor.Current; - m_pManager->m_SpriteVertices.Add( SpriteVertex ); + m_pManager->m_SpriteVertices.push_back( SpriteVertex ); - SpriteVertex.vPos = D3DXVECTOR3( fRectRight, fRectTop, fDepth ); - SpriteVertex.vTex = D3DXVECTOR2( fTexRight, fTexTop ); + SpriteVertex.vPos = XMFLOAT3( fRectRight, fRectTop, fDepth ); + SpriteVertex.vTex = XMFLOAT2( fTexRight, fTexTop ); SpriteVertex.vColor = pElement->TextureColor.Current; - m_pManager->m_SpriteVertices.Add( SpriteVertex ); + m_pManager->m_SpriteVertices.push_back( SpriteVertex ); - SpriteVertex.vPos = D3DXVECTOR3( fRectLeft, fRectBottom, fDepth ); - SpriteVertex.vTex = D3DXVECTOR2( fTexLeft, fTexBottom ); + SpriteVertex.vPos = XMFLOAT3( fRectLeft, fRectBottom, fDepth ); + SpriteVertex.vTex = XMFLOAT2( fTexLeft, fTexBottom ); SpriteVertex.vColor = pElement->TextureColor.Current; - m_pManager->m_SpriteVertices.Add( SpriteVertex ); + m_pManager->m_SpriteVertices.push_back( SpriteVertex ); // tri2 - SpriteVertex.vPos = D3DXVECTOR3( fRectRight, fRectTop, fDepth ); - SpriteVertex.vTex = D3DXVECTOR2( fTexRight, fTexTop ); + SpriteVertex.vPos = XMFLOAT3( fRectRight, fRectTop, fDepth ); + SpriteVertex.vTex = XMFLOAT2( fTexRight, fTexTop ); SpriteVertex.vColor = pElement->TextureColor.Current; - m_pManager->m_SpriteVertices.Add( SpriteVertex ); + m_pManager->m_SpriteVertices.push_back( SpriteVertex ); - SpriteVertex.vPos = D3DXVECTOR3( fRectRight, fRectBottom, fDepth ); - SpriteVertex.vTex = D3DXVECTOR2( fTexRight, fTexBottom ); + SpriteVertex.vPos = XMFLOAT3( fRectRight, fRectBottom, fDepth ); + SpriteVertex.vTex = XMFLOAT2( fTexRight, fTexBottom ); SpriteVertex.vColor = pElement->TextureColor.Current; - m_pManager->m_SpriteVertices.Add( SpriteVertex ); + m_pManager->m_SpriteVertices.push_back( SpriteVertex ); - SpriteVertex.vPos = D3DXVECTOR3( fRectLeft, fRectBottom, fDepth ); - SpriteVertex.vTex = D3DXVECTOR2( fTexLeft, fTexBottom ); + SpriteVertex.vPos = XMFLOAT3( fRectLeft, fRectBottom, fDepth ); + SpriteVertex.vTex = XMFLOAT2( fTexLeft, fTexBottom ); SpriteVertex.vColor = pElement->TextureColor.Current; - m_pManager->m_SpriteVertices.Add( SpriteVertex ); + m_pManager->m_SpriteVertices.push_back( SpriteVertex ); // Why are we drawing the sprite every time? This is very inefficient, but the sprite workaround doesn't have support for sorting now, so we have to // draw a sprite every time to keep the order correct between sprites and text. @@ -2490,298 +1721,28 @@ HRESULT CDXUTDialog::DrawSprite11( CDXUTElement* pElement, RECT* prcDest, float //-------------------------------------------------------------------------------------- -HRESULT CDXUTDialog::CalcTextRect( LPCWSTR strText, CDXUTElement* pElement, RECT* prcDest, int nCount ) +_Use_decl_annotations_ +HRESULT CDXUTDialog::CalcTextRect( LPCWSTR strText, CDXUTElement* pElement, const RECT* prcDest, int nCount ) { - HRESULT hr = S_OK; - - DXUTFontNode* pFontNode = GetFont( pElement->iFont ); - if( pFontNode == NULL ) + auto pFontNode = GetFont( pElement->iFont ); + if( !pFontNode ) return E_FAIL; - DWORD dwTextFormat = pElement->dwTextFormat | DT_CALCRECT; - // Since we are only computing the rectangle, we don't need a sprite. - if( pFontNode->pFont9 ) - { - hr = pFontNode->pFont9->DrawText( NULL, strText, nCount, prcDest, dwTextFormat, pElement->FontColor.Current ); - if( FAILED( hr ) ) - return hr; - } - - return S_OK; -} - - -//-------------------------------------------------------------------------------------- -HRESULT CDXUTDialog::DrawText( LPCWSTR strText, CDXUTElement* pElement, RECT* prcDest, bool bShadow, int nCount, bool bCenter ) -{ - if( m_pManager->GetD3D9Device() ) - return DrawText9( strText, pElement, prcDest, bShadow, nCount ); - else - return DrawText11( m_pManager->GetD3D11Device(), m_pManager->GetD3D11DeviceContext(), strText, pElement, prcDest, bShadow, nCount, bCenter ); -} - - -//-------------------------------------------------------------------------------------- -HRESULT CDXUTDialog::DrawText9( LPCWSTR strText, CDXUTElement* pElement, RECT* prcDest, bool bShadow, int nCount ) -{ - HRESULT hr = S_OK; - - // No need to draw fully transparent layers - if( pElement->FontColor.Current.a == 0 ) - return S_OK; - - RECT rcScreen = *prcDest; - OffsetRect( &rcScreen, m_x, m_y ); - - // If caption is enabled, offset the Y position by its height. - if( m_bCaption ) - OffsetRect( &rcScreen, 0, m_nCaptionHeight ); - - D3DXMATRIX matTransform; - D3DXMatrixIdentity( &matTransform ); - m_pManager->m_pSprite->SetTransform( &matTransform ); - - DXUTFontNode* pFontNode = GetFont( pElement->iFont ); - - if( bShadow ) - { - RECT rcShadow = rcScreen; - OffsetRect( &rcShadow, 1, 1 ); - hr = pFontNode->pFont9->DrawText( m_pManager->m_pSprite, strText, nCount, &rcShadow, pElement->dwTextFormat, - D3DCOLOR_ARGB( DWORD( pElement->FontColor.Current.a * 255 ), 0, 0, 0 ) ); - if( FAILED( hr ) ) - return hr; - } - - hr = pFontNode->pFont9->DrawText( m_pManager->m_pSprite, strText, nCount, &rcScreen, pElement->dwTextFormat, - pElement->FontColor.Current ); - if( FAILED( hr ) ) - return hr; + UNREFERENCED_PARAMETER(strText); + UNREFERENCED_PARAMETER(prcDest); + UNREFERENCED_PARAMETER(nCount); + // TODO - return S_OK; } -ID3D11Buffer* g_pFontBuffer11 = NULL; -UINT g_FontBufferBytes11 = 0; -CGrowableArray<DXUTSpriteVertex> g_FontVertices; -ID3D11ShaderResourceView* g_pFont11 = NULL; -ID3D11InputLayout* g_pInputLayout11 = NULL; -HRESULT InitFont11( ID3D11Device* pd3d11Device, ID3D11InputLayout* pInputLayout ) -{ - HRESULT hr = S_OK; - WCHAR str[MAX_PATH]; - V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, L"UI\\Font.dds" ) ); - - if (pd3d11Device->GetFeatureLevel() < D3D_FEATURE_LEVEL_10_0 ) { - - D3DX11_IMAGE_INFO dii; - D3DX11GetImageInfoFromFile( str, NULL, &dii, NULL ); - - D3DX11_IMAGE_LOAD_INFO dili; - dili.BindFlags = D3DX11_DEFAULT; - dili.CpuAccessFlags = D3DX11_DEFAULT; - dili.Depth = D3DX11_DEFAULT; - dili.Filter = D3DX11_DEFAULT; - dili.FirstMipLevel = 0; - dili.Format = DXGI_FORMAT_R8G8B8A8_UNORM; - dili.Height = D3DX11_DEFAULT; - dili.MipFilter = D3DX11_DEFAULT; - dili.MipLevels = 1; - dili.MiscFlags = D3DX11_DEFAULT; - dili.pSrcInfo = &dii; - dili.Usage = D3D11_USAGE_DEFAULT ; - dili.Width = D3DX11_DEFAULT; - - V_RETURN( D3DX11CreateShaderResourceViewFromFile( pd3d11Device, str, &dili, NULL, &g_pFont11, &hr) ); - } - else - { - V_RETURN( D3DX11CreateShaderResourceViewFromFile( pd3d11Device, str, NULL, NULL, &g_pFont11, &hr) ); - } - -#if defined(PROFILE) || defined(DEBUG) - if (g_pFont11) - { - ID3D11Resource *pRes = NULL; - g_pFont11->GetResource( &pRes ); - DXUT_SetDebugName( pRes, "DXUT Text11" ); - SAFE_RELEASE( pRes ); - } - - DXUT_SetDebugName( g_pFont11, "DXUT Text11" ); -#endif - - g_pInputLayout11 = pInputLayout; - return hr; -} - -void EndFont11() -{ - SAFE_RELEASE( g_pFontBuffer11 ); - g_FontBufferBytes11 = 0; - SAFE_RELEASE( g_pFont11 ); -} - -void BeginText11() -{ - g_FontVertices.Reset(); -} - -void DrawText11DXUT( ID3D11Device* pd3dDevice, ID3D11DeviceContext* pd3d11DeviceContext, - LPCWSTR strText, RECT rcScreen, D3DXCOLOR vFontColor, - float fBBWidth, float fBBHeight, bool bCenter ) -{ - float fCharTexSizeX = 0.010526315f; - //float fGlyphSizeX = 14.0f / fBBWidth; - //float fGlyphSizeY = 32.0f / fBBHeight; - float fGlyphSizeX = 15.0f / fBBWidth; - float fGlyphSizeY = 42.0f / fBBHeight; - - - float fRectLeft = rcScreen.left / fBBWidth; - float fRectTop = 1.0f - rcScreen.top / fBBHeight; - - fRectLeft = fRectLeft * 2.0f - 1.0f; - fRectTop = fRectTop * 2.0f - 1.0f; - - int NumChars = (int)wcslen( strText ); - if (bCenter) { - float fRectRight = rcScreen.right / fBBWidth; - fRectRight = fRectRight * 2.0f - 1.0f; - float fRectBottom = 1.0f - rcScreen.bottom / fBBHeight; - fRectBottom = fRectBottom * 2.0f - 1.0f; - float fcenterx = ((fRectRight - fRectLeft) - (float)NumChars*fGlyphSizeX) *0.5f; - float fcentery = ((fRectTop - fRectBottom) - (float)1*fGlyphSizeY) *0.5f; - fRectLeft += fcenterx ; - fRectTop -= fcentery; - } - float fOriginalLeft = fRectLeft; - float fTexTop = 0.0f; - float fTexBottom = 1.0f; - - float fDepth = 0.5f; - for( int i=0; i<NumChars; i++ ) - { - if( strText[i] == '\n' ) - { - fRectLeft = fOriginalLeft; - fRectTop -= fGlyphSizeY; - - continue; - } - else if( strText[i] < 32 || strText[i] > 126 ) - { - continue; - } - - // Add 6 sprite vertices - DXUTSpriteVertex SpriteVertex; - float fRectRight = fRectLeft + fGlyphSizeX; - float fRectBottom = fRectTop - fGlyphSizeY; - float fTexLeft = ( strText[i] - 32 ) * fCharTexSizeX; - float fTexRight = fTexLeft + fCharTexSizeX; - - // tri1 - SpriteVertex.vPos = D3DXVECTOR3( fRectLeft, fRectTop, fDepth ); - SpriteVertex.vTex = D3DXVECTOR2( fTexLeft, fTexTop ); - SpriteVertex.vColor = vFontColor; - g_FontVertices.Add( SpriteVertex ); - - SpriteVertex.vPos = D3DXVECTOR3( fRectRight, fRectTop, fDepth ); - SpriteVertex.vTex = D3DXVECTOR2( fTexRight, fTexTop ); - SpriteVertex.vColor = vFontColor; - g_FontVertices.Add( SpriteVertex ); - - SpriteVertex.vPos = D3DXVECTOR3( fRectLeft, fRectBottom, fDepth ); - SpriteVertex.vTex = D3DXVECTOR2( fTexLeft, fTexBottom ); - SpriteVertex.vColor = vFontColor; - g_FontVertices.Add( SpriteVertex ); - - // tri2 - SpriteVertex.vPos = D3DXVECTOR3( fRectRight, fRectTop, fDepth ); - SpriteVertex.vTex = D3DXVECTOR2( fTexRight, fTexTop ); - SpriteVertex.vColor = vFontColor; - g_FontVertices.Add( SpriteVertex ); - - SpriteVertex.vPos = D3DXVECTOR3( fRectRight, fRectBottom, fDepth ); - SpriteVertex.vTex = D3DXVECTOR2( fTexRight, fTexBottom ); - SpriteVertex.vColor = vFontColor; - g_FontVertices.Add( SpriteVertex ); - - SpriteVertex.vPos = D3DXVECTOR3( fRectLeft, fRectBottom, fDepth ); - SpriteVertex.vTex = D3DXVECTOR2( fTexLeft, fTexBottom ); - SpriteVertex.vColor = vFontColor; - g_FontVertices.Add( SpriteVertex ); - - fRectLeft += fGlyphSizeX; - - } - - // We have to end text after every line so that rendering order between sprites and fonts is preserved - EndText11( pd3dDevice, pd3d11DeviceContext ); -} - -void EndText11( ID3D11Device* pd3dDevice, ID3D11DeviceContext* pd3d11DeviceContext ) -{ - - // ensure our buffer size can hold our sprites - UINT FontDataBytes = g_FontVertices.GetSize() * sizeof( DXUTSpriteVertex ); - if( g_FontBufferBytes11 < FontDataBytes ) - { - SAFE_RELEASE( g_pFontBuffer11 ); - g_FontBufferBytes11 = FontDataBytes; - - D3D11_BUFFER_DESC BufferDesc; - BufferDesc.ByteWidth = g_FontBufferBytes11; - BufferDesc.Usage = D3D11_USAGE_DYNAMIC; - BufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; - BufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; - BufferDesc.MiscFlags = 0; - - pd3dDevice->CreateBuffer( &BufferDesc, NULL, &g_pFontBuffer11 ); - DXUT_SetDebugName( g_pFontBuffer11, "DXUT Text11" ); - } - - // Copy the sprites over - D3D11_BOX destRegion; - destRegion.left = 0; - destRegion.right = FontDataBytes; - destRegion.top = 0; - destRegion.bottom = 1; - destRegion.front = 0; - destRegion.back = 1; - D3D11_MAPPED_SUBRESOURCE MappedResource; - if ( S_OK == pd3d11DeviceContext->Map( g_pFontBuffer11, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedResource ) ) { - CopyMemory( MappedResource.pData, (void*)g_FontVertices.GetData(), FontDataBytes ); - pd3d11DeviceContext->Unmap(g_pFontBuffer11, 0); - } - - ID3D11ShaderResourceView* pOldTexture = NULL; - pd3d11DeviceContext->PSGetShaderResources( 0, 1, &pOldTexture ); - pd3d11DeviceContext->PSSetShaderResources( 0, 1, &g_pFont11 ); - - // Draw - UINT Stride = sizeof( DXUTSpriteVertex ); - UINT Offset = 0; - pd3d11DeviceContext->IASetVertexBuffers( 0, 1, &g_pFontBuffer11, &Stride, &Offset ); - pd3d11DeviceContext->IASetInputLayout( g_pInputLayout11 ); - pd3d11DeviceContext->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST ); - pd3d11DeviceContext->Draw( g_FontVertices.GetSize(), 0 ); - - pd3d11DeviceContext->PSSetShaderResources( 0, 1, &pOldTexture ); - SAFE_RELEASE( pOldTexture ); - - g_FontVertices.Reset(); -} //-------------------------------------------------------------------------------------- -HRESULT CDXUTDialog::DrawText11( ID3D11Device* pd3dDevice, ID3D11DeviceContext* pd3d11DeviceContext, - LPCWSTR strText, CDXUTElement* pElement, RECT* prcDest, bool bShadow, int nCount, bool bCenter ) +_Use_decl_annotations_ +HRESULT CDXUTDialog::DrawText( LPCWSTR strText, CDXUTElement* pElement, const RECT* prcDest, bool bShadow, bool bCenter ) { - //HRESULT hr = S_OK; - // No need to draw fully transparent layers - if( pElement->FontColor.Current.a == 0 ) + if( pElement->FontColor.Current.w == 0 ) return S_OK; RECT rcScreen = *prcDest; @@ -2794,19 +1755,22 @@ HRESULT CDXUTDialog::DrawText11( ID3D11Device* pd3dDevice, ID3D11DeviceContext* float fBBWidth = ( float )m_pManager->m_nBackBufferWidth; float fBBHeight = ( float )m_pManager->m_nBackBufferHeight; + auto pd3dDevice = m_pManager->GetD3D11Device(); + auto pd3d11DeviceContext = m_pManager->GetD3D11DeviceContext(); + if( bShadow ) { RECT rcShadow = rcScreen; OffsetRect( &rcShadow, 1, 1 ); - D3DXCOLOR vShadowColor( 0,0,0, 1.0f ); + XMFLOAT4 vShadowColor( 0,0,0, 1.0f ); DrawText11DXUT( pd3dDevice, pd3d11DeviceContext, strText, rcShadow, vShadowColor, fBBWidth, fBBHeight, bCenter ); } - D3DXCOLOR vFontColor( pElement->FontColor.Current.r, pElement->FontColor.Current.g, pElement->FontColor.Current.b, 1.0f ); + XMFLOAT4 vFontColor( pElement->FontColor.Current.x, pElement->FontColor.Current.y, pElement->FontColor.Current.z, 1.0f ); DrawText11DXUT( pd3dDevice, pd3d11DeviceContext, strText, rcScreen, vFontColor, fBBWidth, fBBHeight, bCenter ); @@ -2816,8 +1780,9 @@ HRESULT CDXUTDialog::DrawText11( ID3D11Device* pd3dDevice, ID3D11DeviceContext* //-------------------------------------------------------------------------------------- -void CDXUTDialog::SetBackgroundColors( D3DCOLOR colorTopLeft, D3DCOLOR colorTopRight, D3DCOLOR colorBottomLeft, - D3DCOLOR colorBottomRight ) +_Use_decl_annotations_ +void CDXUTDialog::SetBackgroundColors( DWORD colorTopLeft, DWORD colorTopRight, DWORD colorBottomLeft, + DWORD colorBottomRight ) { m_colorTopLeft = colorTopLeft; m_colorTopRight = colorTopRight; @@ -2827,9 +1792,9 @@ void CDXUTDialog::SetBackgroundColors( D3DCOLOR colorTopLeft, D3DCOLOR colorTopR //-------------------------------------------------------------------------------------- -void CDXUTDialog::SetNextDialog( CDXUTDialog* pNextDialog ) +void CDXUTDialog::SetNextDialog( _In_ CDXUTDialog* pNextDialog ) { - if( pNextDialog == NULL ) + if( !pNextDialog ) pNextDialog = this; m_pNextDialog = pNextDialog; @@ -2844,7 +1809,7 @@ void CDXUTDialog::ClearFocus() if( s_pControlFocus ) { s_pControlFocus->OnFocusOut(); - s_pControlFocus = NULL; + s_pControlFocus = nullptr; } ReleaseCapture(); @@ -2855,16 +1820,15 @@ void CDXUTDialog::ClearFocus() void CDXUTDialog::FocusDefaultControl() { // Check for default control in this dialog - for( int i = 0; i < m_Controls.GetSize(); i++ ) + for( auto it = m_Controls.cbegin(); it != m_Controls.cend(); ++it ) { - CDXUTControl* pControl = m_Controls.GetAt( i ); - if( pControl->m_bIsDefault ) + if( (*it)->m_bIsDefault ) { // Remove focus from the current control ClearFocus(); // Give focus to the default control - s_pControlFocus = pControl; + s_pControlFocus = *it; s_pControlFocus->OnFocusIn(); return; } @@ -2873,15 +1837,15 @@ void CDXUTDialog::FocusDefaultControl() //-------------------------------------------------------------------------------------- -bool CDXUTDialog::OnCycleFocus( bool bForward ) +bool CDXUTDialog::OnCycleFocus( _In_ bool bForward ) { - CDXUTControl* pControl = NULL; - CDXUTDialog* pDialog = NULL; // pDialog and pLastDialog are used to track wrapping of + CDXUTControl* pControl = nullptr; + CDXUTDialog* pDialog = nullptr; // pDialog and pLastDialog are used to track wrapping of CDXUTDialog* pLastDialog; // focus from first control to last or vice versa. - if( s_pControlFocus == NULL ) + if( !s_pControlFocus ) { - // If s_pControlFocus is NULL, we focus the first control of first dialog in + // If s_pControlFocus is nullptr, we focus the first control of first dialog in // the case that bForward is true, and focus the last control of last dialog when // bForward is false. // @@ -2889,12 +1853,12 @@ bool CDXUTDialog::OnCycleFocus( bool bForward ) { // Search for the first control from the start of the dialog // array. - for( int d = 0; d < m_pManager->m_Dialogs.GetSize(); ++d ) + for( auto it = m_pManager->m_Dialogs.cbegin(); it != m_pManager->m_Dialogs.cend(); ++it ) { - pDialog = pLastDialog = m_pManager->m_Dialogs.GetAt( d ); - if( pDialog && pDialog->m_Controls.GetSize() > 0 ) + pDialog = pLastDialog = *it; + if( pDialog && !pDialog->m_Controls.empty() ) { - pControl = pDialog->m_Controls.GetAt( 0 ); + pControl = pDialog->m_Controls[ 0 ]; break; } } @@ -2910,12 +1874,12 @@ bool CDXUTDialog::OnCycleFocus( bool bForward ) { // Search for the first control from the end of the dialog // array. - for( int d = m_pManager->m_Dialogs.GetSize() - 1; d >= 0; --d ) + for( auto it = m_pManager->m_Dialogs.crbegin(); it != m_pManager->m_Dialogs.crend(); ++it ) { - pDialog = pLastDialog = m_pManager->m_Dialogs.GetAt( d ); - if( pDialog && pDialog->m_Controls.GetSize() > 0 ) + pDialog = pLastDialog = *it; + if( pDialog && !pDialog->m_Controls.empty() ) { - pControl = pDialog->m_Controls.GetAt( pDialog->m_Controls.GetSize() - 1 ); + pControl = pDialog->m_Controls[ pDialog->m_Controls.size() - 1 ]; break; } } @@ -2939,24 +1903,41 @@ bool CDXUTDialog::OnCycleFocus( bool bForward ) { // Focused control belongs to this dialog. Cycle to the // next/previous control. + assert( pControl != 0 ); + _Analysis_assume_( pControl != 0 ); pLastDialog = s_pControlFocus->m_pDialog; pControl = ( bForward ) ? GetNextControl( s_pControlFocus ) : GetPrevControl( s_pControlFocus ); pDialog = pControl->m_pDialog; } + assert( pControl != 0 ); + _Analysis_assume_( pControl != 0 ); + for( int i = 0; i < 0xffff; i++ ) { // If we just wrapped from last control to first or vice versa, - // set the focused control to NULL. This state, where no control + // set the focused control to nullptr. This state, where no control // has focus, allows the camera to work. - int nLastDialogIndex = m_pManager->m_Dialogs.IndexOf( pLastDialog ); - int nDialogIndex = m_pManager->m_Dialogs.IndexOf( pDialog ); + int nLastDialogIndex = -1; + auto fit = std::find( m_pManager->m_Dialogs.cbegin(), m_pManager->m_Dialogs.cend(), pLastDialog ); + if ( fit != m_pManager->m_Dialogs.cend() ) + { + nLastDialogIndex = int( fit - m_pManager->m_Dialogs.begin() ); + } + + int nDialogIndex = -1; + fit = std::find( m_pManager->m_Dialogs.cbegin(), m_pManager->m_Dialogs.cend(), pDialog ); + if ( fit != m_pManager->m_Dialogs.cend() ) + { + nDialogIndex = int( fit - m_pManager->m_Dialogs.begin() ); + } + if( ( !bForward && nLastDialogIndex < nDialogIndex ) || ( bForward && nDialogIndex < nLastDialogIndex ) ) { if( s_pControlFocus ) s_pControlFocus->OnFocusOut(); - s_pControlFocus = NULL; + s_pControlFocus = nullptr; return true; } @@ -2971,6 +1952,7 @@ bool CDXUTDialog::OnCycleFocus( bool bForward ) if( s_pControlFocus ) s_pControlFocus->OnFocusOut(); s_pControlFocus = pControl; + if( s_pControlFocus ) s_pControlFocus->OnFocusIn(); return true; } @@ -2987,198 +1969,6 @@ bool CDXUTDialog::OnCycleFocus( bool bForward ) //-------------------------------------------------------------------------------------- -HRESULT CDXUTDialogResourceManager::CreateFont9( UINT iFont ) -{ - HRESULT hr = S_OK; - - DXUTFontNode* pFontNode = m_FontCache.GetAt( iFont ); - - SAFE_RELEASE( pFontNode->pFont9 ); - - V_RETURN( D3DXCreateFont( m_pd3d9Device, pFontNode->nHeight, 0, pFontNode->nWeight, 1, FALSE, DEFAULT_CHARSET, - OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, - pFontNode->strFace, &pFontNode->pFont9 ) ); - - return S_OK; -} - - - -//-------------------------------------------------------------------------------------- -HRESULT CDXUTDialogResourceManager::CreateFont11( UINT iFont ) -{ - return S_OK; -} - - -//-------------------------------------------------------------------------------------- -HRESULT CDXUTDialogResourceManager::CreateTexture9( UINT iTexture ) -{ - HRESULT hr = S_OK; - - DXUTTextureNode* pTextureNode = m_TextureCache.GetAt( iTexture ); - - - D3DXIMAGE_INFO info; - - if( !pTextureNode->bFileSource ) - { - if( pTextureNode->nResourceID == 0xFFFF && pTextureNode->hResourceModule == ( HMODULE )0xFFFF ) - { - hr = DXUTCreateGUITextureFromInternalArray9( m_pd3d9Device, &pTextureNode->pTexture9, &info ); - if( FAILED( hr ) ) - return DXTRACE_ERR( L"D3DXCreateTextureFromFileInMemoryEx", hr ); - DXUT_SetDebugName( pTextureNode->pTexture9, "DXUT GUI Texture" ); - } - else - { - LPCWSTR pID = pTextureNode->nResourceID ? ( LPCWSTR )( size_t )pTextureNode->nResourceID : - pTextureNode->strFilename; - - // Create texture from resource - hr = D3DXCreateTextureFromResourceEx( m_pd3d9Device, pTextureNode->hResourceModule, pID, D3DX_DEFAULT, - D3DX_DEFAULT, - 1, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, - D3DX_DEFAULT, D3DX_DEFAULT, 0, - &info, NULL, &pTextureNode->pTexture9 ); - if( FAILED( hr ) ) - return DXTRACE_ERR( L"D3DXCreateTextureFromResourceEx", hr ); - } - } - else - { - // Make sure there's a texture to create - if( pTextureNode->strFilename[0] == 0 ) - return S_OK; - - // Create texture from file - hr = D3DXCreateTextureFromFileEx( m_pd3d9Device, pTextureNode->strFilename, D3DX_DEFAULT, D3DX_DEFAULT, - 1, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, - D3DX_DEFAULT, D3DX_DEFAULT, 0, - &info, NULL, &pTextureNode->pTexture9 ); - if( FAILED( hr ) ) - { - return DXTRACE_ERR( L"D3DXCreateTextureFromFileEx", hr ); - } - } - - // Store dimensions - pTextureNode->dwWidth = info.Width; - pTextureNode->dwHeight = info.Height; - - return S_OK; -} - - -//-------------------------------------------------------------------------------------- -HRESULT CDXUTDialogResourceManager::CreateTexture11( UINT iTexture ) -{ - HRESULT hr = S_OK; - - DXUTTextureNode* pTextureNode = m_TextureCache.GetAt( iTexture ); - - if( !pTextureNode->bFileSource ) - { - if( pTextureNode->nResourceID == 0xFFFF && pTextureNode->hResourceModule == ( HMODULE )0xFFFF ) - { - hr = DXUTCreateGUITextureFromInternalArray11( m_pd3d11Device, &pTextureNode->pTexture11, NULL ); - if( FAILED( hr ) ) - return DXTRACE_ERR( L"D3DX11CreateResourceFromFileInMemory", hr ); - DXUT_SetDebugName( pTextureNode->pTexture11, "DXUT GUI Texture" ); - } - //else - //{ - // LPCWSTR pID = pTextureNode->nResourceID ? ( LPCWSTR )( size_t )pTextureNode->nResourceID : - // pTextureNode->strFilename; - - // D3DX10_IMAGE_INFO SrcInfo; - // D3DX10GetImageInfoFromResource( NULL, pID, NULL, &SrcInfo, NULL ); - - // // Create texture from resource - // ID3D10Resource* pRes; - // D3DX10_IMAGE_LOAD_INFO loadInfo; - // loadInfo.Width = D3DX10_DEFAULT; - // loadInfo.Height = D3DX10_DEFAULT; - // loadInfo.Depth = D3DX10_DEFAULT; - // loadInfo.FirstMipLevel = 0; - // loadInfo.MipLevels = 1; - // loadInfo.Usage = D3D10_USAGE_DEFAULT; - // loadInfo.BindFlags = D3D10_BIND_SHADER_RESOURCE; - // loadInfo.CpuAccessFlags = 0; - // loadInfo.MiscFlags = 0; - // loadInfo.Format = MAKE_TYPELESS( SrcInfo.Format ); - // loadInfo.Filter = D3DX10_FILTER_NONE; - // loadInfo.MipFilter = D3DX10_FILTER_NONE; - // loadInfo.pSrcInfo = &SrcInfo; - - // hr = D3DX10CreateTextureFromResource( m_pd3d10Device, pTextureNode->hResourceModule, pID, &loadInfo, - // NULL, &pRes, NULL ); - // if( FAILED( hr ) ) - // return DXTRACE_ERR( L"D3DX10CreateResourceFromResource", hr ); - // hr = pRes->QueryInterface( __uuidof( ID3D10Texture2D ), ( LPVOID* )&pTextureNode->pTexture10 ); - // SAFE_RELEASE( pRes ); - // if( FAILED( hr ) ) - // return hr; - //} - } - else - { - // - //// Make sure there's a texture to create - //if( pTextureNode->strFilename[0] == 0 ) - // return S_OK; - - //D3DX10_IMAGE_INFO SrcInfo; - //D3DX10GetImageInfoFromFile( pTextureNode->strFilename, NULL, &SrcInfo, NULL ); - - //// Create texture from file - //ID3D10Resource* pRes; - //D3DX10_IMAGE_LOAD_INFO loadInfo; - //loadInfo.Width = D3DX10_DEFAULT; - //loadInfo.Height = D3DX10_DEFAULT; - //loadInfo.Depth = D3DX10_DEFAULT; - //loadInfo.FirstMipLevel = 0; - //loadInfo.MipLevels = 1; - //loadInfo.Usage = D3D10_USAGE_DEFAULT; - //loadInfo.BindFlags = D3D10_BIND_SHADER_RESOURCE; - //loadInfo.CpuAccessFlags = 0; - //loadInfo.MiscFlags = 0; - //loadInfo.Format = MAKE_TYPELESS( SrcInfo.Format ); - //loadInfo.Filter = D3DX10_FILTER_NONE; - //loadInfo.MipFilter = D3DX10_FILTER_NONE; - //loadInfo.pSrcInfo = &SrcInfo; - //hr = D3DX10CreateTextureFromFile( m_pd3d10Device, pTextureNode->strFilename, &loadInfo, NULL, &pRes, NULL ); - //if( FAILED( hr ) ) - //{ - // return DXTRACE_ERR( L"D3DX10CreateResourceFromFileEx", hr ); - //} - //hr = pRes->QueryInterface( __uuidof( ID3D10Texture2D ), ( LPVOID* )&pTextureNode->pTexture10 ); - //SAFE_RELEASE( pRes ); - //if( FAILED( hr ) ) - // return hr; - // - } - - // Store dimensions - D3D11_TEXTURE2D_DESC desc; - pTextureNode->pTexture11->GetDesc( &desc ); - pTextureNode->dwWidth = desc.Width; - pTextureNode->dwHeight = desc.Height; - - // Create resource view - D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc; - SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; - SRVDesc.Format = MAKE_SRGB( desc.Format ); - SRVDesc.Texture2D.MipLevels = 1; - SRVDesc.Texture2D.MostDetailedMip = 0; - hr = m_pd3d11Device->CreateShaderResourceView( pTextureNode->pTexture11, &SRVDesc, &pTextureNode->pTexResView11 ); - DXUT_SetDebugName( pTextureNode->pTexResView11, "DXUT GUI Texture" ); - - return hr; -} - - -//-------------------------------------------------------------------------------------- void CDXUTDialog::InitDefaultElements() { SetFont( 0, L"Arial", 14, FW_NORMAL ); @@ -3474,19 +2264,612 @@ void CDXUTDialog::InitDefaultElements() } +//====================================================================================== +// CDXUTDialogResourceManager +//====================================================================================== //-------------------------------------------------------------------------------------- -// CDXUTControl class +CDXUTDialogResourceManager::CDXUTDialogResourceManager() : + m_pVSRenderUI11(nullptr), + m_pPSRenderUI11(nullptr), + m_pPSRenderUIUntex11(nullptr), + m_pDepthStencilStateUI11(nullptr), + m_pRasterizerStateUI11(nullptr), + m_pBlendStateUI11(nullptr), + m_pSamplerStateUI11(nullptr), + m_pDepthStencilStateStored11(nullptr), + m_pRasterizerStateStored11(nullptr), + m_pBlendStateStored11(nullptr), + m_pSamplerStateStored11(nullptr), + m_pInputLayout11(nullptr), + m_pVBScreenQuad11(nullptr), + m_pSpriteBuffer11(nullptr), + m_SpriteBufferBytes11(0) +{ +} + + +//-------------------------------------------------------------------------------------- +CDXUTDialogResourceManager::~CDXUTDialogResourceManager() +{ + for( auto it = m_FontCache.begin(); it != m_FontCache.end(); ++it ) + { + SAFE_DELETE( *it ); + } + m_FontCache.clear(); + + for( auto it = m_TextureCache.begin(); it != m_TextureCache.end(); ++it ) + { + SAFE_DELETE( *it ); + } + m_TextureCache.clear(); +} + + +//-------------------------------------------------------------------------------------- +_Use_decl_annotations_ +bool CDXUTDialogResourceManager::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) +{ + UNREFERENCED_PARAMETER(hWnd); + UNREFERENCED_PARAMETER(uMsg); + UNREFERENCED_PARAMETER(wParam); + UNREFERENCED_PARAMETER(lParam); + return false; +} + + +_Use_decl_annotations_ +HRESULT CDXUTDialogResourceManager::OnD3D11CreateDevice( ID3D11Device* pd3dDevice, ID3D11DeviceContext* pd3d11DeviceContext ) +{ + m_pd3d11Device = pd3dDevice; + m_pd3d11DeviceContext = pd3d11DeviceContext; + + HRESULT hr = S_OK; + + // Compile Shaders + ID3DBlob* pVSBlob = nullptr; + ID3DBlob* pPSBlob = nullptr; + ID3DBlob* pPSUntexBlob = nullptr; + V_RETURN( D3DCompile( g_strUIEffectFile, g_uUIEffectFileSize, "none", nullptr, nullptr, "VS", "vs_4_0_level_9_1", + D3D10_SHADER_ENABLE_BACKWARDS_COMPATIBILITY, 0, &pVSBlob, nullptr ) ); + V_RETURN( D3DCompile( g_strUIEffectFile, g_uUIEffectFileSize, "none", nullptr, nullptr, "PS", "ps_4_0_level_9_1", + D3D10_SHADER_ENABLE_BACKWARDS_COMPATIBILITY, 0, &pPSBlob, nullptr ) ); + V_RETURN( D3DCompile( g_strUIEffectFile, g_uUIEffectFileSize, "none", nullptr, nullptr, "PSUntex", "ps_4_0_level_9_1", + D3D10_SHADER_ENABLE_BACKWARDS_COMPATIBILITY, 0, &pPSUntexBlob, nullptr ) ); + + // Create Shaders + V_RETURN( pd3dDevice->CreateVertexShader( pVSBlob->GetBufferPointer(), pVSBlob->GetBufferSize(), nullptr, &m_pVSRenderUI11 ) ); + DXUT_SetDebugName( m_pVSRenderUI11, "CDXUTDialogResourceManager" ); + + V_RETURN( pd3dDevice->CreatePixelShader( pPSBlob->GetBufferPointer(), pPSBlob->GetBufferSize(), nullptr, &m_pPSRenderUI11 ) ); + DXUT_SetDebugName( m_pPSRenderUI11, "CDXUTDialogResourceManager" ); + + V_RETURN( pd3dDevice->CreatePixelShader( pPSUntexBlob->GetBufferPointer(), pPSUntexBlob->GetBufferSize(), nullptr, &m_pPSRenderUIUntex11 ) ); + DXUT_SetDebugName( m_pPSRenderUIUntex11, "CDXUTDialogResourceManager" ); + + // States + D3D11_DEPTH_STENCIL_DESC DSDesc; + ZeroMemory( &DSDesc, sizeof( D3D11_DEPTH_STENCIL_DESC ) ); + DSDesc.DepthEnable = FALSE; + DSDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL; + DSDesc.DepthFunc = D3D11_COMPARISON_LESS; + DSDesc.StencilEnable = FALSE; + V_RETURN( pd3dDevice->CreateDepthStencilState( &DSDesc, &m_pDepthStencilStateUI11 ) ); + DXUT_SetDebugName( m_pDepthStencilStateUI11, "CDXUTDialogResourceManager" ); + + D3D11_RASTERIZER_DESC RSDesc; + RSDesc.AntialiasedLineEnable = FALSE; + RSDesc.CullMode = D3D11_CULL_BACK; + RSDesc.DepthBias = 0; + RSDesc.DepthBiasClamp = 0.0f; + RSDesc.DepthClipEnable = TRUE; + RSDesc.FillMode = D3D11_FILL_SOLID; + RSDesc.FrontCounterClockwise = FALSE; + RSDesc.MultisampleEnable = TRUE; + RSDesc.ScissorEnable = FALSE; + RSDesc.SlopeScaledDepthBias = 0.0f; + V_RETURN( pd3dDevice->CreateRasterizerState( &RSDesc, &m_pRasterizerStateUI11 ) ); + DXUT_SetDebugName( m_pRasterizerStateUI11, "CDXUTDialogResourceManager" ); + + D3D11_BLEND_DESC BSDesc; + ZeroMemory( &BSDesc, sizeof( D3D11_BLEND_DESC ) ); + + BSDesc.RenderTarget[0].BlendEnable = TRUE; + BSDesc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA; + BSDesc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA; + BSDesc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD; + BSDesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE; + BSDesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO; + BSDesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD; + BSDesc.RenderTarget[0].RenderTargetWriteMask = 0x0F; + + V_RETURN( pd3dDevice->CreateBlendState( &BSDesc, &m_pBlendStateUI11 ) ); + DXUT_SetDebugName( m_pBlendStateUI11, "CDXUTDialogResourceManager" ); + + D3D11_SAMPLER_DESC SSDesc; + ZeroMemory( &SSDesc, sizeof( D3D11_SAMPLER_DESC ) ); + SSDesc.Filter = D3D11_FILTER_ANISOTROPIC ; + SSDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP; + SSDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP; + SSDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; + SSDesc.ComparisonFunc = D3D11_COMPARISON_NEVER; + SSDesc.MaxAnisotropy = 16; + SSDesc.MinLOD = 0; + SSDesc.MaxLOD = D3D11_FLOAT32_MAX; + if ( pd3dDevice->GetFeatureLevel() < D3D_FEATURE_LEVEL_9_3 ) + { + SSDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; + SSDesc.MaxAnisotropy = 0; + } + V_RETURN( pd3dDevice->CreateSamplerState( &SSDesc, &m_pSamplerStateUI11 ) ); + DXUT_SetDebugName( m_pSamplerStateUI11, "CDXUTDialogResourceManager" ); + + // Create the texture objects in the cache arrays. + for( size_t i = 0; i < m_TextureCache.size(); i++ ) + { + hr = CreateTexture11( static_cast<UINT>( i ) ); + if( FAILED( hr ) ) + return hr; + } + + // Create input layout + const D3D11_INPUT_ELEMENT_DESC layout[] = + { + { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, + { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }, + { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 28, D3D11_INPUT_PER_VERTEX_DATA, 0 }, + }; + + V_RETURN( pd3dDevice->CreateInputLayout( layout, ARRAYSIZE( layout ), pVSBlob->GetBufferPointer(), pVSBlob->GetBufferSize(), &m_pInputLayout11 ) ); + DXUT_SetDebugName( m_pInputLayout11, "CDXUTDialogResourceManager" ); + + // Release the blobs + SAFE_RELEASE( pVSBlob ); + SAFE_RELEASE( pPSBlob ); + SAFE_RELEASE( pPSUntexBlob ); + + // Create a vertex buffer quad for rendering later + D3D11_BUFFER_DESC BufDesc; + BufDesc.ByteWidth = sizeof( DXUT_SCREEN_VERTEX_10 ) * 4; + BufDesc.Usage = D3D11_USAGE_DYNAMIC; + BufDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; + BufDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; + BufDesc.MiscFlags = 0; + V_RETURN( pd3dDevice->CreateBuffer( &BufDesc, nullptr, &m_pVBScreenQuad11 ) ); + DXUT_SetDebugName( m_pVBScreenQuad11, "CDXUTDialogResourceManager" ); + + // Init the D3D11 font + InitFont11( pd3dDevice, m_pInputLayout11 ); + + return S_OK; +} + + //-------------------------------------------------------------------------------------- +_Use_decl_annotations_ +HRESULT CDXUTDialogResourceManager::OnD3D11ResizedSwapChain( ID3D11Device* pd3dDevice, + const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc ) +{ + UNREFERENCED_PARAMETER(pd3dDevice); + + HRESULT hr = S_OK; + + m_nBackBufferWidth = pBackBufferSurfaceDesc->Width; + m_nBackBufferHeight = pBackBufferSurfaceDesc->Height; + + return hr; +} + //-------------------------------------------------------------------------------------- -CDXUTControl::CDXUTControl( CDXUTDialog* pDialog ) +void CDXUTDialogResourceManager::OnD3D11ReleasingSwapChain() +{ +} + + +//-------------------------------------------------------------------------------------- +void CDXUTDialogResourceManager::OnD3D11DestroyDevice() +{ + // Release the resources but don't clear the cache, as these will need to be + // recreated if the device is recreated + + for( auto it = m_TextureCache.begin(); it != m_TextureCache.end(); ++it ) + { + SAFE_RELEASE( (*it)->pTexResView11 ); + SAFE_RELEASE( (*it)->pTexture11 ); + } + + // D3D11 + SAFE_RELEASE( m_pVBScreenQuad11 ); + SAFE_RELEASE( m_pSpriteBuffer11 ); + m_SpriteBufferBytes11 = 0; + SAFE_RELEASE( m_pInputLayout11 ); + + // Shaders + SAFE_RELEASE( m_pVSRenderUI11 ); + SAFE_RELEASE( m_pPSRenderUI11 ); + SAFE_RELEASE( m_pPSRenderUIUntex11 ); + + // States + SAFE_RELEASE( m_pDepthStencilStateUI11 ); + SAFE_RELEASE( m_pRasterizerStateUI11 ); + SAFE_RELEASE( m_pBlendStateUI11 ); + SAFE_RELEASE( m_pSamplerStateUI11 ); + + SAFE_RELEASE( m_pDepthStencilStateStored11 ); + SAFE_RELEASE( m_pRasterizerStateStored11 ); + SAFE_RELEASE( m_pBlendStateStored11 ); + SAFE_RELEASE( m_pSamplerStateStored11 ); + + EndFont11(); +} + + +//-------------------------------------------------------------------------------------- +void CDXUTDialogResourceManager::StoreD3D11State( _In_ ID3D11DeviceContext* pd3dImmediateContext ) +{ + pd3dImmediateContext->OMGetDepthStencilState( &m_pDepthStencilStateStored11, &m_StencilRefStored11 ); + pd3dImmediateContext->RSGetState( &m_pRasterizerStateStored11 ); + pd3dImmediateContext->OMGetBlendState( &m_pBlendStateStored11, m_BlendFactorStored11, &m_SampleMaskStored11 ); + pd3dImmediateContext->PSGetSamplers( 0, 1, &m_pSamplerStateStored11 ); +} + + +//-------------------------------------------------------------------------------------- +void CDXUTDialogResourceManager::RestoreD3D11State( _In_ ID3D11DeviceContext* pd3dImmediateContext ) +{ + pd3dImmediateContext->OMSetDepthStencilState( m_pDepthStencilStateStored11, m_StencilRefStored11 ); + pd3dImmediateContext->RSSetState( m_pRasterizerStateStored11 ); + pd3dImmediateContext->OMSetBlendState( m_pBlendStateStored11, m_BlendFactorStored11, m_SampleMaskStored11 ); + pd3dImmediateContext->PSSetSamplers( 0, 1, &m_pSamplerStateStored11 ); + + SAFE_RELEASE( m_pDepthStencilStateStored11 ); + SAFE_RELEASE( m_pRasterizerStateStored11 ); + SAFE_RELEASE( m_pBlendStateStored11 ); + SAFE_RELEASE( m_pSamplerStateStored11 ); +} + + +//-------------------------------------------------------------------------------------- +void CDXUTDialogResourceManager::ApplyRenderUI11( _In_ ID3D11DeviceContext* pd3dImmediateContext ) +{ + // Shaders + pd3dImmediateContext->VSSetShader( m_pVSRenderUI11, nullptr, 0 ); + pd3dImmediateContext->HSSetShader( nullptr, nullptr, 0 ); + pd3dImmediateContext->DSSetShader( nullptr, nullptr, 0 ); + pd3dImmediateContext->GSSetShader( nullptr, nullptr, 0 ); + pd3dImmediateContext->PSSetShader( m_pPSRenderUI11, nullptr, 0 ); + + // States + pd3dImmediateContext->OMSetDepthStencilState( m_pDepthStencilStateUI11, 0 ); + pd3dImmediateContext->RSSetState( m_pRasterizerStateUI11 ); + float BlendFactor[4] = { 0, 0, 0, 0 }; + pd3dImmediateContext->OMSetBlendState( m_pBlendStateUI11, BlendFactor, 0xFFFFFFFF ); + pd3dImmediateContext->PSSetSamplers( 0, 1, &m_pSamplerStateUI11 ); +} + + +//-------------------------------------------------------------------------------------- +void CDXUTDialogResourceManager::ApplyRenderUIUntex11( _In_ ID3D11DeviceContext* pd3dImmediateContext ) +{ + // Shaders + pd3dImmediateContext->VSSetShader( m_pVSRenderUI11, nullptr, 0 ); + pd3dImmediateContext->HSSetShader( nullptr, nullptr, 0 ); + pd3dImmediateContext->DSSetShader( nullptr, nullptr, 0 ); + pd3dImmediateContext->GSSetShader( nullptr, nullptr, 0 ); + pd3dImmediateContext->PSSetShader( m_pPSRenderUIUntex11, nullptr, 0 ); + + // States + pd3dImmediateContext->OMSetDepthStencilState( m_pDepthStencilStateUI11, 0 ); + pd3dImmediateContext->RSSetState( m_pRasterizerStateUI11 ); + float BlendFactor[4] = { 0, 0, 0, 0 }; + pd3dImmediateContext->OMSetBlendState( m_pBlendStateUI11, BlendFactor, 0xFFFFFFFF ); + pd3dImmediateContext->PSSetSamplers( 0, 1, &m_pSamplerStateUI11 ); +} + + +//-------------------------------------------------------------------------------------- +void CDXUTDialogResourceManager::BeginSprites11( ) +{ + m_SpriteVertices.clear(); +} + + +//-------------------------------------------------------------------------------------- +_Use_decl_annotations_ +void CDXUTDialogResourceManager::EndSprites11( ID3D11Device* pd3dDevice, ID3D11DeviceContext* pd3dImmediateContext ) +{ + + // ensure our buffer size can hold our sprites + UINT SpriteDataBytes = static_cast<UINT>( m_SpriteVertices.size() * sizeof( DXUTSpriteVertex ) ); + if( m_SpriteBufferBytes11 < SpriteDataBytes ) + { + SAFE_RELEASE( m_pSpriteBuffer11 ); + m_SpriteBufferBytes11 = SpriteDataBytes; + + D3D11_BUFFER_DESC BufferDesc; + BufferDesc.ByteWidth = m_SpriteBufferBytes11; + BufferDesc.Usage = D3D11_USAGE_DYNAMIC; + BufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; + BufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; + BufferDesc.MiscFlags = 0; + + if ( FAILED(pd3dDevice->CreateBuffer(&BufferDesc, nullptr, &m_pSpriteBuffer11)) ) + { + m_pSpriteBuffer11 = nullptr; + m_SpriteBufferBytes11 = 0; + return; + } + DXUT_SetDebugName( m_pSpriteBuffer11, "CDXUTDialogResourceManager" ); + } + + // Copy the sprites over + D3D11_BOX destRegion; + destRegion.left = 0; + destRegion.right = SpriteDataBytes; + destRegion.top = 0; + destRegion.bottom = 1; + destRegion.front = 0; + destRegion.back = 1; + D3D11_MAPPED_SUBRESOURCE MappedResource; + if ( S_OK == pd3dImmediateContext->Map( m_pSpriteBuffer11, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedResource ) ) + { + memcpy( MappedResource.pData, (const void*)&m_SpriteVertices[0], SpriteDataBytes ); + pd3dImmediateContext->Unmap(m_pSpriteBuffer11, 0); + } + + // Draw + UINT Stride = sizeof( DXUTSpriteVertex ); + UINT Offset = 0; + pd3dImmediateContext->IASetVertexBuffers( 0, 1, &m_pSpriteBuffer11, &Stride, &Offset ); + pd3dImmediateContext->IASetInputLayout( m_pInputLayout11 ); + pd3dImmediateContext->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST ); + pd3dImmediateContext->Draw( static_cast<UINT>( m_SpriteVertices.size() ), 0 ); + + m_SpriteVertices.clear(); +} + + +//-------------------------------------------------------------------------------------- +bool CDXUTDialogResourceManager::RegisterDialog( _In_ CDXUTDialog* pDialog ) +{ + // Check that the dialog isn't already registered. + for( auto it = m_Dialogs.cbegin(); it != m_Dialogs.cend(); ++it ) + { + if( *it == pDialog ) + return true; + } + + // Add to the list. + m_Dialogs.push_back( pDialog ); + + // Set up next and prev pointers. + if( m_Dialogs.size() > 1 ) + m_Dialogs[m_Dialogs.size() - 2]->SetNextDialog( pDialog ); + m_Dialogs[m_Dialogs.size() - 1]->SetNextDialog( m_Dialogs[0] ); + + return true; +} + + +//-------------------------------------------------------------------------------------- +void CDXUTDialogResourceManager::UnregisterDialog( _In_ CDXUTDialog* pDialog ) +{ + // Search for the dialog in the list. + for( size_t i = 0; i < m_Dialogs.size(); ++i ) + { + if( m_Dialogs[ i ] == pDialog ) + { + m_Dialogs.erase( m_Dialogs.begin() + i ); + if( !m_Dialogs.empty() ) + { + int l, r; + + if( 0 == i ) + l = int( m_Dialogs.size() - 1 ); + else + l = int(i) - 1; + + if( m_Dialogs.size() == i ) + r = 0; + else + r = int( i ); + + m_Dialogs[l]->SetNextDialog( m_Dialogs[r] ); + } + return; + } + } +} + + +//-------------------------------------------------------------------------------------- +void CDXUTDialogResourceManager::EnableKeyboardInputForAllDialogs() +{ + // Enable keyboard input for all registered dialogs + for( auto it = m_Dialogs.begin(); it != m_Dialogs.end(); ++it ) + (*it)->EnableKeyboardInput( true ); +} + + +//-------------------------------------------------------------------------------------- +_Use_decl_annotations_ +int CDXUTDialogResourceManager::AddFont( LPCWSTR strFaceName, LONG height, LONG weight ) +{ + // See if this font already exists + for( size_t i = 0; i < m_FontCache.size(); ++i ) + { + auto pFontNode = m_FontCache[ i ]; + size_t nLen = 0; + nLen = wcsnlen( strFaceName, MAX_PATH); + if( 0 == _wcsnicmp( pFontNode->strFace, strFaceName, nLen ) && + pFontNode->nHeight == height && + pFontNode->nWeight == weight ) + { + return static_cast<int>( i ); + } + } + + // Add a new font and try to create it + auto pNewFontNode = new (std::nothrow) DXUTFontNode; + if( !pNewFontNode ) + return -1; + + ZeroMemory( pNewFontNode, sizeof( DXUTFontNode ) ); + wcscpy_s( pNewFontNode->strFace, MAX_PATH, strFaceName ); + pNewFontNode->nHeight = height; + pNewFontNode->nWeight = weight; + m_FontCache.push_back( pNewFontNode ); + + int iFont = (int)m_FontCache.size() - 1; + + // If a device is available, try to create immediately + return iFont; +} + + +//-------------------------------------------------------------------------------------- +int CDXUTDialogResourceManager::AddTexture( _In_z_ LPCWSTR strFilename ) +{ + // See if this texture already exists + for( size_t i = 0; i < m_TextureCache.size(); ++i ) + { + auto pTextureNode = m_TextureCache[ i ]; + size_t nLen = 0; + nLen = wcsnlen( strFilename, MAX_PATH); + if( pTextureNode->bFileSource && // Sources must match + 0 == _wcsnicmp( pTextureNode->strFilename, strFilename, nLen ) ) + { + return static_cast<int>( i ); + } + } + + // Add a new texture and try to create it + auto pNewTextureNode = new (std::nothrow) DXUTTextureNode; + if( !pNewTextureNode ) + return -1; + + ZeroMemory( pNewTextureNode, sizeof( DXUTTextureNode ) ); + pNewTextureNode->bFileSource = true; + wcscpy_s( pNewTextureNode->strFilename, MAX_PATH, strFilename ); + + m_TextureCache.push_back( pNewTextureNode ); + + int iTexture = int( m_TextureCache.size() ) - 1; + + // If a device is available, try to create immediately + + return iTexture; +} + + +//-------------------------------------------------------------------------------------- +_Use_decl_annotations_ +int CDXUTDialogResourceManager::AddTexture( LPCWSTR strResourceName, HMODULE hResourceModule ) +{ + // See if this texture already exists + for( size_t i = 0; i < m_TextureCache.size(); i++ ) + { + auto pTextureNode = m_TextureCache[ 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 static_cast<int>( i ); + } + else + { + // String-based ID + size_t nLen = 0; + nLen = wcsnlen ( strResourceName, MAX_PATH ); + if( 0 == _wcsnicmp( pTextureNode->strFilename, strResourceName, nLen ) ) + return static_cast<int>( i ); + } + } + } + + // Add a new texture and try to create it + auto pNewTextureNode = new (std::nothrow) DXUTTextureNode; + if( !pNewTextureNode ) + return -1; + + ZeroMemory( pNewTextureNode, sizeof( DXUTTextureNode ) ); + pNewTextureNode->hResourceModule = hResourceModule; + if( IS_INTRESOURCE( strResourceName ) ) + { + pNewTextureNode->nResourceID = ( int )( size_t )strResourceName; + } + else + { + pNewTextureNode->nResourceID = 0; + wcscpy_s( pNewTextureNode->strFilename, MAX_PATH, strResourceName ); + } + + m_TextureCache.push_back( pNewTextureNode ); + + int iTexture = int( m_TextureCache.size() ) - 1; + + // If a device is available, try to create immediately + + return iTexture; +} + + +//-------------------------------------------------------------------------------------- +HRESULT CDXUTDialogResourceManager::CreateTexture11( _In_ UINT iTexture ) +{ + HRESULT hr = S_OK; + + auto pTextureNode = m_TextureCache[ iTexture ]; + + if( !pTextureNode->bFileSource ) + { + if( pTextureNode->nResourceID == 0xFFFF && pTextureNode->hResourceModule == ( HMODULE )0xFFFF ) + { + hr = DXUTCreateGUITextureFromInternalArray( m_pd3d11Device, &pTextureNode->pTexture11 ); + if( FAILED( hr ) ) + return DXTRACE_ERR( L"DXUTCreateGUITextureFromInternalArray", hr ); + DXUT_SetDebugName( pTextureNode->pTexture11, "DXUT GUI Texture" ); + } + } + + // Store dimensions + D3D11_TEXTURE2D_DESC desc; + pTextureNode->pTexture11->GetDesc( &desc ); + pTextureNode->dwWidth = desc.Width; + pTextureNode->dwHeight = desc.Height; + + // Create resource view + D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc; + SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; + SRVDesc.Format = desc.Format; + SRVDesc.Texture2D.MipLevels = 1; + SRVDesc.Texture2D.MostDetailedMip = 0; + hr = m_pd3d11Device->CreateShaderResourceView( pTextureNode->pTexture11, &SRVDesc, &pTextureNode->pTexResView11 ); + if ( FAILED(hr) ) + return hr; + + DXUT_SetDebugName( pTextureNode->pTexResView11, "DXUT GUI Texture" ); + + return hr; +} + + +//====================================================================================== +// CDXUTControl class +//====================================================================================== + +CDXUTControl::CDXUTControl( _In_opt_ CDXUTDialog* pDialog ) { m_Type = DXUT_CONTROL_BUTTON; m_pDialog = pDialog; m_ID = 0; + m_nHotkey = 0; m_Index = 0; - m_pUserData = NULL; + m_pUserData = nullptr; m_bEnabled = true; m_bVisible = true; @@ -3494,7 +2877,7 @@ CDXUTControl::CDXUTControl( CDXUTDialog* pDialog ) m_bHasFocus = false; m_bIsDefault = false; - m_pDialog = NULL; + m_pDialog = nullptr; m_x = 0; m_y = 0; @@ -3505,20 +2888,22 @@ CDXUTControl::CDXUTControl( CDXUTDialog* pDialog ) } +//-------------------------------------------------------------------------------------- CDXUTControl::~CDXUTControl() { - for( int i = 0; i < m_Elements.GetSize(); ++i ) + for( auto it = m_Elements.begin(); it != m_Elements.end(); ++it ) { - delete m_Elements[i]; + auto pElement = *it; + delete pElement; } - m_Elements.RemoveAll(); + m_Elements.clear(); } //-------------------------------------------------------------------------------------- -void CDXUTControl::SetTextColor( D3DCOLOR Color ) +void CDXUTControl::SetTextColor( _In_ DWORD Color ) { - CDXUTElement* pElement = m_Elements.GetAt( 0 ); + auto pElement = m_Elements[ 0 ]; if( pElement ) pElement->FontColor.States[DXUT_STATE_NORMAL] = Color; @@ -3526,30 +2911,23 @@ void CDXUTControl::SetTextColor( D3DCOLOR Color ) //-------------------------------------------------------------------------------------- -HRESULT CDXUTControl::SetElement( UINT iElement, CDXUTElement* pElement ) +HRESULT CDXUTControl::SetElement( _In_ UINT iElement, _In_ CDXUTElement* pElement ) { - HRESULT hr = S_OK; - - if( pElement == NULL ) + if( !pElement ) return E_INVALIDARG; // Make certain the array is this large - for( UINT i = m_Elements.GetSize(); i <= iElement; i++ ) + for( size_t i = m_Elements.size(); i <= iElement; i++ ) { - CDXUTElement* pNewElement = new CDXUTElement(); - if( pNewElement == NULL ) + auto pNewElement = new (std::nothrow) CDXUTElement(); + if( !pNewElement ) return E_OUTOFMEMORY; - hr = m_Elements.Add( pNewElement ); - if( FAILED( hr ) ) - { - SAFE_DELETE( pNewElement ); - return hr; - } + m_Elements.push_back( pNewElement ); } // Update the data - CDXUTElement* pCurElement = m_Elements.GetAt( iElement ); + auto pCurElement = m_Elements[ iElement ]; *pCurElement = *pElement; return S_OK; @@ -3562,10 +2940,9 @@ void CDXUTControl::Refresh() m_bMouseOver = false; m_bHasFocus = false; - for( int i = 0; i < m_Elements.GetSize(); i++ ) + for( auto it = m_Elements.begin(); it != m_Elements.end(); ++it ) { - CDXUTElement* pElement = m_Elements.GetAt( i ); - pElement->Refresh(); + (*it)->Refresh(); } } @@ -3577,30 +2954,30 @@ void CDXUTControl::UpdateRects() } -//-------------------------------------------------------------------------------------- +//====================================================================================== // CDXUTStatic class -//-------------------------------------------------------------------------------------- +//====================================================================================== //-------------------------------------------------------------------------------------- -CDXUTStatic::CDXUTStatic( CDXUTDialog* pDialog ) +CDXUTStatic::CDXUTStatic( _In_opt_ CDXUTDialog* pDialog ) { m_Type = DXUT_CONTROL_STATIC; m_pDialog = pDialog; ZeroMemory( &m_strText, sizeof( m_strText ) ); - for( int i = 0; i < m_Elements.GetSize(); i++ ) + for( auto it = m_Elements.begin(); it != m_Elements.end(); ++it ) { - CDXUTElement* pElement = m_Elements.GetAt( i ); + auto pElement = *it; SAFE_DELETE( pElement ); } - m_Elements.RemoveAll(); + m_Elements.clear(); } //-------------------------------------------------------------------------------------- -void CDXUTStatic::Render( float fElapsedTime ) +void CDXUTStatic::Render( _In_ float fElapsedTime ) { if( m_bVisible == false ) return; @@ -3610,20 +2987,20 @@ void CDXUTStatic::Render( float fElapsedTime ) if( m_bEnabled == false ) iState = DXUT_STATE_DISABLED; - CDXUTElement* pElement = m_Elements.GetAt( 0 ); + auto pElement = m_Elements[ 0 ]; pElement->FontColor.Blend( iState, fElapsedTime ); - m_pDialog->DrawText( m_strText, pElement, &m_rcBoundingBox, false, -1, false); + m_pDialog->DrawText( m_strText, pElement, &m_rcBoundingBox, false, false); } //-------------------------------------------------------------------------------------- -HRESULT CDXUTStatic::GetTextCopy( __out_ecount(bufferCount) LPWSTR strDest, - UINT bufferCount ) +_Use_decl_annotations_ +HRESULT CDXUTStatic::GetTextCopy( LPWSTR strDest, UINT bufferCount ) const { // Validate incoming parameters - if( strDest == NULL || bufferCount == 0 ) + if( !strDest || bufferCount == 0 ) { return E_INVALIDARG; } @@ -3636,9 +3013,9 @@ HRESULT CDXUTStatic::GetTextCopy( __out_ecount(bufferCount) LPWSTR strDest, //-------------------------------------------------------------------------------------- -HRESULT CDXUTStatic::SetText( LPCWSTR strText ) +HRESULT CDXUTStatic::SetText( _In_z_ LPCWSTR strText ) { - if( strText == NULL ) + if( !strText ) { m_strText[0] = 0; return S_OK; @@ -3649,12 +3026,11 @@ HRESULT CDXUTStatic::SetText( LPCWSTR strText ) } -//-------------------------------------------------------------------------------------- +//====================================================================================== // CDXUTButton class -//-------------------------------------------------------------------------------------- +//====================================================================================== -//-------------------------------------------------------------------------------------- -CDXUTButton::CDXUTButton( CDXUTDialog* pDialog ) +CDXUTButton::CDXUTButton( _In_opt_ CDXUTDialog* pDialog ) { m_Type = DXUT_CONTROL_BUTTON; m_pDialog = pDialog; @@ -3664,8 +3040,12 @@ CDXUTButton::CDXUTButton( CDXUTDialog* pDialog ) } //-------------------------------------------------------------------------------------- +_Use_decl_annotations_ bool CDXUTButton::HandleKeyboard( UINT uMsg, WPARAM wParam, LPARAM lParam ) + { + UNREFERENCED_PARAMETER(lParam); + if( !m_bEnabled || !m_bVisible ) return false; @@ -3700,8 +3080,12 @@ bool CDXUTButton::HandleKeyboard( UINT uMsg, WPARAM wParam, LPARAM lParam ) //-------------------------------------------------------------------------------------- -bool CDXUTButton::HandleMouse( UINT uMsg, POINT pt, WPARAM wParam, LPARAM lParam ) +_Use_decl_annotations_ +bool CDXUTButton::HandleMouse( UINT uMsg, const POINT& pt, WPARAM wParam, LPARAM lParam ) { + UNREFERENCED_PARAMETER(wParam); + UNREFERENCED_PARAMETER(lParam); + if( !m_bEnabled || !m_bVisible ) return false; @@ -3750,7 +3134,7 @@ bool CDXUTButton::HandleMouse( UINT uMsg, POINT pt, WPARAM wParam, LPARAM lParam } //-------------------------------------------------------------------------------------- -void CDXUTButton::Render( float fElapsedTime ) +void CDXUTButton::Render( _In_ float fElapsedTime ) { if( m_bVisible == false ) return; @@ -3788,7 +3172,7 @@ void CDXUTButton::Render( float fElapsedTime ) } // Background fill layer - CDXUTElement* pElement = m_Elements.GetAt( 0 ); + auto pElement = m_Elements[ 0 ]; float fBlendRate = ( iState == DXUT_STATE_PRESSED ) ? 0.0f : 0.8f; @@ -3801,27 +3185,26 @@ void CDXUTButton::Render( float fElapsedTime ) pElement->FontColor.Blend( iState, fElapsedTime, fBlendRate ); m_pDialog->DrawSprite( pElement, &rcWindow, DXUT_FAR_BUTTON_DEPTH ); - m_pDialog->DrawText( m_strText, pElement, &rcWindow, false, -1, true ); + m_pDialog->DrawText( m_strText, pElement, &rcWindow, false, true ); // Main button - pElement = m_Elements.GetAt( 1 ); + pElement = m_Elements[ 1 ]; // Blend current color pElement->TextureColor.Blend( iState, fElapsedTime, fBlendRate ); pElement->FontColor.Blend( iState, fElapsedTime, fBlendRate ); m_pDialog->DrawSprite( pElement, &rcWindow, DXUT_NEAR_BUTTON_DEPTH ); - m_pDialog->DrawText( m_strText, pElement, &rcWindow, false, -1, true ); + m_pDialog->DrawText( m_strText, pElement, &rcWindow, false, true ); } -//-------------------------------------------------------------------------------------- +//====================================================================================== // CDXUTCheckBox class -//-------------------------------------------------------------------------------------- +//====================================================================================== -//-------------------------------------------------------------------------------------- -CDXUTCheckBox::CDXUTCheckBox( CDXUTDialog* pDialog ) +CDXUTCheckBox::CDXUTCheckBox( _In_opt_ CDXUTDialog* pDialog ) { m_Type = DXUT_CONTROL_CHECKBOX; m_pDialog = pDialog; @@ -3831,8 +3214,11 @@ CDXUTCheckBox::CDXUTCheckBox( CDXUTDialog* pDialog ) //-------------------------------------------------------------------------------------- +_Use_decl_annotations_ bool CDXUTCheckBox::HandleKeyboard( UINT uMsg, WPARAM wParam, LPARAM lParam ) { + UNREFERENCED_PARAMETER(lParam); + if( !m_bEnabled || !m_bVisible ) return false; @@ -3867,8 +3253,12 @@ bool CDXUTCheckBox::HandleKeyboard( UINT uMsg, WPARAM wParam, LPARAM lParam ) //-------------------------------------------------------------------------------------- -bool CDXUTCheckBox::HandleMouse( UINT uMsg, POINT pt, WPARAM wParam, LPARAM lParam ) +_Use_decl_annotations_ +bool CDXUTCheckBox::HandleMouse( UINT uMsg, const POINT& pt, WPARAM wParam, LPARAM lParam ) { + UNREFERENCED_PARAMETER(wParam); + UNREFERENCED_PARAMETER(lParam); + if( !m_bEnabled || !m_bVisible ) return false; @@ -3915,6 +3305,7 @@ bool CDXUTCheckBox::HandleMouse( UINT uMsg, POINT pt, WPARAM wParam, LPARAM lPar //-------------------------------------------------------------------------------------- +_Use_decl_annotations_ void CDXUTCheckBox::SetCheckedInternal( bool bChecked, bool bFromInput ) { m_bChecked = bChecked; @@ -3924,14 +3315,13 @@ void CDXUTCheckBox::SetCheckedInternal( bool bChecked, bool bFromInput ) //-------------------------------------------------------------------------------------- -BOOL CDXUTCheckBox::ContainsPoint( POINT pt ) +bool CDXUTCheckBox::ContainsPoint( _In_ const POINT& pt ) { return ( PtInRect( &m_rcBoundingBox, pt ) || PtInRect( &m_rcButton, pt ) ); } - //-------------------------------------------------------------------------------------- void CDXUTCheckBox::UpdateRects() { @@ -3945,9 +3335,8 @@ void CDXUTCheckBox::UpdateRects() } - //-------------------------------------------------------------------------------------- -void CDXUTCheckBox::Render( float fElapsedTime ) +void CDXUTCheckBox::Render( _In_ float fElapsedTime ) { if( m_bVisible == false ) return; @@ -3964,7 +3353,7 @@ void CDXUTCheckBox::Render( float fElapsedTime ) else if( m_bHasFocus ) iState = DXUT_STATE_FOCUS; - CDXUTElement* pElement = m_Elements.GetAt( 0 ); + auto pElement = m_Elements[ 0 ]; float fBlendRate = ( iState == DXUT_STATE_PRESSED ) ? 0.0f : 0.8f; @@ -3972,26 +3361,23 @@ void CDXUTCheckBox::Render( float fElapsedTime ) pElement->FontColor.Blend( iState, fElapsedTime, fBlendRate ); m_pDialog->DrawSprite( pElement, &m_rcButton, DXUT_NEAR_BUTTON_DEPTH ); - m_pDialog->DrawText( m_strText, pElement, &m_rcText, false, -1, false ); + m_pDialog->DrawText( m_strText, pElement, &m_rcText, false, false ); if( !m_bChecked ) iState = DXUT_STATE_HIDDEN; - pElement = m_Elements.GetAt( 1 ); + pElement = m_Elements[ 1 ]; pElement->TextureColor.Blend( iState, fElapsedTime, fBlendRate ); m_pDialog->DrawSprite( pElement, &m_rcButton, DXUT_FAR_BUTTON_DEPTH ); } - - -//-------------------------------------------------------------------------------------- +//====================================================================================== // CDXUTRadioButton class -//-------------------------------------------------------------------------------------- +//====================================================================================== -//-------------------------------------------------------------------------------------- -CDXUTRadioButton::CDXUTRadioButton( CDXUTDialog* pDialog ) +CDXUTRadioButton::CDXUTRadioButton( _In_opt_ CDXUTDialog* pDialog ) { m_Type = DXUT_CONTROL_RADIOBUTTON; m_pDialog = pDialog; @@ -4000,8 +3386,11 @@ CDXUTRadioButton::CDXUTRadioButton( CDXUTDialog* pDialog ) //-------------------------------------------------------------------------------------- +_Use_decl_annotations_ bool CDXUTRadioButton::HandleKeyboard( UINT uMsg, WPARAM wParam, LPARAM lParam ) { + UNREFERENCED_PARAMETER(lParam); + if( !m_bEnabled || !m_bVisible ) return false; @@ -4040,8 +3429,12 @@ bool CDXUTRadioButton::HandleKeyboard( UINT uMsg, WPARAM wParam, LPARAM lParam ) //-------------------------------------------------------------------------------------- -bool CDXUTRadioButton::HandleMouse( UINT uMsg, POINT pt, WPARAM wParam, LPARAM lParam ) +_Use_decl_annotations_ +bool CDXUTRadioButton::HandleMouse( UINT uMsg, const POINT& pt, WPARAM wParam, LPARAM lParam ) { + UNREFERENCED_PARAMETER(wParam); + UNREFERENCED_PARAMETER(lParam); + if( !m_bEnabled || !m_bVisible ) return false; @@ -4092,6 +3485,7 @@ bool CDXUTRadioButton::HandleMouse( UINT uMsg, POINT pt, WPARAM wParam, LPARAM l } //-------------------------------------------------------------------------------------- +_Use_decl_annotations_ void CDXUTRadioButton::SetCheckedInternal( bool bChecked, bool bClearGroup, bool bFromInput ) { if( bChecked && bClearGroup ) @@ -4102,14 +3496,11 @@ void CDXUTRadioButton::SetCheckedInternal( bool bChecked, bool bClearGroup, bool } - - -//-------------------------------------------------------------------------------------- +//====================================================================================== // CDXUTComboBox class -//-------------------------------------------------------------------------------------- +//====================================================================================== -//-------------------------------------------------------------------------------------- -CDXUTComboBox::CDXUTComboBox( CDXUTDialog* pDialog ) : m_ScrollBar( pDialog ) +CDXUTComboBox::CDXUTComboBox( _In_opt_ CDXUTDialog* pDialog ) : m_ScrollBar( pDialog ) { m_Type = DXUT_CONTROL_COMBOBOX; m_pDialog = pDialog; @@ -4131,14 +3522,14 @@ CDXUTComboBox::~CDXUTComboBox() //-------------------------------------------------------------------------------------- -void CDXUTComboBox::SetTextColor( D3DCOLOR Color ) +void CDXUTComboBox::SetTextColor( _In_ DWORD Color ) { - CDXUTElement* pElement = m_Elements.GetAt( 0 ); + auto pElement = m_Elements[ 0 ]; if( pElement ) pElement->FontColor.States[DXUT_STATE_NORMAL] = Color; - pElement = m_Elements.GetAt( 2 ); + pElement = m_Elements[ 2 ]; if( pElement ) pElement->FontColor.States[DXUT_STATE_NORMAL] = Color; @@ -4158,20 +3549,20 @@ void CDXUTComboBox::UpdateRects() m_rcText.right = m_rcButton.left; m_rcDropdown = m_rcText; - OffsetRect( &m_rcDropdown, 0, ( int )( 0.90f * RectHeight( m_rcText ) ) ); + OffsetRect( &m_rcDropdown, 0, static_cast<int>( 0.90f * RectHeight( m_rcText ) ) ); m_rcDropdown.bottom += m_nDropHeight; m_rcDropdown.right -= m_nSBWidth; m_rcDropdownText = m_rcDropdown; - m_rcDropdownText.left += ( int )( 0.1f * RectWidth( m_rcDropdown ) ); - m_rcDropdownText.right -= ( int )( 0.1f * RectWidth( m_rcDropdown ) ); - m_rcDropdownText.top += ( int )( 0.1f * RectHeight( m_rcDropdown ) ); - m_rcDropdownText.bottom -= ( int )( 0.1f * RectHeight( m_rcDropdown ) ); + m_rcDropdownText.left += static_cast<int>(0.1f * RectWidth(m_rcDropdown)); + m_rcDropdownText.right -= static_cast<int>(0.1f * RectWidth(m_rcDropdown)); + m_rcDropdownText.top += static_cast<int>(0.1f * RectHeight(m_rcDropdown)); + m_rcDropdownText.bottom -= static_cast<int>(0.1f * RectHeight(m_rcDropdown)); // Update the scrollbar's rects m_ScrollBar.SetLocation( m_rcDropdown.right, m_rcDropdown.top + 2 ); m_ScrollBar.SetSize( m_nSBWidth, RectHeight( m_rcDropdown ) - 2 ); - DXUTFontNode* pFontNode = m_pDialog->GetManager()->GetFontNode( m_Elements.GetAt( 2 )->iFont ); + auto pFontNode = m_pDialog->GetManager()->GetFontNode( m_Elements[ 2 ]->iFont ); if( pFontNode && pFontNode->nHeight ) { m_ScrollBar.SetPageSize( RectHeight( m_rcDropdownText ) / pFontNode->nHeight ); @@ -4193,6 +3584,7 @@ void CDXUTComboBox::OnFocusOut() //-------------------------------------------------------------------------------------- +_Use_decl_annotations_ bool CDXUTComboBox::HandleKeyboard( UINT uMsg, WPARAM wParam, LPARAM lParam ) { const DWORD REPEAT_MASK = ( 0x40000000 ); @@ -4279,7 +3671,8 @@ bool CDXUTComboBox::HandleKeyboard( UINT uMsg, WPARAM wParam, LPARAM lParam ) //-------------------------------------------------------------------------------------- -bool CDXUTComboBox::HandleMouse( UINT uMsg, POINT pt, WPARAM wParam, LPARAM lParam ) +_Use_decl_annotations_ +bool CDXUTComboBox::HandleMouse( UINT uMsg, const POINT& pt, WPARAM wParam, LPARAM lParam ) { if( !m_bEnabled || !m_bVisible ) return false; @@ -4295,13 +3688,13 @@ bool CDXUTComboBox::HandleMouse( UINT uMsg, POINT pt, WPARAM wParam, LPARAM lPar if( m_bOpened && PtInRect( &m_rcDropdown, pt ) ) { // Determine which item has been selected - for( int i = 0; i < m_Items.GetSize(); i++ ) + for( size_t i = 0; i < m_Items.size(); i++ ) { - DXUTComboBoxItem* pItem = m_Items.GetAt( i ); + auto pItem = m_Items[ i ]; if( pItem->bVisible && PtInRect( &pItem->rcActive, pt ) ) { - m_iFocused = i; + m_iFocused = static_cast<int>( i ); } } return true; @@ -4340,13 +3733,13 @@ bool CDXUTComboBox::HandleMouse( UINT uMsg, POINT pt, WPARAM wParam, LPARAM lPar if( m_bOpened && PtInRect( &m_rcDropdown, pt ) ) { // Determine which item has been selected - for( int i = m_ScrollBar.GetTrackPos(); i < m_Items.GetSize(); i++ ) + for( size_t i = m_ScrollBar.GetTrackPos(); i < m_Items.size(); i++ ) { - DXUTComboBoxItem* pItem = m_Items.GetAt( i ); + auto pItem = m_Items[ i ]; if( pItem->bVisible && PtInRect( &pItem->rcActive, pt ) ) { - m_iFocused = m_iSelected = i; + m_iFocused = m_iSelected = static_cast<int>( i ); m_pDialog->SendEvent( EVENT_COMBOBOX_SELECTION_CHANGED, true, this ); m_bOpened = false; @@ -4399,8 +3792,9 @@ bool CDXUTComboBox::HandleMouse( UINT uMsg, POINT pt, WPARAM wParam, LPARAM lPar int zDelta = ( short )HIWORD( wParam ) / WHEEL_DELTA; if( m_bOpened ) { - UINT uLines; - SystemParametersInfo( SPI_GETWHEELSCROLLLINES, 0, &uLines, 0 ); + UINT uLines = 0; + if ( !SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, &uLines, 0) ) + uLines = 0; m_ScrollBar.Scroll( -zDelta * uLines ); } else @@ -4450,7 +3844,7 @@ void CDXUTComboBox::OnHotkey() m_iSelected++; - if( m_iSelected >= ( int )m_Items.GetSize() ) + if( m_iSelected >= ( int )m_Items.size() ) m_iSelected = 0; m_iFocused = m_iSelected; @@ -4459,17 +3853,18 @@ void CDXUTComboBox::OnHotkey() //-------------------------------------------------------------------------------------- -void CDXUTComboBox::Render( float fElapsedTime ) +void CDXUTComboBox::Render( _In_ float fElapsedTime ) { if( m_bVisible == false ) return; + DXUT_CONTROL_STATE iState = DXUT_STATE_NORMAL; if( !m_bOpened ) iState = DXUT_STATE_HIDDEN; // Dropdown box - CDXUTElement* pElement = m_Elements.GetAt( 2 ); + auto pElement = m_Elements[ 2 ]; // If we have not initialized the scroll bar page size, // do that now. @@ -4496,20 +3891,19 @@ void CDXUTComboBox::Render( float fElapsedTime ) m_pDialog->DrawSprite( pElement, &m_rcDropdown, DXUT_NEAR_BUTTON_DEPTH ); // Selection outline - CDXUTElement* pSelectionElement = m_Elements.GetAt( 3 ); + auto pSelectionElement = m_Elements[ 3 ]; pSelectionElement->TextureColor.Current = pElement->TextureColor.Current; - pSelectionElement->FontColor.Current = pSelectionElement->FontColor.States[ DXUT_STATE_NORMAL ]; + pSelectionElement->FontColor.SetCurrent( pSelectionElement->FontColor.States[ DXUT_STATE_NORMAL ] ); - DXUTFontNode* pFont = m_pDialog->GetFont( pElement->iFont ); + auto pFont = m_pDialog->GetFont( pElement->iFont ); if( pFont ) { int curY = m_rcDropdownText.top; int nRemainingHeight = RectHeight( m_rcDropdownText ); - //WCHAR strDropdown[4096] = {0}; - for( int i = m_ScrollBar.GetTrackPos(); i < m_Items.GetSize(); i++ ) + for( size_t i = m_ScrollBar.GetTrackPos(); i < m_Items.size(); i++ ) { - DXUTComboBoxItem* pItem = m_Items.GetAt( i ); + auto pItem = m_Items[ i ]; // Make sure there's room left in the dropdown nRemainingHeight -= pFont->nHeight; @@ -4522,10 +3916,6 @@ void CDXUTComboBox::Render( float fElapsedTime ) SetRect( &pItem->rcActive, m_rcDropdownText.left, curY, m_rcDropdownText.right, curY + pFont->nHeight ); curY += pFont->nHeight; - //debug - //int blue = 50 * i; - //m_pDialog->DrawRect( &pItem->rcActive, 0xFFFF0000 | blue ); - pItem->bVisible = true; if( m_bOpened ) @@ -4533,7 +3923,7 @@ void CDXUTComboBox::Render( float fElapsedTime ) if( ( int )i == m_iFocused ) { RECT rc; - SetRect( &rc, m_rcDropdown.left, pItem->rcActive.top - 2, m_rcDropdown.right, + SetRect( &rc, m_rcDropdown.left, pItem->rcActive.top, m_rcDropdown.right, pItem->rcActive.bottom + 2 ); m_pDialog->DrawSprite( pSelectionElement, &rc, DXUT_NEAR_BUTTON_DEPTH ); m_pDialog->DrawText( pItem->strText, pSelectionElement, &pItem->rcActive ); @@ -4575,7 +3965,7 @@ void CDXUTComboBox::Render( float fElapsedTime ) float fBlendRate = ( iState == DXUT_STATE_PRESSED ) ? 0.0f : 0.8f; // Button - pElement = m_Elements.GetAt( 1 ); + pElement = m_Elements[ 1 ]; // Blend current color pElement->TextureColor.Blend( iState, fElapsedTime, fBlendRate ); @@ -4588,7 +3978,7 @@ void CDXUTComboBox::Render( float fElapsedTime ) iState = DXUT_STATE_PRESSED; // Main text box - pElement = m_Elements.GetAt( 0 ); + pElement = m_Elements[ 0 ]; // Blend current color pElement->TextureColor.Blend( iState, fElapsedTime, fBlendRate ); @@ -4596,12 +3986,12 @@ void CDXUTComboBox::Render( float fElapsedTime ) m_pDialog->DrawSprite( pElement, &m_rcText, DXUT_NEAR_BUTTON_DEPTH ); - if( m_iSelected >= 0 && m_iSelected < ( int )m_Items.GetSize() ) + if( m_iSelected >= 0 && m_iSelected < ( int )m_Items.size() ) { - DXUTComboBoxItem* pItem = m_Items.GetAt( m_iSelected ); - if( pItem != NULL ) + auto pItem = m_Items[ m_iSelected ]; + if( pItem ) { - m_pDialog->DrawText( pItem->strText, pElement, &m_rcText, false, -1, true ); + m_pDialog->DrawText( pItem->strText, pElement, &m_rcText, false, true ); } } @@ -4609,17 +3999,18 @@ void CDXUTComboBox::Render( float fElapsedTime ) //-------------------------------------------------------------------------------------- +_Use_decl_annotations_ HRESULT CDXUTComboBox::AddItem( const WCHAR* strText, void* pData ) { // Validate parameters - if( strText == NULL ) + if( !strText ) { return E_INVALIDARG; } // Create a new item and set the data - DXUTComboBoxItem* pItem = new DXUTComboBoxItem; - if( pItem == NULL ) + auto pItem = new (std::nothrow) DXUTComboBoxItem; + if( !pItem ) { return DXTRACE_ERR_MSGBOX( L"new", E_OUTOFMEMORY ); } @@ -4628,10 +4019,10 @@ HRESULT CDXUTComboBox::AddItem( const WCHAR* strText, void* pData ) wcscpy_s( pItem->strText, 256, strText ); pItem->pData = pData; - m_Items.Add( pItem ); + m_Items.push_back( pItem ); // Update the scroll bar with new range - m_ScrollBar.SetTrackRange( 0, m_Items.GetSize() ); + m_ScrollBar.SetTrackRange( 0, (int)m_Items.size() ); // If this is the only item in the list, it's selected if( GetNumItems() == 1 ) @@ -4646,53 +4037,53 @@ HRESULT CDXUTComboBox::AddItem( const WCHAR* strText, void* pData ) //-------------------------------------------------------------------------------------- -void CDXUTComboBox::RemoveItem( UINT index ) +void CDXUTComboBox::RemoveItem( _In_ UINT index ) { - DXUTComboBoxItem* pItem = m_Items.GetAt( index ); + auto it = m_Items.begin() + index; + auto pItem = *it; SAFE_DELETE( pItem ); - m_Items.Remove( index ); - m_ScrollBar.SetTrackRange( 0, m_Items.GetSize() ); - if( m_iSelected >= m_Items.GetSize() ) - m_iSelected = m_Items.GetSize() - 1; + m_Items.erase( it ); + m_ScrollBar.SetTrackRange( 0, (int)m_Items.size() ); + if( m_iSelected >= (int)m_Items.size() ) + m_iSelected = (int)m_Items.size() - 1; } //-------------------------------------------------------------------------------------- void CDXUTComboBox::RemoveAllItems() { - for( int i = 0; i < m_Items.GetSize(); i++ ) + for( auto it = m_Items.begin(); it != m_Items.end(); ++it ) { - DXUTComboBoxItem* pItem = m_Items.GetAt( i ); + auto pItem = *it; SAFE_DELETE( pItem ); } - m_Items.RemoveAll(); + m_Items.clear(); m_ScrollBar.SetTrackRange( 0, 1 ); m_iFocused = m_iSelected = -1; } - //-------------------------------------------------------------------------------------- -bool CDXUTComboBox::ContainsItem( const WCHAR* strText, UINT iStart ) +bool CDXUTComboBox::ContainsItem( _In_z_ const WCHAR* strText, _In_ UINT iStart ) { return ( -1 != FindItem( strText, iStart ) ); } //-------------------------------------------------------------------------------------- -int CDXUTComboBox::FindItem( const WCHAR* strText, UINT iStart ) +int CDXUTComboBox::FindItem( _In_z_ const WCHAR* strText, _In_ UINT iStart ) const { - if( strText == NULL ) + if( !strText ) return -1; - for( int i = iStart; i < m_Items.GetSize(); i++ ) + for( size_t i = iStart; i < m_Items.size(); i++ ) { - DXUTComboBoxItem* pItem = m_Items.GetAt( i ); + auto pItem = m_Items[ i ]; if( 0 == wcscmp( pItem->strText, strText ) ) { - return i; + return static_cast<int>( i ); } } @@ -4701,40 +4092,40 @@ int CDXUTComboBox::FindItem( const WCHAR* strText, UINT iStart ) //-------------------------------------------------------------------------------------- -void* CDXUTComboBox::GetSelectedData() +void* CDXUTComboBox::GetSelectedData() const { if( m_iSelected < 0 ) - return NULL; + return nullptr; - DXUTComboBoxItem* pItem = m_Items.GetAt( m_iSelected ); + auto pItem = m_Items[ m_iSelected ]; return pItem->pData; } //-------------------------------------------------------------------------------------- -DXUTComboBoxItem* CDXUTComboBox::GetSelectedItem() +DXUTComboBoxItem* CDXUTComboBox::GetSelectedItem() const { if( m_iSelected < 0 ) - return NULL; + return nullptr; - return m_Items.GetAt( m_iSelected ); + return m_Items[ m_iSelected ]; } //-------------------------------------------------------------------------------------- -void* CDXUTComboBox::GetItemData( const WCHAR* strText ) +void* CDXUTComboBox::GetItemData( _In_z_ const WCHAR* strText ) const { int index = FindItem( strText ); if( index == -1 ) { - return NULL; + return nullptr; } - DXUTComboBoxItem* pItem = m_Items.GetAt( index ); - if( pItem == NULL ) + auto pItem = m_Items[ index ]; + if( !pItem ) { - DXTRACE_ERR( L"CGrowableArray::GetAt", E_FAIL ); - return NULL; + DXTRACE_ERR( L"CDXUTComboBox::GetItemData", E_FAIL ); + return nullptr; } return pItem->pData; @@ -4742,17 +4133,17 @@ void* CDXUTComboBox::GetItemData( const WCHAR* strText ) //-------------------------------------------------------------------------------------- -void* CDXUTComboBox::GetItemData( int nIndex ) +void* CDXUTComboBox::GetItemData( _In_ int nIndex ) const { - if( nIndex < 0 || nIndex >= m_Items.GetSize() ) - return NULL; + if( nIndex < 0 || nIndex >= (int)m_Items.size() ) + return nullptr; - return m_Items.GetAt( nIndex )->pData; + return m_Items[ nIndex ]->pData; } //-------------------------------------------------------------------------------------- -HRESULT CDXUTComboBox::SetSelectedByIndex( UINT index ) +HRESULT CDXUTComboBox::SetSelectedByIndex( _In_ UINT index ) { if( index >= GetNumItems() ) return E_INVALIDARG; @@ -4766,9 +4157,9 @@ HRESULT CDXUTComboBox::SetSelectedByIndex( UINT index ) //-------------------------------------------------------------------------------------- -HRESULT CDXUTComboBox::SetSelectedByText( const WCHAR* strText ) +HRESULT CDXUTComboBox::SetSelectedByText( _In_z_ const WCHAR* strText ) { - if( strText == NULL ) + if( !strText ) return E_INVALIDARG; int index = FindItem( strText ); @@ -4784,15 +4175,15 @@ HRESULT CDXUTComboBox::SetSelectedByText( const WCHAR* strText ) //-------------------------------------------------------------------------------------- -HRESULT CDXUTComboBox::SetSelectedByData( void* pData ) +HRESULT CDXUTComboBox::SetSelectedByData( _In_ void* pData ) { - for( int i = 0; i < m_Items.GetSize(); i++ ) + for( size_t i = 0; i < m_Items.size(); i++ ) { - DXUTComboBoxItem* pItem = m_Items.GetAt( i ); + auto pItem = m_Items[ i ]; if( pItem->pData == pData ) { - m_iFocused = m_iSelected = i; + m_iFocused = m_iSelected = static_cast<int>( i ); m_pDialog->SendEvent( EVENT_COMBOBOX_SELECTION_CHANGED, false, this ); return S_OK; } @@ -4802,9 +4193,11 @@ HRESULT CDXUTComboBox::SetSelectedByData( void* pData ) } +//====================================================================================== +// CDXUTSlider class +//====================================================================================== -//-------------------------------------------------------------------------------------- -CDXUTSlider::CDXUTSlider( CDXUTDialog* pDialog ) +CDXUTSlider::CDXUTSlider( _In_opt_ CDXUTDialog* pDialog ) { m_Type = DXUT_CONTROL_SLIDER; m_pDialog = pDialog; @@ -4818,7 +4211,7 @@ CDXUTSlider::CDXUTSlider( CDXUTDialog* pDialog ) //-------------------------------------------------------------------------------------- -BOOL CDXUTSlider::ContainsPoint( POINT pt ) +bool CDXUTSlider::ContainsPoint( _In_ const POINT& pt ) { return ( PtInRect( &m_rcBoundingBox, pt ) || PtInRect( &m_rcButton, pt ) ); @@ -4838,15 +4231,21 @@ void CDXUTSlider::UpdateRects() OffsetRect( &m_rcButton, m_nButtonX, 0 ); } -int CDXUTSlider::ValueFromPos( int x ) + +//-------------------------------------------------------------------------------------- +int CDXUTSlider::ValueFromPos( _In_ int x ) { float fValuePerPixel = ( float )( m_nMax - m_nMin ) / RectWidth( m_rcBoundingBox ); return ( int )( 0.5f + m_nMin + fValuePerPixel * ( x - m_rcBoundingBox.left ) ); } + //-------------------------------------------------------------------------------------- +_Use_decl_annotations_ bool CDXUTSlider::HandleKeyboard( UINT uMsg, WPARAM wParam, LPARAM lParam ) { + UNREFERENCED_PARAMETER(lParam); + if( !m_bEnabled || !m_bVisible ) return false; @@ -4894,8 +4293,11 @@ bool CDXUTSlider::HandleKeyboard( UINT uMsg, WPARAM wParam, LPARAM lParam ) //-------------------------------------------------------------------------------------- -bool CDXUTSlider::HandleMouse( UINT uMsg, POINT pt, WPARAM wParam, LPARAM lParam ) +_Use_decl_annotations_ +bool CDXUTSlider::HandleMouse( UINT uMsg, const POINT& pt, WPARAM wParam, LPARAM lParam ) { + UNREFERENCED_PARAMETER(lParam); + if( !m_bEnabled || !m_bVisible ) return false; @@ -4985,7 +4387,7 @@ bool CDXUTSlider::HandleMouse( UINT uMsg, POINT pt, WPARAM wParam, LPARAM lParam //-------------------------------------------------------------------------------------- -void CDXUTSlider::SetRange( int nMin, int nMax ) +void CDXUTSlider::SetRange( _In_ int nMin, _In_ int nMax ) { m_nMin = nMin; m_nMax = nMax; @@ -4995,11 +4397,11 @@ void CDXUTSlider::SetRange( int nMin, int nMax ) //-------------------------------------------------------------------------------------- -void CDXUTSlider::SetValueInternal( int nValue, bool bFromInput ) +void CDXUTSlider::SetValueInternal( _In_ int nValue, _In_ bool bFromInput ) { // Clamp to range - nValue = __max( m_nMin, nValue ); - nValue = __min( m_nMax, nValue ); + nValue = std::max( m_nMin, nValue ); + nValue = std::min( m_nMax, nValue ); if( nValue == m_nValue ) return; @@ -5012,7 +4414,7 @@ void CDXUTSlider::SetValueInternal( int nValue, bool bFromInput ) //-------------------------------------------------------------------------------------- -void CDXUTSlider::Render( float fElapsedTime ) +void CDXUTSlider::Render( _In_ float fElapsedTime ) { if( m_bVisible == false ) return; @@ -5051,13 +4453,13 @@ void CDXUTSlider::Render( float fElapsedTime ) float fBlendRate = ( iState == DXUT_STATE_PRESSED ) ? 0.0f : 0.8f; - CDXUTElement* pElement = m_Elements.GetAt( 0 ); + auto pElement = m_Elements[ 0 ]; // Blend current color pElement->TextureColor.Blend( iState, fElapsedTime, fBlendRate ); m_pDialog->DrawSprite( pElement, &m_rcBoundingBox, DXUT_FAR_BUTTON_DEPTH ); - pElement = m_Elements.GetAt( 1 ); + pElement = m_Elements[ 1 ]; // Blend current color pElement->TextureColor.Blend( iState, fElapsedTime, fBlendRate ); @@ -5065,12 +4467,11 @@ void CDXUTSlider::Render( float fElapsedTime ) } -//-------------------------------------------------------------------------------------- +//====================================================================================== // CDXUTScrollBar class -//-------------------------------------------------------------------------------------- +//====================================================================================== -//-------------------------------------------------------------------------------------- -CDXUTScrollBar::CDXUTScrollBar( CDXUTDialog* pDialog ) +CDXUTScrollBar::CDXUTScrollBar( _In_opt_ CDXUTDialog* pDialog ) { m_Type = DXUT_CONTROL_SCROLLBAR; m_pDialog = pDialog; @@ -5123,7 +4524,7 @@ void CDXUTScrollBar::UpdateThumbRect() { if( m_nEnd - m_nStart > m_nPageSize ) { - int nThumbHeight = __max( RectHeight( m_rcTrack ) * m_nPageSize / ( m_nEnd - m_nStart ), + int nThumbHeight = std::max( RectHeight( m_rcTrack ) * m_nPageSize / ( m_nEnd - m_nStart ), SCROLLBAR_MINTHUMBSIZE ); int nMaxPosition = m_nEnd - m_nStart - m_nPageSize; m_rcThumb.top = m_rcTrack.top + ( m_nPosition - m_nStart ) * ( RectHeight( m_rcTrack ) - nThumbHeight ) @@ -5144,7 +4545,7 @@ void CDXUTScrollBar::UpdateThumbRect() //-------------------------------------------------------------------------------------- // Scroll() scrolls by nDelta items. A positive value scrolls down, while a negative // value scrolls up. -void CDXUTScrollBar::Scroll( int nDelta ) +void CDXUTScrollBar::Scroll( _In_ int nDelta ) { // Perform scroll m_nPosition += nDelta; @@ -5158,7 +4559,7 @@ void CDXUTScrollBar::Scroll( int nDelta ) //-------------------------------------------------------------------------------------- -void CDXUTScrollBar::ShowItem( int nIndex ) +void CDXUTScrollBar::ShowItem( _In_ int nIndex ) { // Cap the index @@ -5180,15 +4581,23 @@ void CDXUTScrollBar::ShowItem( int nIndex ) //-------------------------------------------------------------------------------------- +_Use_decl_annotations_ bool CDXUTScrollBar::HandleKeyboard( UINT uMsg, WPARAM wParam, LPARAM lParam ) { + UNREFERENCED_PARAMETER(uMsg); + UNREFERENCED_PARAMETER(wParam); + UNREFERENCED_PARAMETER(lParam); return false; } //-------------------------------------------------------------------------------------- -bool CDXUTScrollBar::HandleMouse( UINT uMsg, POINT pt, WPARAM wParam, LPARAM lParam ) +_Use_decl_annotations_ +bool CDXUTScrollBar::HandleMouse( UINT uMsg, const POINT& pt, WPARAM wParam, LPARAM lParam ) { + UNREFERENCED_PARAMETER(wParam); + UNREFERENCED_PARAMETER(lParam); + static int ThumbOffsetY; m_LastMouse = pt; @@ -5298,8 +4707,11 @@ bool CDXUTScrollBar::HandleMouse( UINT uMsg, POINT pt, WPARAM wParam, LPARAM lPa //-------------------------------------------------------------------------------------- +_Use_decl_annotations_ bool CDXUTScrollBar::MsgProc( UINT uMsg, WPARAM wParam, LPARAM lParam ) { + UNREFERENCED_PARAMETER(wParam); + if( WM_CAPTURECHANGED == uMsg ) { // The application just lost mouse capture. We may not have gotten @@ -5313,7 +4725,7 @@ bool CDXUTScrollBar::MsgProc( UINT uMsg, WPARAM wParam, LPARAM lParam ) //-------------------------------------------------------------------------------------- -void CDXUTScrollBar::Render( float fElapsedTime ) +void CDXUTScrollBar::Render( _In_ float fElapsedTime ) { if( m_bVisible == false ) return; @@ -5383,28 +4795,28 @@ void CDXUTScrollBar::Render( float fElapsedTime ) float fBlendRate = ( iState == DXUT_STATE_PRESSED ) ? 0.0f : 0.8f; // Background track layer - CDXUTElement* pElement = m_Elements.GetAt( 0 ); + auto pElement = m_Elements[ 0 ]; // Blend current color pElement->TextureColor.Blend( iState, fElapsedTime, fBlendRate ); m_pDialog->DrawSprite( pElement, &m_rcTrack, DXUT_FAR_BUTTON_DEPTH ); // Up Arrow - pElement = m_Elements.GetAt( 1 ); + pElement = m_Elements[ 1 ]; // Blend current color pElement->TextureColor.Blend( iState, fElapsedTime, fBlendRate ); m_pDialog->DrawSprite( pElement, &m_rcUpButton, DXUT_NEAR_BUTTON_DEPTH ); // Down Arrow - pElement = m_Elements.GetAt( 2 ); + pElement = m_Elements[ 2 ]; // Blend current color pElement->TextureColor.Blend( iState, fElapsedTime, fBlendRate ); m_pDialog->DrawSprite( pElement, &m_rcDownButton, DXUT_NEAR_BUTTON_DEPTH ); // Thumb button - pElement = m_Elements.GetAt( 3 ); + pElement = m_Elements[ 3 ]; // Blend current color pElement->TextureColor.Blend( iState, fElapsedTime, fBlendRate ); @@ -5414,7 +4826,7 @@ void CDXUTScrollBar::Render( float fElapsedTime ) //-------------------------------------------------------------------------------------- -void CDXUTScrollBar::SetTrackRange( int nStart, int nEnd ) +void CDXUTScrollBar::SetTrackRange( _In_ int nStart, _In_ int nEnd ) { m_nStart = nStart; m_nEnd = nEnd; Cap(); @@ -5434,12 +4846,12 @@ void CDXUTScrollBar::Cap() // Clips position at boundaries. Ensures it stays wi m_nPosition = m_nEnd - m_nPageSize + 1; } -//-------------------------------------------------------------------------------------- + +//====================================================================================== // CDXUTListBox class -//-------------------------------------------------------------------------------------- +//====================================================================================== -//-------------------------------------------------------------------------------------- -CDXUTListBox::CDXUTListBox( CDXUTDialog* pDialog ) : m_ScrollBar( pDialog ) +CDXUTListBox::CDXUTListBox( _In_opt_ CDXUTDialog* pDialog ) : m_ScrollBar( pDialog ) { m_Type = DXUT_CONTROL_LISTBOX; m_pDialog = pDialog; @@ -5476,7 +4888,7 @@ void CDXUTListBox::UpdateRects() // Update the scrollbar's rects m_ScrollBar.SetLocation( m_rcBoundingBox.right - m_nSBWidth, m_rcBoundingBox.top ); m_ScrollBar.SetSize( m_nSBWidth, m_height ); - DXUTFontNode* pFontNode = m_pDialog->GetManager()->GetFontNode( m_Elements.GetAt( 0 )->iFont ); + auto pFontNode = m_pDialog->GetManager()->GetFontNode( m_Elements[ 0 ]->iFont ); if( pFontNode && pFontNode->nHeight ) { m_ScrollBar.SetPageSize( RectHeight( m_rcText ) / pFontNode->nHeight ); @@ -5489,9 +4901,10 @@ void CDXUTListBox::UpdateRects() //-------------------------------------------------------------------------------------- +_Use_decl_annotations_ HRESULT CDXUTListBox::AddItem( const WCHAR* wszText, void* pData ) { - DXUTListBoxItem* pNewItem = new DXUTListBoxItem; + auto pNewItem = new (std::nothrow) DXUTListBoxItem; if( !pNewItem ) return E_OUTOFMEMORY; @@ -5500,24 +4913,18 @@ HRESULT CDXUTListBox::AddItem( const WCHAR* wszText, void* pData ) SetRect( &pNewItem->rcActive, 0, 0, 0, 0 ); pNewItem->bSelected = false; - HRESULT hr = m_Items.Add( pNewItem ); - if( FAILED( hr ) ) - { - SAFE_DELETE( pNewItem ); - } - else - { - m_ScrollBar.SetTrackRange( 0, m_Items.GetSize() ); - } + m_Items.push_back( pNewItem ); + m_ScrollBar.SetTrackRange( 0, (int)m_Items.size() ); - return hr; + return S_OK; } //-------------------------------------------------------------------------------------- +_Use_decl_annotations_ HRESULT CDXUTListBox::InsertItem( int nIndex, const WCHAR* wszText, void* pData ) { - DXUTListBoxItem* pNewItem = new DXUTListBoxItem; + auto pNewItem = new (std::nothrow) DXUTListBoxItem; if( !pNewItem ) return E_OUTOFMEMORY; @@ -5526,56 +4933,50 @@ HRESULT CDXUTListBox::InsertItem( int nIndex, const WCHAR* wszText, void* pData SetRect( &pNewItem->rcActive, 0, 0, 0, 0 ); pNewItem->bSelected = false; - HRESULT hr = m_Items.Insert( nIndex, pNewItem ); - if( SUCCEEDED( hr ) ) - m_ScrollBar.SetTrackRange( 0, m_Items.GetSize() ); - else - SAFE_DELETE( pNewItem ); + m_Items[ nIndex ] = pNewItem; + m_ScrollBar.SetTrackRange( 0, (int)m_Items.size() ); - return hr; + return S_OK; } //-------------------------------------------------------------------------------------- -void CDXUTListBox::RemoveItem( int nIndex ) +void CDXUTListBox::RemoveItem( _In_ int nIndex ) { - if( nIndex < 0 || nIndex >= ( int )m_Items.GetSize() ) + if( nIndex < 0 || nIndex >= ( int )m_Items.size() ) return; - DXUTListBoxItem* pItem = m_Items.GetAt( nIndex ); - + auto it = m_Items.begin() + nIndex; + auto pItem = *it; delete pItem; - m_Items.Remove( nIndex ); - m_ScrollBar.SetTrackRange( 0, m_Items.GetSize() ); - if( m_nSelected >= ( int )m_Items.GetSize() ) - m_nSelected = m_Items.GetSize() - 1; + m_Items.erase(it); + m_ScrollBar.SetTrackRange( 0, (int)m_Items.size() ); + if( m_nSelected >= ( int )m_Items.size() ) + m_nSelected = int( m_Items.size() ) - 1; m_pDialog->SendEvent( EVENT_LISTBOX_SELECTION, true, this ); } - - - //-------------------------------------------------------------------------------------- void CDXUTListBox::RemoveAllItems() { - for( int i = 0; i < m_Items.GetSize(); ++i ) + for( auto it = m_Items.begin(); it != m_Items.end(); ++it ) { - DXUTListBoxItem* pItem = m_Items.GetAt( i ); + auto pItem = *it; delete pItem; } - m_Items.RemoveAll(); + m_Items.clear(); m_ScrollBar.SetTrackRange( 0, 1 ); } //-------------------------------------------------------------------------------------- -DXUTListBoxItem* CDXUTListBox::GetItem( int nIndex ) +DXUTListBoxItem* CDXUTListBox::GetItem( _In_ int nIndex ) const { - if( nIndex < 0 || nIndex >= ( int )m_Items.GetSize() ) - return NULL; + if( nIndex < 0 || nIndex >= ( int )m_Items.size() ) + return nullptr; return m_Items[nIndex]; } @@ -5588,7 +4989,7 @@ DXUTListBoxItem* CDXUTListBox::GetItem( int nIndex ) // subsequent searches, the app passes the returned index back to GetSelectedIndex as. // nPreviousSelected. // Returns -1 on error or if no item is selected. -int CDXUTListBox::GetSelectedIndex( int nPreviousSelected ) +int CDXUTListBox::GetSelectedIndex( _In_ int nPreviousSelected ) const { if( nPreviousSelected < -1 ) return -1; @@ -5596,9 +4997,9 @@ int CDXUTListBox::GetSelectedIndex( int nPreviousSelected ) if( m_dwStyle & MULTISELECTION ) { // Multiple selection enabled. Search for the next item with the selected flag. - for( int i = nPreviousSelected + 1; i < ( int )m_Items.GetSize(); ++i ) + for( int i = nPreviousSelected + 1; i < ( int )m_Items.size(); ++i ) { - DXUTListBoxItem* pItem = m_Items.GetAt( i ); + auto pItem = m_Items[ i ]; if( pItem->bSelected ) return i; @@ -5615,10 +5016,10 @@ int CDXUTListBox::GetSelectedIndex( int nPreviousSelected ) //-------------------------------------------------------------------------------------- -void CDXUTListBox::SelectItem( int nNewIndex ) +void CDXUTListBox::SelectItem( _In_ int nNewIndex ) { // If no item exists, do nothing. - if( m_Items.GetSize() == 0 ) + if( m_Items.size() == 0 ) return; int nOldSelected = m_nSelected; @@ -5629,8 +5030,8 @@ void CDXUTListBox::SelectItem( int nNewIndex ) // Perform capping if( m_nSelected < 0 ) m_nSelected = 0; - if( m_nSelected >= ( int )m_Items.GetSize() ) - m_nSelected = m_Items.GetSize() - 1; + if( m_nSelected >= ( int )m_Items.size() ) + m_nSelected = int( m_Items.size() ) - 1; if( nOldSelected != m_nSelected ) { @@ -5651,6 +5052,7 @@ void CDXUTListBox::SelectItem( int nNewIndex ) //-------------------------------------------------------------------------------------- +_Use_decl_annotations_ bool CDXUTListBox::HandleKeyboard( UINT uMsg, WPARAM wParam, LPARAM lParam ) { if( !m_bEnabled || !m_bVisible ) @@ -5673,7 +5075,7 @@ bool CDXUTListBox::HandleKeyboard( UINT uMsg, WPARAM wParam, LPARAM lParam ) case VK_END: { // If no item exists, do nothing. - if( m_Items.GetSize() == 0 ) + if( m_Items.size() == 0 ) return true; int nOldSelected = m_nSelected; @@ -5692,14 +5094,14 @@ bool CDXUTListBox::HandleKeyboard( UINT uMsg, WPARAM wParam, LPARAM lParam ) case VK_HOME: m_nSelected = 0; break; case VK_END: - m_nSelected = m_Items.GetSize() - 1; break; + m_nSelected = int( m_Items.size() ) - 1; break; } // Perform capping if( m_nSelected < 0 ) m_nSelected = 0; - if( m_nSelected >= ( int )m_Items.GetSize() ) - m_nSelected = m_Items.GetSize() - 1; + if( m_nSelected >= ( int )m_Items.size() ) + m_nSelected = int( m_Items.size() ) - 1; if( nOldSelected != m_nSelected ) { @@ -5708,9 +5110,9 @@ bool CDXUTListBox::HandleKeyboard( UINT uMsg, WPARAM wParam, LPARAM lParam ) // Multiple selection // Clear all selection - for( int i = 0; i < ( int )m_Items.GetSize(); ++i ) + for( int i = 0; i < ( int )m_Items.size(); ++i ) { - DXUTListBoxItem* pItem = m_Items[i]; + auto pItem = m_Items[i]; pItem->bSelected = false; } @@ -5718,9 +5120,9 @@ bool CDXUTListBox::HandleKeyboard( UINT uMsg, WPARAM wParam, LPARAM lParam ) { // Select all items from m_nSelStart to // m_nSelected - int nEnd = __max( m_nSelStart, m_nSelected ); + int nEnd = std::max( m_nSelStart, m_nSelected ); - for( int n = __min( m_nSelStart, m_nSelected ); n <= nEnd; ++n ) + for( int n = std::min( m_nSelStart, m_nSelected ); n <= nEnd; ++n ) m_Items[n]->bSelected = true; } else @@ -5759,7 +5161,8 @@ bool CDXUTListBox::HandleKeyboard( UINT uMsg, WPARAM wParam, LPARAM lParam ) //-------------------------------------------------------------------------------------- -bool CDXUTListBox::HandleMouse( UINT uMsg, POINT pt, WPARAM wParam, LPARAM lParam ) +_Use_decl_annotations_ +bool CDXUTListBox::HandleMouse( UINT uMsg, const POINT& pt, WPARAM wParam, LPARAM lParam ) { if( !m_bEnabled || !m_bVisible ) return false; @@ -5778,7 +5181,7 @@ bool CDXUTListBox::HandleMouse( UINT uMsg, POINT pt, WPARAM wParam, LPARAM lPara case WM_LBUTTONDOWN: case WM_LBUTTONDBLCLK: // Check for clicks in the text area - if( m_Items.GetSize() > 0 && PtInRect( &m_rcSelection, pt ) ) + if( !m_Items.empty() && PtInRect( &m_rcSelection, pt ) ) { // Compute the index of the clicked item @@ -5791,7 +5194,7 @@ bool CDXUTListBox::HandleMouse( UINT uMsg, POINT pt, WPARAM wParam, LPARAM lPara // Only proceed if the click falls on top of an item. if( nClicked >= m_ScrollBar.GetTrackPos() && - nClicked < ( int )m_Items.GetSize() && + nClicked < ( int )m_Items.size() && nClicked < m_ScrollBar.GetTrackPos() + m_ScrollBar.GetPageSize() ) { SetCapture( DXUTGetHWND() ); @@ -5817,7 +5220,7 @@ bool CDXUTListBox::HandleMouse( UINT uMsg, POINT pt, WPARAM wParam, LPARAM lPara { // Determine behavior based on the state of Shift and Ctrl - DXUTListBoxItem* pSelItem = m_Items.GetAt( m_nSelected ); + auto pSelItem = m_Items[ m_nSelected ]; if( ( wParam & ( MK_SHIFT | MK_CONTROL ) ) == MK_CONTROL ) { // Control click. Reverse the selection of this item. @@ -5830,24 +5233,24 @@ bool CDXUTListBox::HandleMouse( UINT uMsg, POINT pt, WPARAM wParam, LPARAM lPara // from last selected item to the current item. // Clear everything else. - int nBegin = __min( m_nSelStart, m_nSelected ); - int nEnd = __max( m_nSelStart, m_nSelected ); + int nBegin = std::min( m_nSelStart, m_nSelected ); + int nEnd = std::max( m_nSelStart, m_nSelected ); for( int i = 0; i < nBegin; ++i ) { - DXUTListBoxItem* pItem = m_Items.GetAt( i ); + auto pItem = m_Items[ i ]; pItem->bSelected = false; } - for( int i = nEnd + 1; i < ( int )m_Items.GetSize(); ++i ) + for( int i = nEnd + 1; i < ( int )m_Items.size(); ++i ) { - DXUTListBoxItem* pItem = m_Items.GetAt( i ); + auto pItem = m_Items[ i ]; pItem->bSelected = false; } for( int i = nBegin; i <= nEnd; ++i ) { - DXUTListBoxItem* pItem = m_Items.GetAt( i ); + auto pItem = m_Items[ i ]; pItem->bSelected = true; } } @@ -5860,15 +5263,15 @@ bool CDXUTListBox::HandleMouse( UINT uMsg, POINT pt, WPARAM wParam, LPARAM lPara // the same state as m_nSelStart, not including m_nSelected. // Set m_nSelected to selected. - int nBegin = __min( m_nSelStart, m_nSelected ); - int nEnd = __max( m_nSelStart, m_nSelected ); + int nBegin = std::min( m_nSelStart, m_nSelected ); + int nEnd = std::max( m_nSelStart, m_nSelected ); // The two ends do not need to be set here. - bool bLastSelected = m_Items.GetAt( m_nSelStart )->bSelected; + bool bLastSelected = m_Items[ m_nSelStart ]->bSelected; for( int i = nBegin + 1; i < nEnd; ++i ) { - DXUTListBoxItem* pItem = m_Items.GetAt( i ); + auto pItem = m_Items[ i ]; pItem->bSelected = bLastSelected; } @@ -5885,9 +5288,9 @@ bool CDXUTListBox::HandleMouse( UINT uMsg, POINT pt, WPARAM wParam, LPARAM lPara // item. - for( int i = 0; i < ( int )m_Items.GetSize(); ++i ) + for( int i = 0; i < ( int )m_Items.size(); ++i ) { - DXUTListBoxItem* pItem = m_Items.GetAt( i ); + auto pItem = m_Items[ i ]; pItem->bSelected = false; } @@ -5911,9 +5314,9 @@ bool CDXUTListBox::HandleMouse( UINT uMsg, POINT pt, WPARAM wParam, LPARAM lPara { // Set all items between m_nSelStart and m_nSelected to // the same state as m_nSelStart - int nEnd = __max( m_nSelStart, m_nSelected ); + int nEnd = std::max( m_nSelStart, m_nSelected ); - for( int n = __min( m_nSelStart, m_nSelected ) + 1; n < nEnd; ++n ) + for( int n = std::min( m_nSelStart, m_nSelected ) + 1; n < nEnd; ++n ) m_Items[n]->bSelected = m_Items[m_nSelStart]->bSelected; m_Items[m_nSelected]->bSelected = m_Items[m_nSelStart]->bSelected; @@ -5942,7 +5345,7 @@ bool CDXUTListBox::HandleMouse( UINT uMsg, POINT pt, WPARAM wParam, LPARAM lPara // Only proceed if the cursor is on top of an item. if( nItem >= ( int )m_ScrollBar.GetTrackPos() && - nItem < ( int )m_Items.GetSize() && + nItem < ( int )m_Items.size() && nItem < m_ScrollBar.GetTrackPos() + m_ScrollBar.GetPageSize() ) { m_nSelected = nItem; @@ -5959,7 +5362,7 @@ bool CDXUTListBox::HandleMouse( UINT uMsg, POINT pt, WPARAM wParam, LPARAM lPara { // User drags the mouse below window bottom m_ScrollBar.Scroll( 1 ); - m_nSelected = __min( ( int )m_Items.GetSize(), m_ScrollBar.GetTrackPos() + + m_nSelected = std::min( ( int )m_Items.size(), m_ScrollBar.GetTrackPos() + m_ScrollBar.GetPageSize() ) - 1; m_pDialog->SendEvent( EVENT_LISTBOX_SELECTION, true, this ); } @@ -5968,8 +5371,9 @@ bool CDXUTListBox::HandleMouse( UINT uMsg, POINT pt, WPARAM wParam, LPARAM lPara case WM_MOUSEWHEEL: { - UINT uLines; - SystemParametersInfo( SPI_GETWHEELSCROLLLINES, 0, &uLines, 0 ); + UINT uLines = 0; + if ( !SystemParametersInfo( SPI_GETWHEELSCROLLLINES, 0, &uLines, 0 ) ) + uLines = 0; int nScrollAmount = int( ( short )HIWORD( wParam ) ) / WHEEL_DELTA * uLines; m_ScrollBar.Scroll( -nScrollAmount ); return true; @@ -5981,8 +5385,11 @@ bool CDXUTListBox::HandleMouse( UINT uMsg, POINT pt, WPARAM wParam, LPARAM lPara //-------------------------------------------------------------------------------------- +_Use_decl_annotations_ bool CDXUTListBox::MsgProc( UINT uMsg, WPARAM wParam, LPARAM lParam ) { + UNREFERENCED_PARAMETER(wParam); + if( WM_CAPTURECHANGED == uMsg ) { // The application just lost mouse capture. We may not have gotten @@ -5996,23 +5403,23 @@ bool CDXUTListBox::MsgProc( UINT uMsg, WPARAM wParam, LPARAM lParam ) //-------------------------------------------------------------------------------------- -void CDXUTListBox::Render( float fElapsedTime ) +void CDXUTListBox::Render( _In_ float fElapsedTime ) { if( m_bVisible == false ) return; - CDXUTElement* pElement = m_Elements.GetAt( 0 ); + auto pElement = m_Elements[ 0 ]; pElement->TextureColor.Blend( DXUT_STATE_NORMAL, fElapsedTime ); pElement->FontColor.Blend( DXUT_STATE_NORMAL, fElapsedTime ); - CDXUTElement* pSelElement = m_Elements.GetAt( 1 ); + auto pSelElement = m_Elements[ 1 ]; pSelElement->TextureColor.Blend( DXUT_STATE_NORMAL, fElapsedTime ); pSelElement->FontColor.Blend( DXUT_STATE_NORMAL, fElapsedTime ); m_pDialog->DrawSprite( pElement, &m_rcBoundingBox, DXUT_FAR_BUTTON_DEPTH ); // Render the text - if( m_Items.GetSize() > 0 ) + if( !m_Items.empty() ) { // Find out the height of a single line of text RECT rc = m_rcText; @@ -6034,12 +5441,12 @@ void CDXUTListBox::Render( float fElapsedTime ) } rc.right = m_rcText.right; - for( int i = m_ScrollBar.GetTrackPos(); i < ( int )m_Items.GetSize(); ++i ) + for( int i = m_ScrollBar.GetTrackPos(); i < ( int )m_Items.size(); ++i ) { if( rc.bottom > m_rcText.bottom ) break; - DXUTListBoxItem* pItem = m_Items.GetAt( i ); + auto pItem = m_Items[ i ]; // Determine if we need to render this item with the // selected element. @@ -6076,34 +5483,19 @@ void CDXUTListBox::Render( float fElapsedTime ) } -// Static member initialization -HINSTANCE CUniBuffer::s_hDll = NULL; -HRESULT ( WINAPI*CUniBuffer::_ScriptApplyDigitSubstitution )( const SCRIPT_DIGITSUBSTITUTE*, SCRIPT_CONTROL*, - SCRIPT_STATE* ) = Dummy_ScriptApplyDigitSubstitution; -HRESULT ( WINAPI*CUniBuffer::_ScriptStringAnalyse )( HDC, const void*, int, int, int, DWORD, int, SCRIPT_CONTROL*, - SCRIPT_STATE*, const int*, SCRIPT_TABDEF*, const BYTE*, - SCRIPT_STRING_ANALYSIS* ) = Dummy_ScriptStringAnalyse; -HRESULT ( WINAPI*CUniBuffer::_ScriptStringCPtoX )( SCRIPT_STRING_ANALYSIS, int, BOOL, int* ) = Dummy_ScriptStringCPtoX; -HRESULT ( WINAPI*CUniBuffer::_ScriptStringXtoCP )( SCRIPT_STRING_ANALYSIS, int, int*, int* ) = Dummy_ScriptStringXtoCP; -HRESULT ( WINAPI*CUniBuffer::_ScriptStringFree )( SCRIPT_STRING_ANALYSIS* ) = Dummy_ScriptStringFree; -const SCRIPT_LOGATTR* ( WINAPI*CUniBuffer::_ScriptString_pLogAttr )( SCRIPT_STRING_ANALYSIS ) = - Dummy_ScriptString_pLogAttr; -const int* ( WINAPI*CUniBuffer::_ScriptString_pcOutChars )( SCRIPT_STRING_ANALYSIS ) = - Dummy_ScriptString_pcOutChars; -bool CDXUTEditBox::s_bHideCaret; // If true, we don't render the caret. - - - -//-------------------------------------------------------------------------------------- +//====================================================================================== // CDXUTEditBox class -//-------------------------------------------------------------------------------------- +//====================================================================================== + +// Static member initialization +bool CDXUTEditBox::s_bHideCaret; // If true, we don't render the caret. // When scrolling, EDITBOX_SCROLLEXTENT is reciprocal of the amount to scroll. // If EDITBOX_SCROLLEXTENT = 4, then we scroll 1/4 of the control each time. #define EDITBOX_SCROLLEXTENT 4 //-------------------------------------------------------------------------------------- -CDXUTEditBox::CDXUTEditBox( CDXUTDialog* pDialog ) +CDXUTEditBox::CDXUTEditBox( _In_opt_ CDXUTDialog* pDialog ) { m_Type = DXUT_CONTROL_EDITBOX; m_pDialog = pDialog; @@ -6137,7 +5529,7 @@ CDXUTEditBox::~CDXUTEditBox() // PlaceCaret: Set the caret to a character position, and adjust the scrolling if // necessary. //-------------------------------------------------------------------------------------- -void CDXUTEditBox::PlaceCaret( int nCP ) +void CDXUTEditBox::PlaceCaret( _In_ int nCP ) { assert( nCP >= 0 && nCP <= m_Buffer.GetTextSize() ); m_nCaret = nCP; @@ -6146,7 +5538,7 @@ void CDXUTEditBox::PlaceCaret( int nCP ) int nX1st, nX, nX2; m_Buffer.CPtoX( m_nFirstVisible, FALSE, &nX1st ); // 1st visible char m_Buffer.CPtoX( nCP, FALSE, &nX ); // LEAD - // If nCP is the NULL terminator, get the leading edge instead of trailing. + // If nCP is the nul terminator, get the leading edge instead of trailing. if( nCP == m_Buffer.GetTextSize() ) nX2 = nX; else @@ -6194,9 +5586,9 @@ void CDXUTEditBox::ClearText() //-------------------------------------------------------------------------------------- -void CDXUTEditBox::SetText( LPCWSTR wszText, bool bSelected ) +void CDXUTEditBox::SetText( _In_z_ LPCWSTR wszText, _In_ bool bSelected ) { - assert( wszText != NULL ); + assert( wszText ); m_Buffer.SetText( wszText ); m_nFirstVisible = 0; @@ -6207,8 +5599,8 @@ void CDXUTEditBox::SetText( LPCWSTR wszText, bool bSelected ) //-------------------------------------------------------------------------------------- -HRESULT CDXUTEditBox::GetTextCopy( __out_ecount(bufferCount) LPWSTR strDest, - UINT bufferCount ) +_Use_decl_annotations_ +HRESULT CDXUTEditBox::GetTextCopy( LPWSTR strDest, UINT bufferCount ) const { assert( strDest ); @@ -6221,8 +5613,8 @@ HRESULT CDXUTEditBox::GetTextCopy( __out_ecount(bufferCount) LPWSTR strDest, //-------------------------------------------------------------------------------------- void CDXUTEditBox::DeleteSelectionText() { - int nFirst = __min( m_nCaret, m_nSelStart ); - int nLast = __max( m_nCaret, m_nSelStart ); + int nFirst = std::min( m_nCaret, m_nSelStart ); + int nLast = std::max( m_nCaret, m_nSelStart ); // Update caret and selection PlaceCaret( nFirst ); m_nSelStart = m_nCaret; @@ -6258,23 +5650,27 @@ void CDXUTEditBox::UpdateRects() } +#pragma warning(push) +#pragma warning( disable : 4616 6386 ) void CDXUTEditBox::CopyToClipboard() { // Copy the selection text to the clipboard - if( m_nCaret != m_nSelStart && OpenClipboard( NULL ) ) + if( m_nCaret != m_nSelStart && OpenClipboard( nullptr ) ) { EmptyClipboard(); HGLOBAL hBlock = GlobalAlloc( GMEM_MOVEABLE, sizeof( WCHAR ) * ( m_Buffer.GetTextSize() + 1 ) ); if( hBlock ) { - WCHAR* pwszText = ( WCHAR* )GlobalLock( hBlock ); + auto pwszText = reinterpret_cast<WCHAR*>( GlobalLock( hBlock ) ); if( pwszText ) { - int nFirst = __min( m_nCaret, m_nSelStart ); - int nLast = __max( m_nCaret, m_nSelStart ); + int nFirst = std::min( m_nCaret, m_nSelStart ); + int nLast = std::max( m_nCaret, m_nSelStart ); if( nLast - nFirst > 0 ) - CopyMemory( pwszText, m_Buffer.GetBuffer() + nFirst, ( nLast - nFirst ) * sizeof( WCHAR ) ); + { + memcpy( pwszText, m_Buffer.GetBuffer() + nFirst, ( nLast - nFirst ) * sizeof( WCHAR ) ); + } pwszText[nLast - nFirst] = L'\0'; // Terminate it GlobalUnlock( hBlock ); } @@ -6292,19 +5688,19 @@ void CDXUTEditBox::PasteFromClipboard() { DeleteSelectionText(); - if( OpenClipboard( NULL ) ) + if( OpenClipboard( nullptr ) ) { HANDLE handle = GetClipboardData( CF_UNICODETEXT ); if( handle ) { // Convert the ANSI string to Unicode, then // insert to our buffer. - WCHAR* pwszText = ( WCHAR* )GlobalLock( handle ); + auto pwszText = reinterpret_cast<WCHAR*>( GlobalLock( handle ) ); if( pwszText ) { // Copy all characters up to null. if( m_Buffer.InsertString( m_nCaret, pwszText ) ) - PlaceCaret( m_nCaret + lstrlenW( pwszText ) ); + PlaceCaret( m_nCaret + (int)wcslen( pwszText ) ); m_nSelStart = m_nCaret; GlobalUnlock( handle ); } @@ -6312,11 +5708,15 @@ void CDXUTEditBox::PasteFromClipboard() CloseClipboard(); } } +#pragma warning(pop) //-------------------------------------------------------------------------------------- +_Use_decl_annotations_ bool CDXUTEditBox::HandleKeyboard( UINT uMsg, WPARAM wParam, LPARAM lParam ) { + UNREFERENCED_PARAMETER(lParam); + if( !m_bEnabled || !m_bVisible ) return false; @@ -6441,8 +5841,12 @@ bool CDXUTEditBox::HandleKeyboard( UINT uMsg, WPARAM wParam, LPARAM lParam ) //-------------------------------------------------------------------------------------- -bool CDXUTEditBox::HandleMouse( UINT uMsg, POINT pt, WPARAM wParam, LPARAM lParam ) +_Use_decl_annotations_ +bool CDXUTEditBox::HandleMouse( UINT uMsg, const POINT& pt, WPARAM wParam, LPARAM lParam ) { + UNREFERENCED_PARAMETER(wParam); + UNREFERENCED_PARAMETER(lParam); + if( !m_bEnabled || !m_bVisible ) return false; @@ -6462,9 +5866,9 @@ bool CDXUTEditBox::HandleMouse( UINT uMsg, POINT pt, WPARAM wParam, LPARAM lPara // Determine the character corresponding to the coordinates. int nCP, nTrail, nX1st; m_Buffer.CPtoX( m_nFirstVisible, FALSE, &nX1st ); // X offset of the 1st visible char - if( SUCCEEDED( m_Buffer.XtoCP( pt.x - m_rcText.left + nX1st, &nCP, &nTrail ) ) ) + if( m_Buffer.XtoCP( pt.x - m_rcText.left + nX1st, &nCP, &nTrail ) ) { - // Cap at the NULL character. + // Cap at the nul character. if( nTrail && nCP < m_Buffer.GetTextSize() ) PlaceCaret( nCP + 1 ); else @@ -6486,9 +5890,9 @@ bool CDXUTEditBox::HandleMouse( UINT uMsg, POINT pt, WPARAM wParam, LPARAM lPara // Determine the character corresponding to the coordinates. int nCP, nTrail, nX1st; m_Buffer.CPtoX( m_nFirstVisible, FALSE, &nX1st ); // X offset of the 1st visible char - if( SUCCEEDED( m_Buffer.XtoCP( pt.x - m_rcText.left + nX1st, &nCP, &nTrail ) ) ) + if( m_Buffer.XtoCP( pt.x - m_rcText.left + nX1st, &nCP, &nTrail ) ) { - // Cap at the NULL character. + // Cap at the nul character. if( nTrail && nCP < m_Buffer.GetTextSize() ) PlaceCaret( nCP + 1 ); else @@ -6512,14 +5916,17 @@ void CDXUTEditBox::OnFocusIn() //-------------------------------------------------------------------------------------- +_Use_decl_annotations_ bool CDXUTEditBox::MsgProc( UINT uMsg, WPARAM wParam, LPARAM lParam ) { + UNREFERENCED_PARAMETER(lParam); + if( !m_bEnabled || !m_bVisible ) return false; switch( uMsg ) { - // Make sure that while editing, the keyup and keydown messages associated with + // Make sure that while editing, the keyup and keydown messages associated with // WM_CHAR messages don't go to any non-focused controls or cameras case WM_KEYUP: case WM_KEYDOWN: @@ -6611,7 +6018,7 @@ bool CDXUTEditBox::MsgProc( UINT uMsg, WPARAM wParam, LPARAM lParam ) case 16: // Ctrl P case 27: // Ctrl [ case 29: // Ctrl ] - case 28: // Ctrl \ + case 28: // Ctrl backslash break; default: @@ -6652,15 +6059,14 @@ bool CDXUTEditBox::MsgProc( UINT uMsg, WPARAM wParam, LPARAM lParam ) //-------------------------------------------------------------------------------------- -void CDXUTEditBox::Render( float fElapsedTime ) +void CDXUTEditBox::Render( _In_ float fElapsedTime ) { if( m_bVisible == false ) return; - HRESULT hr; int nSelStartX = 0, nCaretX = 0; // Left and right X cordinates of the selection region - CDXUTElement* pElement = GetElement( 0 ); + auto pElement = GetElement( 0 ); if( pElement ) { m_Buffer.SetFontNode( m_pDialog->GetFont( pElement->iFont ) ); @@ -6671,7 +6077,7 @@ void CDXUTEditBox::Render( float fElapsedTime ) // Render the control graphics for( int e = 0; e < 9; ++e ) { - pElement = m_Elements.GetAt( e ); + pElement = m_Elements[ e ]; pElement->TextureColor.Blend( DXUT_STATE_NORMAL, fElapsedTime ); m_pDialog->DrawSprite( pElement, &m_rcRender[e], DXUT_FAR_BUTTON_DEPTH ); @@ -6686,9 +6092,9 @@ void CDXUTEditBox::Render( float fElapsedTime ) // // Compute the X coordinates of the selection rectangle // - hr = m_Buffer.CPtoX( m_nCaret, FALSE, &nCaretX ); + m_Buffer.CPtoX( m_nCaret, FALSE, &nCaretX ); if( m_nCaret != m_nSelStart ) - hr = m_Buffer.CPtoX( m_nSelStart, FALSE, &nSelStartX ); + m_Buffer.CPtoX( m_nSelStart, FALSE, &nSelStartX ); else nSelStartX = nCaretX; @@ -6709,29 +6115,23 @@ void CDXUTEditBox::Render( float fElapsedTime ) OffsetRect( &rcSelection, m_rcText.left - nXFirst, 0 ); IntersectRect( &rcSelection, &m_rcText, &rcSelection ); - IDirect3DDevice9* pd3dDevice = m_pDialog->GetManager()->GetD3D9Device(); - if( pd3dDevice ) - pd3dDevice->SetRenderState( D3DRS_ZENABLE, FALSE ); m_pDialog->DrawRect( &rcSelection, m_SelBkColor ); - if( pd3dDevice ) - pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE ); } // // Render the text // // Element 0 for text - m_Elements.GetAt( 0 )->FontColor.Current = m_TextColor; - m_pDialog->DrawText( m_Buffer.GetBuffer() + m_nFirstVisible, m_Elements.GetAt( 0 ), &m_rcText ); + m_Elements[ 0 ]->FontColor.SetCurrent( m_TextColor ); + m_pDialog->DrawText( m_Buffer.GetBuffer() + m_nFirstVisible, m_Elements[ 0 ], &m_rcText ); // Render the selected text if( m_nCaret != m_nSelStart ) { - int nFirstToRender = __max( m_nFirstVisible, __min( m_nSelStart, m_nCaret ) ); - int nNumChatToRender = __max( m_nSelStart, m_nCaret ) - nFirstToRender; - m_Elements.GetAt( 0 )->FontColor.Current = m_SelTextColor; + int nFirstToRender = std::max( m_nFirstVisible, std::min( m_nSelStart, m_nCaret ) ); + m_Elements[ 0 ]->FontColor.SetCurrent( m_SelTextColor ); m_pDialog->DrawText( m_Buffer.GetBuffer() + nFirstToRender, - m_Elements.GetAt( 0 ), &rcSelection, false, nNumChatToRender ); + m_Elements[ 0 ], &rcSelection, false ); } // @@ -6773,6 +6173,7 @@ void CDXUTEditBox::Render( float fElapsedTime ) #define IN_FLOAT_CHARSET( c ) \ ( (c) == L'-' || (c) == L'.' || ( (c) >= L'0' && (c) <= L'9' ) ) +_Use_decl_annotations_ void CDXUTEditBox::ParseFloatArray( float* pNumbers, int nCount ) { int nWritten = 0; // Number of floats written @@ -6795,9 +6196,9 @@ void CDXUTEditBox::ParseFloatArray( float* pNumbers, int nCount ) ++pEnd; // Copy the token to our buffer - int nTokenLen = __min( sizeof( wszToken ) / sizeof( wszToken[0] ) - 1, int( pEnd - pToken ) ); + int nTokenLen = std::min<int>( sizeof( wszToken ) / sizeof( wszToken[0] ) - 1, int( pEnd - pToken ) ); wcscpy_s( wszToken, nTokenLen, pToken ); - *pNumbers = ( float )wcstod( wszToken, NULL ); + *pNumbers = ( float )wcstod( wszToken, nullptr ); ++nWritten; ++pNumbers; pToken = pEnd; @@ -6805,6 +6206,8 @@ void CDXUTEditBox::ParseFloatArray( float* pNumbers, int nCount ) } +//-------------------------------------------------------------------------------------- +_Use_decl_annotations_ void CDXUTEditBox::SetTextFloatArray( const float* pNumbers, int nCount ) { WCHAR wszBuffer[512] = @@ -6813,7 +6216,7 @@ void CDXUTEditBox::SetTextFloatArray( const float* pNumbers, int nCount ) }; WCHAR wszTmp[64]; - if( pNumbers == NULL ) + if( !pNumbers ) return; for( int i = 0; i < nCount; ++i ) @@ -6830,54 +6233,24 @@ void CDXUTEditBox::SetTextFloatArray( const float* pNumbers, int nCount ) } - - //-------------------------------------------------------------------------------------- -void CUniBuffer::Initialize() +void CDXUTEditBox::ResetCaretBlink() { - if( s_hDll ) // Only need to do once - return; - - s_hDll = LoadLibrary( UNISCRIBE_DLLNAME ); - if( s_hDll ) - { - FARPROC Temp; - GETPROCADDRESS( s_hDll, ScriptApplyDigitSubstitution, Temp ); - GETPROCADDRESS( s_hDll, ScriptStringAnalyse, Temp ); - GETPROCADDRESS( s_hDll, ScriptStringCPtoX, Temp ); - GETPROCADDRESS( s_hDll, ScriptStringXtoCP, Temp ); - GETPROCADDRESS( s_hDll, ScriptStringFree, Temp ); - GETPROCADDRESS( s_hDll, ScriptString_pLogAttr, Temp ); - GETPROCADDRESS( s_hDll, ScriptString_pcOutChars, Temp ); - } + m_bCaretOn = true; + m_dfLastBlink = DXUTGetGlobalTimer()->GetAbsoluteTime(); } -//-------------------------------------------------------------------------------------- -void CUniBuffer::Uninitialize() -{ - if( s_hDll ) - { - PLACEHOLDERPROC( ScriptApplyDigitSubstitution ); - PLACEHOLDERPROC( ScriptStringAnalyse ); - PLACEHOLDERPROC( ScriptStringCPtoX ); - PLACEHOLDERPROC( ScriptStringXtoCP ); - PLACEHOLDERPROC( ScriptStringFree ); - PLACEHOLDERPROC( ScriptString_pLogAttr ); - PLACEHOLDERPROC( ScriptString_pcOutChars ); - - FreeLibrary( s_hDll ); - s_hDll = NULL; - } -} - +//====================================================================================== +// CUniBuffer +//====================================================================================== //-------------------------------------------------------------------------------------- -bool CUniBuffer::SetBufferSize( int nNewSize ) +bool CUniBuffer::SetBufferSize( _In_ int nNewSize ) { // If the current size is already the maximum allowed, // we can't possibly allocate more. - if( m_nBufferSize == DXUT_MAX_EDITBOXLENGTH ) + if( m_nBufferSize >= DXUT_MAX_EDITBOXLENGTH ) return false; int nAllocateSize = ( nNewSize == -1 || nNewSize < m_nBufferSize * 2 ) ? ( m_nBufferSize ? m_nBufferSize * @@ -6887,7 +6260,7 @@ bool CUniBuffer::SetBufferSize( int nNewSize ) if( nAllocateSize > DXUT_MAX_EDITBOXLENGTH ) nAllocateSize = DXUT_MAX_EDITBOXLENGTH; - WCHAR* pTempBuffer = new WCHAR[nAllocateSize]; + auto pTempBuffer = new (std::nothrow) WCHAR[nAllocateSize]; if( !pTempBuffer ) return false; @@ -6895,7 +6268,7 @@ bool CUniBuffer::SetBufferSize( int nNewSize ) if( m_pwszBuffer ) { - CopyMemory( pTempBuffer, m_pwszBuffer, m_nBufferSize * sizeof( WCHAR ) ); + memcpy( pTempBuffer, m_pwszBuffer, m_nBufferSize * sizeof( WCHAR ) ); delete[] m_pwszBuffer; } @@ -6911,32 +6284,37 @@ bool CUniBuffer::SetBufferSize( int nNewSize ) HRESULT CUniBuffer::Analyse() { if( m_Analysis ) - _ScriptStringFree( &m_Analysis ); + (void)ScriptStringFree( &m_Analysis ); SCRIPT_CONTROL ScriptControl; // For uniscribe SCRIPT_STATE ScriptState; // For uniscribe ZeroMemory( &ScriptControl, sizeof( ScriptControl ) ); ZeroMemory( &ScriptState, sizeof( ScriptState ) ); - _ScriptApplyDigitSubstitution( NULL, &ScriptControl, &ScriptState ); + +#pragma warning(push) +#pragma warning(disable : 4616 6309 6387 ) + HRESULT hr = ScriptApplyDigitSubstitution( nullptr, &ScriptControl, &ScriptState ); + if ( FAILED(hr) ) + return hr; +#pragma warning(pop) if( !m_pFontNode ) return E_FAIL; - HDC hDC = - ( m_pFontNode->pFont9 ? m_pFontNode->pFont9->GetDC() : NULL ); - HRESULT hr = _ScriptStringAnalyse( hDC, - m_pwszBuffer, - lstrlenW( m_pwszBuffer ) + 1, // NULL is also analyzed. - lstrlenW( m_pwszBuffer ) * 3 / 2 + 16, - -1, - SSA_BREAK | SSA_GLYPHS | SSA_FALLBACK | SSA_LINK, - 0, - &ScriptControl, - &ScriptState, - NULL, - NULL, - NULL, - &m_Analysis ); + HDC hDC = nullptr; + hr = ScriptStringAnalyse( hDC, + m_pwszBuffer, + (int)wcslen( m_pwszBuffer ) + 1, // nul is also analyzed. + (int)wcslen( m_pwszBuffer ) * 3 / 2 + 16, + -1, + SSA_BREAK | SSA_GLYPHS | SSA_FALLBACK | SSA_LINK, + 0, + &ScriptControl, + &ScriptState, + nullptr, + nullptr, + nullptr, + &m_Analysis ); if( SUCCEEDED( hr ) ) m_bAnalyseRequired = false; // Analysis is up-to-date return hr; @@ -6944,15 +6322,13 @@ HRESULT CUniBuffer::Analyse() //-------------------------------------------------------------------------------------- -CUniBuffer::CUniBuffer( int nInitialSize ) +CUniBuffer::CUniBuffer( _In_ int nInitialSize ) { - CUniBuffer::Initialize(); // ensure static vars are properly init'ed first - m_nBufferSize = 0; - m_pwszBuffer = NULL; + m_pwszBuffer = nullptr; m_bAnalyseRequired = true; - m_Analysis = NULL; - m_pFontNode = NULL; + m_Analysis = nullptr; + m_pFontNode = nullptr; if( nInitialSize > 0 ) SetBufferSize( nInitialSize ); @@ -6964,12 +6340,12 @@ CUniBuffer::~CUniBuffer() { delete[] m_pwszBuffer; if( m_Analysis ) - _ScriptStringFree( &m_Analysis ); + (void)ScriptStringFree( &m_Analysis ); } //-------------------------------------------------------------------------------------- -WCHAR& CUniBuffer::operator[]( int n ) // No param checking +WCHAR& CUniBuffer::operator[]( _In_ int n ) // No param checking { // This version of operator[] is called only // if we are asking for write access, so @@ -6991,18 +6367,18 @@ void CUniBuffer::Clear() // Inserts the char at specified index. // If nIndex == -1, insert to the end. //-------------------------------------------------------------------------------------- -bool CUniBuffer::InsertChar( int nIndex, WCHAR wChar ) +bool CUniBuffer::InsertChar( _In_ int nIndex, _In_ WCHAR wChar ) { assert( nIndex >= 0 ); - if( nIndex < 0 || nIndex > lstrlenW( m_pwszBuffer ) ) + if( nIndex < 0 || nIndex > (int)wcslen( m_pwszBuffer ) ) return false; // invalid index // Check for maximum length allowed if( GetTextSize() + 1 >= DXUT_MAX_EDITBOXLENGTH ) return false; - if( lstrlenW( m_pwszBuffer ) + 1 >= m_nBufferSize ) + if( (int)wcslen( m_pwszBuffer ) + 1 >= m_nBufferSize ) { if( !SetBufferSize( -1 ) ) return false; // out of memory @@ -7011,7 +6387,7 @@ bool CUniBuffer::InsertChar( int nIndex, WCHAR wChar ) assert( m_nBufferSize >= 2 ); // Shift the characters after the index, start by copying the null terminator - WCHAR* dest = m_pwszBuffer + lstrlenW( m_pwszBuffer ) + 1; + WCHAR* dest = m_pwszBuffer + wcslen( m_pwszBuffer ) + 1; WCHAR* stop = m_pwszBuffer + nIndex; WCHAR* src = dest - 1; @@ -7032,13 +6408,13 @@ bool CUniBuffer::InsertChar( int nIndex, WCHAR wChar ) // Removes the char at specified index. // If nIndex == -1, remove the last char. //-------------------------------------------------------------------------------------- -bool CUniBuffer::RemoveChar( int nIndex ) +bool CUniBuffer::RemoveChar( _In_ int nIndex ) { - if( !lstrlenW( m_pwszBuffer ) || nIndex < 0 || nIndex >= lstrlenW( m_pwszBuffer ) ) + if( !wcslen( m_pwszBuffer ) || nIndex < 0 || nIndex >= (int)wcslen( m_pwszBuffer ) ) return false; // Invalid index MoveMemory( m_pwszBuffer + nIndex, m_pwszBuffer + nIndex + 1, sizeof( WCHAR ) * - ( lstrlenW( m_pwszBuffer ) - nIndex ) ); + ( wcslen( m_pwszBuffer ) - nIndex ) ); m_bAnalyseRequired = true; return true; } @@ -7049,31 +6425,32 @@ bool CUniBuffer::RemoveChar( int nIndex ) // If nCount == -1, the entire string is inserted. // If nIndex == -1, insert to the end. //-------------------------------------------------------------------------------------- +_Use_decl_annotations_ bool CUniBuffer::InsertString( int nIndex, const WCHAR* pStr, int nCount ) { assert( nIndex >= 0 ); if( nIndex < 0 ) return false; - if( nIndex > lstrlenW( m_pwszBuffer ) ) + if( nIndex > (int)wcslen( m_pwszBuffer ) ) return false; // invalid index if( -1 == nCount ) - nCount = lstrlenW( pStr ); + nCount = (int)wcslen( pStr ); // Check for maximum length allowed if( GetTextSize() + nCount >= DXUT_MAX_EDITBOXLENGTH ) return false; - if( lstrlenW( m_pwszBuffer ) + nCount >= m_nBufferSize ) + if( (int)wcslen( m_pwszBuffer ) + nCount >= m_nBufferSize ) { - if( !SetBufferSize( lstrlenW( m_pwszBuffer ) + nCount + 1 ) ) + if( !SetBufferSize( (int)wcslen( m_pwszBuffer ) + nCount + 1 ) ) return false; // out of memory } MoveMemory( m_pwszBuffer + nIndex + nCount, m_pwszBuffer + nIndex, sizeof( WCHAR ) * - ( lstrlenW( m_pwszBuffer ) - nIndex + 1 ) ); - CopyMemory( m_pwszBuffer + nIndex, pStr, nCount * sizeof( WCHAR ) ); + ( wcslen( m_pwszBuffer ) - nIndex + 1 ) ); + memcpy( m_pwszBuffer + nIndex, pStr, nCount * sizeof( WCHAR ) ); m_bAnalyseRequired = true; return true; @@ -7081,11 +6458,11 @@ bool CUniBuffer::InsertString( int nIndex, const WCHAR* pStr, int nCount ) //-------------------------------------------------------------------------------------- -bool CUniBuffer::SetText( LPCWSTR wszText ) +bool CUniBuffer::SetText( _In_z_ LPCWSTR wszText ) { - assert( wszText != NULL ); + assert( wszText ); - int nRequired = int( wcslen( wszText ) + 1 ); + size_t nRequired = wcslen( wszText ) + 1; // Check for maximum length allowed if( nRequired >= DXUT_MAX_EDITBOXLENGTH ) @@ -7107,7 +6484,8 @@ bool CUniBuffer::SetText( LPCWSTR wszText ) //-------------------------------------------------------------------------------------- -HRESULT CUniBuffer::CPtoX( int nCP, BOOL bTrail, int* pX ) +_Use_decl_annotations_ +bool CUniBuffer::CPtoX( int nCP, bool bTrail, int* pX ) { assert( pX ); *pX = 0; // Default @@ -7117,14 +6495,21 @@ HRESULT CUniBuffer::CPtoX( int nCP, BOOL bTrail, int* pX ) hr = Analyse(); if( SUCCEEDED( hr ) ) - hr = _ScriptStringCPtoX( m_Analysis, nCP, bTrail, pX ); + hr = ScriptStringCPtoX( m_Analysis, nCP, bTrail, pX ); - return hr; + if ( FAILED(hr) ) + { + *pX = 0; + return false; + } + + return true; } //-------------------------------------------------------------------------------------- -HRESULT CUniBuffer::XtoCP( int nX, int* pCP, int* pnTrail ) +_Use_decl_annotations_ +bool CUniBuffer::XtoCP( int nX, int* pCP, int* pnTrail ) { assert( pCP && pnTrail ); *pCP = 0; *pnTrail = FALSE; // Default @@ -7133,8 +6518,15 @@ HRESULT CUniBuffer::XtoCP( int nX, int* pCP, int* pnTrail ) if( m_bAnalyseRequired ) hr = Analyse(); - if( SUCCEEDED( hr ) ) - hr = _ScriptStringXtoCP( m_Analysis, nX, pCP, pnTrail ); + if (SUCCEEDED(hr)) + { + hr = ScriptStringXtoCP( m_Analysis, nX, pCP, pnTrail ); + if (FAILED(hr)) + { + *pCP = 0; *pnTrail = FALSE; + return false; + } + } // If the coordinate falls outside the text region, we // can get character positions that don't exist. We must @@ -7143,16 +6535,22 @@ HRESULT CUniBuffer::XtoCP( int nX, int* pCP, int* pnTrail ) { *pCP = 0; *pnTrail = FALSE; } - else if( *pCP > lstrlenW( m_pwszBuffer ) && *pnTrail == FALSE ) + else if( *pCP > (int)wcslen( m_pwszBuffer ) && *pnTrail == FALSE ) { - *pCP = lstrlenW( m_pwszBuffer ); *pnTrail = TRUE; + *pCP = (int)wcslen( m_pwszBuffer ); *pnTrail = TRUE; } - return hr; + if (FAILED(hr)) + { + *pCP = 0; *pnTrail = FALSE; + return false; + } + return true; } //-------------------------------------------------------------------------------------- +_Use_decl_annotations_ void CUniBuffer::GetPriorItemPos( int nCP, int* pPrior ) { *pPrior = nCP; // Default is the char itself @@ -7161,13 +6559,13 @@ void CUniBuffer::GetPriorItemPos( int nCP, int* pPrior ) if( FAILED( Analyse() ) ) return; - const SCRIPT_LOGATTR* pLogAttr = _ScriptString_pLogAttr( m_Analysis ); + const SCRIPT_LOGATTR* pLogAttr = ScriptString_pLogAttr( m_Analysis ); if( !pLogAttr ) return; - if( !_ScriptString_pcOutChars( m_Analysis ) ) + if( !ScriptString_pcOutChars( m_Analysis ) ) return; - int nInitial = *_ScriptString_pcOutChars( m_Analysis ); + int nInitial = *ScriptString_pcOutChars( m_Analysis ); if( nCP - 1 < nInitial ) nInitial = nCP - 1; for( int i = nInitial; i > 0; --i ) @@ -7184,6 +6582,7 @@ void CUniBuffer::GetPriorItemPos( int nCP, int* pPrior ) //-------------------------------------------------------------------------------------- +_Use_decl_annotations_ void CUniBuffer::GetNextItemPos( int nCP, int* pPrior ) { *pPrior = nCP; // Default is the char itself @@ -7194,18 +6593,18 @@ void CUniBuffer::GetNextItemPos( int nCP, int* pPrior ) if( FAILED( hr ) ) return; - const SCRIPT_LOGATTR* pLogAttr = _ScriptString_pLogAttr( m_Analysis ); + const SCRIPT_LOGATTR* pLogAttr = ScriptString_pLogAttr( m_Analysis ); if( !pLogAttr ) return; - if( !_ScriptString_pcOutChars( m_Analysis ) ) + if( !ScriptString_pcOutChars( m_Analysis ) ) return; - int nInitial = *_ScriptString_pcOutChars( m_Analysis ); + int nInitial = *ScriptString_pcOutChars( m_Analysis ); if( nCP + 1 < nInitial ) nInitial = nCP + 1; int i = nInitial; - int limit = *_ScriptString_pcOutChars( m_Analysis ); + int limit = *ScriptString_pcOutChars( m_Analysis ); while( limit > 0 && i < limit - 1 ) { if( pLogAttr[i].fWordStop ) // Either the fWordStop flag is set @@ -7221,23 +6620,20 @@ void CUniBuffer::GetNextItemPos( int nCP, int* pPrior ) } ++i; - limit = *_ScriptString_pcOutChars( m_Analysis ); + limit = *ScriptString_pcOutChars( m_Analysis ); } // We have reached the end. It's always a word stop, so simply return it. - *pPrior = *_ScriptString_pcOutChars( m_Analysis ) - 1; + *pPrior = *ScriptString_pcOutChars( m_Analysis ) - 1; } -//-------------------------------------------------------------------------------------- -void CDXUTEditBox::ResetCaretBlink() -{ - m_bCaretOn = true; - m_dfLastBlink = DXUTGetGlobalTimer()->GetAbsoluteTime(); -} - +//====================================================================================== +// DXUTBlendColor +//====================================================================================== //-------------------------------------------------------------------------------------- -void DXUTBlendColor::Init( D3DCOLOR defaultColor, D3DCOLOR disabledColor, D3DCOLOR hiddenColor ) +_Use_decl_annotations_ +void DXUTBlendColor::Init( DWORD defaultColor, DWORD disabledColor, DWORD hiddenColor ) { for( int i = 0; i < MAX_CONTROL_STATES; i++ ) { @@ -7246,23 +6642,38 @@ void DXUTBlendColor::Init( D3DCOLOR defaultColor, D3DCOLOR disabledColor, D3DCOL States[ DXUT_STATE_DISABLED ] = disabledColor; States[ DXUT_STATE_HIDDEN ] = hiddenColor; - Current = hiddenColor; + SetCurrent( hiddenColor ); } //-------------------------------------------------------------------------------------- +_Use_decl_annotations_ void DXUTBlendColor::Blend( UINT iState, float fElapsedTime, float fRate ) { - D3DXCOLOR destColor = States[ iState ]; - D3DXColorLerp( &Current, &Current, &destColor, 1.0f - powf( fRate, 30 * fElapsedTime ) ); + XMFLOAT4 destColor = D3DCOLOR_TO_D3DCOLORVALUE( States[ iState ] ); + XMVECTOR clr1 = XMLoadFloat4( &destColor ); + XMVECTOR clr = XMLoadFloat4( &Current ); + clr = XMVectorLerp( clr, clr1, 1.0f - powf( fRate, 30 * fElapsedTime ) ); + XMStoreFloat4( &Current, clr ); } +//-------------------------------------------------------------------------------------- +void DXUTBlendColor::SetCurrent( DWORD color ) +{ + Current = D3DCOLOR_TO_D3DCOLORVALUE( color ); +} + + +//====================================================================================== +// CDXUTElement +//====================================================================================== //-------------------------------------------------------------------------------------- -void CDXUTElement::SetTexture( UINT iTexture, RECT* prcTexture, D3DCOLOR defaultTextureColor ) +_Use_decl_annotations_ +void CDXUTElement::SetTexture( UINT texture, RECT* prcTexture, DWORD defaultTextureColor ) { - this->iTexture = iTexture; + iTexture = texture; if( prcTexture ) rcTexture = *prcTexture; @@ -7274,10 +6685,11 @@ void CDXUTElement::SetTexture( UINT iTexture, RECT* prcTexture, D3DCOLOR default //-------------------------------------------------------------------------------------- -void CDXUTElement::SetFont( UINT iFont, D3DCOLOR defaultFontColor, DWORD dwTextFormat ) +_Use_decl_annotations_ +void CDXUTElement::SetFont( UINT font, DWORD defaultFontColor, DWORD textFormat ) { - this->iFont = iFont; - this->dwTextFormat = dwTextFormat; + iFont = font; + dwTextFormat = textFormat; FontColor.Init( defaultFontColor ); } @@ -7286,8 +6698,6 @@ void CDXUTElement::SetFont( UINT iFont, D3DCOLOR defaultFontColor, DWORD dwTextF //-------------------------------------------------------------------------------------- void CDXUTElement::Refresh() { - TextureColor.Current = TextureColor.States[ DXUT_STATE_HIDDEN ]; - FontColor.Current = FontColor.States[ DXUT_STATE_HIDDEN ]; + TextureColor.SetCurrent( TextureColor.States[ DXUT_STATE_HIDDEN ] ); + FontColor.SetCurrent( FontColor.States[ DXUT_STATE_HIDDEN ] ); } - - |