📄 view.cpp
字号:
iCursorOn=ETrue;
iCursorX=aX;
iCursorY=aY;
}
TBool CFleetView::CursorOn() const
{
return iCursorOn;
}
void CFleetView::GetCursor(TInt& aX, TInt& aY) const
{
aX=iCursorX;
aY=iCursorY;
}
void CFleetView::MoveCursor(TInt aDx, TInt aDy)
{
TInt x=iCursorX+aDx;
TInt y=iCursorY+aDy;
if (x<0 || x>=8 || y<0 || y>=8)
return;
iCursorX=x;
iCursorY=y;
}
// view information, from MCoeView
TVwsViewId CFleetView::ViewId() const
{
return TVwsViewId(KUidTpShips, iViewUid);
}
//#define _DEBUGVIEWS_
#ifdef _DEBUGVIEWS_
void CFleetView::ViewActivatedL(const TVwsViewId& /*aPrevViewId*/, TUid aCustomMessageId,
const TDesC8& aCustomMessage)
{
// usual activation
Window().SetOrdinalPosition(0);
(static_cast<CGameAppUi*>(iEikonEnv->EikAppUi()))->SetActiveView(*this);
if (aCustomMessageId.iUid)
{
// display the message information
TBuf<200> buf,buf2;
buf.Format(_L("ID: %x: "), aCustomMessageId.iUid);
buf2.Copy(aCustomMessage);
buf.Append(buf2);
iEikonEnv->InfoMsg(buf);
}
}
#else
void CFleetView::ViewActivatedL(const TVwsViewId& /*aPrevViewId*/, TUid /*aCustomMessageId*/,const TDesC8& /*aCustomMessage*/)
{
Window().SetOrdinalPosition(0);
(static_cast<CGameAppUi*>(iEikonEnv->EikAppUi()))->SetActiveView(*this);
}
#endif
void CFleetView::ViewDeactivated()
{
}
//
// COppFleetView: specialises CFleetView for viewing opposition fleet
//
COppFleetView* COppFleetView::NewL(CFleetViewData& aFleetViewData,
TFleet& aFleet, TBool aP1)
{
COppFleetView* self = new (ELeave) COppFleetView(aFleetViewData, aFleet, aP1);
CleanupStack::PushL(self);
self->ConstructL();
CleanupStack::Pop(self);
return self;
}
COppFleetView::COppFleetView(CFleetViewData& aFleetViewData,
TFleet& aFleet,
TBool aP1)
:CFleetView(aFleetViewData, aFleet)
{
if (aP1)
iViewUid = KP1OppViewUID;
else
iViewUid = KP2OppViewUID;
}
TKeyResponse COppFleetView::OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
{
if (aType!=EEventKey)
return EKeyWasNotConsumed;
if (aKeyEvent.iCode==EQuartzKeyFourWayLeft ||
aKeyEvent.iCode==EQuartzKeyFourWayRight ||
aKeyEvent.iCode==EQuartzKeyFourWayUp ||
aKeyEvent.iCode==EQuartzKeyFourWayDown)
{
// move cursor
if (aKeyEvent.iCode==EQuartzKeyFourWayLeft)
MoveCursor(-1,0);
else if (aKeyEvent.iCode==EQuartzKeyFourWayRight)
MoveCursor(1,0);
else if (aKeyEvent.iCode==EQuartzKeyFourWayUp)
MoveCursor(0,-1);
else if (aKeyEvent.iCode==EQuartzKeyFourWayDown)
MoveCursor(0,1);
// redraw board
DrawTilesNow();
return EKeyWasConsumed;
}
else if (aKeyEvent.iCode==EQuartzKeyConfirm)
{
if (iFleet.IsKnown(iCursorX, iCursorY))
iEikonEnv->InfoMsg(R_GAME_ALREADY_KNOWN);
else
iData.iCmdHandler.ViewCmdHitFleet(iCursorX, iCursorY);
return EKeyWasConsumed;
}
return EKeyWasNotConsumed;
}
void COppFleetView::HandlePointerEventL(const TPointerEvent& aPointerEvent)
{
// check whether we're interested
if (
aPointerEvent.iType==TPointerEvent::EButton1Down &&
iData.iSeaArea.Contains(aPointerEvent.iPosition)
)
{
// identify the tile that was hit
TInt x=(aPointerEvent.iPosition.iX-iData.iSeaArea.iTl.iX)/iData.iTileSize;
TInt y=(aPointerEvent.iPosition.iY-iData.iSeaArea.iTl.iY)/iData.iTileSize;
// move cursor if necessary
TBool iCursorMoved=(x!=iCursorX || y!=iCursorY);
SetCursor(x,y);
// hit square unless it's already known
if (iFleet.IsKnown(x,y))
{
iEikonEnv->InfoMsg(R_GAME_ALREADY_KNOWN);
if (iCursorMoved)
DrawTilesNow();
}
else
iData.iCmdHandler.ViewCmdHitFleet(x,y);
}
}
//
// CMyFleetView: specialises CFleetView for viewing own fleet
//
CMyFleetView* CMyFleetView::NewL(CFleetViewData& aFleetViewData,
TFleet& aFleet, TBool aP1)
{
CMyFleetView* self = new (ELeave) CMyFleetView(aFleetViewData, aFleet, aP1);
CleanupStack::PushL(self);
self->ConstructL();
CleanupStack::Pop(self);
return self;
}
CMyFleetView::CMyFleetView(CFleetViewData& aFleetViewData,
TFleet& aFleet,
TBool aP1)
:CFleetView(aFleetViewData, aFleet)
{
if (aP1)
iViewUid = KP1MyViewUID;
else
iViewUid = KP2MyViewUID;
}
//
// Hider view
//
void CHiderView::ConstructL(const TRect& aRect)
{
CreateWindowL();
SetRect(aRect);
// Create and load a bitmap
iHideBitmap = new (ELeave) CFbsBitmap;
// Get bitmap store--drive independent
TFileName mbmName = iEikonEnv->EikAppUi()->Application()->BitmapStoreName();
User::LeaveIfError(iHideBitmap->Load(mbmName,0));
ActivateL();
}
CHiderView::~CHiderView()
{
delete iHideBitmap;
}
void CHiderView::Draw(const TRect& /*aRect*/) const
{
SystemGc().BitBlt(TPoint(0,0),iHideBitmap);
}
// from MCoeView
TVwsViewId CHiderView::ViewId() const
{
return TVwsViewId(KUidTpShips, KHiderViewUID);
}
void CHiderView::ViewActivatedL(const TVwsViewId& /*aPrevViewId*/, TUid /*aCustomMessageId*/,const TDesC8& /*aCustomMessage*/)
{
Window().SetOrdinalPosition(0);
}
void CHiderView::ViewDeactivated()
{
}
//
// CSoundEffects
//
_LIT(KExplSoundFile,"expl.wav");
_LIT(KMissSoundFile,"miss.wav");
_LIT(KSunkSoundFile,"sunk.wav");
CSoundEffects::CSoundEffects()
{
}
void CSoundEffects::ConstructL()
{
// Get path to sound files
TParsePtrC parse(CEikonEnv::Static()->EikAppUi()->Application()->AppFullName());
TPtrC appPath = parse.DriveAndPath();
ConstructSoundPlayerL(appPath, KExplSoundFile, iExplSoundPlayer);
ConstructSoundPlayerL(appPath, KMissSoundFile, iMissSoundPlayer);
ConstructSoundPlayerL(appPath, KSunkSoundFile, iSunkSoundPlayer);
}
void CSoundEffects::ConstructSoundPlayerL(const TDesC& aPath, const TDesC& aName,
CMdaAudioPlayerUtility*& aPlayer )
{
TFileName name = aPath;
name.Append(aName);
aPlayer=CMdaAudioPlayerUtility::NewFilePlayerL(name, iSoundPlayerCallBack);
}
CSoundEffects::~CSoundEffects()
{
CancelSounds();
delete iExplSoundPlayer;
delete iMissSoundPlayer;
delete iSunkSoundPlayer;
}
void CSoundEffects::CancelSounds()
{
if (iSoundPlayerCallBack.iPlaying)
iCurrentSoundPlayer->Stop();
iCurrentSoundPlayer = NULL;
}
CSoundEffects::TAudioPlayerCallBackHandler::TAudioPlayerCallBackHandler()
:iOK(EFalse),
iPlaying(EFalse)
{
}
void CSoundEffects::TAudioPlayerCallBackHandler::MapcInitComplete(TInt aError, const TTimeIntervalMicroSeconds& )
{
if (aError == KErrNone)
iOK = ETrue;
}
void CSoundEffects::TAudioPlayerCallBackHandler::MapcPlayComplete(TInt /*aError*/)
{
iPlaying = EFalse;
}
// play things
void CSoundEffects::PlaySound(TSound aSound)
{
if (!iSoundPlayerCallBack.iOK) return;
CancelSounds();
switch (aSound)
{
case EExplosion:
iCurrentSoundPlayer = iExplSoundPlayer;
break;
case EMiss:
iCurrentSoundPlayer = iMissSoundPlayer;
break;
case ESunk:
iCurrentSoundPlayer = iSunkSoundPlayer;
break;
};
iCurrentSoundPlayer->Play();
iSoundPlayerCallBack.iPlaying = ETrue;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -