Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

TridAnimator.cxx

Go to the documentation of this file.
00001 #include <sigc++/sigc++.h>
00002 #include <sigc++/slot.h>
00003 #include <sigc++/class_slot.h>
00004 #include <string>
00005  
00006 #include "MessageService/MsgService.h"
00007 
00008 #include "Midad/Gui/GuiMainWindow.h"
00009 #include "Midad/Gui/GuiButton.h"
00010 #include "Midad/Gui/GuiGroup.h"
00011 
00012 #include "TridAnimator.h"
00013 #include "TridControl.h"
00014 
00015 CVSID("$Id: TridAnimator.cxx,v 1.9 2006/06/04 20:37:40 tagg Exp $");
00016 ClassImp(TridAnimator)
00017 
00018 using namespace SigC; 
00019 
00020 TridAnimator::TridAnimator( TridControl& tridControl ):
00021   fTridControl(&tridControl),
00022   fAnimating(false),
00023   fAnimFPS(30),
00024   fPrinting(false),
00025   fFrame(0),
00026   fAnimateModels(true),
00027   fAnimateViews(false),
00028   fUseTimeWindow(false),
00029   fTimeRange(0,150),
00030   fAnimSecPerFrame(1e-6), // sec/sec
00031   fAnimTimeMin(-1e99),
00032   fAnimTimeMax( 1e99),
00033   fAnimTimeWindow(1e-6), // sec  
00034   fViewAnimSpeed(0.1)
00035 {
00036 }
00037 
00038 TridAnimator::TridAnimator( void ):
00039   fTridControl(NULL),
00040   fAnimating(false),
00041   fAnimFPS(10),
00042   fPrinting(false),
00043   fFrame(0),
00044   fAnimateModels(true),
00045   fAnimateViews(false),
00046   fUseTimeWindow(false),
00047   fTimeRange(0,150),
00048   fAnimSecPerFrame(1e-6),
00049   fAnimTimeMin(-1e99),
00050   fAnimTimeMax( 1e99),
00051   fAnimTimeWindow(1e-6), // ns  
00052   fViewAnimSpeed(0.1)
00053 {
00054 }
00055 
00056 TridAnimator::~TridAnimator( void )
00057 {
00058   CloseConfigFrame();
00059 }
00060 
00061 void TridAnimator::StartAnimation()
00062 {
00063   fAnimating = true;
00064   Long_t ms = (Long_t)(1000./fAnimFPS);
00065   if(AnimatingModels()) fAnimTimeMax = fTimeRange.Min();
00066   fAnimTimer.SetObject(this);
00067   fAnimTimer.Start(ms,true);
00068   fAnimTimer.TurnOn();
00069   fFrame = 0;
00070 
00071   MSG("TriD",Msg::kDebug) << "Starting anim." << endl;
00072   if(!fTridControl) MSG("TriD",Msg::kWarning) << "Animator has no control object???" << endl;
00073 }
00074 
00075 void TridAnimator::StopAnimation()
00076 {
00077   fAnimating = false;
00078 }
00079 
00080 Bool_t TridAnimator::HandleTimer( TTimer* )
00081 {
00082   if(fAnimating) {
00083     // Change the clock.
00084     fFrame++;
00085     if(fLabelAnimFrame.get()) {
00086       fLabelAnimFrame->SetText(Form("Frame: %d",GetFrame()));
00087       fLabelAnimFrame->Resize(fLabelAnimFrame->GetDefaultSize());
00088     }
00089 
00090     if(AnimatingModels()){
00091 
00092       fAnimTimeMax += fAnimSecPerFrame;
00093       if(fAnimTimeMax>fTimeRange.Max()) fAnimTimeMax = fTimeRange.Min();
00094 
00095       if(fUseTimeWindow) fAnimTimeMin = fAnimTimeMax - fAnimTimeWindow;
00096       else               fAnimTimeMin = -1e99;
00097 
00098       if(fLabelAnimTime.get()) {
00099         fLabelAnimTime->SetText(Form("Time: %5.2f ns to %5.2f ns",GetAnimTimeMin()/Munits::ns, GetAnimTimeMax()/Munits::ns));
00100         fLabelAnimTime->Resize(fLabelAnimTime->GetDefaultSize());
00101       }
00102       MSG("TriD",Msg::kDebug) << "Refresing anim at t="<< fAnimTimeMin << endl;      
00103     } else {
00104       MSG("TriD",Msg::kDebug) << "Refresing views only"<< endl;      
00105     }
00106     // Emit the update call.
00107     Long_t ms = (Long_t)(1000./fAnimFPS);  
00108     fAnimTimer.Start(ms,true);
00109     if(fTridControl) fTridControl->FireAnimationTrigger();
00110   } else {
00111     MSG("TriD",Msg::kDebug) << "Stopping animation." << endl;
00112     fAnimTimer.Stop();
00113     fAnimTimer.TurnOff();
00114     fAnimTimeMin = -1e99;
00115     fAnimTimeMax =  1e99;
00116     if(fTridControl) fTridControl->FireAnimationTrigger();  
00117   }
00118   return true;
00119 }
00120 
00121 
00123 // Gui stuff.
00124 
00125 void TridAnimator::OpenWindow() 
00126 {
00127   fMainWindow = manage(new GuiMainWindow(300,500));
00128   fMainWindow->SetWindowName("TriD Animation Control");
00129   fMainWindow->SetLayoutHints( kLHintsTop | kLHintsExpandX);
00130   GuiBox* vbox = manage(new GuiVBox(*fMainWindow));
00131   vbox->SetLayoutHints(vbox->GetLayoutHints() | kLHintsExpandX);
00132   fMainWindow->Add(*vbox);
00133   ShowConfigFrame(*vbox);
00134   fMainWindow->ShowAll();
00135   fMainWindow->ConnectClose();
00136   fMainWindow->Resize(fMainWindow->GetDefaultWidth(),fMainWindow->GetDefaultHeight());
00137   fMainWindow->close_window.connect(SigC::slot(*fMainWindow,&GuiMainWindow::KillMe));
00138   fMainWindow->close_window.connect(SigC::slot_class(*this,&TridAnimator::CloseConfigFrame));
00139 }
00140 
00141 
00142 void TridAnimator::ShowConfigFrame(GuiBox &inbox)
00143 {
00144   GuiBox& box = *(manage(new GuiBox(inbox,kVerticalFrame)));
00145   inbox.Add(box);
00146 
00147   fButtonStartStop = SigC::manage(new GuiTextButton(box,(fAnimating ? "Stop Animation" : "Start Animation")));
00148   fButtonStartStop->SetLayoutHints(kLHintsCenterX);
00149   box.Add(*fButtonStartStop);  
00150 
00151 
00152   fLabelAnimTime = SigC::manage(new GuiLabel(box,Form("Time: %5.2g ns",fAnimTimeMin)));
00153   fLabelAnimTime->SetLayoutHints(kLHintsExpandX|kLHintsCenterX|kLHintsTop);
00154   fLabelAnimTime->Resize(fLabelAnimTime->GetDefaultSize());
00155   box.Add(*fLabelAnimTime);
00156 
00157   fLabelAnimFrame = SigC::manage(new GuiLabel(box,Form("Frame: %d",GetFrame())));
00158   fLabelAnimFrame->SetLayoutHints(kLHintsExpandX|kLHintsCenterX|kLHintsTop);
00159   fLabelAnimFrame->Resize(fLabelAnimFrame->GetDefaultSize());
00160   box.Add(*fLabelAnimFrame);
00161 
00162   fButtonPrinting  = SigC::manage(new GuiCheckButton(box,"Print Animation:"));
00163   fButtonPrinting->SetLayoutHints(kLHintsLeft|kLHintsTop);
00164   box.Add(*fButtonPrinting);
00165   
00166   {
00167     GuiBox* hbox = manage(new GuiBox(box,kHorizontalFrame));
00168     hbox->SetLayoutHints(kLHintsTop | kLHintsRight);
00169     box.Add(*hbox);
00170       
00171     GuiLabel* glabel = manage(new GuiLabel(*hbox,"Display Rate (fps):"));
00172     hbox->Add(*glabel);
00173     
00174     fTextEntryFPS = SigC::manage(new GuiTextEntry(*hbox,Form("%.1f",fAnimFPS)));
00175     hbox->Add(*fTextEntryFPS);
00176   }
00177     
00178   fButtonAnimateViews  = SigC::manage(new GuiCheckButton(box,"Animate View Angle"));
00179   fButtonAnimateViews->SetLayoutHints(kLHintsLeft|kLHintsTop);
00180   box.Add(*fButtonAnimateViews);
00181 
00182   GuiGroup* viewGroup = SigC::manage(new GuiGroup(box,"View Animation"));
00183   viewGroup->SetLayoutHints(kLHintsTop|kLHintsExpandX);
00184   box.Add(*viewGroup);
00185   {
00186     GuiBox* hbox = manage(new GuiBox(*viewGroup,kHorizontalFrame));
00187     hbox->SetLayoutHints(kLHintsTop | kLHintsRight);
00188     viewGroup->Add(*hbox);
00189     
00190     GuiLabel* glabel = manage(new GuiLabel(*hbox,"View Rotation Rev/frame:"));
00191     hbox->Add(*glabel);
00192 
00193     fTextEntryViewSpeed = SigC::manage(new GuiTextEntry(*hbox,Form("%.3f",fViewAnimSpeed)));
00194     hbox->Add(*fTextEntryViewSpeed);
00195   }
00196   viewGroup->Layout();
00197   viewGroup->Resize(viewGroup->GetDefaultSize());
00198 
00199 
00200   fButtonAnimateModels = SigC::manage(new GuiCheckButton(box,"Animate Event"));
00201   fButtonAnimateModels->SetLayoutHints(kLHintsLeft|kLHintsTop);
00202   box.Add(*fButtonAnimateModels);
00203 
00204   GuiGroup* eventGroup = SigC::manage(new GuiGroup(box,"Event Animation"));
00205   box.Add(*eventGroup);
00206 
00207   fButtonUseTimeWindow = SigC::manage(new GuiCheckButton(*eventGroup,"Use Time Window"));
00208   eventGroup->SetLayoutHints(kLHintsTop|kLHintsExpandX);  
00209   eventGroup->Add(*fButtonUseTimeWindow);
00210   
00211   {
00212     GuiBox* hbox = manage(new GuiBox(*eventGroup,kHorizontalFrame));
00213     hbox->SetLayoutHints(kLHintsTop | kLHintsRight);
00214     eventGroup->Add(*hbox);
00215     
00216     GuiLabel* glabel = manage(new GuiLabel(*hbox,"Time Window (ns):"));
00217     hbox->Add(*glabel);
00218 
00219     fTextEntryTimeWindow = SigC::manage(new GuiTextEntry(*hbox,Form("%.1f",fAnimTimeWindow)));
00220     hbox->Add(*fTextEntryTimeWindow);
00221   }
00222   
00223   {
00224     GuiBox* hbox = manage(new GuiBox(*eventGroup,kHorizontalFrame));
00225     eventGroup->Add(*hbox);
00226     
00227     GuiLabel* glabel = manage(new GuiLabel(*hbox,"From:"));
00228     glabel->SetLayoutHints(kLHintsTop);
00229     hbox->Add(*glabel);
00230 
00231     fTextEntryTimeRangeStart = SigC::manage(new GuiTextEntry(*hbox,Form("%.1f",fTimeRange.Min())));
00232     hbox->Add(*fTextEntryTimeRangeStart);
00233 
00234     GuiLabel* glabel2 = manage(new GuiLabel(*hbox,"To:"));
00235     glabel2->SetLayoutHints(kLHintsTop);
00236     hbox->Add(*glabel2);
00237 
00238     fTextEntryTimeRangeEnd = SigC::manage(new GuiTextEntry(*hbox,Form("%.1f",fTimeRange.Max())));
00239     hbox->Add(*fTextEntryTimeRangeEnd);
00240 
00241     hbox->Layout();
00242   }
00243   
00244   {
00245     GuiBox* hbox = manage(new GuiBox(*eventGroup,kHorizontalFrame));
00246     hbox->SetLayoutHints(kLHintsTop | kLHintsRight);
00247     eventGroup->Add(*hbox);
00248     
00249     GuiLabel* glabel = manage(new GuiLabel(*hbox,"Event Speed (ns/frame):"));
00250     hbox->Add(*glabel);
00251 
00252     fTextEntrySecPerFrame = SigC::manage(new GuiTextEntry(*hbox,Form("%.1f",fAnimSecPerFrame)));
00253     hbox->Add(*fTextEntrySecPerFrame);
00254    
00255     hbox->Layout();
00256   }
00257   eventGroup->Layout();
00258   eventGroup->Resize(eventGroup->GetDefaultSize());
00259 
00260   box.Layout();
00261 
00262   // Setup hit signals.
00263   fButtonStartStop   ->clicked.connect(slot_class(*this,&TridAnimator::StartStopHandler));
00264 
00265   fButtonPrinting     ->clicked.connect(slot_class(*this,&TridAnimator::ParamChangedHandler));
00266   fButtonAnimateViews ->clicked.connect(slot_class(*this,&TridAnimator::ParamChangedHandler));
00267   fButtonAnimateModels->clicked.connect(slot_class(*this,&TridAnimator::ParamChangedHandler));
00268   fButtonUseTimeWindow->clicked.connect(slot_class(*this,&TridAnimator::ParamChangedHandler));
00269   fTextEntryFPS           ->activated.connect(slot_class(*this,&TridAnimator::ParamChangedHandler));
00270   fTextEntryViewSpeed     ->activated.connect(slot_class(*this,&TridAnimator::ParamChangedHandler));
00271   fTextEntryTimeWindow    ->activated.connect(slot_class(*this,&TridAnimator::ParamChangedHandler));
00272   fTextEntryTimeRangeStart->activated.connect(slot_class(*this,&TridAnimator::ParamChangedHandler)); 
00273   fTextEntryTimeRangeEnd  ->activated.connect(slot_class(*this,&TridAnimator::ParamChangedHandler));
00274   fTextEntrySecPerFrame ->activated.connect(slot_class(*this,&TridAnimator::ParamChangedHandler));
00275   
00276   UpdateGui();
00277 }
00278 
00279 void TridAnimator::CloseConfigFrame()
00280 {
00281   fMainWindow = 0;
00282   fButtonStartStop = 0;
00283   fLabelAnimTime = 0;
00284   fLabelAnimFrame = 0;
00285   fButtonPrinting = 0;
00286   fButtonAnimateViews = 0;
00287   fButtonAnimateModels = 0;
00288   fButtonUseTimeWindow = 0;
00289   fTextEntryFPS = 0;
00290   fTextEntryViewSpeed = 0;
00291   fTextEntryTimeWindow = 0;
00292   fTextEntryTimeRangeStart = 0;
00293   fTextEntryTimeRangeEnd = 0;
00294   fTextEntrySecPerFrame = 0; 
00295 }
00296 
00297 
00298 double GetValueFromString( const char* string, double deFault = 0 ) {
00299   double value = deFault;
00300   int res = sscanf(string,"%lf",&value);
00301   if(res<=0) return deFault;
00302   return value;
00303 }
00304 
00305 void TridAnimator::StartStopHandler()
00306 {
00307   fAnimating = (!fAnimating);
00308 
00309   ReadFromGui(); // Just to make sure.
00310 
00311   if(fAnimating) {
00312     StartAnimation();
00313   } else {
00314     StopAnimation();
00315   }
00316   
00317   TridAnimator::UpdateGui();
00318 }
00319 
00320 
00321 void TridAnimator::ParamChangedHandler()
00322 {
00323   ReadFromGui();
00324 }
00325 
00326 
00327 void TridAnimator::ReadFromGui()
00328 {
00329   // Assume that if one widget exists, they all exist.
00330   if(fButtonStartStop.get())   {
00331     fAnimFPS = GetValueFromString(fTextEntryFPS->GetText(),fAnimFPS);
00332     fTimeRange.Set(GetValueFromString(fTextEntryTimeRangeStart->GetText(),fTimeRange.Min()),
00333                    GetValueFromString(fTextEntryTimeRangeEnd  ->GetText(),fTimeRange.Max())
00334                    );
00335                    
00336     fAnimSecPerFrame = GetValueFromString(fTextEntrySecPerFrame->GetText(),fAnimSecPerFrame);
00337     fAnimTimeWindow    = GetValueFromString(fTextEntryTimeWindow->GetText(), fAnimTimeWindow );
00338     fViewAnimSpeed     = GetValueFromString(fTextEntryViewSpeed->GetText(), fViewAnimSpeed);
00339 
00340     // Convert:
00341     fTimeRange.Set(fTimeRange.Min()*Munits::ns,
00342                    fTimeRange.Max()*Munits::ns);
00343     fAnimSecPerFrame*=Munits::ns;
00344     fAnimTimeWindow*=Munits::ns;
00345     
00346     fAnimateModels = (fButtonAnimateModels->GetState() == kButtonDown);
00347     fPrinting      = (fButtonPrinting->GetState() == kButtonDown);
00348     fAnimateViews  = (fButtonAnimateViews->GetState() == kButtonDown);
00349     fUseTimeWindow = (fButtonUseTimeWindow->GetState() == kButtonDown);
00350   }
00351 }
00352 
00353 void TridAnimator::UpdateGui()
00354 {
00355   // Assume that if one widget exists, they all exist.
00356   if(fButtonStartStop.get())   {
00357     if(fAnimating) fButtonStartStop->SetText(TString("Stop Animation"));
00358     else  fButtonStartStop->SetText(TString("Start Animation"));
00359     
00360     fButtonStartStop->Resize(fButtonStartStop->GetDefaultSize());
00361 
00362     fLabelAnimTime->SetText(Form("Time: %7.2g ns",fAnimTimeMin/Munits::ns));
00363     fLabelAnimTime->Resize(fLabelAnimTime->GetDefaultSize());
00364     fLabelAnimFrame->SetText(Form("Frame: %d",GetFrame()));
00365     fLabelAnimFrame->Resize(fLabelAnimFrame->GetDefaultSize());
00366   
00367     fTextEntryFPS->SetText(Form("%.1f",fAnimFPS));
00368 
00369     fButtonPrinting    ->SetState(fPrinting     ? kButtonDown : kButtonUp );
00370     fButtonAnimateViews->SetState(fAnimateViews ? kButtonDown : kButtonUp );
00371 
00372     fTextEntryViewSpeed->SetText(Form("%.3f",fViewAnimSpeed));
00373 
00374     fButtonAnimateModels->SetState(fAnimateModels ? kButtonDown : kButtonUp );
00375 
00376     fTextEntryTimeWindow->SetText(Form("%.1f",fAnimTimeWindow/Munits::ns));
00377   
00378     fTextEntryTimeRangeStart->SetText(Form("%.1f",fTimeRange.Min()/Munits::ns));
00379     fTextEntryTimeRangeEnd->  SetText(Form("%.1f",fTimeRange.Max()/Munits::ns));
00380     
00381     fTextEntrySecPerFrame->SetText(Form("%.1f",fAnimSecPerFrame/Munits::ns));    
00382   }
00383 }

Generated on Sat Nov 21 22:47:56 2009 for loon by  doxygen 1.3.9.1