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.

107 lines
2.5KB

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