Button.cpp 4.3 KB

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