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

📄 symbolmatrixdrawer.cpp

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


#include "SymbolMatrixDrawer.h"


CSymbolMatrixDrawer::CSymbolMatrixDrawer()
	{

	}

CSymbolMatrixDrawer::~CSymbolMatrixDrawer()
	{

	}


CSymbolMatrixDrawer* CSymbolMatrixDrawer::NewL(ISymbolMatrix* aSymbolMatrix, CApplicationBase* aApplication)
	{
	CSymbolMatrixDrawer* me = new (ELeave) CSymbolMatrixDrawer;
	CleanupStack::PushL(me);
	me->ConstructL(aSymbolMatrix, aApplication);
	CleanupStack::Pop(me);
	return me;
	}


void CSymbolMatrixDrawer::ConstructL(ISymbolMatrix* aSymbolMatrix, CApplicationBase* aApplication)
	{
	iSymbolMatrix = aSymbolMatrix;
	iApplication = aApplication;
	
	SetColors(KRgbWhite, KRgbRed, KRgbBlack);
	SetGridSize(TSize(25, 25));
	}


void CSymbolMatrixDrawer::SetColors(	const TRgb& aBackgroundColor,
										const TRgb& aGridColor,
										const TRgb& aTextColor )
	{
	iBackgroundColor =	0xFF000000 |
						(aBackgroundColor.Red() << 16) |
						(aBackgroundColor.Green() << 8) |
						aBackgroundColor.Blue();
	iGridColor =		0xFF000000 |
						(aGridColor.Red() << 16) |
						(aGridColor.Green() << 8) |
						aGridColor.Blue();
	iTextColor = aTextColor;
	}

void CSymbolMatrixDrawer::SetGridSize(const TSize& aSize)
	{
	iGridSize = aSize;
	}

TSize CSymbolMatrixDrawer::GridSize() const
	{
	return iGridSize;
	}



void CSymbolMatrixDrawer::Draw(	IGraphicsContext& aContext,
								const CFont* aFont,
								const TPoint& aPosition)
	{
	const TInt rows = iSymbolMatrix->RowCount();
	const TInt columns = iSymbolMatrix->ColumnCount();
	const TRect area(	aPosition.iX,
						aPosition.iY,
						aPosition.iX + iGridSize.iWidth * columns,
						aPosition.iY + iGridSize.iHeight * rows);
	
	IGraphicsDevice* device = aContext.GetGraphicsDevice();
	if (device == NULL)
		{
		return;
		}
	device->AddReference();
	
	TBool waslocked = device->IsLocked();
	if (!waslocked)
		{
		device->Lock();
		}
	
	uint32 selectedentry = 0xffffffff;
	iSymbolMatrix->GetSelectedEntry(selectedentry);
	
	// clear background
	CRect rect(area.iTl.iX, area.iTl.iY, area.Width(), area.Height());
	aContext.ClearRegion(iBackgroundColor, rect);
	
	// draw grid
	TInt i;
	TInt coord = aPosition.iY;
	for (i=0; i<rows + 1; i++)
		{
		DrawHorizontalLine(	device,
							area.iTl.iX,
							area.iBr.iX,
							coord,
							iGridColor);
		coord += iGridSize.iHeight;
		}
		
	coord = aPosition.iX;
	for (i=0; i<columns + 1; i++)
		{
		DrawVerticalLine(	device,
							coord,
							area.iTl.iY,
							area.iBr.iY,
							iGridColor);
		coord += iGridSize.iWidth;
		}

	
	// draw symbols using Symbian GDI
	CGraphicsContext* context = iApplication->SystemGc( *device );
	context->UseFont(aFont);
	context->SetPenColor(iTextColor);
	
	uint32 row = 0;
	uint32 column = 0;
	const char16* entry = NULL;
	TRect rc;
	const TInt textbaseline =	(iGridSize.iHeight >> 1) +
								(aFont->TextWidthInPixels(_L("@")) >> 1);
	
	for (i=0; i<(TInt)iSymbolMatrix->EntriesCount(); i++)
		{
		iSymbolMatrix->GetEntry(i, row, column, entry);
		if (entry)
			{
			TBuf<10> symbol = (TUint16*)entry;
			
			rc.iTl.iX = (aPosition.iX + column * iGridSize.iWidth) + 1;
			rc.iBr.iX = rc.iTl.iX + iGridSize.iWidth - 1;
			rc.iTl.iY = (aPosition.iY + row * iGridSize.iHeight) + 1;
			rc.iBr.iY = rc.iTl.iY + iGridSize.iHeight - 1;
			
			if ((TUint32)i == selectedentry)
				{
				// use inverted colors on selected item
				TRgb color(	((iBackgroundColor & 0x00FF0000) >> 16),
							((iBackgroundColor & 0x0000FF00) >> 8),
							(iBackgroundColor & 0x000000FF) );
				
				context->SetPenColor(color);
				
				// fill area with text color
				context->SetBrushColor(iTextColor);
				context->SetBrushStyle(CGraphicsContext::ESolidBrush);
				context->DrawRect(rc);
				}
			
			context->DrawText(	symbol,
								rc,
								textbaseline,
								CGraphicsContext::ECenter);
			
			if ((TUint32)i == selectedentry)
				{
				context->SetPenColor(iTextColor);
				context->SetBrushStyle(CGraphicsContext::ENullBrush);
				}
			}
		}
	
	context->DiscardFont();
	iApplication->ApplySystemGc();
	
	if (!waslocked)
		{
		device->Unlock();
		}
	device->Release();
	}


void CSymbolMatrixDrawer::DrawHorizontalLine(	IGraphicsDevice* aDevice,
												TInt aX1,
												TInt aX2,
												TInt aY,
												TUint32 aColor) const
	{
	// make sure start value is smaller than end value
	TInt start = aX1;
	TInt end = aX2;
	if (start > end)
		{
		TInt tmp;
		tmp = start;
		start = end;
		end = tmp;
		}
	
	// clip the line
	CSize size = aDevice->GetSize();
	if (start >= size.mX || end < 0 ||
		aY < 0 || aY >= size.mY)
		{
		return;
		}
	
	if (start < 0)
		{
		start = 0;
		}
	if (end > size.mX)
		{
		end = size.mX;
		}
	
	
	TUint32* pixels = (TUint32*)aDevice->GetAddress();
	TInt stride = (aDevice->GetStride() >> 2);
	
	// move to correct location
	pixels += aY * stride + start;
	
	TInt i;
	const TInt length = end - start;
	for (i=0; i<length; i++)
		{
		pixels[i] = aColor;
		}
	}


void CSymbolMatrixDrawer::DrawVerticalLine(	IGraphicsDevice* aDevice,
											TInt aX,
											TInt aY1,
											TInt aY2,
											TUint32 aColor) const
	{
	// make sure start value is smaller than end value
	TInt start = aY1;
	TInt end = aY2;
	if (start > end)
		{
		TInt tmp;
		tmp = start;
		start = end;
		end = tmp;
		}
	
	// clip the line
	CSize size = aDevice->GetSize();
	if (start >= size.mY || end < 0 ||
		aX < 0 || aX >= size.mX)
		{
		return;
		}
	
	if (start < 0)
		{
		start = 0;
		}
	if (end > size.mY)
		{
		end = size.mY;
		}

	TUint32* pixels = (TUint32*)aDevice->GetAddress();
	TInt stride = (aDevice->GetStride() >> 2);
	
	// move to correct location
	pixels += start * stride + aX;
	
	TInt i;
	const TInt length = end - start;
	for (i=0; i<length; i++)
		{
		*pixels = aColor;
		pixels += stride;
		}
	}

⌨️ 快捷键说明

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