osgEarth 2.1.1

/home/cube/sources/osgearth/src/applications/osgearth_controls/osgearth_controls.cpp

Go to the documentation of this file.
00001 /* -*-c++-*- */
00002 /* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
00003 * Copyright 2008-2010 Pelican Mapping
00004 * http://osgearth.org
00005 *
00006 * osgEarth is free software; you can redistribute it and/or modify
00007 * it under the terms of the GNU Lesser General Public License as published by
00008 * the Free Software Foundation; either version 2 of the License, or
00009 * (at your option) any later version.
00010 *
00011 * This program is distributed in the hope that it will be useful,
00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 * GNU Lesser General Public License for more details.
00015 *
00016 * You should have received a copy of the GNU Lesser General Public License
00017 * along with this program.  If not, see <http://www.gnu.org/licenses/>
00018 */
00019 
00020 #include <typeinfo>
00021 
00022 #include <osg/Notify>
00023 #include <osgGA/GUIEventHandler>
00024 #include <osgViewer/Viewer>
00025 #include <osgEarthUtil/EarthManipulator>
00026 #include <osgEarthUtil/Controls>
00027 #include <osgEarthSymbology/Color>
00028 
00029 using namespace osgEarth::Symbology;
00030 using namespace osgEarth::Util::Controls;
00031 
00032 
00033 void createControls( ControlCanvas* );
00034 ImageControl* s_imageControl;
00035 
00036 
00037 int main(int argc, char** argv)
00038 {
00039     osg::ArgumentParser arguments(&argc,argv);       
00040     osgViewer::Viewer viewer(arguments);
00041 
00042     osg::Group* root = new osg::Group();
00043     osg::Node* node = osgDB::readNodeFiles( arguments );
00044     if ( node )
00045         root->addChild( node );
00046 
00047     // create a surface to house the controls
00048     ControlCanvas* cs = ControlCanvas::get( &viewer );
00049     root->addChild( cs );
00050 
00051     viewer.setSceneData( root );
00052     viewer.setCameraManipulator( new osgEarth::Util::EarthManipulator );
00053 
00054     // create some controls.
00055     createControls( cs );
00056 
00057     return viewer.run();
00058 }
00059 
00060 struct MyClickHandler : public ControlEventHandler
00061 {
00062     void onClick( Control* control, const osg::Vec2f& pos, int mouseButtonMask )
00063     {
00064         OE_NOTICE << "You clicked at (" << pos.x() << ", " << pos.y() << ") within the control."
00065             << std::endl;
00066     }
00067 };
00068 
00069 static LabelControl* s_sliderLabel;
00070 
00071 struct MySliderHandler : public ControlEventHandler
00072 {
00073     void onValueChanged( Control* control, float value )
00074     {
00075         std::stringstream buf;
00076         buf << (int)value;
00077         std::string str = buf.str();
00078         s_sliderLabel->setText( str );
00079     }
00080 };
00081 
00082 struct RotateImage : public ControlEventHandler
00083 {
00084     void onValueChanged( Control* control, float value )
00085     {
00086         s_imageControl->setRotation( Angular(value) );
00087     }
00088 };
00089 
00090 void
00091 createControls( ControlCanvas* cs )
00092 {
00093     // a container centered on the screen, containing an image and a text label.
00094     {
00095         VBox* center = new VBox();
00096         center->setFrame( new RoundedFrame() );
00097         center->getFrame()->setBackColor( 1,1,1,0.5 );
00098         center->setPadding( 10 );
00099         center->setHorizAlign( Control::ALIGN_CENTER );
00100         center->setVertAlign( Control::ALIGN_CENTER );
00101 
00102         // Add an image:
00103         osg::ref_ptr<osg::Image> image;
00104         if ( HTTPClient::readImageFile("http://demo.pelicanmapping.com/rmweb/readymap_logo.png", image) == HTTPClient::RESULT_OK )
00105         {
00106             s_imageControl = new ImageControl( image.get() );
00107             s_imageControl->setHorizAlign( Control::ALIGN_CENTER );
00108             s_imageControl->setFixSizeForRotation( true );
00109             //imageCon->addEventHandler( new ImageRotationHandler );
00110             center->addControl( s_imageControl );
00111             center->setHorizAlign( Control::ALIGN_CENTER );
00112         }
00113 
00114         // Add a text label:
00115         LabelControl* label = new LabelControl( "osgEarth Controls Toolkit" );
00116         label->setFont( osgText::readFontFile( "arialbd.ttf" ) );
00117         label->setFontSize( 24.0f );
00118         label->setHorizAlign( Control::ALIGN_CENTER );
00119         label->setMargin( 5 );
00120         center->addControl( label );
00121 
00122         // Rotation slider
00123         HBox* rotateBox = new HBox();
00124         rotateBox->setChildVertAlign( Control::ALIGN_CENTER );
00125         rotateBox->setHorizFill( true );
00126         rotateBox->setBackColor( Color::Blue );
00127         {
00128             rotateBox->addControl( new LabelControl("Rotate: ") );
00129 
00130             HSliderControl* rotateSlider = new HSliderControl( -180.0, 180.0, 0.0 );
00131             rotateSlider->addEventHandler( new RotateImage() );
00132             rotateSlider->setHeight( 8.0f );
00133             rotateSlider->setHorizFill( true );
00134             rotateBox->addControl( rotateSlider );
00135         }
00136         center->addControl( rotateBox );
00137 
00138         cs->addControl( center );
00139     }
00140 
00141     // a simple vbox with absolute positioning in the upper left with two text labels.
00142     {
00143         VBox* ul = new VBox();
00144         ul->setPosition( 20, 20 );
00145         ul->setPadding( 10 );
00146         {
00147             LabelControl* title = new LabelControl( "Upper left control", 22, osg::Vec4f(1,1,0,1) );
00148             ul->addControl( title );
00149 
00150             LabelControl* content = new LabelControl( "Here is some text in the upper left control" );
00151             ul->addControl( content );
00152 
00153             HBox* c2 = new HBox();
00154             c2->setChildSpacing( 10 );
00155             {
00156                 HSliderControl* slider = new HSliderControl( 0, 100 );
00157                 slider->setBackColor( .6,0,0,1 );
00158                 slider->setHeight( 25 );
00159                 slider->setWidth( 300 );
00160                 slider->addEventHandler( new MySliderHandler() );
00161                 c2->addControl( slider );
00162 
00163                 s_sliderLabel = new LabelControl();
00164                 s_sliderLabel->setVertAlign( Control::ALIGN_CENTER );
00165                 c2->addControl( s_sliderLabel );        
00166             }
00167             ul->addControl( c2 );
00168 
00169             HBox* c3 = new HBox();
00170             c3->setHorizAlign( Control::ALIGN_CENTER );
00171             c3->setChildSpacing( 10 );
00172             {
00173                 HBox* c4 = new HBox();
00174                 c4->setChildSpacing( 5 );
00175                 {
00176                     c4->addControl( new CheckBoxControl( true ) );
00177                     c4->addControl( new LabelControl( "Checkbox 1" ) );
00178                 }
00179                 c3->addControl( c4 );
00180 
00181                 HBox* c5 = new HBox();
00182                 c5->setChildSpacing( 5 );
00183                 {
00184                     c5->addControl( new CheckBoxControl( false ) );
00185                     c5->addControl( new LabelControl( "Checkbox 2" ) );
00186                 }
00187                 c3->addControl( c5 );
00188             }
00189             ul->addControl( c3 );
00190         }
00191         cs->addControl( ul );
00192 
00193         ul->addEventHandler( new MyClickHandler );
00194     }
00195 
00196     // a centered hbox container along the bottom on the screen.
00197     {
00198         HBox* bottom = new HBox();
00199         bottom->setFrame( new RoundedFrame() );
00200         bottom->getFrame()->setBackColor(0,0,0,0.5);
00201         bottom->setMargin( 10 );
00202         bottom->setChildSpacing( 145 );
00203         bottom->setVertAlign( Control::ALIGN_BOTTOM );
00204         bottom->setHorizAlign( Control::ALIGN_CENTER );
00205 
00206         for( int i=0; i<4; ++i )
00207         {
00208             LabelControl* label = new LabelControl();
00209             std::stringstream buf;
00210             buf << "Label_" << i;
00211             label->setText( buf.str() );
00212             label->setMargin( 10 );
00213             label->setBackColor( 1,1,1,0.4 );
00214             bottom->addControl( label );
00215 
00216             label->setActiveColor(1,.3,.3,1);
00217             label->addEventHandler( new MyClickHandler );
00218         }
00219 
00220         cs->addControl( bottom );
00221     }
00222 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines