DISTRHO Plugin Framework
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.

189 lines
4.7KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-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. #ifndef DGL_COMMON_HPP_INCLUDED
  17. #define DGL_COMMON_HPP_INCLUDED
  18. #include "../ImageBaseWidgets.hpp"
  19. START_NAMESPACE_DGL
  20. // -----------------------------------------------------------------------
  21. template <class ImageType>
  22. struct ButtonImpl {
  23. enum State {
  24. kStateNormal = 0,
  25. kStateHover,
  26. kStateDown
  27. };
  28. int button;
  29. int state;
  30. ImageBaseButton<ImageType>* const self;
  31. typename ImageBaseButton<ImageType>::Callback* callback_img;
  32. explicit ButtonImpl(ImageBaseButton<ImageType>* const s) noexcept
  33. : button(-1),
  34. state(kStateNormal),
  35. self(s),
  36. callback_img(nullptr) {}
  37. bool onMouse(const Widget::MouseEvent& ev)
  38. {
  39. // button was released, handle it now
  40. if (button != -1 && ! ev.press)
  41. {
  42. DISTRHO_SAFE_ASSERT(state == kStateDown);
  43. // release button
  44. const int button2 = button;
  45. button = -1;
  46. // cursor was moved outside the button bounds, ignore click
  47. if (! self->contains(ev.pos))
  48. {
  49. state = kStateNormal;
  50. self->repaint();
  51. return true;
  52. }
  53. // still on bounds, register click
  54. state = kStateHover;
  55. self->repaint();
  56. if (callback_img != nullptr)
  57. callback_img->imageButtonClicked(self, button2);
  58. return true;
  59. }
  60. // button was pressed, wait for release
  61. if (ev.press && self->contains(ev.pos))
  62. {
  63. button = static_cast<int>(ev.button);
  64. state = kStateDown;
  65. self->repaint();
  66. return true;
  67. }
  68. return false;
  69. }
  70. bool onMotion(const Widget::MotionEvent& ev)
  71. {
  72. // keep pressed
  73. if (button != -1)
  74. return true;
  75. if (self->contains(ev.pos))
  76. {
  77. // check if entering hover
  78. if (state == kStateNormal)
  79. {
  80. state = kStateHover;
  81. self->repaint();
  82. return true;
  83. }
  84. }
  85. else
  86. {
  87. // check if exiting hover
  88. if (state == kStateHover)
  89. {
  90. state = kStateNormal;
  91. self->repaint();
  92. return true;
  93. }
  94. }
  95. return false;
  96. }
  97. DISTRHO_PREVENT_HEAP_ALLOCATION
  98. DISTRHO_DECLARE_NON_COPYABLE(ButtonImpl)
  99. };
  100. // -----------------------------------------------------------------------
  101. template <class ImageType>
  102. struct ImageBaseKnob<ImageType>::PrivateData {
  103. ImageType image;
  104. float minimum;
  105. float maximum;
  106. float step;
  107. float value;
  108. float valueDef;
  109. float valueTmp;
  110. bool usingDefault;
  111. bool usingLog;
  112. Orientation orientation;
  113. int rotationAngle;
  114. bool dragging;
  115. double lastX;
  116. double lastY;
  117. Callback* callback;
  118. bool alwaysRepaint;
  119. bool isImgVertical;
  120. uint imgLayerWidth;
  121. uint imgLayerHeight;
  122. uint imgLayerCount;
  123. bool isReady;
  124. union {
  125. uint glTextureId;
  126. void* cairoSurface;
  127. };
  128. explicit PrivateData(const ImageType& img, const Orientation o);
  129. explicit PrivateData(PrivateData* const other);
  130. void assignFrom(PrivateData* const other);
  131. ~PrivateData()
  132. {
  133. cleanup();
  134. }
  135. void init();
  136. void cleanup();
  137. inline float logscale(const float v) const
  138. {
  139. const float b = std::log(maximum/minimum)/(maximum-minimum);
  140. const float a = maximum/std::exp(maximum*b);
  141. return a * std::exp(b*v);
  142. }
  143. inline float invlogscale(const float v) const
  144. {
  145. const float b = std::log(maximum/minimum)/(maximum-minimum);
  146. const float a = maximum/std::exp(maximum*b);
  147. return std::log(v/a)/b;
  148. }
  149. DISTRHO_DECLARE_NON_COPYABLE(PrivateData)
  150. };
  151. // -----------------------------------------------------------------------
  152. END_NAMESPACE_DGL
  153. #endif // DGL_APP_PRIVATE_DATA_HPP_INCLUDED