Widget.H

Go to the documentation of this file.
00001 
00009 #ifndef WIDGET_H
00010 #define WIDGET_H
00011 
00012 namespace VRG3D {
00013 
00014 // forward declaration of WidgetMgr
00015 class WidgetMgr;
00016 typedef ReferenceCountedPointer<WidgetMgr> WidgetMgrRef;
00017 
00018 
00019 
00020 typedef ReferenceCountedPointer<class Widget> WidgetRef;
00032 class Widget : public ReferenceCountedObject
00033 {
00034 public:
00035   Widget(WidgetMgrRef widgetMgr) {
00036     _widgetMgr = widgetMgr;
00037     _isActive = false;
00038   }
00039   virtual ~Widget() {}
00040 
00044   virtual bool canReleaseFocus() { return true; }
00045 
00051   virtual bool pointerOverWidget(Vector3 pointerPosRoomSpace) { return false; }
00052 
00053   virtual void activate()   { _isActive = true; }
00054   virtual void deactivate() { debugAssert(canReleaseFocus()); _isActive = false; }
00055   virtual void processEvent(EventRef events, Array<EventRef> &generatedEvents) {}
00056   virtual void doGraphics(RenderDevice *rd) {}
00057   
00058   bool isActive() { return _isActive; }
00059 
00060 protected:
00061   bool         _isActive;
00062   WidgetMgrRef _widgetMgr;
00063 };
00064 
00065 
00066 
00076 class WidgetMgr : public ReferenceCountedObject
00077 {
00078 public:
00079   WidgetMgr(const std::string &pointerMoveEvent) {
00080     _pointerEvent = pointerMoveEvent;
00081   }
00082   virtual ~WidgetMgr() {}
00083 
00085   void addWidget(WidgetRef widget, bool pointerActivated) {
00086     _widgets.append(widget);
00087     if (pointerActivated) {
00088       _activateOnPointerOver.append(widget);
00089     }
00090   }
00091 
00094   void processEvent(EventRef event, Array<EventRef> &generatedEvents) {
00095     if ((event->getName() == _pointerEvent) && 
00096         ((_activeWidget.isNull()) || (_activeWidget->canReleaseFocus()))) {
00097       Vector3 pos = event->getCoordinateFrameData().translation;
00098       for (int j=0;j<_activateOnPointerOver.size();j++) {
00099         if (_activateOnPointerOver[j]->pointerOverWidget(pos)) {
00100           activateWidget(_activateOnPointerOver[j]);
00101         }
00102       }
00103     }
00104     
00105     if (_activeWidget.notNull()) {
00106       _activeWidget->processEvent(event, generatedEvents);
00107     }
00108   }
00109 
00112   void doGraphics(RenderDevice *rd) {
00113     for (int i=0;i<_widgets.size();i++) {
00114       _widgets[i]->doGraphics(rd);
00115     }
00116 
00117   }
00118 
00119   bool activateWidget(WidgetRef toActivate) {
00120     if (_activeWidget.notNull()) {
00121       if (_activeWidget->canReleaseFocus()) {
00122         _activeWidget->deactivate();
00123       }
00124       else {
00125         return false;
00126       }
00127     }
00128     _activeWidget = toActivate;
00129     _activeWidget->activate();
00130     return true;
00131   }
00132 
00133 private:
00134   WidgetRef        _activeWidget;
00135   Array<WidgetRef> _widgets;
00136 
00140   Array<WidgetRef> _activateOnPointerOver;
00141   std::string      _pointerEvent;
00142 };
00143 
00144 
00145 
00146 } // end namespace
00147 
00148 #endif
00149 
00150 

Generated on Mon Nov 20 23:11:03 2006 for VRG3D-UI by  doxygen 1.4.6