examples/SFExamples/OandXViewArch/S60/src/oandxappui.cpp

00001 /*
00002 Copyright (c) 2002-2011 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010   this list of conditions and the following disclaimer in the documentation
00011   and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013   may be used to endorse or promote products derived from this software
00014   without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description:  
00028 */ 
00029 
00030 #include <avkon.hrh>
00031 #include <aknnotewrappers.h>
00032 
00033 #include "OandXAppUi.h"
00034 #include "oandxcontroller.h"
00035 #include "oandxengine.h"
00036 #include "OandXApplication.h"
00037 #include "OandXAppView.h"
00038 #include "OandXHistView.h"
00039 #include "oandxdefs.h"
00040 #include "OandX.hrh"
00041 #include <OandX.rsg>
00042 
00043 #define MaxInfoNoteTextLen 40
00044 
00045 COandXAppUi::COandXAppUi()
00050         {
00051         // empty.
00052         }
00053 
00054 void COandXAppUi::ConstructL()
00060         {
00061         // for v1.x SDKs, use no-arg BaseConstructL() overload
00062         BaseConstructL(EAknEnableSkin);
00063         iEngine = COandXEngine::NewL();
00064         iController = COandXController::NewL();
00065         
00066         iHistView = COandXHistoryView::NewLC();
00067         AddViewL(iHistView);
00068         CleanupStack::Pop(); // history view
00069 
00070         iAppView = COandXGameView::NewLC();
00071         AddViewL(iAppView);
00072         CleanupStack::Pop(); // game view
00073         
00074     SetDefaultViewL(*iAppView);
00075    
00076         ReportWhoseTurn();
00077         }
00078 
00079 
00080 COandXAppUi::~COandXAppUi()
00086         {
00087         delete iController;
00088         delete iEngine;
00089         }
00090 
00091 void COandXAppUi::HandleCommandL(TInt aCommand)
00098         {
00099         _LIT8(KDummy, "");
00100         switch(aCommand)
00101                 {
00102         case EEikCmdExit:
00103         case EAknSoftkeyExit:
00104                 {
00105                 SaveL();
00106                 Exit();
00107                 }
00108                 break;
00109         case EOandXNewGame:
00110                 {
00111                 iController->Reset();
00112                 iAppView->Container()->ResetView();
00113                 }
00114                 break;
00115         case EOandXFirstPlayer:
00116                 {
00117                 iController->SwitchTurn();
00118                 }
00119                 break;
00120         case EOandXDisplayStats:
00121                 {
00122                 if (iHistView->IsActivated())
00123                         {
00124                         iHistView->ChangeDisplayL(EOandXSetStats);
00125                         }
00126                 else
00127                         {
00128                         ActivateViewL(TVwsViewId( KUidOandXApp, KUidOandXHistoryView), TUid::Uid(EHistoryViewDisplayStats), KDummy );
00129                         }
00130                 break;
00131                 }
00132         case EOandXDisplayHistory:
00133                 {
00134                 if (iHistView->IsActivated())
00135                         {
00136                         iHistView->ChangeDisplayL(EOandXSetHistory);
00137                         }
00138                 else
00139                         {
00140                         ActivateViewL(TVwsViewId(KUidOandXApp, KUidOandXHistoryView), TUid::Uid(EHistoryViewDisplayHistory), KDummy );
00141                         }
00142                 break;
00143                 }
00144         case EOandXDisplayGame:
00145                 {
00146                 ActivateViewL(TVwsViewId( KUidOandXApp, KUidOandXView)) ;
00147                 break;
00148                 }
00149         case EOandXResetHistory:
00150                 {
00151                 Controller().ResetStats();
00152                 iHistView->ChangeDisplayL(iHistView->IsDisplayingHistory());
00153                 break;
00154                 }
00155         default:
00156                 break;
00157                 }
00158         }
00159 
00160 void COandXAppUi::ReportWhoseTurn()
00164         {
00165         iAppView->Container()->ShowTurn();
00166         }
00167 
00168 void COandXAppUi::ReportWinnerL(TInt aWinner)
00173         {
00174         TBuf<MaxInfoNoteTextLen> text;
00175         iEikonEnv->ReadResource(text, aWinner==ETileCross ? R_OANDX_X_WINS : R_OANDX_O_WINS);
00176         CAknInformationNote* infoNote = new (ELeave) CAknInformationNote;
00177         infoNote->ExecuteLD(text);
00178         }
00179 
00180 TStreamId COandXAppUi::StoreL(CStreamStore& aStore) const
00189         {
00190         RStoreWriteStream stream;
00191         TStreamId id = stream.CreateLC(aStore);
00192         stream << *this; // alternatively, use ExternalizeL(stream)
00193         stream.CommitL();
00194         CleanupStack::PopAndDestroy();
00195         return id;
00196         }
00197 
00198 void COandXAppUi::RestoreL(const CStreamStore& aStore, TStreamId aStreamId)
00208         {
00209         RStoreReadStream stream;
00210         stream.OpenLC(aStore,aStreamId);
00211         stream >> *this; // alternatively use InternalizeL(stream)
00212         CleanupStack::PopAndDestroy();
00213         }
00214 
00215 void COandXAppUi::ExternalizeL(RWriteStream& aStream) const
00222         {
00223         iEngine->ExternalizeL(aStream);
00224         iController->ExternalizeL(aStream);
00225         aStream.WriteInt8L(iAppView->Container()->IdOfFocusControl());
00226         }
00227 
00228 void COandXAppUi::InternalizeL(RReadStream& aStream)
00235         {
00236         iEngine->InternalizeL(aStream);
00237         iController->InternalizeL(aStream);
00238         ReportWhoseTurn();
00239         iAppView->Container()->MoveFocusTo(aStream.ReadInt8L());
00240         }
00241 
00242 
00243 // Global accessor functions
00244 
00245 GLDEF_C COandXAppUi* OandXAppUi()
00250         {
00251         return static_cast<COandXAppUi*>(CEikonEnv::Static()->AppUi());
00252         }
00253 
00254 GLDEF_C COandXController& Controller()
00259         {
00260         return *OandXAppUi()->iController;
00261         }
00262 
00263 GLDEF_C COandXEngine& Engine()
00268         {
00269         return *OandXAppUi()->iEngine;
00270         }

Generated by  doxygen 1.6.2