WMPrototypeBoxManipulation.h 4.98 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------

#ifndef WMPROTOTYPEBOXMANIPULATION_H
#define WMPROTOTYPEBOXMANIPULATION_H

#include <map>
#include <string>
#include <vector>

#include <osg/Node>
#include <osg/Geode>
#include <osg/Uniform>

#include "../../kernel/WModule.h"
#include "../../kernel/WModuleInputData.h"

39
#include "../../math/WVector3D.h"
40

41 42
class WPickHandler;

43 44
/**
 * Prototype module
45
 * \ingroup modules
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
 */
class WMPrototypeBoxManipulation : public WModule
{
public:
    /**
     * Standard constructor.
     */
    WMPrototypeBoxManipulation();

    /**
     * Destructor.
     */
    ~WMPrototypeBoxManipulation();

    /**
     * Gives back the name of this module.
     * \return the module's name.
     */
    virtual const std::string getName() const;

    /**
     * Gives back a description of this module.
     * \return description of module.
     */
    virtual const std::string getDescription() const;

    /**
     * Determine what to do if a property was changed.
     * \param propertyName Name of the property.
     */
    void slotPropertyChanged( std::string propertyName );

    /**
     * Due to the prototype design pattern used to build modules, this method returns a new instance of this method. NOTE: it
     * should never be initialized or modified in some other way. A simple new instance is required.
     *
     * \return the prototype used to create every module in OpenWalnut.
     */
    virtual boost::shared_ptr< WModule > factory() const;

    /**
     *  updates the graphics
88
     * \param text text info from pick
89
     */
90
    void updateGFX( std::string text );
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

protected:
    /**
     * Entry point after loading the module. Runs in separate thread.
     */
    virtual void moduleMain();

    /**
     * Initialize the connectors this module is using.
     */
    virtual void connectors();

    /**
     * Initialize the properties for this module.
     */
    virtual void properties();

private:

    /**
     * draw initial graphics
112 113
     * \param minPos the lower left front corener of the box
     * \param maxPos the upper right back corner of the box
114
     */
115
    void draw( wmath::WPosition minPos, wmath::WPosition maxPos );
116 117 118 119 120 121

    boost::shared_mutex m_updateLock; //!< Lock to prevent concurrent threads trying to update the osg node

    osg::Geode* m_geode; //!< Pointer to geode. We need it to be able to update it when callback is invoked.

    boost::shared_ptr< WModuleInputData< WDataSetSingle > > m_input;  //!< Input connector required by this module.
122 123 124 125 126

    bool m_isPicked; //!< Indicates whether the box is currently picked or not.
    wmath::WPosition m_pickedPosition; //!< Caches the old picked position to a allow for cmoparison
    wmath::WPosition m_minPos; //!< The minimum position of the box
    wmath::WPosition m_maxPos; //!< The maximum position of the box
127

128
    osg::ref_ptr< WPickHandler > m_pickHandler; //!< A pointer to the pick handler used to get gui events for moving the box.
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
};

/**
 * Adapter object for realizing callbacks of the node representing the box in the osg
 */
class BoxNodeCallback : public osg::NodeCallback
{
public:
    /**
     * Constructor of the callback adapter.
     * \param module A function of this module will be called
     */
    explicit BoxNodeCallback( boost::shared_ptr< WMPrototypeBoxManipulation > module );

    /**
     * Function that is called by the osg and that call the function in the module.
     * \param node The node we are called.
     * \param nv the visitor calling us.
     */
    virtual void operator()( osg::Node* node, osg::NodeVisitor* nv );

private:
    boost::shared_ptr< WMPrototypeBoxManipulation > m_module; //!< Pointer to the module to which the function that is called belongs to.
};

inline BoxNodeCallback::BoxNodeCallback( boost::shared_ptr< WMPrototypeBoxManipulation > module )
    : m_module( module )
{
}

inline void BoxNodeCallback::operator()( osg::Node* node, osg::NodeVisitor* nv )
{
    if ( m_module )
    {
163
        m_module->updateGFX( "..." );
164 165 166 167 168
    }
    traverse( node, nv );
}

#endif  // WMPROTOTYPEBOXMANIPULATION_H