Keystone 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
  2. *
  3. * This library is open source and may be redistributed and/or modified under
  4. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  5. * (at your option) any later version. The full license is in LICENSE file
  6. * included with this distribution, and on the openscenegraph.org website.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * OpenSceneGraph Public License for more details.
  12. */
  13. #ifndef OSGVIEWER_Keystone
  14. #define OSGVIEWER_Keystone 1
  15. #include <osg/Geometry>
  16. #include <osg/Geode>
  17. #include <osg/DisplaySettings>
  18. #include <osgGA/GUIEventHandler>
  19. #include <osgViewer/Export>
  20. namespace osgViewer
  21. {
  22. class OSGVIEWER_EXPORT Keystone : public osg::Object
  23. {
  24. public:
  25. Keystone();
  26. Keystone(const Keystone& rhs, const osg::CopyOp& copop=osg::CopyOp::SHALLOW_COPY);
  27. META_Object(osgViewer, Keystone)
  28. void reset();
  29. Keystone& operator = (const Keystone& rhs);
  30. void setKeystoneEditingEnabled(bool flag) { keystoneEditingEnabled = flag; }
  31. bool getKeystoneEditingEnabled() const { return keystoneEditingEnabled; }
  32. void setGridColor(const osg::Vec4& color) { gridColour = color; }
  33. osg::Vec4& getGridColor() { return gridColour; }
  34. const osg::Vec4& getGridColor() const { return gridColour; }
  35. void setBottomLeft(const osg::Vec2d& v) { bottom_left = v; }
  36. osg::Vec2d& getBottomLeft() { return bottom_left; }
  37. const osg::Vec2d& getBottomLeft() const { return bottom_left; }
  38. void setBottomRight(const osg::Vec2d& v) { bottom_right = v; }
  39. osg::Vec2d& getBottomRight() { return bottom_right; }
  40. const osg::Vec2d& getBottomRight() const { return bottom_right; }
  41. void setTopLeft(const osg::Vec2d& v) { top_left = v; }
  42. osg::Vec2d& getTopLeft() { return top_left; }
  43. const osg::Vec2d& getTopLeft() const { return top_left; }
  44. void setTopRight(const osg::Vec2d& v) { top_right = v; }
  45. osg::Vec2d& getTopRight() { return top_right; }
  46. const osg::Vec2d& getTopRight() const { return top_right; }
  47. void compute3DPositions(osg::DisplaySettings* ds, osg::Vec3& tl, osg::Vec3& tr, osg::Vec3& br, osg::Vec3& bl) const;
  48. osg::Geode* createKeystoneDistortionMesh();
  49. osg::Node* createGrid();
  50. /** Write the file specified by the "filename" user value field. Return true if file successfully written. */
  51. bool writeToFile();
  52. /** Convenience function that loads and assigns any keystone files specified in the DisplaySettings::KeystoneFileNames list, return true if Keystone's assigned to DisplaySettings.*/
  53. static bool loadKeystoneFiles(osg::DisplaySettings* ds);
  54. protected:
  55. bool keystoneEditingEnabled;
  56. osg::Vec4 gridColour;
  57. osg::Vec2d bottom_left;
  58. osg::Vec2d bottom_right;
  59. osg::Vec2d top_left;
  60. osg::Vec2d top_right;
  61. protected:
  62. virtual ~Keystone() {}
  63. };
  64. class OSGVIEWER_EXPORT KeystoneHandler : public osgGA::GUIEventHandler
  65. {
  66. public:
  67. KeystoneHandler(Keystone* keystone);
  68. ~KeystoneHandler() {}
  69. bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa, osg::Object* obj, osg::NodeVisitor* nv);
  70. void setKeystoneEditingEnabled(bool enabled) { if (_currentControlPoints.valid()) _currentControlPoints->setKeystoneEditingEnabled(enabled); }
  71. bool getKeystoneEditingEnabled() const { return _currentControlPoints.valid() ? _currentControlPoints->getKeystoneEditingEnabled() : false; }
  72. enum Region
  73. {
  74. NONE_SELECTED,
  75. TOP_LEFT,
  76. TOP,
  77. TOP_RIGHT,
  78. RIGHT,
  79. BOTTOM_RIGHT,
  80. BOTTOM,
  81. BOTTOM_LEFT,
  82. LEFT,
  83. CENTER
  84. };
  85. osg::Vec2d incrementScale(const osgGA::GUIEventAdapter& ea) const;
  86. Region computeRegion(const osgGA::GUIEventAdapter& ea) const;
  87. void move(Region region, const osg::Vec2d& delta);
  88. protected:
  89. osg::ref_ptr<Keystone> _keystone;
  90. osg::Vec2d _defaultIncrement;
  91. osg::Vec2d _ctrlIncrement;
  92. osg::Vec2d _shiftIncrement;
  93. osg::Vec2d _keyIncrement;
  94. osg::Vec2d _startPosition;
  95. osg::ref_ptr<Keystone> _startControlPoints;
  96. Region _selectedRegion;
  97. osg::ref_ptr<Keystone> _currentControlPoints;
  98. };
  99. }
  100. #endif