00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef ENT_HUD_H
00028 #define ENT_HUD_H
00029
00030 #include <osg/NodeCallback>
00031 #include <osg/Projection>
00032 #include <PVLE/Util/Util.h>
00033 #include <PVLE/3D/Commons.h>
00034
00042 class Hud : public virtual osg::Group {
00043 public:
00045 Hud(UINT left, UINT right, UINT bottom, UINT top) : left(left), right(right), bottom(bottom), top(top) {
00046
00047 osg::Group::setUpdateCallback(new HudCB(this));
00048
00049
00050
00051 std::pair<osg::Projection*, osg::MatrixTransform*> p = create2DProjection(osg::Matrix::ortho2D(left, right, bottom, top));
00052 pProjection = p.first;
00053 pRoot2D = p.second;
00054 addChild(pProjection);
00055
00056 setHudStatset(pProjection->getOrCreateStateSet());
00057
00058
00059
00060
00061
00062
00063
00064 }
00065
00068 virtual void onProjectionChanged(UINT left, UINT right, UINT bottom, UINT top) {
00069 set2DProjection(left, right, bottom, top);
00070 }
00071
00073 virtual void updateCB(osg::Node *node, osg::NodeVisitor *nv)=0;
00074
00077
00078 inline float fromLeft (float x) const { return left+x; }
00079 inline float fromRight (float x) const { return right-x; }
00080 inline float fromBottom(float y) const { return bottom+y; }
00081 inline float fromTop (float y) const { return top-y; }
00082
00083 inline osg::Vec3 fromCornerLT(float x, float y) const { return osg::Vec3(fromLeft(x), fromTop(y) , 0); }
00084 inline osg::Vec3 fromCornerLB(float x, float y) const { return osg::Vec3(fromLeft(x), fromBottom(y), 0); }
00085 inline osg::Vec3 fromCornerRT(float x, float y) const { return osg::Vec3(fromRight(x), fromTop(y) , 0); }
00086 inline osg::Vec3 fromCornerRB(float x, float y) const { return osg::Vec3(fromRight(x), fromBottom(y), 0); }
00087
00088 inline float relativeFromLeft (float percent) const { return fromLeft (widthRelative (percent)); }
00089 inline float relativeFromRight (float percent) const { return fromRight (widthRelative (percent)); }
00090 inline float relativeFromBottom(float percent) const { return fromBottom(heightRelative(percent)); }
00091 inline float relativeFromTop (float percent) const { return fromTop (heightRelative(percent)); }
00092
00093 inline float widthRelative (float percent) const { return percent*(right-left); }
00094 inline float heightRelative(float percent) const { return percent*(top-bottom); }
00095
00097
00098 protected:
00099 osg::Projection * pProjection;
00100 osg::MatrixTransform * pRoot2D;
00101
00102
00104
00105 class HudCB : public osg::NodeCallback {
00106 public:
00107 HudCB(Hud * pHud) : pHud(pHud) {}
00108 virtual void operator()(osg::Node *node, osg::NodeVisitor *nv) { pHud->updateCB(node, nv); traverse(node, nv); }
00109 protected:
00110 Hud * pHud;
00111 };
00112
00114 void set2DProjection(UINT left, UINT right, UINT bottom, UINT top) {
00115 this->left=left; this->right=right; this->bottom=bottom; this->top=top;
00116 pProjection->setMatrix(osg::Matrix::ortho2D(left, right, bottom, top));
00117 }
00118
00119 private:
00120 UINT left, right, bottom, top;
00121 };
00122
00123
00124 #endif // ENT_HUD_H