2008-07-26 04:04:41 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2008 Robert Osfield
|
|
|
|
*
|
|
|
|
* This library is open source and may be redistributed and/or modified under
|
|
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
|
|
|
|
2008-07-16 01:21:25 +08:00
|
|
|
// -*-c++-*- osgWidget - Code by: Jeremy Moles (cubicool) 2007-2008
|
|
|
|
// $Id: UIObjectParent 55 2008-05-12 19:14:42Z cubicool $
|
|
|
|
|
|
|
|
#ifndef OSGWIDGET_UI_OBJECT_PARENT
|
|
|
|
#define OSGWIDGET_UI_OBEJCT_PARENT
|
|
|
|
|
|
|
|
namespace osgWidget {
|
|
|
|
|
|
|
|
#define META_UIObject(name) \
|
2008-07-26 03:57:17 +08:00
|
|
|
name * cloneAs(\
|
|
|
|
const std::string& newName, \
|
|
|
|
const osg::CopyOp& co = osg::CopyOp::DEEP_COPY_ALL \
|
|
|
|
) const { \
|
|
|
|
name * obj = dynamic_cast<name *>(this->clone(co)); \
|
|
|
|
obj->setName(newName); \
|
|
|
|
return obj; \
|
|
|
|
}
|
2008-07-16 01:21:25 +08:00
|
|
|
|
|
|
|
template <typename T>
|
2008-07-26 03:57:17 +08:00
|
|
|
class UIObjectParent
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef T object_type;
|
|
|
|
typedef osg::observer_ptr<object_type> ptr_type;
|
|
|
|
typedef std::vector<ptr_type> Vector;
|
|
|
|
typedef typename Vector::iterator Iterator;
|
|
|
|
typedef typename Vector::const_iterator ConstIterator;
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
Iterator begin() {
|
|
|
|
return _objects.begin();
|
|
|
|
}
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
ConstIterator begin() const {
|
|
|
|
return _objects.begin();
|
|
|
|
}
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
Iterator end() {
|
|
|
|
return _objects.end();
|
|
|
|
}
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
ConstIterator end() const {
|
|
|
|
return _objects.end();
|
|
|
|
}
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
typename Vector::size_type size() const {
|
|
|
|
return _objects.size();
|
|
|
|
}
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
object_type* getByName(const std::string& name) {
|
|
|
|
return _getByName(name);
|
|
|
|
}
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
const object_type* getByName(const std::string& name) const {
|
|
|
|
return _getByName(name);
|
|
|
|
}
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
object_type* getByIndex(unsigned int index) {
|
|
|
|
return _getByIndex(index);
|
|
|
|
}
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
const object_type* getByIndex(unsigned int index) const {
|
|
|
|
return _getByIndex(index);
|
|
|
|
}
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
unsigned int getNumObjects() const {
|
|
|
|
return _objects.size();
|
|
|
|
}
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
Vector& getObjects() {
|
|
|
|
return _objects;
|
|
|
|
}
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
const Vector& getObjects() const {
|
|
|
|
return _objects;
|
|
|
|
}
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
protected:
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
bool _remove(object_type* obj) {
|
|
|
|
Iterator i = std::find(begin(), end(), obj);
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
if(i == end()) return false;
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
_objects.erase(i);
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
return true;
|
|
|
|
}
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
bool _removeByName(const std::string& name) {
|
|
|
|
for(Iterator i = begin(); i != end(); i++) if(i->get()->getName() == name) {
|
|
|
|
_objects.erase(i);
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
return true;
|
|
|
|
}
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
return false;
|
|
|
|
}
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
Vector _objects;
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
private:
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
// I had to add this to avoid ambiguity errors with MSVC. Garbage.
|
|
|
|
object_type* _getByName(const std::string& name) const {
|
|
|
|
for(ConstIterator i = begin(); i != end(); i++) {
|
|
|
|
if(i->valid() && i->get()->getName() == name) return i->get();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
object_type* _getByIndex(unsigned int index) const {
|
|
|
|
for(ConstIterator i = begin(); i != end(); i++) {
|
|
|
|
if(i->valid() && i->get()->getIndex() == index) return i->get();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2008-07-16 01:21:25 +08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|