Button.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 false;
  44. }
  45. static OrderedImageRgbaU8 generateButtonImage(MediaMethod imageGenerator, int pressed, int width, int height, ColorRgbI32 backColor, String text, 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(font, text)) / 2;
  51. int top = (image_getHeight(result) - font_getSize(font)) / 2;
  52. if (pressed) {
  53. top += 1;
  54. }
  55. font_printLine(result, font, 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 (width < 1) { width = 1; }
  63. if (height < 1) { height = 1; }
  64. if (!this->hasImages) {
  65. completeAssets();
  66. this->imageUp = generateButtonImage(this->button, 0, width, height, this->color.value, this->text.value, this->font);
  67. this->imageDown = generateButtonImage(this->button, 1, width, height, this->color.value, this->text.value, this->font);
  68. this->hasImages = true;
  69. }
  70. }
  71. void Button::drawSelf(ImageRgbaU8& targetImage, const IRect &relativeLocation) {
  72. this->generateGraphics();
  73. draw_alphaFilter(targetImage, (this->pressed && this->inside) ? this->imageDown : this->imageUp, relativeLocation.left(), relativeLocation.top());
  74. }
  75. void Button::receiveMouseEvent(const MouseEvent& event) {
  76. if (event.mouseEventType == MouseEventType::MouseDown) {
  77. this->pressed = true;
  78. } else if (this->pressed && event.mouseEventType == MouseEventType::MouseUp) {
  79. this->pressed = false;
  80. if (this->inside) {
  81. this->callback_pressedEvent();
  82. }
  83. }
  84. this->inside = this->pointIsInside(event.position);
  85. VisualComponent::receiveMouseEvent(event);
  86. }
  87. bool Button::pointIsInside(const IVector2D& pixelPosition) {
  88. this->generateGraphics();
  89. // Get the point relative to the component instead of its direct container
  90. IVector2D localPoint = pixelPosition - this->location.upperLeft();
  91. // Sample opacity at the location
  92. return dsr::image_readPixel_border(this->imageUp, localPoint.x, localPoint.y).alpha > 127;
  93. }
  94. void Button::changedTheme(VisualTheme newTheme) {
  95. this->button = theme_getScalableImage(newTheme, U"Button");
  96. this->hasImages = false;
  97. }
  98. void Button::completeAssets() {
  99. if (this->button.methodIndex == -1) {
  100. this->button = theme_getScalableImage(theme_getDefault(), U"Button");
  101. }
  102. if (this->font.get() == nullptr) {
  103. this->font = font_getDefault();
  104. }
  105. }
  106. void Button::changedLocation(IRect &oldLocation, IRect &newLocation) {
  107. // If the component has changed dimensions then redraw the image
  108. if (oldLocation.size() != newLocation.size()) {
  109. this->hasImages = false;
  110. }
  111. }
  112. void Button::changedAttribute(const ReadableString &name) {
  113. // If an attribute has changed then force the image to be redrawn
  114. this->hasImages = false;
  115. }