Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TGLOverlayButton.cxx
Go to the documentation of this file.
1 // @(#)root/gl:$Id$
2 // Author: Bertrand Bellenot 2008
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2007, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #include "TGLOverlayButton.h"
13 #include "TColor.h"
14 #include "TMath.h"
15 
16 #include <TGLRnrCtx.h>
17 #include <TGLIncludes.h>
18 #include <TGLSelectRecord.h>
19 #include <TGLUtil.h>
20 #include <TGLCamera.h>
21 #include <TGLViewerBase.h>
22 
23 /** \class TGLOverlayButton
24 \ingroup opengl
25 GL-overlay button.
26 */
27 
28 ClassImp(TGLOverlayButton);
29 
30 ////////////////////////////////////////////////////////////////////////////////
31 /// Constructor.
32 
33 TGLOverlayButton::TGLOverlayButton(TGLViewerBase *parent, const char *text,
34  Float_t posx, Float_t posy, Float_t width, Float_t height) :
35  TGLOverlayElement(),
36  fText(text),
37  fActiveID(-1),
38  fBackColor(0x8080ff),
39  fTextColor(0xffffff),
40  fNormAlpha(0.2),
41  fHighAlpha(1.0),
42  fPosX(posx),
43  fPosY(posy),
44  fWidth(width),
45  fHeight(height)
46 {
47  if (parent)
48  parent->AddOverlayElement(this);
49 }
50 
51 ////////////////////////////////////////////////////////////////////////////////
52 /// Render the overlay elements.
53 
54 void TGLOverlayButton::Render(TGLRnrCtx& rnrCtx)
55 {
56  Float_t r, g, b;
57  glMatrixMode(GL_PROJECTION);
58  glPushMatrix();
59  glLoadIdentity();
60  if (rnrCtx.Selection())
61  {
62  TGLRect rect(*rnrCtx.GetPickRectangle());
63  rnrCtx.GetCamera()->WindowToViewport(rect);
64  gluPickMatrix(rect.X(), rect.Y(), rect.Width(), rect.Height(),
65  (Int_t*) rnrCtx.GetCamera()->RefViewport().CArr());
66  }
67  const TGLRect& vp = rnrCtx.RefCamera().RefViewport();
68  glOrtho(vp.X(), vp.Width(), vp.Y(), vp.Height(), 0, 1);
69  glMatrixMode(GL_MODELVIEW);
70  glPushMatrix();
71  glLoadIdentity();
72 
73  Float_t offset = (fPosY >= 0.0)? 0.0 : vp.Height()-fHeight;
74 
75  TGLCapabilitySwitch lights_off(GL_LIGHTING, kFALSE);
76  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
77  glDisable(GL_CULL_FACE);
78  glEnable(GL_BLEND);
79  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
80  glShadeModel(GL_FLAT);
81  glClearColor(0.0, 0.0, 0.0, 0.0);
82  glPushName(1);
83 
84  // Button rendering
85  {
86  TGLCapabilitySwitch move_to_back(GL_POLYGON_OFFSET_FILL, kTRUE);
87  glPolygonOffset(0.5f, 0.5f);
88  glPushMatrix();
89  glTranslatef(fPosX, offset+fPosY, 0);
90  // First the border, same color as text
91  TColor::Pixel2RGB(fTextColor, r, g, b);
92  (fActiveID == 1) ? TGLUtil::Color4f(r, g, b, fHighAlpha):TGLUtil::Color4f(r, g, b, fNormAlpha);
93  TGLUtil::LineWidth(1);
94  glBegin(GL_LINE_LOOP);
95  glVertex2f(0.0, 0.0);
96  glVertex2f(0.0, fHeight);
97  glVertex2f(fWidth, fHeight);
98  glVertex2f(fWidth, 0.0);
99  glEnd();
100  // then the button itself, with its own color
101  // decrease a bit the highlight, to avoid bad effects...
102  TColor::Pixel2RGB(fBackColor, r, g, b);
103  (fActiveID == 1) ? TGLUtil::Color4f(r, g, b, fHighAlpha * 0.8):TGLUtil::Color4f(r, g, b, fNormAlpha);
104  glBegin(GL_QUADS);
105  glVertex2f(0.0, 0.0);
106  glVertex2f(0.0, fHeight);
107  glVertex2f(fWidth, fHeight);
108  glVertex2f(fWidth, 0.0);
109  glEnd();
110  glPopMatrix();
111  }
112 
113  // Text rendering
114  {
115  rnrCtx.RegisterFontNoScale(TMath::Nint(fHeight*0.8), "arial", TGLFont::kPixmap, fFont);
116  fFont.PreRender(kFALSE);
117 
118  TColor::Pixel2RGB(fTextColor, r, g, b);
119  (fActiveID == 1) ? TGLUtil::Color4f(r, g, b, fHighAlpha):TGLUtil::Color4f(r, g, b, fNormAlpha);
120  glPushMatrix();
121  glTranslatef(fPosX+(fWidth/2.0), offset+fPosY+(fHeight/2.0), 0);
122  Float_t llx, lly, llz, urx, ury, urz;
123  fFont.BBox(fText.Data(), llx, lly, llz, urx, ury, urz);
124  glRasterPos2i(0, 0);
125  glBitmap(0, 0, 0, 0, -urx*0.5f, -ury*0.5f, 0);
126  fFont.Render(fText.Data());
127  fFont.PostRender();
128  glPopMatrix();
129  }
130  glPopName();
131 
132  glMatrixMode(GL_PROJECTION);
133  glPopMatrix();
134  glMatrixMode(GL_MODELVIEW);
135  glPopMatrix();
136 }
137 
138 ////////////////////////////////////////////////////////////////////////////////
139 /// Emits "Clicked(TGLViewerBase*)" signal.
140 /// Called when user click on the GL button.
141 
142 void TGLOverlayButton::Clicked(TGLViewerBase *viewer)
143 {
144  Emit("Clicked(TGLViewerBase*)", (Long_t)viewer);
145 }
146 
147 /******************************************************************************/
148 // Virtual event handlers from TGLOverlayElement
149 /******************************************************************************/
150 
151 ////////////////////////////////////////////////////////////////////////////////
152 /// Handle overlay event.
153 /// Return TRUE if event was handled.
154 
155 Bool_t TGLOverlayButton::Handle(TGLRnrCtx & rnrCtx,
156  TGLOvlSelectRecord & rec,
157  Event_t * event)
158 {
159  if (event->fCode != kButton1) {
160  return kFALSE;
161  }
162  switch (event->fType) {
163  case kButtonPress:
164  if (rec.GetItem(1) == 1) {
165  return kTRUE;
166  }
167  break;
168  case kButtonRelease:
169  if (rec.GetItem(1) == 1) {
170  Clicked(rnrCtx.GetViewer());
171  return kTRUE;
172  }
173  break;
174  default:
175  break;
176  }
177  return kFALSE;
178 }
179 
180 ////////////////////////////////////////////////////////////////////////////////
181 /// Mouse has entered overlay area.
182 
183 Bool_t TGLOverlayButton::MouseEnter(TGLOvlSelectRecord& /*rec*/)
184 {
185  fActiveID = 1;
186  return kTRUE;
187 }
188 
189 ////////////////////////////////////////////////////////////////////////////////
190 /// Mouse has left overlay area.
191 
192 void TGLOverlayButton::MouseLeave()
193 {
194  fActiveID = -1;
195 }