| [8588] | 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: |
|---|
| [8600] | 14 | ABCWidget(const std::string& label): |
|---|
| 15 | osgWidget::Label("", label) { |
|---|
| [8602] | 16 | setFont("fonts/Vera.ttf"); |
|---|
| [8600] | 17 | setFontSize(20); |
|---|
| 18 | setCanFill(true); |
|---|
| 19 | setShadow(0.08f); |
|---|
| 20 | addSize(10.0f, 10.0f); |
|---|
| 21 | } |
|---|
| [8588] | 22 | }; |
|---|
| 23 | |
|---|
| 24 | class Button: public osgWidget::Label { |
|---|
| 25 | public: |
|---|
| [8600] | 26 | Button(const std::string& label): |
|---|
| 27 | osgWidget::Label("", label) { |
|---|
| [8602] | 28 | setFont("fonts/Vera.ttf"); |
|---|
| [8600] | 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 | } |
|---|
| [8588] | 36 | |
|---|
| [8600] | 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| [8588] | 41 | |
|---|
| [12310] | 42 | virtual bool mousePush(double, double, const osgWidget::WindowManager*) { |
|---|
| [8600] | 43 | addColor(0.2f, 0.2f, 0.2f, 0.0f); |
|---|
| 44 | |
|---|
| 45 | return true; |
|---|
| 46 | } |
|---|
| [8588] | 47 | |
|---|
| [12310] | 48 | virtual bool mouseRelease(double, double, const osgWidget::WindowManager*) { |
|---|
| [8600] | 49 | addColor(-0.2f, -0.2f, -0.2f, 0.0f); |
|---|
| 50 | |
|---|
| 51 | return true; |
|---|
| 52 | } |
|---|
| [8588] | 53 | }; |
|---|
| 54 | |
|---|
| 55 | class AddRemove: public osgWidget::Box { |
|---|
| [8600] | 56 | osg::ref_ptr<osgWidget::Window> _win1; |
|---|
| [8588] | 57 | |
|---|
| 58 | public: |
|---|
| [8600] | 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")); |
|---|
| [8588] | 64 | |
|---|
| [8600] | 65 | |
|---|
| 66 | |
|---|
| 67 | |
|---|
| [9289] | 68 | getByName("Widget_1")->addCallback(new osgWidget::Callback( |
|---|
| [8600] | 69 | &AddRemove::handlePressAdd, |
|---|
| 70 | this, |
|---|
| 71 | osgWidget::EVENT_MOUSE_PUSH |
|---|
| 72 | )); |
|---|
| [8588] | 73 | |
|---|
| [9289] | 74 | getByName("Widget_2")->addCallback(new osgWidget::Callback( |
|---|
| [8600] | 75 | &AddRemove::handlePressRemove, |
|---|
| 76 | this, |
|---|
| 77 | osgWidget::EVENT_MOUSE_PUSH |
|---|
| 78 | )); |
|---|
| 79 | } |
|---|
| [8588] | 80 | |
|---|
| [8600] | 81 | virtual void managed(osgWidget::WindowManager* wm) { |
|---|
| 82 | osgWidget::Box::managed(wm); |
|---|
| [8588] | 83 | |
|---|
| [8600] | 84 | _win1->setOrigin(250.0f, 0.0f); |
|---|
| [8588] | 85 | |
|---|
| [8600] | 86 | wm->addChild(_win1.get()); |
|---|
| 87 | } |
|---|
| [8588] | 88 | |
|---|
| [8600] | 89 | bool handlePressAdd(osgWidget::Event& ev) { |
|---|
| 90 | static unsigned int num = 0; |
|---|
| [8588] | 91 | |
|---|
| [8600] | 92 | std::stringstream ss; |
|---|
| [8588] | 93 | |
|---|
| [8600] | 94 | ss << "a random widget " << num; |
|---|
| [8588] | 95 | |
|---|
| [8600] | 96 | _win1->addWidget(new ABCWidget(ss.str())); |
|---|
| [8588] | 97 | |
|---|
| [8600] | 98 | num++; |
|---|
| [8588] | 99 | |
|---|
| [8600] | 100 | return true; |
|---|
| 101 | } |
|---|
| [8588] | 102 | |
|---|
| [8600] | 103 | bool handlePressRemove(osgWidget::Event& ev) { |
|---|
| 104 | |
|---|
| 105 | const osgWidget::Box::Vector& v = _win1->getObjects(); |
|---|
| 106 | |
|---|
| 107 | if(!v.size()) return false; |
|---|
| [8588] | 108 | |
|---|
| [8600] | 109 | osgWidget::Widget* w = _win1->getObjects()[v.size() - 1].get(); |
|---|
| [8588] | 110 | |
|---|
| [8600] | 111 | _win1->removeWidget(w); |
|---|
| [8588] | 112 | |
|---|
| [8600] | 113 | return true; |
|---|
| 114 | } |
|---|
| [8588] | 115 | }; |
|---|
| 116 | |
|---|
| 117 | int main(int argc, char** argv) { |
|---|
| [8600] | 118 | osgViewer::Viewer viewer; |
|---|
| [8588] | 119 | |
|---|
| [8600] | 120 | osgWidget::WindowManager* wm = new osgWidget::WindowManager( |
|---|
| 121 | &viewer, |
|---|
| 122 | 1280.0f, |
|---|
| 123 | 1024.0f, |
|---|
| 124 | MASK_2D |
|---|
| 125 | ); |
|---|
| 126 | |
|---|
| 127 | osgWidget::Box* buttons = new AddRemove(); |
|---|
| [8588] | 128 | |
|---|
| [8600] | 129 | wm->addChild(buttons); |
|---|
| [8588] | 130 | |
|---|
| [8600] | 131 | return createExample(viewer, wm); |
|---|
| [8588] | 132 | } |
|---|