2
0

Picture.cpp 3.6 KB

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