TGLOverlayButton.cxx

Go to the documentation of this file.
00001 // @(#)root/gl:$Id: TGLOverlayButton.cxx 33615 2010-05-26 14:21:03Z matevz $
00002 // Author: Bertrand Bellenot 2008
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2007, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 #include "TGLOverlayButton.h"
00013 #include "TColor.h"
00014 #include "TMath.h"
00015 
00016 #include <TGLRnrCtx.h>
00017 #include <TGLIncludes.h>
00018 #include <TGLSelectRecord.h>
00019 #include <TGLUtil.h>
00020 #include <TGLCamera.h>
00021 #include <TGLViewerBase.h>
00022 
00023 //______________________________________________________________________________
00024 //
00025 //
00026 // GL-overaly button.
00027 //
00028 //
00029 
00030 ClassImp(TGLOverlayButton);
00031 
00032 //______________________________________________________________________________
00033 TGLOverlayButton::TGLOverlayButton(TGLViewerBase *parent, const char *text,
00034    Float_t posx, Float_t posy, Float_t width, Float_t height) :
00035    TGLOverlayElement(),
00036    fText(text),
00037    fActiveID(-1),
00038    fBackColor(0x8080ff),
00039    fTextColor(0xffffff),
00040    fNormAlpha(0.2),
00041    fHighAlpha(1.0),
00042    fPosX(posx),
00043    fPosY(posy),
00044    fWidth(width),
00045    fHeight(height)
00046 {
00047    // Constructor.
00048 
00049    if (parent)
00050       parent->AddOverlayElement(this);
00051 }
00052 
00053 /******************************************************************************/
00054 void TGLOverlayButton::Render(TGLRnrCtx& rnrCtx)
00055 {
00056    // Render the overlay elements.
00057 
00058    Float_t r, g, b;
00059    glMatrixMode(GL_PROJECTION);
00060    glPushMatrix();
00061    glLoadIdentity();
00062    if (rnrCtx.Selection())
00063    {
00064       TGLRect rect(*rnrCtx.GetPickRectangle());
00065       rnrCtx.GetCamera()->WindowToViewport(rect);
00066       gluPickMatrix(rect.X(), rect.Y(), rect.Width(), rect.Height(),
00067                     (Int_t*) rnrCtx.GetCamera()->RefViewport().CArr());
00068    }
00069    const TGLRect& vp = rnrCtx.RefCamera().RefViewport();
00070    glOrtho(vp.X(), vp.Width(), vp.Y(), vp.Height(), 0, 1);
00071    glMatrixMode(GL_MODELVIEW);
00072    glPushMatrix();
00073    glLoadIdentity();
00074 
00075    Float_t offset = (fPosY >= 0.0)? 0.0 : vp.Height()-fHeight;
00076 
00077    TGLCapabilitySwitch lights_off(GL_LIGHTING, kFALSE);
00078    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
00079    glDisable(GL_CULL_FACE);
00080    glEnable(GL_BLEND);
00081    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00082    glShadeModel(GL_FLAT);
00083    glClearColor(0.0, 0.0, 0.0, 0.0);
00084    glPushName(1);
00085 
00086    // Button rendering
00087    {
00088       TGLCapabilitySwitch move_to_back(GL_POLYGON_OFFSET_FILL, kTRUE);
00089       glPolygonOffset(0.5f, 0.5f);
00090       glPushMatrix();
00091       glTranslatef(fPosX, offset+fPosY, 0);
00092       // First the border, same color as text
00093       TColor::Pixel2RGB(fTextColor, r, g, b);
00094       (fActiveID == 1) ? TGLUtil::Color4f(r, g, b, fHighAlpha):TGLUtil::Color4f(r, g, b, fNormAlpha);
00095       TGLUtil::LineWidth(1);
00096       glBegin(GL_LINE_LOOP);
00097       glVertex2f(0.0, 0.0);
00098       glVertex2f(0.0, fHeight);
00099       glVertex2f(fWidth, fHeight);
00100       glVertex2f(fWidth, 0.0);
00101       glEnd();
00102       // then the button itself, with its own color
00103       // decrease a bit the highlight, to avoid bad effects...
00104       TColor::Pixel2RGB(fBackColor, r, g, b);
00105       (fActiveID == 1) ? TGLUtil::Color4f(r, g, b, fHighAlpha * 0.8):TGLUtil::Color4f(r, g, b, fNormAlpha);
00106       glBegin(GL_QUADS);
00107       glVertex2f(0.0, 0.0);
00108       glVertex2f(0.0, fHeight);
00109       glVertex2f(fWidth, fHeight);
00110       glVertex2f(fWidth, 0.0);
00111       glEnd();
00112       glPopMatrix();
00113    }
00114 
00115    // Text rendering
00116    {
00117       rnrCtx.RegisterFontNoScale(TMath::Nint(fHeight*0.8), "arial",  TGLFont::kPixmap, fFont);
00118       fFont.PreRender(kFALSE);
00119 
00120       TColor::Pixel2RGB(fTextColor, r, g, b);
00121       (fActiveID == 1) ? TGLUtil::Color4f(r, g, b, fHighAlpha):TGLUtil::Color4f(r, g, b, fNormAlpha);
00122       glPushMatrix();
00123       glTranslatef(fPosX+(fWidth/2.0), offset+fPosY+(fHeight/2.0), 0);
00124       Float_t llx, lly, llz, urx, ury, urz;
00125       fFont.BBox(fText.Data(), llx, lly, llz, urx, ury, urz);
00126       glRasterPos2i(0, 0);
00127       glBitmap(0, 0, 0, 0, -urx*0.5f, -ury*0.5f, 0);
00128       fFont.Render(fText.Data());
00129       fFont.PostRender();
00130       glPopMatrix();
00131    }
00132    glPopName();
00133 
00134    glMatrixMode(GL_PROJECTION);
00135    glPopMatrix();
00136    glMatrixMode(GL_MODELVIEW);
00137    glPopMatrix();
00138 }
00139 
00140 //______________________________________________________________________________
00141 void TGLOverlayButton::Clicked(TGLViewerBase *viewer)
00142 {
00143    // Emits "Clicked(TGLViewerBase*)" signal.
00144    // Called when user click on the GL button.
00145 
00146    Emit("Clicked(TGLViewerBase*)", (Long_t)viewer);
00147 }
00148 
00149 /******************************************************************************/
00150 // Virtual event handlers from TGLOverlayElement
00151 /******************************************************************************/
00152 
00153 //______________________________________________________________________________
00154 Bool_t TGLOverlayButton::Handle(TGLRnrCtx         & rnrCtx,
00155                                TGLOvlSelectRecord & rec,
00156                                Event_t            * event)
00157 {
00158    // Handle overlay event.
00159    // Return TRUE if event was handled.
00160 
00161    if (event->fCode != kButton1) {
00162       return kFALSE;
00163    }
00164    switch (event->fType) {
00165       case kButtonPress:
00166          if (rec.GetItem(1) == 1) {
00167             return kTRUE;
00168          }
00169          break;
00170       case kButtonRelease:
00171          if (rec.GetItem(1) == 1) {
00172             Clicked(rnrCtx.GetViewer());
00173             return kTRUE;
00174          }
00175          break;
00176       default:
00177          break;
00178    }
00179    return kFALSE;
00180 }
00181 
00182 //______________________________________________________________________________
00183 Bool_t TGLOverlayButton::MouseEnter(TGLOvlSelectRecord& /*rec*/)
00184 {
00185    // Mouse has entered overlay area.
00186 
00187    fActiveID = 1;
00188    return kTRUE;
00189 }
00190 
00191 //______________________________________________________________________________
00192 void TGLOverlayButton::MouseLeave()
00193 {
00194    // Mouse has left overlay area.
00195 
00196    fActiveID = -1;
00197 }

Generated on Tue Jul 5 14:18:07 2011 for ROOT_528-00b_version by  doxygen 1.5.1