Panel.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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"Plain");
  30. target.declareAttribute(U"Color");
  31. target.declareAttribute(U"BackgroundClass");
  32. }
  33. Persistent* Panel::findAttribute(const ReadableString &name) {
  34. if (string_caseInsensitiveMatch(name, U"Solid")) {
  35. return &(this->solid);
  36. } else if (string_caseInsensitiveMatch(name, U"Plain")) {
  37. return &(this->plain);
  38. } else if (string_caseInsensitiveMatch(name, U"Color") || string_caseInsensitiveMatch(name, U"BackColor")) {
  39. // Both color and backcolor is accepted as names for the only color.
  40. return &(this->color);
  41. } else if (string_caseInsensitiveMatch(name, U"Class") || string_caseInsensitiveMatch(name, U"BackgroundClass")) {
  42. return &(this->backgroundClass);
  43. } else {
  44. return VisualComponent::findAttribute(name);
  45. }
  46. }
  47. Panel::Panel() {}
  48. bool Panel::isContainer() const {
  49. return true;
  50. }
  51. void Panel::generateGraphics() {
  52. int width = this->location.width();
  53. int height = this->location.height();
  54. if (width < 1) { width = 1; }
  55. if (height < 1) { height = 1; }
  56. if (!this->hasImages) {
  57. completeAssets();
  58. component_generateImage(this->theme, this->background, width, height, this->color.value.red, this->color.value.green, this->color.value.blue)(this->imageBackground);
  59. this->hasImages = true;
  60. }
  61. }
  62. // Fill the background with a solid color
  63. void Panel::drawSelf(ImageRgbaU8& targetImage, const IRect &relativeLocation) {
  64. if (this->solid.value) {
  65. if (this->plain.value) {
  66. draw_rectangle(targetImage, relativeLocation, ColorRgbaI32(this->color.value, 255));
  67. } else {
  68. this->generateGraphics();
  69. if (this->background_filter == 1) {
  70. draw_alphaFilter(targetImage, this->imageBackground, relativeLocation.left(), relativeLocation.top());
  71. } else {
  72. draw_copy(targetImage, this->imageBackground, relativeLocation.left(), relativeLocation.top());
  73. }
  74. }
  75. }
  76. }
  77. void Panel::loadTheme(const VisualTheme &theme) {
  78. this->finalBackgroundClass = theme_selectClass(theme, this->backgroundClass.value, U"Panel");
  79. this->background = theme_getScalableImage(theme, this->finalBackgroundClass);
  80. this->background_filter = theme_getInteger(theme, this->finalBackgroundClass, U"Filter", 0);
  81. }
  82. void Panel::changedTheme(VisualTheme newTheme) {
  83. this->loadTheme(newTheme);
  84. this->hasImages = false;
  85. }
  86. void Panel::completeAssets() {
  87. if (this->background.methodIndex == -1) {
  88. this->loadTheme(theme_getDefault());
  89. }
  90. }
  91. void Panel::changedLocation(const IRect &oldLocation, const IRect &newLocation) {
  92. // If the component has changed dimensions then redraw the image
  93. if (oldLocation.size() != newLocation.size()) {
  94. this->hasImages = false;
  95. }
  96. }
  97. void Panel::changedAttribute(const ReadableString &name) {
  98. if (string_caseInsensitiveMatch(name, U"BackgroundClass")) {
  99. // Update from the theme if the theme class has changed.
  100. this->changedTheme(this->getTheme());
  101. } else if (!string_caseInsensitiveMatch(name, U"Visible")) {
  102. this->hasImages = false;
  103. }
  104. VisualComponent::changedAttribute(name);
  105. }