DXUTgui.cpp
资源名称:Direct3D.rar [点击查看]
上传用户:junlon
上传日期:2022-01-05
资源大小:39075k
文件大小:284k
源码类别:
DirextX编程
开发平台:
Visual C++
- //--------------------------------------------------------------------------------------
- // File: DXUTgui.cpp
- //
- // Desc:
- //
- // Copyright (c) Microsoft Corporation. All rights reserved.
- //--------------------------------------------------------------------------------------
- #include "dxstdafx.h"
- #include "DXUTgui.h"
- #include "DXUTsettingsDlg.h"
- #undef min // use __min instead
- #undef max // use __max instead
- #ifndef WM_XBUTTONDOWN
- #define WM_XBUTTONDOWN 0x020B // (not always defined)
- #endif
- #ifndef WM_XBUTTONUP
- #define WM_XBUTTONUP 0x020C // (not always defined)
- #endif
- #ifndef WM_MOUSEWHEEL
- #define WM_MOUSEWHEEL 0x020A // (not always defined)
- #endif
- #ifndef WHEEL_DELTA
- #define WHEEL_DELTA 120 // (not always defined)
- #endif
- // Minimum scroll bar thumb size
- #define SCROLLBAR_MINTHUMBSIZE 8
- // Delay and repeat period when clicking on the scroll bar arrows
- #define SCROLLBAR_ARROWCLICK_DELAY 0.33
- #define SCROLLBAR_ARROWCLICK_REPEAT 0.05
- #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"
- // DXUT_MAX_EDITBOXLENGTH is the maximum string length allowed in edit boxes,
- // including the NULL 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.
- #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
- struct DXUT_SCREEN_VERTEX
- {
- float x, y, z, h;
- D3DCOLOR 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 DXUT_SCREEN_VERTEX_UNTEX::FVF = D3DFVF_XYZRHW | D3DFVF_DIFFUSE;
- inline int RectWidth( RECT &rc ) { return ( (rc).right - (rc).left ); }
- inline int RectHeight( RECT &rc ) { return ( (rc).bottom - (rc).top ); }
- //--------------------------------------------------------------------------------------
- // 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' ';
- 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;
- }
- //--------------------------------------------------------------------------------------
- CDXUTDialog::~CDXUTDialog()
- {
- int i=0;
- RemoveAllControls();
- m_Fonts.RemoveAll();
- m_Textures.RemoveAll();
- for( i=0; i < m_DefaultElements.GetSize(); i++ )
- {
- DXUTElementHolder* pElementHolder = m_DefaultElements.GetAt( i );
- SAFE_DELETE( pElementHolder );
- }
- m_DefaultElements.RemoveAll();
- }
- //--------------------------------------------------------------------------------------
- void CDXUTDialog::Init( CDXUTDialogResourceManager* pManager, bool bRegisterDialog )
- {
- m_pManager = pManager;
- if( bRegisterDialog )
- pManager->RegisterDialog( this );
- SetTexture( 0, MAKEINTRESOURCE(0xFFFF), (HMODULE)0xFFFF );
- InitDefaultElements();
- }
- //--------------------------------------------------------------------------------------
- void CDXUTDialog::Init( CDXUTDialogResourceManager* pManager, bool bRegisterDialog, LPCWSTR pszControlTextureFilename )
- {
- m_pManager = pManager;
- if( bRegisterDialog )
- pManager->RegisterDialog( this );
- SetTexture( 0, pszControlTextureFilename );
- InitDefaultElements();
- }
- //--------------------------------------------------------------------------------------
- void CDXUTDialog::Init( CDXUTDialogResourceManager* pManager, bool bRegisterDialog, LPCWSTR szControlTextureResourceName, HMODULE hControlTextureResourceModule )
- {
- m_pManager = pManager;
- if( bRegisterDialog )
- pManager->RegisterDialog( this );
- SetTexture( 0, szControlTextureResourceName, hControlTextureResourceModule );
- InitDefaultElements();
- }
- //--------------------------------------------------------------------------------------
- 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." );
- m_pCallbackEvent = pCallback;
- m_pCallbackEventUserContext = pUserContext;
- }
- //--------------------------------------------------------------------------------------
- void CDXUTDialog::RemoveControl( int ID )
- {
- for( int i=0; i < m_Controls.GetSize(); i++ )
- {
- CDXUTControl* pControl = m_Controls.GetAt( i );
- if( pControl->GetID() == ID )
- {
- // 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 );
- return;
- }
- }
- }
- //--------------------------------------------------------------------------------------
- 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();
- }
- //--------------------------------------------------------------------------------------
- CDXUTDialogResourceManager::CDXUTDialogResourceManager()
- {
- m_pd3dDevice = NULL;
- m_pStateBlock = NULL;
- m_pSprite = NULL;
- }
- //--------------------------------------------------------------------------------------
- CDXUTDialogResourceManager::~CDXUTDialogResourceManager()
- {
- int i;
- for( i=0; i < m_FontCache.GetSize(); i++ )
- {
- DXUTFontNode* pFontNode = m_FontCache.GetAt( i );
- SAFE_DELETE( pFontNode );
- }
- m_FontCache.RemoveAll();
- for( i=0; i < m_TextureCache.GetSize(); i++ )
- {
- DXUTTextureNode* pTextureNode = m_TextureCache.GetAt( i );
- SAFE_DELETE( pTextureNode );
- }
- m_TextureCache.RemoveAll();
- CUniBuffer::Uninitialize();
- CDXUTIMEEditBox::Uninitialize();
- }
- //--------------------------------------------------------------------------------------
- HRESULT CDXUTDialogResourceManager::OnCreateDevice( LPDIRECT3DDEVICE9 pd3dDevice )
- {
- HRESULT hr = S_OK;
- int i=0;
- m_pd3dDevice = pd3dDevice;
- for( i=0; i < m_FontCache.GetSize(); i++ )
- {
- hr = CreateFont( i );
- if( FAILED(hr) )
- return hr;
- }
- for( i=0; i < m_TextureCache.GetSize(); i++ )
- {
- hr = CreateTexture( i );
- if( FAILED(hr) )
- return hr;
- }
- hr = D3DXCreateSprite( pd3dDevice, &m_pSprite );
- if( FAILED(hr) )
- return DXUT_ERR( L"D3DXCreateSprite", hr );
- // Call CDXUTIMEEditBox's StaticOnCreateDevice()
- // to initialize certain window-dependent data.
- CDXUTIMEEditBox::StaticOnCreateDevice();
- return S_OK;
- }
- //--------------------------------------------------------------------------------------
- HRESULT CDXUTDialogResourceManager::OnResetDevice()
- {
- HRESULT hr = S_OK;
- for( int i=0; i < m_FontCache.GetSize(); i++ )
- {
- DXUTFontNode* pFontNode = m_FontCache.GetAt( i );
- if( pFontNode->pFont )
- pFontNode->pFont->OnResetDevice();
- }
- if( m_pSprite )
- m_pSprite->OnResetDevice();
- IDirect3DDevice9* pd3dDevice = DXUTGetD3DDevice();
- V_RETURN( pd3dDevice->CreateStateBlock( D3DSBT_ALL, &m_pStateBlock ) );
- return S_OK;
- }
- //--------------------------------------------------------------------------------------
- bool CDXUTDialogResourceManager::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
- {
- // Let the CDXUTIMEEditBox's static message proc handle the msg.
- // This is because some IME messages must be handled to ensure
- // proper functionalities and the static msg proc ensures that
- // this happens even if no control has the input focus.
- if( CDXUTIMEEditBox::StaticMsgProc( uMsg, wParam, lParam ) )
- return true;
- return false;
- }
- //--------------------------------------------------------------------------------------
- void CDXUTDialogResourceManager::OnLostDevice()
- {
- for( int i=0; i < m_FontCache.GetSize(); i++ )
- {
- DXUTFontNode* pFontNode = m_FontCache.GetAt( i );
- if( pFontNode->pFont )
- pFontNode->pFont->OnLostDevice();
- }
- if( m_pSprite )
- m_pSprite->OnLostDevice();
- SAFE_RELEASE( m_pStateBlock );
- }
- //--------------------------------------------------------------------------------------
- void CDXUTDialogResourceManager::OnDestroyDevice()
- {
- int i=0;
- m_pd3dDevice = 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->pFont );
- }
- for( i=0; i < m_TextureCache.GetSize(); i++ )
- {
- DXUTTextureNode* pTextureNode = m_TextureCache.GetAt( i );
- SAFE_RELEASE( pTextureNode->pTexture );
- }
- SAFE_RELEASE( m_pSprite );
- }
- //--------------------------------------------------------------------------------------
- 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 )
- {
- m_Dialogs.Remove( i );
- if( m_Dialogs.GetSize() > 0 )
- {
- int l, r;
- if( 0 == i )
- l = m_Dialogs.GetSize() - 1;
- else
- l = i - 1;
- if( m_Dialogs.GetSize() == i )
- r = 0;
- else
- r = i;
- m_Dialogs[l]->SetNextDialog( m_Dialogs[r] );
- }
- return;
- }
- }
- //--------------------------------------------------------------------------------------
- void CDXUTDialogResourceManager::EnableKeyboardInputForAllDialogs()
- {
- // Enable keyboard input for all registered dialogs
- for( int i = 0; i < m_Dialogs.GetSize(); ++i )
- m_Dialogs[i]->EnableKeyboardInput( true );
- }
- //--------------------------------------------------------------------------------------
- void CDXUTDialog::Refresh()
- {
- if( s_pControlFocus )
- s_pControlFocus->OnFocusOut();
- if( m_pControlMouseOver )
- m_pControlMouseOver->OnMouseLeave();
- s_pControlFocus = NULL;
- s_pControlPressed = NULL;
- m_pControlMouseOver = NULL;
- for( int i=0; i < m_Controls.GetSize(); i++ )
- {
- CDXUTControl* pControl = m_Controls.GetAt(i);
- pControl->Refresh();
- }
- if( m_bKeyboardInput )
- FocusDefaultControl();
- }
- //--------------------------------------------------------------------------------------
- HRESULT CDXUTDialog::OnRender( 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->GetD3DDevice() && 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->GetD3DDevice();
- // Set up a state block here and restore it when finished drawing all the controls
- m_pManager->m_pStateBlock->Capture();
- 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->pTexture );
- 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 };
- DrawSprite( &m_CapElement, &rc );
- rc.left += 5; // Make a left margin
- WCHAR wszOutput[256];
- StringCchCopy( wszOutput, 256, m_wszCaption );
- if( m_bMinimized )
- StringCchCat( wszOutput, 256, L" (Minimized)" );
- 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++ )
- {
- CDXUTControl* pControl = m_Controls.GetAt(i);
- // Focused control is drawn last
- if( pControl == s_pControlFocus )
- continue;
- pControl->Render( pd3dDevice, fElapsedTime );
- }
- if( s_pControlFocus != NULL && s_pControlFocus->m_pDialog == this )
- s_pControlFocus->Render( pd3dDevice, fElapsedTime );
- }
- m_pManager->m_pSprite->End();
- m_pManager->m_pStateBlock->Apply();
- return S_OK;
- }
- //--------------------------------------------------------------------------------------
- VOID CDXUTDialog::SendEvent( UINT nEvent, bool bTriggeredByUser, CDXUTControl* pControl )
- {
- // If no callback has been registered there's nowhere to send the event to
- if( m_pCallbackEvent == NULL )
- return;
- // Discard events triggered programatically if these types of events haven't been
- // enabled
- if( !bTriggeredByUser && !m_bNonUserEvents )
- return;
- m_pCallbackEvent( nEvent, pControl->GetID(), pControl, m_pCallbackEventUserContext );
- }
- //--------------------------------------------------------------------------------------
- int CDXUTDialogResourceManager::AddFont( LPCWSTR strFaceName, LONG height, LONG weight )
- {
- // See if this font already exists
- for( int i=0; i < m_FontCache.GetSize(); i++ )
- {
- DXUTFontNode* pFontNode = m_FontCache.GetAt(i);
- size_t nLen = 0;
- StringCchLength( strFaceName, MAX_PATH, &nLen );
- if( 0 == _wcsnicmp( pFontNode->strFace, strFaceName, nLen ) &&
- pFontNode->nHeight == height &&
- pFontNode->nWeight == weight )
- {
- return i;
- }
- }
- // Add a new font and try to create it
- DXUTFontNode* pNewFontNode = new DXUTFontNode();
- if( pNewFontNode == NULL )
- return -1;
- ZeroMemory( pNewFontNode, sizeof(DXUTFontNode) );
- StringCchCopy( pNewFontNode->strFace, MAX_PATH, strFaceName );
- pNewFontNode->nHeight = height;
- pNewFontNode->nWeight = weight;
- m_FontCache.Add( pNewFontNode );
- int iFont = m_FontCache.GetSize()-1;
- // If a device is available, try to create immediately
- if( m_pd3dDevice )
- CreateFont( iFont );
- return iFont;
- }
- //--------------------------------------------------------------------------------------
- HRESULT CDXUTDialog::SetFont( UINT index, LPCWSTR strFaceName, LONG height, LONG weight )
- {
- // If this assert triggers, you need to call CDXUTDialog::Init() first. This change
- // was made so that the DXUT's GUI could become seperate and optional from DXUT's core. The
- // creation and interfacing with CDXUTDialogResourceManager is now the responsibility
- // of the application if it wishes to use DXUT's GUI.
- assert( m_pManager != NULL && L"To fix call CDXUTDialog::Init() first. See comments for details." );
- // Make sure the list is at least as large as the index being set
- UINT i;
- for( i=m_Fonts.GetSize(); i <= index; i++ )
- {
- m_Fonts.Add( -1 );
- }
- int iFont = m_pManager->AddFont( strFaceName, height, weight );
- m_Fonts.SetAt( index, iFont );
- return S_OK;
- }
- //--------------------------------------------------------------------------------------
- DXUTFontNode* CDXUTDialog::GetFont( UINT index )
- {
- if( NULL == m_pManager )
- return NULL;
- return m_pManager->GetFontNode( m_Fonts.GetAt( index ) );
- }
- //--------------------------------------------------------------------------------------
- int CDXUTDialogResourceManager::AddTexture( LPCWSTR strFilename )
- {
- // See if this texture already exists
- for( int i=0; i < m_TextureCache.GetSize(); i++ )
- {
- DXUTTextureNode* pTextureNode = m_TextureCache.GetAt(i);
- size_t nLen = 0;
- StringCchLength( strFilename, MAX_PATH, &nLen );
- if( pTextureNode->bFileSource && // Sources must match
- 0 == _wcsnicmp( pTextureNode->strFilename, strFilename, nLen ) )
- return i;
- }
- // Add a new texture and try to create it
- DXUTTextureNode* pNewTextureNode = new DXUTTextureNode();
- if( pNewTextureNode == NULL )
- return -1;
- ZeroMemory( pNewTextureNode, sizeof(DXUTTextureNode) );
- pNewTextureNode->bFileSource = true;
- StringCchCopy( pNewTextureNode->strFilename, MAX_PATH, strFilename );
- m_TextureCache.Add( pNewTextureNode );
- int iTexture = m_TextureCache.GetSize()-1;
- // If a device is available, try to create immediately
- if( m_pd3dDevice )
- CreateTexture( iTexture );
- return iTexture;
- }
- //--------------------------------------------------------------------------------------
- int CDXUTDialogResourceManager::AddTexture( LPCWSTR strResourceName, HMODULE hResourceModule )
- {
- // See if this texture already exists
- for( int i=0; i < m_TextureCache.GetSize(); i++ )
- {
- DXUTTextureNode* pTextureNode = m_TextureCache.GetAt(i);
- if( !pTextureNode->bFileSource && // Sources must match
- pTextureNode->hResourceModule == hResourceModule ) // Module handles must match
- {
- if( IS_INTRESOURCE( strResourceName ) )
- {
- // Integer-based ID
- if( (INT_PTR)strResourceName == pTextureNode->nResourceID )
- return i;
- }
- else
- {
- // String-based ID
- size_t nLen = 0;
- StringCchLength( strResourceName, MAX_PATH, &nLen );
- if( 0 == _wcsnicmp( pTextureNode->strFilename, strResourceName, nLen ) )
- return i;
- }
- }
- }
- // Add a new texture and try to create it
- DXUTTextureNode* pNewTextureNode = new DXUTTextureNode();
- if( pNewTextureNode == NULL )
- return -1;
- ZeroMemory( pNewTextureNode, sizeof(DXUTTextureNode) );
- pNewTextureNode->hResourceModule = hResourceModule;
- if( IS_INTRESOURCE( strResourceName ) )
- {
- pNewTextureNode->nResourceID = (int)(size_t)strResourceName;
- }
- else
- {
- pNewTextureNode->nResourceID = 0;
- StringCchCopy( pNewTextureNode->strFilename, MAX_PATH, strResourceName );
- }
- m_TextureCache.Add( pNewTextureNode );
- int iTexture = m_TextureCache.GetSize()-1;
- // If a device is available, try to create immediately
- if( m_pd3dDevice )
- CreateTexture( iTexture );
- return iTexture;
- }
- //--------------------------------------------------------------------------------------
- HRESULT CDXUTDialog::SetTexture( UINT index, LPCWSTR strFilename )
- {
- // If this assert triggers, you need to call CDXUTDialog::Init() first. This change
- // was made so that the DXUT's GUI could become seperate and optional from DXUT's core. The
- // creation and interfacing with CDXUTDialogResourceManager is now the responsibility
- // of the application if it wishes to use DXUT's GUI.
- assert( m_pManager != NULL && L"To fix this, call CDXUTDialog::Init() first. See comments for details." );
- // Make sure the list is at least as large as the index being set
- for( UINT i=m_Textures.GetSize(); i <= index; i++ )
- {
- m_Textures.Add( -1 );
- }
- int iTexture = m_pManager->AddTexture( strFilename );
- m_Textures.SetAt( index, iTexture );
- return S_OK;
- }
- //--------------------------------------------------------------------------------------
- HRESULT CDXUTDialog::SetTexture( UINT index, LPCWSTR strResourceName, HMODULE hResourceModule )
- {
- // If this assert triggers, you need to call CDXUTDialog::Init() first. This change
- // was made so that the DXUT's GUI could become seperate and optional from DXUT's core. The
- // creation and interfacing with CDXUTDialogResourceManager is now the responsibility
- // of the application if it wishes to use DXUT's GUI.
- assert( m_pManager != NULL && L"To fix this, call CDXUTDialog::Init() first. See comments for details." );
- // Make sure the list is at least as large as the index being set
- for( UINT i=m_Textures.GetSize(); i <= index; i++ )
- {
- m_Textures.Add( -1 );
- }
- int iTexture = m_pManager->AddTexture( strResourceName, hResourceModule );
- m_Textures.SetAt( index, iTexture );
- return S_OK;
- }
- //--------------------------------------------------------------------------------------
- DXUTTextureNode* CDXUTDialog::GetTexture( UINT index )
- {
- if( NULL == m_pManager )
- return NULL;
- return m_pManager->GetTextureNode( m_Textures.GetAt( index ) );
- }
- //--------------------------------------------------------------------------------------
- bool CDXUTDialog::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
- {
- bool bHandled = false;
- // For invisible dialog, do not handle anything.
- if( !m_bVisible )
- return false;
- // If automation command-line switch is on, enable this dialog's keyboard input
- // upon any key press or mouse click.
- if( DXUTGetAutomation() &&
- ( WM_LBUTTONDOWN == uMsg || WM_LBUTTONDBLCLK == uMsg || WM_KEYDOWN == uMsg ) )
- {
- m_pManager->EnableKeyboardInputForAllDialogs();
- }
- // If caption is enable, check for clicks in the caption area.
- if( m_bCaption )
- {
- if( uMsg == WM_LBUTTONDOWN || uMsg == WM_LBUTTONDBLCLK )
- {
- POINT mousePoint = { short(LOWORD(lParam)), short(HIWORD(lParam)) };
- if( mousePoint.x >= m_x && mousePoint.x < m_x + m_width &&
- mousePoint.y >= m_y && mousePoint.y < m_y + m_nCaptionHeight )
- {
- m_bDrag = true;
- SetCapture( DXUTGetHWND() );
- return true;
- }
- } else
- if( uMsg == WM_LBUTTONUP && m_bDrag )
- {
- POINT mousePoint = { short(LOWORD(lParam)), short(HIWORD(lParam)) };
- if( mousePoint.x >= m_x && mousePoint.x < m_x + m_width &&
- mousePoint.y >= m_y && mousePoint.y < m_y + m_nCaptionHeight )
- {
- ReleaseCapture();
- m_bDrag = false;
- m_bMinimized = !m_bMinimized;
- return true;
- }
- }
- }
- // If the dialog is minimized, don't send any messages to controls.
- if( m_bMinimized )
- return false;
- // If a control is in focus, it belongs to this dialog, and it's enabled, then give
- // it the first chance at handling the message.
- if( s_pControlFocus &&
- s_pControlFocus->m_pDialog == this &&
- s_pControlFocus->GetEnabled() )
- {
- // If the control MsgProc handles it, then we don't.
- if( s_pControlFocus->MsgProc( uMsg, wParam, lParam ) )
- return true;
- }
- switch( uMsg )
- {
- case WM_SIZE:
- case WM_MOVE:
- {
- // Handle sizing and moving messages so that in case the mouse cursor is moved out
- // of an UI control because of the window adjustment, we can properly
- // unhighlight the highlighted control.
- POINT pt = { -1, -1 };
- OnMouseMove( pt );
- break;
- }
- case WM_ACTIVATEAPP:
- // Call OnFocusIn()/OnFocusOut() of the control that currently has the focus
- // as the application is activated/deactivated. This matches the Windows
- // behavior.
- if( s_pControlFocus &&
- s_pControlFocus->m_pDialog == this &&
- s_pControlFocus->GetEnabled() )
- {
- if( wParam )
- s_pControlFocus->OnFocusIn();
- else
- s_pControlFocus->OnFocusOut();
- }
- break;
- // Keyboard messages
- case WM_KEYDOWN:
- case WM_SYSKEYDOWN:
- case WM_KEYUP:
- case WM_SYSKEYUP:
- {
- // If a control is in focus, it belongs to this dialog, and it's enabled, then give
- // it the first chance at handling the message.
- if( s_pControlFocus &&
- s_pControlFocus->m_pDialog == this &&
- s_pControlFocus->GetEnabled() )
- {
- if( s_pControlFocus->HandleKeyboard( uMsg, wParam, lParam ) )
- return true;
- }
- // Not yet handled, see if this matches a control's hotkey
- // Activate the hotkey if the focus doesn't belong to an
- // edit box.
- if( uMsg == WM_KEYDOWN && ( !s_pControlFocus ||
- ( s_pControlFocus->GetType() != DXUT_CONTROL_EDITBOX
- && s_pControlFocus->GetType() != DXUT_CONTROL_IMEEDITBOX ) ) )
- {
- for( int i=0; i < m_Controls.GetSize(); i++ )
- {
- CDXUTControl* pControl = m_Controls.GetAt( i );
- if( pControl->GetHotkey() == wParam )
- {
- pControl->OnHotkey();
- return true;
- }
- }
- }
- // Not yet handled, check for focus messages
- if( uMsg == WM_KEYDOWN )
- {
- // If keyboard input is not enabled, this message should be ignored
- if( !m_bKeyboardInput )
- return false;
- switch( wParam )
- {
- case VK_RIGHT:
- case VK_DOWN:
- if( s_pControlFocus != NULL )
- {
- return OnCycleFocus( true );
- }
- break;
- case VK_LEFT:
- case VK_UP:
- if( s_pControlFocus != NULL )
- {
- return OnCycleFocus( false );
- }
- break;
- case VK_TAB:
- {
- bool bShiftDown = ((GetKeyState( VK_SHIFT ) & 0x8000) != 0);
- return OnCycleFocus( !bShiftDown );
- }
- }
- }
- break;
- }
- // Mouse messages
- case WM_MOUSEMOVE:
- case WM_LBUTTONDOWN:
- case WM_LBUTTONUP:
- case WM_MBUTTONDOWN:
- case WM_MBUTTONUP:
- case WM_RBUTTONDOWN:
- case WM_RBUTTONUP:
- case WM_XBUTTONDOWN:
- case WM_XBUTTONUP:
- case WM_LBUTTONDBLCLK:
- case WM_MBUTTONDBLCLK:
- case WM_RBUTTONDBLCLK:
- case WM_XBUTTONDBLCLK:
- case WM_MOUSEWHEEL:
- {
- // 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;
- POINT mousePoint = { short(LOWORD(lParam)), short(HIWORD(lParam)) };
- mousePoint.x -= m_x;
- mousePoint.y -= m_y;
- // If caption is enabled, offset the Y coordinate by the negative of its height.
- if( m_bCaption )
- mousePoint.y -= m_nCaptionHeight;
- // If a control is in focus, it belongs to this dialog, and it's enabled, then give
- // it the first chance at handling the message.
- if( s_pControlFocus &&
- s_pControlFocus->m_pDialog == this &&
- s_pControlFocus->GetEnabled() )
- {
- if( s_pControlFocus->HandleMouse( uMsg, mousePoint, wParam, lParam ) )
- return true;
- }
- // Not yet handled, see if the mouse is over any controls
- CDXUTControl* pControl = GetControlAtPoint( mousePoint );
- if( pControl != NULL && pControl->GetEnabled() )
- {
- bHandled = pControl->HandleMouse( uMsg, mousePoint, wParam, lParam );
- if( bHandled )
- return true;
- }
- else
- {
- // Mouse not over any controls in this dialog, if there was a control
- // which had focus it just lost it
- if( uMsg == WM_LBUTTONDOWN &&
- s_pControlFocus &&
- s_pControlFocus->m_pDialog == this )
- {
- s_pControlFocus->OnFocusOut();
- s_pControlFocus = NULL;
- }
- }
- // Still not handled, hand this off to the dialog. Return false to indicate the
- // message should still be handled by the application (usually to move the camera).
- switch( uMsg )
- {
- case WM_MOUSEMOVE:
- OnMouseMove( mousePoint );
- return false;
- }
- break;
- }
- case WM_CAPTURECHANGED:
- {
- // The application has lost mouse capture.
- // The dialog object may not have received
- // a WM_MOUSEUP when capture changed. Reset
- // m_bDrag so that the dialog does not mistakenly
- // think the mouse button is still held down.
- if( (HWND)lParam != hWnd )
- m_bDrag = false;
- }
- }
- return false;
- }
- //--------------------------------------------------------------------------------------
- CDXUTControl* CDXUTDialog::GetControlAtPoint( POINT pt )
- {
- // Search through all child controls for the first one which
- // contains the mouse point
- for( int i=0; i < m_Controls.GetSize(); i++ )
- {
- CDXUTControl* pControl = m_Controls.GetAt(i);
- if( pControl == NULL )
- {
- continue;
- }
- // 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() )
- {
- return pControl;
- }
- }
- return NULL;
- }
- //--------------------------------------------------------------------------------------
- bool CDXUTDialog::GetControlEnabled( int ID )
- {
- CDXUTControl* pControl = GetControl( ID );
- if( pControl == NULL )
- return false;
- return pControl->GetEnabled();
- }
- //--------------------------------------------------------------------------------------
- void CDXUTDialog::SetControlEnabled( int ID, bool bEnabled )
- {
- CDXUTControl* pControl = GetControl( ID );
- if( pControl == NULL )
- return;
- pControl->SetEnabled( bEnabled );
- }
- //--------------------------------------------------------------------------------------
- void CDXUTDialog::OnMouseUp( POINT pt )
- {
- s_pControlPressed = NULL;
- m_pControlMouseOver = NULL;
- }
- //--------------------------------------------------------------------------------------
- void CDXUTDialog::OnMouseMove( POINT pt )
- {
- // Figure out which control the mouse is over now
- CDXUTControl* pControl = GetControlAtPoint( pt );
- // If the mouse is still over the same control, nothing needs to be done
- if( pControl == m_pControlMouseOver )
- return;
- // Handle mouse leaving the old control
- if( m_pControlMouseOver )
- m_pControlMouseOver->OnMouseLeave();
- // Handle mouse entering the new control
- m_pControlMouseOver = pControl;
- if( pControl != NULL )
- m_pControlMouseOver->OnMouseEnter();
- }
- //--------------------------------------------------------------------------------------
- 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++ )
- {
- DXUTElementHolder* pElementHolder = m_DefaultElements.GetAt( i );
- if( pElementHolder->nControlType == nControlType &&
- pElementHolder->iElement == iElement )
- {
- pElementHolder->Element = *pElement;
- return S_OK;
- }
- }
- // Otherwise, add a new entry
- DXUTElementHolder* pNewHolder;
- pNewHolder = new DXUTElementHolder;
- if( pNewHolder == NULL )
- return E_OUTOFMEMORY;
- pNewHolder->nControlType = nControlType;
- pNewHolder->iElement = iElement;
- pNewHolder->Element = *pElement;
- m_DefaultElements.Add( pNewHolder );
- return S_OK;
- }
- //--------------------------------------------------------------------------------------
- CDXUTElement* CDXUTDialog::GetDefaultElement( UINT nControlType, UINT iElement )
- {
- for( int i=0; i < m_DefaultElements.GetSize(); i++ )
- {
- DXUTElementHolder* pElementHolder = m_DefaultElements.GetAt( i );
- if( pElementHolder->nControlType == nControlType &&
- pElementHolder->iElement == iElement )
- {
- return &pElementHolder->Element;
- }
- }
- return NULL;
- }
- //--------------------------------------------------------------------------------------
- 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 );
- if( ppCreated != NULL )
- *ppCreated = pStatic;
- if( pStatic == NULL )
- return E_OUTOFMEMORY;
- hr = AddControl( pStatic );
- if( FAILED(hr) )
- return hr;
- // Set the ID and list index
- pStatic->SetID( ID );
- pStatic->SetText( strText );
- pStatic->SetLocation( x, y );
- pStatic->SetSize( width, height );
- pStatic->m_bIsDefault = bIsDefault;
- return S_OK;
- }
- //--------------------------------------------------------------------------------------
- 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 );
- if( ppCreated != NULL )
- *ppCreated = pButton;
- if( pButton == NULL )
- return E_OUTOFMEMORY;
- hr = AddControl( pButton );
- if( FAILED(hr) )
- return hr;
- // Set the ID and list index
- pButton->SetID( ID );
- pButton->SetText( strText );
- pButton->SetLocation( x, y );
- pButton->SetSize( width, height );
- pButton->SetHotkey( nHotkey );
- pButton->m_bIsDefault = bIsDefault;
- return S_OK;
- }
- //--------------------------------------------------------------------------------------
- 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 );
- if( ppCreated != NULL )
- *ppCreated = pCheckBox;
- if( pCheckBox == NULL )
- return E_OUTOFMEMORY;
- hr = AddControl( pCheckBox );
- if( FAILED(hr) )
- return hr;
- // Set the ID and list index
- pCheckBox->SetID( ID );
- pCheckBox->SetText( strText );
- pCheckBox->SetLocation( x, y );
- pCheckBox->SetSize( width, height );
- pCheckBox->SetHotkey( nHotkey );
- pCheckBox->m_bIsDefault = bIsDefault;
- pCheckBox->SetChecked( bChecked );
- return S_OK;
- }
- //--------------------------------------------------------------------------------------
- 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 );
- if( ppCreated != NULL )
- *ppCreated = pRadioButton;
- if( pRadioButton == NULL )
- return E_OUTOFMEMORY;
- hr = AddControl( pRadioButton );
- if( FAILED(hr) )
- return hr;
- // Set the ID and list index
- pRadioButton->SetID( ID );
- pRadioButton->SetText( strText );
- pRadioButton->SetButtonGroup( nButtonGroup );
- pRadioButton->SetLocation( x, y );
- pRadioButton->SetSize( width, height );
- pRadioButton->SetHotkey( nHotkey );
- pRadioButton->SetChecked( bChecked );
- pRadioButton->m_bIsDefault = bIsDefault;
- pRadioButton->SetChecked( bChecked );
- return S_OK;
- }
- //--------------------------------------------------------------------------------------
- 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 );
- if( ppCreated != NULL )
- *ppCreated = pComboBox;
- if( pComboBox == NULL )
- return E_OUTOFMEMORY;
- hr = AddControl( pComboBox );
- if( FAILED(hr) )
- return hr;
- // Set the ID and list index
- pComboBox->SetID( ID );
- pComboBox->SetLocation( x, y );
- pComboBox->SetSize( width, height );
- pComboBox->SetHotkey( nHotkey );
- pComboBox->m_bIsDefault = bIsDefault;
- return S_OK;
- }
- //--------------------------------------------------------------------------------------
- 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 );
- if( ppCreated != NULL )
- *ppCreated = pSlider;
- if( pSlider == NULL )
- return E_OUTOFMEMORY;
- hr = AddControl( pSlider );
- if( FAILED(hr) )
- return hr;
- // Set the ID and list index
- pSlider->SetID( ID );
- pSlider->SetLocation( x, y );
- pSlider->SetSize( width, height );
- pSlider->m_bIsDefault = bIsDefault;
- pSlider->SetRange( min, max );
- pSlider->SetValue( value );
- pSlider->UpdateRects();
- return S_OK;
- }
- //--------------------------------------------------------------------------------------
- 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 );
- if( ppCreated != NULL )
- *ppCreated = pEditBox;
- if( pEditBox == NULL )
- return E_OUTOFMEMORY;
- hr = AddControl( pEditBox );
- if( FAILED(hr) )
- return hr;
- // Set the ID and position
- pEditBox->SetID( ID );
- pEditBox->SetLocation( x, y );
- pEditBox->SetSize( width, height );
- pEditBox->m_bIsDefault = bIsDefault;
- if( strText )
- pEditBox->SetText( strText );
- return S_OK;
- }
- //--------------------------------------------------------------------------------------
- HRESULT CDXUTDialog::AddIMEEditBox( int ID, LPCWSTR strText, int x, int y, int width, int height, bool bIsDefault, CDXUTIMEEditBox** ppCreated )
- {
- HRESULT hr = S_OK;
- CDXUTIMEEditBox *pEditBox = new CDXUTIMEEditBox( this );
- if( ppCreated != NULL )
- *ppCreated = pEditBox;
- if( pEditBox == NULL )
- return E_OUTOFMEMORY;
- hr = AddControl( pEditBox );
- if( FAILED(hr) )
- return hr;
- // Set the ID and position
- pEditBox->SetID( ID );
- pEditBox->SetLocation( x, y );
- pEditBox->SetSize( width, height );
- pEditBox->m_bIsDefault = bIsDefault;
- if( strText )
- pEditBox->SetText( strText );
- return S_OK;
- }
- //--------------------------------------------------------------------------------------
- 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 );
- if( ppCreated != NULL )
- *ppCreated = pListBox;
- if( pListBox == NULL )
- return E_OUTOFMEMORY;
- hr = AddControl( pListBox );
- if( FAILED(hr) )
- return hr;
- // Set the ID and position
- pListBox->SetID( ID );
- pListBox->SetLocation( x, y );
- pListBox->SetSize( width, height );
- pListBox->SetStyle( dwStyle );
- return S_OK;
- }
- //--------------------------------------------------------------------------------------
- HRESULT CDXUTDialog::InitControl( CDXUTControl* pControl )
- {
- HRESULT hr;
- if( pControl == NULL )
- return E_INVALIDARG;
- pControl->m_Index = m_Controls.GetSize();
- // Look for a default Element entries
- for( int i=0; i < m_DefaultElements.GetSize(); i++ )
- {
- DXUTElementHolder* pElementHolder = m_DefaultElements.GetAt( i );
- if( pElementHolder->nControlType == pControl->GetType() )
- pControl->SetElement( pElementHolder->iElement, &pElementHolder->Element );
- }
- V_RETURN( pControl->OnInit() );
- return S_OK;
- }
- //--------------------------------------------------------------------------------------
- HRESULT CDXUTDialog::AddControl( CDXUTControl* pControl )
- {
- HRESULT hr = S_OK;
- hr = InitControl( pControl );
- if( FAILED(hr) )
- 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 );
- }
- return S_OK;
- }
- //--------------------------------------------------------------------------------------
- CDXUTControl* CDXUTDialog::GetControl( int ID )
- {
- // Try to find the control with the given ID
- for( int i=0; i < m_Controls.GetSize(); i++ )
- {
- CDXUTControl* pControl = m_Controls.GetAt( i );
- if( pControl->GetID() == ID )
- {
- return pControl;
- }
- }
- // Not found
- return NULL;
- }
- //--------------------------------------------------------------------------------------
- CDXUTControl* CDXUTDialog::GetControl( int ID, UINT nControlType )
- {
- // Try to find the control with the given ID
- for( int i=0; i < m_Controls.GetSize(); i++ )
- {
- CDXUTControl* pControl = m_Controls.GetAt( i );
- if( pControl->GetID() == ID && pControl->GetType() == nControlType )
- {
- return pControl;
- }
- }
- // Not found
- return NULL;
- }
- //--------------------------------------------------------------------------------------
- CDXUTControl* CDXUTDialog::GetNextControl( CDXUTControl* pControl )
- {
- int index = pControl->m_Index + 1;
- CDXUTDialog* 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() )
- {
- pDialog = pDialog->m_pNextDialog;
- index = 0;
- }
- return pDialog->m_Controls.GetAt( index );
- }
- //--------------------------------------------------------------------------------------
- CDXUTControl* CDXUTDialog::GetPrevControl( CDXUTControl* pControl )
- {
- int index = pControl->m_Index - 1;
- CDXUTDialog* 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 'previous' control.
- while( index < 0 )
- {
- pDialog = pDialog->m_pPrevDialog;
- if( pDialog == NULL )
- pDialog = pControl->m_pDialog;
- index = pDialog->m_Controls.GetSize() - 1;
- }
- return pDialog->m_Controls.GetAt( index );
- }
- //--------------------------------------------------------------------------------------
- void CDXUTDialog::ClearRadioButtonGroup( UINT nButtonGroup )
- {
- // Find all radio buttons with the given group number
- for( int i=0; i < m_Controls.GetSize(); i++ )
- {
- CDXUTControl* pControl = m_Controls.GetAt( i );
- if( pControl->GetType() == DXUT_CONTROL_RADIOBUTTON )
- {
- CDXUTRadioButton* pRadioButton = (CDXUTRadioButton*) pControl;
- if( pRadioButton->GetButtonGroup() == nButtonGroup )
- pRadioButton->SetChecked( false, false );
- }
- }
- }
- //--------------------------------------------------------------------------------------
- void CDXUTDialog::ClearComboBox( int ID )
- {
- CDXUTComboBox* pComboBox = GetComboBox( ID );
- if( pComboBox == NULL )
- return;
- pComboBox->RemoveAllItems();
- }
- //--------------------------------------------------------------------------------------
- void CDXUTDialog::RequestFocus( CDXUTControl* pControl )
- {
- if( s_pControlFocus == pControl )
- return;
- if( !pControl->CanHaveFocus() )
- return;
- if( s_pControlFocus )
- s_pControlFocus->OnFocusOut();
- pControl->OnFocusIn();
- s_pControlFocus = pControl;
- }
- //--------------------------------------------------------------------------------------
- HRESULT CDXUTDialog::DrawRect( 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->GetD3DDevice();
- // 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->GetD3DDevice();
- // 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 )
- {
- // 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->pTexture, &rcTexture, NULL, &vPos, pElement->TextureColor.Current );
- }
- //--------------------------------------------------------------------------------------
- HRESULT CDXUTDialog::CalcTextRect( LPCWSTR strText, CDXUTElement* pElement, RECT* prcDest, int nCount )
- {
- HRESULT hr = S_OK;
- DXUTFontNode* pFontNode = GetFont( pElement->iFont );
- if( pFontNode == NULL )
- return E_FAIL;
- DWORD dwTextFormat = pElement->dwTextFormat | DT_CALCRECT;
- // Since we are only computing the rectangle, we don't need a sprite.
- hr = pFontNode->pFont->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 )
- {
- 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 );
- //debug
- //DrawRect( &rcScreen, D3DCOLOR_ARGB(100, 255, 0, 0) );
- D3DXMATRIXA16 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->pFont->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->pFont->DrawText( m_pManager->m_pSprite, strText, nCount, &rcScreen, pElement->dwTextFormat, pElement->FontColor.Current );
- if( FAILED(hr) )
- return hr;
- return S_OK;
- }
- //--------------------------------------------------------------------------------------
- void CDXUTDialog::SetBackgroundColors( D3DCOLOR colorTopLeft, D3DCOLOR colorTopRight, D3DCOLOR colorBottomLeft, D3DCOLOR colorBottomRight )
- {
- m_colorTopLeft = colorTopLeft;
- m_colorTopRight = colorTopRight;
- m_colorBottomLeft = colorBottomLeft;
- m_colorBottomRight = colorBottomRight;
- }
- //--------------------------------------------------------------------------------------
- void CDXUTDialog::SetNextDialog( CDXUTDialog* pNextDialog )
- {
- if( pNextDialog == NULL )
- pNextDialog = this;
- m_pNextDialog = pNextDialog;
- if( pNextDialog )
- m_pNextDialog->m_pPrevDialog = this;
- }
- //--------------------------------------------------------------------------------------
- void CDXUTDialog::ClearFocus()
- {
- if( s_pControlFocus )
- {
- s_pControlFocus->OnFocusOut();
- s_pControlFocus = NULL;
- }
- ReleaseCapture();
- }
- //--------------------------------------------------------------------------------------
- void CDXUTDialog::FocusDefaultControl()
- {
- // Check for default control in this dialog
- for( int i=0; i < m_Controls.GetSize(); i++ )
- {
- CDXUTControl* pControl = m_Controls.GetAt( i );
- if( pControl->m_bIsDefault )
- {
- // Remove focus from the current control
- ClearFocus();
- // Give focus to the default control
- s_pControlFocus = pControl;
- s_pControlFocus->OnFocusIn();
- return;
- }
- }
- }
- //--------------------------------------------------------------------------------------
- bool CDXUTDialog::OnCycleFocus( bool bForward )
- {
- CDXUTControl *pControl = NULL;
- CDXUTDialog *pDialog = NULL; // 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 is NULL, 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.
- //
- if( bForward )
- {
- // Search for the first control from the start of the dialog
- // array.
- for( int d = 0; d < m_pManager->m_Dialogs.GetSize(); ++d )
- {
- pDialog = pLastDialog = m_pManager->m_Dialogs.GetAt(d);
- if( pDialog && pDialog->m_Controls.GetSize() > 0 )
- {
- pControl = pDialog->m_Controls.GetAt(0);
- break;
- }
- }
- if( !pDialog || !pControl )
- {
- // No dialog has been registered yet or no controls have been
- // added to the dialogs. Cannot proceed.
- return true;
- }
- }
- else
- {
- // Search for the first control from the end of the dialog
- // array.
- for( int d = m_pManager->m_Dialogs.GetSize() - 1; d >= 0; --d )
- {
- pDialog = pLastDialog = m_pManager->m_Dialogs.GetAt(d);
- if( pDialog && pDialog->m_Controls.GetSize() > 0 )
- {
- pControl = pDialog->m_Controls.GetAt( pDialog->m_Controls.GetSize() - 1 );
- break;
- }
- }
- if( !pDialog || !pControl )
- {
- // No dialog has been registered yet or no controls have been
- // added to the dialogs. Cannot proceed.
- return true;
- }
- }
- }
- else
- if( s_pControlFocus->m_pDialog != this )
- {
- // If a control belonging to another dialog has focus, let that other
- // dialog handle this event by returning false.
- //
- return false;
- }
- else
- {
- // Focused control belongs to this dialog. Cycle to the
- // next/previous control.
- pLastDialog = s_pControlFocus->m_pDialog;
- pControl = (bForward) ? GetNextControl( s_pControlFocus ) : GetPrevControl( s_pControlFocus );
- pDialog = pControl->m_pDialog;
- }
- 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
- // has focus, allows the camera to work.
- int nLastDialogIndex = m_pManager->m_Dialogs.IndexOf( pLastDialog );
- int nDialogIndex = m_pManager->m_Dialogs.IndexOf( pDialog );
- if( ( !bForward && nLastDialogIndex < nDialogIndex ) ||
- ( bForward && nDialogIndex < nLastDialogIndex ) )
- {
- if( s_pControlFocus )
- s_pControlFocus->OnFocusOut();
- s_pControlFocus = NULL;
- return true;
- }
- // If we've gone in a full circle then focus doesn't change
- if( pControl == s_pControlFocus )
- return true;
- // If the dialog accepts keybord input and the control can have focus then
- // move focus
- if( pControl->m_pDialog->m_bKeyboardInput && pControl->CanHaveFocus() )
- {
- if( s_pControlFocus )
- s_pControlFocus->OnFocusOut();
- s_pControlFocus = pControl;
- s_pControlFocus->OnFocusIn();
- return true;
- }
- pLastDialog = pDialog;
- pControl = (bForward) ? GetNextControl( pControl ) : GetPrevControl( pControl );
- pDialog = pControl->m_pDialog;
- }
- // If we reached this point, the chain of dialogs didn't form a complete loop
- DXTRACE_ERR( L"CDXUTDialog: Multiple dialogs are improperly chained together", E_FAIL );
- return false;
- }
- //--------------------------------------------------------------------------------------
- HRESULT CDXUTDialogResourceManager::CreateFont( UINT iFont )
- {
- HRESULT hr = S_OK;
- DXUTFontNode* pFontNode = m_FontCache.GetAt( iFont );
- SAFE_RELEASE( pFontNode->pFont );
- V_RETURN( D3DXCreateFont( m_pd3dDevice, pFontNode->nHeight, 0, pFontNode->nWeight, 1, FALSE, DEFAULT_CHARSET,
- OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE,
- pFontNode->strFace, &pFontNode->pFont ) );
- return S_OK;
- }
- //--------------------------------------------------------------------------------------
- HRESULT CDXUTDialogResourceManager::CreateTexture( 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 = DXUTCreateGUITextureFromInternalArray( m_pd3dDevice, &pTextureNode->pTexture, &info );
- if( FAILED(hr) )
- return DXTRACE_ERR( L"D3DXCreateTextureFromFileInMemoryEx", hr );
- }
- else
- {
- LPCWSTR pID = pTextureNode->nResourceID ? (LPCWSTR)(size_t)pTextureNode->nResourceID : pTextureNode->strFilename;
- // Create texture from resource
- hr = D3DXCreateTextureFromResourceEx( m_pd3dDevice, pTextureNode->hResourceModule, pID, D3DX_DEFAULT, D3DX_DEFAULT,
- 1, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED,
- D3DX_DEFAULT, D3DX_DEFAULT, 0,
- &info, NULL, &pTextureNode->pTexture );
- 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;
- // Find the texture on the hard drive
- WCHAR strPath[MAX_PATH];
- hr = DXUTFindDXSDKMediaFileCch( strPath, MAX_PATH, pTextureNode->strFilename );
- if( FAILED(hr) )
- {
- return DXTRACE_ERR( L"DXUTFindDXSDKMediaFileCch", hr );
- }
- // Create texture from file
- hr = D3DXCreateTextureFromFileEx( m_pd3dDevice, strPath, D3DX_DEFAULT, D3DX_DEFAULT,
- 1, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED,
- D3DX_DEFAULT, D3DX_DEFAULT, 0,
- &info, NULL, &pTextureNode->pTexture );
- if( FAILED(hr) )
- {
- return DXTRACE_ERR( L"D3DXCreateTextureFromFileEx", hr );
- }
- }
- // Store dimensions
- pTextureNode->dwWidth = info.Width;
- pTextureNode->dwHeight = info.Height;
- return S_OK;
- }
- //--------------------------------------------------------------------------------------
- void CDXUTDialog::InitDefaultElements()
- {
- SetFont( 0, L"Arial", 14, FW_NORMAL );
- CDXUTElement Element;
- RECT rcTexture;
- //-------------------------------------
- // Element for the caption
- //-------------------------------------
- m_CapElement.SetFont( 0 );
- SetRect( &rcTexture, 17, 269, 241, 287 );
- m_CapElement.SetTexture( 0, &rcTexture );
- m_CapElement.TextureColor.States[ DXUT_STATE_NORMAL ] = D3DCOLOR_ARGB(255, 255, 255, 255);
- m_CapElement.FontColor.States[ DXUT_STATE_NORMAL ] = D3DCOLOR_ARGB(255, 255, 255, 255);
- m_CapElement.SetFont( 0, D3DCOLOR_ARGB(255, 255, 255, 255), DT_LEFT | DT_VCENTER );
- // Pre-blend as we don't need to transition the state
- m_CapElement.TextureColor.Blend( DXUT_STATE_NORMAL, 10.0f );
- m_CapElement.FontColor.Blend( DXUT_STATE_NORMAL, 10.0f );
- //-------------------------------------
- // CDXUTStatic
- //-------------------------------------
- Element.SetFont( 0 );
- Element.FontColor.States[ DXUT_STATE_DISABLED ] = D3DCOLOR_ARGB( 200, 200, 200, 200 );
- // Assign the Element
- SetDefaultElement( DXUT_CONTROL_STATIC, 0, &Element );
- //-------------------------------------
- // CDXUTButton - Button
- //-------------------------------------
- SetRect( &rcTexture, 0, 0, 136, 54 );
- Element.SetTexture( 0, &rcTexture );
- Element.SetFont( 0 );
- Element.TextureColor.States[ DXUT_STATE_NORMAL ] = D3DCOLOR_ARGB(150, 255, 255, 255);
- Element.TextureColor.States[ DXUT_STATE_PRESSED ] = D3DCOLOR_ARGB(200, 255, 255, 255);
- Element.FontColor.States[ DXUT_STATE_MOUSEOVER ] = D3DCOLOR_ARGB(255, 0, 0, 0);
- // Assign the Element
- SetDefaultElement( DXUT_CONTROL_BUTTON, 0, &Element );
- //-------------------------------------
- // CDXUTButton - Fill layer
- //-------------------------------------
- SetRect( &rcTexture, 136, 0, 252, 54 );
- Element.SetTexture( 0, &rcTexture, D3DCOLOR_ARGB(0, 255, 255, 255) );
- Element.TextureColor.States[ DXUT_STATE_MOUSEOVER ] = D3DCOLOR_ARGB(160, 255, 255, 255);
- Element.TextureColor.States[ DXUT_STATE_PRESSED ] = D3DCOLOR_ARGB(60, 0, 0, 0);
- Element.TextureColor.States[ DXUT_STATE_FOCUS ] = D3DCOLOR_ARGB(30, 255, 255, 255);
- // Assign the Element
- SetDefaultElement( DXUT_CONTROL_BUTTON, 1, &Element );
- //-------------------------------------
- // CDXUTCheckBox - Box
- //-------------------------------------
- SetRect( &rcTexture, 0, 54, 27, 81 );
- Element.SetTexture( 0, &rcTexture );
- Element.SetFont( 0, D3DCOLOR_ARGB(255, 255, 255, 255), DT_LEFT | DT_VCENTER );
- Element.FontColor.States[ DXUT_STATE_DISABLED ] = D3DCOLOR_ARGB( 200, 200, 200, 200 );
- Element.TextureColor.States[ DXUT_STATE_NORMAL ] = D3DCOLOR_ARGB(150, 255, 255, 255);
- Element.TextureColor.States[ DXUT_STATE_FOCUS ] = D3DCOLOR_ARGB(200, 255, 255, 255);
- Element.TextureColor.States[ DXUT_STATE_PRESSED ] = D3DCOLOR_ARGB(255, 255, 255, 255);
- // Assign the Element
- SetDefaultElement( DXUT_CONTROL_CHECKBOX, 0, &Element );
- //-------------------------------------
- // CDXUTCheckBox - Check
- //-------------------------------------
- SetRect( &rcTexture, 27, 54, 54, 81 );
- Element.SetTexture( 0, &rcTexture );
- // Assign the Element
- SetDefaultElement( DXUT_CONTROL_CHECKBOX, 1, &Element );
- //-------------------------------------
- // CDXUTRadioButton - Box
- //-------------------------------------
- SetRect( &rcTexture, 54, 54, 81, 81 );
- Element.SetTexture( 0, &rcTexture );
- Element.SetFont( 0, D3DCOLOR_ARGB(255, 255, 255, 255), DT_LEFT | DT_VCENTER );
- Element.FontColor.States[ DXUT_STATE_DISABLED ] = D3DCOLOR_ARGB( 200, 200, 200, 200 );
- Element.TextureColor.States[ DXUT_STATE_NORMAL ] = D3DCOLOR_ARGB(150, 255, 255, 255);
- Element.TextureColor.States[ DXUT_STATE_FOCUS ] = D3DCOLOR_ARGB(200, 255, 255, 255);
- Element.TextureColor.States[ DXUT_STATE_PRESSED ] = D3DCOLOR_ARGB(255, 255, 255, 255);
- // Assign the Element
- SetDefaultElement( DXUT_CONTROL_RADIOBUTTON, 0, &Element );
- //-------------------------------------
- // CDXUTRadioButton - Check
- //-------------------------------------
- SetRect( &rcTexture, 81, 54, 108, 81 );
- Element.SetTexture( 0, &rcTexture );
- // Assign the Element
- SetDefaultElement( DXUT_CONTROL_RADIOBUTTON, 1, &Element );
- //-------------------------------------
- // CDXUTComboBox - Main
- //-------------------------------------
- SetRect( &rcTexture, 7, 81, 247, 123 );
- Element.SetTexture( 0, &rcTexture );
- Element.SetFont( 0 );
- Element.TextureColor.States[ DXUT_STATE_NORMAL ] = D3DCOLOR_ARGB(150, 200, 200, 200);
- Element.TextureColor.States[ DXUT_STATE_FOCUS ] = D3DCOLOR_ARGB(170, 230, 230, 230);
- Element.TextureColor.States[ DXUT_STATE_DISABLED ] = D3DCOLOR_ARGB(70, 200, 200, 200);
- Element.FontColor.States[ DXUT_STATE_MOUSEOVER ] = D3DCOLOR_ARGB(255, 0, 0, 0);
- Element.FontColor.States[ DXUT_STATE_PRESSED ] = D3DCOLOR_ARGB(255, 0, 0, 0);
- Element.FontColor.States[ DXUT_STATE_DISABLED ] = D3DCOLOR_ARGB(200, 200, 200, 200);
- // Assign the Element
- SetDefaultElement( DXUT_CONTROL_COMBOBOX, 0, &Element );
- //-------------------------------------
- // CDXUTComboBox - Button
- //-------------------------------------
- SetRect( &rcTexture, 98, 189, 151, 238 );
- Element.SetTexture( 0, &rcTexture );
- Element.TextureColor.States[ DXUT_STATE_NORMAL ] = D3DCOLOR_ARGB(150, 255, 255, 255);
- Element.TextureColor.States[ DXUT_STATE_PRESSED ] = D3DCOLOR_ARGB(255, 150, 150, 150);
- Element.TextureColor.States[ DXUT_STATE_FOCUS ] = D3DCOLOR_ARGB(200, 255, 255, 255);
- Element.TextureColor.States[ DXUT_STATE_DISABLED ] = D3DCOLOR_ARGB(70, 255, 255, 255);
- // Assign the Element
- SetDefaultElement( DXUT_CONTROL_COMBOBOX, 1, &Element );
- //-------------------------------------
- // CDXUTComboBox - Dropdown
- //-------------------------------------
- SetRect( &rcTexture, 13, 123, 241, 160 );
- Element.SetTexture( 0, &rcTexture );
- Element.SetFont( 0, D3DCOLOR_ARGB(255, 0, 0, 0), DT_LEFT | DT_TOP );
- // Assign the Element
- SetDefaultElement( DXUT_CONTROL_COMBOBOX, 2, &Element );
- //-------------------------------------
- // CDXUTComboBox - Selection
- //-------------------------------------
- SetRect( &rcTexture, 12, 163, 239, 183 );
- Element.SetTexture( 0, &rcTexture );
- Element.SetFont( 0, D3DCOLOR_ARGB(255, 255, 255, 255), DT_LEFT | DT_TOP );
- // Assign the Element
- SetDefaultElement( DXUT_CONTROL_COMBOBOX, 3, &Element );
- //-------------------------------------
- // CDXUTSlider - Track
- //-------------------------------------
- SetRect( &rcTexture, 1, 187, 93, 228 );
- Element.SetTexture( 0, &rcTexture );
- Element.TextureColor.States[ DXUT_STATE_NORMAL ] = D3DCOLOR_ARGB(150, 255, 255, 255);
- Element.TextureColor.States[ DXUT_STATE_FOCUS ] = D3DCOLOR_ARGB(200, 255, 255, 255);
- Element.TextureColor.States[ DXUT_STATE_DISABLED ] = D3DCOLOR_ARGB(70, 255, 255, 255);
- // Assign the Element
- SetDefaultElement( DXUT_CONTROL_SLIDER, 0, &Element );
- //-------------------------------------
- // CDXUTSlider - Button
- //-------------------------------------
- SetRect( &rcTexture, 151, 193, 192, 234 );
- Element.SetTexture( 0, &rcTexture );
- // Assign the Element
- SetDefaultElement( DXUT_CONTROL_SLIDER, 1, &Element );
- //-------------------------------------
- // CDXUTScrollBar - Track
- //-------------------------------------
- int nScrollBarStartX = 196;
- int nScrollBarStartY = 191;
- SetRect( &rcTexture, nScrollBarStartX + 0, nScrollBarStartY + 21, nScrollBarStartX + 22, nScrollBarStartY + 32 );
- Element.SetTexture( 0, &rcTexture );
- Element.TextureColor.States[ DXUT_STATE_DISABLED ] = D3DCOLOR_ARGB(255, 200, 200, 200);
- // Assign the Element
- SetDefaultElement( DXUT_CONTROL_SCROLLBAR, 0, &Element );
- //-------------------------------------
- // CDXUTScrollBar - Up Arrow
- //-------------------------------------
- SetRect( &rcTexture, nScrollBarStartX + 0, nScrollBarStartY + 1, nScrollBarStartX + 22, nScrollBarStartY + 21 );
- Element.SetTexture( 0, &rcTexture );
- Element.TextureColor.States[ DXUT_STATE_DISABLED ] = D3DCOLOR_ARGB(255, 200, 200, 200);
- // Assign the Element
- SetDefaultElement( DXUT_CONTROL_SCROLLBAR, 1, &Element );
- //-------------------------------------
- // CDXUTScrollBar - Down Arrow
- //-------------------------------------
- SetRect( &rcTexture, nScrollBarStartX + 0, nScrollBarStartY + 32, nScrollBarStartX + 22, nScrollBarStartY + 53 );
- Element.SetTexture( 0, &rcTexture );
- Element.TextureColor.States[ DXUT_STATE_DISABLED ] = D3DCOLOR_ARGB(255, 200, 200, 200);
- // Assign the Element
- SetDefaultElement( DXUT_CONTROL_SCROLLBAR, 2, &Element );
- //-------------------------------------
- // CDXUTScrollBar - Button
- //-------------------------------------
- SetRect( &rcTexture, 220, 192, 238, 234 );
- Element.SetTexture( 0, &rcTexture );
- // Assign the Element
- SetDefaultElement( DXUT_CONTROL_SCROLLBAR, 3, &Element );
- //-------------------------------------
- // CDXUTEditBox
- //-------------------------------------
- // Element assignment:
- // 0 - text area
- // 1 - top left border
- // 2 - top border
- // 3 - top right border
- // 4 - left border
- // 5 - right border
- // 6 - lower left border
- // 7 - lower border
- // 8 - lower right border
- Element.SetFont( 0, D3DCOLOR_ARGB( 255, 0, 0, 0 ), DT_LEFT | DT_TOP );
- // Assign the style
- SetRect( &rcTexture, 14, 90, 241, 113 );
- Element.SetTexture( 0, &rcTexture );
- SetDefaultElement( DXUT_CONTROL_EDITBOX, 0, &Element );
- SetRect( &rcTexture, 8, 82, 14, 90 );
- Element.SetTexture( 0, &rcTexture );
- SetDefaultElement( DXUT_CONTROL_EDITBOX, 1, &Element );
- SetRect( &rcTexture, 14, 82, 241, 90 );
- Element.SetTexture( 0, &rcTexture );
- SetDefaultElement( DXUT_CONTROL_EDITBOX, 2, &Element );
- SetRect( &rcTexture, 241, 82, 246, 90 );
- Element.SetTexture( 0, &rcTexture );
- SetDefaultElement( DXUT_CONTROL_EDITBOX, 3, &Element );
- SetRect( &rcTexture, 8, 90, 14, 113 );
- Element.SetTexture( 0, &rcTexture );
- SetDefaultElement( DXUT_CONTROL_EDITBOX, 4, &Element );
- SetRect( &rcTexture, 241, 90, 246, 113 );
- Element.SetTexture( 0, &rcTexture );
- SetDefaultElement( DXUT_CONTROL_EDITBOX, 5, &Element );
- SetRect( &rcTexture, 8, 113, 14, 121 );
- Element.SetTexture( 0, &rcTexture );
- SetDefaultElement( DXUT_CONTROL_EDITBOX, 6, &Element );
- SetRect( &rcTexture, 14, 113, 241, 121 );
- Element.SetTexture( 0, &rcTexture );
- SetDefaultElement( DXUT_CONTROL_EDITBOX, 7, &Element );
- SetRect( &rcTexture, 241, 113, 246, 121 );
- Element.SetTexture( 0, &rcTexture );
- SetDefaultElement( DXUT_CONTROL_EDITBOX, 8, &Element );
- //-------------------------------------
- // CDXUTIMEEditBox
- //-------------------------------------
- Element.SetFont( 0, D3DCOLOR_ARGB( 255, 0, 0, 0 ), DT_LEFT | DT_TOP );
- // Assign the style
- SetRect( &rcTexture, 14, 90, 241, 113 );
- Element.SetTexture( 0, &rcTexture );
- SetDefaultElement( DXUT_CONTROL_IMEEDITBOX, 0, &Element );
- SetRect( &rcTexture, 8, 82, 14, 90 );
- Element.SetTexture( 0, &rcTexture );
- SetDefaultElement( DXUT_CONTROL_IMEEDITBOX, 1, &Element );
- SetRect( &rcTexture, 14, 82, 241, 90 );
- Element.SetTexture( 0, &rcTexture );
- SetDefaultElement( DXUT_CONTROL_IMEEDITBOX, 2, &Element );
- SetRect( &rcTexture, 241, 82, 246, 90 );
- Element.SetTexture( 0, &rcTexture );
- SetDefaultElement( DXUT_CONTROL_IMEEDITBOX, 3, &Element );
- SetRect( &rcTexture, 8, 90, 14, 113 );
- Element.SetTexture( 0, &rcTexture );
- SetDefaultElement( DXUT_CONTROL_IMEEDITBOX, 4, &Element );
- SetRect( &rcTexture, 241, 90, 246, 113 );
- Element.SetTexture( 0, &rcTexture );
- SetDefaultElement( DXUT_CONTROL_IMEEDITBOX, 5, &Element );
- SetRect( &rcTexture, 8, 113, 14, 121 );
- Element.SetTexture( 0, &rcTexture );
- SetDefaultElement( DXUT_CONTROL_IMEEDITBOX, 6, &Element );
- SetRect( &rcTexture, 14, 113, 241, 121 );
- Element.SetTexture( 0, &rcTexture );
- SetDefaultElement( DXUT_CONTROL_IMEEDITBOX, 7, &Element );
- SetRect( &rcTexture, 241, 113, 246, 121 );
- Element.SetTexture( 0, &rcTexture );
- SetDefaultElement( DXUT_CONTROL_IMEEDITBOX, 8, &Element );
- // Element 9 for IME text, and indicator button
- SetRect( &rcTexture, 0, 0, 136, 54 );
- Element.SetTexture( 0, &rcTexture );
- Element.SetFont( 0, D3DCOLOR_ARGB( 255, 0, 0, 0 ), DT_CENTER | DT_VCENTER );
- SetDefaultElement( DXUT_CONTROL_IMEEDITBOX, 9, &Element );
- //-------------------------------------
- // CDXUTListBox - Main
- //-------------------------------------
- SetRect( &rcTexture, 13, 123, 241, 160 );
- Element.SetTexture( 0, &rcTexture );
- Element.SetFont( 0, D3DCOLOR_ARGB(255, 0, 0, 0), DT_LEFT | DT_TOP );
- // Assign the Element
- SetDefaultElement( DXUT_CONTROL_LISTBOX, 0, &Element );
- //-------------------------------------
- // CDXUTListBox - Selection
- //-------------------------------------
- SetRect( &rcTexture, 16, 166, 240, 183 );
- Element.SetTexture( 0, &rcTexture );
- Element.SetFont( 0, D3DCOLOR_ARGB(255, 255, 255, 255), DT_LEFT | DT_TOP );
- // Assign the Element
- SetDefaultElement( DXUT_CONTROL_LISTBOX, 1, &Element );
- }
- //--------------------------------------------------------------------------------------
- // CDXUTControl class
- //--------------------------------------------------------------------------------------
- //--------------------------------------------------------------------------------------
- CDXUTControl::CDXUTControl( CDXUTDialog *pDialog )
- {
- m_Type = DXUT_CONTROL_BUTTON;
- m_pDialog = pDialog;
- m_ID = 0;
- m_Index = 0;
- m_pUserData = NULL;
- m_bEnabled = true;
- m_bVisible = true;
- m_bMouseOver = false;
- m_bHasFocus = false;
- m_bIsDefault = false;
- m_pDialog = NULL;
- m_x = 0;
- m_y = 0;
- m_width = 0;
- m_height = 0;
- ZeroMemory( &m_rcBoundingBox, sizeof( m_rcBoundingBox ) );
- }
- CDXUTControl::~CDXUTControl()
- {
- for( int i = 0; i < m_Elements.GetSize(); ++i )
- {
- delete m_Elements[i];
- }
- m_Elements.RemoveAll();
- }
- //--------------------------------------------------------------------------------------
- void CDXUTControl::SetTextColor( D3DCOLOR Color )
- {
- CDXUTElement* pElement = m_Elements.GetAt( 0 );
- if( pElement )
- pElement->FontColor.States[DXUT_STATE_NORMAL] = Color;
- }
- //--------------------------------------------------------------------------------------
- HRESULT CDXUTControl::SetElement( UINT iElement, CDXUTElement* pElement )
- {
- HRESULT hr = S_OK;
- if( pElement == NULL )
- return E_INVALIDARG;
- // Make certain the array is this large
- for( UINT i=m_Elements.GetSize(); i <= iElement; i++ )
- {
- CDXUTElement* pNewElement = new CDXUTElement();
- if( pNewElement == NULL )
- return E_OUTOFMEMORY;
- hr = m_Elements.Add( pNewElement );
- if( FAILED(hr) )
- {
- SAFE_DELETE( pNewElement );
- return hr;
- }
- }
- // Update the data
- CDXUTElement* pCurElement = m_Elements.GetAt( iElement );
- *pCurElement = *pElement;
- return S_OK;
- }
- //--------------------------------------------------------------------------------------
- void CDXUTControl::Refresh()
- {
- m_bMouseOver = false;
- m_bHasFocus = false;
- for( int i=0; i < m_Elements.GetSize(); i++ )
- {
- CDXUTElement* pElement = m_Elements.GetAt( i );
- pElement->Refresh();
- }
- }
- //--------------------------------------------------------------------------------------
- void CDXUTControl::UpdateRects()
- {
- SetRect( &m_rcBoundingBox, m_x, m_y, m_x + m_width, m_y + m_height );
- }
- //--------------------------------------------------------------------------------------
- // CDXUTStatic class
- //--------------------------------------------------------------------------------------
- //--------------------------------------------------------------------------------------
- CDXUTStatic::CDXUTStatic( 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++ )
- {
- CDXUTElement* pElement = m_Elements.GetAt( i );
- SAFE_DELETE( pElement );
- }
- m_Elements.RemoveAll();
- }
- //--------------------------------------------------------------------------------------
- void CDXUTStatic::Render( IDirect3DDevice9* pd3dDevice, float fElapsedTime )
- {
- if( m_bVisible == false )
- return;
- DXUT_CONTROL_STATE iState = DXUT_STATE_NORMAL;
- if( m_bEnabled == false )
- iState = DXUT_STATE_DISABLED;
- CDXUTElement* pElement = m_Elements.GetAt( 0 );
- pElement->FontColor.Blend( iState, fElapsedTime );
- m_pDialog->DrawText( m_strText, pElement, &m_rcBoundingBox, true );
- }
- //--------------------------------------------------------------------------------------
- HRESULT CDXUTStatic::GetTextCopy( LPWSTR strDest, UINT bufferCount )
- {
- // Validate incoming parameters
- if( strDest == NULL || bufferCount == 0 )
- {
- return E_INVALIDARG;
- }
- // Copy the window text
- StringCchCopy( strDest, bufferCount, m_strText );
- return S_OK;
- }
- //--------------------------------------------------------------------------------------
- HRESULT CDXUTStatic::SetText( LPCWSTR strText )
- {
- if( strText == NULL )
- {
- m_strText[0] = 0;
- return S_OK;
- }
- StringCchCopy( m_strText, MAX_PATH, strText);
- return S_OK;
- }
- //--------------------------------------------------------------------------------------
- // CDXUTButton class
- //--------------------------------------------------------------------------------------
- //--------------------------------------------------------------------------------------
- CDXUTButton::CDXUTButton( CDXUTDialog *pDialog )
- {
- m_Type = DXUT_CONTROL_BUTTON;
- m_pDialog = pDialog;
- m_bPressed = false;
- m_nHotkey = 0;
- }
- //--------------------------------------------------------------------------------------
- bool CDXUTButton::HandleKeyboard( UINT uMsg, WPARAM wParam, LPARAM lParam )
- {
- if( !m_bEnabled || !m_bVisible )
- return false;
- switch( uMsg )
- {
- case WM_KEYDOWN:
- {
- switch( wParam )
- {
- case VK_SPACE:
- m_bPressed = true;
- return true;
- }
- }
- case WM_KEYUP:
- {
- switch( wParam )
- {
- case VK_SPACE:
- if( m_bPressed == true )
- {
- m_bPressed = false;
- m_pDialog->SendEvent( EVENT_BUTTON_CLICKED, true, this );
- }
- return true;
- }
- }
- }
- return false;
- }
- //--------------------------------------------------------------------------------------
- bool CDXUTButton::HandleMouse( UINT uMsg, POINT pt, WPARAM wParam, LPARAM lParam )
- {
- if( !m_bEnabled || !m_bVisible )
- return false;
- switch( uMsg )
- {
- case WM_LBUTTONDOWN:
- case WM_LBUTTONDBLCLK:
- {
- if( ContainsPoint( pt ) )
- {
- // Pressed while inside the control
- m_bPressed = true;
- SetCapture( DXUTGetHWND() );
- if( !m_bHasFocus )
- m_pDialog->RequestFocus( this );
- return true;
- }
- break;
- }
- case WM_LBUTTONUP:
- {
- if( m_bPressed )
- {
- m_bPressed = false;
- ReleaseCapture();
- if( !m_pDialog->m_bKeyboardInput )
- m_pDialog->ClearFocus();
- // Button click
- if( ContainsPoint( pt ) )
- m_pDialog->SendEvent( EVENT_BUTTON_CLICKED, true, this );
- return true;
- }
- break;
- }
- };
- return false;
- }
- //--------------------------------------------------------------------------------------
- void CDXUTButton::Render( IDirect3DDevice9* pd3dDevice, float fElapsedTime )
- {
- int nOffsetX = 0;
- int nOffsetY = 0;
- DXUT_CONTROL_STATE iState = DXUT_STATE_NORMAL;
- if( m_bVisible == false )
- {
- iState = DXUT_STATE_HIDDEN;
- }
- else if( m_bEnabled == false )
- {
- iState = DXUT_STATE_DISABLED;
- }
- else if( m_bPressed )
- {
- iState = DXUT_STATE_PRESSED;
- nOffsetX = 1;
- nOffsetY = 2;
- }
- else if( m_bMouseOver )
- {
- iState = DXUT_STATE_MOUSEOVER;
- nOffsetX = -1;
- nOffsetY = -2;
- }
- else if( m_bHasFocus )
- {
- iState = DXUT_STATE_FOCUS;
- }
- // Background fill layer
- //TODO: remove magic numbers
- CDXUTElement* pElement = m_Elements.GetAt( 0 );
- float fBlendRate = ( iState == DXUT_STATE_PRESSED ) ? 0.0f : 0.8f;
- RECT rcWindow = m_rcBoundingBox;
- OffsetRect( &rcWindow, nOffsetX, nOffsetY );
- // Blend current color
- pElement->TextureColor.Blend( iState, fElapsedTime, fBlendRate );
- pElement->FontColor.Blend( iState, fElapsedTime, fBlendRate );
- m_pDialog->DrawSprite( pElement, &rcWindow );
- m_pDialog->DrawText( m_strText, pElement, &rcWindow );
- // Main button
- pElement = m_Elements.GetAt( 1 );
- // Blend current color
- pElement->TextureColor.Blend( iState, fElapsedTime, fBlendRate );
- pElement->FontColor.Blend( iState, fElapsedTime, fBlendRate );
- m_pDialog->DrawSprite( pElement, &rcWindow );