Audio plugin host https://kx.studio/carla
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.

149 lines
3.9KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2014 Filipe Coelho <falktx@falktx.com>
  4. * Copyright (C) 2014 Damien Zammit <damien@zamaudio.com>
  5. *
  6. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  7. * or without fee is hereby granted, provided that the above copyright notice and this
  8. * permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  11. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  12. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  13. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  14. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  15. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #ifndef DGL_IMAGE_TOGGLE_HPP_INCLUDED
  18. #define DGL_IMAGE_TOGGLE_HPP_INCLUDED
  19. #include "Image.hpp"
  20. #include "Widget.hpp"
  21. START_NAMESPACE_DGL
  22. // -----------------------------------------------------------------------
  23. class ImageToggle : public Widget
  24. {
  25. public:
  26. class Callback
  27. {
  28. public:
  29. virtual ~Callback() {}
  30. virtual void imageToggleClicked(ImageToggle* imageToggle, int button) = 0;
  31. };
  32. ImageToggle(Window& parent, const Image& imageNormal, const Image& imageDown, int id = 0)
  33. : Widget(parent),
  34. fImageNormal(imageNormal),
  35. fImageDown(imageDown),
  36. fCurImage(&fImageNormal),
  37. fId(id),
  38. fCallback(nullptr)
  39. {
  40. DISTRHO_SAFE_ASSERT(fImageNormal.getSize() == fImageDown.getSize());
  41. setSize(fImageNormal.getSize());
  42. }
  43. ImageToggle(Widget* widget, const Image& imageNormal, const Image& imageDown, int id = 0)
  44. : Widget(widget->getParentWindow()),
  45. fImageNormal(imageNormal),
  46. fImageDown(imageDown),
  47. fCurImage(&fImageNormal),
  48. fId(id),
  49. fCallback(nullptr)
  50. {
  51. DISTRHO_SAFE_ASSERT(fImageNormal.getSize() == fImageDown.getSize());
  52. setSize(fImageNormal.getSize());
  53. }
  54. ImageToggle(const ImageToggle& imageToggle)
  55. : Widget(imageToggle.getParentWindow()),
  56. fImageNormal(imageToggle.fImageNormal),
  57. fImageDown(imageToggle.fImageDown),
  58. fCurImage(&fImageNormal),
  59. fId(imageToggle.fId),
  60. fCallback(imageToggle.fCallback)
  61. {
  62. DISTRHO_SAFE_ASSERT(fImageNormal.getSize() == fImageDown.getSize());
  63. setSize(fImageNormal.getSize());
  64. }
  65. int getId() const noexcept
  66. {
  67. return fId;
  68. }
  69. void setId(int id) noexcept
  70. {
  71. fId = id;
  72. }
  73. float getValue() const noexcept
  74. {
  75. return (fCurImage == &fImageNormal) ? 0.0f : 1.0f;
  76. }
  77. void setValue(float value) noexcept
  78. {
  79. if (value == 1.0f)
  80. fCurImage = &fImageDown;
  81. else if (value == 0.0f)
  82. fCurImage = &fImageNormal;
  83. repaint();
  84. }
  85. void setCallback(Callback* callback) noexcept
  86. {
  87. fCallback = callback;
  88. }
  89. protected:
  90. void onDisplay() override
  91. {
  92. fCurImage->draw();
  93. }
  94. bool onMouse(const MouseEvent& ev) override
  95. {
  96. if (! ev.press)
  97. return false;
  98. if (! contains(ev.pos))
  99. return false;
  100. if (fCurImage != &fImageDown)
  101. fCurImage = &fImageDown;
  102. else if (fCurImage != &fImageNormal)
  103. fCurImage = &fImageNormal;
  104. repaint();
  105. if (fCallback != nullptr)
  106. fCallback->imageToggleClicked(this, ev.button);
  107. return true;
  108. }
  109. private:
  110. Image fImageNormal;
  111. Image fImageDown;
  112. Image* fCurImage;
  113. int fId;
  114. Callback* fCallback;
  115. DISTRHO_LEAK_DETECTOR(ImageToggle)
  116. };
  117. // -----------------------------------------------------------------------
  118. END_NAMESPACE_DGL
  119. #endif // DGL_IMAGE_TOGGLE_HPP_INCLUDED