Show
Ignore:
Timestamp:
11/28/08 15:36:39 (5 years ago)
Author:
robert
Message:

From Jeremy Moles, updates to osgwidget examples.

Merged by Robert Osfield from OpenSceneGraph-osgWidget-dev

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • OpenSceneGraph/trunk/examples/osgwidgetinput/osgwidgetinput.cpp

    r8602 r9289  
    22// $Id: osgwidgetinput.cpp 50 2008-05-06 05:06:36Z cubicool $ 
    33 
     4#include <osgDB/ReadFile> 
    45#include <osgDB/WriteFile> 
    5  
    6 #include <osgWidget/Util> 
     6#include <osgViewer/ViewerEventHandlers> 
    77#include <osgWidget/WindowManager> 
    88#include <osgWidget/Box> 
    9 #include <osgWidget/Table> 
    10 #include <osgWidget/Frame> 
    11 #include <osgWidget/Label> 
    129#include <osgWidget/Input> 
     10#include <osgWidget/ViewerEventHandlers> 
    1311 
    1412const unsigned int MASK_2D = 0xF0000000; 
    15  
    16 const char* INFO = 
    17     "Use the Input Wigets below to enter the X, Y, and Z position of a\n" 
    18     "sphere to be inserted into the scene. Once you've done this, use\n" 
    19     "the button below to add it!" 
    20 ; 
    21  
    22 void setupLabel(osgWidget::Label* label) { 
    23     label->setFontSize(16); 
    24     label->setFontColor(1.0f, 1.0f, 1.0f, 1.0f); 
    25     label->setFont("fonts/Vera.ttf"); 
    26     label->setPadding(2.0f); 
    27     label->setHeight(18.0f); 
    28     label->setCanFill(true); 
    29 } 
    30  
    31 osgWidget::Input* createTableRow( 
    32     osgWidget::Table*  table, 
    33     unsigned int       rowNum, 
    34     const std::string& valName 
    35 ) { 
    36     std::stringstream ssLabel; 
    37     std::stringstream ssInput; 
    38  
    39     ssLabel << "Label_Row" << rowNum; 
    40     ssInput << "Input_Row" << rowNum; 
    41  
    42     osgWidget::Label* label = new osgWidget::Label(ssLabel.str(), valName); 
    43     osgWidget::Input* input = new osgWidget::Input(ssInput.str(), "", 20); 
    44  
    45     setupLabel(label); 
    46     setupLabel(input); 
    47  
    48     label->setWidth(50.0f); 
    49     label->setColor(0.1f, 0.1f, 0.1f, 1.0f); 
    50  
    51     input->setWidth(150.0f); 
    52     input->setColor(0.4f, 0.4f, 0.4f, 1.0f); 
    53  
    54     table->addWidget(label, rowNum, 0); 
    55     table->addWidget(input, rowNum, 1); 
    56  
    57     return input; 
    58 } 
    59  
    60 osgWidget::Label* createLabel(const std::string& text) { 
    61     osgWidget::Label* label = new osgWidget::Label("", text); 
    62  
    63     setupLabel(label); 
    64  
    65     return label; 
    66 } 
    67  
    68 class Button: public osgWidget::Label { 
    69 public: 
    70     typedef std::vector<osgWidget::Input*> Inputs; 
    71  
    72 private: 
    73     Inputs _xyz; 
    74  
    75 public: 
    76     Button(const std::string& text, const Inputs& inputs): 
    77     osgWidget::Label("", text), 
    78     _xyz(inputs) { 
    79         setupLabel(this); 
    80  
    81         setEventMask(osgWidget::EVENT_MASK_MOUSE_CLICK); 
    82         setShadow(0.1f); 
    83         addHeight(4.0f); 
    84     } 
    85  
    86     bool mousePush(double, double, osgWidget::WindowManager*) { 
    87         osgWidget::warn() 
    88             << "x: " << _xyz[0]->getLabel() << std::endl 
    89             << "y: " << _xyz[1]->getLabel() << std::endl 
    90             << "z: " << _xyz[2]->getLabel() << std::endl 
    91         ; 
    92  
    93         return false; 
    94     } 
    95 }; 
    96  
    97 // TODO: Testing our _parent/EmbeddedWindow stuff. 
    98 bool info(osgWidget::Event& ev) { 
    99     osgWidget::warn() << "MousePush @ Window: " << ev.getWindow()->getName() << std::endl; 
    100  
    101     return true; 
    102 } 
    10313 
    10414int main(int argc, char** argv) { 
     
    11424     
    11525    osgWidget::Box*   box   = new osgWidget::Box("vbox", osgWidget::Box::VERTICAL); 
    116     osgWidget::Table* table = new osgWidget::Table("table", 3, 2); 
    117     osgWidget::Box*   lbox1 = new osgWidget::Box("lbox1", osgWidget::Box::HORIZONTAL); 
    118     osgWidget::Box*   lbox2 = new osgWidget::Box("lbox2", osgWidget::Box::HORIZONTAL); 
    119     osgWidget::Frame* frame = osgWidget::Frame::createSimpleFrameWithSingleTexture( 
    120         "frame", 
    121         "osgWidget/theme.png", 
    122         64.0f, 
    123         64.0f, 
    124         16.0f, 
    125         16.0f, 
    126         100.0f, 
    127         100.0f 
     26    osgWidget::Input* input = new osgWidget::Input("input", "", 50); 
     27 
     28    input->setFont("fonts/VeraMono.ttf"); 
     29    input->setFontColor(0.0f, 0.0f, 0.0f, 1.0f); 
     30    input->setFontSize(15); 
     31    input->setYOffset(input->calculateBestYOffset("y")); 
     32    input->setSize(400.0f, input->getText()->getCharacterHeight()); 
     33 
     34    box->addWidget(input); 
     35    box->setOrigin(200.0f, 200.0f); 
     36 
     37    wm->addChild(box); 
     38 
     39    viewer.setUpViewInWindow( 
     40        50, 
     41        50, 
     42        static_cast<int>(wm->getWidth()), 
     43        static_cast<int>(wm->getHeight()) 
    12844    ); 
    12945 
    130     osgWidget::Input* x = createTableRow(table, 0, "X Position"); 
    131     osgWidget::Input* y = createTableRow(table, 1, "Y Position"); 
    132     osgWidget::Input* z = createTableRow(table, 2, "Z Position"); 
    133      
    134     Button::Inputs inputs; 
     46    osg::Camera* camera = wm->createParentOrthoCamera(); 
    13547 
    136     inputs.push_back(x); 
    137     inputs.push_back(y); 
    138     inputs.push_back(z); 
     48    viewer.addEventHandler(new osgWidget::MouseHandler(wm)); 
     49    viewer.addEventHandler(new osgWidget::KeyboardHandler(wm)); 
     50    viewer.addEventHandler(new osgWidget::ResizeHandler(wm, camera)); 
     51    viewer.addEventHandler(new osgWidget::CameraSwitchHandler(wm, camera)); 
     52    viewer.addEventHandler(new osgViewer::WindowSizeHandler()); 
    13953 
    140     table->addCallback(osgWidget::Callback(&info, osgWidget::EVENT_MOUSE_PUSH)); 
     54    wm->resizeAllWindows(); 
    14155 
    142     lbox1->addWidget(createLabel(INFO)); 
    143     lbox2->addWidget(new Button("Add To Scene...", inputs)); 
     56    viewer.setSceneData(camera); 
    14457 
    145     box->addWidget(lbox1->embed()); 
    146     box->addWidget(table->embed()); 
    147     box->addWidget(lbox2->embed()); 
    148     box->addCallback(osgWidget::Callback(&info, osgWidget::EVENT_MOUSE_PUSH)); 
    149  
    150     frame->setWindow(box); 
    151     frame->getEmbeddedWindow()->setSize(box->getWidth(), box->getHeight()); 
    152     frame->getBackground()->setColor(0.0f, 0.0f, 0.0f, 0.0f); 
    153     frame->attachTabFocusCallback(); 
    154  
    155     for(osgWidget::Frame::Iterator i = frame->begin(); i != frame->end(); i++) { 
    156         if(i->valid()) i->get()->setColor(0.5f, 0.7f, 1.0f, 1.0f); 
    157     } 
    158  
    159     wm->addChild(frame); 
    160  
    161     /* 
    162     // Print out our focus list, it should just have 3 widgets. 
    163     osgWidget::WidgetList wl; 
    164  
    165     box->getFocusList(wl); 
    166  
    167     for(osgWidget::WidgetList::iterator i = wl.begin(); i != wl.end(); i++) { 
    168         osgWidget::warn() << i->get()->getName() << std::endl; 
    169     } 
    170     */ 
    171      
    172     lbox1->getBackground()->setColor(1.0f, 0.0f, 0.0f, 1.0f, osgWidget::Widget::UPPER_LEFT); 
    173     lbox1->getBackground()->setColor(0.0f, 1.0f, 0.0f, 1.0f, osgWidget::Widget::LOWER_LEFT); 
    174     lbox1->getBackground()->setColor(0.0f, 0.0f, 1.0f, 1.0f, osgWidget::Widget::LOWER_RIGHT); 
    175     lbox1->getBackground()->setColor(1.0f, 1.0f, 1.0f, 1.0f, osgWidget::Widget::UPPER_RIGHT); 
    176     lbox1->setVisibilityMode(osgWidget::Window::VM_ENTIRE); 
    177     lbox1->update(); 
    178  
    179     int r = osgWidget::createExample(viewer, wm); 
    180  
    181     // osgWidget::writeWindowManagerNode(wm); 
    182     // osgDB::writeNodeFile(*box, "osgWidget.osg"); 
    183  
    184     return r; 
     58    return viewer.run(); 
    18559}