root/OpenSceneGraph/trunk/src/osgSim/ScalarBar.cpp @ 13557

Revision 13557, 6.3 kB (checked in by robert, 14 hours ago)

Created ui lua scripts in a way that makes them compatible with being used with lua require()

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#include <osgSim/ScalarBar>
2#include <osgText/Text>
3#include <osg/Geometry>
4#include <sstream>
5
6using namespace osgSim;
7
8ScalarBar::~ScalarBar()
9{
10}
11
12std::string ScalarBar::ScalarPrinter::printScalar(float scalar)
13{
14    std::stringstream ostr;
15    ostr<<scalar;
16    return ostr.str();
17}
18
19void ScalarBar::setNumColors(int numColors)
20{
21    _numColors = numColors;
22    createDrawables();
23}
24
25int ScalarBar::getNumColors() const
26{
27    return _numColors;
28}
29
30void ScalarBar::setNumLabels(int numLabels)
31{
32    _numLabels = numLabels;
33    createDrawables();
34}
35
36int ScalarBar::getNumLabels() const
37{
38    return _numLabels;
39}
40
41void ScalarBar::setScalarsToColors(ScalarsToColors* stc)
42{
43    _stc = stc;
44    createDrawables();
45}
46
47const ScalarsToColors* ScalarBar::getScalarsToColors() const
48{
49    return _stc.get();
50}
51
52void ScalarBar::setTitle(const std::string& title)
53{
54    _title = title;
55    createDrawables();
56}
57
58const std::string& ScalarBar::getTitle() const
59{
60    return _title;
61}
62
63void ScalarBar::setPosition(const osg::Vec3& pos)
64{
65    _position = pos;
66    createDrawables();
67}
68
69void ScalarBar::setWidth(float width)
70{
71    _width = width;
72    createDrawables();
73}
74
75void ScalarBar::setOrientation(ScalarBar::Orientation orientation)
76{
77    _orientation = orientation;
78    createDrawables();
79}
80
81ScalarBar::Orientation ScalarBar::getOrientation() const
82{
83    return _orientation;
84}
85
86void ScalarBar::setAspectRatio(float aspectRatio)
87{
88    _aspectRatio = aspectRatio;
89    createDrawables();
90}
91
92float ScalarBar::getAspectRatio() const
93{
94    return _aspectRatio;
95}
96
97void ScalarBar::setScalarPrinter(ScalarPrinter* sp)
98{
99    _sp = sp;
100    createDrawables();
101}
102
103const ScalarBar::ScalarPrinter* ScalarBar::getScalarPrinter() const
104{
105    return _sp.get();
106}
107
108
109void ScalarBar::setTextProperties(const TextProperties& tp)
110{
111    _textProperties = tp;
112    createDrawables();
113}
114
115const ScalarBar::TextProperties& ScalarBar::getTextProperties() const
116{
117    return _textProperties;
118}
119
120
121void ScalarBar::createDrawables()
122{
123    // Remove any existing Drawables
124    _drawables.erase(_drawables.begin(), _drawables.end());
125
126    if (_numColors==0) return;
127
128    osg::Matrix matrix;
129    if(_orientation==HORIZONTAL)
130    {
131        matrix = osg::Matrix::translate(_position);
132    }
133    else
134    {
135        matrix = osg::Matrix::rotate(osg::DegreesToRadians(90.0f),1.0f,0.0f,0.0f) * osg::Matrix::translate(_position);
136    }
137
138    // 1. First the bar
139    // =================
140    osg::ref_ptr<deprecated_osg::Geometry> bar = new deprecated_osg::Geometry();
141
142    // Create the bar - created in 'real' coordinate space the moment,
143    // with xyz values reflecting those of the actual scalar values in play.
144    // FIXME: Consider positioning at origin! Should be easy enough to do.
145
146    // Vertices
147    osg::ref_ptr<osg::Vec3Array> vs(new osg::Vec3Array);
148    vs->reserve(2*(_numColors+1));
149
150    float incr = (_stc->getMax() - _stc->getMin()) / _numColors;
151    float xincr = (_width) / _numColors;
152    float arOffset = _width * _aspectRatio;
153
154    int i;
155    for(i=1; i<=_numColors; ++i)
156    {
157        vs->push_back(osg::Vec3((i-1) * xincr, 0.0f,     0.0f)*matrix);
158        vs->push_back(osg::Vec3((i-1) * xincr, arOffset, 0.0f)*matrix);
159        vs->push_back(osg::Vec3(i     * xincr, arOffset, 0.0f)*matrix);
160        vs->push_back(osg::Vec3(i     * xincr, 0.0f,     0.0f)*matrix);
161    }
162    bar->setVertexArray(vs.get());
163
164    // Colours
165    osg::ref_ptr<osg::Vec4Array> cs(new osg::Vec4Array);
166    cs->reserve(_numColors);
167    const float halfIncr = incr*0.5;
168    for(i=0; i<_numColors; ++i)
169    {
170        // We add half an increment to the color look-up to get the color
171        // square in the middle of the 'block'.
172        cs->push_back(_stc->getColor(_stc->getMin() + (i*incr)  + halfIncr));
173    }
174    bar->setColorArray(cs.get());
175    bar->setColorBinding(deprecated_osg::Geometry::BIND_PER_PRIMITIVE);
176
177    // Normal
178    osg::ref_ptr<osg::Vec3Array> ns(new osg::Vec3Array);
179    ns->push_back(osg::Matrix::transform3x3(osg::Vec3(0.0f,0.0f,1.0f),matrix));
180    bar->setNormalArray(ns.get());
181    bar->setNormalBinding(deprecated_osg::Geometry::BIND_OVERALL);
182
183    // The Quad strip that represents the bar
184    bar->addPrimitiveSet(new osg::DrawArrays(GL_QUADS,0,vs->size()));
185
186    addDrawable(bar.get());
187
188    // 2. Then the text labels
189    // =======================
190
191    // Check the character size, if it's 0, estimate a good character size
192    float characterSize = _textProperties._characterSize;
193    if(characterSize == 0) characterSize = _width * 0.03f;
194
195    osgText::Font* font = osgText::readFontFile(_textProperties._fontFile.c_str());
196
197    std::vector<osgText::Text*> texts(_numLabels);      // We'll need to collect pointers to these for later
198    float labelIncr = (_numLabels>0) ? (_stc->getMax()-_stc->getMin())/(_numLabels-1) : 0.0f;
199    float labelxIncr = (_numLabels>0) ? (_width)/(_numLabels-1) : 0.0f;
200    float labely = arOffset + characterSize*0.3f;
201    for(i=0; i<_numLabels; ++i)
202    {
203        osgText::Text* text = new osgText::Text;
204        text->setFont(font);
205        text->setColor(_textProperties._color);
206        text->setFontResolution(_textProperties._fontResolution.first,_textProperties._fontResolution.second);
207        text->setCharacterSize(characterSize);
208        text->setText(_sp->printScalar(_stc->getMin()+(i*labelIncr)));
209
210        text->setPosition(osg::Vec3((i*labelxIncr), labely, 0.0f)*matrix);
211        text->setAlignment(osgText::Text::CENTER_BASE_LINE);
212        text->setAxisAlignment( (_orientation==HORIZONTAL) ? osgText::Text::XY_PLANE :  osgText::Text::XZ_PLANE );
213
214        addDrawable(text);
215
216        texts[i] = text;
217    }
218
219
220    // 3. And finally the title
221    // ========================
222
223    if(_title != "")
224    {
225        osgText::Text* text = new osgText::Text;
226        text->setFont(font);
227        text->setColor(_textProperties._color);
228        text->setFontResolution(_textProperties._fontResolution.first,_textProperties._fontResolution.second);
229        text->setCharacterSize(characterSize);
230        text->setText(_title);
231
232        float titleY = (_numLabels>0) ? labely + characterSize : labely;
233
234        // Position the title at the middle of the bar above any labels.
235        text->setPosition(osg::Vec3((_width/2.0f), titleY, 0.0f)*matrix);
236        text->setAlignment(osgText::Text::CENTER_BASE_LINE);
237        text->setAxisAlignment( (_orientation==HORIZONTAL) ? osgText::Text::XY_PLANE :  osgText::Text::XZ_PLANE );
238
239        addDrawable(text);
240    }
241}
Note: See TracBrowser for help on using the browser.