Gazebo Gui

API Reference

7.2.2
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 #include <gz/rendering/GraphicsAPI.hh>
31 
32 #include "gz/gui/Plugin.hh"
33 
34 #include "MinimalSceneRhi.hh"
35 
36 namespace gz::gui::plugins
37 {
69  class MinimalScene : public Plugin
70  {
71  Q_OBJECT
72 
74  Q_PROPERTY(
75  QString loadingError
76  READ LoadingError
77  WRITE SetLoadingError
78  NOTIFY LoadingErrorChanged
79  )
80 
81 
82  public: MinimalScene();
83 
87  public slots: void OnHovered(int _mouseX, int _mouseY);
88 
91  public slots: void OnFocusWindow();
92 
97  public slots: void OnDropped(const QString &_drop,
98  int _mouseX, int _mouseY);
99 
100  // Documentation inherited
101  public: virtual void LoadConfig(const tinyxml2::XMLElement *_pluginElem)
102  override;
103 
106  public: Q_INVOKABLE QString LoadingError() const;
107 
110  public: Q_INVOKABLE void SetLoadingError(const QString &_loadingError);
111 
113  signals: void LoadingErrorChanged();
114 
116  public: QString loadingError;
117 
120  GZ_UTILS_UNIQUE_IMPL_PTR(dataPtr)
121  };
122 
123  class RenderSync;
124 
132  {
134  public: GzRenderer();
135 
138  public: void Render(RenderSync *_renderSync);
139 
144  public: std::string Initialize();
145 
148  public: void SetGraphicsAPI(const rendering::GraphicsAPI &_graphicsAPI);
149 
151  public: void Destroy();
152 
155  public: void NewMouseEvent(const common::MouseEvent &_e);
156 
159  public: void NewHoverEvent(const math::Vector2i &_hoverPos);
160 
164  public: void NewDropEvent(const std::string &_dropText,
165  const math::Vector2i &_dropPos);
166 
169  public: void HandleKeyPress(const common::KeyEvent &_e);
170 
173  public: void HandleKeyRelease(const common::KeyEvent &_e);
174 
176  private: void HandleMouseEvent();
177 
179  private: void BroadcastHoverPos();
180 
182  private: void BroadcastDrag();
183 
185  private: void BroadcastLeftClick();
186 
188  private: void BroadcastRightClick();
189 
191  private: void BroadcastMousePress();
192 
194  private: void BroadcastScroll();
195 
197  private: void BroadcastKeyRelease();
198 
200  private: void BroadcastDrop();
201 
203  private: void BroadcastKeyPress();
204 
209  public: void TextureId(void* _texturePtr);
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 = true;
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  GZ_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(void* _texturePtr, 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() const;
289 
292  public: void SetSurface(QOffscreenSurface *_surface);
293 
295  public: QOpenGLContext *Context() const;
296 
299  public: void SetContext(QOpenGLContext *_context);
300 
303  public: void SetGraphicsAPI(const rendering::GraphicsAPI &_graphicsAPI);
304 
307  public: std::string Initialize();
308 
311 
314  };
315 
317  class RenderWindowItem : public QQuickItem
318  {
319  Q_OBJECT
320 
323  public: explicit RenderWindowItem(QQuickItem *_parent = nullptr);
324 
325  public: ~RenderWindowItem();
326 
329  public: void SetBackgroundColor(const math::Color &_color);
330 
333  public: void SetAmbientLight(const math::Color &_ambient);
334 
337  public: void SetEngineName(const std::string &_name);
338 
341  public: void SetSceneName(const std::string &_name);
342 
345  public: void SetCameraPose(const math::Pose3d &_pose);
346 
349  public: void SetCameraNearClip(double _near);
350 
353  public: void SetCameraFarClip(double _far);
354 
358  public: void OnHovered(const gz::math::Vector2i &_hoverPos);
359 
363  public: void OnDropped(const QString &_drop,
364  const gz::math::Vector2i &_dropPos);
365 
368  public: void SetSkyEnabled(const bool &_sky);
369 
372  public: void SetCameraHFOV(const math::Angle &_fov);
373 
376  public: void SetGraphicsAPI(const rendering::GraphicsAPI& _graphicsAPI);
377 
380  public: void SetCameraViewController(const std::string &_view_controller);
381 
383  public Q_SLOTS: void Ready();
384 
387  public: void HandleKeyPress(const common::KeyEvent &_e);
388 
391  public: void HandleKeyRelease(const common::KeyEvent &_e);
392 
395  public: void SetErrorCb(std::function<void(const QString &)> _cb);
396 
398  public: void StopRendering();
399 
400  // Documentation inherited
401  protected: virtual void mousePressEvent(QMouseEvent *_e) override;
402 
403  // Documentation inherited
404  protected: virtual void mouseReleaseEvent(QMouseEvent *_e) override;
405 
406  // Documentation inherited
407  protected: virtual void mouseMoveEvent(QMouseEvent *_e) override;
408 
409  // Documentation inherited
410  protected: virtual void keyPressEvent(QKeyEvent *_e) override;
411 
412  // Documentation inherited
413  protected: virtual void keyReleaseEvent(QKeyEvent *_e) override;
414 
415  // Documentation inherited
416  protected: virtual void wheelEvent(QWheelEvent *_e) override;
417 
424  private: QSGNode *updatePaintNode(QSGNode *_oldNode,
425  QQuickItem::UpdatePaintNodeData *_data) override;
426 
429  GZ_UTILS_UNIQUE_IMPL_PTR(dataPtr)
430  };
431 
433  class TextureNode : public QObject, public QSGSimpleTextureNode
434  {
435  Q_OBJECT
436 
442  public: explicit TextureNode(QQuickWindow *_window,
443  RenderSync &_renderSync,
444  const rendering::GraphicsAPI &_graphicsAPI);
445 
447  public: ~TextureNode() override;
448 
453  // public slots: void NewTexture(uint _id, const QSize &_size);
454  public slots: void NewTexture(void* _texturePtr, const QSize &_size);
455 
458  public slots: void PrepareNode();
459 
461  signals: void TextureInUse(RenderSync *_renderSync);
462 
465  signals: void PendingNewTexture();
466 
468  public: QSize size = QSize(0, 0);
469 
471  public: QMutex mutex;
472 
474  public: RenderSync &renderSync;
475 
477  public: QQuickWindow *window = nullptr;
478 
480  private: std::unique_ptr<TextureNodeRhi> rhi;
481  };
482 } // namespace gz::gui::plugins
483 
484 #endif // GZ_GUI_PLUGINS_MINIMALSCENE_HH_