Code:
#include "Menu.h"
#define Red D3DCOLOR_ARGB (255 , 255 , 0 , 0 )
#define Yellow D3DCOLOR_ARGB (255 , 255 , 255 , 0 )
#define Green D3DCOLOR_ARGB (255 , 0 , 255 , 0 )
#define Blue D3DCOLOR_ARGB (255 , 0 , 0 , 255 )
#define Purple D3DCOLOR_ARGB (255 , 102 , 0 , 153 )
#define Pink D3DCOLOR_ARGB (255 , 255 , 20 , 147 )
#define Orange D3DCOLOR_ARGB (255 , 255 , 165 , 0 )
#define Black D3DCOLOR_ARGB (255 , 0 , 0 , 0 )
#define White D3DCOLOR_ARGB (255 , 255 , 255 , 255 )
cMenu Menu;//Menu
cDraw Draw;
//----------------------------------//
cDraw::cDraw()
{
m_pDevice = 0;
m_iLB_FontSize = 14;
}
//----------------------------------//
cDraw::~cDraw()
{
}
//----------------------------------//
//----------------------------------//
void cDraw::Load_Resources( void )
{
m_coLB_Item = D3DCOLOR_ARGB( 255, 0, 0, 0 );
m_coLB_SelItem = D3DCOLOR_ARGB( 255, 0, 200, 0 );
m_coLB_SectionItem = D3DCOLOR_ARGB( 255, 0, 0, 255 );
m_coLB_Back = D3DCOLOR_ARGB( 255, 255, 255, 255 );
m_coLB_Border = D3DCOLOR_ARGB( 255, 0, 0, 0 );
m_coLB_Stripe = D3DCOLOR_XRGB( 248, 248, 248 );
m_coRed = D3DCOLOR_XRGB( 255, 0, 0 );
m_coBlue = D3DCOLOR_XRGB( 0, 0, 255 );
m_coWhite = D3DCOLOR_XRGB( 255, 255, 255 );
m_coBlack = D3DCOLOR_XRGB( 0, 0, 0 );
D3DXCreateFont( m_pDevice, m_iLB_FontSize, 0, FW_MEDIUM, 1, 0 ,DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, PROOF_QUALITY, DEFAULT_PITCH | FF_SCRIPT, "Arial", &m_pLBFont );
//D3DXCreateLine( m_pDevice, &m_pLine );
}
//----------------------------------//
//----------------------------------//
void cDraw::String ( int X, int Y, D3DCOLOR Color, const char *szString, ... )
{
long W = 100, H = 40;
char szBuffer[ 1024 ] = { 0 };
RECT rtFontPos = { X, Y, X + W, Y + H };
va_list va_alist;
va_start( va_alist, szString );
vsprintf( szBuffer, szString, va_alist);
va_end ( va_alist );
m_pLBFont->DrawText( NULL, szBuffer, -1, &rtFontPos, DT_LEFT | DT_NOCLIP, Color );
}
//----------------------------------//
void cDraw::Rect ( int X, int Y, int W, int H, D3DCOLOR Color )
{
D3DXVECTOR2 vLine[ 2 ];
m_pLine->SetWidth( W );
m_pLine->SetAntialias( 0 );
m_pLine->SetGLLines( 1 );
m_pLine->SetWidth( W );
m_pLine->Begin();
vLine[ 0 ][ 0 ] = X+W/2;
vLine[ 0 ][ 1 ] = Y;
vLine[ 1 ][ 0 ] = X+W/2;
vLine[ 1 ][ 1 ] = Y+H;
m_pLine->Draw( vLine, 2, Color );
m_pLine->End();
}
//----------------------------------//
struct _Menu_Item
{
long iType;
char szItem[ MAX_PATH ];
float fMin,
fMax,
fStatus,
fOffset;
}*Menu_Item;
//----------------------------------//
cMenu::cMenu()
{
Menu_Item = new _Menu_Item[ 100 ];
ZeroMemory( Menu_Item, sizeof( _Menu_Item ) * 100 );
m_iMenuItemAmount = 0;
m_iCurrentIndex = 1;
m_iLB_Index = 0;
m_iLB_Amount = 6;
m_bVisible = true;
SetRect( &m_rtClient, 0, 14, 0, 14 );
}
//----------------------------------//
cMenu::~cMenu()
{
delete Menu_Item;
}
//----------------------------------//
//----------------------------------//
void cMenu::Create ( void )
{
Menu.Append_Item( ITEM_TYPE_SECTION, "[Catagory 1]", 0, 0, 1 );
Menu.Append_Item( ITEM_TYPE_OPTION, "Option1", 0, 100, 1 );
Menu.Append_Item( ITEM_TYPE_OPTION, "Option2", 0, 1, 0.1f );
Menu.Append_Item( ITEM_TYPE_OPTION, "Option3", 0, 10, 2 );
Menu.Append_Item( ITEM_TYPE_SECTION, "[Catagory 2]", 0, 0, 1 );
Menu.Append_Item( ITEM_TYPE_OPTION, "Option4", 0, 1, 1 );
Menu.Append_Item( ITEM_TYPE_OPTION, "Option5", 0, 1, 1 );
Menu.Append_Item( ITEM_TYPE_OPTION, "Option6", 0, 1, 1 );
Menu.Append_Item( ITEM_TYPE_OPTION, "Option7", 0, 1, 1 );
for( int i = 0; i < m_iMenuItemAmount; i++ )
{
if( strcmp( Menu_Item[ i ].szItem, " Distance" ) == 0 ) // For setting defaults
Menu_Item[ i ].fStatus = 100;
}
//LoadSettings();
}
//----------------------------------//
//----------------------------------//
void cMenu::SaveSettings ( void )
{
fstream pFile( "Settings.gha", ios::out );
pFile.write( ( const char* )Menu_Item, sizeof( _Menu_Item ) * ( m_iMenuItemAmount - 1 ) );
pFile.close();
}
//----------------------------------//
void cMenu::LoadSettings ( void )
{
fstream pFile( "Settings.gha", ios::in );
pFile.read( ( char* )Menu_Item, sizeof( _Menu_Item ) * ( m_iMenuItemAmount - 1 ) );
pFile.close();
}
//----------------------------------//
//----------------------------------//
char* cMenu::Extend_String ( char* szString, long iLength )
{
static char szBuffer[ 2048 ];
long iCurrentLength = strlen( szString );
strcpy( szBuffer, szString );
for( int i = iCurrentLength; i < iLength; i++ )
strcat( szBuffer, " " );
return szBuffer;
}
//----------------------------------//
void cMenu::Append_Item ( long iType, char* szItem, float fMin, float fMax, float fOffset )
{
strcpy_s( Menu_Item[ m_iMenuItemAmount ].szItem, MAX_PATH, szItem );
Menu_Item[ m_iMenuItemAmount ].fMin = fMin;
Menu_Item[ m_iMenuItemAmount ].fMax = fMax;
Menu_Item[ m_iMenuItemAmount ].iType = iType;
Menu_Item[ m_iMenuItemAmount ].fOffset = fOffset;
Menu_Item[ m_iMenuItemAmount ].fStatus = 0;
m_iMenuItemAmount++;
}
//----------------------------------//
float cMenu::GetItemByName ( char* szName )
{
for( int i = 0; i < m_iMenuItemAmount; i++ )
if( strcmp( szName, Menu_Item[ i ].szItem ) == 0 )
return Menu_Item[ i ].fStatus;
return -1;
}
//----------------------------------//
//----------------------------------//
void cMenu::Keyboard_Input ( void )
{
if( GetAsyncKeyState( VK_LEFT )&1 && Menu_Item[ m_iCurrentIndex ].fStatus - Menu_Item[ m_iCurrentIndex ].fOffset >= Menu_Item[ m_iCurrentIndex ].fMin )
{
Menu_Item[ m_iCurrentIndex ].fStatus -= Menu_Item[ m_iCurrentIndex ].fOffset;
//SaveSettings();
}
if( GetAsyncKeyState( VK_RIGHT )&1 && Menu_Item[ m_iCurrentIndex ].fStatus + Menu_Item[ m_iCurrentIndex ].fOffset <= Menu_Item[ m_iCurrentIndex ].fMax )
{
Menu_Item[ m_iCurrentIndex ].fStatus += Menu_Item[ m_iCurrentIndex ].fOffset;
//SaveSettings();
}
if( GetAsyncKeyState( VK_UP )&1 )
for( int i = m_iCurrentIndex - 1; i > 0; i-- )
if( Menu_Item[ i ].iType == ITEM_TYPE_OPTION )
{
m_iCurrentIndex = i;
break;
}
if( GetAsyncKeyState( VK_DOWN )&1 )
for( int i = m_iCurrentIndex + 1; i < m_iMenuItemAmount; i++ )
if( Menu_Item[ i ].iType == ITEM_TYPE_OPTION )
{
m_iCurrentIndex = i;
break;
}
m_iLB_Index = m_iCurrentIndex - m_iLB_Amount/2;
if( m_iLB_Index < 0 )
m_iLB_Index = 0;
if( m_iLB_Index > m_iMenuItemAmount - m_iLB_Amount )
m_iLB_Index = m_iMenuItemAmount - m_iLB_Amount;
}
//----------------------------------//
//----------------------------------//
void cMenu::List_Box ( void )
{
SetRect( &m_rtLB_Client, m_rtClient.left, m_rtClient.top, 210, Draw.m_iLB_FontSize * m_iLB_Amount );
Draw.Rect( m_rtLB_Client.left - 1, m_rtLB_Client.top - 1, m_rtLB_Client.right + 2, m_rtLB_Client.bottom + 2, Draw.m_coLB_Border );
Draw.Rect( m_rtLB_Client.left, m_rtLB_Client.top, m_rtLB_Client.right, m_rtLB_Client.bottom, Draw.m_coLB_Back );
for( int i = m_rtLB_Client.top; i < m_iLB_Amount * Draw.m_iLB_FontSize + m_rtLB_Client.top; i += Draw.m_iLB_FontSize * 2 )
Draw.Rect( m_rtLB_Client.left, i, 210, Draw.m_iLB_FontSize, Draw.m_coLB_Stripe );
for( int i = m_iLB_Index, k = 0; i < m_iLB_Amount + m_iLB_Index; i++, k++ )
{
if( Menu_Item[ i ].iType == ITEM_TYPE_SECTION )
Draw.String( m_rtLB_Client.left + 1, m_rtLB_Client.top + ( k * Draw.m_iLB_FontSize ) + 1, Draw.m_coLB_SectionItem, "%s", Menu_Item[ i ].szItem );
else
Draw.String( m_rtLB_Client.left + 20, m_rtLB_Client.top + ( k * Draw.m_iLB_FontSize ) + 1, ( i == m_iCurrentIndex ? Draw.m_coLB_SelItem : Draw.m_coLB_Item ), "%s : %.1f", Extend_String( Menu_Item[ i ].szItem, 20 ), Menu_Item[ i ].fStatus );
}
}
//----------------------------------//
void cMenu::Display_Menu ( void )
{
Keyboard_Input();
List_Box();
}
//----------------------------------//
//----------------------------------//
void cMenu::Commands ( void )
{
if( GetAsyncKeyState( VK_INSERT )&1 )
m_bVisible = !m_bVisible;
if( GetItemByName( "Option1" ) )
{
}
if( GetItemByName( "Option2" ) )
{
}
if( GetItemByName( "Option3" ) )
{
}
}
//----------------------------------//
In Present: