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

📄 oandxappui.cpp

📁 Symbian OS C++ for Mobile Phones v3 Example Code
💻 CPP
字号:
// Copyright (c) 2004 - 2006, Symbian Software Ltd. All rights reserved.

#include <avkon.hrh>
#include <aknnotewrappers.h>

#include "OandXAppUi.h"
#include "oandxcontroller.h"
#include "oandxengine.h"
#include "OandXApplication.h"
#include "OandXAppView.h"
#include "OandXHistView.h"
#include "oandxdefs.h"
#include "OandX.hrh"
#include <OandX.rsg>

#define MaxInfoNoteTextLen 40

COandXAppUi::COandXAppUi()
/**
	This empty constructor is defined to ensure that exactly
	one instance is generated.
 */
	{
	// empty.
	}

void COandXAppUi::ConstructL()
/**
	Sets default skin paramters, creates engine and app view.
	
	@see ~COandXAppUi
 */
	{
	// for v1.x SDKs, use no-arg BaseConstructL() overload
	BaseConstructL(EAknEnableSkin);
	iEngine = COandXEngine::NewL();
	iController = COandXController::NewL();
	
	iHistView = COandXHistoryView::NewLC();
	AddViewL(iHistView);
	CleanupStack::Pop(); // history view

	iAppView = COandXGameView::NewLC();
	AddViewL(iAppView);
	CleanupStack::Pop(); // game view
	
    SetDefaultViewL(*iAppView);
   
	ReportWhoseTurn();
	}


COandXAppUi::~COandXAppUi()
/**
	This destructor undoes the secondary initialization in ConstructL.
	
	@see ConstructL
 */
	{
	delete iController;
	delete iEngine;
	}

void COandXAppUi::HandleCommandL(TInt aCommand)
/**
	This function handles commands from menus, softkeys,
	and from the system, e.g. EEikCmdExit.
	
	@param	aCommand		Command identifier.
 */
	{
	_LIT8(KDummy, "");
	switch(aCommand)
		{
	case EEikCmdExit:
	case EAknSoftkeyExit:
		{
		SaveL();
		Exit();
		}
		break;
	case EOandXNewGame:
		{
		iController->Reset();
		iAppView->Container()->ResetView();
		}
		break;
	case EOandXFirstPlayer:
		{
		iController->SwitchTurn();
		}
		break;
	case EOandXDisplayStats:
		{
		if (iHistView->IsActivated())
			{
			iHistView->ChangeDisplayL(EOandXSetStats);
			}
		else
			{
			ActivateViewL(TVwsViewId( KUidOandXApp, KUidOandXHistoryView), TUid::Uid(EHistoryViewDisplayStats), KDummy );
			}
		break;
		}
	case EOandXDisplayHistory:
		{
		if (iHistView->IsActivated())
			{
			iHistView->ChangeDisplayL(EOandXSetHistory);
			}
		else
			{
			ActivateViewL(TVwsViewId(KUidOandXApp, KUidOandXHistoryView), TUid::Uid(EHistoryViewDisplayHistory), KDummy );
			}
		break;
		}
	case EOandXDisplayGame:
		{
		ActivateViewL(TVwsViewId( KUidOandXApp, KUidOandXView)) ;
		break;
		}
	case EOandXResetHistory:
		{
		Controller().ResetStats();
		iHistView->ChangeDisplayL(iHistView->IsDisplayingHistory());
		break;
		}
	default:
		break;
		}
	}

void COandXAppUi::ReportWhoseTurn()
/**
	Tell the user whose turn it is by displaying a nought or cross symbol.
 */
	{
	iAppView->Container()->ShowTurn();
	}

void COandXAppUi::ReportWinnerL(TInt aWinner)
/**
	Tell the user who won the game by displaying the information
	in a platform-specific dialog.
 */
	{
	TBuf<MaxInfoNoteTextLen> text;
	iEikonEnv->ReadResource(text, aWinner==ETileCross ? R_OANDX_X_WINS : R_OANDX_O_WINS);
	CAknInformationNote* infoNote = new (ELeave) CAknInformationNote;
	infoNote->ExecuteLD(text);
	}

TStreamId COandXAppUi::StoreL(CStreamStore& aStore) const
/**
	Store the current game state in the supplied stream store.
	
	@param	aStore			Stream store which will contain new
							stream which encodes the game state.
	@return					The new stream's ID.
	@see RestoreL
 */
	{
	RStoreWriteStream stream;
	TStreamId id = stream.CreateLC(aStore);
	stream << *this; // alternatively, use ExternalizeL(stream)
	stream.CommitL();
	CleanupStack::PopAndDestroy();
	return id;
	}

void COandXAppUi::RestoreL(const CStreamStore& aStore, TStreamId aStreamId)
/**
	Restore the current game state from the supplied stream.
	
	@param	aStore			Stream store which contains a stream
							which contains the externalized game state.
	@param	aStreamId		ID of stream in the store which contains
							the externalize game state.
	@see StoreL
 */
	{
	RStoreReadStream stream;
	stream.OpenLC(aStore,aStreamId);
	stream >> *this; // alternatively use InternalizeL(stream)
	CleanupStack::PopAndDestroy();
	}

void COandXAppUi::ExternalizeL(RWriteStream& aStream) const
/**
	Write the app ui's state, which includes all of the game state,
	to a writable stream.
	
	@param	aStream			Stream to which game state should be written.
 */
	{
	iEngine->ExternalizeL(aStream);
	iController->ExternalizeL(aStream);
	aStream.WriteInt8L(iAppView->Container()->IdOfFocusControl());
	}

void COandXAppUi::InternalizeL(RReadStream& aStream)
/**
	Restore the game state from the supplied readable stream.
	
	@param	aStream			Stream which contains externalized
							game state.
 */
	{
	iEngine->InternalizeL(aStream);
	iController->InternalizeL(aStream);
	ReportWhoseTurn();
	iAppView->Container()->MoveFocusTo(aStream.ReadInt8L());
	}


// Global accessor functions

GLDEF_C COandXAppUi* OandXAppUi()
/**
Accessor function provides access to singleton instance of app ui object.
@return  The application UI, cast to COandXAppUi.
 */
	{
	return static_cast<COandXAppUi*>(CEikonEnv::Static()->AppUi());
	}

GLDEF_C COandXController& Controller()
/**
Return mutable reference to singleton instance of COandXController object.
@return	Singleton COandXController object.
 */
	{
	return *OandXAppUi()->iController;
	}

GLDEF_C COandXEngine& Engine()
/**
Return mutable reference to singleton instance of COandXEngine object.
@return	Singleton COandXEngine object.
 */
	{
	return *OandXAppUi()->iEngine;
	}

⌨️ 快捷键说明

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