platform_ui_adapter.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright 2023 DeepMind Technologies Limited
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef MUJOCO_SIMULATE_PLATFORM_UI_ADAPTER_H_
  15. #define MUJOCO_SIMULATE_PLATFORM_UI_ADAPTER_H_
  16. #include <utility>
  17. #include <mujoco/mujoco.h>
  18. namespace mujoco {
  19. class PlatformUIAdapter {
  20. public:
  21. virtual ~PlatformUIAdapter() = default;
  22. inline mjuiState& state() { return state_; }
  23. inline const mjuiState& state() const { return state_; }
  24. inline mjrContext& mjr_context() { return con_; }
  25. inline const mjrContext& mjr_context() const { return con_; }
  26. inline void SetEventCallback(void (*event_callback)(mjuiState*)) {
  27. event_callback_ = event_callback;
  28. }
  29. inline void SetLayoutCallback(void (*layout_callback)(mjuiState*)) {
  30. layout_callback_ = layout_callback;
  31. }
  32. // Optionally overridable function to (re)create an mjrContext for an mjModel
  33. virtual bool RefreshMjrContext(const mjModel* m, int fontscale);
  34. virtual bool EnsureContextSize();
  35. // Pure virtual functions to be implemented by individual adapters
  36. virtual std::pair<double, double> GetCursorPosition() const = 0;
  37. virtual double GetDisplayPixelsPerInch() const = 0;
  38. virtual std::pair<int, int> GetFramebufferSize() const = 0;
  39. virtual std::pair<int, int> GetWindowSize() const = 0;
  40. virtual bool IsGPUAccelerated() const = 0;
  41. virtual void PollEvents() = 0;
  42. virtual void SetClipboardString(const char* text) = 0;
  43. virtual void SetVSync(bool enabled) = 0;
  44. virtual void SetWindowTitle(const char* title) = 0;
  45. virtual bool ShouldCloseWindow() const = 0;
  46. virtual void SwapBuffers() = 0;
  47. virtual void ToggleFullscreen() = 0;
  48. virtual bool IsLeftMouseButtonPressed() const = 0;
  49. virtual bool IsMiddleMouseButtonPressed() const = 0;
  50. virtual bool IsRightMouseButtonPressed() const = 0;
  51. virtual bool IsAltKeyPressed() const = 0;
  52. virtual bool IsCtrlKeyPressed() const = 0;
  53. virtual bool IsShiftKeyPressed() const = 0;
  54. virtual bool IsMouseButtonDownEvent(int act) const = 0;
  55. virtual bool IsKeyDownEvent(int act) const = 0;
  56. virtual int TranslateKeyCode(int key) const = 0;
  57. virtual mjtButton TranslateMouseButton(int button) const = 0;
  58. protected:
  59. PlatformUIAdapter();
  60. void FreeMjrContext();
  61. // Event handlers
  62. void OnFilesDrop(int count, const char** paths);
  63. virtual void OnKey(int key, int scancode, int act);
  64. void OnMouseButton(int button, int act);
  65. void OnMouseMove(double x, double y);
  66. void OnScroll(double xoffset, double yoffset);
  67. void OnWindowRefresh();
  68. void OnWindowResize(int width, int height);
  69. mjuiState state_;
  70. int last_key_;
  71. void (*event_callback_)(mjuiState*);
  72. void (*layout_callback_)(mjuiState*);
  73. mjrContext con_;
  74. const mjModel* last_model_ = nullptr;
  75. int last_fontscale_ = -1;
  76. private:
  77. void UpdateMjuiState();
  78. };
  79. } // namespace mujoco
  80. #endif // MUJOCO_SIMULATE_PLATFORM_UI_ADAPTER_H_