⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gamemenu.cpp

📁 RGA: Biowaste Game Example This C++ application demonstrates how to create a 2D mobile game for S60
💻 CPP
字号:

#include "GameMenu.h"
#include "ExampleApplication.h"


CGameMenu::CGameMenu(CExampleApplication& aApp)
	: CAppState(aApp)
	{

	}


CGameMenu::~CGameMenu()
	{
	iMenuItems.Close();
	}


TInt CGameMenu::Init()
	{
	iFont = (CFont*)App().BigFont();
	
	iXMargin = 10;
	iYMargin = (iFont->FontMaxHeight() >> 1);
	iYStep = (iFont->FontMaxHeight() + iYMargin);
	
	return KErrNone;
	}


void CGameMenu::AddMenuItem(TMenuItem& aItem)
	{
	// add menu item to array
	iMenuItems.Append(aItem);
	
	// compute new size of the menu
	const TInt textwidth = iFont->TextWidthInPixels(aItem.iText);
	if (textwidth + (iXMargin * 2) > iSize.iWidth)
		{
		iSize.iWidth = textwidth + (iXMargin * 2);
		}
	
	iSize.iHeight = (iMenuItems.Count() * iYStep) + iYMargin;
	}


TInt CGameMenu::SelectedMenuItemId() const
	{
	if (iMenuItems.Count())
		{
		return iMenuItems[iSelectedItem].iID;
		}
	else
		{
		return KErrNotFound;
		}
	}


void CGameMenu::SelectMenuItem(TInt aId)
	{
	TInt i;
	for (i=0; i<iMenuItems.Count(); i++)
		{
		TMenuItem& item = iMenuItems[i];
		
		if (item.iID == (TUint32)aId)
			{
			iSelectedItem = i;
			break;
			}
		}
	}


void CGameMenu::SetPosition(const TPoint& aPosition)
	{
	iPosition = aPosition;
	}


TPoint CGameMenu::Position() const
	{
	return iPosition;
	}

TSize CGameMenu::Size() const
	{
	return iSize;
	}



EAppUpdateState CGameMenu::Update(const TReal64& aFrametime)
	{
	if (iItemShakerCounter > 0.0)
		{
		iItemShakerCounter -= aFrametime;
		}
	
	return EAppUpdateStateContinue;
	}


void CGameMenu::Draw(IGraphicsContext& /*aContext*/)
	{
	IBackBuffer& backbuffer = App().BackBuffer();
	CGraphicsContext* context = App().SystemGc(backbuffer);
	context->UseFont(iFont);
	
	TInt i;

	// draw menu item text shadows
	context->SetPenColor(TRgb(200, 200, 200));
	
	const TInt shadowoffset = 4;
	TPoint pos(	iPosition.iX + iXMargin + shadowoffset,
				iPosition.iY + iFont->FontMaxHeight() + shadowoffset);
	
	TInt xshake = 0;
	TInt yshake = 0;
	
	if (iItemShakerCounter > 0.0)
		{
		const TInt shakeamount = (TInt)iItemShakerCounter + 1;
		xshake = App().RandInt(-shakeamount, shakeamount);
		yshake = App().RandInt(-shakeamount, shakeamount);
		}
	
	for (i=0; i<iMenuItems.Count(); i++)
		{
		TMenuItem& item = iMenuItems[i];

		if (i == iSelectedItem)
			{
			context->DrawText(	item.iText,
					TPoint(pos.iX + xshake, pos.iY + yshake));
			}
		else
			{
			context->DrawText(	item.iText,
								pos);
			}
		
		pos.iY += iYStep;
		}
	
	
	// draw menu items
	pos = TPoint(	iPosition.iX + iXMargin,
					iPosition.iY + iFont->FontMaxHeight());
	
	for (i=0; i<iMenuItems.Count(); i++)
		{
		TMenuItem& item = iMenuItems[i];

		if (i == iSelectedItem)
			{
			context->SetPenColor(KRgbRed);
			context->DrawText(	item.iText,
								TPoint(pos.iX + xshake, pos.iY + yshake));
			}
		else
			{
			context->SetPenColor(TRgb(0, 0, 0));
			context->DrawText(	item.iText,
					pos);
			}
		
		pos.iY += iYStep;
		}
	
	
	context->DiscardFont();
	App().ApplySystemGc();
	}


void CGameMenu::KeyDown(TUint32 aKeyCode)
	{
	if (aKeyCode == INPUT_KEY_DIGITAL_DOWN)
		{
		App().RePlayAudio(KAudioFileMenuMove);
		
		// move selection down
		if (++iSelectedItem >= iMenuItems.Count())
			{
			iSelectedItem = 0;
			}
		iItemShakerCounter = 0.4;
		}
	else if (aKeyCode == INPUT_KEY_DIGITAL_UP)
		{
		App().RePlayAudio(KAudioFileMenuMove);

		// move selection up
		if (--iSelectedItem < 0)
			{
			iSelectedItem = iMenuItems.Count() - 1;
			}
		iItemShakerCounter = 0.4;
		}
	else if (	aKeyCode == INPUT_KEY_OK ||
				aKeyCode == INPUT_KEY_LSK ||
				aKeyCode == INPUT_KEY_RSK)
		{
		App().RePlayAudio(KAudioFileMenuSelect);

		// menu item selection, its handled in application
		// menu state
		iItemShakerCounter = 3.0;
		}
	}



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -