Panel.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 "Panel.h"
  24. using namespace dsr;
  25. PERSISTENT_DEFINITION(Panel)
  26. void Panel::declareAttributes(StructureDefinition &target) const {
  27. VisualComponent::declareAttributes(target);
  28. target.declareAttribute(U"Solid");
  29. target.declareAttribute(U"Color");
  30. }
  31. Persistent* Panel::findAttribute(const ReadableString &name) {
  32. if (string_caseInsensitiveMatch(name, U"Solid")) {
  33. return &(this->solid);
  34. } else if (string_caseInsensitiveMatch(name, U"Color") || string_caseInsensitiveMatch(name, U"BackColor")) {
  35. // Both color and backcolor is accepted as names for the only color.
  36. return &(this->color);
  37. } else {
  38. return VisualComponent::findAttribute(name);
  39. }
  40. }
  41. Panel::Panel() {}
  42. bool Panel::isContainer() const {
  43. return true;
  44. }
  45. void Panel::generateGraphics() {
  46. int width = this->location.width();
  47. int height = this->location.height();
  48. if (width < 1) { width = 1; }
  49. if (height < 1) { height = 1; }
  50. if (!this->hasImages) {
  51. completeAssets();
  52. this->generateImage(this->background, width, height, this->color.value.red, this->color.value.green, this->color.value.blue)(this->imageBackground);
  53. this->hasImages = true;
  54. }
  55. }
  56. // Fill the background with a solid color
  57. void Panel::drawSelf(ImageRgbaU8& targetImage, const IRect &relativeLocation) {
  58. if (this->solid.value) {
  59. this->generateGraphics();
  60. draw_copy(targetImage, this->imageBackground, relativeLocation.left(), relativeLocation.top());
  61. //draw_rectangle(targetImage, relativeLocation, ColorRgbaI32(this->color.value, 255));
  62. }
  63. }
  64. void Panel::changedTheme(VisualTheme newTheme) {
  65. this->background = theme_getScalableImage(newTheme, U"Panel");
  66. this->hasImages = false;
  67. }
  68. void Panel::completeAssets() {
  69. if (this->background.methodIndex == -1) {
  70. this->background = theme_getScalableImage(theme_getDefault(), U"Panel");
  71. }
  72. }
  73. void Panel::changedLocation(const IRect &oldLocation, const IRect &newLocation) {
  74. // If the component has changed dimensions then redraw the image
  75. if (oldLocation.size() != newLocation.size()) {
  76. this->hasImages = false;
  77. }
  78. }
  79. void Panel::changedAttribute(const ReadableString &name) {
  80. if (!string_caseInsensitiveMatch(name, U"Visible")) {
  81. this->hasImages = false;
  82. }
  83. }