Toolbar.cpp 6.3 KB

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