Picture.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2021 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 "Picture.h"
  24. #include "../../../api/filterAPI.h"
  25. using namespace dsr;
  26. PERSISTENT_DEFINITION(Picture)
  27. void Picture::declareAttributes(StructureDefinition &target) const {
  28. VisualComponent::declareAttributes(target);
  29. target.declareAttribute(U"Image");
  30. target.declareAttribute(U"ImagePressed");
  31. target.declareAttribute(U"Interpolation");
  32. }
  33. Persistent* Picture::findAttribute(const ReadableString &name) {
  34. if (string_caseInsensitiveMatch(name, U"Image")) {
  35. return &(this->image);
  36. } else if (string_caseInsensitiveMatch(name, U"ImagePressed")) {
  37. return &(this->imagePressed);
  38. } else if (string_caseInsensitiveMatch(name, U"Interpolation")) {
  39. return &(this->interpolation);
  40. } else if (string_caseInsensitiveMatch(name, U"Clickable")) {
  41. return &(this->clickable);
  42. } else {
  43. return VisualComponent::findAttribute(name);
  44. }
  45. }
  46. Picture::Picture() {}
  47. bool Picture::isContainer() const {
  48. return false;
  49. }
  50. void Picture::drawSelf(ImageRgbaU8& targetImage, const IRect &relativeLocation) {
  51. if (image_exists(this->image.value)) {
  52. this->generateGraphics();
  53. draw_alphaFilter(targetImage, (this->pressed && this->inside && image_exists(this->finalImagePressed)) ? this->finalImagePressed : this->finalImage, relativeLocation.left(), relativeLocation.top());
  54. }
  55. }
  56. bool Picture::pointIsInside(const IVector2D& pixelPosition) {
  57. if (this->clickable.value) {
  58. this->generateGraphics();
  59. // Get the point relative to the component instead of its direct container
  60. IVector2D localPoint = pixelPosition - this->location.upperLeft();
  61. // Sample opacity at the location
  62. return dsr::image_readPixel_border(this->finalImage, localPoint.x, localPoint.y).alpha > 127;
  63. } else {
  64. return false;
  65. }
  66. }
  67. void Picture::receiveMouseEvent(const MouseEvent& event) {
  68. if (event.mouseEventType == MouseEventType::MouseDown) {
  69. this->pressed = true;
  70. } else if (this->pressed && event.mouseEventType == MouseEventType::MouseUp) {
  71. this->pressed = false;
  72. if (this->inside) {
  73. this->callback_pressedEvent();
  74. }
  75. }
  76. this->inside = this->pointIsInside(event.position);
  77. VisualComponent::receiveMouseEvent(event);
  78. }
  79. void Picture::generateGraphics() {
  80. int width = this->location.width();
  81. int height = this->location.height();
  82. if (width < 1) { width = 1; }
  83. if (height < 1) { height = 1; }
  84. if (!this->hasImages) {
  85. this->finalImage = filter_resize(this->image.value, this->interpolation.value ? Sampler::Linear : Sampler::Nearest, width, height);
  86. this->finalImagePressed = filter_resize(this->imagePressed.value, this->interpolation.value ? Sampler::Linear : Sampler::Nearest, width, height);
  87. this->hasImages = true;
  88. }
  89. }
  90. void Picture::changedLocation(const IRect &oldLocation, const IRect &newLocation) {
  91. // If the component has changed dimensions then redraw the image
  92. if (oldLocation.size() != newLocation.size()) {
  93. this->hasImages = false;
  94. }
  95. }
  96. void Picture::changedAttribute(const ReadableString &name) {
  97. if (!string_caseInsensitiveMatch(name, U"Visible")) {
  98. this->hasImages = false;
  99. }
  100. VisualComponent::changedAttribute(name);
  101. }