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

📄 tictactoeappview.cpp

📁 《Symbian S60第3版手机程序开发与实用教程》光盘源代码
💻 CPP
字号:
/*
* ==============================================================================
*  Name        : TicTacToeAppView.cpp
*  Part of     : TicTacToe
*  Description : 
*  Version     : 
*
*  Copyright (c) 2005 Nokia Corporation.
* ==============================================================================
*/

// INCLUDE FILES

#include "TicTacToeAppView.h"
#include <TicTacToe.rsg>
#include <TicTacToe.mbg>

#include <coemain.h>
#include <aknnotewrappers.h>
#include <akniconutils.h>
#include <stringloader.h>

// CONSTANTS

const TInt KTttMarginFactor = 24;

_LIT(KTttIconFile, "\\resource\\apps\\TicTacToe.mif");

// ============================ MEMBER FUNCTIONS ===============================

// -----------------------------------------------------------------------------
// CTicTacToeAppView::NewL
// Create a CTicTacToeAppView object, which will draw itself to aRect.
// -----------------------------------------------------------------------------
//
CTicTacToeAppView* CTicTacToeAppView::NewL(const TRect& aRect, TInt aGraphicsSet)
    {
    CTicTacToeAppView* self = new (ELeave) CTicTacToeAppView;
    CleanupStack::PushL(self);
    self->ConstructL(aRect, aGraphicsSet);
    CleanupStack::Pop(self);
    return self;
    }

// -----------------------------------------------------------------------------
// CTicTacToeAppView::ConstructL
// Perform the second phase construction of a CTicTacToeAppView object.
// -----------------------------------------------------------------------------
//
void CTicTacToeAppView::ConstructL(const TRect& aRect, TInt aGraphicsSet)
    {
    // Create a window for this application view
    CreateWindowL();

    // Set the windows size
    SetRect(aRect);

    LoadIconsL(aGraphicsSet);

    // Activate the window, which makes it ready to be drawn
    ActivateL();
    }

// -----------------------------------------------------------------------------
// CTicTacToeAppView::CTicTacToeAppView
// Perform the first phase of two phase construction.
// -----------------------------------------------------------------------------
//
CTicTacToeAppView::CTicTacToeAppView()
    {
    NewGame();
    }


// -----------------------------------------------------------------------------
// CTicTacToeAppView::~CTicTacToeAppView
// Destroy the object.
// -----------------------------------------------------------------------------
//
CTicTacToeAppView::~CTicTacToeAppView()
    {
    delete iBitmapGrid;
    delete iBitmapGridMask;
    delete iBitmapNought;
    delete iBitmapNoughtMask;
    delete iBitmapCross;
    delete iBitmapCrossMask;
    }

// -----------------------------------------------------------------------------
// CTicTacToeAppView::LoadIconsL
// Load a set of graphics.
// -----------------------------------------------------------------------------
//
void CTicTacToeAppView::LoadIconsL(TInt aGraphicsSet)
    {
    // Delete the old icon bitmaps
    delete iBitmapGrid;
    iBitmapGrid = NULL;
    delete iBitmapGridMask;
    iBitmapGridMask = NULL;
    delete iBitmapNought;
    iBitmapNought = NULL;
    delete iBitmapNoughtMask;
    iBitmapNoughtMask = NULL;
    delete iBitmapCross;
    iBitmapCross = NULL;
    delete iBitmapCrossMask;
    iBitmapCrossMask = NULL;

    TFileName iconFile(KTttIconFile);
    User::LeaveIfError(CompleteWithAppPath(iconFile));
    
    // Create the new icon bitmaps
    if ( 0 == aGraphicsSet )
        {
        AknIconUtils::CreateIconL(iBitmapGrid, iBitmapGridMask, iconFile, EMbmTictactoeGrid, EMbmTictactoeGrid_mask);
        AknIconUtils::CreateIconL(iBitmapNought, iBitmapNoughtMask, iconFile, EMbmTictactoeNought, EMbmTictactoeNought_mask);
        AknIconUtils::CreateIconL(iBitmapCross, iBitmapCrossMask, iconFile, EMbmTictactoeCross, EMbmTictactoeCross_mask);
        }
    else
        {
        AknIconUtils::CreateIconL(iBitmapGrid, iBitmapGridMask, iconFile, EMbmTictactoeGrid2, EMbmTictactoeGrid2_mask);
        AknIconUtils::CreateIconL(iBitmapNought, iBitmapNoughtMask, iconFile, EMbmTictactoeNought2, EMbmTictactoeNought2_mask);
        AknIconUtils::CreateIconL(iBitmapCross, iBitmapCrossMask, iconFile, EMbmTictactoeCross2, EMbmTictactoeCross2_mask);
        }

    SetIconSizes();
    DrawDeferred();
    }

// -----------------------------------------------------------------------------
// CTicTacToeAppView::NewGame
// Start a new game.
// -----------------------------------------------------------------------------
//
void CTicTacToeAppView::NewGame()
    {
    iBoard.Reset();
    iPlayer = ETttPlayerNought;     // Noughts always go first
    DrawDeferred();
    }

