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.

103 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. void Button::setBackgroundColor(const Color color)
  39. {
  40. backgroundColor = color;
  41. }
  42. void Button::setFontScale(const float scale)
  43. {
  44. fontScale = scale;
  45. }
  46. void Button::setLabel(const std::string& label2)
  47. {
  48. label = label2;
  49. }
  50. void Button::setLabelColor(const Color color)
  51. {
  52. labelColor = color;
  53. }
  54. void Button::onNanoDisplay()
  55. {
  56. const uint w = getWidth();
  57. const uint h = getHeight();
  58. const float margin = 1.0f;
  59. // Background
  60. beginPath();
  61. fillColor(backgroundColor);
  62. strokeColor(labelColor);
  63. rect(margin, margin, w - 2 * margin, h - 2 * margin);
  64. fill();
  65. stroke();
  66. closePath();
  67. // Label
  68. beginPath();
  69. fontSize(14 * fontScale);
  70. fillColor(labelColor);
  71. Rectangle<float> bounds;
  72. textBounds(0, 0, label.c_str(), NULL, bounds);
  73. float tx = w / 2.0f ;
  74. float ty = h / 2.0f;
  75. textAlign(ALIGN_CENTER | ALIGN_MIDDLE);
  76. fillColor(255, 255, 255, 255);
  77. text(tx, ty, label.c_str(), NULL);
  78. closePath();
  79. }
  80. bool Button::onMouse(const MouseEvent& ev)
  81. {
  82. return ButtonEventHandler::mouseEvent(ev);
  83. }
  84. bool Button::onMotion(const MotionEvent& ev)
  85. {
  86. return ButtonEventHandler::motionEvent(ev);
  87. }
  88. END_NAMESPACE_DGL