Picture.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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::generateGraphics() {
  65. int width = this->location.width();
  66. int height = this->location.height();
  67. if (width < 1) { width = 1; }
  68. if (height < 1) { height = 1; }
  69. if (!this->hasImages) {
  70. this->finalImage = filter_resize(this->image.value, this->interpolation.value ? Sampler::Linear : Sampler::Nearest, width, height);
  71. this->hasImages = true;
  72. }
  73. }
  74. void Picture::changedLocation(const IRect &oldLocation, const IRect &newLocation) {
  75. // If the component has changed dimensions then redraw the image
  76. if (oldLocation.size() != newLocation.size()) {
  77. this->hasImages = false;
  78. }
  79. }
  80. void Picture::changedAttribute(const ReadableString &name) {
  81. // If an attribute has changed then force the image to be redrawn
  82. this->hasImages = false;
  83. }