mopoidcontainer.cpp

来自「mopoid game symbian os application devel」· C++ 代码 · 共 339 行

CPP
339
字号
/*
========================================================================
 Name        : MopoidContainer.cpp
 Author      : 
 Copyright   : 
 Description : Owns the visible UI resources.
 License     : 
 
========================================================================
*/
// [[[ begin generated region: do not modify [Generated System Includes]
#include <aknviewappui.h>
#include <eikappui.h>
#include <Mopoid.rsg>
// ]]] end generated region [Generated System Includes]

// [[[ begin generated region: do not modify [Generated User Includes]
#include "MopoidContainer.h"
#include "MopoidContainerView.h"
#include "Mopoid.hrh"
#include "MopoidContainer.hrh"
// ]]] end generated region [Generated User Includes]

// [[[ begin generated region: do not modify [Generated Constants]
// ]]] end generated region [Generated Constants]

/**
 * First phase of Symbian two-phase construction. Should not 
 * contain any code that could leave.
 */
CMopoidContainer::CMopoidContainer()
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	// ]]] end generated region [Generated Contents]

	}
/** 
 * Destroy child controls.
 */
CMopoidContainer::~CMopoidContainer()
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	// ]]] end generated region [Generated Contents]

	delete iGameEngine;
	}

/**
 * Construct the control (first phase).
 *  Creates an instance and initializes it.
 *  Instance is not left on cleanup stack.
 * @param aRect bounding rectangle
 * @param aParent owning parent, or NULL
 * @param aCommandObserver command observer
 * @return initialized instance of CMopoidContainer
 */
CMopoidContainer* CMopoidContainer::NewL(const TRect& aRect,
		const CCoeControl* aParent, MEikCommandObserver* aCommandObserver)
	{
	CMopoidContainer* self = CMopoidContainer::NewLC(aRect, aParent,
			aCommandObserver);
	CleanupStack::Pop(self);
	return self;
	}

/**
 * Construct the control (first phase).
 *  Creates an instance and initializes it.
 *  Instance is left on cleanup stack.
 * @param aRect The rectangle for this window
 * @param aParent owning parent, or NULL
 * @param aCommandObserver command observer
 * @return new instance of CMopoidContainer
 */
CMopoidContainer* CMopoidContainer::NewLC(const TRect& aRect,
		const CCoeControl* aParent, MEikCommandObserver* aCommandObserver)
	{
	CMopoidContainer* self = new ( ELeave ) CMopoidContainer();
	CleanupStack::PushL(self);
	self->ConstructL(aRect, aParent, aCommandObserver);
	return self;
	}

/**
 * Construct the control (second phase).
 *  Creates a window to contain the controls and activates it.
 * @param aRect bounding rectangle
 * @param aCommandObserver command observer
 * @param aParent owning parent, or NULL
 */
void CMopoidContainer::ConstructL(const TRect& aRect,
		const CCoeControl* aParent, MEikCommandObserver* aCommandObserver)
	{
	if (!aParent)
		{
		CreateWindowL();
		}
	else
		{
		SetContainerWindowL( *aParent);
		}
	iFocusControl = NULL;
	iCommandObserver = aCommandObserver;
	InitializeControlsL();

	// Load game engine
	iGameEngine = CMopoidGameEngine::NewL(this);
	
	// Set the window to fullscreen including the softkey-bar, not to the rectangle
	// that the application gets with this function (which would not draw over the
	// softkey-bar).
	SetExtentToWholeScreen();	// Results in a call of SizeChanged()
	
	iGameEngine->StartNewGameL();
	
	ActivateL();
	// [[[ begin generated region: do not modify [Post-ActivateL initializations]
	// ]]] end generated region [Post-ActivateL initializations]

	}

/**
 * Return the number of controls in the container (override)
 * @return count
 */
TInt CMopoidContainer::CountComponentControls() const
	{
	return ( int ) ELastControl;
	}

/**
 * Get the control with the given index (override)
 * @param aIndex Control index [0...n) (limited by #CountComponentControls)
 * @return Pointer to control
 */
CCoeControl* CMopoidContainer::ComponentControl(TInt aIndex) const
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	switch ( aIndex )
		{
		}
	// ]]] end generated region [Generated Contents]

	// handle any user controls here...

	return NULL;
	}

/**
 *	Handle resizing of the container. This implementation will lay out
 *  full-sized controls like list boxes for any screen size, and will layout
 *  labels, editors, etc. to the size they were given in the UI designer.
 *  This code will need to be modified to adjust arbitrary controls to
 *  any screen size.
 */
void CMopoidContainer::SizeChanged()
	{
	CCoeControl::SizeChanged();
	LayoutControls();
	// [[[ begin generated region: do not modify [Generated Contents]
			
	// ]]] end generated region [Generated Contents]

	if (iGameEngine)
		{
		// TODO: Query the current (= new) size of the control
		// and send it to the game engine, so that it can resize
		// all the graphics accordingly.
		iGameEngine->SetScreenSize(this->Size());
		}
	}

