// $Revision: 1.2 $ // Copyright (C) 1994, 1995 Taligent, Inc. All rights reserved. // TilesView.C #ifndef TaligentSample_TILESVIEW #include "TilesView.h" #endif #ifndef TaligentSample_TILESMODEL #include "TilesModel.h" #endif // This macro call matches a macro call to VersionDeclarationsMacro // in the TTilesView class declaration. This macro defines the // versioning utility functions. VersionDefinitionsMacro(TTilesView, kOriginalVersion); // This is how the view is actually constructed, with a TGUIBundle*. TTilesView::TTilesView(TGUIBundle* bundle) : TDocumentComponentView (bundle) {} // Default constructor TTilesView::TTilesView() : TDocumentComponentView() {} // Copy constructor TTilesView::TTilesView(const TTilesView& copy) : TDocumentComponentView(copy) {} // Destructor TTilesView::~TTilesView() {} // HandleAfterConnectionToViewRoot is called by the Framework when the view is added // to the view hierachy. This usually happens just before the view is made // visible. Initialization routines are usually added here. void TTilesView::HandleAfterConnectionToViewRoot() { // Call the base class version TDocumentComponentView::HandleAfterConnectionToViewRoot (); // Build a map between the TTilesModel's tile types (enums) and the // actual graphics used to render them in the view. BuildTilesGraphicsMapToModel(); // Set default view size SetAllocatedArea (TGArea(TGRect(TGPoint(0,0), TGPoint(200, 100)))); } // HandleBeforeDisconnectionFromViewRoot is called by the Framework when the view is removed // to the view hierachy. This usually happens just before the view is destroyed. // Cleanup routines are usually added here. void TTilesView::HandleBeforeDisconnectionFromViewRoot() { // call the base class version TDocumentComponentView::HandleBeforeDisconnectionFromViewRoot(); DestroyTilesGraphicsMapToModel(); } // Draw contents renders the data from the model. This is the primary method // of a content view. The TGrafPort is already clipped to the region which needs // to be redrawn, so DrawContents should re-render the entire view and rely on // the port to do any necessary clipping. void TTilesView::DrawContents(TGrafPort& port) const { // Create a smart pointer which will lock the model for reading, // and will release the lock on destruction of the pointer. const TModelPointerTo model(GetModelReference()); TGArea area; GetBounds(area); // Fill the background with white TGrafBundle whitebg (new TColorPaint(TRGBColor(.9,1,1)), TGrafBundle::kFill); port.Draw (area, whitebg); // Get each tile and draw it according to its type, color position and size. int numTiles = model->GetNumTiles(); for (int i = 0; i < numTiles; i++) { const TTile* tile = model->GetTileForReading(i); DrawTile(port, tile); } } // Draw an individual TTile into the port. void TTilesView::DrawTile (TGrafPort& port, const TTile* tile) const { // Create a graph bundle specifying color fill properties. TGrafBundle* bundle = new TGrafBundle (new TColorPaint(tile->GetColor()), new TColorPaint(TColorPaint::GetBlack()), TGrafBundle::kFill); // Copy the MGraphic from the table created by BuildTilesGraphicsMapToModel // Use ::Copy to get a polymorphic copy. MGraphic* tileGraphic = ::CopyPointer(fTilesGraphics[tile->GetType()]); // Scale and translate the graphic tileGraphic->ScaleBy(tile->GetSize()); tileGraphic->TranslateBy(tile->GetPosition()); // Have graphic adopt the drawing attributes tileGraphic->AdoptBundle(bundle); tileGraphic->Draw(port); delete tileGraphic; } // Build an array of MGraphics which maps the data in the model (kRock, // kPaper, kScissors) to drawable objects for use by DrawTile. void TTilesView::BuildTilesGraphicsMapToModel () { // Create a circle to represent the kRock, normalized to size 1.0 TEllipse* circle = new TEllipse(TGRect(TGPoint::kOrigin, TGPoint(1.0, 1.0))); // Create a square to represent the kPaper, normalized to size 1.0 TPolygon* square = new TPolygon(TGRect(TGPoint(0.0, 0.0), TGPoint(1.0, 1.0))); // Create a Triangle to represent the kScissors, normalized to size 1.0 TGPointArray pointArray (3); pointArray.SetPoint(0, TGPoint(0.5, 0.0)); pointArray.SetPoint(1, TGPoint(1.0, 1.0)); pointArray.SetPoint(2, TGPoint(0.0, 1.0)); TPolygon* triangle = new TPolygon(TGPolygon(pointArray)); // Place the MGraphics in the array fTilesGraphics[TTile::kRock] = circle; fTilesGraphics[TTile::kPaper] = square; fTilesGraphics[TTile::kScissors] = triangle; } void TTilesView::DestroyTilesGraphicsMapToModel () { delete fTilesGraphics[TTile::kRock]; delete fTilesGraphics[TTile::kPaper]; delete fTilesGraphics[TTile::kScissors]; } // Use static to force instantiation of a class template from the template class. // Future C++ syntax will support a template keyword for doing this. static TGUIModelViewStationeryFor theStationery;