AL_USDMaya  0.16.6
USD to Maya Bridge
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
MenuBuilder.h
1 #pragma once
2 #include "AL/maya/Common.h"
3 
4 #include "maya/MStatus.h"
5 #include "maya/MString.h"
6 #include "maya/MGlobal.h"
7 
8 #include <sstream>
9 #include <string>
10 #include <vector>
11 #include <set>
12 
13 namespace AL {
14 namespace maya {
15 //----------------------------------------------------------------------------------------------------------------------
38 //----------------------------------------------------------------------------------------------------------------------
40 {
41 public:
42 
44  struct MenuItem
45  {
46  std::string label;
47  std::string command;
48  std::string optionBox;
49  bool checkBox;
51  };
52 
53  #ifndef AL_GENERATING_DOCS
54  struct Menu
55  {
56  friend class MenuBuilder;
57 
58  ~Menu()
59  {
60  m_name.clear();
61  m_childMenus.clear();
62  m_menuItems.clear();
63  }
64 
65  inline bool operator < (const Menu& menu) const
66  { return m_name < menu.m_name; }
67 
68  inline bool operator == (const Menu& menu) const
69  { return m_name == menu.m_name; }
70 
71  inline void print_indent(std::ostream& os, int indent) const
72  { for(int i = 0; i <= indent; ++i) os << " "; }
73 
74  void generate(std::ostringstream& os, std::ostringstream& kill, const char* prefix, int indent = 0) const;
75 
76  const std::string& name() const { return m_name; }
77  const std::set<Menu>& childMenus() const { return m_childMenus; }
78  const std::vector<MenuItem>& menuItems() const { return m_menuItems; }
79 
80  private:
81  std::string m_name;
82  mutable std::set<Menu> m_childMenus;
83  mutable std::vector<MenuItem> m_menuItems;
84  };
85 
86  // for unit testing ONLY!!
87  static const std::set<Menu>& rootMenus()
88  { return m_menus; }
89 
90  // for unit testing ONLY!!
91  static void clearRootMenus()
92  { m_menus.clear(); }
93  #endif
94 
101  static MenuItem* addEntry(const char* menuItemPath, const char* command, bool hasCheckbox = false, bool defaultCheckBoxValue = false);
102 
108  static bool addEntry(const char* menuItemPath, const char* command, const char* optionBoxCommand);
109 
118  template<typename FnPlugin>
119  static MStatus generatePluginUI(FnPlugin& fnPlugin, const MString& prefix, const MString& extraOnInit = "", const MString& extraOnExit = "")
120  {
121  if(m_menus.empty())
122  return MS::kSuccess;
123  MString ui_init = prefix + "_initGUI";
124  MString ui_exit = prefix + "_exitGUI";
125 
126  // set up init script
127  std::ostringstream initGUI;
128  initGUI << "global proc " << ui_init.asChar() << "()\n{\n global string $gMainWindow;\n";
129 
130  if(extraOnInit.length())
131  initGUI << " " << extraOnInit.asChar() << std::endl;
132 
133  // set up exit script
134  std::ostringstream exitGUI;
135  exitGUI << "global proc " << ui_exit.asChar() << "()\n{\n";
136  if(extraOnExit.length())
137  exitGUI << " " << extraOnExit.asChar() << std::endl;
138  exitGUI << " deleteUI ";
139 
140  // now construct all of the menu related gubbins.
141  for(auto it = m_menus.begin(); it != m_menus.end(); ++it)
142  {
143  it->generate(initGUI, exitGUI, prefix.asChar());
144  }
145  m_menus.clear();
146 
147  // finish off
148 
149  initGUI << "}\n\n";
150  exitGUI << ";\n}\n\n";
151 
152  MGlobal::executeCommand(initGUI.str().c_str());
153  MGlobal::executeCommand(exitGUI.str().c_str());
154 
155  #if AL_USD_PRINT_UI_CODE
156  std::cout << initGUI.str() << std::endl;
157  std::cout << exitGUI.str() << std::endl;
158  #endif
159 
160  return fnPlugin.registerUI(ui_init, ui_exit);
161  }
162 
163 private:
164  static std::set<Menu> m_menus;
165 };
166 
167 //----------------------------------------------------------------------------------------------------------------------
168 } // usdmaya
169 } // AL
170 //----------------------------------------------------------------------------------------------------------------------
std::string command
the MEL command to execute
Definition: MenuBuilder.h:47
bool checkBox
true if an option box exists for this item
Definition: MenuBuilder.h:49
std::string optionBox
the MEL command to execute when the option box is checked
Definition: MenuBuilder.h:48
A structure that represents a menu item.
Definition: MenuBuilder.h:44
static MenuItem * addEntry(const char *menuItemPath, const char *command, bool hasCheckbox=false, bool defaultCheckBoxValue=false)
add an entry to the menu
static MStatus generatePluginUI(FnPlugin &fnPlugin, const MString &prefix, const MString &extraOnInit="", const MString &extraOnExit="")
generates an init and exit script that intialises the GUI on plugin load/unload (via MFnPlugin::regis...
Definition: MenuBuilder.h:119
std::string label
the menu item label
Definition: MenuBuilder.h:46
Definition: MenuBuilder.h:54
bool checkBoxValue
the default value for the check box if it is enabled
Definition: MenuBuilder.h:50
You shouldn't need to care about this class (sort of). Probably the only thing you'll want to do is e...
Definition: MenuBuilder.h:39