// [[[ begin generated function: do not modify
/**
 * Layout components as specified in the UI Designer
 */
void CMopoidContainer::LayoutControls()
	{
	}
// ]]] end generated function

/**
 *	Handle key events.
 */
TKeyResponse CMopoidContainer::OfferKeyEventL(const TKeyEvent& aKeyEvent,
		TEventCode aType)
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	
	// ]]] end generated region [Generated Contents]

	TKeyResponse handled = EKeyWasNotConsumed;
	if (aType == EEventKeyDown)
		{
		// Player pressed a key. Store this so that the panel moves in the
		// corresponding direction until the player releases the key.
		switch (aKeyEvent.iScanCode)
			{
			// TODO: Handle left and right keys

            // Handle left and right keys
            case EStdKeyLeftArrow:
                {
                    iGameEngine->iKeyHandler.LeftPressed();
    				handled = EKeyWasConsumed;
                    break;
                }
            case EStdKeyRightArrow:
                {
                    iGameEngine->iKeyHandler.RightPressed();
    				handled = EKeyWasConsumed;
                    break;
                }
			case EStdKeyDevice0:
			case EStdKeyDevice1:
				{
				// Left or right softkey
				iGameEngine->PauseGame();
				handled = EKeyWasConsumed;
				break;
				}
			case '5':
			case EStdKeyDevice3:
				{
				// Joystick pressed. If the menu was active, resume the game.
				if (iGameEngine->iHaveFocus
						&& iGameEngine->iSettings->iGamePaused)
					{
					iGameEngine->ResumeGameL();
					handled = EKeyWasConsumed;
					}
				break;
				}
			}
		}
	else
		if (aType == EEventKeyUp)
			{
			// Player released a key.
			switch (aKeyEvent.iScanCode)
				{
				case EStdKeyLeftArrow:
				case EStdKeyRightArrow:
				case '4':
				case '6':
					{
					// Not moving in any direction anymore.
					iGameEngine->iKeyHandler.NoDirection();
					handled = EKeyWasConsumed;
					break;
					}
				}

			}
	return handled;
	}

// [[[ begin generated function: do not modify
/**
 *	Initialize each control upon creation.
 */				
void CMopoidContainer::InitializeControlsL()
	{
	
	}
// ]]] end generated function

/** 
 * Handle global resource changes, such as scalable UI or skin events (override)
 */
void CMopoidContainer::HandleResourceChange(TInt aType)
	{
	CCoeControl::HandleResourceChange(aType);
	// TODO: Adapt the game field size to the current screen resolution
	// and orientation by calling SetExtentToWholeScreen()
	// in case the resource change type is a layout variant switch.
	if(aType == KEikDynamicLayoutVariantSwitch)
	{
		// User switched the layout configuration or the screen resolution :)
		// -> we have to recreate the layout
		SetExtentToWholeScreen();	// Results in a call of SizeChanged()
	}
	// [[[ begin generated region: do not modify [Generated Contents]
	// ]]] end generated region [Generated Contents]

	}

/**
 *	Draw container contents.
 */
void CMopoidContainer::Draw(const TRect& aRect) const
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	CWindowGc& gc = SystemGc();
	gc.Clear( aRect );
	
	// ]]] end generated region [Generated Contents]
	
	// WARNING: Remove the gc.Clear(aRect) from the automatically generated
	// region above! Otherwise, the screen will flicker on some devices
	// like the E61 due to the frequent white filling of the screen.
	
	if (iGameEngine && iGameEngine->iBackBufferBmp)
		{
		gc.BitBlt(TPoint(0, 0), iGameEngine->iBackBufferBmp);

		// Game paused?
		if (iGameEngine->iSettings->iGamePaused)
			{
			// Choose a red color
			gc.SetPenStyle(CGraphicsContext::ESolidPen);
			gc.SetPenColor(KRgbRed);
			TBuf<30> tempStr;

			// Display the message to press the joystick to end the pause.
			if (iGameEngine->iSettings->iGameStatus
					!= CMopoidSettings::EFinished
					&& iGameEngine->iSettings->iGameStatus
							!= CMopoidSettings::EDied)
				{
				gc.UseFont(iGameEngine->iSettings->iFontUsed);
				CEikonEnv::Static()->ReadResource(tempStr, R_PRESSJOYSTICK);
				gc.DrawText(tempStr, iGameEngine->iSettings->iScreenRect,
						iGameEngine->iSettings->iScreenRect.Height() / 4 * 3 + iGameEngine->iSettings->iTextYOffset, CGraphicsContext::ECenter);
				}

			// Display game paused string.
			if (iGameEngine->iSettings->iGameStatus == CMopoidSettings::EPlaying)
				{
				gc.UseFont(CEikonEnv::Static()->TitleFont());
				CEikonEnv::Static()->ReadResource(tempStr, R_PAUSE);
				gc.DrawText(tempStr, iGameEngine->iSettings->iScreenRect,
						iGameEngine->iSettings->iScreenRect.Height() / 4 * 3, CGraphicsContext::ECenter);
				}
			}
		}
	}

⌨️ 快捷键说明

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