ListBox.cpp 8.3 KB

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