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.

125 lines
3.3KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2016 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 "../ImageWidgets.hpp"
  19. START_NAMESPACE_DGL
  20. // -----------------------------------------------------------------------
  21. struct ButtonImpl {
  22. enum State {
  23. kStateNormal = 0,
  24. kStateHover,
  25. kStateDown
  26. };
  27. int button;
  28. int state;
  29. Widget* self;
  30. ImageButton::Callback* callback_img;
  31. ButtonImpl(Widget* const s) noexcept
  32. : button(-1),
  33. state(kStateNormal),
  34. self(s),
  35. callback_img(nullptr) {}
  36. bool onMouse(const Widget::MouseEvent& ev)
  37. {
  38. // button was released, handle it now
  39. if (button != -1 && ! ev.press)
  40. {
  41. DISTRHO_SAFE_ASSERT(state == kStateDown);
  42. // release button
  43. const int button2 = button;
  44. button = -1;
  45. // cursor was moved outside the button bounds, ignore click
  46. if (! self->contains(ev.pos))
  47. {
  48. state = kStateNormal;
  49. self->repaint();
  50. return true;
  51. }
  52. // still on bounds, register click
  53. state = kStateHover;
  54. self->repaint();
  55. if (callback_img != nullptr)
  56. callback_img->imageButtonClicked((ImageButton*)self, button2);
  57. return true;
  58. }
  59. // button was pressed, wait for release
  60. if (ev.press && self->contains(ev.pos))
  61. {
  62. button = ev.button;
  63. state = kStateDown;
  64. self->repaint();
  65. return true;
  66. }
  67. return false;
  68. }
  69. bool onMotion(const Widget::MotionEvent& ev)
  70. {
  71. // keep pressed
  72. if (button != -1)
  73. return true;
  74. if (self->contains(ev.pos))
  75. {
  76. // check if entering hover
  77. if (state == kStateNormal)
  78. {
  79. state = kStateHover;
  80. self->repaint();
  81. return true;
  82. }
  83. }
  84. else
  85. {
  86. // check if exiting hover
  87. if (state == kStateHover)
  88. {
  89. state = kStateNormal;
  90. self->repaint();
  91. return true;
  92. }
  93. }
  94. return false;
  95. }
  96. DISTRHO_PREVENT_HEAP_ALLOCATION
  97. DISTRHO_DECLARE_NON_COPY_STRUCT(ButtonImpl)
  98. };
  99. // -----------------------------------------------------------------------
  100. END_NAMESPACE_DGL
  101. #endif // DGL_APP_PRIVATE_DATA_HPP_INCLUDED