MeshLib
 
Loading...
Searching...
No Matches
State Plugins overview

Base Functions

State plugins is simple objects to work with MeshLib scene and/or objects. It is one of possible inheritance of RibbonMenuItem

It has base functions:

Available function - ISceneStateCheck interface function that block plugin in UI

Note
This fuction can be inherited from some helper classes SceneStateExactCheck SceneStateAtLeastCheck SceneStateOrCheck SceneStateAndCheck
// return empty string if all requirements are satisfied, otherwise return first unsatisfied requirement
virtual std::string isAvailable( const std::vector<std::shared_ptr<const Object>>& ) const { return ""; }

On Enable function - will be called if user enables plugin (only possible if isAvailable has returned true), if onEnable_ function returns false plugin will not be enabled (useful for some expensive checks, not to do them in each frame available function)

MRVIEWER_API virtual bool onEnable_();

On Disable function - will be called if user disable plugin, it is necessary to clear all plugin data

MRVIEWER_API virtual bool onDisable_();

Draw Dialog function - this function is called in each frame, only if plugin is enabled (will not be called after onDisable_ function). It is necessary to show plugin custom UI.

MRVIEWER_API virtual void drawDialog( float menuScaling, ImGuiContext* ctx );

How to make your first plugin

To create you plugin you need to inherit class from MR::StatePlugin or MR::StateListenerPlugin (in case you need to get some events)
Example:

class MyOwnPlugin : public MR::StatePlugin
{
public:
MyOwnPlugin():MR::StatePlugin("My Plugin Name"){}
virtual std::string isAvailable(const std::vector<std::shared_ptr<const MR::Object>>& selectedObjects) const override
{
if ( selectedObjects.size() == 1 )
return "";
return "Exactly one object should be selected";
}
virtual void drawDialog( float menuScaling, ImGuiContext* ctx) override
{
auto windowSize = viewer->viewport().getViewportRect();
auto menuWidth = 280.0f * menuScaling;
ImGui::SetCurrentContext( ctx );
ImGui::SetNextWindowPos( ImVec2( windowSize.width - menuWidth, 0 ), ImGuiCond_FirstUseEver );
ImGui::SetNextWindowSize( ImVec2( menuWidth, 0 ), ImGuiCond_FirstUseEver );
ImGui::SetNextWindowSizeConstraints( ImVec2( menuWidth, -1.0f ), ImVec2( menuWidth, -1.0f ) );
ImGui::Begin( plugin_name.c_str(), &dialogIsOpen_, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_AlwaysAutoResize );
if (ImGui::Button("Some action"))
someAction_();
ImGui::End();
}
private:
std::shared_ptr<MR::Object> obj_;
// some action of this plugin
void someAction_();
virtual bool onEnable_() override
{
obj_ = MR::getAllObjectsInTree<MR::Object>( &MR::SceneRoot::get(), MR::ObjectSelectivityType::Selected )[0];
return true;
}
virtual bool onDisable_() override
{
obj_.reset();
return true;
}
}
static MRMESH_API SceneRootObject & get()
Definition MRStatePlugin.h:135
Definition MRCameraOrientationPlugin.h:7

Register your plugin with

#define MR_REGISTER_RIBBON_ITEM(pluginType)
Definition MRRibbonMenu.h:320

To show this item on Ribbon menu, it should be present in MenuSchema.ui.json file (special page about these files are coming soon)
StateListenerPlugin is more simple way to make plugin listen to some events:

// This plugin will listen to mouse move and mouse down, also isAvailable function will be imlemented with SceneStateExactCheck
class MyListenerPlugin : public StateListenerPlugin<MouseMoveListener,MouseDownListener>, public SceneStateExactCheck<1, ObjectMesh>

Find more: RibbonMenuItem StateBasePlugin StateListenerPlugin