Menu.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 "Menu.h"
  24. using namespace dsr;
  25. PERSISTENT_DEFINITION(Menu)
  26. void Menu::declareAttributes(StructureDefinition &target) const {
  27. VisualComponent::declareAttributes(target);
  28. target.declareAttribute(U"BackColor");
  29. target.declareAttribute(U"ForeColor");
  30. target.declareAttribute(U"Text");
  31. target.declareAttribute(U"Padding");
  32. target.declareAttribute(U"Spacing");
  33. }
  34. Persistent* Menu::findAttribute(const ReadableString &name) {
  35. if (string_caseInsensitiveMatch(name, U"Color") || string_caseInsensitiveMatch(name, U"BackColor")) {
  36. // The short Color alias refers to the back color in Buttons, because most buttons use black text.
  37. return &(this->backColor);
  38. } else if (string_caseInsensitiveMatch(name, U"ForeColor")) {
  39. return &(this->foreColor);
  40. } else if (string_caseInsensitiveMatch(name, U"Text")) {
  41. return &(this->text);
  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. Menu::Menu() {}
  51. bool Menu::isContainer() const {
  52. return true;
  53. }
  54. static OrderedImageRgbaU8 generateHeadImage(Menu &menu, MediaMethod imageGenerator, int pressed, int width, int height, ColorRgbI32 backColor, ColorRgbI32 foreColor, const ReadableString &text, RasterFont font) {
  55. // Create a scaled image
  56. OrderedImageRgbaU8 result;
  57. component_generateImage(menu.getTheme(), imageGenerator, width, height, backColor.red, backColor.green, backColor.blue, pressed)(result);
  58. if (string_length(text) > 0) {
  59. int left = (image_getWidth(result) - font_getLineWidth(font, text)) / 2;
  60. int top = (image_getHeight(result) - font_getSize(font)) / 2;
  61. if (pressed) {
  62. top += 1;
  63. }
  64. font_printLine(result, font, text, IVector2D(left, top), ColorRgbaI32(foreColor, 255));
  65. }
  66. return result;
  67. }
  68. void Menu::generateGraphics() {
  69. int headWidth = this->location.width();
  70. int headHeight = this->location.height();
  71. if (headWidth < 1) { headWidth = 1; }
  72. if (headHeight < 1) { headHeight = 1; }
  73. if (!this->hasImages) {
  74. completeAssets();
  75. this->imageUp = generateHeadImage(*this, this->headImageMethod, 0, headWidth, headHeight, this->backColor.value, this->foreColor.value, this->text.value, this->font);
  76. this->imageDown = generateHeadImage(*this, this->headImageMethod, 0, headWidth, headHeight, ColorRgbI32(0, 0, 0), ColorRgbI32(255, 255, 255), this->text.value, this->font);
  77. this->hasImages = true;
  78. }
  79. }
  80. // Fill the listBackgroundImageMethod with a solid color
  81. void Menu::drawSelf(ImageRgbaU8& targetImage, const IRect &relativeLocation) {
  82. this->generateGraphics();
  83. draw_alphaFilter(targetImage, this->showingOverlay ? this->imageDown : this->imageUp, relativeLocation.left(), relativeLocation.top());
  84. }
  85. void Menu::generateBackground() {
  86. if (!image_exists(this->listBackgroundImage)) {
  87. int listWidth = this->overlayLocation.width();
  88. int listHeight = this->overlayLocation.height();
  89. if (listWidth < 1) { listWidth = 1; }
  90. if (listHeight < 1) { listHeight = 1; }
  91. component_generateImage(this->theme, this->listBackgroundImageMethod, listWidth, listHeight, this->backColor.value.red, this->backColor.value.green, this->backColor.value.blue)(this->listBackgroundImage);
  92. }
  93. }
  94. void Menu::showOverlay() {
  95. // Both showingOverlay and makeFocused are currently required for the overlay to be visible.
  96. this->showingOverlay = true;
  97. this->makeFocused();
  98. IRect memberBound = this->children[0]->location;
  99. for (int i = 1; i < this->getChildCount(); i++) {
  100. memberBound = IRect::merge(memberBound, this->children[i]->location);
  101. }
  102. this->overlayLocation = memberBound.expanded(this->padding.value) + this->location.upperLeft();
  103. }
  104. bool Menu::managesChildren() {
  105. return true;
  106. }
  107. bool Menu::pointIsInsideOfOverlay(const IVector2D& pixelPosition) {
  108. return pixelPosition.x > this->overlayLocation.left() && pixelPosition.x < this->overlayLocation.right() && pixelPosition.y > this->overlayLocation.top() && pixelPosition.y < this->overlayLocation.bottom();
  109. }
  110. // TODO: Draw decorations using headImage.
  111. void Menu::drawOverlay(ImageRgbaU8& targetImage, const IVector2D &absoluteOffset) {
  112. if (this->showingOverlay) {
  113. this->generateBackground();
  114. // TODO: Let the theme select between solid and alpha filtered drawing.
  115. IVector2D overlayOffset = absoluteOffset + this->overlayLocation.upperLeft();
  116. draw_copy(targetImage, this->listBackgroundImage, overlayOffset.x, overlayOffset.y);
  117. for (int i = 0; i < this->getChildCount(); i++) {
  118. this->children[i]->draw(targetImage, absoluteOffset + this->location.upperLeft());
  119. }
  120. }
  121. }
  122. void Menu::changedTheme(VisualTheme newTheme) {
  123. this->headImageMethod = theme_getScalableImage(newTheme, this->subMenu ? U"MenuSub" : U"MenuTop");
  124. this->listBackgroundImageMethod = theme_getScalableImage(newTheme, U"MenuList");
  125. this->hasImages = false;
  126. }
  127. void Menu::completeAssets() {
  128. if (this->headImageMethod.methodIndex == -1) {
  129. // Work as a sub-menu if the direct parent is also a menu.
  130. this->subMenu = this->parent != nullptr && dynamic_cast<Menu*>(this->parent) != nullptr;
  131. this->headImageMethod = theme_getScalableImage(theme_getDefault(), this->subMenu ? U"MenuSub" : U"MenuTop");
  132. this->listBackgroundImageMethod = theme_getScalableImage(theme_getDefault(), U"MenuList");
  133. }
  134. if (this->font.get() == nullptr) {
  135. this->font = font_getDefault();
  136. }
  137. }
  138. void Menu::changedLocation(const IRect &oldLocation, const IRect &newLocation) {
  139. // If the component has changed dimensions then redraw the image
  140. if (oldLocation.size() != newLocation.size()) {
  141. this->hasImages = false;
  142. }
  143. }
  144. void Menu::changedAttribute(const ReadableString &name) {
  145. if (!string_caseInsensitiveMatch(name, U"Visible")) {
  146. this->hasImages = false;
  147. }
  148. }
  149. void Menu::lostFocus() {
  150. // Hide the menu when losing focus.
  151. this->showingOverlay = false;
  152. // Erase the list image to save memory.
  153. this->listBackgroundImage = OrderedImageRgbaU8();
  154. }
  155. void Menu::updateLocationEvent(const IRect& oldLocation, const IRect& newLocation) {
  156. int left = this->padding.value;
  157. int top = this->padding.value;
  158. int overlap = 3;
  159. if (this->subMenu) {
  160. left += newLocation.width() - overlap;
  161. } else {
  162. top += newLocation.height() - overlap;
  163. }
  164. int maxWidth = newLocation.width() - (this->padding.value * 2);
  165. for (int i = 0; i < this->getChildCount(); i++) {
  166. int width = this->children[i]->getDesiredDimensions().x;
  167. if (maxWidth < width) maxWidth = width;
  168. }
  169. for (int i = 0; i < this->getChildCount(); i++) {
  170. int height = this->children[i]->getDesiredDimensions().y;
  171. this->children[i]->applyLayout(IRect(left, top, maxWidth, height));
  172. top += height + this->spacing.value;
  173. }
  174. }
  175. static void closeEntireMenu(VisualComponent* menu) {
  176. while (menu->parent != nullptr) {
  177. menu->showingOverlay = false;
  178. menu = menu->parent;
  179. }
  180. }
  181. void Menu::receiveMouseEvent(const MouseEvent& event) {
  182. int childCount = this->getChildCount();
  183. IVector2D positionFromParent = event.position;
  184. MouseEvent localEvent = event;
  185. localEvent.position -= this->location.upperLeft();
  186. if (this->showingOverlay && this->pointIsInsideOfOverlay(event.position)) {
  187. for (int i = childCount - 1; i >= 0; i--) {
  188. if (this->children[i]->pointIsInside(localEvent.position)) {
  189. this->children[i]->makeFocused();
  190. MouseEvent childEvent = localEvent;
  191. childEvent.position -= this->children[i]->location.upperLeft();
  192. this->children[i]->sendMouseEvent(childEvent, true);
  193. break;
  194. }
  195. }
  196. } else if (this->pointIsInside(event.position)) {
  197. if (childCount > 0) { // Has a list of members to open, toggle expansion when clicked.
  198. if (this->subMenu) { // Menu within another menu.
  199. // Hover to expand sub-menu's list.
  200. if (event.mouseEventType == MouseEventType::MouseMove && !this->showingOverlay) {
  201. this->showOverlay();
  202. }
  203. } else { // Top menu, which is usually placed in a toolbar.
  204. bool toggleExpansion = false;
  205. if (event.mouseEventType == MouseEventType::MouseDown) {
  206. // Toggle expansion when headImageMethod is clicked.
  207. toggleExpansion = true;
  208. } else if (event.mouseEventType == MouseEventType::MouseMove && !this->showingOverlay) {
  209. // Automatically expand hovered top-menus neighboring an opened top menu.
  210. if (this->parent != nullptr) {
  211. if (this->parent->focusComponent.get() != nullptr && this->parent->focusComponent->showingOverlay) {
  212. toggleExpansion = true;
  213. }
  214. }
  215. }
  216. if (toggleExpansion) {
  217. // Menu components with child members will toggle visibility for its list when pressed.
  218. if (this->showingOverlay) {
  219. closeEntireMenu(this);
  220. } else {
  221. this->showOverlay();
  222. }
  223. }
  224. }
  225. } else { // List item, because it has no children.
  226. // Childless menu components are treated as menu items that can be clicked to perform an action and close the menu.
  227. if (event.mouseEventType == MouseEventType::MouseDown) {
  228. // Hide overlays all the way to root.
  229. closeEntireMenu(this);
  230. // Call the event assigned to this menu item.
  231. this->callback_pressedEvent();
  232. }
  233. }
  234. // Because the main body was interacted with, the mouse events are passed on.
  235. VisualComponent::receiveMouseEvent(event);
  236. }
  237. }
  238. IVector2D Menu::getDesiredDimensions() {
  239. this->completeAssets();
  240. int sizeAdder = this->padding.value * 2;
  241. return IVector2D(font_getLineWidth(this->font, this->text.value) + sizeAdder, font_getSize(this->font) + sizeAdder);
  242. }