ListBox.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2020 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 "ListBox.h"
  24. #include <math.h>
  25. using namespace dsr;
  26. PERSISTENT_DEFINITION(ListBox)
  27. void ListBox::declareAttributes(StructureDefinition &target) const {
  28. VisualComponent::declareAttributes(target);
  29. target.declareAttribute(U"Color");
  30. target.declareAttribute(U"List");
  31. target.declareAttribute(U"SelectedIndex");
  32. }
  33. Persistent* ListBox::findAttribute(const ReadableString &name) {
  34. if (string_caseInsensitiveMatch(name, U"Color")) {
  35. return &(this->color);
  36. } else if (string_caseInsensitiveMatch(name, U"List")) {
  37. return &(this->list);
  38. } else if (string_caseInsensitiveMatch(name, U"SelectedIndex")) {
  39. return &(this->selectedIndex);
  40. } else {
  41. return VisualComponent::findAttribute(name);
  42. }
  43. }
  44. ListBox::ListBox() {}
  45. bool ListBox::isContainer() const {
  46. return true;
  47. }
  48. static const int textBorderLeft = 4;
  49. static const int textBorderTop = 4;
  50. void ListBox::generateGraphics() {
  51. int width = this->location.width();
  52. int height = this->location.height();
  53. if (width < 1) { width = 1; }
  54. if (height < 1) { height = 1; }
  55. if (!this->hasImages) {
  56. completeAssets();
  57. this->listBox(width, height, this->color.value.red, this->color.value.green, this->color.value.blue)(this->image);
  58. int verticalStep = this->font->size;
  59. int left = textBorderLeft;
  60. int top = textBorderTop;
  61. for (int64_t i = this->firstVisible; i < this->list.value.length() && top < height; i++) {
  62. ColorRgbaI32 textColor;
  63. if (i == this->pressedIndex) {
  64. textColor = ColorRgbaI32(255, 255, 255, 255);
  65. } else if (i == this->selectedIndex.value) {
  66. textColor = ColorRgbaI32(255, 255, 255, 255);
  67. } else {
  68. textColor = ColorRgbaI32(0, 0, 0, 255);
  69. }
  70. if (i == this->selectedIndex.value) {
  71. draw_rectangle(this->image, IRect(left, top, width - (textBorderLeft * 2), verticalStep), ColorRgbaI32(0, 0, 0, 255));
  72. }
  73. this->font->printLine(this->image, this->list.value[i], IVector2D(left, top), textColor);
  74. top += verticalStep;
  75. }
  76. this->hasImages = true;
  77. }
  78. }
  79. void ListBox::drawSelf(ImageRgbaU8& targetImage, const IRect &relativeLocation) {
  80. this->generateGraphics();
  81. draw_copy(targetImage, this->image, relativeLocation.left(), relativeLocation.top());
  82. }
  83. void ListBox::receiveMouseEvent(const MouseEvent& event) {
  84. this->inside = this->pointIsInside(event.position);
  85. int64_t maxIndex = this->list.value.length() - 1;
  86. int64_t hoverIndex = this->firstVisible + ((event.position.y - this->location.top() - textBorderTop) / this->font->size);
  87. if (hoverIndex > maxIndex) {
  88. hoverIndex = -1;
  89. }
  90. if (event.mouseEventType == MouseEventType::MouseDown) {
  91. this->pressedIndex = hoverIndex;
  92. this->hasImages = false; // Force redraw
  93. } else if (this->pressedIndex > -1 && event.mouseEventType == MouseEventType::MouseUp) {
  94. if (this->inside && hoverIndex == this->pressedIndex) {
  95. this->selectedIndex.value = hoverIndex;
  96. this->callback_pressedEvent();
  97. }
  98. this->pressedIndex = -1;
  99. this->hasImages = false; // Force redraw
  100. } else if (event.mouseEventType == MouseEventType::Scroll) {
  101. if (event.key == MouseKeyEnum::ScrollUp) {
  102. this->firstVisible--;
  103. if (this->firstVisible < 0) {
  104. this->firstVisible = 0;
  105. }
  106. } else if (event.key == MouseKeyEnum::ScrollDown) {
  107. this->firstVisible++;
  108. if (this->firstVisible > maxIndex) {
  109. this->firstVisible = maxIndex;
  110. }
  111. }
  112. this->hasImages = false; // Force redraw
  113. }
  114. VisualComponent::receiveMouseEvent(event);
  115. }
  116. void ListBox::changedTheme(VisualTheme newTheme) {
  117. this->listBox = theme_getScalableImage(newTheme, U"Panel");
  118. this->hasImages = false; // Force redraw
  119. }
  120. void ListBox::completeAssets() {
  121. if (this->listBox.methodIndex == -1) {
  122. this->listBox = theme_getScalableImage(theme_getDefault(), U"Panel");
  123. }
  124. if (this->font.get() == nullptr) {
  125. this->font = font_getDefault();
  126. }
  127. }
  128. void ListBox::changedLocation(IRect &oldLocation, IRect &newLocation) {
  129. // If the component has changed dimensions then redraw the image
  130. if (oldLocation.size() != newLocation.size()) {
  131. this->hasImages = false;
  132. this->limitScrolling();
  133. }
  134. }
  135. void ListBox::changedAttribute(const ReadableString &name) {
  136. // If an attribute has changed then force the image to be redrawn
  137. this->hasImages = false;
  138. if (string_caseInsensitiveMatch(name, U"List")) {
  139. // Reset selection on full list updates
  140. this->selectedIndex.value = 0;
  141. }
  142. this->limitSelection();
  143. this->limitScrolling();
  144. }
  145. int64_t ListBox::getSelectedIndex() {
  146. int64_t index = this->selectedIndex.value;
  147. if (this->selectedIndex.value < 0 || this->selectedIndex.value >= this->list.value.length()) {
  148. index = -1;
  149. }
  150. return index;
  151. }
  152. void ListBox::limitSelection() {
  153. // Get the maximum index
  154. int maxIndex = this->list.value.length() - 1;
  155. if (maxIndex < 0) {
  156. maxIndex = 0;
  157. }
  158. // Reset selection on out of bound
  159. if (this->selectedIndex.value < 0 || this->selectedIndex.value > maxIndex) {
  160. this->selectedIndex.value = 0;
  161. }
  162. }
  163. // Optional limit of scrolling, to be applied when the user don't explicitly scroll away from the selection
  164. // limitSelection should be called before limitScrolling, because scrolling limits depend on selection
  165. void ListBox::limitScrolling() {
  166. // Try to load the font before estimating how big the view is
  167. this->completeAssets();
  168. if (this->font.get() == nullptr) {
  169. throwError("Cannot get the font size because ListBox failed to get a font!\n");
  170. }
  171. int visibleRange = this->location.height() / (this->font->size);
  172. int maxScroll = this->selectedIndex.value;
  173. int minScroll = maxScroll + 1 - visibleRange;
  174. if (this->firstVisible < minScroll) {
  175. this->firstVisible = minScroll;
  176. } else if (this->firstVisible > maxScroll) {
  177. this->firstVisible = maxScroll;
  178. }
  179. }
  180. String ListBox::call(const ReadableString &methodName, const ReadableString &arguments) {
  181. if (string_caseInsensitiveMatch(methodName, U"ClearAll")) {
  182. // Remove all elements from the list
  183. this->list.value.clear();
  184. this->hasImages = false;
  185. this->selectedIndex.value = 0;
  186. this->firstVisible = 0;
  187. return U"";
  188. } else if (string_caseInsensitiveMatch(methodName, U"PushElement")) {
  189. // Push a new element to the list
  190. // No quote mangling needed for this single argument.
  191. this->list.value.push(arguments);
  192. this->selectedIndex.value = this->list.value.length() - 1;
  193. this->limitScrolling();
  194. this->hasImages = false;
  195. return U"";
  196. } else if (string_caseInsensitiveMatch(methodName, U"RemoveElement")) {
  197. // Remove an element who's index is given in the only input argument
  198. int64_t index = string_parseInteger(arguments);
  199. if (index < 0 || index >= this->list.value.length()) {
  200. throwError("Index (", arguments, " = ", index, ") out of bound in RemoveElement!\n");
  201. }
  202. this->list.value.remove(index);
  203. this->limitSelection();
  204. this->limitScrolling();
  205. this->hasImages = false;
  206. return U"";
  207. } else if (string_caseInsensitiveMatch(methodName, U"GetLength")) {
  208. // Returns the length of the list
  209. return string_combine(this->list.value.length());
  210. } else if (string_caseInsensitiveMatch(methodName, U"GetSelectedIndex")) {
  211. // Returns the selected index or -1 if nothing is selected
  212. return string_combine(this->getSelectedIndex());
  213. } else if (string_caseInsensitiveMatch(methodName, U"GetSelectedText")) {
  214. // Returns the selected element's text or an empty string if nothing is selected
  215. int64_t index = getSelectedIndex();
  216. if (index > -1) {
  217. return this->list.value[index];
  218. } else {
  219. return U"";
  220. }
  221. } else {
  222. return VisualComponent::call(methodName, arguments);
  223. }
  224. }