📄 didevimg.cpp
字号:
//-----------------------------------------------------------------------------
// File: DIDevImg.cpp
//
// Desc: Implementation for CDIDevImage class, which encapsulates methods for
// drawing device images, callout strings, and object highlights.
//
// Copyright( c ) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#include "DIDevImg.h"
#include <d3dx9tex.h>
//-----------------------------------------------------------------------------
// Name: CDIDevImage
// Desc: Constructor
//-----------------------------------------------------------------------------
CDIDevImage::CDIDevImage()
{
m_bInitialized = FALSE;
m_bCustomUI = FALSE;
m_bInvalidUI = TRUE;
m_ahImages = NULL;
m_dwNumViews = 0;
m_dwNumObjects = 0;
m_apObject = NULL;
m_dwWidthPref = 0;
m_dwHeightPref = 0;
m_dwScaleMethod = 0;
m_BkColor = D3DCOLOR_ARGB(255, 0, 0, 0);
m_hFont = NULL;
}
//-----------------------------------------------------------------------------
// Name: ~CDIDevImage
// Desc: Destructor
//-----------------------------------------------------------------------------
CDIDevImage::~CDIDevImage()
{
CleanUp();
}
//-----------------------------------------------------------------------------
// Name: Init
// Desc: Responsible for initializing and preparing the CDIDevImage object for
// rendering the device image. Init must be called before the other
// public functions.
// Args: pDIDevice - Pointer to a DirectInputDevice object for which the
// configuration UI should be created.
// Ret: DI_OK - Success; image found.
// DI_IMAGENOTFOUND - Success; no image. Default UI created instead.
// DIERR_INVALIDPARAM - Fail; invalid argument passed.
//-----------------------------------------------------------------------------
HRESULT CDIDevImage::Init( LPDIRECTINPUTDEVICE8 pDIDevice )
{
HRESULT hr = DI_OK;
// Sanity check
if( NULL == pDIDevice )
return DIERR_INVALIDPARAM;
// Always start clean
CleanUp();
// retrieve the image info from DirectInput
hr = LoadImageInfo( pDIDevice );
if( FAILED(hr) )
{
// Image information may have partially loaded. Clean out whatever
// is stored since we'll need to create a custom UI from scratch.
CleanUp();
// For one reason or another, the image info for this device
// could not be loaded. At this point, create a default UI.
m_bCustomUI = true;
hr = CreateCustomImageInfo( pDIDevice );
if( FAILED(hr) )
{
SAFE_RELEASE(pDIDevice);
CleanUp();
return hr;
}
}
// Create the default callout font
CreateFont();
// Made it through initialization. Set the initialized flag to allow the
// other member functions to be called.
m_bInitialized = true;
// Both of these values indicate success, but the client may wish to know
// whether an image was actually found for this device, or if we are just
// producing a default UI.
return m_bCustomUI ? DI_IMAGENOTFOUND : DI_OK;
}
//-----------------------------------------------------------------------------
// Name: SetCalloutState
// Desc: Sets the state for a specific callout
// Args: dwObjId - Object ID of the requested callout. This corresponds to the
// dwType value returned by EnumDeviceObjects.
// dwCalloutState - New state of the callout, which may be zero or more
// combinations of:
// DIDICOS_HIGHLIGHTED - Overlay drawn. Callout drawn in high color.
// DIDICOS_INVISIBLE - Overlay and callout string not drawn.
// DIDICOS_TOOLTIP - Tooltip drawn if callout text is truncated.
// Ret: DI_OK - Success.
// DIERR_NOTINITIALIZED - Fail; Init() must be called first.
// DIERR_OBJECTNOTFOUND - Fail; Given ID not found in list of objects
//-----------------------------------------------------------------------------
HRESULT CDIDevImage::SetCalloutState( DWORD dwObjId, DWORD dwCalloutState )
{
// Verify initialization
if( false == m_bInitialized )
return DIERR_NOTINITIALIZED;
CDIDIObject* pObject = GetObject( dwObjId );
if( NULL == pObject )
return DIERR_OBJECTNOTFOUND;
DWORD dwOldState = pObject->GetCalloutState();
pObject->SetCalloutState( dwCalloutState );
// This action might change the layout for the custom UI
if( m_bCustomUI &&
( DIDICOS_INVISIBLE & dwOldState ||
DIDICOS_INVISIBLE & dwCalloutState ) )
m_bInvalidUI = TRUE;
return DI_OK;
}
//-----------------------------------------------------------------------------
// Name: GetCalloutState
// Desc: Returns the state of a specific callout
// Args: dwObjId - Object ID of the requested callout. This corresponds to the
// dwType value returned by EnumDeviceObjects.
// pdwCalloutState - Pointer to a variable which will contain the current
// callout value after a successful return.
// Ret: DI_OK - Success.
// DIERR_NOTINITIALIZED - Fail; Init() must be called first.
// DIERR_OBJECTNOTFOUND - Fail; Given ID not found in list of objects
//-----------------------------------------------------------------------------
HRESULT CDIDevImage::GetCalloutState( DWORD dwObjId, LPDWORD pdwCalloutState )
{
// Verify initialization
if( false == m_bInitialized )
return DIERR_NOTINITIALIZED;
CDIDIObject* pObject = GetObject( dwObjId );
if( NULL == pObject )
return DIERR_OBJECTNOTFOUND;
*pdwCalloutState = pObject->GetCalloutState();
return DI_OK;
}
//-----------------------------------------------------------------------------
// Name: SetCalloutColors
// Desc: Sets the callout-unique colors to be used when painting a specific
// callout
// Args: dwObjId - Object ID of the requested callout. This corresponds to the
// dwType value returned by EnumDeviceObjects.
// crColorNormal - Foreground text color for callout strings in a normal
// state.
// crColorHigh - Foreground text color for callout strings in a
// highlighted state.
// Ret: DI_OK - Success.
// DIERR_NOTINITIALIZED - Fail; Init() must be called first.
// DIERR_OBJECTNOTFOUND - Fail; Given ID not found in list of objects
//-----------------------------------------------------------------------------
HRESULT CDIDevImage::SetCalloutColors( DWORD dwObjId, COLORREF crColorNormal, COLORREF crColorHigh )
{
// Verify initialization
if( false == m_bInitialized )
return DIERR_NOTINITIALIZED;
CDIDIObject* pObject = GetObject( dwObjId );
if( NULL == pObject )
return DIERR_OBJECTNOTFOUND;
pObject->SetCalloutColors( crColorNormal, crColorHigh );
return DI_OK;
}
//-----------------------------------------------------------------------------
// Name: GetCalloutColors
// Desc: Obtains the callout-unique colors used when painting a specific
// callout
// Args: dwObjId - Object ID of the requested callout. This corresponds to the
// dwType value returned by EnumDeviceObjects.
// pcrColorNormal - Pointer to a COLORREF variable which will contain
// the normal callout color after a successful return. May be NULL.
// pcrColorHigh - Pointer to a COLORREF variable which will contain
// the highlight callout color after a successful return. May be NULL.
// Ret: DI_OK - Success.
// DIERR_NOTINITIALIZED - Fail; Init() must be called first.
// DIERR_OBJECTNOTFOUND - Fail; Given ID not found in list of objects
//-----------------------------------------------------------------------------
HRESULT CDIDevImage::GetCalloutColors( DWORD dwObjId, LPCOLORREF pcrColorNormal, LPCOLORREF pcrColorHigh )
{
// Verify initialization
if( false == m_bInitialized )
return DIERR_NOTINITIALIZED;
CDIDIObject* pObject = GetObject( dwObjId );
if( NULL == pObject )
return DIERR_OBJECTNOTFOUND;
pObject->GetCalloutColors( pcrColorNormal, pcrColorHigh );
return DI_OK;
}
//-----------------------------------------------------------------------------
// Name: SetCalloutText
// Desc: Sets the text associated with the callout specified by an object ID
// Args: dwObjId - Object ID of the requested callout. This corresponds to the
// dwType value returned by EnumDeviceObjects.
// strText - New callout text.
// Ret: DI_OK - Success.
// DIERR_INVALIDPARAM - Fail; Null pointer passed.
// DIERR_NOTINITIALIZED - Fail; Init() must be called first.
// DIERR_OBJECTNOTFOUND - Fail; Given ID not found in list of objects
//-----------------------------------------------------------------------------
HRESULT CDIDevImage::SetCalloutText( DWORD dwObjId, LPCTSTR strText )
{
// Verify initialization
if( false == m_bInitialized )
return DIERR_NOTINITIALIZED;
if( NULL == strText )
return DIERR_INVALIDPARAM;
CDIDIObject* pObject = GetObject( dwObjId );
if( NULL == pObject )
return DIERR_OBJECTNOTFOUND;
pObject->SetCalloutText( strText );
// This action might change the layout for the custom UI
if( m_bCustomUI )
m_bInvalidUI = TRUE;
return DI_OK;
}
//-----------------------------------------------------------------------------
// Name: GetCalloutText
// Desc: Returns the text associated with a specific callout
// Args: dwObjId - Object ID of the requested callout. This corresponds to the
// dwType value returned by EnumDeviceObjects.
// pstrText - Pointer to a string buffer which will collect the current
// callout string.
// dwSize - Maximum number of characters to copy into the buffer.
// Ret: DI_OK - Success.
// DIERR_INVALIDPARAM - Fail; Null pointer passed.
// DIERR_NOTINITIALIZED - Fail; Init() must be called first.
// DIERR_OBJECTNOTFOUND - Fail; Given ID not found in list of objects
//-----------------------------------------------------------------------------
HRESULT CDIDevImage::GetCalloutText( DWORD dwObjId, LPTSTR pstrText, DWORD dwSize )
{
// Verify initialization
if( false == m_bInitialized )
return DIERR_NOTINITIALIZED;
if( NULL == pstrText )
return DIERR_INVALIDPARAM;
CDIDIObject* pObject = GetObject( dwObjId );
if( NULL == pObject )
return DIERR_OBJECTNOTFOUND;
pObject->GetCalloutText( pstrText, dwSize );
return DI_OK;
}
//-----------------------------------------------------------------------------
// Name: GetObjFromPoint
// Desc: Returns the ID of the object on the device corresponding to the
// callout which contains the given point.
// Args: Pt - Point to check against callout rect coordinates
// pdwObjId - Pointer to a variable which will contain the object ID upon
// successful return.
// Ret: DI_OK - Success.
// DIERR_INVALIDPARAM - Fail; Null pointer passed.
// DIERR_NOTINITIALIZED - Fail; Init() must be called first.
// DIERR_OBJECTNOTFOUND - Fail; Given ID not found in list of objects
//-----------------------------------------------------------------------------
HRESULT CDIDevImage::GetObjFromPoint( POINT Pt, LPDWORD pdwObjId )
{
// Verify initialization
if( false == m_bInitialized )
return DIERR_NOTINITIALIZED;
if( NULL == pdwObjId )
return DIERR_INVALIDPARAM;
if( m_dwActiveView > m_dwNumViews )
return E_FAIL;
// For a custom UI, this method depends on the UI being calculated
if( m_bCustomUI && m_bInvalidUI )
BuildCustomUI();
DIDICallout *pCallout = NULL;
// for each object
for( UINT i=0; i < m_dwNumObjects; i++ )
{
pCallout = m_apObject[i]->GetCallout( m_dwActiveView );
if( PtInRect( &(pCallout->rcScaled), Pt ) )
{
// if the point is inside the scaled bounding rect, the
// correct callout has been found.
*pdwObjId = m_apObject[i]->GetID();
return DI_OK;
}
}
return S_FALSE;
}
//-----------------------------------------------------------------------------
// Name: SetActiveView
// Desc: Activates the provided view. This view will be painted when the
// device image is rendered.
// Args: dwView - The index of the new view.
// Ret: DI_OK - Success.
// DIERR_INVALIDPARAM - Fail; View is out of range.
// DIERR_NOTINITIALIZED - Fail; Init() must be called first.
//-----------------------------------------------------------------------------
HRESULT CDIDevImage::SetActiveView( DWORD dwView )
{
// Verify initialization
if( false == m_bInitialized )
return DIERR_NOTINITIALIZED;
// For a custom UI, this method depends on the UI being calculated
if( m_bCustomUI && m_bInvalidUI )
BuildCustomUI();
// Make sure view exists
if( dwView >= m_dwNumViews )
return DIERR_INVALIDPARAM;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -