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

📄 oandxappui.cpp

📁 《SymbianOSC手机应用开发》源码
💻 CPP
字号:
// Copyright (c) 2004, Symbian Software Ltd. All rights reserved.

#include <uikon.hrh>

#include "OandXAppUi.h"
#include "OandXAppView.h"
#include "oandxdefs.h"
#include "OandX.hrh"
#include <OandX.rsg>
#include <eikspane.h>
#include <oandx.mbg>
#include <eikfutil.h>
#include "OandX.pan"

#define MaxInfoNoteTextLen 40


// First player selection dialog

/*
// Use this class for 'standard' dialog processing

class COandXFirstPlayDialog : public CEikDialog
    {
public:
    COandXFirstPlayDialog(TBool aCrossTurn, TBool& aFlag);
private:
    // from CEikDialog
    void PostLayoutDynInitL(); // initialization
    TBool OkToExitL(TInt aKeycode); // termination
private:
    TBool iCrossTurn;
    TBool& iChanged;
    };

COandXFirstPlayDialog::COandXFirstPlayDialog(TBool aCrossTurn, TBool& aFlag)
	: iCrossTurn(aCrossTurn), iChanged(aFlag)
	{
	}

void COandXFirstPlayDialog::PostLayoutDynInitL()
    {
    CEikListBox *listBox = (CEikListBox *)Control(EOandXPlayerListControl);
    listBox->SetCurrentItemIndex(iCrossTurn ? 0:1); // Suggest a change
    };

TBool COandXFirstPlayDialog::OkToExitL(TInt [/]*aKeycode*[/])

	{
    CEikListBox *listBox = (CEikListBox *)Control(EOandXPlayerListControl);
    TInt index = listBox->CurrentItemIndex();
    if (((index == 1) && !iCrossTurn) || ((index == 0) && iCrossTurn))
        {
        iChanged = ETrue;
        }
	return ETrue;
	}
*/

// AppUi class functions

COandXAppUi::COandXAppUi()
    {
    }

COandXAppUi::~COandXAppUi()
    {
    if (iAppView)
        {
        iEikonEnv->RemoveFromStack(iAppView);
        delete iAppView;
        iAppView = NULL;
        }
    iTileState.Close();
    iWinLines.Close();
    }

void COandXAppUi::SetWinLineL(TInt aTile0, TInt aTile1, TInt aTile2)
    {
    User::LeaveIfError(iWinLines.Append(aTile0));
    User::LeaveIfError(iWinLines.Append(aTile1));
    User::LeaveIfError(iWinLines.Append(aTile2));
    }


void COandXAppUi::ConstructL()
    {
    BaseConstructL();
	//SetToolbarL(R_OANDX_NOUGHT_TOOLBAR);
    iAppView = new(ELeave) COandXAppView();
    iAppView->ConstructL(ClientRect(), this);
    AddToStackL(iAppView); // Enable keypresses to the view
    for (TInt i=0; i<KNumberOfTiles; i++)
        {
        User::LeaveIfError(iTileState.Append(ETileBlank));
        }
    // Set the eight possible winning lines
    SetWinLineL(0,1,2);
    SetWinLineL(3,4,5);
    SetWinLineL(6,7,8);
    SetWinLineL(0,3,6);
    SetWinLineL(1,4,7);
    SetWinLineL(2,5,8);
    SetWinLineL(0,4,8);
    SetWinLineL(2,4,6);
    }


void COandXAppUi::HandleCommandL(TInt aCommand)
    {
    switch(aCommand)
        {
        case EEikCmdExit:
            {
            SaveL();
            Exit();
            }
            break;
        case EOandXNewGame:
            {
            for (TInt i=0; i<KNumberOfTiles; i++)
                {
                iTileState[i] = 0;
                }
            iGameStatus = EGameNew;
            if (IsCrossTurn())
                {
                SwitchTurnL();
                }
            iAppView->ResetView();
            }
            break;
        case EOandXSwitchTurn:
            {
// Use this code for 'standard' dialog processing
/*
            TBool playerChanged=EFalse;
            CEikDialog *dialog=new(ELeave) COandXFirstPlayDialog(iCrossTurn,playerChanged);
            if (dialog->ExecuteLD(R_OANDX_FIRST_MOVE_DIALOG))
                {
                if (playerChanged)
                    {
                    SwitchTurnL();
                    }
                }
*/
// Use this code for menu-only first player selection
            SwitchTurnL();
// End of code alternatives
            }
            break;
        default:
            {
            break;
            }
            break;
        }
    }

void COandXAppUi::DynInitMenuPaneL(TInt aResourceId, CEikMenuPane* aMenuPane)
    {
    if (aResourceId == R_OANDX_MENU)
        {
        if (iGameStatus == EGameNew)
            {
            CEikMenuPaneItem::SData item;

// Use this line for menu-only first player selection
            iCoeEnv->ReadResource(item.iText, IsCrossTurn()?R_OANDX_O_MOVES_FIRST:R_OANDX_X_MOVES_FIRST );

// Use this line for first player selection by dialog
//            iCoeEnv->ReadResource(item.iText, R_OANDX_WHO_MOVES_FIRST );

            item.iCommandId = EOandXSwitchTurn;
            item.iFlags = 0;
            item.iCascadeId = 0;
            aMenuPane->AddMenuItemL(item);
            }
        }
    }

TBool COandXAppUi::IsCrossTurn()
    {
    return iCrossTurn;
    }

void COandXAppUi::SetToolbarPaneL(TBool aCrossTurn)
    {
    //SetToolbarL(aCrossTurn ? R_OANDX_CROSS_TOOLBAR : R_OANDX_NOUGHT_TOOLBAR);
    }

void COandXAppUi::SwitchTurnL()
    {
    SetToolbarPaneL(!iCrossTurn);    
    iCrossTurn = !iCrossTurn;
    }

TBool COandXAppUi::HitSquareL(TInt aIndex)
    {
    if (iGameStatus == EGameEnded)
        {
        return EFalse;
        }
    if (iGameStatus == EGameNew)
        {
        iGameStatus = EGamePlaying;
        }
    if (iTileState[aIndex] == ETileBlank)
        {
        TBool crossTurn = IsCrossTurn();
        SwitchTurnL();
        iTileState[aIndex] = crossTurn ? ETileCross : ETileNought;
        if (TInt winner = GameWonBy())
            {
            iGameStatus = EGameEnded;
            iEikonEnv->InfoMsg(winner==ETileCross ? R_OANDX_X_WINS : R_OANDX_O_WINS);
            }
        return ETrue;
        }
    return EFalse;
    }

TInt COandXAppUi::SquareStatus(TInt aIndex)
    {
    return iTileState[aIndex];
    }

TInt COandXAppUi::GameWonBy()
    {
    TInt sum = 0;
    for (TInt i=0; i<KCountWinLines; i++)
        {
        for (TInt j=0; j<KTilesPerWinLine; j++)
            {
            sum += iTileState[iWinLines[i*KTilesPerWinLine+j]];
            }
        if ((sum == KTilesPerWinLine * ETileNought) || (sum == KTilesPerWinLine * ETileCross))
            { // all noughts or all crosses
            sum /= KTilesPerWinLine; // equal to ETileNought or ETileCross
            break;
            }
        else
            {
            sum = 0; // no winner
            }
        }
    return sum;
    }

TStreamId COandXAppUi::StoreL(CStreamStore& aStore) const
    {
    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)
    {
    RStoreReadStream stream;
    stream.OpenLC(aStore,aStreamId);
    stream >> *this; // alternatively use InternalizeL(stream)
    CleanupStack::PopAndDestroy();
    }

void COandXAppUi::ExternalizeL(RWriteStream& aStream) const
    {
    for (TInt i = 0; i<KNumberOfTiles; i++)
        {
        aStream.WriteInt8L(iTileState[i]);
        }
    aStream.WriteInt8L(iGameStatus);
    aStream.WriteInt8L(iCrossTurn);
    aStream.WriteInt8L(iAppView->IdOfFocusControl());
    }

void COandXAppUi::InternalizeL(RReadStream& aStream)
    {
    for (TInt i = 0; i<KNumberOfTiles; i++)
        {
        iTileState[i] = aStream.ReadInt8L();
        }
    iGameStatus = aStream.ReadInt8L();
    iCrossTurn = aStream.ReadInt8L();
    SetToolbarPaneL(iCrossTurn);
    iAppView->MoveFocusTo(aStream.ReadInt8L());
    }

⌨️ 快捷键说明

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