| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | #include <osgWidget/Util> |
|---|
| 5 | #include <osgWidget/WindowManager> |
|---|
| 6 | #include <osgWidget/Table> |
|---|
| 7 | #include <osgWidget/Box> |
|---|
| 8 | #include <osgWidget/Label> |
|---|
| 9 | |
|---|
| 10 | const unsigned int MASK_2D = 0xF0000000; |
|---|
| 11 | |
|---|
| 12 | class ABCWidget: public osgWidget::Label { |
|---|
| 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 | } |
|---|
| 22 | }; |
|---|
| 23 | |
|---|
| 24 | class Button: public osgWidget::Label { |
|---|
| 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 | } |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | virtual bool mousePush(double, double, osgWidget::WindowManager*) { |
|---|
| 43 | addColor(0.2f, 0.2f, 0.2f, 0.0f); |
|---|
| 44 | |
|---|
| 45 | return true; |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | virtual bool mouseRelease(double, double, osgWidget::WindowManager*) { |
|---|
| 49 | addColor(-0.2f, -0.2f, -0.2f, 0.0f); |
|---|
| 50 | |
|---|
| 51 | return true; |
|---|
| 52 | } |
|---|
| 53 | }; |
|---|
| 54 | |
|---|
| 55 | class AddRemove: public osgWidget::Box { |
|---|
| 56 | osg::ref_ptr<osgWidget::Window> _win1; |
|---|
| 57 | |
|---|
| 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")); |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | getByName("Widget_1")->addCallback(osgWidget::Callback( |
|---|
| 69 | &AddRemove::handlePressAdd, |
|---|
| 70 | this, |
|---|
| 71 | osgWidget::EVENT_MOUSE_PUSH |
|---|
| 72 | )); |
|---|
| 73 | |
|---|
| 74 | getByName("Widget_2")->addCallback(osgWidget::Callback( |
|---|
| 75 | &AddRemove::handlePressRemove, |
|---|
| 76 | this, |
|---|
| 77 | osgWidget::EVENT_MOUSE_PUSH |
|---|
| 78 | )); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | virtual void managed(osgWidget::WindowManager* wm) { |
|---|
| 82 | osgWidget::Box::managed(wm); |
|---|
| 83 | |
|---|
| 84 | _win1->setOrigin(250.0f, 0.0f); |
|---|
| 85 | |
|---|
| 86 | wm->addChild(_win1.get()); |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | bool handlePressAdd(osgWidget::Event& ev) { |
|---|
| 90 | static unsigned int num = 0; |
|---|
| 91 | |
|---|
| 92 | std::stringstream ss; |
|---|
| 93 | |
|---|
| 94 | ss << "a random widget " << num; |
|---|
| 95 | |
|---|
| 96 | _win1->addWidget(new ABCWidget(ss.str())); |
|---|
| 97 | _win1->resize(); |
|---|
| 98 | |
|---|
| 99 | num++; |
|---|
| 100 | |
|---|
| 101 | return true; |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | bool handlePressRemove(osgWidget::Event& ev) { |
|---|
| 105 | |
|---|
| 106 | const osgWidget::Box::Vector& v = _win1->getObjects(); |
|---|
| 107 | |
|---|
| 108 | if(!v.size()) return false; |
|---|
| 109 | |
|---|
| 110 | osgWidget::Widget* w = _win1->getObjects()[v.size() - 1].get(); |
|---|
| 111 | |
|---|
| 112 | _win1->removeWidget(w); |
|---|
| 113 | _win1->resize(); |
|---|
| 114 | |
|---|
| 115 | return true; |
|---|
| 116 | } |
|---|
| 117 | }; |
|---|
| 118 | |
|---|
| 119 | int main(int argc, char** argv) { |
|---|
| 120 | osgViewer::Viewer viewer; |
|---|
| 121 | |
|---|
| 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 | |
|---|
| 131 | wm->addChild(buttons); |
|---|
| 132 | |
|---|
| 133 | return createExample(viewer, wm); |
|---|
| 134 | } |
|---|