📄 dxutgui.cpp
字号:
//--------------------------------------------------------------------------------------
// File: DXUTgui.cpp
//
// Desc:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------
#include "dxstdafx.h"
#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
// 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
//--------------------------------------------------------------------------------------
// Global/Static Members
//--------------------------------------------------------------------------------------
CDXUTDialogResourceManager* DXUTGetGlobalDialogResourceManager()
{
// Using an accessor function gives control of the construction order
static CDXUTDialogResourceManager manager;
return &manager;
}
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;
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_bCaption = false;
m_bMinimized = 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_fTimeLastRefresh = 0;
m_pControlMouseOver = NULL;
m_pNextDialog = this;
m_pPrevDialog = this;
m_nDefaultControlID = 0xffff;
m_bNonUserEvents = false;
m_bKeyboardInput = false;
m_bMouseInput = true;
InitDefaultElements();
}
//--------------------------------------------------------------------------------------
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::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();
}
//--------------------------------------------------------------------------------------
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;
}
//--------------------------------------------------------------------------------------
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 );
}
//--------------------------------------------------------------------------------------
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 )
{
// See if the dialog needs to be refreshed
if( m_fTimeLastRefresh < s_fTimeRefresh )
{
m_fTimeLastRefresh = DXUTGetTime();
Refresh();
}
DXUT_SCREEN_VERTEX vertices[4] =
{
(float)m_x, (float)m_y, 0.5f, 1.0f, m_colorTopLeft, 0.0f, 0.5f,
(float)m_x + m_width, (float)m_y, 0.5f, 1.0f, m_colorTopRight, 1.0f, 0.5f,
(float)m_x + m_width, (float)m_y + m_height, 0.5f, 1.0f, m_colorBottomRight, 1.0f, 1.0f,
(float)m_x, (float)m_y + m_height, 0.5f, 1.0f, m_colorBottomLeft, 0.0f, 1.0f,
};
IDirect3DDevice9* pd3dDevice = DXUTGetGlobalDialogResourceManager()->GetD3DDevice();
// Set up a state block here and restore it when finished drawing all the controls
DXUTGetGlobalDialogResourceManager()->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->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->SetVertexShader( NULL );
pd3dDevice->SetPixelShader( NULL );
//pd3dDevice->Clear( 0, NULL, D3DCLEAR_ZBUFFER, 0, 1.0f, 0 );
pd3dDevice->SetRenderState( D3DRS_ZENABLE, FALSE );
if( !m_bMinimized )
{
pd3dDevice->SetFVF( DXUT_SCREEN_VERTEX::FVF );
pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, vertices, sizeof(DXUT_SCREEN_VERTEX) );
}
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 );
DXUTGetGlobalDialogResourceManager()->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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -