openscenegraph
osgWidget/Widget
Go to the documentation of this file.
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2008 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version. The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * OpenSceneGraph Public License for more details.
12*/
13
14// Code by: Jeremy Moles (cubicool) 2007-2008
15
16#ifndef OSGWIDGET_WIDGET
17#define OSGWIDGET_WIDGET
18
19#include <osg/Texture2D>
20#include <osgWidget/EventInterface>
21#include <osgWidget/StyleInterface>
22#include <osgWidget/UIObjectParent>
23#include <osgWidget/Types>
24
25namespace osgWidget {
26
27class Window;
28class WindowManager;
29
30// A Widget is a rectangular region that receives events about the state of various input
31// devices such as the pointer and keyboard. It is aware of it's width, height, and origin but
32// nothing else. It is the job of higher-level container objects to organize layouts and
33// the like, and to contextualize the meaning of the widgets "origin" (whether it is absolute
34// or relative).
35class OSGWIDGET_EXPORT Widget: public osg::Geometry, public EventInterface, public StyleInterface {
36public:
37 enum Corner {
38 LOWER_LEFT = 0,
39 LOWER_RIGHT = 1,
40 UPPER_RIGHT = 2,
41 UPPER_LEFT = 3,
42 LL = LOWER_LEFT,
43 LR = LOWER_RIGHT,
44 UR = UPPER_RIGHT,
45 UL = UPPER_LEFT,
46 ALL_CORNERS = 4
47 };
48
49 enum Layer {
50 LAYER_TOP = 100,
51 LAYER_HIGH = 75,
52 LAYER_MIDDLE = 50,
53 LAYER_LOW = 25,
54 LAYER_BG = 0
55 };
56
57 enum VerticalAlignment {
58 VA_CENTER,
59 VA_TOP,
60 VA_BOTTOM
61 };
62
63 enum HorizontalAlignment {
64 HA_CENTER,
65 HA_LEFT,
66 HA_RIGHT
67 };
68
69 enum CoordinateMode {
70 CM_ABSOLUTE,
71 CM_RELATIVE
72 };
73
74
75 Widget (const std::string& = "", point_type = 0.0f, point_type = 0.0f);
76 Widget (const Widget&, const osg::CopyOp&);
77
78 META_Object (osgWidget, Widget);
79
80 virtual ~Widget() {
81 }
82
83 // This method is called when the widget is added to a Window; this is useful
84 // when the widget needs to do something advanced (like a Label). The parent
85 // is passed as the first argument, although _parent should already be set.
86 virtual void parented(Window*) {
87 }
88
89 virtual void unparented(Window*) {
90 }
91
92 // This method is called when the widget's parent Window is added to a
93 // WindowManager object. The base managed method (WHICH YOU MUST REMEMBER
94 // TO CALL IN YOUR DERIVED METHODS!) sets the TexMat properly depending
95 // on what coordinate system you're using.
96 virtual void managed(WindowManager*) {
97 }
98
99 virtual void unmanaged(WindowManager*) {
100 }
101
102 // This method is called when the widget is resized or reallocated in any
103 // way. This is useful when the widget manages some internal Drawables that need
104 // to be modified in some way.
105 virtual void positioned() {
106 }
107
108 void setDimensions(
109 point_type = -1.0f,
110 point_type = -1.0f,
111 point_type = -1.0f,
112 point_type = -1.0f,
113 point_type = -1.0f
114 );
115
116 void setPadding (point_type);
117 void setColor (color_type, color_type, color_type, color_type, Corner = ALL_CORNERS);
118 void addColor (color_type, color_type, color_type, color_type, Corner = ALL_CORNERS);
119 void setTexCoord (texcoord_type, texcoord_type, Corner = ALL_CORNERS);
120 void setLayer (Layer l, unsigned int offset = 0);
121
122 // These are additional texture coordinate setting methods.
123 // This method will use a given origin as the LOWER_LEFT point and texture the
124 // remaining area based on some width and height values.
125 void setTexCoordRegion(point_type, point_type, point_type, point_type);
126
127 // These are convenience methods for setting up wrapping modes.
128 void setTexCoordWrapHorizontal ();
129 void setTexCoordWrapVertical ();
130
131 bool setImage (osg::Image*, bool = false, bool = false);
132
133 template<class T> bool setImage(const osg::ref_ptr<T>& image, bool f1 = false, bool f2 = false) { return setImage(image.get(), f1, f2); }
134
135
136 bool setImage (const std::string&, bool = false, bool = false);
137 bool setTexture (osg::Texture*, bool = false, bool = false);
138
139 void addX (point_type);
140 void addY (point_type);
141 void addWidth (point_type);
142 void addHeight (point_type);
143 void addOrigin (point_type, point_type);
144 void addSize (point_type, point_type);
145
146 point_type getWidth () const;
147 point_type getHeight () const;
148 point_type getX () const;
149 point_type getY () const;
150 point_type getZ () const;
151 point_type getPadHorizontal () const;
152 point_type getPadVertical () const;
153
154 const Point& getPoint (Corner = ALL_CORNERS) const;
155 const Color& getColor (Corner = ALL_CORNERS) const;
156 const TexCoord& getTexCoord (Corner = ALL_CORNERS) const;
157
158 Color getImageColorAtXY (point_type x, point_type y) const;
159 XYCoord localXY (double, double) const;
160
161 bool isPaddingUniform() const;
162
163 bool isManaged() const {
164 return _isManaged;
165 }
166
167 bool isStyled() const {
168 return _isStyled;
169 }
170
171 void setDimensions(const Quad& q, point_type z = -1.0f) {
172 setDimensions(q[0], q[1], q[2], q[3], z);
173 }
174
175 void setX(point_type x) {
176 setDimensions(x);
177 }
178
179 void setY(point_type y) {
180 setDimensions(-1.0f, y);
181 }
182
183 // This method should be use with extreme caution.
184 void setZ(point_type z) {
185 setDimensions(-1.0f, -1.0f, -1.0f, -1.0f, z);
186 }
187
188 void setWidth(point_type w) {
189 setDimensions(-1.0f, -1.0f, w);
190 }
191
192 void setHeight(point_type h) {
193 setDimensions(-1.0f, -1.0f, -1.0f, h);
194 }
195
196 void setOrigin(point_type x, point_type y) {
197 setDimensions(x, y);
198 }
199
200 void setOrigin(const XYCoord& xy) {
201 setOrigin(xy.x(), xy.y());
202 }
203
204 void setSize(point_type w, point_type h) {
205 setDimensions(-1.0f, -1.0f, w, h);
206 }
207
208 void setSize(const XYCoord& xy) {
209 setSize(xy.x(), xy.y());
210 }
211
212 void setColor(const Color& col, Corner p = ALL_CORNERS) {
213 setColor(col.r(), col.g(), col.b(), col.a(), p);
214 }
215
216 void setTexCoord(const XYCoord& xy, Corner p = ALL_CORNERS) {
217 setTexCoord(xy.x(), xy.y(), p);
218 }
219
220 void setTexCoordRegion(const XYCoord& xy, point_type w, point_type h) {
221 setTexCoordRegion(xy.x(), xy.y(), w, h);
222 }
223
224 void setTexCoordRegion(point_type x, point_type y, const XYCoord& wh) {
225 setTexCoordRegion(x, y, wh.x(), wh.y());
226 }
227
228 void setTexCoordRegion(const XYCoord& xy, const XYCoord& wh) {
229 setTexCoordRegion(xy.x(), xy.y(), wh.x(), wh.y());
230 }
231
232 void addColor(const Color& col, Corner p = ALL_CORNERS) {
233 addColor(col.r(), col.g(), col.b(), col.a(), p);
234 }
235
236 void addOrigin(const XYCoord& xy) {
237 addOrigin(xy.x(), xy.y());
238 }
239
240 void addSize(const XYCoord& xy) {
241 addSize(xy.x(), xy.y());
242 }
243
244 void setMinimumSize(point_type width, point_type height) {
245 _minWidth = width;
246 _minHeight = height;
247 }
248
249 void setMinimumSize(const XYCoord& xy) {
250 setMinimumSize(xy.x(), xy.y());
251 }
252
253 void setPadLeft(point_type p) {
254 _padLeft = p;
255 }
256
257 void setPadRight(point_type p) {
258 _padRight = p;
259 }
260
261 void setPadTop(point_type p) {
262 _padTop = p;
263 }
264
265 void setPadBottom(point_type p) {
266 _padBottom = p;
267 }
268
269 void setAlignHorizontal(HorizontalAlignment h) {
270 _halign = h;
271 }
272
273 void setAlignVertical(VerticalAlignment v) {
274 _valign = v;
275 }
276
277 void setCoordinateMode(CoordinateMode cm) {
278 _coordMode = cm;
279 }
280
281 void setCanFill(bool f) {
282 _canFill = f;
283 }
284
285 void setCanClone(bool c) {
286 _canClone = c;
287 }
288
289 WindowManager* getWindowManager() {
290 return _getWindowManager();
291 }
292
293 const WindowManager* getWindowManager() const {
294 return _getWindowManager();
295 }
296
297 Window* getParent() {
298 return _parent;
299 }
300
301 const Window* getParent() const {
302 return _parent;
303 }
304
305 unsigned int getIndex() const {
306 return _index;
307 }
308
309 XYCoord getOrigin() const {
310 return XYCoord(getX(), getY());
311 }
312
313 Color getImageColorAtXY(const XYCoord& xy) const {
314 return getImageColorAtXY(xy.x(), xy.y());
315 }
316
317 Color getImageColorAtPointerXY(double x, double y) const {
318 return getImageColorAtXY(localXY(x, y));
319 }
320
321 Point getPosition() const {
322 return Point(getX(), getY(), getZ());
323 }
324
325 XYCoord getSize() const {
326 return XYCoord(getWidth(), getHeight());
327 }
328
329 Quad getDimensions() const {
330 return Quad(getX(), getY(), getWidth(), getHeight());
331 }
332
333 point_type getPadLeft() const {
334 return _padLeft;
335 }
336
337 point_type getPadRight() const {
338 return _padRight;
339 }
340
341 point_type getPadTop() const {
342 return _padTop;
343 }
344
345 point_type getPadBottom() const {
346 return _padBottom;
347 }
348
349 HorizontalAlignment getAlignHorizontal() const {
350 return _halign;
351 }
352
353 VerticalAlignment getAlignVertical() const {
354 return _valign;
355 }
356
357 CoordinateMode getCoordinateMode() const {
358 return _coordMode;
359 }
360
361 bool canFill() const {
362 return _canFill;
363 }
364
365 bool canClone() const {
366 return _canClone;
367 }
368
369 // This casts the bool _fill variable to be used in iteratively in functions such
370 // as Window::_accumulate and whatnot.
371 point_type getFillAsNumeric() const {
372 return static_cast<point_type>(_canFill);
373 }
374
375 point_type getWidthTotal() const {
376 return getWidth() + getPadHorizontal();
377 }
378
379 point_type getHeightTotal() const {
380 return getHeight() + getPadVertical();
381 }
382
383 point_type getMinWidth() const {
384 return _minWidth;
385 }
386
387 point_type getMinHeight() const {
388 return _minHeight;
389 }
390
391 point_type getMinWidthTotal() const {
392 return _minWidth + getPadHorizontal();
393 }
394
395 point_type getMinHeightTotal() const {
396 return _minHeight + getPadVertical();
397 }
398
399 unsigned int getLayer() const {
400 return _layer;
401 }
402
403 protected:
404
405 point_type _calculateZ(unsigned int) const;
406
407 PointArray* _verts() {
408 return dynamic_cast<PointArray*>(getVertexArray());
409 }
410
411 const PointArray* _verts() const {
412 return dynamic_cast<const PointArray*>(getVertexArray());
413 }
414
415 ColorArray* _cols() {
416 return dynamic_cast<ColorArray*>(getColorArray());
417 }
418
419 const ColorArray* _cols() const {
420 return dynamic_cast<const ColorArray*>(getColorArray());
421 }
422
423 TexCoordArray* _texs() {
424 return dynamic_cast<TexCoordArray*>(getTexCoordArray(0));
425 }
426
427 const TexCoordArray* _texs() const {
428 return dynamic_cast<const TexCoordArray*>(getTexCoordArray(0));
429 }
430
431 osg::Texture* _texture() {
432 osg::StateSet* ss = getStateSet();
433
434 if(!ss) return 0;
435
436 return dynamic_cast<osg::Texture2D*>(
437 ss->getTextureAttribute(0, osg::StateAttribute::TEXTURE)
438 );
439 }
440
441 const osg::Texture* _texture() const {
442 const osg::StateSet* ss = getStateSet();
443
444 if(!ss) return 0;
445
446 return dynamic_cast<const osg::Texture2D*>(
447 ss->getTextureAttribute(0, osg::StateAttribute::TEXTURE)
448 );
449 }
450
451 osg::Image* _image() {
452 return _getImage();
453 }
454
455 const osg::Image* _image() const {
456 return _getImage();
457 }
458
459 friend class Window;
460
461 // TODO: Because of the current class design, I don't think it's possible to
462 // have a ref_ptr here. :(
463 Window* _parent;
464
465 unsigned int _index;
466 unsigned int _layer;
467
468 // Padding is the value of pixels of space between whatever the widget is "contianed"
469 // in and the point at which it starts getting placed.
470 point_type _padLeft;
471 point_type _padRight;
472 point_type _padTop;
473 point_type _padBottom;
474
475 // The alignments are used in conjunction when the widget is NOT set to fill.
476 VerticalAlignment _valign;
477 HorizontalAlignment _halign;
478
479 // This flag determines how sizing values are interpretted by setDimensions().
480 CoordinateMode _coordMode;
481
482 // These are the relative values, which are not stored directly in our verts
483 // array but kept around for calculation later.
484 Quad _relCoords;
485
486 // This fill flag determines whether or not the widget will resize itself to fill
487 // all available space.
488 bool _canFill;
489
490 // Set this to false in an implementation to prevent copying.
491 bool _canClone;
492
493 // This variable is only used by the Window object to determine if it's necessary
494 // to call managed().
495 bool _isManaged;
496
497 // This variable is like _isManaged; it is used to store whether the Widget has
498 // been styled yet.
499 bool _isStyled;
500
501 // Set these variables to be the minimum size of a Widget so that it cannot be
502 // resized any smaller than this.
503 point_type _minWidth;
504 point_type _minHeight;
505
506 static osg::ref_ptr<PointArray> _norms;
507
508 WindowManager* _getWindowManager () const;
509 osg::Image* _getImage () const;
510
511};
512
513typedef std::list<osg::observer_ptr<Widget> > WidgetList;
514
515// A Widget subclass that prints stuff using osg::notify(). :)
516struct NotifyWidget: public Widget {
517 META_Object(osgWidget, NotifyWidget);
518
519 NotifyWidget(const std::string& n = "", point_type w = 0.0f, point_type h = 0.0f):
520 Widget(n, w, h) {
521 setEventMask(EVENT_ALL);
522 }
523
524 NotifyWidget(const NotifyWidget& widget, const osg::CopyOp& co):
525 Widget(widget, co) {
526 }
527
528 bool focus(const WindowManager*) {
529 osg::notify(osg::NOTICE) << _name << " > focus called" << std::endl;
530
531 return false;
532 }
533
534 bool unfocus(const WindowManager*) {
535 osg::notify(osg::NOTICE) << _name << " > unfocus called" << std::endl;
536
537 return false;
538 }
539
540 bool mouseEnter(double, double, const WindowManager*) {
541 osg::notify(osg::NOTICE) << _name << " > mouseEnter called" << std::endl;
542
543 return false;
544 }
545
546 bool mouseOver(double, double, const WindowManager*) {
547 osg::notify(osg::NOTICE) << _name << " > mouseOver called" << std::endl;
548
549 return false;
550 }
551
552 bool mouseLeave(double, double, const WindowManager*) {
553 osg::notify(osg::NOTICE) << _name << " > mouseLeave called" << std::endl;
554
555 return false;
556 }
557
558 bool mouseDrag(double, double, const WindowManager*) {
559 osg::notify(osg::NOTICE) << _name << " > mouseDrag called" << std::endl;
560
561 return false;
562 }
563
564 bool mousePush(double, double, const WindowManager*) {
565 osg::notify(osg::NOTICE) << _name << " > mousePush called" << std::endl;
566
567 return false;
568 }
569
570 bool mouseRelease(double, double, const WindowManager*) {
571 osg::notify(osg::NOTICE) << _name << " > mouseRelease called" << std::endl;
572
573 return false;
574 }
575
576 bool mouseScroll(double, double, const WindowManager*) {
577 osg::notify(osg::NOTICE) << _name << " > mouseScroll called" << std::endl;
578
579 return false;
580 }
581
582 bool keyPress(int, int, const WindowManager*) {
583 osg::notify(osg::NOTICE) << _name << " > keyPress called" << std::endl;
584
585 return false;
586 }
587
588 bool keyRelease(int, int, const WindowManager*) {
589 osg::notify(osg::NOTICE) << _name << " > keyRelease called" << std::endl;
590
591 return false;
592 }
593};
594
595// A Widget that eats all events and returns true.
596struct NullWidget: public Widget {
597 META_Object(osgWidget, NullWidget);
598
599 NullWidget(const std::string& n = "", point_type w = 0.0f, point_type h = 0.0f):
600 Widget(n, w, h) {
601 setEventMask(EVENT_ALL);
602 }
603
604 NullWidget(const NullWidget& widget, const osg::CopyOp& co):
605 Widget(widget, co) {
606 }
607
608 bool focus(const WindowManager*) {
609 return true;
610 }
611
612 bool unfocus(const WindowManager*) {
613 return true;
614 }
615
616 bool mouseEnter(double, double, const WindowManager*) {
617 return true;
618 }
619
620 bool mouseOver(double, double, const WindowManager*) {
621 return true;
622 }
623
624 bool mouseLeave(double, double, const WindowManager*) {
625 return true;
626 }
627
628 bool mouseDrag(double, double, const WindowManager*) {
629 return true;
630 }
631
632 bool mousePush(double, double, const WindowManager*) {
633 return true;
634 }
635
636 bool mouseRelease(double, double, const WindowManager*) {
637 return true;
638 }
639
640 bool mouseScroll(double, double, const WindowManager*) {
641 return true;
642 }
643
644 bool keyPress(int, int, const WindowManager*) {
645 return true;
646 }
647
648 bool keyRelease(int, int, const WindowManager*) {
649 return true;
650 }
651};
652
653}
654
655#endif