Skip to content

How should I use TBWidgetsReader to set up a widget? #125

@karamellpelle

Description

@karamellpelle

The TBWidgetsReader class works very well to read children widgets into a target. And it's easy to make it parse custom subclasses of TBWidget: use the TB_WIDGET_FACTORY macro and override the virtual OnInflate(const INFLATE_INFO& ) method of TBWidget.

The turbobadger demo uses TBWidgetsReader to set up it's windows: It calls void DemoWindow::LoadResource(TBNode &node) to read its windows from .tb.txt files. This works well for it child widgets, but not itself (i.e. the widget DemoWindow); the .tb.txt-file is not able to set very useful TBWidget and TBWindow properties like LayoutParams, Opacity, etc.. To set the window size, DemoWindow reads the custom WindowInfo field instead of using lp>width and lp>height because they can't be read. Shouldn't there be a possibility for widgets to read themselves from TBNode's?

I solved this by creating a template

////////////////////////////////////////////////////////////////////////////////
// create a widget for us from node and adding it to 'parent'. see
// TBWidget::OnInflate() and TBWidgetsReader::CreateWIdget()
// in tb_widgets_reader.cpp

template <typename Widget>
Widget* new_widget(TBWidget* parent, TBNode* node)
{
    auto ret = new Widget();
   
    INFLATE_INFO info( g_widgets_reader, parent, node, TBValue::TYPE_NULL );

    // this adds it to the parent, see 'TBWidget::OnInflate()' 
    ret->OnInflate( info );

    // now read and add children
    g_widgets_reader->LoadNodeTree( ret, node );

    return ret;
}

, the only downside is that it needs a parent because TBWidget::OnInflate(const INFLATE_INFO& ) adds itself to the parent (and does not check for a nullptr target).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions