2
0

Button.cpp 6.2 KB

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