Panel.cpp 3.4 KB

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