📄 s60btlabappview.cpp
字号:
}
else
{
return EKeyWasNotConsumed;
}
return EKeyWasConsumed;
}
void CS60BTLabAppView::StartClientL()
{
if (iClient || iServer)
{
EndGameL();
}
iClient = CBTConnectionClient::NewL(*this, *this);
iClient->ConnectL();
}
void CS60BTLabAppView::StartHostL()
{
if (iClient || iServer)
{
EndGameL();
}
iServer = CBTConnectionHost::NewL(*this, *this);
iServer->StartL();
iWaitNote = new (ELeave) CAknWaitDialog(reinterpret_cast<CEikDialog**>(&iWaitNote), ETrue);
if (iWaitNote->ExecuteLD(R_WAIT_NOTE_CONNECTION_SOFTKEY_CANCEL) == 0)
{
// The user pressed cancel
iServer->StopL();
}
}
void CS60BTLabAppView::EndGameL()
{
LogL(_L8("EndGameL - function entry"));
delete iClient;
iClient = NULL;
if (iServer)
{
delete iServer;
iServer = NULL;
}
// reset the counter to 0
if (iMySprite)
{
iMySprite->SetBounceCount(0);
}
LogL(_L8("EndGameL - function exit"));
}
void CS60BTLabAppView::UpdateScreen()
{
// Move the player's ball sprite
iMySprite->Move(iBallImage->SizeInPixels(),Rect());
// and the other player's ball
iOtherSprite->Move(iOtherBallImage->SizeInPixels(),Rect());
// Update the screen
CWindowGc& gc = SystemGc();
gc.Activate(*DrawableWindow());
UpdateDisplay();
gc.Deactivate();
}
void CS60BTLabAppView::ClientConnected(TInt aError)
{
if (iWaitNote)
{
TRAPD(error, iWaitNote->ProcessFinishedL();)
}
if (KErrNone == aError)
{
TRAP(aError, SendPositionsToClientL();)
}
else
{
TRAPD(
error,
HBufC* buf = CEikonEnv::Static()->AllocReadResourceL(R_CONNECTION_ERROR_TEXT);
CAknInformationNote* informationNote = new (ELeave) CAknInformationNote;
informationNote->ExecuteLD(buf->Des());
delete buf;
)
LogL(_L8("ClientConnected, error = "), aError);
}
}
void CS60BTLabAppView::ClientDataRead(const TDesC8& aData)
{
TRAPD(
error,
if (aData.Length())
{
TKeyPress keyPress = (TKeyPress)aData[0];
switch (keyPress)
{
case ELeftArrow:
if(iOtherSprite->GetXVelocity() > 0)
{
LogL(_L8("Client pressed left"));
// change X velocity so ball is moving left
iOtherSprite->SetXVelocity(-(iOtherSprite->GetXVelocity()));
}
break;
case ERightArrow:
if(iOtherSprite->GetXVelocity() < 0)
{
LogL(_L8("Client pressed right"));
// change X velocity so ball is moving right
iOtherSprite->SetXVelocity(-(iOtherSprite->GetXVelocity()));
}
break;
case EUpArrow:
if(iOtherSprite->GetYVelocity() > 0)
{
LogL(_L8("Client pressed up"));
// change Y velocity so ball is moving up
iOtherSprite->SetYVelocity(-(iOtherSprite->GetYVelocity()));
}
break;
case EDownArrow:
if(iOtherSprite->GetYVelocity() < 0)
{
LogL(_L8("Client pressed down"));
// change Y velocity so ball is moving down
iOtherSprite->SetYVelocity(-(iOtherSprite->GetYVelocity()));
}
break;
case ENoKey:
// do nothing
break;
}
SendPositionsToClientL();
)
}
}
void CS60BTLabAppView::ClientDisconnected(TInt /* aError */)
{
TRAPD(
error,
HBufC* buf = CEikonEnv::Static()->AllocReadResourceL(R_CLIENT_DISCONNECTED_TEXT);
CAknInformationNote* informationNote = new (ELeave) CAknInformationNote;
informationNote->ExecuteLD(buf->Des());
delete buf;
)
}
void CS60BTLabAppView::HostConnected(TInt aError)
{
TRAPD(
err,
if (KErrNone == aError)
{
LogL(_L8("HostConnected, reading data"));
iClient->ReadDataL();
}
else
{
TInt resourceId = R_CONNECTION_ERROR_TEXT;
if (KErrNotFound == aError)
{
resourceId = R_HOST_NOT_AVAILABLE_TEXT;
}
HBufC* buf = CEikonEnv::Static()->AllocReadResourceL(R_CONNECTION_ERROR_TEXT);
CAknInformationNote* informationNote = new (ELeave) CAknInformationNote;
informationNote->ExecuteLD(buf->Des());
delete buf;
LogL(_L8("HostConnected, error = "), aError);
}
)
}
void CS60BTLabAppView::HostDataRead(const TDesC8& aData)
{
TInt error = KErrNone;
TBuf8<5> posData = aData;
TRAP(
error,
if (posData.Length() == 5)
{
SetSpritePositions(posData);
UpdateScreen(); // update the screen
// Need to write data to server wrt key presses
TBuf8<1> keyPressData(1);
keyPressData[0] = static_cast<TUint8>(iKeyPressed);
switch (iKeyPressed)
{
case ELeftArrow:
LogL(_L8("Pressed left"));
break;
case ERightArrow:
LogL(_L8("Pressed right"));
break;
case EUpArrow:
LogL(_L8("Pressed up"));
break;
case EDownArrow:
LogL(_L8("Pressed down"));
break;
default:
// do nothing
break;
}
iClient->WriteDataSyncL(keyPressData);
iKeyPressed = ENoKey;
LogL(_L8("Reading pos..."));
iClient->ReadDataL();
}
else
{
LogL(_L8("HostDataRead, length = "), aData.Length());
}
)
if (KErrNotReady == error ||
KErrDisconnected == error)
{
HostDisconnected(error);
}
}
void CS60BTLabAppView::HostDisconnected(TInt /* aError */)
{
TRAPD(
error,
HBufC* buf = CEikonEnv::Static()->AllocReadResourceL(R_HOST_DISCONNECTED_TEXT);
CAknInformationNote* informationNote = new (ELeave) CAknInformationNote;
informationNote->ExecuteLD(buf->Des());
delete buf;
)
}
void CS60BTLabAppView::SendPositionsToClientL()
{
TBuf8<5> posData(5);
TPoint myBallPos = iMySprite->Position();
posData[0] = static_cast<TUint8>(myBallPos.iX);
posData[1] = static_cast<TUint8>(myBallPos.iY);
TPoint otherBallPos = iOtherSprite->Position();
posData[2] = static_cast<TUint8>(otherBallPos.iX);
posData[3] = static_cast<TUint8>(otherBallPos.iY);
posData[4] = static_cast<TUint8>(iOtherSprite->GetBounceCount());
TRAPD(
error,
iServer->WriteDataSyncL(posData);
UpdateScreen(); // update the screen
LogL(_L8("Reading key press..."));
iServer->ReadDataL();
)
if (KErrNotReady == error ||
KErrDisconnected == error)
{
ClientDisconnected(error);
}
if (KErrNone != error)
{
User::Leave(error);
}
}
void CS60BTLabAppView::SetSpritePositions(const TDesC8& aData)
{
TPoint otherBallPos;
otherBallPos.iX = aData[0];
otherBallPos.iY = aData[1];
TPoint myBallPos;
myBallPos.iX = aData[2];
myBallPos.iY = aData[3];
iMySprite->SetPosition(myBallPos);
iOtherSprite->SetPosition(otherBallPos);
iMySprite->SetBounceCount(aData[4]);
}
void CS60BTLabAppView::LogL(const TDesC8& aText)
{
LogL(aText, KNullDesC8);
}
void CS60BTLabAppView::LogL(const TDesC8& aText, TInt aNumber)
{
TBuf8<KMaxTIntLen> numberString;
numberString.Num(aNumber);
LogL(aText, numberString);
}
void CS60BTLabAppView::LogL(const TDesC8& /*aText*/, const TDesC8& /*aExtraText*/)
{
/*
HBufC8* buffer = HBufC8::NewLC(aText.Length() + aExtraText.Length() + KCRLF().Length());
buffer->Des().Append(aText);
buffer->Des().Append(aExtraText);
buffer->Des().Append(KCRLF);
User::LeaveIfError(iDebugLogFile.Write(*buffer));
iDebugLogFile.Flush();
CleanupStack::PopAndDestroy(buffer);
*/
}
// End of File
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -