Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:11:33

0001 #include <sstream>
0002 
0003 #include "TGLIncludes.h"
0004 #include "TGLCamera.h"
0005 #include "TGLRnrCtx.h"
0006 #include "TGLSelectRecord.h"
0007 #include "TGLViewerBase.h"
0008 #include "TGLViewer.h"
0009 #include "TImage.h"
0010 #include "TEveManager.h"
0011 
0012 #include "Fireworks/Core/interface/CmsAnnotation.h"
0013 #include "Fireworks/Core/src/FWCheckBoxIcon.h"
0014 #include "Fireworks/Core/interface/FWConfiguration.h"
0015 
0016 CmsAnnotation::CmsAnnotation(TGLViewerBase* parent, Float_t posx, Float_t posy)
0017     : TGLOverlayElement(TGLOverlayElement::kUser),
0018       fPosX(posx),
0019       fPosY(posy),
0020       fMouseX(0),
0021       fMouseY(0),
0022       fDrag(kNone),
0023       fParent(nullptr),
0024       fSize(0.2),
0025       fSizeDrag(0.0),
0026       fActive(false),
0027       fAllowDestroy(false) {
0028   // Constructor.
0029   // Create annotation as plain text
0030 
0031   parent->AddOverlayElement(this);
0032   fParent = (TGLViewer*)parent;
0033 }
0034 
0035 CmsAnnotation::~CmsAnnotation() {
0036   // Destructor.
0037 
0038   fParent->RemoveOverlayElement(this);
0039 }
0040 
0041 void CmsAnnotation::Render(TGLRnrCtx& rnrCtx) {
0042   if (rnrCtx.GetCamera()->RefViewport().Width() == 0 || rnrCtx.GetCamera()->RefViewport().Height() == 0)
0043     return;
0044 
0045   static UInt_t ttid_black = 0;
0046   static UInt_t ttid_white = 0;
0047 
0048   bool whiteBg = rnrCtx.ColorSet().Background().GetColorIndex() == kWhite;
0049   UInt_t& ttid = whiteBg ? ttid_white : ttid_black;
0050 
0051   if ((whiteBg == false && ttid == 0) || (whiteBg && ttid == 0)) {
0052     TImage* imgs[3];
0053     TString base = whiteBg ? "White" : "Black";
0054     imgs[0] = TImage::Open(FWCheckBoxIcon::coreIcondir() + "CMSLogo" + base + "Bg.png");
0055     imgs[1] = TImage::Open(FWCheckBoxIcon::coreIcondir() + "CMSLogo" + base + "BgM.png");
0056     imgs[2] = TImage::Open(FWCheckBoxIcon::coreIcondir() + "CMSLogo" + base + "BgS.png");
0057 
0058     glGenTextures(1, &ttid);
0059     glBindTexture(GL_TEXTURE_2D, ttid);
0060 
0061     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
0062     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
0063     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
0064     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
0065     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
0066     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 2);
0067 
0068     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
0069     glPixelStorei(GL_UNPACK_SWAP_BYTES, 1);
0070 
0071     for (int i = 0; i < 3; i++)
0072       glTexImage2D(GL_TEXTURE_2D,
0073                    i,
0074                    GL_RGBA,
0075                    imgs[i]->GetWidth(),
0076                    imgs[i]->GetHeight(),
0077                    0,
0078                    GL_BGRA,
0079                    GL_UNSIGNED_BYTE,
0080                    imgs[i]->GetArgbArray());
0081 
0082     glPixelStorei(GL_UNPACK_SWAP_BYTES, 0);
0083 
0084     for (int i = 0; i < 3; i++)
0085       delete imgs[i];
0086   }
0087 
0088   glPushAttrib(GL_ENABLE_BIT | GL_LINE_BIT | GL_POLYGON_BIT);
0089   TGLCapabilitySwitch lights_off(GL_LIGHTING, kFALSE);
0090 
0091   // reset matrix
0092   glMatrixMode(GL_PROJECTION);
0093   glPushMatrix();
0094   glLoadIdentity();
0095 
0096   if (rnrCtx.Selection()) {
0097     TGLRect rect(*rnrCtx.GetPickRectangle());
0098     rnrCtx.GetCamera()->WindowToViewport(rect);
0099     gluPickMatrix(rect.X(), rect.Y(), rect.Width(), rect.Height(), (Int_t*)rnrCtx.GetCamera()->RefViewport().CArr());
0100   }
0101   const TGLRect& vp = rnrCtx.RefCamera().RefViewport();
0102   glOrtho(vp.X(), vp.Width(), vp.Y(), vp.Height(), 0, 1);
0103   glMatrixMode(GL_MODELVIEW);
0104   glPushMatrix();
0105   glLoadIdentity();
0106 
0107   // move to pos
0108   Float_t posX = vp.Width() * fPosX;
0109   Float_t posY = vp.Height() * fPosY;
0110   glTranslatef(posX, posY, -0.99);
0111 
0112   glDisable(GL_DEPTH_TEST);
0113   glEnable(GL_BLEND);
0114   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
0115 
0116   glEnable(GL_TEXTURE_2D);
0117   glBindTexture(GL_TEXTURE_2D, ttid);
0118 
0119   glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
0120 
0121   // logo
0122   glPushName(kMove);
0123   TGLUtil::Color(rnrCtx.ColorSet().Background().GetColorIndex());
0124   glBegin(GL_QUADS);
0125   Float_t z = 0.9;
0126   Float_t a = fSize * vp.Height();
0127   glTexCoord2f(0, 1);
0128   glVertex3f(0, -a, z);
0129   glTexCoord2f(1, 1);
0130   glVertex3f(a, -a, z);
0131   glTexCoord2f(1, 0);
0132   glVertex3f(a, 0, z);
0133   glTexCoord2f(0, 0);
0134   glVertex3f(0, 0, z);
0135   glEnd();
0136   glPopName();
0137 
0138   glDisable(GL_TEXTURE_2D);
0139 
0140   if (fActive) {
0141     // resize button
0142     glPushMatrix();
0143     glBegin(GL_QUADS);
0144     Float_t a = fSize * vp.Height();
0145     TGLUtil::ColorTransparency(rnrCtx.ColorSet().Markup().GetColorIndex(), 95);
0146     glTexCoord2f(0, 1);
0147     glVertex3f(0, -a, z);
0148     glTexCoord2f(1, 1);
0149     glVertex3f(a, -a, z);
0150     glTexCoord2f(1, 0);
0151     glVertex3f(a, 0, z);
0152     glTexCoord2f(0, 0);
0153     glVertex3f(0, 0, z);
0154     glEnd();
0155 
0156     glTranslatef(a, -a, 0);
0157     a *= 0.2;
0158     z = 0.95;
0159     glPushName(kResize);
0160     TGLUtil::ColorTransparency(rnrCtx.ColorSet().Markup().GetColorIndex(), 100);
0161     glBegin(GL_QUADS);
0162     glVertex3f(0, 0, z);
0163     glVertex3f(0, a, z);
0164     glVertex3f(-a, a, z);
0165     glVertex3f(-a, 0, z);
0166     glEnd();
0167     {
0168       glTranslatef(-a / 3, a / 3, 0);
0169       glBegin(GL_LINES);
0170       TGLUtil::ColorTransparency(rnrCtx.ColorSet().Markup().GetColorIndex(), 40);
0171       Float_t s = a / 3;
0172       glVertex3f(0, 0, z);
0173       glVertex3f(0, s, z);
0174       glVertex3f(0, 0, z);
0175       glVertex3f(-s, 0, z);
0176       glEnd();
0177     }
0178     glPopName();
0179     glPopMatrix();
0180 
0181     // delete
0182     if (fAllowDestroy) {
0183       glPushName(7);
0184       TGLUtil::ColorTransparency(rnrCtx.ColorSet().Markup().GetColorIndex(), 100);
0185       glTranslatef(0, -a, 0);
0186       glBegin(GL_QUADS);
0187       glVertex3f(0, 0, z);
0188       glVertex3f(a, 0, z);
0189       glVertex3f(a, a, z);
0190       glVertex3f(0, a, z);
0191       glEnd();
0192       {
0193         glBegin(GL_LINES);
0194         TGLUtil::ColorTransparency(rnrCtx.ColorSet().Markup().GetColorIndex(), 40);
0195         Float_t s = a / 3;
0196         glVertex3f(s, s, z);
0197         glVertex3f(a - s, a - s, z);
0198         glVertex3f(s, a - s, z);
0199         glVertex3f(a - s, s, z);
0200         glEnd();
0201       }
0202       glPopName();
0203     }
0204   }
0205 
0206   glEnable(GL_DEPTH_TEST);
0207   glMatrixMode(GL_PROJECTION);
0208   glPopMatrix();
0209   glMatrixMode(GL_MODELVIEW);
0210   glPopMatrix();
0211 
0212   glPopAttrib();
0213 }
0214 
0215 Bool_t CmsAnnotation::Handle(TGLRnrCtx& rnrCtx, TGLOvlSelectRecord& selRec, Event_t* event) {
0216   // Handle overlay event.
0217   // Return TRUE if event was handled.
0218 
0219   if (selRec.GetN() < 2)
0220     return kFALSE;
0221   int recID = selRec.GetItem(1);
0222 
0223   switch (event->fType) {
0224     case kButtonPress: {
0225       fMouseX = event->fX;
0226       fMouseY = event->fY;
0227       fDrag = (recID == kResize) ? kResize : kMove;
0228       fSizeDrag = fSize;
0229       return kTRUE;
0230     }
0231     case kButtonRelease: {
0232       fDrag = kNone;
0233       if (recID == 7) {
0234         fParent->RequestDraw(rnrCtx.ViewerLOD());
0235         delete this;
0236         return kTRUE;
0237       }
0238       break;
0239     }
0240     case kMotionNotify: {
0241       const TGLRect& vp = rnrCtx.RefCamera().RefViewport();
0242       if (vp.Width() == 0 || vp.Height() == 0)
0243         return false;
0244       if (fDrag != kNone) {
0245         if (fDrag == kMove) {
0246           fPosX += (Float_t)(event->fX - fMouseX) / vp.Width();
0247           fPosY -= (Float_t)(event->fY - fMouseY) / vp.Height();
0248           fMouseX = event->fX;
0249           fMouseY = event->fY;
0250 
0251           Float_t h = fSize;
0252           Float_t w = fSize / vp.Aspect();
0253 
0254           // Make sure we don't go offscreen (use fDraw variables set in draw)
0255           if (fPosX < 0)
0256             fPosX = 0;
0257           else if (fPosX + w > 1.0f)
0258             fPosX = 1.0f - w;
0259           if (fPosY < h)
0260             fPosY = h;
0261           else if (fPosY > 1.0f)
0262             fPosY = 1.0f;
0263         } else {
0264           using namespace TMath;
0265           Float_t oovpw = 1.0f / vp.Width(), oovph = 1.0f / vp.Height();
0266 
0267           Float_t xw = oovpw * Min(Max(0, event->fX), vp.Width());
0268           Float_t yw = oovph * Min(Max(0, vp.Height() - event->fY), vp.Height());
0269 
0270           Float_t rx = Max((xw - fPosX) / (oovpw * fMouseX - fPosX), 0.0f);
0271           Float_t ry = Max((yw - fPosY) / (oovph * (vp.Height() - fMouseY) - fPosY), 0.0f);
0272 
0273           fSize = Max(fSizeDrag * Min(rx, ry), 0.01f);
0274         }
0275       }
0276       return kTRUE;
0277     }
0278     default: {
0279       return kFALSE;
0280     }
0281   }
0282 
0283   return false;
0284 }
0285 
0286 //______________________________________________________________________________
0287 Bool_t CmsAnnotation::MouseEnter(TGLOvlSelectRecord& /*rec*/) {
0288   // Mouse has entered overlay area.
0289 
0290   fActive = kTRUE;
0291   return kTRUE;
0292 }
0293 
0294 //______________________________________________________________________
0295 void CmsAnnotation::MouseLeave() {
0296   // Mouse has left overlay area.
0297 
0298   fActive = kFALSE;
0299 }
0300 
0301 //______________________________________________________________________
0302 bool CmsAnnotation::getVisible() const { return GetState() == TGLOverlayElement::kActive; }
0303 
0304 //______________________________________________________________________
0305 void CmsAnnotation::setVisible(Bool_t x) {
0306   SetState(x ? (TGLOverlayElement::kActive) : (TGLOverlayElement::kInvisible));
0307   fParent->Changed();
0308   gEve->Redraw3D();
0309 }
0310 
0311 //______________________________________________________________________________
0312 
0313 void CmsAnnotation::addTo(FWConfiguration& iTo) const {
0314   std::stringstream s;
0315   s << fSize;
0316   iTo.addKeyValue("LogoSize", FWConfiguration(s.str()));
0317 
0318   std::stringstream x;
0319   x << fPosX;
0320   iTo.addKeyValue("LogoPosX", FWConfiguration(x.str()));
0321 
0322   std::stringstream y;
0323   y << fPosY;
0324   iTo.addKeyValue("LogoPosY", FWConfiguration(y.str()));
0325 }
0326 
0327 void CmsAnnotation::setFrom(const FWConfiguration& iFrom) {
0328   const FWConfiguration* value;
0329   value = iFrom.valueForKey("LogoSize");
0330   if (value)
0331     fSize = atof(value->value().c_str());
0332 
0333   value = iFrom.valueForKey("LogoPosX");
0334   if (value)
0335     fPosX = atof(value->value().c_str());
0336 
0337   value = iFrom.valueForKey("LogoPosY");
0338   if (value)
0339     fPosY = atof(value->value().c_str());
0340 }