2008-07-16 01:21:25 +08:00
|
|
|
// -*-c++-*- osgWidget - Code by: Jeremy Moles (cubicool) 2007-2008
|
|
|
|
// $Id: osgwidgetbox.cpp 59 2008-05-15 20:55:31Z cubicool $
|
|
|
|
|
|
|
|
// NOTE: You'll find this example very similar to osgwidgetwindow. However, here we
|
|
|
|
// demonstrate a bit of subclassing of Widget so that we can respond to events
|
|
|
|
// such as mouseEnter and mouseLeave. We also demonstrate the use of padding, though
|
|
|
|
// fill and alignment should be working too.
|
|
|
|
|
|
|
|
#include <osg/io_utils>
|
|
|
|
#include <osgDB/ReadFile>
|
|
|
|
#include <osgWidget/Util>
|
|
|
|
#include <osgWidget/WindowManager>
|
|
|
|
#include <osgWidget/Box>
|
|
|
|
|
|
|
|
const unsigned int MASK_2D = 0xF0000000;
|
|
|
|
const unsigned int MASK_3D = 0x0F000000;
|
|
|
|
|
|
|
|
struct ColorWidget: public osgWidget::Widget {
|
2008-12-17 04:29:00 +08:00
|
|
|
ColorWidget():
|
|
|
|
osgWidget::Widget("", 256.0f, 256.0f) {
|
2009-11-19 19:30:22 +08:00
|
|
|
setEventMask(osgWidget::EVENT_ALL);
|
2008-12-17 04:29:00 +08:00
|
|
|
}
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2009-11-19 19:30:22 +08:00
|
|
|
bool mouseEnter(double, double, const osgWidget::WindowManager*) {
|
2008-12-17 04:29:00 +08:00
|
|
|
addColor(-osgWidget::Color(0.4f, 0.4f, 0.4f, 0.0f));
|
2015-10-22 22:14:53 +08:00
|
|
|
|
2008-12-17 04:29:00 +08:00
|
|
|
// osgWidget::warn() << "enter: " << getColor() << std::endl;
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-12-17 04:29:00 +08:00
|
|
|
return true;
|
|
|
|
}
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2009-11-19 19:30:22 +08:00
|
|
|
bool mouseLeave(double, double, const osgWidget::WindowManager*) {
|
2008-12-17 04:29:00 +08:00
|
|
|
addColor(osgWidget::Color(0.4f, 0.4f, 0.4f, 0.0f));
|
2015-10-22 22:14:53 +08:00
|
|
|
|
2008-12-17 04:29:00 +08:00
|
|
|
// osgWidget::warn() << "leave: " << getColor() << std::endl;
|
2015-10-22 22:14:53 +08:00
|
|
|
|
2008-12-17 04:29:00 +08:00
|
|
|
return true;
|
|
|
|
}
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2009-11-19 19:30:22 +08:00
|
|
|
bool mouseOver(double x, double y, const osgWidget::WindowManager*) {
|
2015-10-22 22:14:53 +08:00
|
|
|
|
2008-12-17 04:29:00 +08:00
|
|
|
osgWidget::Color c = getImageColorAtPointerXY(x, y);
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-12-17 04:29:00 +08:00
|
|
|
if(c.a() < 0.001f) {
|
|
|
|
// osgWidget::warn() << "Transparent Pixel: " << x << " " << y << std::endl;
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-12-17 04:29:00 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-12-17 04:29:00 +08:00
|
|
|
bool keyUp(int key, int keyMask, osgWidget::WindowManager*) {
|
|
|
|
// osgWidget::warn() << "..." << key << " - " << keyMask << std::endl;
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-12-17 04:29:00 +08:00
|
|
|
return true;
|
|
|
|
}
|
2008-07-16 01:21:25 +08:00
|
|
|
};
|
|
|
|
|
2008-11-28 22:36:39 +08:00
|
|
|
osgWidget::Box* createBox(const std::string& name, osgWidget::Box::BoxType bt) {
|
2008-12-17 04:29:00 +08:00
|
|
|
osgWidget::Box* box = new osgWidget::Box(name, bt, true);
|
|
|
|
osgWidget::Widget* widget1 = new osgWidget::Widget(name + "_widget1", 100.0f, 100.0f);
|
|
|
|
osgWidget::Widget* widget2 = new osgWidget::Widget(name + "_widget2", 100.0f, 100.0f);
|
|
|
|
osgWidget::Widget* widget3 = new ColorWidget();
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-12-17 04:29:00 +08:00
|
|
|
widget1->setColor(0.3f, 0.3f, 0.3f, 1.0f);
|
|
|
|
widget2->setColor(0.6f, 0.6f, 0.6f, 1.0f);
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-12-17 04:29:00 +08:00
|
|
|
widget3->setImage("osgWidget/natascha.png");
|
|
|
|
widget3->setTexCoord(0.0f, 0.0f, osgWidget::Widget::LOWER_LEFT);
|
|
|
|
widget3->setTexCoord(1.0f, 0.0f, osgWidget::Widget::LOWER_RIGHT);
|
|
|
|
widget3->setTexCoord(1.0f, 1.0f, osgWidget::Widget::UPPER_RIGHT);
|
|
|
|
widget3->setTexCoord(0.0f, 1.0f, osgWidget::Widget::UPPER_LEFT);
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-12-17 04:29:00 +08:00
|
|
|
box->addWidget(widget1);
|
|
|
|
box->addWidget(widget2);
|
|
|
|
box->addWidget(widget3);
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-12-17 04:29:00 +08:00
|
|
|
return box;
|
2008-07-16 01:21:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
2008-12-17 04:29:00 +08:00
|
|
|
osgViewer::Viewer viewer;
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-12-17 04:29:00 +08:00
|
|
|
osgWidget::WindowManager* wm = new osgWidget::WindowManager(
|
|
|
|
&viewer,
|
|
|
|
1280.0f,
|
|
|
|
1024.0f,
|
|
|
|
MASK_2D,
|
|
|
|
osgWidget::WindowManager::WM_PICK_DEBUG
|
|
|
|
);
|
2015-10-22 22:14:53 +08:00
|
|
|
|
2008-12-17 04:29:00 +08:00
|
|
|
wm->setPointerFocusMode(osgWidget::WindowManager::PFM_SLOPPY);
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-12-17 04:29:00 +08:00
|
|
|
osgWidget::Window* box1 = createBox("HBOX", osgWidget::Box::HORIZONTAL);
|
|
|
|
osgWidget::Window* box2 = createBox("VBOX", osgWidget::Box::VERTICAL);
|
|
|
|
osgWidget::Window* box3 = createBox("HBOX2", osgWidget::Box::HORIZONTAL);
|
|
|
|
osgWidget::Window* box4 = createBox("VBOX2", osgWidget::Box::VERTICAL);
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-12-17 04:29:00 +08:00
|
|
|
box1->getBackground()->setColor(1.0f, 0.0f, 0.0f, 0.8f);
|
|
|
|
box1->attachMoveCallback();
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-12-17 04:29:00 +08:00
|
|
|
box2->getBackground()->setColor(0.0f, 1.0f, 0.0f, 0.8f);
|
|
|
|
box2->attachMoveCallback();
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-12-17 04:29:00 +08:00
|
|
|
box3->getBackground()->setColor(0.0f, 0.0f, 1.0f, 0.8f);
|
|
|
|
box3->attachMoveCallback();
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-12-17 04:29:00 +08:00
|
|
|
wm->addChild(box1);
|
|
|
|
wm->addChild(box2);
|
|
|
|
wm->addChild(box3);
|
|
|
|
wm->addChild(box4);
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-12-17 04:29:00 +08:00
|
|
|
box4->hide();
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2015-10-22 22:14:53 +08:00
|
|
|
osg::ref_ptr<osg::Node> model = osgDB::readRefNodeFile("spaceship.osgt");
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-12-17 04:29:00 +08:00
|
|
|
model->setNodeMask(MASK_3D);
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2015-10-22 22:14:53 +08:00
|
|
|
return osgWidget::createExample(viewer, wm, model.get());
|
2008-07-16 01:21:25 +08:00
|
|
|
}
|