- Timestamp:
- 07/16/08 00:03:59 (5 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
OpenSceneGraph/trunk/examples/osgwidgetaddremove/osgwidgetaddremove.cpp
r8588 r8600 12 12 class ABCWidget: public osgWidget::Label { 13 13 public: 14 ABCWidget(const std::string& label):15 osgWidget::Label("", label) {16 setFont("fonts/Calibri1.ttf");17 setFontSize(20);18 setCanFill(true);19 setShadow(0.08f);20 addSize(10.0f, 10.0f);21 }14 ABCWidget(const std::string& label): 15 osgWidget::Label("", label) { 16 setFont("fonts/Calibri1.ttf"); 17 setFontSize(20); 18 setCanFill(true); 19 setShadow(0.08f); 20 addSize(10.0f, 10.0f); 21 } 22 22 }; 23 23 24 24 class Button: public osgWidget::Label { 25 25 public: 26 Button(const std::string& label):27 osgWidget::Label("", label) {28 setFont("fonts/Calibri1.ttf");29 setFontSize(30);30 setColor(0.8f, 0.2f, 0.2f, 0.8f);31 setCanFill(true);32 setShadow(0.1f);33 setEventMask(osgWidget::EVENT_MASK_MOUSE_CLICK);34 addSize(20.0f, 20.0f);35 }26 Button(const std::string& label): 27 osgWidget::Label("", label) { 28 setFont("fonts/Calibri1.ttf"); 29 setFontSize(30); 30 setColor(0.8f, 0.2f, 0.2f, 0.8f); 31 setCanFill(true); 32 setShadow(0.1f); 33 setEventMask(osgWidget::EVENT_MASK_MOUSE_CLICK); 34 addSize(20.0f, 20.0f); 35 } 36 36 37 // NOTE! I need to make it clearer than Push/Release can happen so fast that38 // the changes you make aren't visible with your refresh rate. Throttling state39 // changes and what-have-you on mousePush/mouseRelease/etc. is going to be40 // annoying...37 // NOTE! I need to make it clearer than Push/Release can happen so fast that 38 // the changes you make aren't visible with your refresh rate. Throttling state 39 // changes and what-have-you on mousePush/mouseRelease/etc. is going to be 40 // annoying... 41 41 42 virtual bool mousePush(double, double, osgWidget::WindowManager*) {43 addColor(0.2f, 0.2f, 0.2f, 0.0f);44 45 return true;46 }42 virtual bool mousePush(double, double, osgWidget::WindowManager*) { 43 addColor(0.2f, 0.2f, 0.2f, 0.0f); 44 45 return true; 46 } 47 47 48 virtual bool mouseRelease(double, double, osgWidget::WindowManager*) {49 addColor(-0.2f, -0.2f, -0.2f, 0.0f);50 51 return true;52 }48 virtual bool mouseRelease(double, double, osgWidget::WindowManager*) { 49 addColor(-0.2f, -0.2f, -0.2f, 0.0f); 50 51 return true; 52 } 53 53 }; 54 54 55 55 class AddRemove: public osgWidget::Box { 56 osg::ref_ptr<osgWidget::Window> _win1;56 osg::ref_ptr<osgWidget::Window> _win1; 57 57 58 58 public: 59 AddRemove():60 osgWidget::Box ("buttons", osgWidget::Box::VERTICAL),61 _win1 (new osgWidget::Box("win1", osgWidget::Box::VERTICAL)) {62 addWidget(new Button("Add Widget"));63 addWidget(new Button("Remove Widget"));59 AddRemove(): 60 osgWidget::Box ("buttons", osgWidget::Box::VERTICAL), 61 _win1 (new osgWidget::Box("win1", osgWidget::Box::VERTICAL)) { 62 addWidget(new Button("Add Widget")); 63 addWidget(new Button("Remove Widget")); 64 64 65 // Take special note here! Not only do the Button objects have their66 // own overridden methods for changing the color, but they have attached67 // callbacks for doing the work with local data.68 getByName("Widget_1")->addCallback(osgWidget::Callback(69 &AddRemove::handlePressAdd,70 this,71 osgWidget::EVENT_MOUSE_PUSH72 ));65 // Take special note here! Not only do the Button objects have their 66 // own overridden methods for changing the color, but they have attached 67 // callbacks for doing the work with local data. 68 getByName("Widget_1")->addCallback(osgWidget::Callback( 69 &AddRemove::handlePressAdd, 70 this, 71 osgWidget::EVENT_MOUSE_PUSH 72 )); 73 73 74 getByName("Widget_2")->addCallback(osgWidget::Callback(75 &AddRemove::handlePressRemove,76 this,77 osgWidget::EVENT_MOUSE_PUSH78 ));79 }74 getByName("Widget_2")->addCallback(osgWidget::Callback( 75 &AddRemove::handlePressRemove, 76 this, 77 osgWidget::EVENT_MOUSE_PUSH 78 )); 79 } 80 80 81 virtual void managed(osgWidget::WindowManager* wm) {82 osgWidget::Box::managed(wm);81 virtual void managed(osgWidget::WindowManager* wm) { 82 osgWidget::Box::managed(wm); 83 83 84 _win1->setOrigin(250.0f, 0.0f);84 _win1->setOrigin(250.0f, 0.0f); 85 85 86 wm->addChild(_win1.get());87 }86 wm->addChild(_win1.get()); 87 } 88 88 89 bool handlePressAdd(osgWidget::Event& ev) {90 static unsigned int num = 0;89 bool handlePressAdd(osgWidget::Event& ev) { 90 static unsigned int num = 0; 91 91 92 std::stringstream ss;92 std::stringstream ss; 93 93 94 ss << "a random widget " << num;94 ss << "a random widget " << num; 95 95 96 _win1->addWidget(new ABCWidget(ss.str()));97 _win1->resize();96 _win1->addWidget(new ABCWidget(ss.str())); 97 _win1->resize(); 98 98 99 num++;99 num++; 100 100 101 return true;102 }101 return true; 102 } 103 103 104 bool handlePressRemove(osgWidget::Event& ev) {105 // TODO: Temporary hack!106 const osgWidget::Box::Vector& v = _win1->getObjects();107 108 if(!v.size()) return false;104 bool handlePressRemove(osgWidget::Event& ev) { 105 // TODO: Temporary hack! 106 const osgWidget::Box::Vector& v = _win1->getObjects(); 107 108 if(!v.size()) return false; 109 109 110 osgWidget::Widget* w = _win1->getObjects()[v.size() - 1].get();110 osgWidget::Widget* w = _win1->getObjects()[v.size() - 1].get(); 111 111 112 _win1->removeWidget(w);113 _win1->resize();112 _win1->removeWidget(w); 113 _win1->resize(); 114 114 115 return true;116 }115 return true; 116 } 117 117 }; 118 118 119 119 int main(int argc, char** argv) { 120 osgViewer::Viewer viewer;120 osgViewer::Viewer viewer; 121 121 122 osgWidget::WindowManager* wm = new osgWidget::WindowManager(123 &viewer,124 1280.0f,125 1024.0f,126 MASK_2D127 );128 129 osgWidget::Box* buttons = new AddRemove();122 osgWidget::WindowManager* wm = new osgWidget::WindowManager( 123 &viewer, 124 1280.0f, 125 1024.0f, 126 MASK_2D 127 ); 128 129 osgWidget::Box* buttons = new AddRemove(); 130 130 131 wm->addChild(buttons);131 wm->addChild(buttons); 132 132 133 return createExample(viewer, wm);133 return createExample(viewer, wm); 134 134 }
