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.

105 lines
2.5KB

  1. /*
  2. * Copyright (C) 2018-2019 Rob van den Berg <rghvdberg at gmail dot org>
  3. *
  4. * This file is part of CharacterCompressor
  5. *
  6. * Nnjas2 is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * CharacterCompressor is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with CharacterCompressor. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. #include "NanoButton.hpp"
  20. #include "Window.hpp"
  21. START_NAMESPACE_DISTRHO
  22. Button::Button(Widget *parent, Callback *cb)
  23. : NanoWidget(parent),
  24. fCallback(cb),
  25. buttonActive(false)
  26. {
  27. loadSharedResources();
  28. fNanoFont = findFont(NANOVG_DEJAVU_SANS_TTF);
  29. labelColor = Color(255, 255, 255);
  30. backgroundColor = Color(32,32,32);
  31. Label = "button";
  32. }
  33. void Button::onNanoDisplay()
  34. {
  35. auto w = getWidth();
  36. auto h = getHeight();
  37. auto margin = 1.0f;
  38. // Background
  39. beginPath();
  40. fillColor(backgroundColor);
  41. strokeColor(borderColor);
  42. rect(margin, margin, w - 2 * margin, h-2*margin);
  43. fill();
  44. stroke();
  45. closePath();
  46. //Label
  47. beginPath();
  48. fontFaceId(fNanoFont);
  49. fontSize(14);
  50. fillColor(labelColor);
  51. Rectangle<float> bounds;
  52. textBounds(0, 0, Label.c_str(), NULL, bounds);
  53. // float tw = bounds.getWidth();
  54. // float th = bounds.getHeight();
  55. float tx = w / 2.0f ;
  56. float ty = h / 2.0f;
  57. textAlign(ALIGN_CENTER | ALIGN_MIDDLE);
  58. fillColor(255, 255, 255, 255);
  59. text(tx, ty, Label.c_str(), NULL);
  60. closePath();
  61. }
  62. void Button::setLabel(std::string label)
  63. {
  64. Label = label;
  65. }
  66. void Button::setLabelColor(const Color color)
  67. {
  68. labelColor = color;
  69. borderColor = color;
  70. }
  71. void Button::setBackgroundColor(const Color color)
  72. {
  73. backgroundColor = color;
  74. }
  75. bool Button::onMouse(const MouseEvent &ev)
  76. {
  77. if (ev.press && contains(ev.pos))
  78. {
  79. buttonActive = true;
  80. setLabelColor(labelColor);
  81. fCallback->buttonClicked(this, true);
  82. return true;
  83. }
  84. else if (buttonActive)
  85. {
  86. buttonActive = false;
  87. //setLabelColor(Color(128,128,128));
  88. return true;
  89. }
  90. return false;
  91. }
  92. END_NAMESPACE_DISTRHO