Panel.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 (!this->hasImages) {
  48. completeAssets();
  49. this->background(width, height, this->color.value.red, this->color.value.green, this->color.value.blue)(this->imageBackground);
  50. this->hasImages = true;
  51. }
  52. }
  53. // Fill the background with a solid color
  54. void Panel::drawSelf(ImageRgbaU8& targetImage, const IRect &relativeLocation) {
  55. if (this->solid.value) {
  56. this->generateGraphics();
  57. draw_copy(targetImage, this->imageBackground, relativeLocation.left(), relativeLocation.top());
  58. //draw_rectangle(targetImage, relativeLocation, ColorRgbaI32(this->color.value, 255));
  59. }
  60. }
  61. void Panel::changedTheme(VisualTheme newTheme) {
  62. this->background = theme_getScalableImage(newTheme, U"Panel");
  63. this->hasImages = false;
  64. }
  65. void Panel::completeAssets() {
  66. if (this->background.methodIndex == -1) {
  67. this->background = theme_getScalableImage(theme_getDefault(), U"Panel");
  68. }
  69. }
  70. void Panel::changedLocation(IRect &oldLocation, IRect &newLocation) {
  71. // If the component has changed dimensions then redraw the image
  72. if (oldLocation.size() != newLocation.size()) {
  73. this->hasImages = false;
  74. }
  75. }
  76. void Panel::changedAttribute(const ReadableString &name) {
  77. // If an attribute has changed then force the image to be redrawn
  78. this->hasImages = false;
  79. }