All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
PluginExample.cpp

This example demonstrates how widgets can be made into plugins.

PluginExample-screenshot.png
Screenshot of our minimalistic PluginExample

Plugins are widgets that can be instantiated by a string. They can be loaded dynamically during runtime instead of having to be statically linked in.

In this example, we create a simple custom widget that modulates its background color as a function of time:

class SampleWidget : public MultiWidgets::Widget
{
public:
SampleWidget() : m_age(0) {}
// Implement simple color modulation
virtual void update(const MultiWidgets::FrameInfo & frameInfo) OVERRIDE
{
// Color modulation:
m_age += frameInfo.dt();
setBackgroundColor(std::sin(m_age * 1.31f) * 0.5f + 0.5f,
std::sin(m_age * 1.53f) * 0.5f + 0.5f,
std::cos(m_age * 1.87f) * 0.5f + 0.5f,
1.0f);
}
private:
// Age in seconds for color modulation
float m_age;
};

To create a plugin from the widget, we just need to use a simple macro:

WIDGET_PLUGIN(SampleWidget, "Cornerstone.Examples.PluginExample", "PluginExample")

This macro creates the data structures necessary to load the plugin. Basically, it creates a factory function that creates an instance of the SampleWidget when called. This factory function is automatically registered to the MultiWidgets::Plugins class. It can be instantiated with MultiWidgets::createPlugin function.

Full source code for the example is listed below:

/* Copyright (C) 2007-2013 Multi Touch Oy, Finland, http://www.multitaction.com
*
* This file is part of MultiTouch Cornerstone.
*
* All rights reserved. You may use this file only for purposes for which you
* have a specific, written permission from Multi Touch Oy.
*
*/
#include <MultiWidgets/Application.hpp>
#include <MultiWidgets/Plugins.hpp>
namespace Examples
{
class SampleWidget : public MultiWidgets::Widget
{
public:
SampleWidget() : m_age(0) {}
// Implement simple color modulation
virtual void update(const MultiWidgets::FrameInfo & frameInfo) OVERRIDE
{
// Color modulation:
m_age += frameInfo.dt();
setBackgroundColor(std::sin(m_age * 1.31f) * 0.5f + 0.5f,
std::sin(m_age * 1.53f) * 0.5f + 0.5f,
std::cos(m_age * 1.87f) * 0.5f + 0.5f,
1.0f);
}
private:
// Age in seconds for color modulation
float m_age;
};
WIDGET_PLUGIN(SampleWidget, "Cornerstone.Examples.PluginExample", "PluginExample")
}
int main(int argc, char** argv)
{
if(!app.init(argc, argv))
return 1;
auto w = MultiWidgets::createPlugin("Cornerstone.Examples.PluginExample");
app.mainLayer()->addChild(w);
w->setSize(app.mainLayer()->size());
return app.run();
}