Esempio n. 1
0
void Sketch::menuCallback(MenuItem * item)
{
    /*if(item == _modeButtons)
    {
    finishGeometry();
    removeMenuItems(_mode);
    _mode = (DrawMode)_modeButtons->firstNumOn();
    _brushRoot->removeChildren(0,_brushRoot->getNumChildren());
    if(_mode >= 0)
    {
        _brushRoot->addChild(_brushes[_mode]);
    }

    _sizeRV->setValue(1.0);
    addMenuItems(_mode);
    }
    else if(item == _sizeRV)
    {
    }
    else if(item == _lineType)
    {
    finishGeometry();
    _lt = (LineType)_lineType->firstNumOn();
    }
    else if(item == _shapeType)
    {
    finishGeometry();
    _st = (ShapeType)_shapeType->firstNumOn();
    }*/

    if(item == _modeButtons)
    {
        finishGeometry();
        removeMenuItems(_mode);
        _mode = (DrawMode)_modeButtons->firstNumOn();
        //_brushRoot->removeChildren(0,_brushRoot->getNumChildren());
        //if(_mode >= 0)
        //{
        //    _brushRoot->addChild(_brushes[_mode]);
        //}

        _sizeRV->setValue(1.0);
        addMenuItems(_mode);
        createGeometry();
    }
    else if(item == _sizeRV)
    {
        if(_activeObject)
        {
            _activeObject->setSize(_sizeRV->getValue());
        }
    }
    else if(item == _lineType)
    {
        finishGeometry();
        _lt = (SketchLine::LineType)_lineType->firstNumOn();
        createGeometry();
    }
    else if(item == _lineTube)
    {
        SketchLine * line = dynamic_cast<SketchLine*>(_activeObject);
        if(line)
        {
            line->setTube(_lineTube->getValue());
        }
    }
    else if(item == _lineSnap)
    {
        SketchLine * line = dynamic_cast<SketchLine*>(_activeObject);
        if(line)
        {
            line->setSnap(_lineSnap->getValue());
        }
    }
    else if(item == _shapeType)
    {
        finishGeometry();
        _st = (ShapeType)_shapeType->firstNumOn();
        createGeometry();
    }
    else if(item == _saveButton)
    {
        osgDB::writeNodeFile(*_sketchRoot.get(), "/home/aprudhom/ribbontest.obj");
    }
    else if(item == _csCB)
    {
        _colorSelector->setVisible(_csCB->getValue());
    }
}
Esempio n. 2
0
void Sketch::menuCallback(MenuItem * item)
{
    if(item == _modeButtons)
    {
        finishGeometry();
        removeMenuItems(_mode);
        preFrame();
        _mode = (Mode)_modeButtons->firstNumOn();

        _sizeRV->setValue(1.0);
        addMenuItems(_mode);

        if (_mode == MOVE || _mode == SELECT)
        {
            _moveBrushGeode = new osg::Geode();
            _moveBrushGeode->addDrawable(_moveBrushDrawable);
            _brushRoot->addChild(_moveBrushGeode);
        }
        else
        {
            _brushRoot->removeChild(_moveBrushGeode);
        }
    } 

    else if (item == _drawModeButtons)
    {
        finishGeometry();
        removeMenuItems(_drawMode);
        _drawMode = (DrawMode)_drawModeButtons->firstNumOn(); 
        addMenuItems(_drawMode);

        // Only draw shapes and layouts when type is picked
        if (_drawMode != SHAPE && _drawMode != LAYOUT)
        {
            createGeometry();
        }
    }

    else if (item == _shapeType)
    {
        finishGeometry();
        _st = (SketchShape::ShapeType)_shapeType->firstNumOn();
        createGeometry();
    }

    else if (item == _layoutType)
    {
        finishGeometry();
        _st = (SketchShape::ShapeType)(_layoutType->firstNumOn() + 4);
        _lot = (Layout::LayoutType)(_layoutType->firstNumOn());
        createGeometry();
    }

    else if (item == _csCB)
    {
        _colorSelector->setVisible(_csCB->getValue());
    }

    else if (item == _freezeCB)
    {
        if(_freezeCB->getValue()) // Object -> Scene
        {
            _sketchRoot->postMult(PluginHelper::getObjectToWorldTransform());
            _lastPoint = PluginHelper::getObjectToWorldTransform() * _lastPoint;

            PluginHelper::getObjectsRoot()->removeChild(_sketchRoot);
            PluginHelper::getScene()->addChild(_sketchRoot);
            _isObjectRoot = false;
        }
        else // Scene -> Object
        {
            _sketchRoot->postMult(PluginHelper::getWorldToObjectTransform());
            _lastPoint = PluginHelper::getWorldToObjectTransform() * _lastPoint;

            PluginHelper::getScene()->removeChild(_sketchRoot);
            PluginHelper::getObjectsRoot()->addChild(_sketchRoot);
            _isObjectRoot = true;
        }
    }

    else if (item == _snapToGridCB)
    {
    }

    else if (item == _orientToViewerCB)
    {
    }

    else if (item == _sizeRV)
    {
        if (_mode == DRAW)
        {
            if(_activeObject)
            {
                _activeObject->setSize(_sizeRV->getValue());
            }
            if (_highlightPat)
            {
                _highlightPat->setScale(osg::Vec3(_sizeRV->getValue(),
                                                  _sizeRV->getValue(),
                                                  _sizeRV->getValue()));
            }
        }
        else if (_mode == SELECT)
        {
            float s = _sizeRV->getValue();
            vector<PositionAttitudeTransform *>::iterator it;
            bool isLayout;

            for (it = _movingList.begin(); it != _movingList.end(); ++it)
            {
                isLayout = false;

                // layouts do not get uniformly scaled
                for (int i = 0; i < _layoutList.size(); ++i)
                {
                    if ((*it) == _layoutList[i]->getPat())
                    {
                        _layoutList[i]->scaleMajorRadius(s);
                        isLayout = true;
                    }
                }
                if (!isLayout)
                {
                    (*it)->setScale(osg::Vec3(s,s,s));
                }
            }
        }
    }

    else if (item == _selectAllButton)
    {
        _movingList.clear();
        for (int i = 0; i < _shapeList.size(); ++i)
        {
            _movingList.push_back(_shapeList[i]->getPat());
            _shapeList[i]->highlight();
        }
    }
    
    else if (item == _clearSelectButton)
    {
        _movingList.clear();
    }

    else if (item == _tessellationsRV)
    {
        float t = _tessellationsRV->getValue();
        int tes = (int)floor(t);
        SketchShape * shape;
        for (int i = 0; i < _objectList.size(); ++i)
        {
            shape = dynamic_cast<SketchShape *>(_objectList[i]);
            if (shape)
            {
                shape->setTessellations(tes);
            }
        }
    }

    else if (item == _lineType)
    {
        finishGeometry();
        _lt = (SketchLine::LineType)_lineType->firstNumOn();
        createGeometry();
    }

    else if (item == _lineTube)
    {
        SketchLine * line = dynamic_cast<SketchLine*>(_activeObject);
        if(line)
        {
            line->setTube(_lineTube->getValue());
        }
    }

    else if (item == _lineSnap)
    {
        SketchLine * line = dynamic_cast<SketchLine*>(_activeObject);
        if(line)
        {
            line->setSnap(_lineSnap->getValue());
        }
    }

    else if (item == _showLayoutCB)
    {
        for (int i = 0; i < _layoutList.size(); ++i)
        {
            if (_showLayoutCB->getValue())
            {
                _layoutList[i]->show();
            }
            else
            {
                _layoutList[i]->hide();
            }
        }
    }

    else if (item == _layoutSizeRV)
    {
        for (int i = 0; i < _layoutList.size(); ++i)
        {
            _layoutList[i]->scaleMajorRadius(_layoutSizeRV->getValue());
        }
    }

    else if (item == _shapeWireframe)
    {
        SketchShape * shape = dynamic_cast<SketchShape*>(_activeObject);
        if(shape)
        {
            shape->setWireframe(_shapeWireframe->getValue());
        }
    }


    else if (item == _transparentHLCB)
    {
        SketchShape::setTransparentHighlight(_transparentHLCB->getValue());    
    }

    else if (item == _textHLCB)
    {
         SketchShape::setTextHighlight(_textHLCB->getValue());       
    }

    else if (item == _boldHLCB)
    {
        SketchShape::setBoldHighlight(_boldHLCB->getValue());
    }

    else if (item == _pulsatingHLCB)
    {
        SketchShape::setPulsatingHighlight(_pulsatingHLCB->getValue());
    }


    else if (item == _saveButton)
    {
        std::string filename;
        size_t pos;
        string sub;
        int i, max = 0;
        
        // traverses directory searching for files like sketch-005.osg
        if (DIR *dir = opendir(_dataDir.c_str()))
        {
            while (struct dirent *entry = readdir(dir))
            {
                filename = entry->d_name;
                // sketch-005.osg
                pos = filename.rfind("-");

                // 005.osg
                sub = filename.substr(pos + 1, filename.size() - pos);

                pos = filename.rfind(".");
                // 005
                sub = sub.substr(0, filename.size() - pos + 1);

                i = atoi(sub.c_str());

                if (i != 0 && i > max)
                {
                    max = i; 
                }
            }

            char buf [10];
            sprintf(buf, "%03d", ++max);
            filename = _dataDir + "sketch-" + buf + ".osg";
            std::cout << "Saving " << filename << std::endl;
            osgDB::writeNodeFile(*_sketchRoot.get(), filename);
            closedir(dir);
        }
    }

    else if (item == _clearButton)
    {
        _sketchRoot->removeChildren(0, _sketchRoot->getNumChildren());
        for (int i = 0; i < _objectList.size(); ++i)
        {
            delete _objectList[i];
        }
        _objectList.clear();
        _shapeList.clear();
        _layoutList.clear();

        _patList.clear();
        _movingList.clear();

        _sketchGeode = new osg::Geode();
        _sketchRoot->addChild(_sketchGeode);

        finishGeometry();
        createGeometry();
    }

    else if (item == _loadMenu)
    {
        cvr::MenuButton * button;
        std::string filename;

        size_t pos;
        string sub;
        
        vector<std::string> addList;

        if (DIR *dir = opendir(_dataDir.c_str()))
        {
            while (struct dirent *entry = readdir(dir))
            {
                filename = entry->d_name; 
                if (strcmp(entry->d_name, ".") && strcmp(entry->d_name, ".."))
                {
                    pos = filename.rfind(".");
                    sub = filename.substr(pos + 1, filename.size() - pos);

                    if (!strcmp(sub.c_str(), "osg"))
                    {
                        bool found = false;
                        for (vector<std::string>::iterator it = _loadFileList.begin();
                            it != _loadFileList.end(); ++it)
                        {
                            if ((*it).compare(filename))
                            {
                                found = true;
                                break;
                            }
                        }

                        if (!found)
                        {
                            addList.push_back(filename);
                        }
                    }
                }
            }
            closedir(dir);
        }

        sort(_loadFileList.begin(), _loadFileList.end());
        sort(addList.begin(), addList.end());       

        for (int i = 0; i < addList.size(); ++i)
        {
            button = new MenuButton(addList[i]);
            button->setCallback(this);
            _loadFileButtons.push_back(button);
            _loadMenu->addItem(button);
            _loadFileList.push_back(_dataDir + addList[i]);
        }
    }

    for (int i = 0; i < _loadFileButtons.size(); ++i)
    {
        if (item == _loadMenu->getChild(i))
        {
            std::cout << "Loading " << _loadFileList[i] << std::endl;
            osg::Node * node = osgDB::readNodeFile(_loadFileList[i]);

            if (node == NULL)
            {
                std::cout << "Error loading file." << std::endl;
                return;
            }
            _sketchRoot->removeChildren(0, _sketchRoot->getNumChildren());
            _sketchRoot->addChild(node);
            
            // get all children of root that are PATs, for shape objects
            osg::MatrixTransform * mat = dynamic_cast<MatrixTransform *>(node);
            if (mat)
            {
                for (int i = 0; i < mat->getNumChildren(); ++i)
                {
                    osg::PositionAttitudeTransform * pat = 
                        dynamic_cast<PositionAttitudeTransform*>(mat->getChild(i));
                    if (pat)
                    {
                        _patList.push_back(pat);
                    }
                } 
            }
        }
    }
}
Esempio n. 3
0
void StateIdle::MouseButton(int button, int state, int x, int y) {
    if (button == Mouse::MOUSE_BUTTON_SCROLL) {
        mCamera->setCamDist(mCamera->distance + 0.1f * state);
    }
    if (Controller::bMoveCenterMode) {
        return;
    }
    if (button == Mouse::MOUSE_BUTTON_LEFT) {
        std::vector<Vector3> selectedPoints;
        if (state == Mouse::MOUSE_ACTION_DOWN) {
            internalState = INTERNAL_STATE_MOUSE_DOWN;
            Vector3 v;
            mCamera->getPoint(x, y, SketchLine::getGlobalLineSegments(), v);

            if (!Controller::bCurrLine) {
                selectedPoints.push_back(v);
                Plane::buildPlane(selectedPoints, Controller::currPlane,
                                  Vector3(0, 1, 0));
                if (Controller::currPlane.N.dot(mCamera->getDirection()) > 0) {
                    Controller::currPlane = -Controller::currPlane;
                }
                dynamic_cast<StateDraw*>(State::statePool[STATE_DRAW])
                    ->selectedPoints = selectedPoints;
                dynamic_cast<StateDraw*>(State::statePool[STATE_DRAW])
                    ->startPoint = v;
                dynamic_cast<StateDraw*>(State::statePool[STATE_DRAW])
                    ->endPoint = v;
                dynamic_cast<StateDraw*>(State::statePool[STATE_DRAW])
                    ->internalState =
                    StateDraw::STATE_DRAW_START_POINT_SELECTED;
                enterState(State::statePool[STATE_DRAW]);
                internalState = INTERNAL_STATE_IDLE;
            } else {
                selectedPoint = v;
            }
        } else if (state == Mouse::MOUSE_ACTION_UP) {
            if (Controller::bCurrLine) {
                SketchLine* sketchLine =
                    SketchLine::lineSegmentToSkectLine(Controller::currLine.ID);
                if (sketchLine == NULL) {
                    return;
                }
                unsigned long size = sketchLine->getLineSegments().size();
                selectedPoints.push_back(
                    sketchLine->getLineSegments()[0].points[0]);
                selectedPoints.push_back(
                    sketchLine->getLineSegments()[size - 1].points[1]);
                Plane::buildPlane(selectedPoints, Controller::currPlane,
                                  Vector3(0, 0, 1));
                if (Controller::currPlane.N.dot(mCamera->getDirection()) > 0) {
                    Controller::currPlane = -Controller::currPlane;
                }
                Quaternion q = Quaternion::fromVector(Controller::currPlane.N,
                                                      Quaternion::Z_NEG_AXIS);
                mCamera->rotateCamTo(q);
                mCamera->setCamCenterTo((Controller::currLine.points[0] +
                                         Controller::currLine.points[1]) /
                                        2.0f);
                dynamic_cast<StateDraw*>(State::statePool[STATE_DRAW])
                    ->selectedPoints = selectedPoints;
                enterState(State::statePool[STATE_DRAW]);
                internalState = INTERNAL_STATE_IDLE;
            }
        }
    }
}