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.

201 lines
5.2KB

  1. /*
  2. * DISTRHO CVCRack Plugin
  3. * Copyright (C) 2021 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. * or without fee is hereby granted, provided that the above copyright notice and this
  7. * permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  10. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  11. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  12. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  13. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <app/common.hpp>
  17. #include <app/Scene.hpp>
  18. #include <context.hpp>
  19. #include <engine/Engine.hpp>
  20. #include <patch.hpp>
  21. #include <ui/common.hpp>
  22. #include <window/Window.hpp>
  23. #include "DistrhoUI.hpp"
  24. #include "ResizeHandle.hpp"
  25. GLFWAPI const char* glfwGetClipboardString(GLFWwindow* window) { return nullptr; }
  26. GLFWAPI void glfwSetClipboardString(GLFWwindow* window, const char*) {}
  27. GLFWAPI const char* glfwGetKeyName(int key, int scancode) { return nullptr; }
  28. GLFWAPI int glfwGetKeyScancode(int key) { return 0; }
  29. namespace rack {
  30. namespace window {
  31. DISTRHO_NAMESPACE::UI* lastUI = nullptr;
  32. void mouseButtonCallback(Window* win, int button, int action, int mods);
  33. void cursorPosCallback(Window* win, double xpos, double ypos);
  34. void scrollCallback(Window* win, double x, double y);
  35. }
  36. }
  37. START_NAMESPACE_DISTRHO
  38. // -----------------------------------------------------------------------------------------------------------
  39. struct Initializer2 {
  40. Initializer2()
  41. {
  42. using namespace rack;
  43. }
  44. ~Initializer2()
  45. {
  46. using namespace rack;
  47. }
  48. };
  49. static const Initializer2& getInitializer2Instance()
  50. {
  51. static const Initializer2 init;
  52. return init;
  53. }
  54. class CVCRackUI : public UI
  55. {
  56. ResizeHandle fResizeHandle;
  57. public:
  58. CVCRackUI()
  59. : UI(1280, 720),
  60. fResizeHandle(this)
  61. {
  62. using namespace rack;
  63. // Initialize context
  64. INFO("Initializing context");
  65. window::lastUI = this;
  66. contextSet(new Context);
  67. APP->engine = new engine::Engine;
  68. APP->history = new history::State;
  69. APP->event = new widget::EventState;
  70. APP->scene = new app::Scene;
  71. APP->event->rootWidget = APP->scene;
  72. APP->patch = new patch::Manager;
  73. /*if (!settings::headless)*/ {
  74. APP->window = new window::Window;
  75. }
  76. window::lastUI = nullptr;
  77. APP->engine->startFallbackThread();
  78. }
  79. ~CVCRackUI() override
  80. {
  81. using namespace rack;
  82. delete APP;
  83. contextSet(NULL);
  84. }
  85. void onDisplay() override
  86. {
  87. APP->window->step();
  88. }
  89. void uiIdle() override
  90. {
  91. repaint();
  92. }
  93. protected:
  94. /* --------------------------------------------------------------------------------------------------------
  95. * DSP/Plugin Callbacks */
  96. /**
  97. A parameter has changed on the plugin side.
  98. This is called by the host to inform the UI about parameter changes.
  99. */
  100. void parameterChanged(uint32_t index, float value) override
  101. {
  102. }
  103. // -------------------------------------------------------------------------------------------------------
  104. bool onMouse(const MouseEvent& ev) override
  105. {
  106. int button;
  107. int mods = 0;
  108. int action = ev.press;
  109. if (ev.mod & kModifierControl)
  110. mods |= GLFW_MOD_CONTROL;
  111. if (ev.mod & kModifierShift)
  112. mods |= GLFW_MOD_SHIFT;
  113. if (ev.mod & kModifierAlt)
  114. mods |= GLFW_MOD_ALT;
  115. switch (ev.button)
  116. {
  117. case 0:
  118. button = GLFW_MOUSE_BUTTON_MIDDLE;
  119. break;
  120. case 1:
  121. button = GLFW_MOUSE_BUTTON_LEFT;
  122. break;
  123. case 2:
  124. button = GLFW_MOUSE_BUTTON_RIGHT;
  125. break;
  126. default:
  127. button = 0;
  128. break;
  129. }
  130. mouseButtonCallback(APP->window, button, action, mods);
  131. return true;
  132. }
  133. bool onMotion(const MotionEvent& ev) override
  134. {
  135. cursorPosCallback(APP->window, ev.pos.getX(), ev.pos.getY());
  136. return true;
  137. }
  138. bool onScroll(const ScrollEvent& ev) override
  139. {
  140. scrollCallback(APP->window, ev.delta.getX(), ev.delta.getY());
  141. return true;
  142. }
  143. #if 0
  144. void onResize(const ResizeEvent& ev) override
  145. {
  146. UI::onResize(ev);
  147. // APP->window->setSize(rack::math::Vec(ev.size.getWidth(), ev.size.getHeight()));
  148. }
  149. #endif
  150. // TODO uiFocus
  151. private:
  152. /**
  153. Set our UI class as non-copyable and add a leak detector just in case.
  154. */
  155. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CVCRackUI)
  156. };
  157. /* ------------------------------------------------------------------------------------------------------------
  158. * UI entry point, called by DPF to create a new UI instance. */
  159. UI* createUI()
  160. {
  161. getInitializer2Instance();
  162. return new CVCRackUI();
  163. }
  164. // -----------------------------------------------------------------------------------------------------------
  165. END_NAMESPACE_DISTRHO