// -----------------------------------------------------------------------------
// CTicTacToeAppView::Draw
// Draw this CTicTacToeAppView to the screen.
// -----------------------------------------------------------------------------
//
void CTicTacToeAppView::Draw(const TRect& /*aRect*/) const
    {
    // Get the standard graphics context 
    CWindowGc& gc = SystemGc();

    // Clear the screen
    gc.Clear(Rect());

    // Draw the board
    gc.BitBltMasked(iBoardRect.iTl, iBitmapGrid, iBoardRect.Size(), iBitmapGridMask, EFalse);
    for ( TInt x = 0; x < KTicTacToeColumns; x++ )
        {
        TPoint topLeft = iBoardRect.iTl + iCellMargin + TPoint(x * iBoardRect.Width() / KTicTacToeColumns, 0);

        for ( TInt y = 0; y < KTicTacToeRows; y++ )
            {
            TTicTacToePlayer player = iBoard.Player(x, y);

            switch ( player )
                {
                case ETttPlayerNought:
                    gc.BitBltMasked(topLeft, iBitmapNought, iCellSize, iBitmapNoughtMask, EFalse);
                    break;
                case ETttPlayerCross:
                    gc.BitBltMasked(topLeft, iBitmapCross, iCellSize, iBitmapCrossMask, EFalse);
                    break;
                default:
                    // Do nothing
                    break;
                }

            topLeft += TPoint(0, iBoardRect.Height() / KTicTacToeRows);
            }
        }
    }

// -----------------------------------------------------------------------------
// CTicTacToeAppView::OfferKeyEventL
// Handle any user keypresses.
// -----------------------------------------------------------------------------
//
TKeyResponse CTicTacToeAppView::OfferKeyEventL(const TKeyEvent& aKeyEvent,
                                                TEventCode aType)
    {
    // We only want the key press, not the key up/down event
    // Check if a number key 1-9 was pressed
    if ( EEventKey == aType && '1' <= aKeyEvent.iCode && aKeyEvent.iCode <= '9' )
        {
        TInt cell = aKeyEvent.iCode - '1';

        // Try move in the cell
        if ( iBoard.Move(cell, iPlayer) )
            {
            DrawDeferred();

            // Check if the game is finished
            if ( iBoard.HasWon(iPlayer) )
                {
                _LIT(KPlayerNoughtStr, "O");
                _LIT(KPlayerCrossStr, "X");
                HBufC* message = StringLoader::LoadLC(R_TICTACTOE_WINNER_STR,
                            ( ETttPlayerCross == iPlayer ) ? KPlayerCrossStr : KPlayerNoughtStr);
                CAknInformationNote* informationNote = new (ELeave) CAknInformationNote;
                informationNote->ExecuteLD(*message);
                CleanupStack::PopAndDestroy(message);
                }
            else if ( iBoard.IsDraw() )
                {
                HBufC* message = StringLoader::LoadLC(R_TICTACTOE_DRAW_STR);
                CAknInformationNote* informationNote = new (ELeave) CAknInformationNote;
                informationNote->ExecuteLD(*message);
                CleanupStack::PopAndDestroy(message);
                }

            // Switch player
            iPlayer = ( ETttPlayerCross == iPlayer ) ? ETttPlayerNought : ETttPlayerCross;
            }

        return EKeyWasConsumed;
        }

    // Return the default functionality
    return CCoeControl::OfferKeyEventL(aKeyEvent, aType);
    }

// -----------------------------------------------------------------------------
// CTicTacToeAppView::SizeChanged
// Handle a change in the size of the control.
// -----------------------------------------------------------------------------
//
void CTicTacToeAppView::SizeChanged()
    {
    SetIconSizes();
    DrawDeferred();
    }

// -----------------------------------------------------------------------------
// CTicTacToeAppView::SetIconSizes
// Set the sizes of the scalable icons.
// -----------------------------------------------------------------------------
//
void CTicTacToeAppView::SetIconSizes()
    {
    TRect rect(Rect());
    if ( rect.Width() > rect.Height() )
        {
        iBoardRect.SetRect(TPoint((rect.Width() - rect.Height()) / 2, 0),
                            TSize(rect.Height(), rect.Height()));
        }
    else
        {
        iBoardRect.SetRect(TPoint(0, (rect.Height() - rect.Width()) / 2),
                            TSize(rect.Width(), rect.Width()));
        }

    iCellMargin.iX = iBoardRect.Width() / KTttMarginFactor;
    iCellMargin.iY = iBoardRect.Height() / KTttMarginFactor;
    iCellSize.SetSize((iBoardRect.Width() / KTicTacToeColumns) - (iCellMargin.iX * 2),
                    (iBoardRect.Height() / KTicTacToeRows) - (iCellMargin.iY * 2));

    AknIconUtils::SetSize(iBitmapGrid, iBoardRect.Size());
    AknIconUtils::SetSize(iBitmapGridMask, iBoardRect.Size());
    AknIconUtils::SetSize(iBitmapNought, iCellSize);
    AknIconUtils::SetSize(iBitmapNoughtMask, iCellSize);
    AknIconUtils::SetSize(iBitmapCross, iCellSize);
    AknIconUtils::SetSize(iBitmapCrossMask, iCellSize);
    }

//  End of File

⌨️ 快捷键说明

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