Button.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2018 to 2019 David Forsgren Piuva
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would be
  16. // appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such, and must not be
  19. // misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source
  22. // distribution.
  23. #include "Button.h"
  24. #include <math.h>
  25. using namespace dsr;
  26. PERSISTENT_DEFINITION(Button)
  27. void Button::declareAttributes(StructureDefinition &target) const {
  28. VisualComponent::declareAttributes(target);
  29. target.declareAttribute(U"Color");
  30. target.declareAttribute(U"Text");
  31. }
  32. Persistent* Button::findAttribute(const ReadableString &name) {
  33. if (string_caseInsensitiveMatch(name, U"Color")) {
  34. return &(this->color);
  35. } else if (string_caseInsensitiveMatch(name, U"Text")) {
  36. return &(this->text);
  37. } else {
  38. return VisualComponent::findAttribute(name);
  39. }
  40. }
  41. Button::Button() {}
  42. bool Button::isContainer() const {
  43. return true;
  44. }
  45. static OrderedImageRgbaU8 generateButtonImage(MediaMethod imageGenerator, int pressed, int width, int height, ColorRgbI32 backColor, String text, const std::shared_ptr<RasterFont>& font) {
  46. // Create a scaled image
  47. OrderedImageRgbaU8 result;
  48. imageGenerator(width, height, pressed, backColor.red, backColor.green, backColor.blue)(result);
  49. if (text.length() > 0) {
  50. int left = (image_getWidth(result) - font->getLineWidth(text)) / 2;
  51. int top = (image_getHeight(result) - font->size) / 2;
  52. if (pressed) {
  53. top += 1;
  54. }
  55. font->printLine(result, text, IVector2D(left, top), ColorRgbaI32(0, 0, 0, 255));
  56. }
  57. return result;
  58. }
  59. void Button::generateGraphics() {
  60. int width = this->location.width();
  61. int height = this->location.height();
  62. if (!this->hasImages || this->lastWidth != width || this->lastHeight != height || !string_match(this->text.value, this->lastText) || this->color.value != this->lastColor) {
  63. completeAssets();
  64. this->imageUp = generateButtonImage(this->button, 0, width, height, this->color.value, this->text.value, this->font);
  65. this->imageDown = generateButtonImage(this->button, 1, width, height, this->color.value, this->text.value, this->font);
  66. this->lastWidth = width;
  67. this->lastHeight = height;
  68. this->lastText = this->text.value;
  69. this->lastColor = this->color.value;
  70. this->hasImages = true;
  71. }
  72. }
  73. void Button::drawSelf(ImageRgbaU8& targetImage, const IRect &relativeLocation) {
  74. this->generateGraphics();
  75. draw_alphaFilter(targetImage, (this->pressed && this->inside) ? this->imageDown : this->imageUp, relativeLocation.left(), relativeLocation.top());
  76. }
  77. void Button::receiveMouseEvent(const MouseEvent& event) {
  78. if (event.mouseEventType == MouseEventType::MouseDown) {
  79. this->pressed = true;
  80. } else if (this->pressed && event.mouseEventType == MouseEventType::MouseUp) {
  81. this->pressed = false;
  82. if (this->inside) {
  83. this->callback_pressedEvent();
  84. }
  85. }
  86. this->inside = this->pointIsInside(event.position);
  87. VisualComponent::receiveMouseEvent(event);
  88. }
  89. bool Button::pointIsInside(const IVector2D& pixelPosition) {
  90. this->generateGraphics();
  91. // Get the point relative to the component instead of its direct container
  92. IVector2D localPoint = pixelPosition - this->location.upperLeft();
  93. // Sample opacity at the location
  94. return dsr::image_readPixel_border(this->imageUp, localPoint.x, localPoint.y).alpha > 127;
  95. }
  96. void Button::changedTheme(VisualTheme newTheme) {
  97. this->button = theme_getScalableImage(newTheme, U"Button");
  98. this->hasImages = false;
  99. }
  100. void Button::completeAssets() {
  101. if (this->button.methodIndex == -1) {
  102. this->button = theme_getScalableImage(theme_getDefault(), U"Button");
  103. }
  104. if (this->font.get() == nullptr) {
  105. this->font = font_getDefault();
  106. }
  107. }