Button.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. target.declareAttribute(U"Padding");
  32. }
  33. Persistent* Button::findAttribute(const ReadableString &name) {
  34. if (string_caseInsensitiveMatch(name, U"Color") || string_caseInsensitiveMatch(name, U"BackColor")) {
  35. // The short Color alias refers to the back color in Buttons, because most buttons use black text.
  36. return &(this->backColor);
  37. } else if (string_caseInsensitiveMatch(name, U"ForeColor")) {
  38. return &(this->foreColor);
  39. } else if (string_caseInsensitiveMatch(name, U"Text")) {
  40. return &(this->text);
  41. } else if (string_caseInsensitiveMatch(name, U"Padding")) {
  42. return &(this->padding);
  43. } else {
  44. return VisualComponent::findAttribute(name);
  45. }
  46. }
  47. Button::Button() {}
  48. bool Button::isContainer() const {
  49. return false;
  50. }
  51. static OrderedImageRgbaU8 generateButtonImage(Button &button, MediaMethod imageGenerator, int pressed, int width, int height, ColorRgbI32 backColor, ColorRgbI32 foreColor, String text, RasterFont font) {
  52. // Create a scaled image
  53. OrderedImageRgbaU8 result;
  54. component_generateImage(button.getTheme(), imageGenerator, width, height, backColor.red, backColor.green, backColor.blue, pressed)(result);
  55. if (string_length(text) > 0) {
  56. int left = (image_getWidth(result) - font_getLineWidth(font, text)) / 2;
  57. int top = (image_getHeight(result) - font_getSize(font)) / 2;
  58. if (pressed) {
  59. top += 1;
  60. }
  61. font_printLine(result, font, text, IVector2D(left, top), ColorRgbaI32(foreColor, 255));
  62. }
  63. return result;
  64. }
  65. void Button::generateGraphics() {
  66. int width = this->location.width();
  67. int height = this->location.height();
  68. if (width < 1) { width = 1; }
  69. if (height < 1) { height = 1; }
  70. if (!this->hasImages) {
  71. completeAssets();
  72. this->imageUp = generateButtonImage(*this, this->button, 0, width, height, this->backColor.value, this->foreColor.value, this->text.value, this->font);
  73. this->imageDown = generateButtonImage(*this, this->button, 1, width, height, this->backColor.value, this->foreColor.value, this->text.value, this->font);
  74. this->hasImages = true;
  75. }
  76. }
  77. void Button::drawSelf(ImageRgbaU8& targetImage, const IRect &relativeLocation) {
  78. this->generateGraphics();
  79. draw_alphaFilter(targetImage, (this->pressed && this->inside) ? this->imageDown : this->imageUp, relativeLocation.left(), relativeLocation.top());
  80. }
  81. void Button::receiveMouseEvent(const MouseEvent& event) {
  82. if (event.mouseEventType == MouseEventType::MouseDown) {
  83. this->pressed = true;
  84. } else if (this->pressed && event.mouseEventType == MouseEventType::MouseUp) {
  85. this->pressed = false;
  86. if (this->inside) {
  87. this->callback_pressedEvent();
  88. }
  89. }
  90. this->inside = this->pointIsInside(event.position);
  91. VisualComponent::receiveMouseEvent(event);
  92. }
  93. bool Button::pointIsInside(const IVector2D& pixelPosition) {
  94. this->generateGraphics();
  95. // Get the point relative to the component instead of its direct container
  96. IVector2D localPoint = pixelPosition - this->location.upperLeft();
  97. // Sample opacity at the location
  98. return dsr::image_readPixel_border(this->imageUp, localPoint.x, localPoint.y).alpha > 127;
  99. }
  100. void Button::changedTheme(VisualTheme newTheme) {
  101. this->button = theme_getScalableImage(newTheme, U"Button");
  102. this->hasImages = false;
  103. }
  104. void Button::completeAssets() {
  105. if (this->button.methodIndex == -1) {
  106. this->button = theme_getScalableImage(theme_getDefault(), U"Button");
  107. }
  108. if (this->font.get() == nullptr) {
  109. this->font = font_getDefault();
  110. }
  111. }
  112. void Button::changedLocation(const IRect &oldLocation, const IRect &newLocation) {
  113. // If the component has changed dimensions then redraw the image
  114. if (oldLocation.size() != newLocation.size()) {
  115. this->hasImages = false;
  116. }
  117. }
  118. void Button::changedAttribute(const ReadableString &name) {
  119. if (!string_caseInsensitiveMatch(name, U"Visible")) {
  120. this->hasImages = false;
  121. }
  122. }
  123. IVector2D Button::getDesiredDimensions() {
  124. this->completeAssets();
  125. int sizeAdder = this->padding.value * 2;
  126. return IVector2D(font_getLineWidth(this->font, this->text.value) + sizeAdder, font_getSize(this->font) + sizeAdder);
  127. }