You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
3.4KB

  1. #pragma once
  2. #include "common.hpp"
  3. #include "math.hpp"
  4. #include <memory>
  5. #include <map>
  6. #define GLEW_STATIC
  7. #include <GL/glew.h>
  8. #include <GLFW/glfw3.h>
  9. #include <nanovg.h>
  10. #define NANOVG_GL2
  11. #include <nanovg_gl.h>
  12. #include <nanovg_gl_utils.h>
  13. #include <nanosvg.h>
  14. /** Remaps Ctrl to Cmd on Mac
  15. Use this instead of GLFW_MOD_CONTROL, since Cmd should be used on Mac in place of Ctrl on Linux/Windows.
  16. */
  17. #if defined ARCH_MAC
  18. #define RACK_MOD_CTRL GLFW_MOD_SUPER
  19. #define RACK_MOD_CTRL_NAME "Cmd"
  20. #else
  21. #define RACK_MOD_CTRL GLFW_MOD_CONTROL
  22. #define RACK_MOD_CTRL_NAME "Ctrl"
  23. #endif
  24. #define RACK_MOD_SHIFT_NAME "Shift"
  25. #define RACK_MOD_ALT_NAME "Alt"
  26. /** Filters actual mod keys from the mod flags.
  27. Use this if you don't care about GLFW_MOD_CAPS_LOCK and GLFW_MOD_NUM_LOCK.
  28. Example usage:
  29. if ((e.mod & RACK_MOD_MASK) == (RACK_MOD_CTRL | GLFW_MOD_SHIFT)) ...
  30. */
  31. #define RACK_MOD_MASK (GLFW_MOD_SHIFT | GLFW_MOD_CONTROL | GLFW_MOD_ALT | GLFW_MOD_SUPER)
  32. namespace rack {
  33. // Constructing these directly will load from the disk each time. Use the load() functions to load from disk and cache them as long as the shared_ptr is held.
  34. struct Font {
  35. NVGcontext *vg;
  36. int handle = -1;
  37. /** Don't call this directly but instead use `APP->window->loadFont()` */
  38. void loadFile(const std::string &filename, NVGcontext *vg);
  39. ~Font();
  40. /** Use `APP->window->loadFont()` instead. */
  41. DEPRECATED static std::shared_ptr<Font> load(const std::string &filename);
  42. };
  43. struct Image {
  44. NVGcontext *vg;
  45. int handle = -1;
  46. /** Don't call this directly but instead use `APP->window->loadImage()` */
  47. void loadFile(const std::string &filename, NVGcontext *vg);
  48. ~Image();
  49. /** Use `APP->window->loadImage()` instead. */
  50. DEPRECATED static std::shared_ptr<Image> load(const std::string &filename);
  51. };
  52. struct Svg {
  53. NSVGimage *handle = NULL;
  54. /** Don't call this directly but instead use `APP->window->loadSvg()` */
  55. void loadFile(const std::string &filename);
  56. ~Svg();
  57. /** Use `APP->window->loadSvg()` instead. */
  58. DEPRECATED static std::shared_ptr<Svg> load(const std::string &filename);
  59. };
  60. DEPRECATED typedef Svg SVG;
  61. struct Window {
  62. GLFWwindow *win = NULL;
  63. NVGcontext *vg = NULL;
  64. /** The scaling ratio */
  65. float pixelRatio = 1.f;
  66. /* The ratio between the framebuffer size and the window size reported by the OS.
  67. This is not equal to gPixelRatio in general.
  68. */
  69. float windowRatio = 1.f;
  70. int frame = 0;
  71. /** The last known absolute mouse position in the window */
  72. math::Vec mousePos;
  73. std::shared_ptr<Font> uiFont;
  74. double frameTimeStart = 0.f;
  75. /** Use load*() instead of modifying these directly. */
  76. std::map<std::string, std::weak_ptr<Font>> fontCache;
  77. std::map<std::string, std::weak_ptr<Image>> imageCache;
  78. std::map<std::string, std::weak_ptr<Svg>> svgCache;
  79. struct Internal;
  80. Internal *internal;
  81. Window();
  82. ~Window();
  83. void run();
  84. /** Takes a screenshot of each module */
  85. void screenshot(float zoom);
  86. void close();
  87. void cursorLock();
  88. void cursorUnlock();
  89. /** Gets the current keyboard mod state
  90. Don't call this from a Key event. Simply use `e.mods` instead.
  91. */
  92. int getMods();
  93. void setFullScreen(bool fullScreen);
  94. bool isFullScreen();
  95. bool isFrameOverdue();
  96. std::shared_ptr<Font> loadFont(const std::string &filename);
  97. std::shared_ptr<Image> loadImage(const std::string &filename);
  98. std::shared_ptr<Svg> loadSvg(const std::string &filename);
  99. };
  100. void windowInit();
  101. void windowDestroy();
  102. } // namespace rack