// $Revision: 1.18 $ //Copyright (C) Taligent, Inc. All rights reserved. #ifndef TaligentSamples_INTERFACESUBSYSTEMT #include "InterfaceSubsystem.h" #endif #ifndef Taligent_GRAPHICIMAGE #include #endif #ifndef _TEXTDISPLAY_ #include #endif TGUIModelViewStationeryFor simulationStationery; //============================================================================== // TSimulationModel ModelDefinitionsMacroOne(TSimulationModel, kOriginalVersion, TGUIEmbedderModel); static const TGRect TSimulationModel_kViewExtent(0.0, 0.0, 300.0, 300.0); const TGRect& TSimulationModel::kViewExtent = TSimulationModel_kViewExtent; TSimulationModel::TSimulationModel() : TGUIEmbedderModel(), fSimulationLevel(), fSimulationState(kBetweenLevels), fConnection(this) { fConnection.Connect(); fConnection.AdoptInterest(fSimulationLevel.CreateLevelEndedInterest(), HandleLevelEnded); fConnection.AdoptInterest(fSimulationLevel.CreateRefreshInterest(), HandleImageChanged); } TSimulationModel::TSimulationModel(const TSimulationModel& source) : TGUIEmbedderModel(source), fSimulationLevel(source.fSimulationLevel), fSimulationState(kBetweenLevels), fConnection(this, HandleLevelEnded) { fConnection.Connect(); fConnection.AdoptInterest(fSimulationLevel.CreateLevelEndedInterest(), HandleLevelEnded); fConnection.AdoptInterest(fSimulationLevel.CreateRefreshInterest(), HandleImageChanged); } TSimulationModel::~TSimulationModel() { fSimulationLevel.Stop(); fConnection.RemoveAllInterests(); } TSimulationModel& TSimulationModel::operator=(const TSimulationModel& source) { if (&source != this) { TGUIEmbedderModel::operator=(source); fSimulationLevel = source.fSimulationLevel; } return *this; } TStream& TSimulationModel::operator>>=(TStream& toStream) const { WriteVersion(toStream); TGUIEmbedderModel::operator>>=(toStream); fSimulationLevel >>= toStream; long temp = fSimulationState; temp >>= toStream; return toStream; } TStream& TSimulationModel::operator<<=(TStream& fromStream) { VersionInfo info = ReadVersion(fromStream); switch (info) { case kOriginalVersion: { TGUIEmbedderModel::operator<<=(fromStream); fSimulationLevel <<= fromStream; long temp; temp <<= fromStream; fSimulationState = (ESimulationState)temp; } break; default: throw TInvalidVersionError(); } return fromStream; } bool TSimulationModel::IsEqual(const MCollectible* other) const { bool result = false; if (this == other) { result = true; } else if (!TGUIEmbedderModel::IsEqual(other)) { result = false; } else { result = true; } return result; } long TSimulationModel::Hash() const { return 0; } TModelSelection* TSimulationModel::CreateSelection() const { return new TSimulationSelection(*this); } const TImage* TSimulationModel::UseImage() const { return fSimulationLevel.UseImage(); } void TSimulationModel::DoneWithImage() const { fSimulationLevel.DoneWithImage(); } bool TSimulationModel::KeyDown(TKeyDownEvent& event) { return fSimulationLevel.KeyDown(event); } bool TSimulationModel::KeyUp(TKeyUpEvent& event) { return fSimulationLevel.KeyUp(event); } void TSimulationModel::StartSimulation() { if (fSimulationState == kPaused) { fSimulationLevel.Restart(); } else if (fSimulationState == kBetweenLevels) { static const long kNumSmallAsteroids = 0; static const long kNumMediumAsteroids = 0; static const long kNumLargeAtsteroids = 1; static const long kNumShips = 1; fSimulationLevel.Start(kViewExtent, kNumSmallAsteroids, kNumMediumAsteroids, kNumLargeAtsteroids, kNumShips); } fSimulationState = kRunning; } void TSimulationModel::StopSimulation() { if (fSimulationState == kRunning) { fSimulationLevel.Stop(); fSimulationState = kPaused; } } TModelInterest TSimulationModel::GetImageChangedInterest() const { static TToken kToken("TSimulationModel::GetImageChangedInterest"); return TModelInterest( *this, kToken ); } TSimulationLevel* TSimulationModel::CopySimulation() const { TSimulationLevel* result = new TSimulationLevel(fSimulationLevel); return result; } void TSimulationModel::AdoptSimulation(TSimulationLevel* newSimulation) { fSimulationLevel = *newSimulation; delete newSimulation; } TSimulationLevel* TSimulationModel::OrphanSimulation() { static const long kNumSmallAsteroids = 0; static const long kNumMediumAsteroids = 0; static const long kNumLargeAtsteroids = 1; static const long kNumShips = 1; if (fSimulationState == kRunning) { fSimulationLevel.Stop(); } TSimulationLevel* result = new TSimulationLevel(fSimulationLevel); if (fSimulationState == kRunning) { fSimulationLevel.Start(kViewExtent, kNumSmallAsteroids, kNumMediumAsteroids, kNumLargeAtsteroids, kNumShips); } return result; } void TSimulationModel::HandleImageChanged(const TNotification& note) { NotifyOfChange(TNotification(GetImageChangedInterest())); } void TSimulationModel::HandleLevelEnded(const TNotification& note) { TDocumentComponentWriteEntry entry(GetReference()); if (fSimulationState == kRunning) { static const long kNumSmallAsteroids = 0; static const long kNumMediumAsteroids = 0; static const long kNumLargeAtsteroids = 1; static const long kNumShips = 1; fSimulationLevel.Stop(); fSimulationLevel.Start(kViewExtent, kNumSmallAsteroids, kNumMediumAsteroids, kNumLargeAtsteroids, kNumShips); } } //============================================================================== // TSimulationSelection MCollectibleDefinitionsMacro(TSimulationSelection, kOriginalVersion); DynamicCastDefinitionsMacroOne(TSimulationSelection, TGUIEmbedderModelSelectionFor); TSimulationSelection::TSimulationSelection() : TGUIEmbedderModelSelectionFor() { } TSimulationSelection::TSimulationSelection(const TModelReference& model) : TGUIEmbedderModelSelectionFor(model) { } TSimulationSelection::TSimulationSelection(const TSimulationModel& theModel) : TGUIEmbedderModelSelectionFor(theModel) { } TSimulationSelection::TSimulationSelection(const TSimulationSelection& source) : TGUIEmbedderModelSelectionFor(source) { } TSimulationSelection::~TSimulationSelection() { } TSimulationSelection& TSimulationSelection::operator=(const TSimulationSelection& source) { if (&source != this) { TGUIEmbedderModelSelectionFor::operator=(source); } return *this; } TStream& TSimulationSelection::operator>>=(TStream& toStream) const { WriteVersion(toStream); TGUIEmbedderModelSelectionFor::operator>>=(toStream); return toStream; } TStream& TSimulationSelection::operator<<=(TStream& fromStream) { VersionInfo info = ReadVersion(fromStream); switch (info) { case kOriginalVersion: TGUIEmbedderModelSelectionFor::operator<<=(fromStream); break; default: throw TInvalidVersionError(); } return fromStream; } void TSimulationSelection::CopyDataIntoModelSubclass(TSimulationModel& destModel) const { const TSimulationModel* model = GetModelForReading(); destModel.AdoptSimulation(model->CopySimulation()); } void TSimulationSelection::MoveDataOutofModelSubclass(TSimulationModel& sourceModel) { TSimulationModel* model = GetModelForWriting(); model->AdoptSimulation(sourceModel.OrphanSimulation()); } void TSimulationSelection::MoveDataIntoModelSubclass(TSimulationModel& destModel) { TSimulationModel* model = GetModelForWriting(); destModel.AdoptSimulation(model->OrphanSimulation()); } //============================================================================== // TSimulationView MCollectibleDefinitionsMacro(TSimulationView, kOriginalVersion); TSimulationView::TSimulationView(TGUIBundle* bundle) : TGUIEmbedderModelView(bundle), MKeyEventHandler(), VViewInitialize(&gMetaInfo), fRefreshConnection(), fDrawSplashScreen(true) { CheckForInitialize(&gMetaInfo); } TSimulationView::~TSimulationView() { fRefreshConnection.RemoveAllInterests(); CheckForFinalize(&gMetaInfo); } TSimulationView::TSimulationView() : TGUIEmbedderModelView(), MKeyEventHandler(), VViewInitialize(&gMetaInfo), fRefreshConnection() { CheckForInitialize(&gMetaInfo); } TSimulationView::TSimulationView(const TSimulationView& source) : TGUIEmbedderModelView(source), MKeyEventHandler(), VViewInitialize(&gMetaInfo), fRefreshConnection() { CheckForInitialize(&gMetaInfo); } void TSimulationView::DrawContents(TGrafPort& port) const { TGUIEmbedderModelView::DrawContents(port); if (fDrawSplashScreen) { TTextDisplay message(TStandardText("Click here to start...")); static const TGPoint messageLocation(40.0, 40.0); message.SetOrigin(messageLocation); message.Draw(port); } else { TModelPointerTo model(GetModelReference()); const TImage* image = model->UseImage(); image->Draw(port); model->DoneWithImage(); } } void TSimulationView::HandleActivate() { TGUIEmbedderModelView::HandleActivate(); RegisterSimpleTarget(*(GetEventReceiver())); fDrawSplashScreen = false; TModelPointerTo model(GetModelReference()); model->StartSimulation(); } void TSimulationView::HandleDeactivate() { TModelPointerTo model(GetModelReference()); model->StopSimulation(); DeregisterSimpleTarget(*(GetEventReceiver())); TGUIEmbedderModelView::HandleDeactivate(); } void TSimulationView::HandleRefreshNeeded(const TNotification& noteIn) { //InvalidateAll(); DrawContents(*GetGrafPort()); } bool TSimulationView::KeyDown(TKeyDownEvent& event) { return TModelPointerTo(GetModelReference())->KeyDown(event); } bool TSimulationView::KeyUp(TKeyUpEvent& event) { return TModelPointerTo(GetModelReference())->KeyUp(event); } void TSimulationView::HandleAfterConnectionToViewRoot() { static const TGRect kViewExtent(0.0, 0.0, 300.0, 300.0); TGUIEmbedderModelView::HandleAfterConnectionToViewRoot(); SetAllocatedAreaInParent(kViewExtent); fRefreshConnection.SetReceiver(this); fRefreshConnection.SetRequestProcessor(GetRequestProcessor()); TModelPointerTo model(GetModelReference()); fRefreshConnection.AddInterest(model->GetImageChangedInterest(), HandleRefreshNeeded); fRefreshConnection.Connect(); fDrawSplashScreen = true; } void TSimulationView::HandleBeforeDisconnectionFromViewRoot() { fRefreshConnection.RemoveAllInterests(); fRefreshConnection.Disconnect(); TGUIEmbedderModelView::HandleBeforeDisconnectionFromViewRoot(); } TSimulationView& TSimulationView::operator=(const TSimulationView& source) { return *this; }