/* ======================================================================== *\ ! ! * ! * This file is part of CheObs, the Modular Analysis and Reconstruction ! * Software. It is distributed to you in the hope that it can be a useful ! * and timesaving tool in analysing Data of imaging Cerenkov telescopes. ! * It is distributed WITHOUT ANY WARRANTY. ! * ! * Permission to use, copy, modify and distribute this software and its ! * documentation for any purpose is hereby granted without fee, ! * provided that the above copyright notice appears in all copies and ! * that both that copyright notice and this permission notice appear ! * in supporting documentation. It is provided "as is" without express ! * or implied warranty. ! * ! ! ! Author(s): Thomas Bretz, 7/2002 ! ! Copyright: CheObs Software Development, 2000-2009 ! ! \* ======================================================================== */ ///////////////////////////////////////////////////////////////////////////// // // MagicSnake // ---------- // // Camera Display Games: Snake // // Start the game by: // MagicSnake snake; // // Controll the worm using right/left. Make sure, that the mouse pointer // is inside the canvas. // // Move the mouse pointer outside the canvas to pause the game. // // The pixel colors have the following meaning: // -------------------------------------------- // Green: Food, collect all packages. // Red: Bombs, don't touch it! // Yellow: Transport, try it. // Magenta: Home, touch it to win the game. // // To restart the game use the context menu. It can only be accessed if // the game has been stopped (either because you win the game or because // you hit a bomb) With the context menu you can also toggle between // different camera layouts. // //////////////////////////////////////////////////////////////////////////// #include "MagicSnake.h" #include #include #include #include #include #include #include #include "MH.h" #include "MGeomPix.h" #include "MGeomCamCT1.h" #include "MGeomCamMagic.h" ClassImp(MagicSnake); using namespace std; void MagicSnake::Free() { if (!fGeomCam) return; delete fGeomCam; delete fArray; } void MagicSnake::ChangeCamera() { if (!fDone) Done("Changing Camera...", 22); static Bool_t ct1=kFALSE; cout << "Change to " << (ct1?"Magic":"CT1") << endl; if (ct1) SetNewCamera(new MGeomCamMagic); else SetNewCamera(new MGeomCamCT1); ct1 = !ct1; Reset(); AppendPad(); } void MagicSnake::SetNewCamera(MGeomCam *geom) { Free(); // // Set new camera // fGeomCam = geom; // // create the hexagons of the display // fNumPixels = fGeomCam->GetNumPixels(); fRange = fGeomCam->GetMaxRadius(); fColors.Set(fNumPixels); } void MagicSnake::Init() { // // Make sure, that the object is destroyed when the canvas/pad is // destroyed. Make also sure, that the interpreter doesn't try to // delete it a second time. // SetBit(kCanDelete); gInterpreter->DeleteGlobal(this); Draw(); Pause(); fTimer.TurnOn(); } // ------------------------------------------------------------------------ // // default constructor // MagicSnake::MagicSnake() : fTimer(this, 500, kTRUE), fGeomCam(NULL), fDone(NULL), fPaused(NULL) { SetNewCamera(new MGeomCamMagic); Init(); } // ------------------------------------------------------------------------ // // default constructor // MagicSnake::MagicSnake(const MGeomCam &geom) : fTimer(this, 500, kTRUE), fGeomCam(NULL), fDone(NULL), fPaused(NULL) { SetNewCamera(static_cast(geom.Clone())); Init(); } void MagicSnake::Pause(Bool_t yes) { if (yes && !fPaused) { fPaused = new TText(0, 0, "Paused!"); fPaused->SetTextColor(kWhite); fPaused->SetTextAlign(22); fPaused->SetTextSize(0.05); // white fPaused->Draw(); #if ROOT_VERSION_CODE > ROOT_VERSION(3,01,06) fPaused->SetBit(kNoContextMenu|kCannotPick); ResetBit(kNoContextMenu); #endif //fDrawingPad->Update(); } if (yes) return; if (!fDone) { #if ROOT_VERSION_CODE > ROOT_VERSION(3,01,06) //fDrawingPad->SetBit(kNoContextMenu); SetBit(kNoContextMenu); #endif } if (!fPaused) return; //fDrawingPad->Update(); delete fPaused; fPaused=NULL; } // ------------------------------------------------------------------------ // // Destructor. Deletes TClonesArrays for hexagons and legend elements. // MagicSnake::~MagicSnake() { Free(); Pause(kFALSE); if (fDone) delete fDone; /* if (fDrawingPad->GetListOfPrimitives()->FindObject(this)==this) { fDrawingPad->RecursiveRemove(this); delete fDrawingPad; }*/ } // ------------------------------------------------------------------------ // // This is called at any time the canvas should get repainted. // Here we maintain an aspect ratio of 5/4=1.15. This makes sure, // that the camera image doesn't get distorted by resizing the canvas. // void MagicSnake::Paint(Option_t *opt) { const Float_t r = fGeomCam->GetMaxRadius(); MH::SetPadRange(-r, -r, r, r*1.1); TAttLine line; TAttFill fill; for (UInt_t i=0; iSetBorderMode(0); // // Append this object, so that the aspect ratio is maintained // (Paint-function is called) // AppendPad(option); // // Reset the game pad // Reset(); fShow.SetTextAlign(23); // centered/bottom #if ROOT_VERSION_CODE > ROOT_VERSION(3,01,06) fShow.SetBit(kNoContextMenu|kCannotPick); #endif fShow.Draw(); } void MagicSnake::Update() { TString txt = "Pixels: "; txt += fNumPixels; txt += " Bombs: "; txt += fNumBombs; txt += " Food: "; txt += fNumFood; fShow.SetText(0, fRange, txt); } // ------------------------------------------------------------------------ // // reset the all pixel colors to a default value // void MagicSnake::Reset() { fDirection = fGeomCam->InheritsFrom("MGeomCamCT1") ? kLeft : kRight; fLength = 2; for (UInt_t i=0; iGetColor doesn't allow more than 100 colors! if (fNumFood>46) fNumFood=46; fArray = new Int_t[fNumFood+3]; fArray[0] = 0; fArray[1] = 1; for (int i=0; iGetColor(51+i)->SetRGB(f, f, f); } for (int i=0; iSetFillColor(22); #if ROOT_VERSION_CODE > ROOT_VERSION(3,01,06) //fDrawingPad->SetBit(kNoContextMenu); SetBit(kNoContextMenu); #endif if (!fDone) return; delete fDone; fDone = NULL; } void MagicSnake::Done(TString txt, Int_t col) { //(*this)[fArray[fLength-1]].SetFillColor(kBlue); fDone = new TText(0, 0, txt); fDone->SetTextColor(kWhite); // white fDone->SetTextAlign(22); // centered/centered fDone->SetTextSize(0.05); // white fDone->Draw(); gPad->SetFillColor(col); #if ROOT_VERSION_CODE > ROOT_VERSION(3,01,06) fDone->SetBit(kNoContextMenu|kCannotPick); ResetBit(kNoContextMenu); #endif } // ------------------------------------------------------------------------ // // Execute a mouse event on the camera // void MagicSnake::ExecuteEvent(Int_t event, Int_t keycode, Int_t keysym) { if (event==kMouseEnter || event==kMouseMotion) { Pause(kFALSE); return; } if (fDone) return; if (event==kMouseLeave) { Pause(); return; } if (event!=kKeyPress) return; switch (keysym) { case kKey_Left: fDirection --; break; case kKey_Right: fDirection ++; break; case kKey_Escape: Done("Reset...", 22); Reset(); return; default: cout << "Keysym=0x" << hex << keysym << endl; } if (fDirection < kRightTop) fDirection = kLeftTop; if (fDirection > kLeftTop) fDirection = kRightTop; } void MagicSnake::Step(Int_t newpix) { if ((*fGeomCam)[newpix].TestBit(kHasTransport)) { fColors[fArray[0]] = kBackground; (*fGeomCam)[fArray[0]].ResetBit(kHasWorm); for (int i=1; i=dx && y>dy) { newpix=idx; dy=y; } continue; case kRight: if (x>dx) { newpix=idx; dx=x; } continue; case kRightBottom: if (x>=dx && ydy) { newpix=idx; dy=y; } continue; case kLeft: if (xModified(); gPad->Update(); return kTRUE; }