Button.cpp 5.4 KB

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