osgWidget.UIObjectParent是OpenSceneGraph中的一个类,用于布局一组子对象。它是osgWidget::Window的基类,因此它也被称为“窗口”或“容器”。
osgWidget.UIObjectParent的主要职责是管理一组子对象。子对象可以是任何类型的osg::Drawable或任何类型的osgWidget::Widget。osgWidget.UIObjectParent负责保持子对象的布局,并处理所有输入事件和交互。
osgWidget::Widget
    |
    +--osgWidget::Window
        |
        +--osgWidget::UIObjectParent
osgWidget.UIObjectParent有两个构造函数可供使用:
UIObjectParent();
UIObjectParent(const std::string& name);
使用第一个构造函数将创建一个没有名称的UIObjectParent。使用第二个构造函数将创建一个带有名称的UIObjectParent,该名称用于调试和标识。
osgWidget.UIObjectParent通过以下函数向子对象提供管理:
void addChild(osg::Node* child);
void addChild(Widget* child);
void removeChild(osg::Node* child);
void removeChild(Widget* child);
addChild用于将一个osg::Node或osgWidget::Widget添加为子对象。removeChild用于删除指定的子对象。addChild和removeChild将处理子对象的所有管理,包括位置和输入事件处理。
可以使用以下函数获取和管理子对象列表:
unsigned int getNumChildren() const;
osg::Node* getChild(unsigned int i) const;
Widget* getChildWidget(unsigned int i) const;
bool contains(Widget* child) const;
getNumChildren返回已添加的子对象数量。getChild返回指定索引处的osg::Node。getChildWidget返回指定索引处的osgWidget::Widget。contains检查是否已添加指定的子对象。
osgWidget.UIObjectParent使用以下函数来管理子对象的布局:
void resize();
void updateLayout();
void setPadding(float padding);
float getPadding() const;
void setMargin(float margin);
float getMargin() const;
void setAlignment(osg::Vec2 alignment);
osg::Vec2 getAlignment() const;
resize用于重新计算布局。通常只有在添加或删除子对象时才需要调用此函数。
updateLayout用于强制更新布局,即使没有添加或删除子对象。这在更改窗口大小或更改包含子对象的布局时非常有用。
setPadding设置内边距的大小,即子对象之间的空间量。
getPadding获取内边距的大小。
setMargin设置外边距的大小,即窗口周围的空间量。
getMargin获取外边距的大小。
setAlignment设置子对象相对于父对象的对齐方式。对齐值是一个范围在0到1之间的osg::Vec2,其中0.0表示左对齐或顶对齐,1.0表示右对齐或底对齐,中间的值表示相对位置。例如,0.5表示水平或垂直居中。
getAlignment获取子对象的对齐方式。
默认情况下,osgWidget.UIObjectParent将处理以下输入事件:
可以通过以下函数禁用和启用特定事件处理:
void setAcceptMouseButtonEvents(bool accept);
bool getAcceptMouseButtonEvents() const;
void setAcceptMouseMotionEvents(bool accept);
bool getAcceptMouseMotionEvents() const;
void setAcceptKeyboardEvents(bool accept);
bool getAcceptKeyboardEvents() const;
void setAcceptCompositeEvents(bool accept);
bool getAcceptCompositeEvents() const;
使用osgWidget.UIObjectParent创建一个简单的窗口,其中包含一些按钮:
#include <osgWidget/WindowManager>
#include <osgWidget/Box>
#include <osgWidget/Button>
osgWidget::WindowManager* wm = new osgWidget::WindowManager(viewport_width, viewport_height);
osg::ref_ptr<osgWidget::Box> box = new osgWidget::Box("mainbox", osgWidget::Box::VERTICAL);
wm->addChild(box);
osg::ref_ptr<osgWidget::Button> btn1 = new osgWidget::Button("btn1", "Button 1");
osg::ref_ptr<osgWidget::Button> btn2 = new osgWidget::Button("btn2", "Button 2");
osg::ref_ptr<osgWidget::Button> btn3 = new osgWidget::Button("btn3", "Button 3");
box->addWidget(btn1);
box->addWidget(btn2);
box->addWidget(btn3);
这将创建一个名为“mainbox”的osgWidget::Box,该Box使用垂直布局。然后,它将创建三个名为“btn1”、“btn2”和“btn3”的按钮,并将它们添加到Box中。最后,Box将添加到WindowManager中,以在场景中显示。