💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
2.4 KiB

  1. /*
  2. * Copyright (C) 2017-2019 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper Source Code.
  5. *
  6. * Antkeeper Source Code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper Source Code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper Source Code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "menu.hpp"
  20. MenuItem::MenuItem():
  21. activatedCallback(nullptr)
  22. {
  23. container = new UIContainer();
  24. container->setAnchor(Anchor::TOP_LEFT);
  25. nameLabel = new UILabel();
  26. nameLabel->setAnchor(Anchor::TOP_LEFT);
  27. valueLabel = new UILabel();
  28. valueLabel->setAnchor(Anchor::TOP_RIGHT);
  29. container->addChild(nameLabel);
  30. container->addChild(valueLabel);
  31. }
  32. MenuItem::~MenuItem()
  33. {
  34. delete container;
  35. delete nameLabel;
  36. delete valueLabel;
  37. }
  38. void MenuItem::setFont(Font* font)
  39. {
  40. nameLabel->setFont(font);
  41. valueLabel->setFont(font);
  42. }
  43. void MenuItem::setName(const std::string& name)
  44. {
  45. nameLabel->setText(name);
  46. }
  47. void MenuItem::setValue(const std::string& value)
  48. {
  49. valueLabel->setText(value);
  50. }
  51. void MenuItem::setActivatedCallback(std::function<void()> callback)
  52. {
  53. activatedCallback = callback;
  54. }
  55. void MenuItem::activate()
  56. {
  57. if (activatedCallback)
  58. {
  59. activatedCallback();
  60. }
  61. }
  62. Menu::Menu()
  63. {
  64. container = new UIContainer();
  65. }
  66. Menu::~Menu()
  67. {
  68. removeItems();
  69. delete container;
  70. }
  71. MenuItem* Menu::addItem()
  72. {
  73. MenuItem* item = new MenuItem();
  74. container->addChild(item->container);
  75. items.push_back(item);
  76. return item;
  77. }
  78. void Menu::removeItems()
  79. {
  80. for (MenuItem* item: items)
  81. {
  82. container->removeChild(item->container);
  83. delete item;
  84. }
  85. items.clear();
  86. }
  87. void Menu::setFonts(Font* font)
  88. {
  89. for (MenuItem* item: items)
  90. {
  91. item->setFont(font);
  92. }
  93. }
  94. void Menu::resize(int w, int h)
  95. {
  96. container->setDimensions(Vector2(w, h));
  97. int spacing = h / items.size();
  98. int offset = 0;
  99. for (MenuItem* item: items)
  100. {
  101. item->container->setTranslation(Vector2(0, offset));
  102. item->container->setDimensions(Vector2(w, item->getNameLabel()->getFont()->getMetrics().getHeight()));
  103. offset += spacing;
  104. }
  105. }