OpenSceneGraph/examples/osgwidgetaddremove/osgwidgetaddremove.cpp

135 lines
3.5 KiB
C++
Raw Normal View History

// -*-c++-*- osgWidget - Code by: Jeremy Moles (cubicool) 2007-2008
// $Id: osgwidgetaddremove.cpp 45 2008-04-23 16:46:11Z cubicool $
#include <osgWidget/Util>
#include <osgWidget/WindowManager>
#include <osgWidget/Table>
#include <osgWidget/Box>
#include <osgWidget/Label>
const unsigned int MASK_2D = 0xF0000000;
class ABCWidget: public osgWidget::Label {
public:
2008-07-16 06:03:59 +08:00
ABCWidget(const std::string& label):
osgWidget::Label("", label) {
setFont("fonts/Vera.ttf");
2008-07-16 06:03:59 +08:00
setFontSize(20);
setCanFill(true);
setShadow(0.08f);
addSize(10.0f, 10.0f);
}
};
class Button: public osgWidget::Label {
public:
2008-07-16 06:03:59 +08:00
Button(const std::string& label):
osgWidget::Label("", label) {
setFont("fonts/Vera.ttf");
2008-07-16 06:03:59 +08:00
setFontSize(30);
setColor(0.8f, 0.2f, 0.2f, 0.8f);
setCanFill(true);
setShadow(0.1f);
setEventMask(osgWidget::EVENT_MASK_MOUSE_CLICK);
addSize(20.0f, 20.0f);
}
// NOTE! I need to make it clearer than Push/Release can happen so fast that
// the changes you make aren't visible with your refresh rate. Throttling state
// changes and what-have-you on mousePush/mouseRelease/etc. is going to be
// annoying...
virtual bool mousePush(double, double, osgWidget::WindowManager*) {
addColor(0.2f, 0.2f, 0.2f, 0.0f);
return true;
}
virtual bool mouseRelease(double, double, osgWidget::WindowManager*) {
addColor(-0.2f, -0.2f, -0.2f, 0.0f);
return true;
}
};
class AddRemove: public osgWidget::Box {
2008-07-16 06:03:59 +08:00
osg::ref_ptr<osgWidget::Window> _win1;
public:
2008-07-16 06:03:59 +08:00
AddRemove():
osgWidget::Box ("buttons", osgWidget::Box::VERTICAL),
_win1 (new osgWidget::Box("win1", osgWidget::Box::VERTICAL)) {
addWidget(new Button("Add Widget"));
addWidget(new Button("Remove Widget"));
2008-07-16 06:03:59 +08:00
// Take special note here! Not only do the Button objects have their
// own overridden methods for changing the color, but they have attached
// callbacks for doing the work with local data.
getByName("Widget_1")->addCallback(osgWidget::Callback(
&AddRemove::handlePressAdd,
this,
osgWidget::EVENT_MOUSE_PUSH
));
2008-07-16 06:03:59 +08:00
getByName("Widget_2")->addCallback(osgWidget::Callback(
&AddRemove::handlePressRemove,
this,
osgWidget::EVENT_MOUSE_PUSH
));
}
2008-07-16 06:03:59 +08:00
virtual void managed(osgWidget::WindowManager* wm) {
osgWidget::Box::managed(wm);
2008-07-16 06:03:59 +08:00
_win1->setOrigin(250.0f, 0.0f);
2008-07-16 06:03:59 +08:00
wm->addChild(_win1.get());
}
2008-07-16 06:03:59 +08:00
bool handlePressAdd(osgWidget::Event& ev) {
static unsigned int num = 0;
2008-07-16 06:03:59 +08:00
std::stringstream ss;
2008-07-16 06:03:59 +08:00
ss << "a random widget " << num;
2008-07-16 06:03:59 +08:00
_win1->addWidget(new ABCWidget(ss.str()));
_win1->resize();
2008-07-16 06:03:59 +08:00
num++;
2008-07-16 06:03:59 +08:00
return true;
}
2008-07-16 06:03:59 +08:00
bool handlePressRemove(osgWidget::Event& ev) {
// TODO: Temporary hack!
const osgWidget::Box::Vector& v = _win1->getObjects();
if(!v.size()) return false;
2008-07-16 06:03:59 +08:00
osgWidget::Widget* w = _win1->getObjects()[v.size() - 1].get();
2008-07-16 06:03:59 +08:00
_win1->removeWidget(w);
_win1->resize();
2008-07-16 06:03:59 +08:00
return true;
}
};
int main(int argc, char** argv) {
2008-07-16 06:03:59 +08:00
osgViewer::Viewer viewer;
2008-07-16 06:03:59 +08:00
osgWidget::WindowManager* wm = new osgWidget::WindowManager(
&viewer,
1280.0f,
1024.0f,
MASK_2D
);
osgWidget::Box* buttons = new AddRemove();
2008-07-16 06:03:59 +08:00
wm->addChild(buttons);
2008-07-16 06:03:59 +08:00
return createExample(viewer, wm);
}