Picture.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 {
  38. return VisualComponent::findAttribute(name);
  39. }
  40. }
  41. Picture::Picture() {}
  42. bool Picture::isContainer() const {
  43. return false;
  44. }
  45. void Picture::drawSelf(ImageRgbaU8& targetImage, const IRect &relativeLocation) {
  46. if (image_exists(this->image.value)) {
  47. this->generateGraphics();
  48. draw_alphaFilter(targetImage, this->finalImage, relativeLocation.left(), relativeLocation.top());
  49. }
  50. }
  51. bool Picture::pointIsInside(const IVector2D& pixelPosition) {
  52. return false;
  53. }
  54. void Picture::generateGraphics() {
  55. int width = this->location.width();
  56. int height = this->location.height();
  57. if (width < 1) { width = 1; }
  58. if (height < 1) { height = 1; }
  59. if (!this->hasImages) {
  60. this->finalImage = filter_resize(this->image.value, this->interpolation.value ? Sampler::Linear : Sampler::Nearest, width, height);
  61. this->hasImages = true;
  62. }
  63. }
  64. void Picture::changedLocation(const IRect &oldLocation, const IRect &newLocation) {
  65. // If the component has changed dimensions then redraw the image
  66. if (oldLocation.size() != newLocation.size()) {
  67. this->hasImages = false;
  68. }
  69. }
  70. void Picture::changedAttribute(const ReadableString &name) {
  71. // If an attribute has changed then force the image to be redrawn
  72. this->hasImages = false;
  73. }