Using lambda expressions with CEGUI

C++ lambdas is just another name for anonymous methods, which can be defined within another method. It’s particularly useful for callback methods.

With CEGUI, it’s easy to define a callback for a pushbutton like this:

// Exit button
{
	CEGUI::PushButton* button = static_cast<CEGUI::PushButton*>(m_WindowManager->createWindow("TaharezLook/Button" ));
	button->setPosition(CEGUI::UVector2(cegui_reldim(0.40f), cegui_reldim( 0.50f)));
	button->setSize( buttonSize );
	button->setText("Exit");

	auto handleExitClicked = [&] ( const CEGUI::EventArgs & args ) -> bool
	{
		// Send message
		sendToProcessing(ExitGameMessage());

		return true;
	};

	button->subscribeEvent( button->EventMouseClick, handleExitClicked );

	root.addChildWindow(button);
}

This is just a simple example on how to use lambdas with CEGUI.

About Kent Fagerjord

Professional Software Developer, doing mostly C++. Connect with Kent on Twitter.
This entry was posted in coding and tagged , , . Bookmark the permalink.