Toolbar.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2018 to 2023 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 "Toolbar.h"
  24. using namespace dsr;
  25. PERSISTENT_DEFINITION(Toolbar)
  26. void Toolbar::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"Padding");
  32. target.declareAttribute(U"Spacing");
  33. }
  34. Persistent* Toolbar::findAttribute(const ReadableString &name) {
  35. if (string_caseInsensitiveMatch(name, U"Solid")) {
  36. return &(this->solid);
  37. } else if (string_caseInsensitiveMatch(name, U"Plain")) {
  38. return &(this->plain);
  39. } else if (string_caseInsensitiveMatch(name, U"Color") || string_caseInsensitiveMatch(name, U"BackColor")) {
  40. // Both color and backcolor is accepted as names for the only color.
  41. return &(this->color);
  42. } else if (string_caseInsensitiveMatch(name, U"Padding")) {
  43. return &(this->padding);
  44. } else if (string_caseInsensitiveMatch(name, U"Spacing")) {
  45. return &(this->spacing);
  46. } else {
  47. return VisualComponent::findAttribute(name);
  48. }
  49. }
  50. Toolbar::Toolbar() {}
  51. bool Toolbar::isContainer() const {
  52. return true;
  53. }
  54. void Toolbar::generateGraphics() {
  55. int width = this->location.width();
  56. int height = this->location.height();
  57. if (width < 1) { width = 1; }
  58. if (height < 1) { height = 1; }
  59. if (!this->hasImages) {
  60. completeAssets();
  61. component_generateImage(this->theme, this->background, width, height, this->color.value.red, this->color.value.green, this->color.value.blue)(this->imageBackground);
  62. this->hasImages = true;
  63. }
  64. }
  65. // Fill the background with a solid color
  66. void Toolbar::drawSelf(ImageRgbaU8& targetImage, const IRect &relativeLocation) {
  67. if (this->solid.value) {
  68. if (this->plain.value) {
  69. draw_rectangle(targetImage, relativeLocation, ColorRgbaI32(this->color.value, 255));
  70. } else {
  71. this->generateGraphics();
  72. draw_copy(targetImage, this->imageBackground, relativeLocation.left(), relativeLocation.top());
  73. }
  74. }
  75. }
  76. void Toolbar::changedTheme(VisualTheme newTheme) {
  77. this->background = theme_getScalableImage(newTheme, U"Toolbar");
  78. this->hasImages = false;
  79. }
  80. void Toolbar::completeAssets() {
  81. if (this->background.methodIndex == -1) {
  82. this->background = theme_getScalableImage(theme_getDefault(), U"Toolbar");
  83. }
  84. }
  85. void Toolbar::changedLocation(const IRect &oldLocation, const IRect &newLocation) {
  86. // If the component has changed dimensions then redraw the image
  87. if (oldLocation.size() != newLocation.size()) {
  88. this->hasImages = false;
  89. }
  90. }
  91. void Toolbar::changedAttribute(const ReadableString &name) {
  92. if (!string_caseInsensitiveMatch(name, U"Visible")) {
  93. this->hasImages = false;
  94. }
  95. VisualComponent::changedAttribute(name);
  96. }
  97. void Toolbar::updateLocationEvent(const IRect& oldLocation, const IRect& newLocation) {
  98. int widthStretch = this->region.right.getRatio() - this->region.left.getRatio();
  99. int heightStretch = this->region.bottom.getRatio() - this->region.top.getRatio();
  100. // Place members vertically if the toolbar mostly stretches vertically or if it has uniform stretch and is taller than wide.
  101. bool vertical = (widthStretch < heightStretch) || ((widthStretch == heightStretch) && (newLocation.width() < newLocation.height()));
  102. if (vertical) {
  103. // TODO: Add scroll buttons on the sides if there is not enough space for all child components.
  104. // Place each child component in order.
  105. // Each child is created within a segmented region, but can choose to add more padding or limit its height for fine adjustments.
  106. int left = this->padding.value;
  107. int top = this->padding.value;
  108. int width = newLocation.width() - (this->padding.value * 2);
  109. for (int i = 0; i < this->getChildCount(); i++) {
  110. int height = this->children[i]->getDesiredDimensions().y;
  111. this->children[i]->applyLayout(IRect(left, top, width, height));
  112. top += height + this->spacing.value;
  113. }
  114. } else {
  115. // TODO: Add scroll buttons on the sides if there is not enough space for all child components.
  116. // Place each child component in order.
  117. // Each child is created within a segmented region, but can choose to add more padding or limit its height for fine adjustments.
  118. int left = this->padding.value;
  119. int top = this->padding.value;
  120. int height = newLocation.height() - (this->padding.value * 2);
  121. for (int i = 0; i < this->getChildCount(); i++) {
  122. int width = this->children[i]->getDesiredDimensions().x;
  123. this->children[i]->applyLayout(IRect(left, top, width, height));
  124. left += width + this->spacing.value;
  125. }
  126. }
  127. }