CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_13_patch3/src/Fireworks/Core/src/CmsAnnotation.cc

Go to the documentation of this file.
00001 #include <sstream>
00002 
00003 #include "TGLIncludes.h"
00004 #include "TGLCamera.h"
00005 #include "TGLRnrCtx.h"
00006 #include "TGLSelectRecord.h"
00007 #include "TGLViewerBase.h"
00008 #include "TGLViewer.h"
00009 #include "TImage.h"
00010 #include "TEveManager.h"
00011 
00012 #include "Fireworks/Core/interface/CmsAnnotation.h"
00013 #include "Fireworks/Core/src/FWCheckBoxIcon.h"
00014 #include "Fireworks/Core/interface/FWConfiguration.h"
00015 
00016 CmsAnnotation::CmsAnnotation(TGLViewerBase *parent, Float_t posx, Float_t posy) :
00017    TGLOverlayElement(TGLOverlayElement::kUser),
00018    fPosX(posx), fPosY(posy),
00019    fMouseX(0),  fMouseY(0),
00020    fDrag(kNone),
00021    fParent(0),
00022    fSize(0.2),
00023    fSizeDrag(0.0),
00024    fActive(false),
00025    fAllowDestroy(false)
00026 {
00027    // Constructor.
00028    // Create annotation as plain text
00029 
00030    parent->AddOverlayElement(this);
00031    fParent = (TGLViewer*)parent;
00032 }
00033 
00034 
00035 CmsAnnotation::~CmsAnnotation()
00036 {
00037    // Destructor.
00038 
00039    fParent->RemoveOverlayElement(this);
00040 }
00041 
00042 
00043 void
00044 CmsAnnotation::Render(TGLRnrCtx& rnrCtx)
00045 { 
00046    if (rnrCtx.GetCamera()->RefViewport().Width() == 0 || 
00047        rnrCtx.GetCamera()->RefViewport().Height() == 0 ) return;
00048    
00049    static UInt_t ttid_black = 0;
00050    static UInt_t ttid_white = 0;
00051      
00052    bool whiteBg = rnrCtx.ColorSet().Background().GetColorIndex() == kWhite;
00053    UInt_t& ttid = whiteBg ? ttid_white : ttid_black;
00054 
00055    if ( (whiteBg == false && ttid == 0) || (whiteBg && ttid == 0))
00056    {
00057       TImage* imgs[3];
00058       TString base =   whiteBg ? "White" : "Black";  
00059       imgs[0] = TImage::Open(FWCheckBoxIcon::coreIcondir()+"CMSLogo" + base + "Bg.png");
00060       imgs[1] = TImage::Open(FWCheckBoxIcon::coreIcondir()+"CMSLogo" + base + "BgM.png");
00061       imgs[2] = TImage::Open(FWCheckBoxIcon::coreIcondir()+"CMSLogo" + base + "BgS.png");
00062 
00063       glGenTextures(1, &ttid);
00064       glBindTexture(GL_TEXTURE_2D, ttid);
00065       
00066       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
00067       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
00068       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00069       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
00070       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL,0);
00071       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 2);
00072       
00073       
00074       glPixelStorei(GL_UNPACK_ALIGNMENT,  1);
00075       glPixelStorei(GL_UNPACK_SWAP_BYTES, 1);
00076       
00077       for (int i=0; i < 3; i++)
00078          glTexImage2D(GL_TEXTURE_2D, i, GL_RGBA, imgs[i]->GetWidth(), imgs[i]->GetHeight(), 0,
00079                       GL_BGRA, GL_UNSIGNED_BYTE, imgs[i]->GetArgbArray()); 
00080       
00081       glPixelStorei(GL_UNPACK_SWAP_BYTES, 0);
00082      
00083       for (int i=0; i < 3; i++)
00084          delete imgs[i];
00085    }
00086 
00087    glPushAttrib(GL_ENABLE_BIT | GL_LINE_BIT | GL_POLYGON_BIT );
00088    TGLCapabilitySwitch lights_off(GL_LIGHTING, kFALSE);
00089 
00090 
00091    // reset matrix
00092    glMatrixMode(GL_PROJECTION);
00093    glPushMatrix();
00094    glLoadIdentity();
00095 
00096    if (rnrCtx.Selection())
00097    {
00098       TGLRect rect(*rnrCtx.GetPickRectangle());
00099       rnrCtx.GetCamera()->WindowToViewport(rect);
00100       gluPickMatrix(rect.X(), rect.Y(), rect.Width(), rect.Height(),
00101                     (Int_t*) rnrCtx.GetCamera()->RefViewport().CArr());
00102    }
00103    const TGLRect& vp = rnrCtx.RefCamera().RefViewport();
00104    glOrtho(vp.X(), vp.Width(), vp.Y(), vp.Height(), 0, 1);
00105    glMatrixMode(GL_MODELVIEW);
00106    glPushMatrix();
00107    glLoadIdentity();
00108 
00109    // move to pos
00110    Float_t posX = vp.Width()  * fPosX;
00111    Float_t posY = vp.Height() * fPosY;
00112    glTranslatef(posX, posY, -0.99);
00113 
00114    glDisable(GL_DEPTH_TEST);
00115    glEnable(GL_BLEND);
00116    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00117 
00118    glEnable(GL_TEXTURE_2D);
00119    glBindTexture(GL_TEXTURE_2D, ttid);
00120 
00121    glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
00122 
00123    // logo
00124    glPushName(kMove);
00125    TGLUtil::Color(rnrCtx.ColorSet().Background().GetColorIndex());
00126    glBegin(GL_QUADS);
00127    Float_t z =  0.9;
00128    Float_t a = fSize * vp.Height();
00129    glTexCoord2f(0, 1); glVertex3f( 0, -a, z);
00130    glTexCoord2f(1, 1); glVertex3f( a, -a, z);
00131    glTexCoord2f(1, 0); glVertex3f( a,  0, z);
00132    glTexCoord2f(0, 0); glVertex3f( 0,  0, z);  
00133    glEnd();
00134    glPopName();
00135 
00136 
00137    glDisable(GL_TEXTURE_2D);
00138 
00139    if (fActive)
00140    {
00141       // resize button
00142       glPushMatrix();
00143       glBegin(GL_QUADS);
00144       Float_t a = fSize * vp.Height();
00145       TGLUtil::ColorTransparency(rnrCtx.ColorSet().Markup().GetColorIndex(), 95);
00146       glTexCoord2f(0, 1); glVertex3f( 0, -a, z);
00147       glTexCoord2f(1, 1); glVertex3f( a, -a, z);
00148       glTexCoord2f(1, 0); glVertex3f( a,  0, z);
00149       glTexCoord2f(0, 0); glVertex3f( 0,  0, z);  
00150       glEnd();
00151 
00152       glTranslatef(a, -a, 0);
00153       a *= 0.2;
00154       z = 0.95;
00155       glPushName(kResize);
00156       TGLUtil::ColorTransparency(rnrCtx.ColorSet().Markup().GetColorIndex(), 100);
00157       glBegin(GL_QUADS);
00158       glVertex3f( 0, 0, z);
00159       glVertex3f( 0, a, z);
00160       glVertex3f(-a, a, z);
00161       glVertex3f(-a, 0, z);
00162       glEnd();
00163       {
00164          glTranslatef(-a/3, a/3, 0);
00165          glBegin(GL_LINES);
00166          TGLUtil::ColorTransparency(rnrCtx.ColorSet().Markup().GetColorIndex(), 40);
00167          Float_t s = a / 3;
00168          glVertex3f( 0, 0, z); glVertex3f( 0, s, z);
00169          glVertex3f( 0, 0, z); glVertex3f( -s, 0, z);
00170          glEnd();
00171       }
00172       glPopName();
00173       glPopMatrix();
00174 
00175       // delete
00176       if (fAllowDestroy)
00177       {
00178          glPushName(7);
00179          TGLUtil::ColorTransparency(rnrCtx.ColorSet().Markup().GetColorIndex(), 100);
00180          glTranslatef(0, -a, 0);
00181          glBegin(GL_QUADS);
00182          glVertex3f( 0, 0, z);
00183          glVertex3f( a, 0, z);
00184          glVertex3f( a, a, z);
00185          glVertex3f( 0, a, z);
00186          glEnd();
00187          {
00188             glBegin(GL_LINES);
00189             TGLUtil::ColorTransparency(rnrCtx.ColorSet().Markup().GetColorIndex(), 40);
00190             Float_t s = a/3;
00191             glVertex3f( s, s, z);
00192             glVertex3f( a-s, a-s, z);
00193             glVertex3f( s, a-s, z);
00194             glVertex3f( a-s, s, z);
00195             glEnd();
00196          }
00197          glPopName();   
00198       }
00199    }
00200 
00201    glEnable(GL_DEPTH_TEST);
00202    glMatrixMode(GL_PROJECTION);
00203    glPopMatrix();
00204    glMatrixMode(GL_MODELVIEW);
00205    glPopMatrix();
00206 
00207    glPopAttrib();
00208 }
00209 
00210 
00211 Bool_t CmsAnnotation::Handle(TGLRnrCtx&          rnrCtx,
00212                              TGLOvlSelectRecord& selRec,
00213                              Event_t*            event)
00214 {
00215    // Handle overlay event.
00216    // Return TRUE if event was handled.
00217 
00218    if (selRec.GetN() < 2) return kFALSE;
00219    int recID = selRec.GetItem(1);
00220 
00221    switch (event->fType)
00222    {
00223       case kButtonPress:
00224       {
00225          fMouseX = event->fX;
00226          fMouseY = event->fY;
00227          fDrag = (recID == kResize) ? kResize : kMove;
00228          fSizeDrag = fSize;
00229          return kTRUE;
00230       }
00231       case kButtonRelease:
00232       {
00233          fDrag = kNone;
00234          if (recID == 7)
00235          {
00236             fParent->RequestDraw(rnrCtx.ViewerLOD());
00237             delete this;
00238             return kTRUE;
00239          }
00240          break;
00241       }
00242       case kMotionNotify:
00243       {
00244          const TGLRect& vp = rnrCtx.RefCamera().RefViewport();
00245          if (vp.Width() == 0 || vp.Height() == 0) return false;
00246          if (fDrag != kNone)
00247          {
00248             if (fDrag == kMove)
00249             {
00250                fPosX += (Float_t)(event->fX - fMouseX) / vp.Width();
00251                fPosY -= (Float_t)(event->fY - fMouseY) / vp.Height();
00252                fMouseX = event->fX;
00253                fMouseY = event->fY;
00254 
00255                Float_t h = fSize;
00256                Float_t w = fSize/vp.Aspect();
00257 
00258                // Make sure we don't go offscreen (use fDraw variables set in draw)
00259                if (fPosX < 0)
00260                   fPosX = 0;
00261                else if (fPosX +w > 1.0f)
00262                   fPosX = 1.0f - w;
00263                if (fPosY < h)
00264                   fPosY = h;
00265                else if (fPosY > 1.0f)
00266                   fPosY = 1.0f;
00267             }
00268             else
00269             {
00270                using namespace TMath;
00271                Float_t oovpw = 1.0f / vp.Width(), oovph = 1.0f / vp.Height();
00272                
00273                Float_t xw = oovpw * Min(Max(0, event->fX), vp.Width());
00274                Float_t yw = oovph * Min(Max(0, vp.Height() - event->fY), vp.Height());
00275                
00276                Float_t rx = Max((xw - fPosX) / (oovpw * fMouseX - fPosX), 0.0f);
00277                Float_t ry = Max((yw - fPosY) / (oovph*(vp.Height() - fMouseY) - fPosY), 0.0f);
00278                
00279                fSize  = Max(fSizeDrag * Min(rx, ry), 0.01f);               
00280             }
00281          }
00282          return kTRUE;
00283       }
00284       default:
00285       {
00286          return kFALSE;
00287       }
00288    }
00289 
00290    return false;
00291 }
00292 
00293 //______________________________________________________________________________
00294 Bool_t CmsAnnotation::MouseEnter(TGLOvlSelectRecord& /*rec*/)
00295 {
00296    // Mouse has entered overlay area.
00297 
00298    fActive = kTRUE;
00299    return kTRUE;
00300 }
00301 
00302 //______________________________________________________________________
00303 void CmsAnnotation::MouseLeave()
00304 {
00305    // Mouse has left overlay area.
00306 
00307    fActive = kFALSE;
00308 }
00309 
00310 
00311 //______________________________________________________________________
00312 bool CmsAnnotation::getVisible() const
00313 {
00314    return GetState() == TGLOverlayElement::kActive;
00315 }
00316 
00317 //______________________________________________________________________
00318 void CmsAnnotation::setVisible(Bool_t x)
00319 {
00320    SetState(x ? (TGLOverlayElement::kActive) : (TGLOverlayElement::kInvisible));
00321    fParent->Changed();
00322    gEve->Redraw3D();
00323 }
00324 
00325 //______________________________________________________________________________
00326 
00327 void
00328 CmsAnnotation::addTo(FWConfiguration& iTo) const
00329 {
00330    std::stringstream s;
00331    s<<fSize;
00332    iTo.addKeyValue("LogoSize",FWConfiguration(s.str()));
00333    
00334    std::stringstream x;
00335    x<<fPosX;
00336    iTo.addKeyValue("LogoPosX",FWConfiguration(x.str()));
00337    
00338    std::stringstream y;
00339    y<<fPosY;
00340    iTo.addKeyValue("LogoPosY",FWConfiguration(y.str()));
00341 }
00342 
00343 void
00344 CmsAnnotation::setFrom(const FWConfiguration& iFrom) 
00345 {
00346    const FWConfiguration* value;
00347    value = iFrom.valueForKey("LogoSize");
00348    if (value) fSize = atof(value->value().c_str());
00349    
00350    value = iFrom.valueForKey("LogoPosX");
00351    if (value) fPosX = atof(value->value().c_str());
00352    
00353    value = iFrom.valueForKey("LogoPosY");
00354    if (value) fPosY = atof(value->value().c_str());
00355    
00356 }