|
Katana Plug-in APIs 0.1
|
00001 // Copyright (c) 2018 The Foundry Visionmongers Ltd. All Rights Reserved. 00002 00003 #ifndef _FNBASELOCATOR_H_ 00004 #define _FNBASELOCATOR_H_ 00005 00006 #include <FnViewer/plugin/FnViewerDelegate.h> 00007 #include <FnViewer/plugin/FnViewerDelegateComponent.h> 00008 #include <FnViewer/plugin/FnViewport.h> 00009 #include <FnViewer/plugin/FnViewportLayer.h> 00010 #include <FnViewer/utils/FnDrawingHelpers.h> 00011 #include <FnViewer/utils/FnGLShaderProgram.h> 00012 #include <FnViewer/utils/FnImathHelpers.h> 00013 00014 #include <GL/glew.h> 00015 00016 #include <map> 00017 #include <memory> 00018 #include <set> 00019 #include <string> 00020 #include <unordered_set> 00021 #include <vector> 00022 00023 namespace Foundry 00024 { 00025 namespace Katana 00026 { 00027 namespace ViewerUtils 00028 { 00029 class FnBaseLocatorVDC; 00030 class FnBaseLocatorViewportLayer; 00031 00032 typedef std::map<std::string, std::set<std::string> > LocatorLocationsMap; 00033 00039 /***** Locator *****/ 00040 00047 class FnBaseLocator 00048 { 00049 public: 00051 typedef std::shared_ptr<FnBaseLocator> Ptr; 00053 typedef std::weak_ptr<FnBaseLocator> WeakPtr; 00054 00055 FnBaseLocator(); 00056 virtual ~FnBaseLocator(); 00057 00060 virtual void setup() = 0; 00061 00064 virtual void cleanup() = 0; 00065 00067 virtual void locationSetup(const std::string& locationPath, 00068 bool isPicking); 00069 00071 virtual void locationCleanup(const std::string& locationPath, 00072 bool isPicking); 00073 00076 virtual void draw(const std::string& locationPath) = 0; 00077 00081 virtual void pickerDraw(const std::string& locationPath) = 0; 00082 00084 virtual void onFrameBegin(bool isPicking); 00085 00087 virtual void onFrameEnd(bool isPicking); 00088 00089 public: 00091 FnBaseLocatorVDC* getViewerDelegateComponent() const; 00092 00094 void setViewerDelegateComponent(FnBaseLocatorVDC* vdc); 00095 00097 FnBaseLocatorViewportLayer* getViewportLayer() const; 00098 00100 void setViewportLayer(FnBaseLocatorViewportLayer* vdc); 00101 00104 FnAttribute::Attribute getAttribute(const std::string& locationPath, 00105 const std::string& attrName = ""); 00106 00109 bool isLocationSelected(const std::string& locationPath); 00110 00112 GLShaderProgram* getDefaultShaderProgram() const; 00113 00115 void setDefaultShaderProgram(GLShaderProgram* shaderProgram); 00116 private: 00117 FnBaseLocatorVDC* m_viewerDelegateComponent; 00118 FnBaseLocatorViewportLayer* m_viewportLayer; 00119 GLShaderProgram* m_shader; 00120 }; 00121 00122 00123 /***** Viewer Delegate Component *****/ 00124 00128 typedef FnBaseLocator* (*LocatorCreateCallback)(); 00133 typedef bool (*LocatorMatchesCallback)( 00134 const Foundry::Katana::ViewerAPI::ViewerLocationEvent& event); 00140 typedef FnAttribute::DoubleAttribute (*LocatorGetBoundsCallback)( 00141 const Foundry::Katana::ViewerAPI::ViewerLocationEvent& event); 00147 typedef FnAttribute::DoubleAttribute (*LocatorComputeExtentCallback)( 00148 const Foundry::Katana::ViewerAPI::ViewerLocationEvent& event); 00154 typedef bool (*OverridesBaseGeometryCallback)( 00155 const Foundry::Katana::ViewerAPI::ViewerLocationEvent& event); 00156 00157 00163 class FnBaseLocatorVDC : public FnKat::ViewerAPI::ViewerDelegateComponent 00164 { 00166 struct LocatorContainer 00167 { 00168 std::string name; 00169 LocatorCreateCallback create; 00170 LocatorMatchesCallback matches; 00171 OverridesBaseGeometryCallback overridesBaseGeometry; 00172 LocatorGetBoundsCallback getBounds; 00173 LocatorComputeExtentCallback computeExtent; 00174 }; 00175 typedef std::vector<LocatorContainer*> LocatorContainerVector; 00176 00178 class SceneNode : public std::enable_shared_from_this<SceneNode> 00179 { 00180 public: 00181 enum class InheritableFlag { 00182 kFalse, 00183 kTrue, 00184 kNotSet, 00185 }; 00186 00188 typedef std::shared_ptr<SceneNode> Ptr; 00190 typedef std::weak_ptr<SceneNode> WeakPtr; 00191 00192 explicit SceneNode(const std::string& locationPath); 00193 virtual ~SceneNode(); 00194 00197 void addChild(const std::string& name, SceneNode::Ptr child); 00198 00201 void removeChild(const std::string& name); 00202 00204 SceneNode::WeakPtr getChild(const std::string& name); 00205 00207 std::map<std::string, SceneNode::Ptr> getChildren() const; 00208 00210 SceneNode::WeakPtr getParent() const; 00211 00214 void setParent(SceneNode::WeakPtr parent); 00215 00218 std::string getLocationPath() const; 00219 00221 void addLocatorName(const std::string& locatorName); 00222 00224 void removeLocatorName(const std::string& locatorName); 00225 00228 void clearLocatorNames(); 00229 00232 bool hasLocator() const; 00233 00236 std::set<std::string> getLocatorNames() const; 00237 00240 void getDescendantLocatorLocations( 00241 LocatorLocationsMap& locatorLocations); 00242 00245 bool isSelected() const; 00246 00248 void setSelected(bool selected); 00249 00252 bool isEnabled() const; 00253 00256 void setEnabled(bool enabled); 00257 00259 bool isHidden() const; 00260 00262 void setHidden(InheritableFlag hidden); 00263 00265 bool isPickable() const; 00266 00268 void setPickable(InheritableFlag pickable); 00269 private: 00270 std::string m_locationPath; 00271 SceneNode::WeakPtr m_parent; 00272 std::map<std::string, SceneNode::Ptr> m_children; 00273 std::set<std::string> m_locatorNames; 00274 bool m_selected; 00275 bool m_enabled; 00276 InheritableFlag m_hidden; 00277 InheritableFlag m_pickable; 00278 }; 00279 00280 public: 00281 FnBaseLocatorVDC(); 00282 virtual ~FnBaseLocatorVDC(); 00283 00284 static void flush(); 00285 00286 static FnKat::ViewerAPI::ViewerDelegateComponent* create() 00287 { 00288 return new FnBaseLocatorVDC(); 00289 } 00290 00292 static void registerLocator( 00293 const std::string& name, 00294 LocatorCreateCallback create, 00295 LocatorMatchesCallback matches, 00296 OverridesBaseGeometryCallback overridesBaseGeometry, 00297 LocatorGetBoundsCallback getBounds, 00298 LocatorComputeExtentCallback computeExtent); 00299 00301 virtual void setup() {} 00302 00304 virtual void cleanup() {} 00305 00307 virtual bool locationEvent( 00308 const Foundry::Katana::ViewerAPI::ViewerLocationEvent& event, 00309 bool locationHandled); 00310 00312 virtual void locationsSelected( 00313 const std::vector<std::string>& locationPaths); 00314 00316 virtual void* getPrivateData(void* inputData); 00317 00319 virtual FnAttribute::DoubleAttribute getBounds( 00320 const std::string& locationPath); 00321 00323 virtual FnAttribute::DoubleAttribute computeExtent( 00324 const std::string& locationPath); 00325 00326 public: 00328 void removeLocation(const std::string& locationPath); 00329 00332 virtual SceneNode::Ptr createSceneNode( 00333 const FnKat::ViewerAPI::ViewerLocationEvent& event); 00334 00336 void dirtyAllViewports(); 00337 00339 SceneNode::Ptr getRootSceneNode() const { return m_rootSceneNode; } 00340 00343 std::weak_ptr<LocatorLocationsMap> getLocatorLocations(); 00344 00346 static LocatorContainerVector getRegisteredLocators(); 00347 00350 bool isLocationSelected(const std::string& locationPath); 00351 00353 bool isLocationHidden(const std::string& locationPath); 00354 00355 private: 00356 static LocatorContainerVector m_registeredLocators; 00357 SceneNode::Ptr m_rootSceneNode; 00358 std::shared_ptr<LocatorLocationsMap> m_locatorLocations; 00359 std::unordered_set<std::string> m_selectedLocations; 00360 }; 00361 00362 00363 /***** Viewport Layer *****/ 00364 00369 class FnBaseLocatorViewportLayer : public FnKat::ViewerAPI::ViewportLayer 00370 { 00371 public: 00372 FnBaseLocatorViewportLayer(); 00373 virtual ~FnBaseLocatorViewportLayer(); 00374 00375 static Foundry::Katana::ViewerAPI::ViewportLayer* create() 00376 { 00377 return new FnBaseLocatorViewportLayer(); 00378 } 00379 00380 virtual void freeze() {} 00381 00382 virtual void thaw() {} 00383 00385 virtual void setup(); 00386 00388 virtual void cleanup(); 00389 00391 virtual void draw(); 00392 00394 virtual void pickerDraw( 00395 unsigned int x, 00396 unsigned int y, 00397 unsigned int w, 00398 unsigned int h, 00399 const Foundry::Katana::ViewerAPI::PickedAttrsMap& ignoreAttrs); 00400 00403 virtual bool usesPickingOnHover() { return true; } 00404 00406 virtual void resize(unsigned int width, unsigned int height); 00407 00409 virtual void setOption( 00410 Foundry::Katana::ViewerAPI::OptionIdGenerator::value_type optionId, 00411 FnAttribute::Attribute attr); 00412 00414 virtual void initializeLocators(); 00415 00418 virtual void drawLocators(bool isPicking, 00419 std::set<std::string>& ignoreLocations); 00420 00421 private: 00422 FnKat::ViewerAPI::ViewerDelegateWrapperPtr m_viewerDelegate; 00423 FnKat::ViewerAPI::ViewportWrapperPtr m_viewport; 00424 FnBaseLocatorVDC* m_viewerDelegateComponent; 00425 std::weak_ptr<LocatorLocationsMap> 00426 m_locatorLocations; //* Deprecate & use the VDC function? 00427 std::map<std::string, FnBaseLocator::Ptr> m_locators; 00428 00429 GLShaderProgram m_shader; 00430 std::string m_vdcName; 00431 bool m_initialized; 00432 00433 std::vector<std::string> m_excludedLocationsPicking; 00434 }; 00435 00438 00439 00440 #define REGISTER_LOCATOR(CLASS_NAME) \ 00441 FnKat::ViewerUtils::FnBaseLocatorVDC::registerLocator( \ 00442 #CLASS_NAME, &CLASS_NAME::create, &CLASS_NAME::matches, \ 00443 &CLASS_NAME::overridesBaseGeometry, &CLASS_NAME::getBounds, \ 00444 &CLASS_NAME::computeExtent); 00445 00447 00448 } // namespace ViewerUtils 00449 } // namespace Katana 00450 } // namespace Foundry 00451 00452 #endif
1.7.3