Panel.cpp 3.1 KB

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