Skip to content

Commit

Permalink
Added get function that can directly cast to desired type
Browse files Browse the repository at this point in the history
The lines below will now both work (instead of only the first one).
tgui::EditBox::Ptr(gui.get("myEditBox"))->setText("Hello");
gui.get<tgui::EditBox>("myEditBox")->setText("Hello");
  • Loading branch information
texus committed Sep 15, 2013
1 parent 17ff174 commit 2189df4
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
24 changes: 24 additions & 0 deletions include/TGUI/Container.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,30 @@ namespace tgui
Widget::Ptr get(const sf::String& widgetName) const;


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// \brief Returns a pointer to an earlier created widget.
///
/// \param widgetName The name that was given to the widget when it was added to the container.
///
/// \return Pointer to the earlier created widget.
/// The pointer will already be casted to the desired type.
///
/// \warning This function will return nullptr when an unknown widget name was passed.
///
/// Usage example:
/// \code
/// tgui::Picture::Ptr pic(container, "picName");
/// tgui::Picture::Ptr pic2 = container.get<tgui::Picture>("picName");
/// \endcode
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template <class T>
typename T::Ptr get(const sf::String& widgetName) const
{
return typename T::Ptr(get(widgetName));
}


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// \brief Makes a copy of any existing widget and returns the pointer to the new widget.
///
Expand Down
24 changes: 24 additions & 0 deletions include/TGUI/Gui.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,30 @@ namespace tgui
Widget::Ptr get(const sf::String& widgetName) const;


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// \brief Returns a pointer to an earlier created widget.
///
/// \param widgetName The name that was given to the widget when it was added to the container.
///
/// \return Pointer to the earlier created widget.
/// The pointer will already be casted to the desired type.
///
/// \warning This function will return nullptr when an unknown widget name was passed.
///
/// Usage example:
/// \code
/// tgui::Picture::Ptr pic(container, "picName");
/// tgui::Picture::Ptr pic2 = container.get<tgui::Picture>("picName");
/// \endcode
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template <class T>
typename T::Ptr get(const sf::String& widgetName) const
{
return m_Container.get<T>(widgetName);
}


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// \brief Makes a copy of any existing widget and returns the pointer to the new widget.
///
Expand Down

0 comments on commit 2189df4

Please sign in to comment.