Gazebo Gui

API Reference

6.8.0
MinimalScene.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2017 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 
18 #ifndef GZ_GUI_PLUGINS_MINIMALSCENE_HH_
19 #define GZ_GUI_PLUGINS_MINIMALSCENE_HH_
20 
21 #include <string>
22 #include <memory>
23 
24 #include <gz/common/KeyEvent.hh>
25 #include <gz/common/MouseEvent.hh>
26 #include <gz/math/Color.hh>
27 #include <gz/math/Pose3.hh>
28 #include <gz/math/Vector2.hh>
29 #include <gz/utils/ImplPtr.hh>
30 
31 #include "gz/gui/Plugin.hh"
32 
33 namespace ignition
34 {
35 namespace gui
36 {
37 namespace plugins
38 {
68  class MinimalScene : public Plugin
69  {
70  Q_OBJECT
71 
73  Q_PROPERTY(
75  READ LoadingError
76  WRITE SetLoadingError
77  NOTIFY LoadingErrorChanged
78  )
79 
80 
81  public: MinimalScene();
82 
86  public slots: void OnHovered(int _mouseX, int _mouseY);
87 
90  public slots: void OnFocusWindow();
91 
96  public slots: void OnDropped(const QString &_drop,
97  int _mouseX, int _mouseY);
98 
99  // Documentation inherited
100  public: virtual void LoadConfig(const tinyxml2::XMLElement *_pluginElem)
101  override;
102 
105  public: Q_INVOKABLE QString LoadingError() const;
106 
109  public: Q_INVOKABLE void SetLoadingError(const QString &_loadingError);
110 
112  signals: void LoadingErrorChanged();
113 
115  public: QString loadingError;
116 
119  IGN_UTILS_UNIQUE_IMPL_PTR(dataPtr)
120  };
121 
122  class RenderSync;
123 
131  {
133  public: IgnRenderer();
134 
137  public: void Render(RenderSync *_renderSync);
138 
142  public: std::string Initialize();
143 
145  public: void Destroy();
146 
149  public: void NewMouseEvent(const common::MouseEvent &_e);
150 
153  public: void NewHoverEvent(const math::Vector2i &_hoverPos);
154 
158  public: void NewDropEvent(const std::string &_dropText,
159  const math::Vector2i &_dropPos);
160 
163  public: void HandleKeyPress(const common::KeyEvent &_e);
164 
167  public: void HandleKeyRelease(const common::KeyEvent &_e);
168 
170  private: void HandleMouseEvent();
171 
173  private: void BroadcastHoverPos();
174 
176  private: void BroadcastDrag();
177 
179  private: void BroadcastLeftClick();
180 
182  private: void BroadcastRightClick();
183 
185  private: void BroadcastMousePress();
186 
188  private: void BroadcastScroll();
189 
191  private: void BroadcastKeyRelease();
192 
194  private: void BroadcastDrop();
195 
197  private: void BroadcastKeyPress();
198 
203  private: math::Vector3d ScreenToScene(const math::Vector2i &_screenPos)
204  const;
205 
209  public: GLuint textureId;
210 
212  public: std::string engineName = "ogre";
213 
215  public: std::string sceneName = "scene";
216 
218  public: math::Pose3d cameraPose = math::Pose3d(0, 0, 2, 0, 0.4, 0);
219 
221  public: double cameraNearClip = 0.01;
222 
224  public: double cameraFarClip = 1000.0;
225 
227  public: math::Color backgroundColor = math::Color::Black;
228 
230  public: math::Color ambientLight = math::Color(0.3f, 0.3f, 0.3f, 1.0f);
231 
233  public: bool initialized = false;
234 
236  public: QSize textureSize = QSize(1024, 1024);
237 
239  public: bool textureDirty = false;
240 
242  public: bool skyEnable = false;
243 
245  public: math::Angle cameraHFOV = math::Angle(M_PI * 0.5);
246 
248  public: std::string cameraViewController{""};
249 
252  IGN_UTILS_UNIQUE_IMPL_PTR(dataPtr)
253  };
254 
256  class RenderThread : public QThread
257  {
258  Q_OBJECT
259 
261  public: RenderThread();
262 
266  public slots: void RenderNext(RenderSync *_renderSync);
267 
269  public slots: void ShutDown();
270 
272  public slots: void SizeChanged();
273 
278  signals: void TextureReady(uint _id, const QSize &_size);
279 
282  public: void SetErrorCb(std::function<void(const QString &)> _cb);
283 
285  public: std::function<void(const QString &)> errorCb;
286 
288  public: QOffscreenSurface *surface = nullptr;
289 
291  public: QOpenGLContext *context = nullptr;
292 
295  };
296 
298  class RenderWindowItem : public QQuickItem
299  {
300  Q_OBJECT
301 
304  public: explicit RenderWindowItem(QQuickItem *_parent = nullptr);
305 
306  public: ~RenderWindowItem();
307 
310  public: void SetBackgroundColor(const math::Color &_color);
311 
314  public: void SetAmbientLight(const math::Color &_ambient);
315 
318  public: void SetEngineName(const std::string &_name);
319 
322  public: void SetSceneName(const std::string &_name);
323 
326  public: void SetCameraPose(const math::Pose3d &_pose);
327 
330  public: void SetCameraNearClip(double _near);
331 
334  public: void SetCameraFarClip(double _far);
335 
339  public: void OnHovered(const gz::math::Vector2i &_hoverPos);
340 
344  public: void OnDropped(const QString &_drop,
345  const gz::math::Vector2i &_dropPos);
346 
349  public: void SetSkyEnabled(const bool &_sky);
350 
353  public: void SetCameraHFOV(const math::Angle &_fov);
354 
357  public: void SetCameraViewController(const std::string &_view_controller);
358 
360  public Q_SLOTS: void Ready();
361 
364  public: void HandleKeyPress(const common::KeyEvent &_e);
365 
368  public: void HandleKeyRelease(const common::KeyEvent &_e);
369 
372  public: void SetErrorCb(std::function<void(const QString &)> _cb);
373 
375  public: void StopRendering();
376 
377  // Documentation inherited
378  protected: virtual void mousePressEvent(QMouseEvent *_e) override;
379 
380  // Documentation inherited
381  protected: virtual void mouseReleaseEvent(QMouseEvent *_e) override;
382 
383  // Documentation inherited
384  protected: virtual void mouseMoveEvent(QMouseEvent *_e) override;
385 
386  // Documentation inherited
387  protected: virtual void keyPressEvent(QKeyEvent *_e) override;
388 
389  // Documentation inherited
390  protected: virtual void keyReleaseEvent(QKeyEvent *_e) override;
391 
392  // Documentation inherited
393  protected: virtual void wheelEvent(QWheelEvent *_e) override;
394 
401  private: QSGNode *updatePaintNode(QSGNode *_oldNode,
402  QQuickItem::UpdatePaintNodeData *_data) override;
403 
406  IGN_UTILS_UNIQUE_IMPL_PTR(dataPtr)
407  };
408 
410  class TextureNode : public QObject, public QSGSimpleTextureNode
411  {
412  Q_OBJECT
413 
418  public: explicit TextureNode(QQuickWindow *_window,
419  RenderSync &_renderSync);
420 
422  public: ~TextureNode() override;
423 
428  public slots: void NewTexture(uint _id, const QSize &_size);
429 
432  public slots: void PrepareNode();
433 
435  signals: void TextureInUse(RenderSync *_renderSync);
436 
439  signals: void PendingNewTexture();
440 
442  public: uint id = 0;
443 
445  public: QSize size = QSize(0, 0);
446 
448  public: QMutex mutex;
449 
451  public: RenderSync &renderSync;
452 
454  public: QSGTexture *texture = nullptr;
455 
457  public: QQuickWindow *window = nullptr;
458  };
459 }
460 }
461 }
462 
463 #endif
Q_INVOKABLE void SetLoadingError(const QString &_loadingError)
Set the loading error message.
IgnRenderer ignRenderer
Ign-rendering renderer.
Definition: MinimalScene.hh:294
Creates a Gazebo rendering scene and user camera. It is possible to orbit the camera around the scene...
Definition: MinimalScene.hh:68
STL class.
std::function< void(const QString &)> errorCb
Function to be called if there are errors.
Definition: MinimalScene.hh:285
void OnFocusWindow()
Callback when the mouse enters the render window to focus the window for mouse/key events.
GLuint textureId
Render texture id.
Definition: MinimalScene.hh:209
void OnHovered(int _mouseX, int _mouseY)
Callback when the mouse hovers to a new position.
static const Color Black
Pose3< double > Pose3d
Texture node for displaying the render texture from gz-renderer.
Definition: MinimalScene.hh:410
Base class for Gazebo GUI plugins.
Definition: gz/gui/Plugin.hh:54
Q_INVOKABLE QString LoadingError() const
Get the loading error string.
RenderSync & renderSync
See RenderSync.
Definition: MinimalScene.hh:451
void OnDropped(const QString &_drop, int _mouseX, int _mouseY)
Callback when receives a drop event.
QString loadingError
Loading error message.
Definition: MinimalScene.hh:78
QMutex mutex
Mutex to protect the texture variables.
Definition: MinimalScene.hh:448
Rendering thread.
Definition: MinimalScene.hh:256
A QQUickItem that manages the render window.
Definition: MinimalScene.hh:298
Ign-rendering renderer. All ign-rendering calls should be performed inside this class as it makes sur...
Definition: MinimalScene.hh:130
Definition: gz/gui/Application.hh:35
virtual void LoadConfig(const tinyxml2::XMLElement *_pluginElem) override
Load the plugin with a configuration file. Override this on custom plugins to handle custom configura...
void LoadingErrorChanged()
Notify that loading error has changed.